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 lockfilein/tmpfolder gave an O/P in permission
section “—x——“, which I could not decipher. Changing the permission of
the lockfile withchmod 777 filenamethroughrootuser id does not work.
And the system has to be rebooted to get the lockfile removed from the/tmpdir.
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.