#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;
};
const char *ctdbd_pidfile = NULL;
+static struct pidfile_context *ctdbd_pidfile_ctx = NULL;
static void daemon_incoming_packet(void *, struct ctdb_req_header *);
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);
}
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