longbowk
longbowk

Reputation: 229

How to detect if a syscall exists on linux programmatically?

I am trying to compile a C program that will be run on some older version of kernel. The kernel where I compile the program is not the same version as the kernel version where my program will be run. If I call a syscall that only on newer version of kernel, will the program crash, or return a error value?

Upvotes: 4

Views: 3793

Answers (1)

Wintermute
Wintermute

Reputation: 44063

That depends. It is rather rare that system calls are invoked directly (the syscalls (2) man page even makes note of this); it is more common to use library wrappers such as write, read, open, close and others. If you attempt to to use a wrapper function for a syscall that doesn't exist on the system in question, it is likely that the wrapper also does not exist, in which case your program will not get past the runtime linker and fail to start.

If, on the other hand, you invoke system calls (more or less) directly through the syscall function, e.g.

syscall(SYS_write, fd, buffer, size);

instead of

write(fd, buffer, size);

then syscall will return -1 if you attempt to invoke a nonexistent system call, and errno will be set to ENOSYS.

If you use a libc that is new enough to contain the wrapper function with a kernel that is old enough to not support the corresponding system call, the wrapper function will probably hand down this error. Check the documentation of the wrapper function in question.

Upvotes: 9

Related Questions