ctdb-mutex: Factor out function fcntl_lock_fd()
authorMartin Schwenke <martin@meltin.net>
Tue, 8 Feb 2022 00:56:46 +0000 (11:56 +1100)
committerMartin Schwenke <martins@samba.org>
Thu, 28 Jul 2022 10:09:34 +0000 (10:09 +0000)
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>
ctdb/server/ctdb_mutex_fcntl_helper.c

index cdc1450cd2b14fd4356e648846387898acacac5c..2f9e32580c210e3f1658578238f4f9099b99fd49 100644 (file)
 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';
        }