From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Matthew Leach Newsgroups: gmane.emacs.devel Subject: [PATCH v3] Add systemd socket launching support Date: Wed, 30 Mar 2016 21:53:36 +0100 Message-ID: <874mbn7kmn.fsf@mattleach.net> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1459371253 4517 80.91.229.3 (30 Mar 2016 20:54:13 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 30 Mar 2016 20:54:13 +0000 (UTC) Cc: Eli Zaretskii To: emacs-devel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Mar 30 22:53:59 2016 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1alN7n-0000My-1D for ged-emacs-devel@m.gmane.org; Wed, 30 Mar 2016 22:53:59 +0200 Original-Received: from localhost ([::1]:56728 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1alN7m-0007bD-Cv for ged-emacs-devel@m.gmane.org; Wed, 30 Mar 2016 16:53:58 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:57663) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1alN7f-0007VE-6z for emacs-devel@gnu.org; Wed, 30 Mar 2016 16:53:53 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1alN7d-0008Sx-3a for emacs-devel@gnu.org; Wed, 30 Mar 2016 16:53:51 -0400 Original-Received: from mx0.mattleach.net ([176.58.118.143]:39036) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1alN7Z-0008Rz-G4; Wed, 30 Mar 2016 16:53:45 -0400 Original-Received: by mx0.mattleach.net (Postfix, from userid 99) id 9872A6222E; Wed, 30 Mar 2016 21:53:44 +0100 (BST) Original-Received: from spline.mattleach.net (host81-159-23-200.range81-159.btcentralplus.com [81.159.23.200]) by mx0.mattleach.net (Postfix) with ESMTPSA id 6C8B16222C; Wed, 30 Mar 2016 21:53:36 +0100 (BST) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 176.58.118.143 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:202479 Archived-At: --=-=-= Content-Type: text/plain Hi all, Here is a new version of the systemd integration patches. This time I've remembered to include the documentation updates. Speaking of which, I'm not sure whether I should include the below unit samples as a guide for people that are wanting to a use socket-activated Emacs server; I'm not sure whether it is appropriate or where they should go. emacs.socket ============ [Socket] ListenStream=/home/foobar/test.socket [Install] WantedBy=sockets.target emacs.service ============= [Service] Type=forking ExecStart=/path/to/emacs --daemon Feedback & comments welcome! Changes since v1 ================ * Call sd_listen_fds with a paramter of 1 to remove the state that systemd sets up. * Check the socket's validity when passed from systemd. * Remove lisp functions to simplify the API. * Change the name of the paramter passed to make_network_process to ':usepassedsocket' to keep things more generic. Changes since v2 ================ * Add documentation and NEWS section. * Rename :usepassedsockets to :use-systemd-socket. Thanks, -- Matt --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Check-for-libsystemd-when-building-Emacs.patch >From a4146c3db79923f6befc9acad730a4e891684a6f Mon Sep 17 00:00:00 2001 From: Matthew Leach Date: Sat, 26 Mar 2016 16:41:17 +0000 Subject: [PATCH v3 1/4] Check for libsystemd when building Emacs. * configure.ac: Add new default-on option systemd and check for necessary systemd libraries at configure time. * src/Makefile.in: Add libsystemd library and C flags to the Emacs compilation options. --- configure.ac | 13 +++++++++++++ src/Makefile.in | 6 +++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index f3846f4..d3628d2 100644 --- a/configure.ac +++ b/configure.ac @@ -330,6 +330,7 @@ OPTION_DEFAULT_ON([tiff],[don't compile with TIFF image support]) OPTION_DEFAULT_ON([gif],[don't compile with GIF image support]) OPTION_DEFAULT_ON([png],[don't compile with PNG image support]) OPTION_DEFAULT_ON([rsvg],[don't compile with SVG image support]) +OPTION_DEFAULT_ON([systemd],[don't compile with systemd support]) OPTION_DEFAULT_OFF([cairo],[compile with Cairo drawing (experimental)]) OPTION_DEFAULT_ON([xml2],[don't compile with XML parsing support]) OPTION_DEFAULT_ON([imagemagick],[don't compile with ImageMagick image support]) @@ -2716,6 +2717,18 @@ fi AC_SUBST(LIBGNUTLS_LIBS) AC_SUBST(LIBGNUTLS_CFLAGS) +HAVE_SYSTEMD=no +if test "${with_systemd}" = "yes" ; then + EMACS_CHECK_MODULES([LIBSYSTEMD], [libsystemd >= 226], + [HAVE_SYSTEMD=yes], [HAVE_SYSTEMD=no]) + if test "${HAVE_SYSTEMD}" = "yes"; then + AC_DEFINE(HAVE_SYSTEMD, 1, [Define if using systemd.]) + fi +fi + +AC_SUBST(LIBSYSTEMD_LIBS) +AC_SUBST(LIBSYSTEMD_CFLAGS) + NOTIFY_OBJ= NOTIFY_SUMMARY=no diff --git a/src/Makefile.in b/src/Makefile.in index c290a60..fc9360a 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -307,6 +307,9 @@ LIBSELINUX_LIBS = @LIBSELINUX_LIBS@ LIBGNUTLS_LIBS = @LIBGNUTLS_LIBS@ LIBGNUTLS_CFLAGS = @LIBGNUTLS_CFLAGS@ +LIBSYSTEMD_LIBS = @LIBSYSTEMD_LIBS@ +LIBSYSTEMD_CFLAGS = @LIBSYSTEMD_CFLAGS@ + INTERVALS_H = dispextern.h intervals.h composite.h GETLOADAVG_LIBS = @GETLOADAVG_LIBS@ @@ -372,6 +375,7 @@ ALL_CFLAGS=-Demacs $(MYCPPFLAGS) -I. -I$(srcdir) \ $(WEBKIT_CFLAGS) \ $(SETTINGS_CFLAGS) $(FREETYPE_CFLAGS) $(FONTCONFIG_CFLAGS) \ $(LIBOTF_CFLAGS) $(M17N_FLT_CFLAGS) $(DEPFLAGS) \ + $(LIBSYSTEMD_CFLAGS) \ $(LIBGNUTLS_CFLAGS) $(NOTIFY_CFLAGS) $(CAIRO_CFLAGS) \ $(WARN_CFLAGS) $(WERROR_CFLAGS) $(CFLAGS) ALL_OBJC_CFLAGS=$(ALL_CFLAGS) $(GNU_OBJC_CFLAGS) @@ -489,7 +493,7 @@ LIBES = $(LIBS) $(W32_LIBS) $(LIBS_GNUSTEP) $(LIBX_BASE) $(LIBIMAGE) \ $(LIBS_TERMCAP) $(GETLOADAVG_LIBS) $(SETTINGS_LIBS) $(LIBSELINUX_LIBS) \ $(FREETYPE_LIBS) $(FONTCONFIG_LIBS) $(LIBOTF_LIBS) $(M17N_FLT_LIBS) \ $(LIBGNUTLS_LIBS) $(LIB_PTHREAD) $(GETADDRINFO_A_LIBS) \ - $(NOTIFY_LIBS) $(LIB_MATH) $(LIBZ) $(LIBMODULES) + $(NOTIFY_LIBS) $(LIB_MATH) $(LIBZ) $(LIBMODULES) $(LIBSYSTEMD_LIBS) $(leimdir)/leim-list.el: bootstrap-emacs$(EXEEXT) $(MAKE) -C ../leim leim-list.el EMACS="$(bootstrap_exe)" -- 2.7.4 --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0002-Read-the-number-of-sockets-passed-by-systemd.patch >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 --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0003-Allow-network-processes-to-be-made-with-a-pre-alloca.patch >From d971465937c971beb2886ad59827005f69546247 Mon Sep 17 00:00:00 2001 From: Matthew Leach Date: Sat, 26 Mar 2016 19:37:10 +0000 Subject: [PATCH v3 3/4] Allow network processes to be made with a pre-allocated fd. * src/process.c (connect_network_socket): Allow a pre-allocated socket descriptor to be used if passed to Emacs, avoiding the call to socket() and bind(). (Fmake_network_process): Allow users to pass in :use-systemd-socket on the parameter plist to use a socket descriptor that has been passed to Emacs from systemd. (wait_reading_process_output): Call socket() & bind() every time. (syms_of_process): New symbol ":use-passed-socket". * doc/lispref/processes.texi (Network Processes): Document new `make-network-process' option ':use-systemd-socket'. --- doc/lispref/processes.texi | 6 ++++++ src/process.c | 36 +++++++++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/doc/lispref/processes.texi b/doc/lispref/processes.texi index 8d3df55..1d43c25 100644 --- a/doc/lispref/processes.texi +++ b/doc/lispref/processes.texi @@ -2367,6 +2367,12 @@ automatically for the given @var{host} and @var{service}. ignored. @code{ipv4} and @code{ipv6} specify to use IPv4 and IPv6, respectively. +@item :use-systemd-socket @var{use-systemd-socket} +If @var{use-systemd-socket} is non-@code{nil} and Emacs was passed a +network socket by systemd, use that socket instead of allocating one. +This is used by the Emacs server code to allow on-demand socket +activation. + @item :local @var{local-address} For a server process, @var{local-address} is the address to listen on. It overrides @var{family}, @var{host} and @var{service}, so you diff --git a/src/process.c b/src/process.c index 198e7de..28d2631 100644 --- a/src/process.c +++ b/src/process.c @@ -3075,7 +3075,8 @@ finish_after_tls_connection (Lisp_Object proc) #endif static void -connect_network_socket (Lisp_Object proc, Lisp_Object ip_addresses) +connect_network_socket (Lisp_Object proc, Lisp_Object ip_addresses, + Lisp_Object use_systemd_socket_p) { ptrdiff_t count = SPECPDL_INDEX (); ptrdiff_t count1; @@ -3089,6 +3090,16 @@ connect_network_socket (Lisp_Object proc, Lisp_Object ip_addresses) struct Lisp_Process *p = XPROCESS (proc); Lisp_Object contact = p->childp; int optbits = 0; + int systemd_socket_descriptor = 0; + +#ifdef HAVE_SYSTEMD + if (!NILP (use_systemd_socket_p)) + { + extern int systemd_socket; + systemd_socket_descriptor = systemd_socket; + } +#endif /* HAVE_SYSTEMD */ + /* Do this in case we never enter the while-loop below. */ count1 = SPECPDL_INDEX (); @@ -3109,7 +3120,11 @@ connect_network_socket (Lisp_Object proc, Lisp_Object ip_addresses) sa = xmalloc (addrlen); conv_lisp_to_sockaddr (family, ip_address, sa, addrlen); - s = socket (family, p->socktype | SOCK_CLOEXEC, p->ai_protocol); + if (systemd_socket_descriptor) + s = systemd_socket_descriptor; + else + s = socket (family, p->socktype | SOCK_CLOEXEC, p->ai_protocol); + if (s < 0) { xerrno = errno; @@ -3168,8 +3183,11 @@ connect_network_socket (Lisp_Object proc, Lisp_Object ip_addresses) report_file_error ("Cannot set reuse option on server socket", Qnil); } - if (bind (s, sa, addrlen)) - report_file_error ("Cannot bind server socket", Qnil); + /* If we are passed a socket descriptor from systemd, it is + already bound. */ + if (!systemd_socket_descriptor) + if (bind (s, sa, addrlen)) + report_file_error ("Cannot bind server socket", Qnil); #ifdef HAVE_GETSOCKNAME if (p->port == 0) @@ -3534,6 +3552,8 @@ The following network options can be specified for this connection: (this is allowed by default for a server process). :bindtodevice NAME -- bind to interface NAME. Using this may require special privileges on some systems. +:use-systemd-socket BOOL -- Use any pre-allocated sockets that have + been passed to Emacs by systemd. Consult the relevant system programmer's manual pages for more information on using these options. @@ -3578,7 +3598,7 @@ usage: (make-network-process &rest ARGS) */) EMACS_INT port = 0; Lisp_Object tem; Lisp_Object name, buffer, host, service, address; - Lisp_Object filter, sentinel; + Lisp_Object filter, sentinel, use_systemd_socket_p; Lisp_Object ip_addresses = Qnil; int socktype; int family = -1; @@ -3618,6 +3638,7 @@ usage: (make-network-process &rest ARGS) */) buffer = Fplist_get (contact, QCbuffer); filter = Fplist_get (contact, QCfilter); sentinel = Fplist_get (contact, QCsentinel); + use_systemd_socket_p = Fplist_get (contact, QCuse_systemd_socket); CHECK_STRING (name); @@ -3914,7 +3935,7 @@ usage: (make-network-process &rest ARGS) */) } #endif - connect_network_socket (proc, ip_addresses); + connect_network_socket (proc, ip_addresses, use_systemd_socket_p); return proc; } @@ -4848,7 +4869,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, { Lisp_Object ip_addresses = check_for_dns (aproc); if (!NILP (ip_addresses) && !EQ (ip_addresses, Qt)) - connect_network_socket (aproc, ip_addresses); + connect_network_socket (aproc, ip_addresses, Qnil); else retry_for_async = true; } @@ -7837,6 +7858,7 @@ syms_of_process (void) DEFSYM (QCserver, ":server"); DEFSYM (QCnowait, ":nowait"); DEFSYM (QCsentinel, ":sentinel"); + DEFSYM (QCuse_systemd_socket, ":use-systemd-socket"); DEFSYM (QCtls_parameters, ":tls-parameters"); DEFSYM (Qnsm_verify_connection, "nsm-verify-connection"); DEFSYM (QClog, ":log"); -- 2.7.4 --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0004-When-Emacs-is-passed-a-socket-descriptor-make-the-se.patch >From 8804c97495a4b210447ce6195956526017f4d188 Mon Sep 17 00:00:00 2001 From: Matthew Leach Date: Sat, 26 Mar 2016 20:43:26 +0000 Subject: [PATCH v3 4/4] When Emacs is passed a socket descriptor, make the server listen on it. * lisp/server.el (server-start): Set :use-systemd-socket to `t' when calling `make-network-process'. * etc/NEWS: Document new systemd functionality and build option to disable it. * doc/emacs/misc.texi (Emacs Server): Document systemd socket passing functionality. squash! When Emacs is passed a socket descriptor, make the server listen on it. --- doc/emacs/misc.texi | 9 +++++++++ etc/NEWS | 7 +++++++ lisp/server.el | 1 + 3 files changed, 17 insertions(+) diff --git a/doc/emacs/misc.texi b/doc/emacs/misc.texi index b5a2150..d805648 100644 --- a/doc/emacs/misc.texi +++ b/doc/emacs/misc.texi @@ -1580,6 +1580,15 @@ option. @xref{Initial Options}. When Emacs is started this way, it calls @code{server-start} after initialization, and returns control to the calling terminal instead of opening an initial frame; it then waits in the background, listening for edit requests. + +@cindex socket activation, systemd, Emacs +@item +If Emacs has been built with @command{systemd} support, the Emacs +server can be started by socket activation. The @command{systemd} +service creates a socket and listens for connections on it; when +@command{emacsclient} connects to it for the first time, +@command{systemd} can launch the Emacs server and hands over the +socket to it for servicing @command{emacsclient} connections. @end itemize @cindex @env{TEXEDIT} environment variable diff --git a/etc/NEWS b/etc/NEWS index 66777e9..9afed5b 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -26,6 +26,13 @@ otherwise leave it unmarked. * Installation Changes in Emacs 25.2 +++ +** Emacs now has systemd support. This allows socket based +activation, where systemd can invoke the Emacs process upon a +socket connection and hand over the socket to Emacs. Emacs will +use this socket for communicating with the emacsclient program. +This can be disabled with the new configure option +'--disable-systemd'. + ** New configure option '--disable-build-details' attempts to build an Emacs that is more likely to be reproducible; that is, if you build and install Emacs twice, the second Emacs is a copy of the first. diff --git a/lisp/server.el b/lisp/server.el index 5243820..2c9113c 100644 --- a/lisp/server.el +++ b/lisp/server.el @@ -655,6 +655,7 @@ server or call `\\[server-force-delete]' to forcibly disconnect it.")) :noquery t :sentinel #'server-sentinel :filter #'server-process-filter + :use-systemd-socket t ;; We must receive file names without being decoded. ;; Those are decoded by server-process-filter according ;; to file-name-coding-system. Also don't get -- 2.7.4 --=-=-=--