From 81d8f89385fcf1e79a3c9d7b1fbab1cd696a03ee Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Thu, 22 Sep 2016 14:35:03 +1000 Subject: [PATCH] ctdb-daemon: Use PID file abstraction BUG: https://bugzilla.samba.org/show_bug.cgi?id=12287 Signed-off-by: Martin Schwenke Reviewed-by: Amitay Isaacs (cherry picked from commit 5148e02adb7b2ea34da9c826a682c1387773402b) --- ctdb/server/ctdb_daemon.c | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/ctdb/server/ctdb_daemon.c b/ctdb/server/ctdb_daemon.c index 47e49dfa3cd..d3ea2631db0 100644 --- a/ctdb/server/ctdb_daemon.c +++ b/ctdb/server/ctdb_daemon.c @@ -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 -- 2.34.1