aboutsummaryrefslogtreecommitdiff
path: root/tty.c
diff options
context:
space:
mode:
authorTiago Cunha <tcunha@gmx.com>2009-09-23 14:39:30 +0000
committerTiago Cunha <tcunha@gmx.com>2009-09-23 14:39:30 +0000
commitacedc2dcf2673cf199ba1ba08705dc8fd270c0c7 (patch)
treef51d5c52a93e0f6f2fba161d87e0cd0ac7230ea0 /tty.c
parentc40d8cbda4c391008b4c46d211d6f3c09accd81a (diff)
downloadrtmux-acedc2dcf2673cf199ba1ba08705dc8fd270c0c7.tar.gz
rtmux-acedc2dcf2673cf199ba1ba08705dc8fd270c0c7.tar.bz2
rtmux-acedc2dcf2673cf199ba1ba08705dc8fd270c0c7.zip
Sync OpenBSD patchset 345:
Don't attempt to open() the tty path, rely on the client sending its stdin fd with imsg and fatal if it doesn't, then set the FD_CLOEXEC flag in tty_init instead of tty_open to prevent them leaking into child processes if any are created between the two calls. This bumps the protocol version, so the tmux server should be killed before upgrading.
Diffstat (limited to 'tty.c')
-rw-r--r--tty.c48
1 files changed, 25 insertions, 23 deletions
diff --git a/tty.c b/tty.c
index 4d18b8b3..98c25ccf 100644
--- a/tty.c
+++ b/tty.c
@@ -1,4 +1,4 @@
-/* $Id: tty.c,v 1.133 2009-09-23 14:33:13 tcunha Exp $ */
+/* $Id: tty.c,v 1.134 2009-09-23 14:39:30 tcunha Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -44,16 +44,31 @@ void tty_cell(struct tty *,
const struct grid_cell *, const struct grid_utf8 *);
void
-tty_init(struct tty *tty, int fd, char *path, char *term)
+tty_init(struct tty *tty, int fd, char *term)
{
- tty->path = xstrdup(path);
- tty->fd = fd;
+ int mode;
+ char *path;
+
+ memset(tty, 0, sizeof *tty);
tty->log_fd = -1;
if (term == NULL || *term == '\0')
tty->termname = xstrdup("unknown");
else
tty->termname = xstrdup(term);
+
+ if ((mode = fcntl(fd, F_GETFL)) == -1)
+ 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");
+ tty->fd = fd;
+
+ if ((path = ttyname(fd)) == NULL)
+ fatalx("ttyname failed");
+ tty->path = xstrdup(path);
+
tty->flags = 0;
tty->term_flags = 0;
}
@@ -61,28 +76,15 @@ tty_init(struct tty *tty, int fd, char *path, char *term)
int
tty_open(struct tty *tty, const char *overrides, char **cause)
{
- int mode;
+ int fd;
- if (tty->fd == -1) {
- tty->fd = open(tty->path, O_RDWR|O_NONBLOCK);
- if (tty->fd == -1) {
- xasprintf(cause, "%s: %s", tty->path, strerror(errno));
- return (-1);
- }
+ if (debug_level > 3) {
+ fd = open("tmux.out", O_WRONLY|O_CREAT|O_TRUNC, 0644);
+ if (fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
+ fatal("fcntl failed");
+ tty->log_fd = fd;
}
- if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
- fatal("fcntl failed");
- if (fcntl(tty->fd, F_SETFL, mode|O_NONBLOCK) == -1)
- fatal("fcntl failed");
- if (fcntl(tty->fd, F_SETFD, FD_CLOEXEC) == -1)
- fatal("fcntl failed");
-
- if (debug_level > 3)
- tty->log_fd = open("tmux.out", O_WRONLY|O_CREAT|O_TRUNC, 0644);
- else
- tty->log_fd = -1;
-
tty->term = tty_term_find(tty->termname, tty->fd, overrides, cause);
if (tty->term == NULL) {
tty_close(tty);