From 23a88f868dd431202c49183810a9d4f2fac91416 Mon Sep 17 00:00:00 2001 From: Peter Rajnoha Date: Wed, 14 Mar 2012 15:51:51 +0000 Subject: [PATCH] Use SD_ACTIVATION env. var. in systemd units to better detect systemd in use. LISTEN_PID and LISTEN_FDS environment variables are defined only during systemd "start" action. But we still need to know whether we're activated during "reload" action as well - we use the reload action to call "dmeventd -R"/"lvmetad -R" for statefull daemon restart. We can't use normal "restart" as that is simply composed of "stop" and "start" and we would lose any state the daemon has. --- WHATS_NEW_DM | 1 + daemons/dmeventd/dmeventd.c | 6 ++++++ libdaemon/server/daemon-server.c | 6 ++++++ scripts/dm_event_systemd_red_hat.service.in | 1 + scripts/lvm2_lvmetad_systemd_red_hat.service.in | 1 + 5 files changed, 15 insertions(+) diff --git a/WHATS_NEW_DM b/WHATS_NEW_DM index afa694453..ef3f21058 100644 --- a/WHATS_NEW_DM +++ b/WHATS_NEW_DM @@ -1,5 +1,6 @@ Version 1.02.75 - ================================ + Use SD_ACTIVATION env. var. in systemd units to better detect systemd in use. Do not run a new dmeventd instance on restart if there's no existing one. Make the time window for reading fifo longer 5sec (1.02.73). diff --git a/daemons/dmeventd/dmeventd.c b/daemons/dmeventd/dmeventd.c index b61fb8b83..0b3da1e4a 100644 --- a/daemons/dmeventd/dmeventd.c +++ b/daemons/dmeventd/dmeventd.c @@ -56,6 +56,7 @@ # define OOM_SCORE_ADJ_MIN (-1000) /* Systemd on-demand activation support */ +# define SD_ACTIVATION_ENV_VAR_NAME "SD_ACTIVATION" # define SD_LISTEN_PID_ENV_VAR_NAME "LISTEN_PID" # define SD_LISTEN_FDS_ENV_VAR_NAME "LISTEN_FDS" # define SD_LISTEN_FDS_START 3 @@ -1698,6 +1699,10 @@ static int _systemd_handover(struct dm_event_fifos *fifos) memset(fifos, 0, sizeof(*fifos)); + /* SD_ACTIVATION must be set! */ + if (!(e = getenv(SD_ACTIVATION_ENV_VAR_NAME)) || strcmp(e, "1")) + goto out; + /* LISTEN_PID must be equal to our PID! */ if (!(e = getenv(SD_LISTEN_PID_ENV_VAR_NAME))) goto out; @@ -1729,6 +1734,7 @@ static int _systemd_handover(struct dm_event_fifos *fifos) } out: + unsetenv(SD_ACTIVATION_ENV_VAR_NAME); unsetenv(SD_LISTEN_PID_ENV_VAR_NAME); unsetenv(SD_LISTEN_FDS_ENV_VAR_NAME); return r; diff --git a/libdaemon/server/daemon-server.c b/libdaemon/server/daemon-server.c index 3cdec3483..3e72cd115 100644 --- a/libdaemon/server/daemon-server.c +++ b/libdaemon/server/daemon-server.c @@ -69,6 +69,7 @@ static void _exit_handler(int sig __attribute__((unused))) # define OOM_SCORE_ADJ_MIN (-1000) /* Systemd on-demand activation support */ +# define SD_ACTIVATION_ENV_VAR_NAME "SD_ACTIVATION" # define SD_LISTEN_PID_ENV_VAR_NAME "LISTEN_PID" # define SD_LISTEN_FDS_ENV_VAR_NAME "LISTEN_FDS" # define SD_LISTEN_FDS_START 3 @@ -164,6 +165,10 @@ static int _systemd_handover(struct daemon_state *ds) unsigned long env_pid, env_listen_fds; int r = 0; + /* SD_ACTIVATION must be set! */ + if (!(e = getenv(SD_ACTIVATION_ENV_VAR_NAME)) || strcmp(e, "1")) + goto out; + /* LISTEN_PID must be equal to our PID! */ if (!(e = getenv(SD_LISTEN_PID_ENV_VAR_NAME))) goto out; @@ -188,6 +193,7 @@ static int _systemd_handover(struct daemon_state *ds) ds->socket_fd = SD_FD_SOCKET_SERVER; out: + unsetenv(SD_ACTIVATION_ENV_VAR_NAME); unsetenv(SD_LISTEN_PID_ENV_VAR_NAME); unsetenv(SD_LISTEN_FDS_ENV_VAR_NAME); return r; diff --git a/scripts/dm_event_systemd_red_hat.service.in b/scripts/dm_event_systemd_red_hat.service.in index dc83c6d8d..02a24c6f2 100644 --- a/scripts/dm_event_systemd_red_hat.service.in +++ b/scripts/dm_event_systemd_red_hat.service.in @@ -9,6 +9,7 @@ DefaultDependencies=no Type=forking ExecStart=@sbindir@/dmeventd ExecReload=@sbindir@/dmeventd -R +Environment=SD_ACTIVATION=1 PIDFile=@DMEVENTD_PIDFILE@ OOMScoreAdjust=-1000 diff --git a/scripts/lvm2_lvmetad_systemd_red_hat.service.in b/scripts/lvm2_lvmetad_systemd_red_hat.service.in index afb46c229..44827fde1 100644 --- a/scripts/lvm2_lvmetad_systemd_red_hat.service.in +++ b/scripts/lvm2_lvmetad_systemd_red_hat.service.in @@ -11,6 +11,7 @@ NonBlocking=true ExecStart=@sbindir@/lvmetad ExecStartPost=@sbindir@/vgscan ExecReload=@sbindir@/lvmetad -R +Environment=SD_ACTIVATION=1 Restart=on-abort PIDFile=@DEFAULT_RUN_DIR@/lvmetad.pid