Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to safely use new Linux functions?

Tags:

c

linux

I'm writing code that uses sched_setaffinity, which requires kernel 2.5.8 or later. I've been trying to find out if these things are possible:

  1. Systems with older kernels to compile this gracefully, perhaps just ignoring that code segment entirely.
  2. If I send someone with an older kernel a compiled binary, it will step over this function or simply print a warning.

I guess my question is, how do you use new kernel functions safely, without breaking the entire application when using an older system?

like image 285
wlformyd Avatar asked May 23 '12 21:05

wlformyd


People also ask

How do you execute a function in Linux?

using function keyword : A function in linux can be declared by using keyword function before the name of the function. Different statements can be separated by a semicolon or a new line.

What happens when you press Ctrl Z in Linux?

ctrl+z stops the process and returns you to the current shell. You can now type fg to continue process, or type bg to continue the process in the background.


2 Answers

Use dlopen() with NULL as the filename, and dlsym() the function you want to use. If the dlsym() succeeds, call the function through the function pointer that was returned.

like image 120
Ted Percival Avatar answered Oct 19 '22 01:10

Ted Percival


Are you trying to get your program to link or to run? You can invoke the system call directly via the glibc syscall() function without needing a recent C library. Obviously it's going to fail on earlier systems without support (a quick test shows the kernel returns -1 == ENOSYS for unimplemented syscall numbers), so you will need to test for that.

like image 35
Andy Ross Avatar answered Oct 18 '22 23:10

Andy Ross