当从进程写入文件描述符时,如何打开文件描述符并将其回显到终端?
我有一个备份程序 Duplicity,它将其日志写入参数指定的文件描述符--log-fd=16
。
果然,如果我运行lsof -p <duplicity PID>
我会看到:
python2 9224 myuser 0r CHR 1,3 0t0 6 /dev/null
python2 9224 myuser 1w CHR 1,3 0t0 6 /dev/null
python2 9224 myuser 2w CHR 1,3 0t0 6 /dev/null
python2 9224 myuser 3u a_inode 0,11 0 7005 [eventfd]
python2 9224 myuser 4u unix 0x0000000000000000 0t0 158199 type=STREAM
python2 9224 myuser 5u a_inode 0,11 0 7005 [eventfd]
python2 9224 myuser 6u a_inode 0,11 0 7005 [eventfd]
python2 9224 myuser 7r DIR 8,3 4096 22414346 <some random file being accessed during the backup>
python2 9224 myuser 8r CHR 1,9 0t0 11 /dev/urandom
python2 9224 myuser 15r FIFO 0,10 0t0 157054 pipe
python2 9224 myuser 16w FIFO 0,10 0t0 157054 pipe
但是,如果我尝试在 Python 中打开文件描述符,则会收到错误:
>>> import os
>>> os.fdopen(16)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 9] Bad file descriptor
为什么是这样?如何读取文件描述符?
答案1
使用strace
(跟踪系统调用和信号)。
用法:
sudo strace -p <PID of writing process> -s 9999 -e write=<corresponding FD>
从手册页:
-p pid Attach to the process with the process ID pid and begin tracing. The trace may be terminated
at any time by a keyboard interrupt signal (CTRL-C). strace will respond by detaching itself
from the traced process(es) leaving it (them) to continue running. Multiple -p options can
be used to attach to many processes in addition to command (which is optional if at least one
-p option is given). -p "`pidof PROG`" syntax is supported.
-s strsize Specify the maximum string size to print (the default is 32). Note that filenames are not
considered strings and are always printed in full.
-e read=set
Perform a full hexadecimal and ASCII dump of all the data read from file descriptors listed in the
specified set. For example, to see all input activity on file descriptors 3 and 5 use
-e read=3,5. Note that this is independent from the normal tracing of the read(2) system call
which is controlled by the option -e trace=read.
-e write=set
Perform a full hexadecimal and ASCII dump of all the data written to file descriptors listed in
the specified set. For example, to see all output activity on file descriptors 3 and 5 use
-e write=3,5. Note that this is independent from the normal tracing of the write(2) system call
which is controlled by the option -e trace=write.
答案2
我相信口是心非的--log=fd
选项适用于您想要与日志分离的复杂stderr
管道stdout
。
这个答案对这个问题举个例子。这是一个简单的例子:
#!/bin/sh
# Generate output on three different fds
echo hello >&3
echo world >&2
echo today >&1
当像这样执行时,
./foo 2> 2.log 3> 3.log 1> 1.log
结果是
$ cat 1.log 2.log 3.log
today
world
hello
答案3
Linux 最近获得了针对此类事情的系统调用:
使用
pidfd_open
从 PID 获取“PID FD”。使用
pidfd_getfd
通过另一个进程的 PID FD 获取文件描述符。
从 Python 3.9 开始,pidfd_open
可用作os.pidfd_open
。
pidfd_getfd
尚未通过Python标准库公开,但幸运的是ctypes
让我们打电话syscall
,Linux系统调用号永远不会改变,Linux系统调用API和ABI只是以向后兼容的方式改变。
所以!
from ctypes import CDLL, c_int, c_long, c_uint, get_errno
from functools import partial
from os import strerror
_syscall = CDLL(None, use_errno=True).syscall
# Non-variadic system call number argument:
_syscall.argtypes = [c_long]
def pidfd_getfd(pidfd, targetfd):
fd = _syscall(
438, # system call number of pidfd_getfd
c_int(pidfd),
c_int(targetfd),
c_uint(0), # unused "flags" argument
)
if fd == -1:
errno = get_errno()
raise OSError(errno, strerror(errno))
return fd
因此,在您的示例中,感兴趣的 PID 为 9224,os.fdopen(16)
您无需调用 ,而是执行os.fdopen(pidfd_getfd(os.pidfd_open(9224), 16))
。
请注意,只有当您拥有访问目标进程所需的权限时,这才有效,因此您可能需要使用提升的权限(例如sudo
)运行此代码,具体取决于进程的启动方式以及系统的配置方式。