我有一个可执行的二进制文件(无源),可以克隆()。它没有打印任何东西,我认为孩子正在尝试打印任何东西,我希望它这样做。即使子进程实际上失败了,它也总是成功返回。我想要孩子的退货代码。提示也会立即返回,但我想要在前台运行的东西;要么通过神奇地取消分叉,要么等待子进程终止。
我能做些什么?仅Linux就可以了。
我尝试了这样的脚本,至少获得输出并留在前台:
thecmd &
daemon="$(pgrep -P $!)"
cat "/proc/$daemon/fd/2" >&2
但在我得到它之前,孩子可能会失败并且没有输出。
我试图获取守护进程和标准输出:
cmd &
daemon="$(pgrep -P $!)"
echo "daemon: $daemon"
echo "parent's stdout:"
readlink -f "/proc/$!/fd/1"
echo "child's stdout:"
readlink -f "/proc/$daemon/fd/1"
印刷:
daemon:
parent's stdout:
child's stdout:
这是strace -f
子进程失败的输出(为什么没有 stderr?):
23266 execve("/usr/local/sbin/tobiiusbserviced", ["/usr/local/sbin/tobiiusbserviced"], 0x7ffc21846b08 /* 17 vars */) = 0
23266 brk(NULL) = 0x2368000
23266 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
23266 openat(AT_FDCWD, "/usr/local/lib/tobiiusb/tls/haswell/x86_64/libtobii_osal.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
23266 stat("/usr/local/lib/tobiiusb/tls/haswell/x86_64", 0x7ffdbcb20d80) = -1 ENOENT (No such file or directory)
23266 openat(AT_FDCWD, "/usr/local/lib/tobiiusb/tls/haswell/libtobii_osal.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
23266 stat("/usr/local/lib/tobiiusb/tls/haswell", 0x7ffdbcb20d80) = -1 ENOENT (No such file or directory)
23266 openat(AT_FDCWD, "/usr/local/lib/tobiiusb/tls/x86_64/libtobii_osal.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
23266 stat("/usr/local/lib/tobiiusb/tls/x86_64", 0x7ffdbcb20d80) = -1 ENOENT (No such file or directory)
23266 openat(AT_FDCWD, "/usr/local/lib/tobiiusb/tls/libtobii_osal.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
23266 stat("/usr/local/lib/tobiiusb/tls", 0x7ffdbcb20d80) = -1 ENOENT (No such file or directory)
23266 openat(AT_FDCWD, "/usr/local/lib/tobiiusb/haswell/x86_64/libtobii_osal.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
23266 stat("/usr/local/lib/tobiiusb/haswell/x86_64", 0x7ffdbcb20d80) = -1 ENOENT (No such file or directory)
23266 openat(AT_FDCWD, "/usr/local/lib/tobiiusb/haswell/libtobii_osal.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
23266 stat("/usr/local/lib/tobiiusb/haswell", 0x7ffdbcb20d80) = -1 ENOENT (No such file or directory)
23266 openat(AT_FDCWD, "/usr/local/lib/tobiiusb/x86_64/libtobii_osal.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
23266 stat("/usr/local/lib/tobiiusb/x86_64", 0x7ffdbcb20d80) = -1 ENOENT (No such file or directory)
23266 openat(AT_FDCWD, "/usr/local/lib/tobiiusb/libtobii_osal.so", O_RDONLY|O_CLOEXEC) = 3
23266 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0@6\0\0\0\0\0\0"..., 832) = 832
23266 fstat(3, {st_mode=S_IFREG|0644, st_size=54270, ...}) = 0
23266 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa9213b8000
23266 mmap(NULL, 2141088, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fa9211ad000
23266 mprotect(0x7fa9211b7000, 2093056, PROT_NONE) = 0
23266 mmap(0x7fa9213b6000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x9000) = 0x7fa9213b6000
23266 close(3) = 0
23266 openat(AT_FDCWD, "/usr/local/lib/tobiiusb/libtobii_libc.so", O_RDONLY|O_CLOEXEC) = 3
23266 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\300\"\0\0\0\0\0\0"..., 832) = 832
23266 fstat(3, {st_mode=S_IFREG|0644, st_size=56335, ...}) = 0
23266 mmap(NULL, 2161696, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fa920f9d000
23266 mprotect(0x7fa920fa8000, 2093056, PROT_NONE) = 0
23266 mmap(0x7fa9211a7000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xa000) = 0x7fa9211a7000
23266 mmap(0x7fa9211a9000, 15392, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fa9211a9000
23266 close(3) = 0
23266 openat(AT_FDCWD, "/usr/local/lib/tobiiusb/libudev.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
23266 openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
23266 fstat(3, {st_mode=S_IFREG|0644, st_size=81564, ...}) = 0
23266 mmap(NULL, 81564, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fa920f89000
23266 close(3) = 0
23266 openat(AT_FDCWD, "/usr/lib/libudev.so.1", O_RDONLY|O_CLOEXEC) = 3
23266 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\200Y\0\0\0\0\0\0"..., 832) = 832
23266 fstat(3, {st_mode=S_IFREG|0755, st_size=158016, ...}) = 0
23266 mmap(NULL, 162056, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fa920f61000
23266 mprotect(0x7fa920f66000, 135168, PROT_NONE) = 0
23266 mmap(0x7fa920f66000, 94208, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x5000) = 0x7fa920f66000
23266 mmap(0x7fa920f7d000, 36864, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c000) = 0x7fa920f7d000
23266 mmap(0x7fa920f87000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x25000) = 0x7fa920f87000
23266 close(3) = 0
23266 openat(AT_FDCWD, "/usr/local/lib/tobiiusb/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
23266 openat(AT_FDCWD, "/usr/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
23266 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0@\177\2\0\0\0\0\0"..., 832) = 832
23266 fstat(3, {st_mode=S_IFREG|0755, st_size=12976264, ...}) = 0
23266 mmap(NULL, 1856160, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fa920d9b000
23266 mmap(0x7fa920dc1000, 1351680, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x26000) = 0x7fa920dc1000
23266 mmap(0x7fa920f0b000, 311296, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x170000) = 0x7fa920f0b000
23266 mmap(0x7fa920f57000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1bb000) = 0x7fa920f57000
23266 mmap(0x7fa920f5d000, 12960, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fa920f5d000
23266 close(3) = 0
23266 openat(AT_FDCWD, "/usr/local/lib/tobiiusb/libtobii_usb.so", O_RDONLY|O_CLOEXEC) = 3
23266 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\300\30\0\0\0\0\0\0"..., 832) = 832
23266 fstat(3, {st_mode=S_IFREG|0644, st_size=33286, ...}) = 0
23266 mmap(NULL, 2122072, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fa920b94000
23266 mprotect(0x7fa920b9a000, 2093056, PROT_NONE) = 0
23266 mmap(0x7fa920d99000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x5000) = 0x7fa920d99000
23266 close(3) = 0
23266 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa920b92000
23266 arch_prctl(ARCH_SET_FS, 0x7fa920b93040) = 0
23266 mprotect(0x7fa920f57000, 12288, PROT_READ) = 0
23266 mprotect(0x7fa920d99000, 4096, PROT_READ) = 0
23266 mprotect(0x7fa920f87000, 4096, PROT_READ) = 0
23266 mprotect(0x7fa9213b6000, 4096, PROT_READ) = 0
23266 mprotect(0x7fa9211a7000, 4096, PROT_READ) = 0
23266 mprotect(0x604000, 4096, PROT_READ) = 0
23266 mprotect(0x7fa9213e4000, 4096, PROT_READ) = 0
23266 munmap(0x7fa920f89000, 81564) = 0
23266 clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7fa920b93310) = 23267
23266 exit_group(0 <unfinished ...>
23267 umask(000 <unfinished ...>
23266 <... exit_group resumed>) = ?
23267 <... umask resumed>) = 022
23267 setsid( <unfinished ...>
23266 +++ exited with 0 +++
23267 <... setsid resumed>) = 23267
23267 brk(NULL) = 0x2368000
23267 brk(0x2389000) = 0x2389000
23267 openat(AT_FDCWD, "/var/run/tobiiusb/tobiiusbservice.pid", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
23267 fstat(3, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
23267 write(3, "23267", 5) = 5
23267 close(3) = 0
23267 rt_sigaction(SIGTERM, {sa_handler=0x4023e0, sa_mask=[], sa_flags=SA_RESTORER|SA_SIGINFO, sa_restorer=0x7fa920dd8000}, NULL, 8) = 0
23267 rt_sigaction(SIGUSR1, {sa_handler=0x402c70, sa_mask=[USR1], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fa920dd8000}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
23267 rt_sigaction(SIGUSR2, {sa_handler=0x402d30, sa_mask=[USR2], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fa920dd8000}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0
23267 chdir("/") = 0
23267 close(0) = 0
23267 close(1) = 0
23267 close(2) = 0
23267 openat(AT_FDCWD, "/etc/udev/udev.conf", O_RDONLY|O_CLOEXEC) = 0
23267 fstat(0, {st_mode=S_IFREG|0644, st_size=49, ...}) = 0
23267 read(0, "# see udev.conf(5) for details\n\n"..., 4096) = 49
23267 read(0, "", 4096) = 0
23267 close(0) = 0
23267 access("/run/udev/control", F_OK) = 0
23267 socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT) = 0
23267 setsockopt(0, SOL_SOCKET, SO_ATTACH_FILTER, {len=10, filter=0x7ffdbcb20a20}, 16) = 0
23267 bind(0, {sa_family=AF_NETLINK, nl_pid=0, nl_groups=0x000002}, 12) = 0
23267 getsockname(0, {sa_family=AF_NETLINK, nl_pid=23267, nl_groups=0x000002}, [12]) = 0
23267 setsockopt(0, SOL_SOCKET, SO_PASSCRED, [1], 4) = 0
23267 openat(AT_FDCWD, "/sys/devices/system/cpu/online", O_RDONLY|O_CLOEXEC) = 1
23267 read(1, "0-3\n", 8192) = 4
23267 close(1) = 0
23267 sched_get_priority_max(SCHED_FIFO) = 99
23267 sched_get_priority_min(SCHED_FIFO) = 1
23267 writev(2, [{iov_base="/usr/local/sbin/tobiiusbserviced", iov_len=32}, {iov_base=": ", iov_len=2}, {iov_base="symbol lookup error", iov_len=19}, {iov_base=": ", iov_len=2}, {iov_base="/usr/local/lib/tobiiusb/libtobii"..., iov_len=40}, {iov_base=": ", iov_len=2}, {iov_base="undefined symbol: pthread_create", iov_len=32}, {iov_base="", iov_len=0}, {iov_base="", iov_len=0}, {iov_base="\n", iov_len=1}], 10) = -1 EBADF (Bad file descriptor)
23267 exit_group(127) = ?
23267 +++ exited with 127 +++
答案1
您的访问尝试 可能是徒劳的。
/proc/pid/fd/N
只要您对程序的唯一访问权限是黑盒,运行
strace -f
可能是您能做的最好的事情。 (然后strace
在输出中搜索exit
和write
。)类似的选项是尝试在调试器下运行程序。如果您想自动化此操作,请尝试确定明确的需求,尽您所能,然后在遇到困难时提出新问题。也许最好的解决方案是获得白盒访问;即,获取源代码。使用反向编译器从二进制文件生成源代码可能是一种选择。
查看
strace
您提供的输出,我们可以看到- 子进程使用文件描述符3写入“
23267
”/var/run/tobiiusb/tobiiusbservice.pid
。这实际上是子进程的PID。 - 它使用系统调用将“/usr/local/sbin/tobiiusbserviced:符号查找错误:/usr/local/lib/tobiiusb/libtobii…:未定义符号:pthread_create\n”写入文件描述符2
writev
。上面字符串中的“...”并不是字面上的三个点,而是表示已被截断的文本,以防止输出过于冗长。要查看完整数据,请尝试将--verbose=write
或--verbose='/write*'
选项传递给strace
。和/或--write=all
(或--write=0,1,2,3,4,5,6,7
,如果“all
”不起作用)。 - 上述错误消息没有出现在任何地方,因为程序之前已经关闭了文件描述符 0、1 和 2。
- 正如您所怀疑的,子进程以状态 127 退出(使用
exit_group
系统调用)。
- 子进程使用文件描述符3写入“