]> code.delx.au - gnu-emacs/blobdiff - src/process.c
Fix the call to set_network_coding_system
[gnu-emacs] / src / process.c
index 2dfad669072ec650cb4bc67a44a203432ba4bb45..2d0fb567943f7e4547b1edd44a2e0b9a105f4327 100644 (file)
@@ -267,8 +267,11 @@ static int max_process_desc;
 /* The largest descriptor currently in use for input; -1 if none.  */
 static int max_input_desc;
 
-/* The descriptor  of any sockets passed to Emacs; -1 if none. */
-static int external_sock_fd = -1;
+/* Set the external socket descriptor for Emacs to use when
+   `make-network-process' is called with a non-nil
+   `:use-external-socket' option.  The value should be either -1, or
+   the file descriptor of a socket that is already bound.  */
+static int external_sock_fd;
 
 /* Indexed by descriptor, gives the process (if any) for that descriptor.  */
 static Lisp_Object chan_process[FD_SETSIZE];
@@ -3099,11 +3102,10 @@ connect_network_socket (Lisp_Object proc, Lisp_Object ip_addresses,
     {
       socket_to_use = external_sock_fd;
 
-      /* Ensure we don't consume the external socket twice. */
+      /* Ensure we don't consume the external socket twice.  */
       external_sock_fd = -1;
     }
 
-
   /* Do this in case we never enter the while-loop below.  */
   count1 = SPECPDL_INDEX ();
   s = -1;
@@ -3123,15 +3125,15 @@ connect_network_socket (Lisp_Object proc, Lisp_Object ip_addresses,
       sa = xmalloc (addrlen);
       conv_lisp_to_sockaddr (family, ip_address, sa, addrlen);
 
-      if (socket_to_use != -1)
-          s = socket_to_use;
-      else
-          s = socket (family, p->socktype | SOCK_CLOEXEC, p->ai_protocol);
-
+      s = socket_to_use;
       if (s < 0)
        {
-         xerrno = errno;
-         continue;
+         s = socket (family, p->socktype | SOCK_CLOEXEC, p->ai_protocol);
+         if (s < 0)
+           {
+             xerrno = errno;
+             continue;
+           }
        }
 
 #ifdef DATAGRAM_SOCKETS
@@ -3186,11 +3188,9 @@ connect_network_socket (Lisp_Object proc, Lisp_Object ip_addresses,
                  report_file_error ("Cannot set reuse option on server socket", Qnil);
              }
 
-          /* If we are passed a socket descriptor, it should be
-             already bound. */
-         if (socket_to_use == -1)
-           if (bind (s, sa, addrlen))
-             report_file_error ("Cannot bind server socket", Qnil);
+          /* If passed a socket descriptor, it should be already bound. */
+         if (socket_to_use < 0 && bind (s, sa, addrlen) != 0)
+           report_file_error ("Cannot bind server socket", Qnil);
 
 #ifdef HAVE_GETSOCKNAME
          if (p->port == 0)
@@ -3910,7 +3910,7 @@ usage: (make-network-process &rest ARGS)  */)
   p->gnutls_boot_parameters = tem;
 #endif
 
-  set_network_socket_coding_system (proc, service, host, name);
+  set_network_socket_coding_system (proc, host, service, name);
 
   unbind_to (count, Qnil);
 
@@ -4278,19 +4278,6 @@ Data that is unavailable is returned as nil.  */)
 #endif
 }
 
-/* If program file NAME starts with /: for quoting a magic
-   name, remove that, preserving the multibyteness of NAME.  */
-
-Lisp_Object
-remove_slash_colon (Lisp_Object name)
-{
-  return
-    ((SBYTES (name) > 2 && SREF (name, 0) == '/' && SREF (name, 1) == ':')
-     ? make_specified_string (SSDATA (name) + 2, SCHARS (name) - 2,
-                             SBYTES (name) - 2, STRING_MULTIBYTE (name))
-     : name);
-}
-
 /* Turn off input and output for process PROC.  */
 
 static void
@@ -7483,6 +7470,19 @@ add_timer_wait_descriptor (int fd)
 
 #endif /* HAVE_TIMERFD */
 
+/* If program file NAME starts with /: for quoting a magic
+   name, remove that, preserving the multibyteness of NAME.  */
+
+Lisp_Object
+remove_slash_colon (Lisp_Object name)
+{
+  return
+    ((SBYTES (name) > 2 && SREF (name, 0) == '/' && SREF (name, 1) == ':')
+     ? make_specified_string (SSDATA (name) + 2, SCHARS (name) - 2,
+                             SBYTES (name) - 2, STRING_MULTIBYTE (name))
+     : name);
+}
+
 /* Add DESC to the set of keyboard input descriptors.  */
 
 void
@@ -7736,24 +7736,14 @@ catch_child_signal (void)
 }
 #endif /* subprocesses */
 
-/* Set the external socket descriptor for Emacs to use when
-   `make-network-process' is called with a non-nil
-   `:use-external-socket' option.  The fd should have been checked to
-   ensure it is a valid socket and is already bound.  */
-void
-set_external_socket_descriptor(int fd)
-{
-    external_sock_fd = fd;
-}
-
 \f
 /* This is not called "init_process" because that is the name of a
    Mach system call, so it would cause problems on Darwin systems.  */
 void
-init_process_emacs (void)
+init_process_emacs (int sockfd)
 {
 #ifdef subprocesses
-  register int i;
+  int i;
 
   inhibit_sentinels = 0;
 
@@ -7776,6 +7766,7 @@ init_process_emacs (void)
   FD_ZERO (&non_process_wait_mask);
   FD_ZERO (&write_mask);
   max_process_desc = max_input_desc = -1;
+  external_sock_fd = sockfd;
   memset (fd_callback_info, 0, sizeof (fd_callback_info));
 
   FD_ZERO (&connect_wait_mask);
@@ -8019,13 +8010,6 @@ The variable takes effect when `start-process' is called.  */);
   defsubr (&Sset_process_filter_multibyte);
   defsubr (&Sprocess_filter_multibyte_p);
 
-#endif /* subprocesses */
-
-  defsubr (&Sget_buffer_process);
-  defsubr (&Sprocess_inherit_coding_system_flag);
-  defsubr (&Slist_system_processes);
-  defsubr (&Sprocess_attributes);
-
  {
    Lisp_Object subfeatures = Qnil;
    const struct socket_options *sopt;
@@ -8058,4 +8042,10 @@ The variable takes effect when `start-process' is called.  */);
    Fprovide (intern_c_string ("make-network-process"), subfeatures);
  }
 
+#endif /* subprocesses */
+
+  defsubr (&Sget_buffer_process);
+  defsubr (&Sprocess_inherit_coding_system_flag);
+  defsubr (&Slist_system_processes);
+  defsubr (&Sprocess_attributes);
 }