简体   繁体   English

查找锁定文件的进程的PID

[英]Find PID of a process that locked a file

I need to find who locked a file using python (posix/linux). 我需要找到谁使用python(posix / linux)锁定文件。 Currently I use this method: 目前我使用这种方法:

flk = struct.pack('hhqql', fcntl.F_WRLCK, 0, 0, 0, 0)
flk = struct.unpack('hhqql', fcntl.fcntl(self.__file, fcntl.F_GETLK , flk))

if flk[0] == fcntl.F_UNLCK:
    # file is unlocked ...
else:
    pid = flk[4]

This solution is not architecture-independent. 该解决方案不依赖于架构。 Structure passed to fcntl contains fields such as off_t or pid_t. 传递给fcntl的结构包含off_t或pid_t等字段。 I cannot make assumptions about sizes of those types. 我不能对这些类型的大小做出假设。

struct flock {
    ...
    short l_type;    /* Type of lock: F_RDLCK,
                    F_WRLCK, F_UNLCK */
    short l_whence;  /* How to interpret l_start:
                    SEEK_SET, SEEK_CUR, SEEK_END */
    off_t l_start;   /* Starting offset for lock */
    off_t l_len;     /* Number of bytes to lock */
    pid_t l_pid;     /* PID of process blocking our lock
                    (F_GETLK only) */
    ...
};

Is there any other way to find the PID? 还有其他方法可以找到PID吗? Or maybe sizes of off_t and pid_t? 或者也许是off_t和pid_t的大小? The solution must be fully portable between different architectures. 解决方案必须在不同架构之间完全可移植

Edit I decided to use lsof program as suggested below. 编辑我决定使用lsof程序,如下所示。 Another option is to parse /proc/locks file. 另一种选择是解析/ proc / locks文件。

Maybe you can try use external program, lsof, to do that? 也许你可以尝试使用外部程序,lsof,这样做?

  Lsof revision 4.85 lists on its standard output file information about files opened by processes for the following UNIX dialects: AIX 5.3 Apple Darwin 9 and Mac OS X 10.[56] FreeBSD 4.9 and 6.4 for x86-based systems FreeBSD 8.[02] and 9.0 for AMD64-based systems Linux 2.1.72 and above for x86-based systems Solaris 9, 10 and 11 

I had previously used 'hhllh' because I thought it would most closely map to off_t across platforms. 我以前使用'hhllh'因为我认为它最接近平台上的off_t But eventually I settled on 'hhqqh' , which works for me on 32bit and 64bit systems. 但最终我选择了'hhqqh' ,它适用于32位和64位系统。

I was surprised by this solution, but what seems to be going on for the the 32bit boxes I was looking at is bits/fcntl.h creates the flock struct with off64_t unless the system already defines __USE_FILE_OFFSET64 (in which case it just uses the regular 'ol off_t ) so it seems like the size of l_start and l_len is always 8 (the python struct format char 'q' is long long ). 我对这个解决方案感到惊讶,但似乎是什么回事了32位箱子我看是bits/fcntl.h创建flock与结构off64_t除非系统已经定义__USE_FILE_OFFSET64 (在这种情况下,它只是使用了正'ol off_t )所以看起来l_startl_len的大小总是8(python struct format l_len 'q' long long )。

This is probably not entirely portable, I can't say all modern 32bit systems are going to do this, but it was good enough for me for now - so I thought I'd mention it in case others (like myself) would rather not have to exec another process and do a bunch of string parsing, YMMV. 这可能不是完全可移植的,我不能说所有现代的32位系统都会这样做,但现在对我来说已经足够了 - 所以我想我会提到它,以防其他人(比如我自己)不愿意必须执行另一个进程并执行一堆字符串解析,YMMV。 Also if I'm misunderstanding (or poorly explaining) why this format string seems to work on both platforms - maybe someone could correct me? 此外,如果我误解(或解释不清楚)为什么这个格式字符串似乎在两个平台上都有效 - 也许有人可以纠正我?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM