From a1b256b0b562481404bda8304e04dee7d7d5e7c9 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 24 May 2010 22:31:38 +0200 Subject: [PATCH] unit: introduce exit.service for exiting from session instances --- Makefile.am | 7 ++++++- fixme | 2 -- man/systemd.special.xml.in | 30 ++++++++++++++++++++++++++++++ src/manager.c | 21 ++++++++++++++------- src/manager.h | 1 + units/session/.gitignore | 1 + units/session/exit.service.in | 8 ++++++++ 7 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 units/session/.gitignore create mode 100644 units/session/exit.service.in diff --git a/Makefile.am b/Makefile.am index a96b89d6..bd252392 100644 --- a/Makefile.am +++ b/Makefile.am @@ -128,7 +128,8 @@ dist_sessionunit_DATA = \ units/session/default.target sessionunit_DATA = \ - units/session/remote-fs.target + units/session/remote-fs.target \ + units/session/exit.service EXTRA_DIST = \ units/basic.target.m4 \ @@ -139,6 +140,7 @@ EXTRA_DIST = \ units/systemd-initctl.service.in \ units/systemd-logger.service.in \ units/syslog.target.in \ + units/session/exit.service.in \ LICENSE \ README \ DISTRO_PORTING @@ -359,6 +361,8 @@ SED_PROCESS = \ $(SED) -e 's,@libexecdir\@,$(libexecdir),g' \ -e 's,@pkglibexecdir\@,$(pkglibexecdir),g' \ -e 's,@SPECIAL_SYSLOG_SERVICE\@,$(SPECIAL_SYSLOG_SERVICE),g' \ + -e 's,@SPECIAL_DBUS_SERVICE\@,$(SPECIAL_DBUS_SERVICE),g' \ + -e 's,@SYSTEMCTL\@,$(bindir)/systemctl,g' \ < $< > $@ units/%: units/%.in Makefile @@ -391,6 +395,7 @@ CLEANFILES = \ units/multi-user.target \ units/remote-fs.target \ units/session/remote-fs.target \ + units/session/exit.service \ man/systemd.special.7 \ man/systemd.special.html diff --git a/fixme b/fixme index 6b6f7f7d..b6752d9b 100644 --- a/fixme +++ b/fixme @@ -64,8 +64,6 @@ * tcpwrap -* introduce exit.target for session instances - Regularly: * look for close() vs. close_nointr() vs. close_nointr_nofail() diff --git a/man/systemd.special.xml.in b/man/systemd.special.xml.in index 0af9c6e7..1c9e88c6 100644 --- a/man/systemd.special.xml.in +++ b/man/systemd.special.xml.in @@ -53,6 +53,7 @@ default.target, display-manager.service, emergency.service, + exit.service, graphical.target, halt.target, kbrequest.target, @@ -635,6 +636,35 @@ shutdown.target, sockets.target, swap.target. + + In addition the following special unit is + understood only when systemd runs as service instance: + + + + exit.service + + A special service unit + for shutting down the + session. + + Applications wanting to + terminate the session should + start this unit. If systemd + receives SIGTERM or SIGINT + when running as session daemon + it will start this + unit. + + Normally, this pulls in + shutdown.target + which in turn should be + conflicted by all units that + want to be shut down on + session exit. + + + diff --git a/src/manager.c b/src/manager.c index 2a773c6d..933dd506 100644 --- a/src/manager.c +++ b/src/manager.c @@ -1791,11 +1791,13 @@ static int manager_dispatch_sigchld(Manager *m) { return 0; } -static void manager_start_target(Manager *m, const char *name) { +static int manager_start_target(Manager *m, const char *name) { int r; if ((r = manager_add_job_by_name(m, JOB_START, name, JOB_REPLACE, true, NULL)) < 0) log_error("Failed to enqueue %s job: %s", name, strerror(-r)); + + return r; } static int manager_process_signal_fd(Manager *m) { @@ -1824,14 +1826,14 @@ static int manager_process_signal_fd(Manager *m) { break; case SIGTERM: - if (m->running_as == MANAGER_INIT) + if (m->running_as == MANAGER_INIT) { /* This is for compatibility with the * original sysvinit */ m->exit_code = MANAGER_REEXECUTE; - else - m->exit_code = MANAGER_EXIT; + break; + } - return 0; + /* Fall through */ case SIGINT: if (m->running_as == MANAGER_INIT) { @@ -1839,8 +1841,13 @@ static int manager_process_signal_fd(Manager *m) { break; } - m->exit_code = MANAGER_EXIT; - return 0; + /* Run the exit target if there is one, if not, just exit. */ + if (manager_start_target(m, SPECIAL_EXIT_SERVICE) < 0) { + m->exit_code = MANAGER_EXIT; + return 0; + } + + break; case SIGWINCH: if (m->running_as == MANAGER_INIT) diff --git a/src/manager.h b/src/manager.h index 22ba04f7..210e6605 100644 --- a/src/manager.h +++ b/src/manager.h @@ -109,6 +109,7 @@ struct Watch { #define SPECIAL_MAIL_TRANSFER_AGENT_TARGET "mail-transfer-agent.target" /* Debian's $mail-{transport|transfer-agent */ #define SPECIAL_BASIC_TARGET "basic.target" #define SPECIAL_RESCUE_TARGET "rescue.target" +#define SPECIAL_EXIT_SERVICE "exit.service" #ifndef SPECIAL_DBUS_SERVICE #define SPECIAL_DBUS_SERVICE "dbus.service" diff --git a/units/session/.gitignore b/units/session/.gitignore new file mode 100644 index 00000000..eeb62b32 --- /dev/null +++ b/units/session/.gitignore @@ -0,0 +1 @@ +exit.service diff --git a/units/session/exit.service.in b/units/session/exit.service.in new file mode 100644 index 00000000..86c8bbc6 --- /dev/null +++ b/units/session/exit.service.in @@ -0,0 +1,8 @@ +[Unit] +Description=Exit +Requires=shutdown.target +After=shutdown.target + +[Service] +Type=finish +ExecStart=@SYSTEMCTL@ --session daemon-exit -- 2.39.5