ctdbd: Add --pidfile option
authorAmitay Isaacs <amitay@gmail.com>
Fri, 19 Apr 2013 06:47:32 +0000 (16:47 +1000)
committerAmitay Isaacs <amitay@gmail.com>
Fri, 19 Apr 2013 06:48:57 +0000 (16:48 +1000)
Default is not to create a pid file.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Pair-programmed-with: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Michael Adam <obnox@samba.org>
(cherry picked from commit 996e74d3db0c50f91b320af8ab7c43ea6b1136af)

Conflicts:
server/ctdb_daemon.c

include/ctdb_private.h
server/ctdb_daemon.c
server/ctdbd.c

index 8ebd718d03c65aa471f3554ce59087b095a5f9b5..7d67a10a19785f0432ea0519b9c1a8c112adfcf5 100644 (file)
@@ -1343,6 +1343,7 @@ int32_t ctdb_control_set_recmaster(struct ctdb_context *ctdb, uint32_t opcode, T
 
 extern int script_log_level;
 extern bool fast_start;
+extern const char *ctdbd_pidfile;
 
 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb,
                                             uint32_t call_type,
index 09b3d3236aa199fea539da245bafc2836aa480b2..2930e08a3c6debf28cda40d37c46e5c46220cd1e 100644 (file)
@@ -37,6 +37,8 @@ struct ctdb_client_pid_list {
        struct ctdb_client *client;
 };
 
+const char *ctdbd_pidfile = NULL;
+
 static void daemon_incoming_packet(void *, struct ctdb_req_header *);
 
 static void print_exit_message(void)
@@ -1066,6 +1068,38 @@ static void ctdb_setup_event_callback(struct ctdb_context *ctdb, int status,
                                 tdb_null, NULL, NULL);
 }
 
+static void ctdb_remove_pidfile(void)
+{
+       if (ctdbd_pidfile != NULL && !ctdb_is_child_process()) {
+               if (unlink(ctdbd_pidfile) == 0) {
+                       DEBUG(DEBUG_INFO, ("Removed PID file %s\n",
+                                          ctdbd_pidfile));
+               } else {
+                       DEBUG(DEBUG_WARNING, ("Failed to Remove PID file %s\n",
+                                             ctdbd_pidfile));
+               }
+       }
+}
+
+static void ctdb_create_pidfile(pid_t pid)
+{
+       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));
+                       exit(11);
+               }
+
+               fprintf(fp, "%d\n", pid);
+               fclose(fp);
+               DEBUG(DEBUG_INFO, ("Created PID file %s\n", ctdbd_pidfile));
+               atexit(ctdb_remove_pidfile);
+       }
+}
+
 /*
   start the protocol going as a daemon
 */
@@ -1103,8 +1137,7 @@ int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork, bool use_syslog,
        block_signal(SIGPIPE);
 
        ctdb->ctdbd_pid = getpid();
-
-
+       ctdb_create_pidfile(ctdb->ctdbd_pid);
        DEBUG(DEBUG_ERR, ("Starting CTDBD as pid : %u\n", ctdb->ctdbd_pid));
 
        if (ctdb->do_setsched) {
index a8d956d8658d0b836ccf198b3e56891c805349e8..11e96da1e0cc9cf378180b5fd59ac73fa1bccb3b 100644 (file)
@@ -133,6 +133,7 @@ int main(int argc, const char *argv[])
                { "dbdir-persistent", 0, POPT_ARG_STRING, &options.db_dir_persistent, 0, "directory for persistent tdb files", NULL },
                { "dbdir-state", 0, POPT_ARG_STRING, &options.db_dir_state, 0, "directory for internal state tdb files", NULL },
                { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock_file, 0, "location of recovery lock file", "filename" },
+               { "pidfile", 0, POPT_ARG_STRING, &ctdbd_pidfile, 0, "location of PID file", "filename" },
                { "valgrinding", 0, POPT_ARG_NONE, &options.valgrinding, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
                { "nosetsched", 0, POPT_ARG_NONE, &options.nosetsched, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
                { "syslog", 0, POPT_ARG_NONE, &options.use_syslog, 0, "log messages to syslog", NULL },