all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Matthew Leach <matthew@mattleach.net>
To: emacs-devel <emacs-devel@gnu.org>
Cc: Matthew Leach <matthew@mattleach.net>
Subject: [PATCH 3/5] Permit systemd-allocated socket file-descriptors to be used.
Date: Sat, 26 Mar 2016 21:16:40 +0000	[thread overview]
Message-ID: <1459027002-13215-4-git-send-email-matthew@mattleach.net> (raw)
In-Reply-To: <1459027002-13215-1-git-send-email-matthew@mattleach.net>

* 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




  parent reply	other threads:[~2016-03-26 21:16 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-26 21:16 [PATCH 0/5] Add systemd socket launching support Matthew Leach
2016-03-26 21:16 ` [PATCH 1/5] Check for libsystemd when building Emacs Matthew Leach
2016-03-26 21:16 ` [PATCH 2/5] Read the number of sockets passed by systemd Matthew Leach
2016-03-26 21:16 ` Matthew Leach [this message]
2016-03-27 12:08   ` [PATCH 3/5] Permit systemd-allocated socket file-descriptors to be used Lars Magne Ingebrigtsen
2016-03-27 12:23     ` Matthew Leach
2016-03-27 12:42       ` Andreas Schwab
2016-03-27 13:38         ` Matthew Leach
2016-03-27 13:05       ` Lars Magne Ingebrigtsen
2016-03-27 13:39         ` Matthew Leach
2016-03-26 21:16 ` [PATCH 4/5] Allow the systed socket fd to be retrieved Matthew Leach
2016-03-26 21:16 ` [PATCH 5/5] When set, use the systemd socket descriptor Matthew Leach
2016-03-27  0:47 ` [PATCH 0/5] Add systemd socket launching support Alan Mackenzie
2016-03-27  0:59   ` Alexis
2016-03-27  8:44     ` Matthew Leach
2016-03-27 11:15       ` Alexis
2016-03-27  8:41   ` Matthew Leach
2016-03-27 13:48     ` Wolfgang Jenkner
2016-03-27 13:53       ` Matthew Leach
2016-03-27 14:18         ` Wolfgang Jenkner
2016-03-27 15:49     ` Mark Oteiza
2016-03-27 18:16       ` Matthew Leach
2016-03-27 14:49 ` Eli Zaretskii
2016-03-27 15:17   ` Matthew Leach
2016-03-27 15:23     ` Eli Zaretskii
2016-03-27 17:21     ` Philipp Stephani
2016-03-27 18:10       ` Matthew Leach

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1459027002-13215-4-git-send-email-matthew@mattleach.net \
    --to=matthew@mattleach.net \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.