(这一答案使人大而大的假设是,这种情况将持续在含水层上。)
The C source code of lsof
, a tool that tells which programs currently have an open file descriptor to a specific file, is freely available. However, just to warn you, I couldn t make any sense out of it. There are references to reading kernel memory, so to me it s either voodoo or black magic.
尽管如此,没有任何东西阻止你操作<<>tlof。 通过您自己的方案。 从你自己的方案中执行第三方方案,通常是你试图避免的几种原因,例如安全(如果恶意方案使用人改动<<>lsof<>code>,则会利用你的方案特权,并可能产生catastrophic consequences)但检查<<>lsof。 来源代码,我得出结论,没有公开的APIC,以确定哪些方案已经开放。 如果你不害怕在<代码>/usr/sbin上改变方案的人,你可以考虑这样做。
int isOpen(const char* file)
{
char* command;
// BE AWARE THAT THIS WILL NOT WORK IF THE FILE NAME CONTAINS A DOUBLE QUOTE
// OR IF IT CAN SOMEHOW BE ALTERED THROUGH SHELL EXPANSION
// you should either try to fix it yourself, or use a function of the `exec`
// family that won t trigger shell expansion.
// It would be an EXTREMELY BAD idea to call `lsof` without an absolute path
// since it could result in another program being run. If this is not where
// `lsof` resides on your system, change it to the appropriate absolute path.
asprintf(&command, "/usr/sbin/lsof "%s"", file);
int result = system(command);
free(command);
return result;
}
如果你还需要知道贵国的档案(推定cp
?),那么你也可以以类似方式使用<代码>popen<>/code>读到。 <代码>popen descriptors behave such as f open
descriptors, so all You need to do is fread
them and see You can seek their program s name. 在我的机器上,<代码>lsof输出情况如下:
$ lsof document.pdf
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
SomeApp 873 felix txt REG 14,3 303260 5165763 document.pdf