The branch, master has been updated via 9468d0f util: Add error handling to become_daemon() via ed8c914 util: Move become_daemon.c to samba-util-core via b9804e9 util: Modernise logging via a500925 util: Make function definitions consistent with header file via 298af74 util: Add become_daemon.h via 2b982fc util: Avoid use of includes.h from beeec1f tests: replace traffic_summary test with python blackbox test
https://git.samba.org/?p=samba.git;a=shortlog;h=master - Log ----------------------------------------------------------------- commit 9468d0f41e37d505502f3faee736aaf3b44a17ff Author: Martin Schwenke <mar...@meltin.net> Date: Tue Aug 15 12:41:03 2017 +1000 util: Add error handling to become_daemon() Log failure and exit if fork() or setsid() fails. Leave the logic in the non-setsid() code as it is. This is probably meant to fall through on failure of either opening /dev/tty or ioctl(). Documentation for the ioctl() failure case is far from clear. Signed-off-by: Martin Schwenke <mar...@meltin.net> Reviewed-by: Andreas Schneider <a...@samba.org> Autobuild-User(master): Andreas Schneider <a...@cryptomilk.org> Autobuild-Date(master): Thu Aug 17 11:48:32 CEST 2017 on sn-devel-144 commit ed8c914b42428eaf134bb88770e4e6edaf83d494 Author: Martin Schwenke <mar...@meltin.net> Date: Tue Aug 15 11:43:12 2017 +1000 util: Move become_daemon.c to samba-util-core So that CTDB can use it. Signed-off-by: Martin Schwenke <mar...@meltin.net> Reviewed-by: Andreas Schneider <a...@samba.org> commit b9804e9e3c665bd2f5e43407226040231737fc63 Author: Martin Schwenke <mar...@meltin.net> Date: Tue Aug 15 11:41:58 2017 +1000 util: Modernise logging Switch to using DBG_ERR(), wrap logging/sd_notifyf() lines. Signed-off-by: Martin Schwenke <mar...@meltin.net> Reviewed-by: Andreas Schneider <a...@samba.org> commit a50092531af8c837c6e45c3c755fcb20b6ce323b Author: Martin Schwenke <mar...@meltin.net> Date: Tue Aug 15 11:22:45 2017 +1000 util: Make function definitions consistent with header file no_process_group -> no_session, name -> daemon, drop _PUBLIC_. Signed-off-by: Martin Schwenke <mar...@meltin.net> Reviewed-by: Andreas Schneider <a...@samba.org> commit 298af748ac09963f0bcacdba200ee43e081bbaa8 Author: Martin Schwenke <mar...@meltin.net> Date: Tue Aug 15 11:12:35 2017 +1000 util: Add become_daemon.h Rename argument no_process_group to no_session to describe what it actually does. Consistently use "daemon" for name of daemon argument. Add documentation. Signed-off-by: Martin Schwenke <mar...@meltin.net> Reviewed-by: Andreas Schneider <a...@samba.org> commit 2b982fc309c6c09e5f451075f135a3478be5eabf Author: Martin Schwenke <mar...@meltin.net> Date: Tue Aug 15 11:11:39 2017 +1000 util: Avoid use of includes.h Signed-off-by: Martin Schwenke <mar...@meltin.net> Reviewed-by: Andreas Schneider <a...@samba.org> ----------------------------------------------------------------------- Summary of changes: lib/util/become_daemon.c | 66 ++++++++++++++++++++++--------------- lib/util/become_daemon.h | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/util/samba_util.h | 28 +--------------- lib/util/wscript_build | 5 +-- 4 files changed, 130 insertions(+), 55 deletions(-) create mode 100644 lib/util/become_daemon.h Changeset truncated at 500 lines: diff --git a/lib/util/become_daemon.c b/lib/util/become_daemon.c index 9979fad..22c1778 100644 --- a/lib/util/become_daemon.c +++ b/lib/util/become_daemon.c @@ -21,40 +21,41 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include "includes.h" +#include "replace.h" #include "system/filesys.h" #include "system/locale.h" #if defined(HAVE_LIBSYSTEMD_DAEMON) || defined(HAVE_LIBSYSTEMD) #include <systemd/sd-daemon.h> #endif -#include "lib/util/close_low_fd.h" + +#include "close_low_fd.h" +#include "debug.h" + +#include "become_daemon.h" /******************************************************************* Close the low 3 fd's and open dev/null in their place. ********************************************************************/ -_PUBLIC_ void close_low_fds(bool stdin_too, bool stdout_too, bool stderr_too) +void close_low_fds(bool stdin_too, bool stdout_too, bool stderr_too) { if (stdin_too) { int ret = close_low_fd(0); if (ret != 0) { - DEBUG(0, ("%s: close_low_fd(0) failed: %s\n", - __func__, strerror(ret))); + DBG_ERR("close_low_fd(0) failed: %s\n", strerror(ret)); } } if (stdout_too) { int ret = close_low_fd(1); if (ret != 0) { - DEBUG(0, ("%s: close_low_fd(1) failed: %s\n", - __func__, strerror(ret))); + DBG_ERR("close_low_fd(1) failed: %s\n", strerror(ret)); } } if (stderr_too) { int ret = close_low_fd(2); if (ret != 0) { - DEBUG(0, ("%s: close_low_fd(2) failed: %s\n", - __func__, strerror(ret))); + DBG_ERR("close_low_fd(2) failed: %s\n", strerror(ret)); } } } @@ -63,14 +64,20 @@ _PUBLIC_ void close_low_fds(bool stdin_too, bool stdout_too, bool stderr_too) Become a daemon, discarding the controlling terminal. ****************************************************************************/ -_PUBLIC_ void become_daemon(bool do_fork, bool no_process_group, bool log_stdout) +void become_daemon(bool do_fork, bool no_session, bool log_stdout) { pid_t newpid; if (do_fork) { newpid = fork(); + if (newpid == -1) { + exit_daemon("Fork failed", errno); + } if (newpid) { #if defined(HAVE_LIBSYSTEMD_DAEMON) || defined(HAVE_LIBSYSTEMD) - sd_notifyf(0, "READY=0\nSTATUS=Starting process...\nMAINPID=%lu", (unsigned long) newpid); + sd_notifyf(0, + "READY=0\nSTATUS=Starting process...\n" + "MAINPID=%lu", + (unsigned long) newpid); #endif /* HAVE_LIBSYSTEMD_DAEMON */ _exit(0); } @@ -78,9 +85,14 @@ _PUBLIC_ void become_daemon(bool do_fork, bool no_process_group, bool log_stdout /* detach from the terminal */ #ifdef HAVE_SETSID - if (!no_process_group) setsid(); + if (!no_session) { + int ret = setsid(); + if (ret == -1) { + exit_daemon("Failed to create session", errno); + } + } #elif defined(TIOCNOTTY) - if (!no_process_group) { + if (!no_session) { int i = open("/dev/tty", O_RDWR, 0); if (i != -1) { ioctl(i, (int) TIOCNOTTY, (char *)0); @@ -96,7 +108,7 @@ _PUBLIC_ void become_daemon(bool do_fork, bool no_process_group, bool log_stdout close_low_fds(do_fork, !log_stdout, false); } -_PUBLIC_ void exit_daemon(const char *msg, int error) +void exit_daemon(const char *msg, int error) { #if defined(HAVE_LIBSYSTEMD_DAEMON) || defined(HAVE_LIBSYSTEMD) if (msg == NULL) { @@ -108,29 +120,31 @@ _PUBLIC_ void exit_daemon(const char *msg, int error) msg, error); #endif - DEBUG(0, ("STATUS=daemon failed to start: %s, error code %d\n", msg, error)); + DBG_ERR("STATUS=daemon failed to start: %s, error code %d\n", + msg, error); exit(1); } -_PUBLIC_ void daemon_ready(const char *name) +void daemon_ready(const char *daemon) { - if (name == NULL) { - name = "Samba"; + if (daemon == NULL) { + daemon = "Samba"; } #if defined(HAVE_LIBSYSTEMD_DAEMON) || defined(HAVE_LIBSYSTEMD) - sd_notifyf(0, "READY=1\nSTATUS=%s: ready to serve connections...", name); + 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\n", name)); + DBG_ERR("STATUS=daemon '%s' finished starting up and ready to serve " + "connections\n", daemon); } -_PUBLIC_ void daemon_status(const char *name, const char *msg) +void daemon_status(const char *daemon, const char *msg) { - if (name == NULL) { - name = "Samba"; + if (daemon == NULL) { + daemon = "Samba"; } #if defined(HAVE_LIBSYSTEMD_DAEMON) || defined(HAVE_LIBSYSTEMD) - sd_notifyf(0, "\nSTATUS=%s: %s", name, msg); + sd_notifyf(0, "\nSTATUS=%s: %s", daemon, msg); #endif - DEBUG(0, ("STATUS=daemon '%s' : %s", name, msg)); + DBG_ERR("STATUS=daemon '%s' : %s", daemon, msg); } diff --git a/lib/util/become_daemon.h b/lib/util/become_daemon.h new file mode 100644 index 0000000..d697a68 --- /dev/null +++ b/lib/util/become_daemon.h @@ -0,0 +1,86 @@ +/* + Unix SMB/CIFS implementation. + Samba utility functions + Copyright (C) Andrew Tridgell 1992-1998 + Copyright (C) Jeremy Allison 2001-2002 + Copyright (C) Simo Sorce 2001 + Copyright (C) Jim McDonough (j...@us.ibm.com) 2003. + Copyright (C) James J Myers 2003 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef _BECOME_DAEMON_H +#define _BECOME_DAEMON_H + +#include <stdbool.h> + +/** + * @file become_daemon.h + * + * @brief Utilities for demonising + */ + +/** + * @brief Close the low 3 file descriptors and open /dev/null in their place + * + * @param[in] stdin_too Should stdin be closed? + * @param[in] stdout_too Should stdout be closed? + * @param[in] stderr_too Should stderr be closed? +**/ +void close_low_fds(bool stdin_too, bool stdout_too, bool stderr_too); + +/** + * @brief Become a daemon, optionally discarding the controlling terminal + * + * @param[in] do_fork Should the process fork? + * @param[in] no_session Don't start a new session + * @param[in] log_stdour Should stdout be closed? +**/ +void become_daemon(bool do_fork, bool no_session, bool log_stdout); + +/** + * @brief Exit daemon and log an error message at ERR level + * + * Optionally report failure to systemd if systemd integration is + * enabled. + * + * @param[in] msg Message to log, generated from error if NULL + * @param[in] error Errno of error that occurred +**/ +void exit_daemon(const char *msg, int error); + +/** + * @brief Log at ERR level that the daemon is ready to serve connections + * + * Optionally report status to systemd if systemd integration is enabled. + * + * @param[in] daemon Name of daemon to include it message +**/ +void daemon_ready(const char *daemon); + +/** + * @brief Log at ERR level the specified daemon status + * + * For example if it is not ready to serve connections and is waiting + * for some event to happen. + * + * Optionally report status to systemd if systemd integration is enabled. + * + * @param[in] daemon Name of daemon to include it message + * @param[in] msg Message to log +**/ +void daemon_status(const char *daemon, const char *msg); + +#endif /* _BECOME_DAEMON_H */ diff --git a/lib/util/samba_util.h b/lib/util/samba_util.h index 0bb04b3..3daf3df 100644 --- a/lib/util/samba_util.h +++ b/lib/util/samba_util.h @@ -567,33 +567,7 @@ int gen_fnmatch(const char *pattern, const char *string); #include "idtree.h" #include "idtree_random.h" -/** - Close the low 3 fd's and open dev/null in their place -**/ -_PUBLIC_ void close_low_fds(bool stdin_too, bool stdout_too, bool stderr_too); - -/** - Become a daemon, discarding the controlling terminal. -**/ -_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); - -/* - * Report the daemon status. For example if it is not ready to serve connections - * and is waiting for some event to happen. - */ -_PUBLIC_ void daemon_status(const char *name, const char *msg); +#include "become_daemon.h" /** * @brief Get a password from the console. diff --git a/lib/util/wscript_build b/lib/util/wscript_build index 989db36..bb3cdd1 100644 --- a/lib/util/wscript_build +++ b/lib/util/wscript_build @@ -71,7 +71,8 @@ bld.SAMBA_SUBSYSTEM('samba-util-core', source='''data_blob.c util_file.c time.c signal.c util.c idtree.c fault.c substitute.c util_process.c util_strlist.c - strv_util.c bitmap.c select.c pidfile.c''', + strv_util.c bitmap.c select.c pidfile.c + become_daemon.c ''', deps='''time-basic samba-debug socket-blocking talloc tevent execinfo pthread strv''', local_include=False) @@ -114,7 +115,7 @@ else: bld.SAMBA_LIBRARY('samba-util', source='''talloc_stack.c smb_threads.c - rbtree.c rfc1738.c become_daemon.c system.c getpass.c + rbtree.c rfc1738.c system.c getpass.c genrand_util.c fsusage.c params.c util_id.c util_net.c util_strlist_v3.c util_paths.c -- Samba Shared Repository