diff options
author | Martin Schwenke <martin@meltin.net> | 2022-02-08 11:56:46 +1100 |
---|---|---|
committer | Martin Schwenke <martins@samba.org> | 2022-07-28 10:09:34 +0000 |
commit | c07e81abf04c20fb591376efcaa9b738a60c1a58 (patch) | |
tree | 56b9234317856d5fdd2323be4d080fba56a8f85d /ctdb | |
parent | 9daf22a5c9d4ccdd706de883141ed807cab4df92 (diff) | |
download | samba-c07e81abf04c20fb591376efcaa9b738a60c1a58.tar.gz |
ctdb-mutex: Factor out function fcntl_lock_fd()
Allows blocking mode and start offset to be specified. Always locks a
1-byte range.
Make the lock structure static to avoid initialising the whole
structure each time.
Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
Diffstat (limited to 'ctdb')
-rw-r--r-- | ctdb/server/ctdb_mutex_fcntl_helper.c | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/ctdb/server/ctdb_mutex_fcntl_helper.c b/ctdb/server/ctdb_mutex_fcntl_helper.c index cdc1450cd2b..2f9e32580c2 100644 --- a/ctdb/server/ctdb_mutex_fcntl_helper.c +++ b/ctdb/server/ctdb_mutex_fcntl_helper.c @@ -41,10 +41,28 @@ static char progpath[PATH_MAX]; static char *progname = NULL; +static int fcntl_lock_fd(int fd, bool block, off_t start) +{ + static struct flock lock = { + .l_type = F_WRLCK, + .l_whence = SEEK_SET, + .l_len = 1, + .l_pid = 0, + }; + int cmd = block ? F_SETLKW : F_SETLK; + + lock.l_start = start; + if (fcntl(fd, cmd, &lock) != 0) { + return errno; + } + + return 0; +} + static char fcntl_lock(const char *file, int *outfd) { int fd; - struct flock lock; + int ret; fd = open(file, O_RDWR|O_CREAT, 0600); if (fd == -1) { @@ -53,17 +71,10 @@ static char fcntl_lock(const char *file, int *outfd) return '3'; } - lock.l_type = F_WRLCK; - lock.l_whence = SEEK_SET; - lock.l_start = 0; - lock.l_len = 1; - lock.l_pid = 0; - - if (fcntl(fd, F_SETLK, &lock) != 0) { - int saved_errno = errno; + ret = fcntl_lock_fd(fd, false, 0); + if (ret != 0) { close(fd); - if (saved_errno == EACCES || - saved_errno == EAGAIN) { + if (ret == EACCES || ret == EAGAIN) { /* Lock contention, fail silently */ return '1'; } @@ -71,7 +82,9 @@ static char fcntl_lock(const char *file, int *outfd) /* Log an error for any other failure */ fprintf(stderr, "%s: Failed to get lock on '%s' - (%s)\n", - progname, file, strerror(saved_errno)); + progname, + file, + strerror(ret)); return '3'; } |