How to make system call from another system call in kernel space

≯℡__Kan透↙ 提交于 2019-12-02 13:35:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!