Set FD_CLOEXEC in a little convenience function that does the right thing
authorben <ben@cda61777-01e9-0310-a592-d414129be87e>
Sat, 9 Dec 2006 15:44:31 +0000 (15:44 +0000)
committerben <ben@cda61777-01e9-0310-a592-d414129be87e>
Sat, 9 Dec 2006 15:44:31 +0000 (15:44 +0000)
with F_GETFD and F_SETFD.

git-svn-id: svn://svn.tartarus.org/sgt/putty@6978 cda61777-01e9-0310-a592-d414129be87e

unix/unix.h
unix/uxagentc.c
unix/uxmisc.c
unix/uxnet.c
unix/uxpty.c
unix/uxser.c

index 8235897..fd206a9 100644 (file)
@@ -128,6 +128,9 @@ void gtk_setup_config_box(struct controlbox *b, int midsession, void *window);
 void (*putty_signal(int sig, void (*func)(int)))(int);
 void block_signal(int sig, int block_it);
 
+/* uxmisc.c */
+int cloexec(int);
+
 /*
  * Exports from unicode.c.
  */
index 7b737d1..3605c60 100644 (file)
@@ -122,7 +122,7 @@ int agent_query(void *in, int inlen, void **out, int *outlen,
        exit(1);
     }
 
-    fcntl(sock, F_SETFD, FD_CLOEXEC);
+    cloexec(sock);
 
     addr.sun_family = AF_UNIX;
     strncpy(addr.sun_path, name, sizeof(addr.sun_path));
index c613a20..74eb156 100644 (file)
@@ -2,6 +2,7 @@
  * PuTTY miscellaneous Unix stuff
  */
 
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -121,3 +122,14 @@ void pgp_fingerprints(void)
          "PuTTY Master Key (DSA), 1024-bit:\n"
          "  " PGP_DSA_MASTER_KEY_FP "\n", stdout);
 }
+
+/*
+ * Set FD_CLOEXEC on a file descriptor
+ */
+int cloexec(int fd) {
+    int fdflags;
+
+    fdflags = fcntl(fd, F_GETFD);
+    if (fdflags == -1) return -1;
+    return fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
+}
index 2b3d8cb..e1dfce3 100644 (file)
@@ -470,7 +470,7 @@ static int try_connect(Actual_Socket sock)
        goto ret;
     }
 
-    fcntl(s, F_SETFD, FD_CLOEXEC);
+    cloexec(s);
 
     if (sock->oobinline) {
        int b = TRUE;
@@ -725,7 +725,7 @@ Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only, i
        return (Socket) ret;
     }
 
-    fcntl(s, F_SETFD, FD_CLOEXEC);
+    cloexec(s);
 
     ret->oobinline = 0;
 
index e47f798..cc01a67 100644 (file)
@@ -277,7 +277,7 @@ static int pty_open_slave(Pty pty)
 {
     if (pty->slave_fd < 0) {
        pty->slave_fd = open(pty->name, O_RDWR);
-        fcntl(pty->slave_fd, F_SETFD, FD_CLOEXEC);
+        cloexec(pty->slave_fd);
     }
 
     return pty->slave_fd;
@@ -309,7 +309,7 @@ static void pty_open_master(Pty pty)
                    strcpy(pty->name, master_name);
                    pty->name[5] = 't'; /* /dev/ptyXX -> /dev/ttyXX */
 
-                    fcntl(pty->master_fd, F_SETFD, FD_CLOEXEC);
+                    cloexec(pty->master_fd);
 
                    if (pty_open_slave(pty) >= 0 &&
                        access(pty->name, R_OK | W_OK) == 0)
@@ -350,7 +350,7 @@ static void pty_open_master(Pty pty)
        exit(1);
     }
 
-    fcntl(pty->master_fd, F_SETFD, FD_CLOEXEC);
+    cloexec(pty->master_fd);
 
     pty->name[FILENAME_MAX-1] = '\0';
     strncpy(pty->name, ptsname(pty->master_fd), FILENAME_MAX-1);
index 08f2157..454d7b3 100644 (file)
@@ -257,7 +257,7 @@ static const char *serial_init(void *frontend_handle, void **backend_handle,
     if (serial->fd < 0)
        return "Unable to open serial port";
 
-    fcntl(serial->fd, F_SETFD, FD_CLOEXEC);
+    cloexec(serial->fd);
 
     err = serial_configure(serial, cfg);
     if (err)