Trying to set FD_CLOEXEC on every fd is a lost cause, just use

closefrom() before exec.
This commit is contained in:
Nicholas Marriott 2010-10-16 08:31:55 +00:00
parent 6c42f1a89e
commit f56b4ec2ff
8 changed files with 8 additions and 21 deletions

View File

@ -90,8 +90,6 @@ server_started:
fatal("fcntl failed"); fatal("fcntl failed");
if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1) if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
fatal("fcntl failed"); fatal("fcntl failed");
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
fatal("fcntl failed");
imsg_init(&client_ibuf, fd); imsg_init(&client_ibuf, fd);
event_set(&client_event, fd, EV_READ, client_callback, NULL); event_set(&client_event, fd, EV_READ, client_callback, NULL);

View File

@ -112,6 +112,8 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO) if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO)
close(null_fd); close(null_fd);
closefrom(STDERR_FILENO + 1);
command = status_replace(c, NULL, data->arg, time(NULL), 0); command = status_replace(c, NULL, data->arg, time(NULL), 0);
execl(_PATH_BSHELL, "sh", "-c", command, (char *) NULL); execl(_PATH_BSHELL, "sh", "-c", command, (char *) NULL);
_exit(1); _exit(1);
@ -130,8 +132,6 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
fatal("fcntl failed"); fatal("fcntl failed");
if (fcntl(wp->pipe_fd, F_SETFL, mode|O_NONBLOCK) == -1) if (fcntl(wp->pipe_fd, F_SETFL, mode|O_NONBLOCK) == -1)
fatal("fcntl failed"); fatal("fcntl failed");
if (fcntl(wp->pipe_fd, F_SETFD, FD_CLOEXEC) == -1)
fatal("fcntl failed");
return (0); return (0);
} }
} }

4
job.c
View File

@ -169,6 +169,8 @@ job_run(struct job *job)
if (nullfd != STDIN_FILENO && nullfd != STDERR_FILENO) if (nullfd != STDIN_FILENO && nullfd != STDERR_FILENO)
close(nullfd); close(nullfd);
closefrom(STDERR_FILENO + 1);
execl(_PATH_BSHELL, "sh", "-c", job->cmd, (char *) NULL); execl(_PATH_BSHELL, "sh", "-c", job->cmd, (char *) NULL);
fatal("execl failed"); fatal("execl failed");
default: /* parent */ default: /* parent */
@ -179,8 +181,6 @@ job_run(struct job *job)
fatal("fcntl failed"); fatal("fcntl failed");
if (fcntl(job->fd, F_SETFL, mode|O_NONBLOCK) == -1) if (fcntl(job->fd, F_SETFL, mode|O_NONBLOCK) == -1)
fatal("fcntl failed"); fatal("fcntl failed");
if (fcntl(job->fd, F_SETFD, FD_CLOEXEC) == -1)
fatal("fcntl failed");
if (job->event != NULL) if (job->event != NULL)
bufferevent_free(job->event); bufferevent_free(job->event);

View File

@ -60,8 +60,6 @@ server_client_create(int fd)
fatal("fcntl failed"); fatal("fcntl failed");
if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1) if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
fatal("fcntl failed"); fatal("fcntl failed");
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
fatal("fcntl failed");
c = xcalloc(1, sizeof *c); c = xcalloc(1, sizeof *c);
c->references = 0; c->references = 0;
@ -706,8 +704,6 @@ server_client_msg_dispatch(struct client *c)
if ((mode = fcntl(c->stdin_fd, F_GETFL)) != -1) if ((mode = fcntl(c->stdin_fd, F_GETFL)) != -1)
fcntl(c->stdin_fd, F_SETFL, mode|O_NONBLOCK); 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); server_client_msg_identify(c, &identifydata, imsg.fd);
break; break;
@ -725,8 +721,6 @@ server_client_msg_dispatch(struct client *c)
if ((mode = fcntl(c->stdout_fd, F_GETFL)) != -1) if ((mode = fcntl(c->stdout_fd, F_GETFL)) != -1)
fcntl(c->stdout_fd, F_SETFL, mode|O_NONBLOCK); fcntl(c->stdout_fd, F_SETFL, mode|O_NONBLOCK);
if (fcntl(c->stdout_fd, F_SETFD, FD_CLOEXEC) == -1)
fatal("fcntl failed");
break; break;
case MSG_STDERR: case MSG_STDERR:
if (datalen != 0) if (datalen != 0)
@ -742,8 +736,6 @@ server_client_msg_dispatch(struct client *c)
if ((mode = fcntl(c->stderr_fd, F_GETFL)) != -1) if ((mode = fcntl(c->stderr_fd, F_GETFL)) != -1)
fcntl(c->stderr_fd, F_SETFL, mode|O_NONBLOCK); fcntl(c->stderr_fd, F_SETFL, mode|O_NONBLOCK);
if (fcntl(c->stderr_fd, F_SETFD, FD_CLOEXEC) == -1)
fatal("fcntl failed");
break; break;
case MSG_RESIZE: case MSG_RESIZE:
if (datalen != 0) if (datalen != 0)

View File

@ -98,8 +98,6 @@ server_create_socket(void)
fatal("fcntl failed"); fatal("fcntl failed");
if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1) if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
fatal("fcntl failed"); fatal("fcntl failed");
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
fatal("fcntl failed");
server_update_socket(); server_update_socket();

2
tmux.c
View File

@ -223,6 +223,8 @@ shell_exec(const char *shell, const char *shellcmd)
xasprintf(&argv0, "%s", shellname); xasprintf(&argv0, "%s", shellname);
setenv("SHELL", shell, 1); setenv("SHELL", shell, 1);
closefrom(STDERR_FILENO + 1);
execl(shell, argv0, "-c", shellcmd, (char *) NULL); execl(shell, argv0, "-c", shellcmd, (char *) NULL);
fatal("execl failed"); fatal("execl failed");
} }

3
tty.c
View File

@ -61,9 +61,6 @@ tty_init(struct tty *tty, int fd, char *term)
tty->termname = xstrdup("unknown"); tty->termname = xstrdup("unknown");
else else
tty->termname = xstrdup(term); tty->termname = xstrdup(term);
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
fatal("fcntl failed");
tty->fd = fd; tty->fd = fd;
if ((path = ttyname(fd)) == NULL) if ((path = ttyname(fd)) == NULL)

View File

@ -573,6 +573,8 @@ window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0) if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
fatal("tcgetattr failed"); fatal("tcgetattr failed");
closefrom(STDERR_FILENO + 1);
environ_push(env); environ_push(env);
clear_signals(1); clear_signals(1);
@ -603,8 +605,6 @@ window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
fatal("fcntl failed"); fatal("fcntl failed");
if (fcntl(wp->fd, F_SETFL, mode|O_NONBLOCK) == -1) if (fcntl(wp->fd, F_SETFL, mode|O_NONBLOCK) == -1)
fatal("fcntl failed"); fatal("fcntl failed");
if (fcntl(wp->fd, F_SETFD, FD_CLOEXEC) == -1)
fatal("fcntl failed");
wp->event = bufferevent_new(wp->fd, wp->event = bufferevent_new(wp->fd,
window_pane_read_callback, NULL, window_pane_error_callback, wp); window_pane_read_callback, NULL, window_pane_error_callback, wp);
bufferevent_enable(wp->event, EV_READ|EV_WRITE); bufferevent_enable(wp->event, EV_READ|EV_WRITE);