Linux:我可以在不使用任何IPC(管道等)的情况下读取另一个进程的输出吗?
在linux中是否有可能以某种方式读取另一个进程的输出(来自stdout和stderr)而不知道它?假设我有一个进程 A 在后台运行,进程 B 想要读取其输出 - 这可能吗?我无法使用管道或屏幕程序。我尝试从 /proc/xxx/fd 或 /pts/x 控制台等读取内容,但到目前为止没有任何效果。
Is it possible in linux to somehow read the output (from stdout and stderr) of another process without it knowing about it? So lets say I have a process A running in the background and process B wants to read its output - is it possible? I can't use pipes or the screen program. I tried reading from /proc/xxx/fd or from /pts/x consoles and so on, but nothing worked so far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在内核中,我想您可以编写一个驱动程序来挂钩读取和写入以获得您想要的东西。
在用户空间中,您可以编译一个修改后的 glibc,它会注销 stdout 和 stdout。例如,stderr 输出到某个文件以及进程和线程 ID。但如果你破坏了某些东西,那就有风险了。 (假设您要跟踪的应用程序没有静态链接或直接对内核进行系统调用)
In the kernel I guess you could write a driver that hooks the reads and writes to get what you want.
In User space you could compile a modified glibc which logs out stdout & stderr output to some file along with the process and thread ID for example. But that's risky if you break something. (assuming applications you want to trace are not linked statically or make direct syscalls to the kernel)
我读到你的问题的含义是你不打算编写内核代码,并且这个想法不是修改你正在监视的可执行文件。
考虑到这些限制,答案很简单。不,你不能。该进程调用 write(1 或 write(2),这些可以去任何地方,并且系统中没有内置的“窃听”功能来帮助您查看途中的流量。
I read the implication of your question that you're not about to write kernel code, and that the idea is not to modify the executable that you are spying upon.
Given those constraints, the answer is simple. No. You cannot. The process calls write(1, or write(2, and those could go anywhere, and there's no 'wiretap' provision built into the system to help you see the traffic on the way.
只需使用 dup2 函数即可:
By simply using the dup2 function :