diff options
author | Alexander Bokovoy <ab@samba.org> | 2014-03-25 12:53:04 +0200 |
---|---|---|
committer | Karolin Seeger <kseeger@samba.org> | 2014-05-20 11:47:41 +0200 |
commit | 2c616189a9f037d26dbdceb34b950002888bb23f (patch) | |
tree | 6ea6c1e9b08cff353ff89e5456312ebd5645b947 /lib/util | |
parent | 79825000597b9772bae4f9e52cd3a3fc5ccb0257 (diff) | |
download | samba-2c616189a9f037d26dbdceb34b950002888bb23f.tar.gz |
add systemd integration
Add --with-systemd / --without-systemd options to check whether
libsystemd-daemon library is available and use it to report service
startup status to systemd for smbd/winbindd/nmbd and AD DC.
The problem it solves is correct reporting of the Samba services
at the point when they are ready to serve clients, important for
high availability software integration.
BUG: https://bugzilla.samba.org/show_bug.cgi?id=10517
Signed-off-by: Alexander Bokovoy <ab@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
(cherry picked from commit 54b5d9a7384ae27b2a26586ff909128427c05abe)
Diffstat (limited to 'lib/util')
-rw-r--r-- | lib/util/become_daemon.c | 37 | ||||
-rw-r--r-- | lib/util/samba_util.h | 12 | ||||
-rwxr-xr-x | lib/util/wscript_build | 2 |
3 files changed, 49 insertions, 2 deletions
diff --git a/lib/util/become_daemon.c b/lib/util/become_daemon.c index 2ca047861f..35c8b3252b 100644 --- a/lib/util/become_daemon.c +++ b/lib/util/become_daemon.c @@ -24,6 +24,9 @@ #include "includes.h" #include "system/filesys.h" #include "system/locale.h" +#if HAVE_SYSTEMD +#include <systemd/sd-daemon.h> +#endif /******************************************************************* Close the low 3 fd's and open dev/null in their place. @@ -75,8 +78,13 @@ _PUBLIC_ void close_low_fds(bool stdin_too, bool stdout_too, bool stderr_too) _PUBLIC_ void become_daemon(bool do_fork, bool no_process_group, bool log_stdout) { + pid_t newpid; if (do_fork) { - if (fork()) { + newpid = fork(); + if (newpid) { +#if HAVE_SYSTEMD + sd_notifyf(0, "READY=0\nSTATUS=Starting process...\nMAINPID=%lu", (unsigned long) newpid); +#endif /* HAVE_SYSTEMD */ _exit(0); } } @@ -100,3 +108,30 @@ _PUBLIC_ void become_daemon(bool do_fork, bool no_process_group, bool log_stdout * never close stderr (but debug might dup it onto a log file) */ close_low_fds(do_fork, !log_stdout, false); } + +_PUBLIC_ void exit_daemon(const char *msg, int error) +{ +#ifdef HAVE_SYSTEMD + if (msg == NULL) { + msg = strerror(error); + } + + sd_notifyf(0, "STATUS=daemon failed to start: %s\n" + "ERRNO=%i", + msg, + error); +#endif + DEBUG(0, ("STATUS=daemon failed to start: %s, error code %d\n", msg, error)); + exit(1); +} + +_PUBLIC_ void daemon_ready(const char *daemon) +{ + if (daemon == NULL) { + daemon = "Samba"; + } +#ifdef HAVE_SYSTEMD + sd_notifyf(0, "READY=1\nSTATUS=%s: ready to serve connections...", daemon); +#endif + DEBUG(0, ("STATUS=daemon '%s' finished starting up and ready to serve connections", daemon)); +} diff --git a/lib/util/samba_util.h b/lib/util/samba_util.h index f52347e536..29967103ea 100644 --- a/lib/util/samba_util.h +++ b/lib/util/samba_util.h @@ -842,6 +842,18 @@ _PUBLIC_ void close_low_fds(bool stdin_too, bool stdout_too, bool stderr_too); _PUBLIC_ void become_daemon(bool do_fork, bool no_process_group, bool log_stdout); /** + Exit daemon and print error message to the log at level 0 + Optionally report failure to systemd if systemd integration is enabled +**/ +_PUBLIC_ void exit_daemon(const char *msg, int error); + +/** + Report that the daemon is ready to serve connections to the log at level 0 + Optionally report status to systemd if systemd integration is enabled +**/ +_PUBLIC_ void daemon_ready(const char *daemon); + +/** * @brief Get a password from the console. * * You should make sure that the buffer is an empty string! diff --git a/lib/util/wscript_build b/lib/util/wscript_build index 39a16135ef..5087116174 100755 --- a/lib/util/wscript_build +++ b/lib/util/wscript_build @@ -10,7 +10,7 @@ bld.SAMBA_LIBRARY('samba-util', server_id.c dprintf.c parmlist.c bitmap.c pidfile.c tevent_debug.c util_process.c''', deps='DYNCONFIG', - public_deps='talloc tevent execinfo uid_wrapper pthread LIBCRYPTO charset util_setid', + public_deps='talloc tevent execinfo uid_wrapper pthread LIBCRYPTO charset util_setid systemd-daemon', public_headers='debug.h attr.h byteorder.h data_blob.h memory.h safe_string.h time.h talloc_stack.h xfile.h dlinklist.h samba_util.h string_wrappers.h', header_path= [ ('dlinklist.h samba_util.h', '.'), ('*', 'util') ], local_include=False, |