How To Lock A File So That Other Process Cannot Cat It?
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
struct flock fl;
fl.l_start = 0;
fl.l_len = 517; //found the size of the file outside (not best practise, I know)
fl.l_whence = SEEK_SET;
int fd = open("test", O_RDWR);
if(fd<0)
perror("open");
fl.l_type = F_RDLCK;
fl.l_pid = getpid();
if(fcntl(fd, F_SETLK, &fl) < 0)
{
perror("fcntl"); //geting fcntl: Invalid argument
exit(1);
}
if(fl.l_type != F_UNLCK)
{
printf("file has been exclusively locked by process:%u\n",fl.l_pid);
printf("press enter to release the file\n");
getchar();
fl.l_type = F_UNLCK; //file released
}
}
I would like to lock a file (test
), which contain lorem ipsum (some random text), so that other process could not cat
it until the currect process release the lock. But which argument passed to fcntl
is wrong?
EDIT:
After comments I have initialized some members of fl
variable (see edits), no, despite working. I can still cat
the locked file test
in another process... why, when it is locked?
Answer
File locking is not mandatory locking — it is advisory locking.
That means that if a program such as cat
does not look to see whether a file is locked, it doesn't matter whether some other program locks it or not — cat
will still read the file.
Unless you use a variant of cat
that does check for file locks, you are not going to be able to use file locking to stop cat
.
What can you do instead?
- Rename the file.
- Change permissions on the file.
- Decide not to worry about it.
The last option is the easiest — and probably most effective.
Some systems do support mandatory file locking. Typically, that's indicated by setting the SGID bit on a non-executable file. If you're using such a system, then you should be able to prevent cat
from working on a locked file.
Related Questions
- → OctoberCMS Backend Loging Hash Error
- → "failed to open stream" error when executing "migrate:make"
- → OctoberCMS - How to make collapsible list default to active only on non-mobile
- → Create plugin that makes objects from model in back-end
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → How to install console for plugin development in October CMS
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → How to update data attribute on Ajax complete
- → October CMS - Conditionally Load a Different Page