From d28c2a5c97a14f3fc83b3665ba28deefbeee805e Mon Sep 17 00:00:00 2001 From: Matthew Leach Date: Sat, 26 Mar 2016 18:50:14 +0000 Subject: [PATCH v3 2/4] Read the number of sockets passed by systemd. * src/emacs.c (systemd_socket): New variable for storing the socket descriptor passed in by systemd. (main): Call sd_listen_fds to read the number of sockets passed. --- src/emacs.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/emacs.c b/src/emacs.c index 95d1905..df5fade 100644 --- a/src/emacs.c +++ b/src/emacs.c @@ -56,6 +56,11 @@ along with GNU Emacs. If not, see . */ #include #endif +#ifdef HAVE_SYSTEMD +#include +#include +#endif /* HAVE_SYSTEMD */ + #ifdef HAVE_WINDOW_SYSTEM #include TERM_HEADER #endif /* HAVE_WINDOW_SYSTEM */ @@ -195,6 +200,12 @@ int daemon_pipe[2]; HANDLE w32_daemon_event; #endif +#ifdef HAVE_SYSTEMD +/* The socket descriptor passed by systemd. If nothing has been + passed, this will be 0. */ +int systemd_socket = 0; +#endif /* HAVE_SYSTEMD */ + /* Save argv and argc. */ char **initial_argv; int initial_argc; @@ -997,6 +1008,24 @@ main (int argc, char **argv) exit (1); } +#ifdef HAVE_SYSTEMD + /* Read the number of sockets passed through by systemd. */ + systemd_socket = sd_listen_fds(1); + + if (systemd_socket > 1) + { + fprintf (stderr, "\nWarning: systemd has passed more than one socket to the Emacs process.\n\ +Try adding 'Accept=false' in the Emacs socket unit file.\n"); + systemd_socket = 0; + } + else if (systemd_socket == 1 && + sd_is_socket (SD_LISTEN_FDS_START, + AF_UNSPEC, SOCK_STREAM, 1) >= 0) + systemd_socket = SD_LISTEN_FDS_START; + else + systemd_socket = 0; +#endif + #ifndef DAEMON_MUST_EXEC #ifdef USE_GTK fprintf (stderr, "\nWarning: due to a long standing Gtk+ bug\nhttp://bugzilla.gnome.org/show_bug.cgi?id=85715\n\ -- 2.7.4