与 child 中的 child 互动
Interact with child of child
我需要直接与使用 subprocess
生成的进程的 stdin
和 stdout
进行交互。我可以这样做:
proc = subprocess.Popen("/bin/bash", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
proc.stdin.write(b"whoami\n")
print(proc.stdout.readline())
但是,一旦进程分叉,我就无法再与其交互。我有这个测试程序:
// child.c
#include <unistd.h>
int main() {
execve("/bin/bash", NULL, NULL);
}
我用它编译:
gcc -o child child.c
当我在 Popen
调用中使用 child
而不是 /bin/bash
尝试上面的 python 代码时,我得到一个 broken pipe
错误。
我尝试了什么:
由于 python 创建的文件描述符默认情况下不可继承,我尝试更改它:
inr, inw = os.pipe()
outr, outw = os.pipe()
os.set_inheritable(inr, True)
os.set_inheritable(inw, True)
os.set_inheritable(outr, True)
os.set_inheritable(outw, True)
proc = subprocess.Popen("./child", stdin=inr, stdout=outw, stderr=outw)
proc.stdin.write(b"whoami\n")
print(proc.stdout.readline())
os.close(inr)
os.close(inw)
os.close(outr)
os.close(outw)
但我仍然得到同样的错误。我认为这应该是一件容易的事,而且我可能遗漏了一些东西。感谢任何帮助。
[编辑] 在我编辑我的 post 之前,我使用 test
而不是 child
作为我的测试 c
可执行文件的名称。我了解到它确实是我的PATH中的一个程序,位于/usr/bin/test
.
将可执行文件名称更改为 child
成功解决了我的问题。经过数小时的反复试验,我知道答案会很简单...
非常感谢 Barmar 指出这一点!
当您调用 test
时,您调用的是 /bin/test
,而不是您的程序。
要么重命名您的 C 程序,使其不与标准命令冲突,要么使用路径名访问它,使其不搜索 $PATH
。
proc = subprocess.Popen("./test", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
我需要直接与使用 subprocess
生成的进程的 stdin
和 stdout
进行交互。我可以这样做:
proc = subprocess.Popen("/bin/bash", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
proc.stdin.write(b"whoami\n")
print(proc.stdout.readline())
但是,一旦进程分叉,我就无法再与其交互。我有这个测试程序:
// child.c
#include <unistd.h>
int main() {
execve("/bin/bash", NULL, NULL);
}
我用它编译:
gcc -o child child.c
当我在 Popen
调用中使用 child
而不是 /bin/bash
尝试上面的 python 代码时,我得到一个 broken pipe
错误。
我尝试了什么:
由于 python 创建的文件描述符默认情况下不可继承,我尝试更改它:
inr, inw = os.pipe()
outr, outw = os.pipe()
os.set_inheritable(inr, True)
os.set_inheritable(inw, True)
os.set_inheritable(outr, True)
os.set_inheritable(outw, True)
proc = subprocess.Popen("./child", stdin=inr, stdout=outw, stderr=outw)
proc.stdin.write(b"whoami\n")
print(proc.stdout.readline())
os.close(inr)
os.close(inw)
os.close(outr)
os.close(outw)
但我仍然得到同样的错误。我认为这应该是一件容易的事,而且我可能遗漏了一些东西。感谢任何帮助。
[编辑] 在我编辑我的 post 之前,我使用 test
而不是 child
作为我的测试 c
可执行文件的名称。我了解到它确实是我的PATH中的一个程序,位于/usr/bin/test
.
将可执行文件名称更改为 child
成功解决了我的问题。经过数小时的反复试验,我知道答案会很简单...
非常感谢 Barmar 指出这一点!
当您调用 test
时,您调用的是 /bin/test
,而不是您的程序。
要么重命名您的 C 程序,使其不与标准命令冲突,要么使用路径名访问它,使其不搜索 $PATH
。
proc = subprocess.Popen("./test", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)