From 3dc9363f7bf8e0a6e881e3c2694be61e53a5f457 Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Sat, 18 Jan 2025 20:11:59 -0800 Subject: [PATCH] diod: always run in the foreground Problem: diod puts itself in the background by default, but this can be confusing and unexpected when running the service manually. In fact, given that systemd can manage daemons running in the foreground, it is probably no longer useful at all. Drop the --foreground option and never daemonize. Update tests. --- src/cmd/diod.c | 36 +---------------------------------- src/libdiod/diod_conf.c | 13 ------------- src/libdiod/diod_conf.h | 5 ----- src/libdiod/test/configfile.c | 2 -- tests/misc/t15 | 2 +- 5 files changed, 2 insertions(+), 56 deletions(-) diff --git a/src/cmd/diod.c b/src/cmd/diod.c index 50b7461b..37595b54 100644 --- a/src/cmd/diod.c +++ b/src/cmd/diod.c @@ -14,9 +14,6 @@ #include "config.h" #endif #include -#ifndef _BSD_SOURCE -#define _BSD_SOURCE /* daemon () */ -#endif #include #include #include @@ -57,7 +54,6 @@ typedef enum { SRV_FILEDES, SRV_SOCKTEST, SRV_NORMAL } srvmode_t; -static void _daemonize (void); static void _setrlimit (void); static void _become_user (char *name, uid_t uid, int realtoo); static void _service_run (srvmode_t mode, int rfdno, int wfdno); @@ -66,10 +62,9 @@ static void _service_run (srvmode_t mode, int rfdno, int wfdno); #define NR_OPEN 1048576 /* works on RHEL 5 x86_64 arch */ #endif -static const char *options = "fr:w:d:l:t:e:Eo:u:SL:nHpc:NU:s"; +static const char *options = "r:w:d:l:t:e:Eo:u:SL:nHpc:NU:s"; static const struct option longopts[] = { - {"foreground", no_argument, 0, 'f'}, {"rfdno", required_argument, 0, 'r'}, {"wfdno", required_argument, 0, 'w'}, {"debug", required_argument, 0, 'd'}, @@ -96,7 +91,6 @@ usage() { fprintf (stderr, "Usage: diod [OPTIONS]\n" -" -f,--foreground do not fork and disassociate with tty\n" " -r,--rfdno service connected client on read file descriptor\n" " -w,--wfdno service connected client on write file descriptor\n" " -l,--listen IP:PORT set interface to listen on (multiple -l allowed)\n" @@ -149,9 +143,6 @@ main(int argc, char **argv) opterr = 0; while ((c = getopt_long (argc, argv, options, longopts, NULL)) != -1) { switch (c) { - case 'f': /* --foreground */ - diod_conf_set_foreground (1); - break; case 'r': /* --rfdno */ mode = SRV_FILEDES; rfdno = strtoul (optarg, NULL, 10); @@ -339,26 +330,6 @@ _setrlimit (void) } -/* Create run directory if it doesn't exist and chdir there. - * Disassociate from parent's controlling tty. - * Exit on error. - */ -static void -_daemonize (void) -{ - char rdir[PATH_MAX]; - - snprintf (rdir, sizeof(rdir), "%s/run/diod", X_LOCALSTATEDIR); - if (mkdir (rdir, 0755) < 0 && errno != EEXIST) { - msg ("failed to find/create %s, running out of /tmp", rdir); - snprintf (rdir, sizeof(rdir), "/tmp"); - } - if (chdir (rdir) < 0) - err_exit ("chdir %s", rdir); - if (daemon (1, 0) < 0) - err_exit ("daemon"); -} - /** ** Service startup **/ @@ -599,11 +570,6 @@ _service_run (srvmode_t mode, int rfdno, int wfdno) } } - if (!diod_conf_get_foreground () && mode != SRV_FILEDES) - _daemonize (); /* implicit fork - no pthreads before this */ - if (!diod_conf_get_foreground () && mode != SRV_FILEDES) - diod_log_set_dest (diod_conf_get_logdest ()); - /* drop root */ if (euid == 0) { if (diod_conf_get_allsquash ()) diff --git a/src/libdiod/diod_conf.c b/src/libdiod/diod_conf.c index 37439944..63f9b9c5 100644 --- a/src/libdiod/diod_conf.c +++ b/src/libdiod/diod_conf.c @@ -56,7 +56,6 @@ /* ro_mask values to protect attribute from overwrite by config file */ #define RO_DEBUGLEVEL 0x00000001 #define RO_NWTHREADS 0x00000002 -#define RO_FOREGROUND 0x00000004 #define RO_AUTH_REQUIRED 0x00000008 #define RO_RUNASUID 0x00000010 #define RO_USERDB 0x00000020 @@ -77,7 +76,6 @@ typedef struct { int debuglevel; int nwthreads; - int foreground; int auth_required; int hostname_lookup; int statfs_passthru; @@ -165,7 +163,6 @@ diod_conf_init (void) { config.debuglevel = DFLT_DEBUGLEVEL; config.nwthreads = DFLT_NWTHREADS; - config.foreground = DFLT_FOREGROUND; config.auth_required = DFLT_AUTH_REQUIRED; config.hostname_lookup = DFLT_HOSTNAME_LOOKUP; config.statfs_passthru = DFLT_STATFS_PASSTHRU; @@ -242,16 +239,6 @@ void diod_conf_set_nwthreads (int i) config.ro_mask |= RO_NWTHREADS; } -/* foreground - run daemon in foreground - */ -int diod_conf_get_foreground (void) { return config.foreground; } -int diod_conf_opt_foreground (void) { return config.ro_mask & RO_FOREGROUND; } -void diod_conf_set_foreground (int i) -{ - config.foreground = i; - config.ro_mask |= RO_FOREGROUND; -} - /* auth_required - whether to accept unauthenticated attaches */ int diod_conf_get_auth_required (void) { return config.auth_required; } diff --git a/src/libdiod/diod_conf.h b/src/libdiod/diod_conf.h index cba4eac5..dd7d8c37 100644 --- a/src/libdiod/diod_conf.h +++ b/src/libdiod/diod_conf.h @@ -16,7 +16,6 @@ #define DFLT_DEBUGLEVEL 0 #define DFLT_NWTHREADS 16 #define DFLT_MAXMMAP 0 -#define DFLT_FOREGROUND 0 #define DFLT_AUTH_REQUIRED 1 #define DFLT_HOSTNAME_LOOKUP 1 #define DFLT_STATFS_PASSTHRU 0 @@ -50,10 +49,6 @@ int diod_conf_get_nwthreads (void); int diod_conf_opt_nwthreads (void); void diod_conf_set_nwthreads (int i); -int diod_conf_get_foreground (void); -int diod_conf_opt_foreground (void); -void diod_conf_set_foreground (int i); - int diod_conf_get_auth_required (void); int diod_conf_opt_auth_required (void); void diod_conf_set_auth_required (int i); diff --git a/src/libdiod/test/configfile.c b/src/libdiod/test/configfile.c index 4e4e9acd..79a93119 100644 --- a/src/libdiod/test/configfile.c +++ b/src/libdiod/test/configfile.c @@ -61,7 +61,6 @@ exports = {\n\ is (s, path, "configpath is %s", path); ok (diod_conf_get_debuglevel () == DFLT_DEBUGLEVEL, "debuglevel is default"); ok (diod_conf_get_nwthreads () == DFLT_NWTHREADS, "nwthreads is default"); - ok (diod_conf_get_foreground () == DFLT_FOREGROUND, "foreground is default"); ok (diod_conf_get_auth_required () == DFLT_AUTH_REQUIRED, "auth_required is default"); ok (diod_conf_get_hostname_lookup () == DFLT_HOSTNAME_LOOKUP, @@ -156,7 +155,6 @@ exports = { \"/g/g1\" }\n"; is (s, path, "configpath is %s", path); ok (diod_conf_get_debuglevel () == DFLT_DEBUGLEVEL, "debuglevel is default"); ok (diod_conf_get_nwthreads () == 64, "nwthreads is 64"); - ok (diod_conf_get_foreground () == DFLT_FOREGROUND, "foreground is default"); ok (diod_conf_get_auth_required () != 0, "auth_required is true"); ok (diod_conf_get_hostname_lookup () == DFLT_HOSTNAME_LOOKUP, "hostname_lookup is default"); diff --git a/tests/misc/t15 b/tests/misc/t15 index 77f0f8c6..c7c7f5de 100755 --- a/tests/misc/t15 +++ b/tests/misc/t15 @@ -12,7 +12,7 @@ bg_test () TEST=$(basename $0 | cut -d- -f1) sockfile=$(mktemp) bg_test $sockfile & -${MISC_SRCDIR}/memcheck ${TOP_BUILDDIR}/src/cmd/diod -c /dev/null -n -f -e ctl -l $sockfile -s >$TEST.out 2>&1 +${MISC_SRCDIR}/memcheck ${TOP_BUILDDIR}/src/cmd/diod -c /dev/null -n -e ctl -l $sockfile -s >$TEST.out 2>&1 diff ${MISC_SRCDIR}/$TEST.exp $TEST.out >$TEST.diff rm -f $sockfile wait %1