From b348e98e47d3ef287fed4d2e272bbf08540e0295 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Tue, 5 May 2009 13:36:17 +0200 Subject: Use tevent for shutdown signals, remove old pidfile, make sssd single-instance. Use tevent signal handling facilities for handlong SIGTERM and SIGINT in the monitor. Remove pidfile on SIGTERM and SIGINT. Make sssd single-instance by checking if we suceeded in signaling the process in the pidfile. --- server/monitor/monitor.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ server/util/server.c | 10 ++++++- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/server/monitor/monitor.c b/server/monitor/monitor.c index 1404858d0..fbc3b880f 100644 --- a/server/monitor/monitor.c +++ b/server/monitor/monitor.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "config.h" #ifdef HAVE_SYS_INOTIFY_H #include @@ -121,6 +122,7 @@ static int add_new_provider(struct mt_ctx *ctx, const char *name); static int monitor_signal_reconf(struct confdb_ctx *cdb, void *pvt); static int update_monitor_config(struct mt_ctx *ctx); +static int monitor_cleanup(); /* dbus_get_monitor_version * Return the monitor version over D-BUS */ @@ -987,6 +989,55 @@ static void monitor_hup(struct tevent_context *ev, update_monitor_config(ctx); } +static int monitor_cleanup(void) +{ + char *file; + int ret; + TALLOC_CTX *tmp_ctx; + + tmp_ctx = talloc_new(NULL); + if (!tmp_ctx) return; + + file = talloc_asprintf(tmp_ctx, "%s/%s.pid", PID_PATH, "sssd"); + if (file == NULL) { + return ENOMEM; + } + + errno = 0; + ret = unlink(file); + if (ret == -1) { + ret = errno; + DEBUG(0, ("Error removing pidfile! (%d [%s])\n", + ret, strerror(ret))); + talloc_free(file); + return errno; + } + + talloc_free(file); + return EOK; +} + +static void monitor_quit(struct tevent_context *ev, + struct tevent_signal *se, + int signum, + int count, + void *siginfo, + void *private_data) +{ + struct mt_ctx *ctx = talloc_get_type(private_data, struct mt_ctx); + + monitor_cleanup(); + +#if HAVE_GETPGRP + if (getpgrp() == getpid()) { + DEBUG(0,("%s: killing children\n", strsignal(signum))); + kill(-getpgrp(), SIGTERM); + } +#endif + + exit(0); +} + #ifdef HAVE_SYS_INOTIFY_H static void config_file_changed(struct tevent_context *ev, struct tevent_fd *fde, @@ -1319,6 +1370,22 @@ int monitor_process_init(TALLOC_CTX *mem_ctx, return EIO; } + /* Set up an event handler for a SIGINT */ + tes = tevent_add_signal(ctx->ev, ctx, SIGINT, 0, + monitor_quit, ctx); + if (tes == NULL) { + talloc_free(ctx); + return EIO; + } + + /* Set up an event handler for a SIGTERM */ + tes = tevent_add_signal(ctx->ev, ctx, SIGTERM, 0, + monitor_quit, ctx); + if (tes == NULL) { + talloc_free(ctx); + return EIO; + } + return EOK; } @@ -1902,6 +1969,9 @@ int main(int argc, const char *argv[]) /* loop on main */ server_loop(main_ctx); + ret = monitor_cleanup(); + if (ret != EOK) return 5; + return 0; } diff --git a/server/util/server.c b/server/util/server.c index b2d6a3ce0..a24029229 100644 --- a/server/util/server.c +++ b/server/util/server.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "util/util.h" #include "ldb.h" #include "confdb/confdb.h" @@ -110,11 +111,18 @@ int pidfile(const char *path, const char *name) if (pid != 0) { errno = 0; ret = kill(pid, 0); - if (ret != 0 && errno != ESRCH) { + /* succeeded in signaling the process -> another sssd process */ + if (ret == 0) { close(fd); free(file); return EEXIST; } + if (ret != 0 && errno != ESRCH) { + err = errno; + close(fd); + free(file); + return err; + } } } -- cgit