diff options
author | Daan De Meyer <daan.j.demeyer@gmail.com> | 2023-03-13 13:03:32 +0100 |
---|---|---|
committer | Daan De Meyer <daan.j.demeyer@gmail.com> | 2023-03-13 13:04:08 +0100 |
commit | 846c9c12e75e832e41a6c21ee818f6726c142214 (patch) | |
tree | ae86f7c670737fe7061b1ae19333e21bb0c4aaf6 /src/basic/lock-util.c | |
parent | execute: Use log_unit_error_errno() instead of log_error_errno() (diff) | |
download | systemd-846c9c12e75e832e41a6c21ee818f6726c142214.tar.xz systemd-846c9c12e75e832e41a6c21ee818f6726c142214.zip |
lock-util: Add posix_lock()
POSIX locks with the same interface as flock().
Diffstat (limited to 'src/basic/lock-util.c')
-rw-r--r-- | src/basic/lock-util.c | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/src/basic/lock-util.c b/src/basic/lock-util.c index ba0c23445b..c7a7b68ebc 100644 --- a/src/basic/lock-util.c +++ b/src/basic/lock-util.c @@ -109,12 +109,15 @@ void release_lock_file(LockFile *f) { f->operation = 0; } -int unposix_lock(int fd, int operation) { +static int fcntl_lock(int fd, int operation, bool ofd) { int cmd, type, r; assert(fd >= 0); - cmd = (operation & LOCK_NB) ? F_OFD_SETLK : F_OFD_SETLKW; + if (ofd) + cmd = (operation & LOCK_NB) ? F_OFD_SETLK : F_OFD_SETLKW; + else + cmd = (operation & LOCK_NB) ? F_SETLK : F_SETLKW; switch (operation & ~LOCK_NB) { case LOCK_EX: @@ -143,12 +146,30 @@ int unposix_lock(int fd, int operation) { return r; } +int posix_lock(int fd, int operation) { + return fcntl_lock(fd, operation, /*ofd=*/ false); +} + +int unposix_lock(int fd, int operation) { + return fcntl_lock(fd, operation, /*ofd=*/ true); +} + +void posix_unlockpp(int **fd) { + assert(fd); + + if (!*fd || **fd < 0) + return; + + (void) fcntl_lock(**fd, LOCK_UN, /*ofd=*/ false); + *fd = NULL; +} + void unposix_unlockpp(int **fd) { assert(fd); if (!*fd || **fd < 0) return; - (void) unposix_lock(**fd, LOCK_UN); + (void) fcntl_lock(**fd, LOCK_UN, /*ofd=*/ true); *fd = NULL; } |