summaryrefslogtreecommitdiff
path: root/lib-src
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2018-12-02 23:51:11 -0800
committerPaul Eggert <eggert@cs.ucla.edu>2018-12-02 23:55:01 -0800
commit5c412405c7422b356484a933179f852c30ce2f24 (patch)
tree81ebe767e19fcab0a08704af193da4a8f5817e88 /lib-src
parentf5090b91299cbd36901bef7b94aeef618b1bc6d8 (diff)
downloademacs-5c412405c7422b356484a933179f852c30ce2f24.tar.gz
emacsclient: don’t leak socket to child processes
* lib-src/emacsclient.c [!WINDOWSNT]: Include fcntl.h. (cloexec_socket): New function. (set_tcp_socket, set_local_socket): Use it.
Diffstat (limited to 'lib-src')
-rw-r--r--lib-src/emacsclient.c23
1 files changed, 21 insertions, 2 deletions
diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index 1c62c09451c..a428788344e 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -50,6 +50,7 @@ char *w32_getenv (const char *);
# include "syswait.h"
# include <arpa/inet.h>
+# include <fcntl.h>
# include <netinet/in.h>
# include <sys/socket.h>
# include <sys/un.h>
@@ -976,6 +977,24 @@ get_server_config (const char *config_file, struct sockaddr_in *server,
return true;
}
+/* Like socket (DOMAIN, TYPE, PROTOCOL), except arrange for the
+ resulting file descriptor to be close-on-exec. */
+
+static HSOCKET
+cloexec_socket (int domain, int type, int protocol)
+{
+#ifdef SOCK_CLOEXEC
+ return socket (domain, type | SOCK_CLOEXEC, protocol);
+#else
+ HSOCKET s = socket (domain, type, protocol);
+# ifndef WINDOWSNT
+ if (0 <= s)
+ fcntl (s, F_SETFD, FD_CLOEXEC);
+# endif
+ return s;
+#endif
+}
+
static HSOCKET
set_tcp_socket (const char *local_server_file)
{
@@ -994,7 +1013,7 @@ set_tcp_socket (const char *local_server_file)
progname, inet_ntoa (server.in.sin_addr));
/* Open up an AF_INET socket. */
- HSOCKET s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ HSOCKET s = cloexec_socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s < 0)
{
/* Since we have an alternate to try out, this is not an error
@@ -1404,7 +1423,7 @@ set_local_socket (char const *server_name)
if (sock_status == 0)
{
- HSOCKET s = socket (AF_UNIX, SOCK_STREAM, 0);
+ HSOCKET s = cloexec_socket (AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (s < 0)
{
message (true, "%s: socket: %s\n", progname, strerror (errno));