* [PATCH v2 1/4] Check for libsystemd when building Emacs.
2016-03-27 20:39 [PATCH v2 0/4] Add systemd socket launching support Matthew Leach
@ 2016-03-27 20:39 ` Matthew Leach
2016-03-27 20:39 ` [PATCH v2 2/4] Read the number of sockets passed by systemd Matthew Leach
` (2 subsequent siblings)
3 siblings, 0 replies; 9+ messages in thread
From: Matthew Leach @ 2016-03-27 20:39 UTC (permalink / raw)
To: emacs-devel; +Cc: Matthew Leach
* 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
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 2/4] Read the number of sockets passed by systemd.
2016-03-27 20:39 [PATCH v2 0/4] Add systemd socket launching support Matthew Leach
2016-03-27 20:39 ` [PATCH v2 1/4] Check for libsystemd when building Emacs Matthew Leach
@ 2016-03-27 20:39 ` Matthew Leach
2016-03-27 20:39 ` [PATCH v2 3/4] Allow network processes to be made with a pre-allocated fd Matthew Leach
2016-03-27 20:39 ` [PATCH v2 4/4] When Emacs is passed a socket descriptor, make the server listen on it Matthew Leach
3 siblings, 0 replies; 9+ messages in thread
From: Matthew Leach @ 2016-03-27 20:39 UTC (permalink / raw)
To: emacs-devel; +Cc: Matthew Leach
* 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 <http://www.gnu.org/licenses/>. */
#include <binary-io.h>
#endif
+#ifdef HAVE_SYSTEMD
+#include <systemd/sd-daemon.h>
+#include <sys/socket.h>
+#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
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 3/4] Allow network processes to be made with a pre-allocated fd.
2016-03-27 20:39 [PATCH v2 0/4] Add systemd socket launching support Matthew Leach
2016-03-27 20:39 ` [PATCH v2 1/4] Check for libsystemd when building Emacs Matthew Leach
2016-03-27 20:39 ` [PATCH v2 2/4] Read the number of sockets passed by systemd Matthew Leach
@ 2016-03-27 20:39 ` Matthew Leach
2016-03-27 21:18 ` Andreas Schwab
` (2 more replies)
2016-03-27 20:39 ` [PATCH v2 4/4] When Emacs is passed a socket descriptor, make the server listen on it Matthew Leach
3 siblings, 3 replies; 9+ messages in thread
From: Matthew Leach @ 2016-03-27 20:39 UTC (permalink / raw)
To: emacs-devel; +Cc: Matthew Leach
* 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 :usepassedsocket
on the parameter plist to use a socket descriptor that has been
passed to Emacs.
(wait_reading_process_output): Call socket() & bind() every time.
(syms_of_process): New symbol ":usepassedsocket".
---
src/process.c | 36 +++++++++++++++++++++++++++++-------
1 file changed, 29 insertions(+), 7 deletions(-)
diff --git a/src/process.c b/src/process.c
index 198e7de..2a0a0d5 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_passed_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 passed_socket_descriptor = 0;
+
+#ifdef HAVE_SYSTEMD
+ if (!NILP (use_passed_socket_p))
+ {
+ extern int systemd_socket;
+ passed_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 (passed_socket_descriptor)
+ s = passed_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, it is already
+ bound. */
+ if (!passed_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.
+:usepassedsocket BOOL -- Use any pre-allocated sockets that have
+ been passed to Emacs.
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_passed_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_passed_socket_p = Fplist_get (contact, QCuse_passed_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_passed_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_passed_socket, ":usepassedsocket");
DEFSYM (QCtls_parameters, ":tls-parameters");
DEFSYM (Qnsm_verify_connection, "nsm-verify-connection");
DEFSYM (QClog, ":log");
--
2.7.4
^ permalink raw reply related [flat|nested] 9+ messages in thread