From 4d9af27b0bbb19466aecc9a39b9eb94796b2c7d8 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Fri, 23 Jan 2009 16:59:14 +0000 Subject: [PATCH] Better error messages for fork. --- CHANGES | 3 ++- TODO | 1 - cmd-attach-session.c | 4 ++-- cmd-link-window.c | 8 +++++--- cmd-move-window.c | 8 +++++--- cmd-new-session.c | 12 ++++++++---- cmd-new-window.c | 9 +++++---- cmd-respawn-window.c | 9 +++++---- cmd-split-window.c | 11 ++++++----- session.c | 22 +++++++++++++--------- tmux.h | 17 +++++++++-------- window.c | 29 +++++++++++++++++------------ 12 files changed, 77 insertions(+), 56 deletions(-) diff --git a/CHANGES b/CHANGES index 0d06ff20..69e19d33 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,6 @@ 23 January 2009 +* Better error messages when creating a session or window fails. * Oops. Return non-zero on error. Reported by Will Maier. 21 January 2009 @@ -1001,7 +1002,7 @@ (including mutt, emacs). No status bar yet and no key remapping or other customisation. -$Id: CHANGES,v 1.231 2009-01-23 16:19:56 nicm Exp $ +$Id: CHANGES,v 1.232 2009-01-23 16:59:14 nicm Exp $ LocalWords: showw utf UTF fulvio ciriaco joshe OSC APC gettime abc DEF OA clr LocalWords: rivo nurges lscm Erdely eol smysession mysession ek dstname RB ms diff --git a/TODO b/TODO index 2e5030ca..7fe47aca 100644 --- a/TODO +++ b/TODO @@ -86,5 +86,4 @@ - document split-window -p and -l - UTF-8 combining characters don't work (probably should be width 1 but are listed as 2) -- better error reporting from session_new etc - attach should have a flag to create session if it doesn't exist diff --git a/cmd-attach-session.c b/cmd-attach-session.c index 1a403c65..ad9ed857 100644 --- a/cmd-attach-session.c +++ b/cmd-attach-session.c @@ -1,4 +1,4 @@ -/* $Id: cmd-attach-session.c,v 1.23 2009-01-19 18:23:40 nicm Exp $ */ +/* $Id: cmd-attach-session.c,v 1.24 2009-01-23 16:59:14 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -62,7 +62,7 @@ cmd_attach_session_exec(struct cmd *self, struct cmd_ctx *ctx) } if (tty_open(&ctx->cmdclient->tty, &cause) != 0) { - ctx->error(ctx, "%s", cause); + ctx->error(ctx, "terminal open failed: %s", cause); xfree(cause); return (-1); } diff --git a/cmd-link-window.c b/cmd-link-window.c index 9227221a..48ebf891 100644 --- a/cmd-link-window.c +++ b/cmd-link-window.c @@ -1,4 +1,4 @@ -/* $Id: cmd-link-window.c,v 1.27 2009-01-19 18:23:40 nicm Exp $ */ +/* $Id: cmd-link-window.c,v 1.28 2009-01-23 16:59:14 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -47,6 +47,7 @@ cmd_link_window_exec(struct cmd *self, struct cmd_ctx *ctx) struct cmd_srcdst_data *data = self->data; struct session *dst; struct winlink *wl_src, *wl_dst; + char *cause; int idx; if ((wl_src = cmd_find_window(ctx, data->src, NULL)) == NULL) @@ -89,9 +90,10 @@ cmd_link_window_exec(struct cmd *self, struct cmd_ctx *ctx) } } - wl_dst = session_attach(dst, wl_src->window, idx); + wl_dst = session_attach(dst, wl_src->window, idx, &cause); if (wl_dst == NULL) { - ctx->error(ctx, "index in use: %d", idx); + ctx->error(ctx, "create session failed: %s", cause); + xfree(cause); return (-1); } diff --git a/cmd-move-window.c b/cmd-move-window.c index 65812b17..b7f02091 100644 --- a/cmd-move-window.c +++ b/cmd-move-window.c @@ -1,4 +1,4 @@ -/* $Id: cmd-move-window.c,v 1.4 2009-01-19 18:23:40 nicm Exp $ */ +/* $Id: cmd-move-window.c,v 1.5 2009-01-23 16:59:14 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -50,6 +50,7 @@ cmd_move_window_exec(struct cmd *self, struct cmd_ctx *ctx) struct client *c; u_int i; int destroyed, idx; + char *cause; if ((wl_src = cmd_find_window(ctx, data->src, &src)) == NULL) return (-1); @@ -91,9 +92,10 @@ cmd_move_window_exec(struct cmd *self, struct cmd_ctx *ctx) } } - wl_dst = session_attach(dst, wl_src->window, idx); + wl_dst = session_attach(dst, wl_src->window, idx, &cause); if (wl_dst == NULL) { - ctx->error(ctx, "index in use: %d", idx); + ctx->error(ctx, "attach window failed: %s", cause); + xfree(cause); return (-1); } diff --git a/cmd-new-session.c b/cmd-new-session.c index 322e8b4b..76dd5130 100644 --- a/cmd-new-session.c +++ b/cmd-new-session.c @@ -1,4 +1,4 @@ -/* $Id: cmd-new-session.c,v 1.37 2009-01-20 19:35:03 nicm Exp $ */ +/* $Id: cmd-new-session.c,v 1.38 2009-01-23 16:59:14 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -158,14 +158,18 @@ cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx) } if (!data->flag_detached && tty_open(&c->tty, &cause) != 0) { - ctx->error(ctx, "%s", cause); + ctx->error(ctx, "open terminal failed: %s", cause); xfree(cause); return (-1); } - if ((s = session_create(data->newname, cmd, cwd, sx, sy)) == NULL) - fatalx("session_create failed"); + s = session_create(data->newname, cmd, cwd, sx, sy, &cause); + if (s == NULL) { + ctx->error(ctx, "create session failed: %s", cause); + xfree(cause); + return (-1); + } if (data->winname != NULL) { xfree(s->curw->window->name); s->curw->window->name = xstrdup(data->winname); diff --git a/cmd-new-window.c b/cmd-new-window.c index 921a47d6..76872b5a 100644 --- a/cmd-new-window.c +++ b/cmd-new-window.c @@ -1,4 +1,4 @@ -/* $Id: cmd-new-window.c,v 1.30 2009-01-21 22:21:49 nicm Exp $ */ +/* $Id: cmd-new-window.c,v 1.31 2009-01-23 16:59:14 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -120,7 +120,7 @@ cmd_new_window_exec(struct cmd *self, struct cmd_ctx *ctx) struct cmd_new_window_data *data = self->data; struct session *s; struct winlink *wl; - char *cmd, *cwd; + char *cmd, *cwd, *cause; int idx; if (data == NULL) @@ -168,9 +168,10 @@ cmd_new_window_exec(struct cmd *self, struct cmd_ctx *ctx) else cwd = ctx->cmdclient->cwd; - wl = session_new(s, data->name, cmd, cwd, idx); + wl = session_new(s, data->name, cmd, cwd, idx, &cause); if (wl == NULL) { - ctx->error(ctx, "command failed: %s", cmd); + ctx->error(ctx, "create window failed: %s", cause); + xfree(cause); return (-1); } if (!data->flag_detached) { diff --git a/cmd-respawn-window.c b/cmd-respawn-window.c index deccb6f8..8dcbfaea 100644 --- a/cmd-respawn-window.c +++ b/cmd-respawn-window.c @@ -1,4 +1,4 @@ -/* $Id: cmd-respawn-window.c,v 1.12 2009-01-19 18:23:40 nicm Exp $ */ +/* $Id: cmd-respawn-window.c,v 1.13 2009-01-23 16:59:14 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -50,7 +50,7 @@ cmd_respawn_window_exec(struct cmd *self, struct cmd_ctx *ctx) struct window_pane *wp; struct session *s; const char *env[] = CHILD_ENVIRON; - char buf[256]; + char buf[256], *cause; u_int i; if ((wl = cmd_find_window(ctx, data->target, &s)) == NULL) @@ -77,8 +77,9 @@ cmd_respawn_window_exec(struct cmd *self, struct cmd_ctx *ctx) window_destroy_panes(w); TAILQ_INSERT_HEAD(&w->panes, wp, entry); window_pane_resize(wp, w->sx, w->sy); - if (window_pane_spawn(wp, data->arg, NULL, env) != 0) { - ctx->error(ctx, "respawn failed: %s:%d", s->name, wl->idx); + if (window_pane_spawn(wp, data->arg, NULL, env, &cause) != 0) { + ctx->error(ctx, "respawn window failed: %s", cause); + xfree(cause); return (-1); } screen_reinit(&wp->base); diff --git a/cmd-split-window.c b/cmd-split-window.c index bf725377..7471446c 100644 --- a/cmd-split-window.c +++ b/cmd-split-window.c @@ -1,4 +1,4 @@ -/* $Id: cmd-split-window.c,v 1.7 2009-01-21 19:38:51 nicm Exp $ */ +/* $Id: cmd-split-window.c,v 1.8 2009-01-23 16:59:14 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott @@ -140,8 +140,7 @@ cmd_split_window_exec(struct cmd *self, struct cmd_ctx *ctx) struct window *w; struct window_pane *wp; const char *env[] = CHILD_ENVIRON; - char buf[256]; - char *cmd, *cwd; + char buf[256], *cmd, *cwd, *cause; u_int i, hlimit, lines; if ((wl = cmd_find_window(ctx, data->target, &s)) == NULL) @@ -168,8 +167,10 @@ cmd_split_window_exec(struct cmd *self, struct cmd_ctx *ctx) lines = (w->active->sy * data->percentage) / 100; hlimit = options_get_number(&s->options, "history-limit"); - if ((wp = window_add_pane(w, lines, cmd, cwd, env, hlimit)) == NULL) { - ctx->error(ctx, "command failed: %s", cmd); + wp = window_add_pane(w, lines, cmd, cwd, env, hlimit, &cause); + if (wp == NULL) { + ctx->error(ctx, "create pane failed: %s", cause); + xfree(cause); return (-1); } server_redraw_window(w); diff --git a/session.c b/session.c index 9d381f38..d2984bc1 100644 --- a/session.c +++ b/session.c @@ -1,4 +1,4 @@ -/* $Id: session.c,v 1.51 2009-01-18 18:31:45 nicm Exp $ */ +/* $Id: session.c,v 1.52 2009-01-23 16:59:14 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -112,8 +112,8 @@ session_find(const char *name) /* Create a new session. */ struct session * -session_create( - const char *name, const char *cmd, const char *cwd, u_int sx, u_int sy) +session_create(const char *name, + const char *cmd, const char *cwd, u_int sx, u_int sy, char **cause) { struct session *s; u_int i; @@ -145,7 +145,7 @@ session_create( s->name = xstrdup(name); else xasprintf(&s->name, "%u", i); - if (session_new(s, NULL, cmd, cwd, -1) == NULL) { + if (session_new(s, NULL, cmd, cwd, -1, cause) == NULL) { session_destroy(s); return (NULL); } @@ -197,7 +197,7 @@ session_index(struct session *s, u_int *i) /* Create a new window on a session. */ struct winlink * session_new(struct session *s, - const char *name, const char *cmd, const char *cwd, int idx) + const char *name, const char *cmd, const char *cwd, int idx, char **cause) { struct window *w; const char *env[] = CHILD_ENVIRON; @@ -210,18 +210,22 @@ session_new(struct session *s, env[0] = buf; hlimit = options_get_number(&s->options, "history-limit"); - w = window_create(name, cmd, cwd, env, s->sx, s->sy, hlimit); + w = window_create(name, cmd, cwd, env, s->sx, s->sy, hlimit, cause); if (w == NULL) return (NULL); - return (session_attach(s, w, idx)); + return (session_attach(s, w, idx, cause)); } /* Attach a window to a session. */ struct winlink * -session_attach(struct session *s, struct window *w, int idx) +session_attach(struct session *s, struct window *w, int idx, char **cause) { - return (winlink_add(&s->windows, w, idx)); + struct winlink *wl; + + if ((wl = winlink_add(&s->windows, w, idx)) == NULL) + xasprintf(cause, "index in use: %d", idx); + return (wl); } /* Detach a window from a session. */ diff --git a/tmux.h b/tmux.h index 327552e9..a0590fdd 100644 --- a/tmux.h +++ b/tmux.h @@ -1,4 +1,4 @@ -/* $Id: tmux.h,v 1.247 2009-01-21 22:47:31 nicm Exp $ */ +/* $Id: tmux.h,v 1.248 2009-01-23 16:59:14 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -1419,12 +1419,12 @@ void winlink_stack_push(struct winlink_stack *, struct winlink *); void winlink_stack_remove(struct winlink_stack *, struct winlink *); int window_index(struct window *, u_int *); struct window *window_create(const char *, const char *, - const char *, const char **, u_int, u_int, u_int); + const char *, const char **, u_int, u_int, u_int, char **); void window_destroy(struct window *); int window_resize(struct window *, u_int, u_int); void window_set_active_pane(struct window *, struct window_pane *); struct window_pane *window_add_pane(struct window *, int, - const char *, const char *, const char **, u_int); + const char *, const char *, const char **, u_int, char **); void window_remove_pane(struct window *, struct window_pane *); u_int window_index_of_pane(struct window *, struct window_pane *); struct window_pane *window_pane_at_index(struct window *, u_int); @@ -1435,7 +1435,7 @@ void window_destroy_panes(struct window *); struct window_pane *window_pane_create(struct window *, u_int, u_int, u_int); void window_pane_destroy(struct window_pane *); int window_pane_spawn(struct window_pane *, - const char *, const char *, const char **); + const char *, const char *, const char **, char **); int window_pane_resize(struct window_pane *, u_int, u_int); void window_calculate_sizes(struct window *); int window_pane_set_mode( @@ -1478,13 +1478,14 @@ void session_alert_cancel(struct session *, struct winlink *); int session_alert_has(struct session *, struct winlink *, int); int session_alert_has_window(struct session *, struct window *, int); struct session *session_find(const char *); -struct session *session_create( - const char *, const char *, const char *, u_int, u_int); +struct session *session_create(const char *, const char *, + const char *, u_int, u_int, char **); void session_destroy(struct session *); int session_index(struct session *, u_int *); struct winlink *session_new(struct session *, - const char *, const char *, const char *, int); -struct winlink *session_attach(struct session *, struct window *, int); + const char *, const char *, const char *, int, char **); +struct winlink *session_attach( + struct session *, struct window *, int, char **); int session_detach(struct session *, struct winlink *); int session_has(struct session *, struct window *); int session_next(struct session *, int); diff --git a/window.c b/window.c index f9c2870e..9eb96e25 100644 --- a/window.c +++ b/window.c @@ -1,4 +1,4 @@ -/* $Id: window.c,v 1.61 2009-01-21 19:38:51 nicm Exp $ */ +/* $Id: window.c,v 1.62 2009-01-23 16:59:14 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -200,13 +201,14 @@ window_index(struct window *s, u_int *i) } struct window * -window_create(const char *name, const char *cmd, - const char *cwd, const char **envp, u_int sx, u_int sy, u_int hlimit) +window_create(const char *name, const char *cmd, const char *cwd, + const char **envp, u_int sx, u_int sy, u_int hlimit, char **cause) { struct window *w; u_int i; w = xmalloc(sizeof *w); + w->name = NULL; w->flags = 0; TAILQ_INIT(&w->panes); @@ -228,7 +230,7 @@ window_create(const char *name, const char *cmd, ARRAY_ADD(&windows, w); w->references = 0; - if (window_add_pane(w, -1, cmd, cwd, envp, hlimit) == NULL) { + if (window_add_pane(w, -1, cmd, cwd, envp, hlimit, cause) == NULL) { window_destroy(w); return (NULL); } @@ -256,8 +258,9 @@ window_destroy(struct window *w) options_free(&w->options); window_destroy_panes(w); - - xfree(w->name); + + if (w->name != NULL) + xfree(w->name); xfree(w); } @@ -378,8 +381,8 @@ window_set_active_pane(struct window *w, struct window_pane *wp) } struct window_pane * -window_add_pane(struct window *w, int wanty, - const char *cmd, const char *cwd, const char **envp, u_int hlimit) +window_add_pane(struct window *w, int wanty, const char *cmd, + const char *cwd, const char **envp, u_int hlimit, char **cause) { struct window_pane *wp; u_int sizey; @@ -408,7 +411,7 @@ window_add_pane(struct window *w, int wanty, else TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry); window_update_panes(w); - if (window_pane_spawn(wp, cmd, cwd, envp) != 0) { + if (window_pane_spawn(wp, cmd, cwd, envp, cause) != 0) { window_remove_pane(w, wp); return (NULL); } @@ -535,7 +538,7 @@ window_pane_destroy(struct window_pane *wp) int window_pane_spawn(struct window_pane *wp, - const char *cmd, const char *cwd, const char **envp) + const char *cmd, const char *cwd, const char **envp, char **cause) { struct winsize ws; int mode; @@ -564,11 +567,13 @@ window_pane_spawn(struct window_pane *wp, fatal("gettimeofday"); tv.tv_sec = 0; tv.tv_usec = NAME_INTERVAL * 1000L; - timeradd(&wp->window->name_timer, &tv, &wp->window->name_timer); + timeradd(&wp->window->name_timer, &tv, &wp->window->name_timer); switch (forkpty(&wp->fd, NULL, NULL, &ws)) { case -1: - return (1); + wp->fd = -1; + xasprintf(cause, "%s: %s", cmd, strerror(errno)); + return (-1); case 0: if (chdir(wp->cwd) != 0) chdir("/");