[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 3/4] Allow network processes to be made with a pre-allocated f
From: |
Matthew Leach |
Subject: |
[PATCH v2 3/4] Allow network processes to be made with a pre-allocated fd. |
Date: |
Sun, 27 Mar 2016 21:39:42 +0100 |
* 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