Linux:我可以在不使用任何IPC(管道等)的情况下读取另一个进程的输出吗?

发布于 2024-09-07 14:31:33 字数 156 浏览 1 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二货你真萌 2024-09-14 14:31:33

在内核中,我想您可以编写一个驱动程序来挂钩读取和写入以获得您想要的东西。

在用户空间中,您可以编译一个修改后的 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)

安穩 2024-09-14 14:31:33

我读到你的问题的含义是你不打算编写内核代码,并且这个想法不是修改你正在监视的可执行文件。

考虑到这些限制,答案很简单。不,你不能。该进程调用 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.

本王不退位尔等都是臣 2024-09-14 14:31:33

只需使用 dup2 函数即可:

int b_fd; /* This is the B process File descriptor*/
int a_fd /* This is the A process File descriptor*/  

 int main (int argc, char*argv[]){
     /** I suppose that you can init the file descriptor for A*/
     dup2( b_fd, a_fd);
     /**Now everything that A will output will be written in B file descriptor*/

  }

By simply using the dup2 function :

int b_fd; /* This is the B process File descriptor*/
int a_fd /* This is the A process File descriptor*/  

 int main (int argc, char*argv[]){
     /** I suppose that you can init the file descriptor for A*/
     dup2( b_fd, a_fd);
     /**Now everything that A will output will be written in B file descriptor*/

  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文