Initialize cwd fd to -1 so that we don't close fd 0 if the client is

destroyed before it is changed. Also allow ttyname() to fail. Fixes
problems when running out of file descriptors reported by Bruno Sutic.
This commit is contained in:
nicm 2015-07-13 15:49:31 +00:00
parent 81069f66f9
commit c7374c31c4
3 changed files with 11 additions and 5 deletions

View File

@ -96,6 +96,8 @@ server_client_create(int fd)
environ_init(&c->environ); environ_init(&c->environ);
c->cwd = -1;
c->cmdq = cmdq_new(c); c->cmdq = cmdq_new(c);
c->cmdq->client_exit = 1; c->cmdq->client_exit = 1;
@ -1253,12 +1255,11 @@ server_client_msg_identify(struct client *c, struct imsg *imsg)
if (c->fd == -1) if (c->fd == -1)
return; return;
if (!isatty(c->fd)) { if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
close(c->fd); close(c->fd);
c->fd = -1; c->fd = -1;
return; return;
} }
tty_init(&c->tty, c, c->fd, c->term);
if (c->flags & CLIENT_UTF8) if (c->flags & CLIENT_UTF8)
c->tty.flags |= TTY_UTF8; c->tty.flags |= TTY_UTF8;
if (c->flags & CLIENT_256COLOURS) if (c->flags & CLIENT_256COLOURS)

2
tmux.h
View File

@ -1600,7 +1600,7 @@ void tty_putcode_ptr2(struct tty *, enum tty_code_code, const void *,
void tty_puts(struct tty *, const char *); void tty_puts(struct tty *, const char *);
void tty_putc(struct tty *, u_char); void tty_putc(struct tty *, u_char);
void tty_putn(struct tty *, const void *, size_t, u_int); void tty_putn(struct tty *, const void *, size_t, u_int);
void tty_init(struct tty *, struct client *, int, char *); int tty_init(struct tty *, struct client *, int, char *);
int tty_resize(struct tty *); int tty_resize(struct tty *);
int tty_set_size(struct tty *, u_int, u_int); int tty_set_size(struct tty *, u_int, u_int);
void tty_set_class(struct tty *, u_int); void tty_set_class(struct tty *, u_int);

9
tty.c
View File

@ -59,11 +59,14 @@ void tty_default_colours(struct grid_cell *, const struct window_pane *);
#define tty_pane_full_width(tty, ctx) \ #define tty_pane_full_width(tty, ctx) \
((ctx)->xoff == 0 && screen_size_x((ctx)->wp->screen) >= (tty)->sx) ((ctx)->xoff == 0 && screen_size_x((ctx)->wp->screen) >= (tty)->sx)
void int
tty_init(struct tty *tty, struct client *c, int fd, char *term) tty_init(struct tty *tty, struct client *c, int fd, char *term)
{ {
char *path; char *path;
if (!isatty(fd))
return (-1);
memset(tty, 0, sizeof *tty); memset(tty, 0, sizeof *tty);
tty->log_fd = -1; tty->log_fd = -1;
@ -75,13 +78,15 @@ tty_init(struct tty *tty, struct client *c, int fd, char *term)
tty->client = c; tty->client = c;
if ((path = ttyname(fd)) == NULL) if ((path = ttyname(fd)) == NULL)
fatalx("ttyname failed"); return (-1);
tty->path = xstrdup(path); tty->path = xstrdup(path);
tty->cstyle = 0; tty->cstyle = 0;
tty->ccolour = xstrdup(""); tty->ccolour = xstrdup("");
tty->flags = 0; tty->flags = 0;
tty->term_flags = 0; tty->term_flags = 0;
return (0);
} }
int int