From db9b7dcedb001b942ad945a56b2d7bf9b77d7a6a Mon Sep 17 00:00:00 2001 From: ben Date: Sat, 9 Dec 2006 15:44:31 +0000 Subject: [PATCH] Set FD_CLOEXEC in a little convenience function that does the right thing with F_GETFD and F_SETFD. git-svn-id: svn://svn.tartarus.org/sgt/putty@6978 cda61777-01e9-0310-a592-d414129be87e --- unix/unix.h | 3 +++ unix/uxagentc.c | 2 +- unix/uxmisc.c | 12 ++++++++++++ unix/uxnet.c | 4 ++-- unix/uxpty.c | 6 +++--- unix/uxser.c | 2 +- 6 files changed, 22 insertions(+), 7 deletions(-) diff --git a/unix/unix.h b/unix/unix.h index 8235897b..fd206a9d 100644 --- a/unix/unix.h +++ b/unix/unix.h @@ -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. */ diff --git a/unix/uxagentc.c b/unix/uxagentc.c index 7b737d1d..3605c601 100644 --- a/unix/uxagentc.c +++ b/unix/uxagentc.c @@ -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)); diff --git a/unix/uxmisc.c b/unix/uxmisc.c index c613a204..74eb1568 100644 --- a/unix/uxmisc.c +++ b/unix/uxmisc.c @@ -2,6 +2,7 @@ * PuTTY miscellaneous Unix stuff */ +#include #include #include #include @@ -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); +} diff --git a/unix/uxnet.c b/unix/uxnet.c index 2b3d8cbc..e1dfce32 100644 --- a/unix/uxnet.c +++ b/unix/uxnet.c @@ -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; diff --git a/unix/uxpty.c b/unix/uxpty.c index e47f7981..cc01a67b 100644 --- a/unix/uxpty.c +++ b/unix/uxpty.c @@ -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); diff --git a/unix/uxser.c b/unix/uxser.c index 08f21575..454d7b36 100644 --- a/unix/uxser.c +++ b/unix/uxser.c @@ -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) -- 2.11.0