Skip to content

Instantly share code, notes, and snippets.

@cwshu
Created April 1, 2017 18:18
Show Gist options
  • Save cwshu/578d9e80c9d969efcd32fb8f156beb64 to your computer and use it in GitHub Desktop.
Save cwshu/578d9e80c9d969efcd32fb8f156beb64 to your computer and use it in GitHub Desktop.
Linux Kernel system call macro magic 1: syscall arguments
#define SYSCALL_DEFINE2(name, ...) SYSCALL_DEFINEx(2, _##name, __VA_ARGS__)
#define SYSCALL_DEFINEx(x, name, ...) \
static inline long SYSC##name(__MAP(x,__SC_DECL,__VA_ARGS__))
#define __SC_DECL(t, a) t a
#define __SC_ARGS(t, a) a
#define __MAP0(m,...)
#define __MAP1(m,t,a) m(t,a)
#define __MAP2(m,t,a,...) m(t,a), __MAP1(m,__VA_ARGS__)
#define __MAP3(m,t,a,...) m(t,a), __MAP2(m,__VA_ARGS__)
#define __MAP4(m,t,a,...) m(t,a), __MAP3(m,__VA_ARGS__)
#define __MAP5(m,t,a,...) m(t,a), __MAP4(m,__VA_ARGS__)
#define __MAP6(m,t,a,...) m(t,a), __MAP5(m,__VA_ARGS__)
#define __MAP(n,...) __MAP##n(__VA_ARGS__)
SYSCALL_DEFINE2(add, int, a, int, b){
return a + b;
}
// real call isn't in Linux Kernel. it's just my practice.
#define REAL_CALL(x, name, ... ) \
real_##SYSC_##name(__MAP(x,__SC_ARGS,__VA_ARGS__));
REAL_CALL(2, add, int, a, int, b)
# 1 "s1.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "s1.c"
# 19 "s1.c"
static inline long SYSC_add(int a, int b){
return a + b;
}
real_SYSC_add(a, b);
@cwshu
Copy link
Author

cwshu commented Apr 1, 2017

use techinique from /include/linux/syscall.h

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment