When run as a systemd service using the systemd notification method to synchronise startup, logging should be done to syslog so the log entry format is consistent between daemon and systemd usage. So, rather than run use the forground option, add an option to tell the automounter it's being run as a systemd service and use syslog for logging when its present on the command line. Signed-off-by: Ian Kent <raven@xxxxxxxxxx> --- CHANGELOG | 1 + daemon/automount.c | 22 +++++++++++++++++++--- include/automount.h | 1 + man/automount.8 | 4 ++++ samples/autofs.service.in | 2 +- 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 4e1d9c98..21fc9ca7 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -47,6 +47,7 @@ xx/xx/2018 autofs-5.1.5 - update build info with systemd. - use flags for startup boolean options. - move close stdio descriptors to become_daemon(). +- add systemd service command line option. 19/12/2017 autofs-5.1.4 - fix spec file url. diff --git a/daemon/automount.c b/daemon/automount.c index c1360ed6..4628f20c 100644 --- a/daemon/automount.c +++ b/daemon/automount.c @@ -1210,13 +1210,21 @@ static void become_daemon(unsigned int flags) } /* Detach from foreground process */ - if (flags & DAEMON_FLAGS_FOREGROUND) { + if (flags & DAEMON_FLAGS_FOREGROUND && + !(flags & DAEMON_FLAGS_SYSTEMD_SERVICE)) { if ((flags & DAEMON_FLAGS_CHECK_DAEMON) && !aquire_flag_file()) { fprintf(stderr, "%s: program is already running.\n", program); exit(1); } log_to_stderr(); + } else if (flags & DAEMON_FLAGS_SYSTEMD_SERVICE) { + if ((flags & DAEMON_FLAGS_CHECK_DAEMON) && !aquire_flag_file()) { + fprintf(stderr, "%s: program is already running.\n", + program); + exit(1); + } + open_log(); } else { int nullfd; @@ -1925,6 +1933,8 @@ static void usage(void) " -d --debug log debuging info\n" " -Dvariable=value, --define variable=value\n" " define global macro variable\n" + " -S --systemd-service\n" + " run automounter as a systemd service\n" " -f --foreground do not fork into background\n" " -r --random-multimount-selection\n" " use ramdom replicated server selection\n" @@ -2190,7 +2200,7 @@ int main(int argc, char *argv[]) time_t timeout; time_t age = monotonic_time(NULL); struct rlimit rlim; - const char *options = "+hp:t:vmdD:fVrO:l:n:CFM"; + const char *options = "+hp:t:vmdD:SfVrO:l:n:CFM"; static const struct option long_options[] = { {"help", 0, 0, 'h'}, {"pid-file", 1, 0, 'p'}, @@ -2198,6 +2208,7 @@ int main(int argc, char *argv[]) {"verbose", 0, 0, 'v'}, {"debug", 0, 0, 'd'}, {"define", 1, 0, 'D'}, + {"systemd-service", 0, 0, 'S'}, {"foreground", 0, 0, 'f'}, {"random-multimount-selection", 0, 0, 'r'}, {"negative-timeout", 1, 0, 'n'}, @@ -2266,6 +2277,10 @@ int main(int argc, char *argv[]) macro_parse_globalvar(optarg); break; + case 'S': + flags |= DAEMON_FLAGS_SYSTEMD_SERVICE; + break; + case 'f': flags |= DAEMON_FLAGS_FOREGROUND; break; @@ -2653,7 +2668,8 @@ int main(int argc, char *argv[]) } #ifdef WITH_SYSTEMD - sd_notify(1, "READY=1"); + if (flags & DAEMON_FLAGS_SYSTEMD_SERVICE) + sd_notify(1, "READY=1"); #endif state_mach_thid = pthread_self(); diff --git a/include/automount.h b/include/automount.h index 848fd0be..45fde53e 100644 --- a/include/automount.h +++ b/include/automount.h @@ -67,6 +67,7 @@ #endif #define DAEMON_FLAGS_FOREGROUND 0x0001 +#define DAEMON_FLAGS_SYSTEMD_SERVICE 0x0002 #define DAEMON_FLAGS_HAVE_GLOBAL_OPTIONS 0x0004 #define DAEMON_FLAGS_GHOST 0x0008 #define DAEMON_FLAGS_CHECK_DAEMON 0x0010 diff --git a/man/automount.8 b/man/automount.8 index 68d2aaa3..9f92288e 100644 --- a/man/automount.8 +++ b/man/automount.8 @@ -57,6 +57,10 @@ Define a global macro substitution variable. Global definitions are over-ridden macro definitions of the same name specified in mount entries. .TP +.I \-S, \-\-systemd-service +Used when running the automounter as a systemd service to ensure log entry +format is consistent with the log entry format when running as a daemon. +.TP .I "\-f, \-\-foreground" Run the daemon in the foreground and log to stderr instead of syslog." .TP diff --git a/samples/autofs.service.in b/samples/autofs.service.in index 281d31ef..175a806d 100644 --- a/samples/autofs.service.in +++ b/samples/autofs.service.in @@ -6,7 +6,7 @@ Wants=network-online.target rpc-statd.service rpcbind.service [Service] Type=notify EnvironmentFile=-@@autofsconfdir@@/autofs -ExecStart=@@sbindir@@/automount $OPTIONS --foreground --dont-check-daemon +ExecStart=@@sbindir@@/automount $OPTIONS --systemd-service --dont-check-daemon ExecReload=/usr/bin/kill -HUP $MAINPID KillMode=process TimeoutSec=180