put_user() Linux 内核
执行后 put_user(message[i], buf+i);
如何从用户空间访问消息?
我真的不明白从哪里访问字符串消息以及我可以用它做什么?
After doing put_user(message[i], buf+i);
how can I access message from user space?
I really don't understand where the string message is to be accessed from and what I can do with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
put_user()
只能在进行系统调用的进程上下文中调用。考虑一个调用
ptrace(2)
的应用程序(请参阅kernel/ptrace.c
)。内核将调用特定于体系结构的
ptrace
帮助程序:在 x86 平台上,
arch_ptrace()
定义在arch/x86/kernel/ptrace.c< /code>:
当进程调用
ptrace(2)
并要求执行PTRACE_PEEKUSR
时,内核需要返回信息(ret
)给用户。内核使用指向用户提供的缓冲区的datap
指针来了解在进程中的何处写入tmp
的值。几乎每次调用
put_user()
的情况都会由用户态进程启动。向用户空间发送信号是一个明显的区别,内核启动发送信号,但内核有代码(请参阅arch/x86/kernel/signal.c函数__setup_frame()) 找到堆栈帧并将需要处理的信号写入其中。
因此,经过冗长的讨论:您将通过您提供给内核写入的任何缓冲区来访问进程中的数据 - 它可能是特定于驱动程序的
缓冲区ioctl(2)
,它可能是您创建的新系统调用的缓冲区参数,您有很多选择。put_user()
should only be called in the context of a process making a system call.Consider an application calling
ptrace(2)
(seekernel/ptrace.c
).The kernel will call into an architecture-specific
ptrace
helper:On the x86 platform,
arch_ptrace()
is defined inarch/x86/kernel/ptrace.c
:When a process calls
ptrace(2)
and asks to perform aPTRACE_PEEKUSR
, the kernel needs to return information (ret
) back to the user. The kernel uses thedatap
pointer to a user supplied buffer to know where in the process to write the value oftmp
.Almost every case of calling
put_user()
will be initiated by a userland process. Sending signals to userspace is an obvious difference, where the kernel initiates sending the signal, but the kernel has code (seearch/x86/kernel/signal.c
function__setup_frame()
) to find the stack frame and write into it the need to handle a signal.So, after a long-winded discussion: You will access your data in your process via whatever buffer you gave to the kernel to write into -- it could be a buffer for a driver-specific
ioctl(2)
, it could be a buffer argument to a new system call you create, you have a lot of choices.put_user(x, ptr)
。这里 x 是要复制到用户空间的值,而 ptr 是用户空间中的目标地址。因此,在用户应用程序中,可以通过
buf+i
访问该消息。put_user (x, ptr)
. Herex
is the value to copy to user space, whileptr
is the destination address, in user space.So in user application the message can be accessed by
buf+i
.