Linux file leases - fcntl F_SETLEASE when file is

2019-08-14 08:09发布

What happens if I'm trying to set a, say, F_RDLCK lease on a file that is already open for a writing somewhere else.

fcntl(2) says

A read lease can be placed only on a file descriptor that is opened read-only.

Does it mean it should be open read-only system-wise and otherwise fcntl(2) call will fail with, presumingly EACCES? If so, is there any other way to wait for the lease to be possible other than using inotify(7) (IN_CLOSE_WRITE)?

标签: linux
1条回答
Animai°情兽
2楼-- · 2019-08-14 08:32

This call:

fcntl(fd, F_SETLEASE, F_RDLCK)

will fail with EAGAIN ("Resource temporarily unavailable") if underlying file is currently opened for writing in any process.

It corresponds to description in manual page:

EACCES or EAGAIN

Operation is prohibited by locks held by other processes.

If caller itself opened fd for writing (i.e. not with O_RDONLY flag), it's just a special case of this rule and fcntl() also returns EAGAIN.

查看更多
登录 后发表回答