From f56b4ec2ffa6d5667a3bd86040a1c771c1de33a5 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Sat, 16 Oct 2010 08:31:55 +0000 Subject: [PATCH] Trying to set FD_CLOEXEC on every fd is a lost cause, just use closefrom() before exec. --- client.c | 2 -- cmd-pipe-pane.c | 4 ++-- job.c | 4 ++-- server-client.c | 8 -------- server.c | 2 -- tmux.c | 2 ++ tty.c | 3 --- window.c | 4 ++-- 8 files changed, 8 insertions(+), 21 deletions(-) diff --git a/client.c b/client.c index e6b17c59..d9682b49 100644 --- a/client.c +++ b/client.c @@ -90,8 +90,6 @@ server_started: fatal("fcntl failed"); if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1) fatal("fcntl failed"); - if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) - fatal("fcntl failed"); imsg_init(&client_ibuf, fd); event_set(&client_event, fd, EV_READ, client_callback, NULL); diff --git a/cmd-pipe-pane.c b/cmd-pipe-pane.c index be6f8e25..d287d3b6 100644 --- a/cmd-pipe-pane.c +++ b/cmd-pipe-pane.c @@ -112,6 +112,8 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmd_ctx *ctx) if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO) close(null_fd); + closefrom(STDERR_FILENO + 1); + command = status_replace(c, NULL, data->arg, time(NULL), 0); execl(_PATH_BSHELL, "sh", "-c", command, (char *) NULL); _exit(1); @@ -130,8 +132,6 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmd_ctx *ctx) fatal("fcntl failed"); if (fcntl(wp->pipe_fd, F_SETFL, mode|O_NONBLOCK) == -1) fatal("fcntl failed"); - if (fcntl(wp->pipe_fd, F_SETFD, FD_CLOEXEC) == -1) - fatal("fcntl failed"); return (0); } } diff --git a/job.c b/job.c index e0dc99ef..ef4066e5 100644 --- a/job.c +++ b/job.c @@ -169,6 +169,8 @@ job_run(struct job *job) if (nullfd != STDIN_FILENO && nullfd != STDERR_FILENO) close(nullfd); + closefrom(STDERR_FILENO + 1); + execl(_PATH_BSHELL, "sh", "-c", job->cmd, (char *) NULL); fatal("execl failed"); default: /* parent */ @@ -179,8 +181,6 @@ job_run(struct job *job) fatal("fcntl failed"); if (fcntl(job->fd, F_SETFL, mode|O_NONBLOCK) == -1) fatal("fcntl failed"); - if (fcntl(job->fd, F_SETFD, FD_CLOEXEC) == -1) - fatal("fcntl failed"); if (job->event != NULL) bufferevent_free(job->event); diff --git a/server-client.c b/server-client.c index 427f8dfb..cd3eb079 100644 --- a/server-client.c +++ b/server-client.c @@ -60,8 +60,6 @@ server_client_create(int fd) fatal("fcntl failed"); if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1) fatal("fcntl failed"); - if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) - fatal("fcntl failed"); c = xcalloc(1, sizeof *c); c->references = 0; @@ -706,8 +704,6 @@ server_client_msg_dispatch(struct client *c) if ((mode = fcntl(c->stdin_fd, F_GETFL)) != -1) fcntl(c->stdin_fd, F_SETFL, mode|O_NONBLOCK); - if (fcntl(c->stdin_fd, F_SETFD, FD_CLOEXEC) == -1) - fatal("fcntl failed"); server_client_msg_identify(c, &identifydata, imsg.fd); break; @@ -725,8 +721,6 @@ server_client_msg_dispatch(struct client *c) if ((mode = fcntl(c->stdout_fd, F_GETFL)) != -1) fcntl(c->stdout_fd, F_SETFL, mode|O_NONBLOCK); - if (fcntl(c->stdout_fd, F_SETFD, FD_CLOEXEC) == -1) - fatal("fcntl failed"); break; case MSG_STDERR: if (datalen != 0) @@ -742,8 +736,6 @@ server_client_msg_dispatch(struct client *c) if ((mode = fcntl(c->stderr_fd, F_GETFL)) != -1) fcntl(c->stderr_fd, F_SETFL, mode|O_NONBLOCK); - if (fcntl(c->stderr_fd, F_SETFD, FD_CLOEXEC) == -1) - fatal("fcntl failed"); break; case MSG_RESIZE: if (datalen != 0) diff --git a/server.c b/server.c index 7dfd5be6..721bb570 100644 --- a/server.c +++ b/server.c @@ -98,8 +98,6 @@ server_create_socket(void) fatal("fcntl failed"); if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1) fatal("fcntl failed"); - if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) - fatal("fcntl failed"); server_update_socket(); diff --git a/tmux.c b/tmux.c index 842fd1b9..fe1fd394 100644 --- a/tmux.c +++ b/tmux.c @@ -223,6 +223,8 @@ shell_exec(const char *shell, const char *shellcmd) xasprintf(&argv0, "%s", shellname); setenv("SHELL", shell, 1); + closefrom(STDERR_FILENO + 1); + execl(shell, argv0, "-c", shellcmd, (char *) NULL); fatal("execl failed"); } diff --git a/tty.c b/tty.c index 268ea2e8..379fa42d 100644 --- a/tty.c +++ b/tty.c @@ -61,9 +61,6 @@ tty_init(struct tty *tty, int fd, char *term) tty->termname = xstrdup("unknown"); else tty->termname = xstrdup(term); - - if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) - fatal("fcntl failed"); tty->fd = fd; if ((path = ttyname(fd)) == NULL) diff --git a/window.c b/window.c index 731eff02..181ffd7c 100644 --- a/window.c +++ b/window.c @@ -573,6 +573,8 @@ window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell, if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0) fatal("tcgetattr failed"); + closefrom(STDERR_FILENO + 1); + environ_push(env); clear_signals(1); @@ -603,8 +605,6 @@ window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell, fatal("fcntl failed"); if (fcntl(wp->fd, F_SETFL, mode|O_NONBLOCK) == -1) fatal("fcntl failed"); - if (fcntl(wp->fd, F_SETFD, FD_CLOEXEC) == -1) - fatal("fcntl failed"); wp->event = bufferevent_new(wp->fd, window_pane_read_callback, NULL, window_pane_error_callback, wp); bufferevent_enable(wp->event, EV_READ|EV_WRITE);