Skip to content

Commit

Permalink
diod: always run in the foreground
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
garlick committed Jan 19, 2025
1 parent cc881bd commit 3dc9363
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 56 deletions.
36 changes: 1 addition & 35 deletions src/cmd/diod.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
#include "config.h"
#endif
#include <poll.h>
#ifndef _BSD_SOURCE
#define _BSD_SOURCE /* daemon () */
#endif
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
Expand Down Expand Up @@ -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);
Expand All @@ -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'},
Expand All @@ -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"
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
**/
Expand Down Expand Up @@ -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 ())
Expand Down
13 changes: 0 additions & 13 deletions src/libdiod/diod_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -77,7 +76,6 @@
typedef struct {
int debuglevel;
int nwthreads;
int foreground;
int auth_required;
int hostname_lookup;
int statfs_passthru;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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; }
Expand Down
5 changes: 0 additions & 5 deletions src/libdiod/diod_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions src/libdiod/test/configfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion tests/misc/t15
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3dc9363

Please sign in to comment.