问题
I am new in Linux kernel development. I have implemented a system call say my_pid in linux kernel 2.6. I want to call getpid system call from my system call. How can I do it?
I want something like:
pid_t my_pid(){ return getpid(); }
Also from C in user-space I can call any system call using: syscall(); What is the generic way to do this in kernel mode?
回答1:
There is no generic way of doing this.
If you are in kernel space, you should invoke kernel functions that implement the system call functionality directly instead of using syscall
-type instructions, or use other means of extracting the desired information / affecting the desired action.
For the specific case of getpid()
, you can simply use current->pid
.
The kernel name current
is always a pointer to the current task_struct
, which is defined via <linux/sched.h> (search for struct task_struct
). Code that accesses members of that usually gets inlined, i.e. there's not even a function call (and much less a system call) required to get these when your code is running as part of the kernel.
来源:https://stackoverflow.com/questions/7894359/how-to-make-system-call-from-another-system-call-in-kernel-space