如果连 root 都无法删除使用 O_CREAT 创建的锁定文件,我犯了什么错误? O_EXCEL?

发布于 2025-01-07 04:58:00 字数 654 浏览 21 评论 0原文

我们创建了一个锁定文件来避免竞争条件。锁定文件在 /tmp 目录中创建,该目录设置了粘性位。我们没有将文件模式作为可选参数传递 文件创建期间的第三个参数。该文件是使用 fopen 函数创建的,

int fd = fopen(filename, O_CREAT | O_EXCEL);

一旦使用结束,我们将删除该锁定文件。但有时文件没有被删除 它保留在 /tmp 文件夹中,阻止其他进程,并且应用程序保持活动状态而不继续进行。正在通过向系统发送命令来删除锁定文件 使用的命令是/usr/bin -rf。令人惊讶的是,此后文件所有者和 root 用户都无法删除该文件。

/tmp 文件夹中执行操作 ll lockfile 授予 O/P 权限 “---x------”部分,我无法破译。更改权限 通过 root 用户 ID 使用 chmod 777 filename 的锁定文件不起作用。 并且必须重新启动系统才能从 /tmp 目录中删除锁定文件。

We have created a lock file to avoid a race condition. The lockfile is created in /tmp directory, which has the sticky bit set. We are not passing the mode of the file as the optional
third argument during the file creation. And the file is created using fopen function and

int fd = fopen(filename, O_CREAT | O_EXCEL);

We are deleting this lockfile once its use is over. But sometimes the file is not deleted
and it remains in the /tmp folder, blocking the other process and the application remains just active without proceeding further. The lock file is being deleted by sending command to system
and the command used is /usr/bin -rf. What was surprising that neither the file owner nor the root user was able to delete the file after that.

Doing an operation ll lockfile in /tmp folder gave an O/P in permission
section "---x------", which I could not decipher. Changing the permission of
the lockfile with chmod 777 filename through root user id does not work.
And the system has to be rebooted to get the lockfile removed from the /tmp dir.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

究竟谁懂我的在乎 2025-01-14 04:58:00

你说你正在创建这样的文件:

int fd = fopen(filename, O_CREAT | O_EXCEL);

That does not match the signature of fopen:

FILE *fopen(const char *filename, const char *mode);

所以我猜你可能实际上正在使用 open:

int fd = open(filename, O_CREAT | O_EXCL);

这是一个错误,因为 open(2) 的第三个参数是“mode”,它是使用 O_CREAT 时强制。

由于您没有传递模式参数,因此您正在调用未定义的行为,并且模式可能被设置为某个不需要的值。尝试将 0666 作为第三个参数传递给 open(2) 并看看是否有帮助。

You say you're creating the file like this:

int fd = fopen(filename, O_CREAT | O_EXCEL);

That does not match the signature of fopen:

FILE *fopen(const char *filename, const char *mode);

So I presume you might actually be using open:

int fd = open(filename, O_CREAT | O_EXCL);

Which is an error because the third argument to open(2) is "mode" and it is mandatory when O_CREAT is used.

Since you are not passing the mode argument, you are invoking undefined behavior, and the mode is probably getting set to some undesired value. Try passing 0666 as the third argument to open(2) and see if that helps.

尸血腥色 2025-01-14 04:58:00

同时要解决问题并删除文件——以 root 身份运行以下命令:

chmod 755 /tmp/lockfile
rm /tmp/lockfile

Meanwhile to fix the problem and delete the file -- as root run this:

chmod 755 /tmp/lockfile
rm /tmp/lockfile
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文