ctdb-daemon: Use PID file abstraction
authorMartin Schwenke <martin@meltin.net>
Thu, 22 Sep 2016 04:35:03 +0000 (14:35 +1000)
committerStefan Metzmacher <metze@samba.org>
Thu, 13 Oct 2016 14:55:40 +0000 (16:55 +0200)
BUG: https://bugzilla.samba.org/show_bug.cgi?id=12287

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
(cherry picked from commit 5148e02adb7b2ea34da9c826a682c1387773402b)

ctdb/server/ctdb_daemon.c

index 47e49dfa3cd2516a1087700e04408c12a8a7400e..d3ea2631db01ac1b6dc6f9192874d276ed6af3a6 100644 (file)
@@ -44,6 +44,7 @@
 #include "common/system.h"
 #include "common/common.h"
 #include "common/logging.h"
+#include "common/pidfile.h"
 
 struct ctdb_client_pid_list {
        struct ctdb_client_pid_list *next, *prev;
@@ -53,6 +54,7 @@ struct ctdb_client_pid_list {
 };
 
 const char *ctdbd_pidfile = NULL;
+static struct pidfile_context *ctdbd_pidfile_ctx = NULL;
 
 static void daemon_incoming_packet(void *, struct ctdb_req_header *);
 
@@ -1149,32 +1151,21 @@ static void ctdb_tevent_trace(enum tevent_trace_point tp,
 
 static void ctdb_remove_pidfile(void)
 {
-       /* Only the main ctdbd's PID matches the SID */
-       if (ctdbd_pidfile != NULL && getsid(0) == getpid()) {
-               if (unlink(ctdbd_pidfile) == 0) {
-                       DEBUG(DEBUG_NOTICE, ("Removed PID file %s\n",
-                                            ctdbd_pidfile));
-               } else {
-                       DEBUG(DEBUG_WARNING, ("Failed to Remove PID file %s\n",
-                                             ctdbd_pidfile));
-               }
-       }
+       TALLOC_FREE(ctdbd_pidfile_ctx);
 }
 
-static void ctdb_create_pidfile(pid_t pid)
+static void ctdb_create_pidfile(TALLOC_CTX *mem_ctx)
 {
        if (ctdbd_pidfile != NULL) {
-               FILE *fp;
-
-               fp = fopen(ctdbd_pidfile, "w");
-               if (fp == NULL) {
-                       DEBUG(DEBUG_ALERT,
-                             ("Failed to open PID file %s\n", ctdbd_pidfile));
+               int ret = pidfile_create(mem_ctx, ctdbd_pidfile,
+                                        &ctdbd_pidfile_ctx);
+               if (ret != 0) {
+                       DEBUG(DEBUG_ERR,
+                             ("Failed to create PID file %s\n",
+                              ctdbd_pidfile));
                        exit(11);
                }
 
-               fprintf(fp, "%d\n", pid);
-               fclose(fp);
                DEBUG(DEBUG_NOTICE, ("Created PID file %s\n", ctdbd_pidfile));
                atexit(ctdb_remove_pidfile);
        }
@@ -1265,7 +1256,7 @@ int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork)
        ctdb->ctdbd_pid = getpid();
        DEBUG(DEBUG_ERR, ("Starting CTDBD (Version %s) as PID: %u\n",
                          CTDB_VERSION_STRING, ctdb->ctdbd_pid));
-       ctdb_create_pidfile(ctdb->ctdbd_pid);
+       ctdb_create_pidfile(ctdb);
 
        /* Make sure we log something when the daemon terminates.
         * This must be the first exit handler to run (so the last to