wlformyd
wlformyd

Reputation: 237

How to safely use new Linux functions?

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?

Upvotes: 6

Views: 176

Answers (2)

Andy Ross
Andy Ross

Reputation: 12043

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.

Upvotes: 2

T Percival
T Percival

Reputation: 8674

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.

Upvotes: 3

Related Questions