ctdb: Add new helper ctdb_mutex_fcntl_helper
authorMartin Schwenke <martin@meltin.net>
Tue, 8 Dec 2015 05:23:50 +0000 (16:23 +1100)
committerAmitay Isaacs <amitay@samba.org>
Thu, 28 Apr 2016 07:39:16 +0000 (09:39 +0200)
This implements the type of fcntl locking that the recovery lock uses.
The intent is to use it for multiple locks and allow the choice of
helper to be configured.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/packaging/RPM/ctdb.spec.in
ctdb/server/ctdb_mutex_fcntl_helper.c [new file with mode: 0644]
ctdb/wscript

index 19c2af19e5e35788876d7b25104491491281b7ab..d4da15918b0c444cf6341f6c703e0d5db347b89c 100644 (file)
@@ -204,6 +204,7 @@ rm -rf $RPM_BUILD_ROOT
 %{_libexecdir}/ctdb/ctdb_lock_helper
 %{_libexecdir}/ctdb/ctdb_event_helper
 %{_libexecdir}/ctdb/ctdb_recovery_helper
+%{_libexecdir}/ctdb/ctdb_mutex_fcntl_helper
 %{_libexecdir}/ctdb/ctdb_natgw
 %{_libexecdir}/ctdb/ctdb_lvs
 %{_libexecdir}/ctdb/ctdb_killtcp
diff --git a/ctdb/server/ctdb_mutex_fcntl_helper.c b/ctdb/server/ctdb_mutex_fcntl_helper.c
new file mode 100644 (file)
index 0000000..93c7f62
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+   CTDB mutex fcntl lock file helper
+
+   Copyright (C) Martin Schwenke 2015
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "replace.h"
+#include "system/filesys.h"
+#include "system/network.h"
+
+/* protocol.h is just needed for ctdb_sock_addr, which is used in system.h */
+#include "protocol/protocol.h"
+#include "common/system.h"
+
+static char *progname = NULL;
+
+static char fcntl_lock(const char *file)
+{
+       int fd;
+       struct flock lock;
+
+       fd = open(file, O_RDWR|O_CREAT, 0600);
+       if (fd == -1) {
+               fprintf(stderr, "%s: Unable to open %s - (%s)\n",
+                       progname, file, strerror(errno));
+               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;
+               close(fd);
+               fd = -1;
+               if (saved_errno == EACCES ||
+                   saved_errno == EAGAIN) {
+                       /* Lock contention, fail silently */
+                       return '1';
+               }
+
+               /* Log an error for any other failure */
+               fprintf(stderr,
+                       "%s: Failed to get lock on '%s' - (%s)\n",
+                       progname, file, strerror(saved_errno));
+               return '3';
+       }
+
+       return '0';
+}
+
+int main(int argc, char *argv[])
+{
+       char result;
+       int ppid;
+       const char *file = NULL;
+
+       progname = argv[0];
+
+       if (argc != 2) {
+               fprintf(stderr, "Usage: %s <file>\n", progname);
+               exit(1);
+       }
+
+       ppid = getppid();
+       file = argv[1];
+
+       result = fcntl_lock(file);
+       sys_write(STDOUT_FILENO, &result, 1);
+
+       ctdb_wait_for_process_to_exit(ppid);
+
+       return 0;
+}
index 2a7c23638851990828a2004f268cacb9f7f6e3bf..6a50a3966fa67583d2815bbf7f73cf09d9b350cc 100755 (executable)
@@ -451,6 +451,12 @@ def build(bld):
                              samba-util replace tdb''',
                      install_path='${CTDB_HELPER_BINDIR}')
 
+    bld.SAMBA_BINARY('ctdb_mutex_fcntl_helper',
+                     source='server/ctdb_mutex_fcntl_helper.c',
+                     deps='ctdb-system',
+                     includes='include',
+                     install_path='${CTDB_HELPER_BINDIR}')
+
     bld.SAMBA_GENERATOR('ctdb-smnotify-h',
                         source='utils/smnotify/smnotify.x',
                         target='utils/smnotify/smnotify.h',