如果连 root 都无法删除使用 O_CREAT 创建的锁定文件,我犯了什么错误? O_EXCEL?
我们创建了一个锁定文件来避免竞争条件。锁定文件在 /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 withchmod 777 filename
throughroot
user id does not work.
And the system has to be rebooted to get the lockfile removed from the/tmp
dir.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你说你正在创建这样的文件:
That does not match the signature of fopen:
所以我猜你可能实际上正在使用 open:
这是一个错误,因为 open(2) 的第三个参数是“mode”,它是使用 O_CREAT 时强制。
由于您没有传递模式参数,因此您正在调用未定义的行为,并且模式可能被设置为某个不需要的值。尝试将 0666 作为第三个参数传递给 open(2) 并看看是否有帮助。
You say you're creating the file like this:
That does not match the signature of fopen:
So I presume you might actually be using open:
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.
同时要解决问题并删除文件——以 root 身份运行以下命令:
Meanwhile to fix the problem and delete the file -- as root run this: