- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我需要检查文件是否被系统上的任何其他进程打开。出于性能考虑,扫描 /proc
不是一个选项。
一个优雅的方法是读取内核的 struct inode
的 i_count
成员,对应于文件。
我在用户空间中有一个文件的 fd,如何从内核空间中获取关联的 struct inode
的 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
普通用户只能租用他们拥有的文件。但是,不需要完全的 super 用户权限就可以租用任意文件; cap_lease
能力就足够了。在大多数当前的 Linux 发行版中,您可以使用
sudo setcap cap_lease=pe writelease
将功能添加到二进制文件;然而,这意味着任何用户都可以运行它,并对他们希望的任何文件进行写租约。 (除非您首先审查该程序,以确保不会造成安全风险,否则您不应该这样做!但是,对于在您自己的系统上进行测试非常好。)
在一个终端窗口中,租用某个文件,可能是 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()
将旧文件替换为新文件(或硬链接(hard link))。那是不被文件租约捕获的;您需要使用 fanotify 或 dnotify 来检测它们。
如果文件已经打开(例如,less writelease.c
在另一个窗口中打开),输出很可能会有所不同
writelease.c: Cannot get a write lease: Resource temporarily unavailable.
如果租约成功,你可以使用Ctrl+C中断程序(发送一个INT
信号),发送一个TERM
信号,或使用其他程序打开文件。例如,如果您在另一个窗口中启动 less writelease.c
,write lease 程序将输出
Process 1089 is opening the file. Releasing the lease.
然后退出。
限制:
如上所述,这只能可靠地检测文件是否被另一个进程打开或截断。如果文件或其任何父目录被重命名,则不会捕获。您需要将 fanotify 或 dnotify watches 添加到目录(以及直到挂载点的父目录)才能捕获它们。当然,这些只是事后发生,而不是像文件租用信号那样同步发生。然而,对于日志轮换,这应该不是问题。
只能租用本地文件。对于日志文件,这应该不是问题,因为它们肯定是本地的。 (对于远程日志记录,您应该重定向整个系统日志,而不是对日志文件使用网络共享。)
当另一个进程试图打开文件,而您对其有写租约时,打开不能被无限期地阻止。 (当然,您可以检测试图打开它的进程,并向它发送 SIGKILL
以终止它,但这将非常残酷。事实上,我自己甚至还没有尝试过,所以我不确定在宽限期 /proc/sys/fs/lease-break-time
秒之后租约是否仍然被打破。)(我个人已将文件截断为零然而,在放弃租约之前的字节数。)
即使租约所有者重命名文件,或将其移动到同一挂载的另一个目录中,打开程序仍会打开(重命名/移动)文件。从根本上说,当发送租用信号时,打开已经在进行中,我们只能延迟几秒钟,而不能真正取消它。
关于c - 从用户空间获取打开文件的引用计数(inode->i_count),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37085937/
我开始学习 Oracle JavaSE 认证考试。 我创建了一个 IntelliJ Idea 项目来处理我的训练源代码。我想尽量减少 IntelliJ Idea 的帮助。 我只想使用:颜色语法、终端选
默认情况下,.DPR 和 .DPROJ 的文件扩展名描述是相同的,因此在资源管理器中打开具有相同基本名称的项目文件时,两个文件描述都会列为“Delphi 项目文件”,这提供了一个选择开发人员 - 要打
我目前正在从 android 网站了解 Navigation Drawer,我正在使用他们的示例 http://developer.android.com/training/implementing-
我需要帮助。 我在 A3:A500 列中有单词和数字 我需要改变他们的名字。 如果单元格包含单词“previ”,则如果单元格是数字,则将字母“p”放入新列中。如果它是一个词,那么不要放“p” ...就
我正在尝试编写一些 VBA,它允许按钮添加一个空行,保持相同的格式,就在 SUM 公式所在的行上方。 到目前为止,我实现了创建一个空行,但我不知道如何实现代码以让该新行继承相同的格式样式(包括边框和格
我在共享网络驱动器上有两个工作簿: 工作簿 A(表) 工作簿 B(数据透视表 - 连接到源工作簿 A) 我正在尝试,当打开 Workbook B 时,运行宏并执行以下操作: 取消保护工作簿 B 上的某
我正在开发一个需要在在线/离线模式下进行测试的应用程序,所以我想知道是否有任何方法可以打开/关闭 iPad 模拟器的互联网连接(不关闭我的 MAC 的互联网服务)。请帮忙 最佳答案 不,模拟器使用与您
我需要对目录的所有文件执行我的脚本(搜索)。以下是有效的方法。我只是问哪个最好。 (我需要格式的文件名:parsedchpt31_4.txt) 全局: my $parse_corpus; #(for
在我的代码中,我想有条件地执行一些操作: #ifdef DEBUG NSLog(@"I'm in debug mode"); #endif 我已配置“项目”->“编辑项目设置”->“构建”选项卡,以便
我编写了一个小程序来比较笔记本电脑的性能。为了使程序CPU更加密集,我用一些多线程代码(通过Parallel API实现)实现了Rabin-Karp模式匹配算法。 我注意到,当在关闭编译器优化标志的情
使用以下代码来关闭模态并打开第二个模态。总是遇到同样的问题可以关闭一个但不能打开第二个,或者如果我更改顺序我可以打开一个但不能关闭另一个。 (我想我已经尝试过101版本了)。如果有人能帮忙的话。
blue sky 默认情况下,当指针悬停时显示标题。 是否可以切换它,例如: $('#button').on('click', function(){ if (something) {turn
我正在编写一个简单的宏,它将打开、保存和关闭一个 Excel 文件(例如 myworkbook.xlsx),但我无法执行此操作。我的文件 myworkbook.xlsx 位于以下位置: C:\User
我正在加载两个 geoJson 层 - 出于测试目的,两个层都是相同的数据,但是是从两个不同的 json 文件中提取的。当我在图层 Controller 中打开和关闭图层时,图层的绘制顺序会发生变化。
我在我的设置 Activity 中发现,当用户单击 ToggleButton 时,它应该在整个应用程序中静音,但它不起作用。我在教程类中放入的 SoundPool onClick 按钮声音仍在 onC
我有一部双卡手机。如果我想打开飞行模式,两个 SIM 卡都会发生这种情况。 是否可以通过编程方式仅对一张SIM卡进行操作(用户可以选择两者之一)?我看到了here上的帖子,他们一直工作到 API 16
我目前正在开发一个带有一些 pipe() 和重定向的 C shell 程序。 我使用 dup2() stdout 和 stderr (1 & 2) 重定向。 当我用 int fd = open("te
Jquery: 有没有办法捕获浏览器打开“打开/另存为”对话框时触发的事件? Open/Save dialog example http://qpack.orcanos.com/helpcenter/
我知道你可以用 window.close 关闭 window.open 但还有其他方法吗?我有一个打开 facebook 连接的弹出窗口,我想在用户连接到 facebook 时关闭弹出窗口,然后刷新父
我搜索一个事件,如果不存在,则搜索一种方法来了解屏幕是否关闭(电源选项 - 控制面板 - 关闭显示设置)。 这些解决方案都不适合我。 所以要么我在某个地方错了,要么就是不合适。 How to get
我是一名优秀的程序员,十分优秀!