从用户 space 获取打开文件的引用计数 (inode->i_count)
Getting open file's reference count (inode->i_count) from user space
我需要检查文件是否被系统上的任何其他进程打开。出于性能考虑,扫描 /proc
不是一个选项。
一种优雅的方法是读取内核 struct inode
的 i_count
成员,对应于文件。
我在用户 space 中有一个文件的 fd,我如何从内核 space 中获得关联 struct inode
的 i_count
,知道吗?
建议您描述实际问题。
一般来说,由于文件可以随时打开,所以您的检查结果会立即过时。尽管没有用户空间用户,但 i_count 也可以增加。
这不是对所述问题的回答,而是一个示例程序,如何使用 Linux 特定的文件租约来可靠地检测对本地文件的访问何时在当前计算机上是独占的;也就是说,该文件未被同一台机器上的任何其他进程打开。授予写租用后,当任何其他进程试图打开写租用文件时,原始进程会收到通知。
writelease.c:
#define _POSIX_C_SOURCE 200809L
#define _GNU_SOURCE
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#define LEASE_SIGNAL (SIGRTMIN+0)
int main(int argc, char *argv[])
{
sigset_t signals;
siginfo_t info;
int fd, result;
sigemptyset(&signals);
sigaddset(&signals, LEASE_SIGNAL);
sigaddset(&signals, SIGINT);
sigaddset(&signals, SIGTERM);
if (sigprocmask(SIG_BLOCK, &signals, NULL) == -1) {
fprintf(stderr, "Cannot block signals: %s.\n", strerror(errno));
return EXIT_FAILURE;
}
if (argc != 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
fprintf(stderr, "\n");
fprintf(stderr, "Usage: %s [ -h | --help ]\n", argv[0]);
fprintf(stderr, " %s FILENAME\n", argv[0]);
fprintf(stderr, "\n");
return EXIT_SUCCESS;
}
/* Open file. Should not be interrupted, but let's be overly paranoid. */
do {
fd = open(argv[1], O_RDONLY | O_NOCTTY);
} while (fd == -1 && errno == EINTR);
if (fd == -1) {
fprintf(stderr, "%s: %s.\n", argv[1], strerror(errno));
return EXIT_FAILURE;
}
/* This should not be interrupted ever, but let's again be overly paranoid. */
do {
result = fcntl(fd, F_SETSIG, LEASE_SIGNAL);
} while (result == -1 && errno == EINTR);
if (result == -1) {
fprintf(stderr, "%s: Cannot change signal: %s.\n", argv[1], strerror(errno));
return EXIT_FAILURE;
}
/* Try to get a write lease on the file. */
do {
result = fcntl(fd, F_SETLEASE, F_WRLCK);
} while (result == -1 && errno == EINTR);
if (result == -1) {
fprintf(stderr, "%s: Cannot get a write lease: %s.\n", argv[1], strerror(errno));
return EXIT_FAILURE;
}
printf("%s: Write lease obtained; this process (%ld) is the only one with an open description to it.\n",
argv[1], (long)getpid());
fflush(stdout);
/* Wait for the first signal. */
do {
info.si_fd = -1;
result = sigwaitinfo(&signals, &info);
} while (result == -1 && errno == EINTR);
if (result == -1) {
fprintf(stderr, "Uh-oh. sigwaitinfo() failed; this should never occur. %s.\n", strerror(result));
return EXIT_FAILURE;
}
if (result == LEASE_SIGNAL && info.si_fd == fd)
printf("Process %ld is opening the file. Releasing the lease.\n", (long)info.si_pid);
else
printf("Received signal %d (%s); exiting.\n", result, strsignal(result));
fflush(stdout);
/* Closing the file descriptor releases the lease.
* At exit, the kernel will do this for us, so explicit close() here
* is not necessary. Again, just being overly pedantic and careful. */
close(fd);
return EXIT_SUCCESS;
}
使用
编译以上内容
gcc -std=c99 -Wall -Wextra -O2 writelease.c -o writelease
普通用户只能租用他们拥有的文件。但是,不需要完全的超级用户权限就可以租用任意文件; cap_lease
能力就足够了。在大多数当前 Linux 发行版中,您可以使用
sudo setcap cap_lease=pe writelease
将功能添加到二进制文件;然而,这意味着任何用户都可以 运行 它,并在他们想要的任何文件上进行写租约。 (除非您先审查该程序,以确保不会造成安全风险,否则您不应该这样做!但是,非常适合在您自己的系统上进行测试。)
在一个终端 window 中,租用某个文件,也许是 writelease.c
文件:
./writelease writelease.c
如果文件没有被任何进程打开,它会输出类似
的内容
writelease.c: Write lease obtained; this process (5782) is the only one with an open description to it.
请注意,许多编辑器(例如 gedit
)不会使文件永久打开,可能只是使用 rename()
(或硬链接)将旧文件替换为新文件。即 not 被文件租约捕获;你需要使用 fanotify 或 dnotify 来检测这些。
如果文件已经打开(比如,less writelease.c
在另一个 window 中打开),输出会有所不同,很可能
writelease.c: Cannot get a write lease: Resource temporarily unavailable.
如果租用成功,你可以使用Ctrl+C中断程序(发送一个INT
信号) ,向其发送 TERM
信号,或使用其他程序打开文件。例如,如果您在另一个 window 中启动 less writelease.c
,写入租用程序将输出
Process 1089 is opening the file. Releasing the lease.
然后退出。
限制:
如上所述,这只能可靠地检测文件是否被打开或 t运行由另一个进程处理。如果文件或其任何父目录被重命名,则不会捕获。您需要将 fanotify 或 dnotify watches 添加到目录(以及直到挂载点的父目录)才能捕获它们。当然,这些仅在 事后 发生,而不是像文件租用信号那样同步发生。但是,对于日志轮换,这应该不是问题。
只能租用本地文件。对于日志文件,这应该不是问题,因为它们肯定是本地的。 (对于远程日志记录,您应该重定向整个系统日志,而不是对日志文件使用网络共享。)
当另一个进程试图打开文件,而您对文件有写租约时,不能无限期地阻止打开。 (当然,你可以检测试图打开它的进程,并发送 SIGKILL
来杀死它,但那将是非常残酷的。事实上,我自己甚至还没有尝试过,所以我不确定在宽限期 /proc/sys/fs/lease-break-time
秒之后租约是否仍然中断。)(不过,我个人已经 运行 在放弃租约之前将文件设置为零字节。)
即使租约所有者重命名文件,或将其移动到同一装载上的另一个目录,打开器仍会打开 (renamed/moved) 文件。从根本上讲,在发送租用信号时,打开已经在进行中,我们只能延迟几秒钟,不能真正取消它。
您是否尝试过使用 lsof,一个用户 space 实用程序?
A tutorial.
当对内核对象(设备文件)调用 Open() 系统调用时,内核创建一个打开的文件描述符结构:struct file *
每次在设备文件上发出 open() 系统调用时,都会在内核中分配一个新的结构文件 *。
Objective是,对于每一个open(),在driver中应该只有一个release方法调用。
如果进程在设备 file/kernel 对象上有 n 个 open() 系统调用问题,则在内核中创建 n 个结构文件结构。
现在,如果进程是 fork()/dup(),内核中不会创建新的结构文件。只有现有 struct file 结构的引用计数会增加。此引用计数表示共享文件描述符的进程数。
发出 close() 时,结构文件的引用计数会减少。如果达到0,则在driver中调用release方法。
这就是如何确保对于每个 open(),只有一次和一次 release() 调用。
因此,在某种程度上,它不是 inode->icount,而是 filp->f_count 表示共享同一文件描述符的进程数。
我需要检查文件是否被系统上的任何其他进程打开。出于性能考虑,扫描 /proc
不是一个选项。
一种优雅的方法是读取内核 struct inode
的 i_count
成员,对应于文件。
我在用户 space 中有一个文件的 fd,我如何从内核 space 中获得关联 struct inode
的 i_count
,知道吗?
建议您描述实际问题。
一般来说,由于文件可以随时打开,所以您的检查结果会立即过时。尽管没有用户空间用户,但 i_count 也可以增加。
这不是对所述问题的回答,而是一个示例程序,如何使用 Linux 特定的文件租约来可靠地检测对本地文件的访问何时在当前计算机上是独占的;也就是说,该文件未被同一台机器上的任何其他进程打开。授予写租用后,当任何其他进程试图打开写租用文件时,原始进程会收到通知。
writelease.c:
#define _POSIX_C_SOURCE 200809L
#define _GNU_SOURCE
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#define LEASE_SIGNAL (SIGRTMIN+0)
int main(int argc, char *argv[])
{
sigset_t signals;
siginfo_t info;
int fd, result;
sigemptyset(&signals);
sigaddset(&signals, LEASE_SIGNAL);
sigaddset(&signals, SIGINT);
sigaddset(&signals, SIGTERM);
if (sigprocmask(SIG_BLOCK, &signals, NULL) == -1) {
fprintf(stderr, "Cannot block signals: %s.\n", strerror(errno));
return EXIT_FAILURE;
}
if (argc != 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
fprintf(stderr, "\n");
fprintf(stderr, "Usage: %s [ -h | --help ]\n", argv[0]);
fprintf(stderr, " %s FILENAME\n", argv[0]);
fprintf(stderr, "\n");
return EXIT_SUCCESS;
}
/* Open file. Should not be interrupted, but let's be overly paranoid. */
do {
fd = open(argv[1], O_RDONLY | O_NOCTTY);
} while (fd == -1 && errno == EINTR);
if (fd == -1) {
fprintf(stderr, "%s: %s.\n", argv[1], strerror(errno));
return EXIT_FAILURE;
}
/* This should not be interrupted ever, but let's again be overly paranoid. */
do {
result = fcntl(fd, F_SETSIG, LEASE_SIGNAL);
} while (result == -1 && errno == EINTR);
if (result == -1) {
fprintf(stderr, "%s: Cannot change signal: %s.\n", argv[1], strerror(errno));
return EXIT_FAILURE;
}
/* Try to get a write lease on the file. */
do {
result = fcntl(fd, F_SETLEASE, F_WRLCK);
} while (result == -1 && errno == EINTR);
if (result == -1) {
fprintf(stderr, "%s: Cannot get a write lease: %s.\n", argv[1], strerror(errno));
return EXIT_FAILURE;
}
printf("%s: Write lease obtained; this process (%ld) is the only one with an open description to it.\n",
argv[1], (long)getpid());
fflush(stdout);
/* Wait for the first signal. */
do {
info.si_fd = -1;
result = sigwaitinfo(&signals, &info);
} while (result == -1 && errno == EINTR);
if (result == -1) {
fprintf(stderr, "Uh-oh. sigwaitinfo() failed; this should never occur. %s.\n", strerror(result));
return EXIT_FAILURE;
}
if (result == LEASE_SIGNAL && info.si_fd == fd)
printf("Process %ld is opening the file. Releasing the lease.\n", (long)info.si_pid);
else
printf("Received signal %d (%s); exiting.\n", result, strsignal(result));
fflush(stdout);
/* Closing the file descriptor releases the lease.
* At exit, the kernel will do this for us, so explicit close() here
* is not necessary. Again, just being overly pedantic and careful. */
close(fd);
return EXIT_SUCCESS;
}
使用
编译以上内容gcc -std=c99 -Wall -Wextra -O2 writelease.c -o writelease
普通用户只能租用他们拥有的文件。但是,不需要完全的超级用户权限就可以租用任意文件; cap_lease
能力就足够了。在大多数当前 Linux 发行版中,您可以使用
sudo setcap cap_lease=pe writelease
将功能添加到二进制文件;然而,这意味着任何用户都可以 运行 它,并在他们想要的任何文件上进行写租约。 (除非您先审查该程序,以确保不会造成安全风险,否则您不应该这样做!但是,非常适合在您自己的系统上进行测试。)
在一个终端 window 中,租用某个文件,也许是 writelease.c
文件:
./writelease writelease.c
如果文件没有被任何进程打开,它会输出类似
的内容writelease.c: Write lease obtained; this process (5782) is the only one with an open description to it.
请注意,许多编辑器(例如 gedit
)不会使文件永久打开,可能只是使用 rename()
(或硬链接)将旧文件替换为新文件。即 not 被文件租约捕获;你需要使用 fanotify 或 dnotify 来检测这些。
如果文件已经打开(比如,less writelease.c
在另一个 window 中打开),输出会有所不同,很可能
writelease.c: Cannot get a write lease: Resource temporarily unavailable.
如果租用成功,你可以使用Ctrl+C中断程序(发送一个INT
信号) ,向其发送 TERM
信号,或使用其他程序打开文件。例如,如果您在另一个 window 中启动 less writelease.c
,写入租用程序将输出
Process 1089 is opening the file. Releasing the lease.
然后退出。
限制:
如上所述,这只能可靠地检测文件是否被打开或 t运行由另一个进程处理。如果文件或其任何父目录被重命名,则不会捕获。您需要将 fanotify 或 dnotify watches 添加到目录(以及直到挂载点的父目录)才能捕获它们。当然,这些仅在 事后 发生,而不是像文件租用信号那样同步发生。但是,对于日志轮换,这应该不是问题。
只能租用本地文件。对于日志文件,这应该不是问题,因为它们肯定是本地的。 (对于远程日志记录,您应该重定向整个系统日志,而不是对日志文件使用网络共享。)
当另一个进程试图打开文件,而您对文件有写租约时,不能无限期地阻止打开。 (当然,你可以检测试图打开它的进程,并发送 SIGKILL
来杀死它,但那将是非常残酷的。事实上,我自己甚至还没有尝试过,所以我不确定在宽限期 /proc/sys/fs/lease-break-time
秒之后租约是否仍然中断。)(不过,我个人已经 运行 在放弃租约之前将文件设置为零字节。)
即使租约所有者重命名文件,或将其移动到同一装载上的另一个目录,打开器仍会打开 (renamed/moved) 文件。从根本上讲,在发送租用信号时,打开已经在进行中,我们只能延迟几秒钟,不能真正取消它。
您是否尝试过使用 lsof,一个用户 space 实用程序? A tutorial.
当对内核对象(设备文件)调用 Open() 系统调用时,内核创建一个打开的文件描述符结构:struct file *
每次在设备文件上发出 open() 系统调用时,都会在内核中分配一个新的结构文件 *。
Objective是,对于每一个open(),在driver中应该只有一个release方法调用。
如果进程在设备 file/kernel 对象上有 n 个 open() 系统调用问题,则在内核中创建 n 个结构文件结构。
现在,如果进程是 fork()/dup(),内核中不会创建新的结构文件。只有现有 struct file 结构的引用计数会增加。此引用计数表示共享文件描述符的进程数。
发出 close() 时,结构文件的引用计数会减少。如果达到0,则在driver中调用release方法。
这就是如何确保对于每个 open(),只有一次和一次 release() 调用。
因此,在某种程度上,它不是 inode->icount,而是 filp->f_count 表示共享同一文件描述符的进程数。