I have two processes which I am to synchronize.
Process A is running as root and creates a Reentrant-Lock on a specific file.
Process B is running under a “normal” user and should wait for the lock being releases by process A.
I tried many ways but cannot get it working because of wrong file permissions.
Here is the code:
(removed the in-VM-synchronisation-stuff):
Lock:
FileChannel channel = new RandomAccessFile(pFile, "rw").getChannel();
lock = channel.tryLock();
hasLock:
RandomAccessFile file = new RandomAccessFile(pFile, "rw");
FileChannel channel = file.getChannel();
FileLock lock = channel.tryLock();
if (lock == null) {
return true;
}
lock.release();
The problem I have is that the lock gets created as:
-rw-r--r--. 1 root root 0 May 7 21:42 lockfile.lock
if I try to check the lock (by process B running as normal user) I get a
java.io.FileNotFoundException: _lockfile_ (Permission denied)
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:212)
I tried to umask the directory to all g=rwx,o=rwx but this seems to be ignored. I tried to create the file first, setWritable(true, false) but this seems to be resetted. I did not get any method to work. I tried to use mode “r” instead of “rw” in hasLock but this leads to a ChannelNotWritableException.
So the main question is: how can I influence the permissions of the created lockfile?
Does anyone has some suggestions?
Regards Michael
Finally I got a solution:
The way of first creating the file was the correct one. The mistake I made was that I set the permissions before file creation (expecting that the permissions are stored in the file object and used on creation). I have to first create the file, then set the permissions to all and then lock it:
the lockfile is now created with worlwide-writable (this may be a security issue but actually there is no problem visible as the file does not contain any content.
Thanks to everyone helping me to find a solution!
Regards Michael