From 19f5f7b19b0dcdae87476a3fd51c41f840b2b80f Mon Sep 17 00:00:00 2001 From: Lucas Werkmeister Date: Sat, 9 Jun 2018 15:01:08 +0200 Subject: [PATCH] Notify systemd in daemon-initialized and kill-emacs (Bug#31498) With --[bg-]daemon and Type=forking, systemd will only consider the daemon to have fully started up once the original process exits, and will wait until then to start units depending on the Emacs service. To get the same functionality with --fg-daemon, use Type=notify instead of Type=simple and explicitly send a readiness notification to systemd at the point where the forked process would in --bg-daemon mode notify its parent process and cause it to exit. Similarly, notify systemd at the beginning of the shutdown process as well. (Both of these calls are successful no-ops if emacs was not started by systemd.) * etc/emacs.service: Update Type. * src/emacs.c (daemon-initialized) [HAVE_LIBSYSTEMD]: * src/emacs.c (kill-emacs) [HAVE_LIBSYSTEMD]: Call sd_notify(). --- etc/NEWS | 8 ++++++++ etc/emacs.service | 2 +- src/emacs.c | 11 +++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/etc/NEWS b/etc/NEWS index 21b648cbb4c..995ceb67b78 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -81,6 +81,14 @@ work right without some adjustment: - you can use the new 'package-quickstart' so activation of packages does not need to pay attention to 'package-load-list' or 'package-user-dir' any more. +--- +** Emacs now notifies systemd when startup finishes or shutdown begins. +Units that are ordered after 'emacs.service' will only be started +after Emacs has finished initialization and is ready for use. +(If your Emacs is installed in a non-standard location and you copied the +emacs.service file to eg ~/.config/systemd/user/, you will need to copy +the new version of the file again.) + * Changes in Emacs 27.1 diff --git a/etc/emacs.service b/etc/emacs.service index b29177b120c..dbcb6bc301e 100644 --- a/etc/emacs.service +++ b/etc/emacs.service @@ -7,7 +7,7 @@ Description=Emacs text editor Documentation=info:emacs man:emacs(1) https://gnu.org/software/emacs/ [Service] -Type=simple +Type=notify ExecStart=emacs --fg-daemon ExecStop=emacsclient --eval "(kill-emacs)" Environment=SSH_AUTH_SOCK=%t/keyring/ssh diff --git a/src/emacs.c b/src/emacs.c index 861d70735ca..130a9f8fc8e 100644 --- a/src/emacs.c +++ b/src/emacs.c @@ -2019,6 +2019,10 @@ all of which are called before Emacs is actually killed. */ { int exit_code; +#ifdef HAVE_LIBSYSTEMD + sd_notify(0, "STOPPING=1"); +#endif /* HAVE_LIBSYSTEMD */ + /* Fsignal calls emacs_abort () if it sees that waiting_for_input is set. */ waiting_for_input = 0; @@ -2479,6 +2483,13 @@ from the parent process and its tty file descriptors. */) error ("This function can only be called after loading the init files"); #ifndef WINDOWSNT + if (daemon_type == 1) + { +#ifdef HAVE_LIBSYSTEMD + sd_notify(0, "READY=1"); +#endif /* HAVE_LIBSYSTEMD */ + } + if (daemon_type == 2) { int nfd; -- 2.39.2