Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does an undefined system call return -1?

I have defined a 'helloworld' system call in my Linux kernel and recompiled it. The code for the system call is:

#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/sched.h>
#include<linux/syscalls.h>
#include "processInfo.h"
asmlinkage long sys_listProcessInfo(void)
{
    printk("Hello World. My new syscall..FOSS Lab!\n");
    return 0;
}

But when I'm calling this system call from the same operating system with another kernel version, which does not include this system call, using the below code:

#include<stdio.h>
#include<linux/kernel.h>
#include<sys/syscall.h>
#include<unistd.h>
int main()
{
    long int var = syscall(326);
    printf("Returning: %ld\n",var);
    return 0;
}

The variable var gets the value -1. I would like to know how var gets -1 instead of displaying an error.

like image 206
tonyjosi Avatar asked Oct 18 '22 13:10

tonyjosi


1 Answers

Why do you expect an error? The syscall function exists, the linker can resolve it. So there won't be an error from the compiler or linker. When you run the executable, the old kernel's syscall function detects that 326 is an invalid system call number and the functions returns -1, probably with errno set to ENOSYS = system call not implemented.

like image 162
Jens Avatar answered Oct 21 '22 03:10

Jens