代表用户进程在kthread中打开文件
我正在编写一个Linux内核模块,当用户进程调用它时(使用ioctl),它将启动一个kthread。
我怎样才能使用这个kthread代表用户进程打开一个文件,也就是说,当它返回时,用户进程可以访问这个文件本身!?
I am writing a linux kernel module which would start a kthread when a user process calls to it (using ioctl).
How can i open a file using this kthread on bahalf of user process, that is, when it returns the user process can access this file itself!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样做其实不太明智。要打开用户空间进程可以读取的文件,您需要向该进程返回一个文件描述符。
您可能会返回一个将内核线程连接到用户空间线程的 UNIX 域套接字对,并让内核线程使用
SCM_RIGHTS
消息在该套接字上传递打开的文件描述符。然而,更合适的做法是简单地在 ioctl() 调用中在原始进程的上下文中打开文件并在那里返回文件描述符。
It's not really sensible to do this. To open a file that the userspace process can read, you need to return a file descriptor to that process.
Potentially you could return a UNIX-domain socketpair connecting the kernel thread to the userspace thread, and have the kernel thread pass open file descriptors across that socket using a
SCM_RIGHTS
message.It is likely to be more appropriate, however, to simply open the file in the context of the original process in the
ioctl()
call and return the file descriptor there.