JavaNIO: check file lock created by root user from a process started by a regular user?

I have two processes that I have to synchronize.

Process A runs as root and creates a Reentrant lock for a specific file. Process B runs under the "normal" user and must wait for the lock to be released by process A.

I tried many ways, but I can not get it to work due to incorrect file permissions. Here is the code: (the synchronization file in VM is deleted):

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 is that the lock is created as:

-rw-r--r--. 1 root root 0 May  7 21:42 lockfile.lock

if I try to check the lock (if process B works as a regular user), I get

java.io.FileNotFoundException: _lockfile_ (Permission denied)
    at java.io.RandomAccessFile.open(Native Method)
    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:212)

g = rwx, o = rwx, , , . , setWritable (true, false), , , . . "r" "rw" hasLock, ChannelNotWritableException.

, : ?

- ?

+3
2

, :

. , , , (, ). , , :

file.createNewFile();
file.setWritable(true, false);
FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
channel.tryLock()

( , , .

, !

+1

umask , "rw", , group others. , umask, .

, , , , .

+1

All Articles