aboutsummaryrefslogtreecommitdiffstats
path: root/lib-src
diff options
context:
space:
mode:
authorPaul Eggert2018-12-02 23:51:11 -0800
committerPaul Eggert2018-12-02 23:55:01 -0800
commit5c412405c7422b356484a933179f852c30ce2f24 (patch)
tree81ebe767e19fcab0a08704af193da4a8f5817e88 /lib-src
parentf5090b91299cbd36901bef7b94aeef618b1bc6d8 (diff)
downloademacs-5c412405c7422b356484a933179f852c30ce2f24.tar.gz
emacs-5c412405c7422b356484a933179f852c30ce2f24.zip
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 *);
50# include "syswait.h" 50# include "syswait.h"
51 51
52# include <arpa/inet.h> 52# include <arpa/inet.h>
53# include <fcntl.h>
53# include <netinet/in.h> 54# include <netinet/in.h>
54# include <sys/socket.h> 55# include <sys/socket.h>
55# include <sys/un.h> 56# include <sys/un.h>
@@ -976,6 +977,24 @@ get_server_config (const char *config_file, struct sockaddr_in *server,
976 return true; 977 return true;
977} 978}
978 979
980/* Like socket (DOMAIN, TYPE, PROTOCOL), except arrange for the
981 resulting file descriptor to be close-on-exec. */
982
983static HSOCKET
984cloexec_socket (int domain, int type, int protocol)
985{
986#ifdef SOCK_CLOEXEC
987 return socket (domain, type | SOCK_CLOEXEC, protocol);
988#else
989 HSOCKET s = socket (domain, type, protocol);
990# ifndef WINDOWSNT
991 if (0 <= s)
992 fcntl (s, F_SETFD, FD_CLOEXEC);
993# endif
994 return s;
995#endif
996}
997
979static HSOCKET 998static HSOCKET
980set_tcp_socket (const char *local_server_file) 999set_tcp_socket (const char *local_server_file)
981{ 1000{
@@ -994,7 +1013,7 @@ set_tcp_socket (const char *local_server_file)
994 progname, inet_ntoa (server.in.sin_addr)); 1013 progname, inet_ntoa (server.in.sin_addr));
995 1014
996 /* Open up an AF_INET socket. */ 1015 /* Open up an AF_INET socket. */
997 HSOCKET s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); 1016 HSOCKET s = cloexec_socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
998 if (s < 0) 1017 if (s < 0)
999 { 1018 {
1000 /* Since we have an alternate to try out, this is not an error 1019 /* Since we have an alternate to try out, this is not an error
@@ -1404,7 +1423,7 @@ set_local_socket (char const *server_name)
1404 1423
1405 if (sock_status == 0) 1424 if (sock_status == 0)
1406 { 1425 {
1407 HSOCKET s = socket (AF_UNIX, SOCK_STREAM, 0); 1426 HSOCKET s = cloexec_socket (AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
1408 if (s < 0) 1427 if (s < 0)
1409 { 1428 {
1410 message (true, "%s: socket: %s\n", progname, strerror (errno)); 1429 message (true, "%s: socket: %s\n", progname, strerror (errno));