Search code examples
cmacrossystem-calls

Macro to call either raw syscall or libc wrapper


I would like to have macro, which would based on first parameter call either raw syscall or libc wrapper. e.g.

#define SYSCALL_VARIANT(variant, scall, ...) \
    do { \
        if (variant) \
            syscall(__NR_ scall,  ##__VA_ARGS__); \
        else \
            scall(##__VA_ARGS__); \
    } while (0)

if (SYSCALL_VARIANT(0, pkey_mprotect, buffer, size, prot, pkey)) == -1)
    ...

I have no idea how to join __NR_ and scall to be e.g. __NR_pkey_mprotect. Is it even possible?


Solution

  • A correct way should be:

    #define SYSCALL_VARIANT(variant, scall, ...)       \
        do {                                           \
            if (variant)                               \
                syscall(__NR_##scall,  ##__VA_ARGS__); \
            else                                       \
                scall(__VA_ARGS__);                    \
        } while (0)
    
    if (SYSCALL_VARIANT(0, pkey_mprotect, buffer, size, prot, pkey)) == -1)
        ...
    

    (the __VA_ARGS__ only needs ## when the previous token is a comma , to eliminate the comma if the arglist is empty, e.g. if you call fork() as SYSCALL_VARIANT(0,fork))