emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 3/5] Permit systemd-allocated socket file-descriptors to be used.


From: Matthew Leach
Subject: [PATCH 3/5] Permit systemd-allocated socket file-descriptors to be used.
Date: Sat, 26 Mar 2016 21:16:40 +0000

* src/process.c (connect_network_socket): Allow a systemd-allocated
  file-descriptor to be passed, and use it, avoiding the call to
  socket() and bind().
  (Fmake_network_process): Allow users to pass in :systemd-fd on the
  parameter plist to use a systemd fd.
  (wait_reading_process_output): Call socket() & bind() every time.
  (syms_of_process): New symbol.
---
 src/process.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/src/process.c b/src/process.c
index 198e7de..b6bc757 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 systemd_fd)
 {
   ptrdiff_t count = SPECPDL_INDEX ();
   ptrdiff_t count1;
@@ -3089,6 +3090,8 @@ 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 use_systemd_fd = !NILP (systemd_fd) && INTEGERP (systemd_fd) &&
+      XINT (systemd_fd) != 0;
 
   /* Do this in case we never enter the while-loop below.  */
   count1 = SPECPDL_INDEX ();
@@ -3109,7 +3112,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 (use_systemd_fd)
+          s = XINT (systemd_fd);
+      else
+          s = socket (family, p->socktype | SOCK_CLOEXEC, p->ai_protocol);
+
       if (s < 0)
        {
          xerrno = errno;
@@ -3168,8 +3175,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 in an fd from systemd, it is already
+             bound. */
+         if (!use_systemd_fd)
+           if (bind (s, sa, addrlen))
+             report_file_error ("Cannot bind server socket", Qnil);
 
 #ifdef HAVE_GETSOCKNAME
          if (p->port == 0)
@@ -3534,6 +3544,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.
+:systemd-fd INT    -- use this file-descriptor, passed in through systemd,
+                      and don't call socket() and bind().
 
 Consult the relevant system programmer's manual pages for more
 information on using these options.
@@ -3577,7 +3589,7 @@ usage: (make-network-process &rest ARGS)  */)
 #endif
   EMACS_INT port = 0;
   Lisp_Object tem;
-  Lisp_Object name, buffer, host, service, address;
+  Lisp_Object name, buffer, host, service, address, systemd_fd;
   Lisp_Object filter, sentinel;
   Lisp_Object ip_addresses = Qnil;
   int socktype;
@@ -3618,6 +3630,7 @@ usage: (make-network-process &rest ARGS)  */)
   buffer = Fplist_get (contact, QCbuffer);
   filter = Fplist_get (contact, QCfilter);
   sentinel = Fplist_get (contact, QCsentinel);
+  systemd_fd = Fplist_get (contact, QCsystemd_fd);
 
   CHECK_STRING (name);
 
@@ -3914,7 +3927,7 @@ usage: (make-network-process &rest ARGS)  */)
     }
 #endif
 
-  connect_network_socket (proc, ip_addresses);
+  connect_network_socket (proc, ip_addresses, systemd_fd);
   return proc;
 }
 
@@ -4848,7 +4861,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 +7850,7 @@ syms_of_process (void)
   DEFSYM (QCserver, ":server");
   DEFSYM (QCnowait, ":nowait");
   DEFSYM (QCsentinel, ":sentinel");
+  DEFSYM (QCsystemd_fd, ":systemd-fd");
   DEFSYM (QCtls_parameters, ":tls-parameters");
   DEFSYM (Qnsm_verify_connection, "nsm-verify-connection");
   DEFSYM (QClog, ":log");
-- 
2.7.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]