From 1a0e1f89246685730612819d23a8398b340cb673 Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Wed, 29 Jan 2020 16:28:46 +1100 Subject: [PATCH] ctdb-daemon: Fork when not interactive and test mode is enabled There is no sane way of keeping stdin open when using the shell to background ctdbd in local_daemons.sh. Instead, have ctdbd fork when not interactive and when test mode is enabled. become_daemon() can't be used for this: if it forks then it also closes stdin. For the interactive case, become_daemon() wasn't doing anything special, so do nothing instead. Signed-off-by: Martin Schwenke Reviewed-by: Amitay Isaacs --- ctdb/server/ctdb_daemon.c | 28 ++++++++++++++++++++++++++-- ctdb/tests/local_daemons.sh | 2 +- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/ctdb/server/ctdb_daemon.c b/ctdb/server/ctdb_daemon.c index 8b4c05887f9..7ebb419bc1f 100644 --- a/ctdb/server/ctdb_daemon.c +++ b/ctdb/server/ctdb_daemon.c @@ -1439,6 +1439,22 @@ static int setup_stdin_handler(struct ctdb_context *ctdb) return 0; } +static void fork_only(void) +{ + pid_t pid; + + pid = fork(); + if (pid == -1) { + D_ERR("Fork failed (errno=%d)\n", errno); + exit(1); + } + + if (pid != 0) { + /* Parent simply exits... */ + exit(0); + } +} + /* start the protocol going as a daemon */ @@ -1448,9 +1464,17 @@ int ctdb_start_daemon(struct ctdb_context *ctdb, { int res, ret = -1; struct tevent_fd *fde; - bool do_fork = !(interactive || test_mode_enabled); - become_daemon(do_fork, !do_fork, false); + /* Fork if not interactive */ + if (!interactive) { + if (test_mode_enabled) { + /* Keep stdin open */ + fork_only(); + } else { + /* Fork, close stdin, start a session */ + become_daemon(true, false, false); + } + } ignore_signal(SIGPIPE); ignore_signal(SIGUSR1); diff --git a/ctdb/tests/local_daemons.sh b/ctdb/tests/local_daemons.sh index 9306697f92a..e9f9423cb0c 100755 --- a/ctdb/tests/local_daemons.sh +++ b/ctdb/tests/local_daemons.sh @@ -354,7 +354,7 @@ local_daemons_start () onnode_common - onnode -i "$_nodes" "${VALGRIND:-} ctdbd &" + onnode -i "$_nodes" "${VALGRIND:-} ctdbd" } local_daemons_stop () -- 2.47.2