From 053e40572c32d021ad8bd2922c97c5e6dcbe2c81 Mon Sep 17 00:00:00 2001 From: Tiago Cunha Date: Mon, 2 Nov 2009 21:38:27 +0000 Subject: [PATCH] Sync OpenBSD patchset 475: Add a flag for jobs that shouldn't be freed after they've died and use it for status jobs, then only kill those jobs when status-left, status-right or set-titles-string is changed. Fixes problems with changing options from inside #(). --- cmd-if-shell.c | 8 +++----- cmd-run-shell.c | 6 ++---- cmd-server-info.c | 9 ++++++++- cmd-set-option.c | 38 ++++++++++++++++++++++++++++++++------ job.c | 18 ++++++++++++++---- server-job.c | 7 +++++-- status.c | 6 +++--- tmux.h | 6 ++++-- 8 files changed, 71 insertions(+), 27 deletions(-) diff --git a/cmd-if-shell.c b/cmd-if-shell.c index 2ef4f7a3..10c2f1d6 100644 --- a/cmd-if-shell.c +++ b/cmd-if-shell.c @@ -1,4 +1,4 @@ -/* $Id: cmd-if-shell.c,v 1.6 2009-10-12 00:29:03 tcunha Exp $ */ +/* $Id: cmd-if-shell.c,v 1.7 2009-11-02 21:38:26 tcunha Exp $ */ /* * Copyright (c) 2009 Tiago Cunha @@ -65,7 +65,7 @@ cmd_if_shell_exec(struct cmd *self, struct cmd_ctx *ctx) if (ctx->curclient != NULL) ctx->curclient->references++; - job = job_add(NULL, NULL, + job = job_add(NULL, 0, NULL, data->arg, cmd_if_shell_callback, cmd_if_shell_free, cdata); job_run(job); @@ -80,10 +80,8 @@ cmd_if_shell_callback(struct job *job) struct cmd_list *cmdlist; char *cause; - if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0) { - job_free(job); /* calls cmd_if_shell_free */ + if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0) return; - } if (cmd_string_parse(cdata->cmd, &cmdlist, &cause) != 0) { if (cause != NULL) { diff --git a/cmd-run-shell.c b/cmd-run-shell.c index 8ddfbb81..15d117e4 100644 --- a/cmd-run-shell.c +++ b/cmd-run-shell.c @@ -1,4 +1,4 @@ -/* $Id: cmd-run-shell.c,v 1.3 2009-10-12 00:26:06 tcunha Exp $ */ +/* $Id: cmd-run-shell.c,v 1.4 2009-11-02 21:38:26 tcunha Exp $ */ /* * Copyright (c) 2009 Tiago Cunha @@ -65,7 +65,7 @@ cmd_run_shell_exec(struct cmd *self, struct cmd_ctx *ctx) if (ctx->curclient != NULL) ctx->curclient->references++; - job = job_add(NULL, NULL, + job = job_add(NULL, 0, NULL, data->arg, cmd_run_shell_callback, cmd_run_shell_free, cdata); job_run(job); @@ -117,8 +117,6 @@ cmd_run_shell_callback(struct job *job) ctx->info(ctx, "%s", msg); xfree(msg); } - - job_free(job); /* calls cmd_run_shell_free */ } void diff --git a/cmd-server-info.c b/cmd-server-info.c index fff0b831..90c3bf60 100644 --- a/cmd-server-info.c +++ b/cmd-server-info.c @@ -1,4 +1,4 @@ -/* $Id: cmd-server-info.c,v 1.31 2009-10-28 23:12:38 tcunha Exp $ */ +/* $Id: cmd-server-info.c,v 1.32 2009-11-02 21:38:26 tcunha Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -55,6 +55,7 @@ cmd_server_info_exec(unused struct cmd *self, struct cmd_ctx *ctx) struct tty_code *code; struct tty_term_code_entry *ent; struct utsname un; + struct job *job; struct grid *gd; struct grid_line *gl; u_int i, j, k; @@ -178,5 +179,11 @@ cmd_server_info_exec(unused struct cmd *self, struct cmd_ctx *ctx) } ctx->print(ctx, "%s", ""); + ctx->print(ctx, "Jobs:"); + SLIST_FOREACH(job, &all_jobs, lentry) { + ctx->print(ctx, "%s [fd=%d, pid=%d, status=%d, flags=0x%x]", + job->cmd, job->fd, job->pid, job->status, job->flags); + } + return (0); } diff --git a/cmd-set-option.c b/cmd-set-option.c index 901523b1..89e43bed 100644 --- a/cmd-set-option.c +++ b/cmd-set-option.c @@ -1,4 +1,4 @@ -/* $Id: cmd-set-option.c,v 1.84 2009-10-11 23:55:26 tcunha Exp $ */ +/* $Id: cmd-set-option.c,v 1.85 2009-11-02 21:38:26 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -108,7 +108,10 @@ cmd_set_option_exec(struct cmd *self, struct cmd_ctx *ctx) struct client *c; struct options *oo; const struct set_option_entry *entry, *opt; + struct jobs *jobs; + struct job *job, *nextjob; u_int i; + int try_again; if (data->chflags & CMD_CHFLAG('g')) oo = &global_s_options; @@ -184,11 +187,34 @@ cmd_set_option_exec(struct cmd *self, struct cmd_ctx *ctx) } recalculate_sizes(); - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c != NULL && c->session != NULL) { - job_tree_free(&c->status_jobs); - job_tree_init(&c->status_jobs); + + /* + * Special-case: kill all persistent jobs if status-left, status-right + * or set-titles-string have changed. Persistent jobs are only used by + * the status line at the moment so this works XXX. + */ + if (strcmp(entry->name, "status-left") == 0 || + strcmp(entry->name, "status-right") == 0 || + strcmp(entry->name, "set-titles-string") == 0) { + for (i = 0; i < ARRAY_LENGTH(&clients); i++) { + c = ARRAY_ITEM(&clients, i); + if (c == NULL || c->session == NULL) + continue; + + jobs = &c->status_jobs; + do { + try_again = 0; + job = RB_ROOT(jobs); + while (job != NULL) { + nextjob = RB_NEXT(jobs, jobs, job); + if (job->flags & JOB_PERSIST) { + job_remove(jobs, job); + try_again = 1; + break; + } + job = nextjob; + } + } while (try_again); server_redraw_client(c); } } diff --git a/job.c b/job.c index 3b735840..cd263325 100644 --- a/job.c +++ b/job.c @@ -1,4 +1,4 @@ -/* $Id: job.c,v 1.8 2009-10-23 17:27:40 tcunha Exp $ */ +/* $Id: job.c,v 1.9 2009-11-02 21:38:26 tcunha Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott @@ -72,7 +72,7 @@ job_get(struct jobs *jobs, const char *cmd) /* Add a job. */ struct job * -job_add(struct jobs *jobs, struct client *c, const char *cmd, +job_add(struct jobs *jobs, int flags, struct client *c, const char *cmd, void (*callbackfn)(struct job *), void (*freefn)(void *), void *data) { struct job *job; @@ -80,6 +80,7 @@ job_add(struct jobs *jobs, struct client *c, const char *cmd, job = xmalloc(sizeof *job); job->cmd = xstrdup(cmd); job->pid = -1; + job->status = 0; job->client = c; @@ -90,15 +91,24 @@ job_add(struct jobs *jobs, struct client *c, const char *cmd, job->freefn = freefn; job->data = data; - job->flags = JOB_DONE; + job->flags = flags|JOB_DONE; if (jobs != NULL) RB_INSERT(jobs, jobs, job); SLIST_INSERT_HEAD(&all_jobs, job, lentry); - + return (job); } +/* Remove job from tree and free. */ +void +job_remove(struct jobs *jobs, struct job *job) +{ + if (jobs != NULL) + RB_REMOVE(jobs, jobs, job); + job_free(job); +} + /* Kill and free an individual job. */ void job_free(struct job *job) diff --git a/server-job.c b/server-job.c index 366ab2df..7417de96 100644 --- a/server-job.c +++ b/server-job.c @@ -1,4 +1,4 @@ -/* $Id: server-job.c,v 1.2 2009-10-28 23:14:15 tcunha Exp $ */ +/* $Id: server-job.c,v 1.3 2009-11-02 21:38:26 tcunha Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott @@ -64,7 +64,10 @@ restart: if (job->callbackfn != NULL) { job->callbackfn(job); - goto restart; /* could be freed by callback */ + if ((!job->flags & JOB_PERSIST)) { + job_free(job); + goto restart; + } } } } diff --git a/status.c b/status.c index 909e2946..08ca4c2c 100644 --- a/status.c +++ b/status.c @@ -1,4 +1,4 @@ -/* $Id: status.c,v 1.123 2009-10-11 23:55:26 tcunha Exp $ */ +/* $Id: status.c,v 1.124 2009-11-02 21:38:26 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -476,8 +476,8 @@ status_job(struct client *c, char **iptr) job = job_get(&c->status_jobs, cmd); if (job == NULL) { - job = job_add( - &c->status_jobs, c, cmd, status_job_callback, xfree, NULL); + job = job_add(&c->status_jobs, + JOB_PERSIST, c, cmd, status_job_callback, xfree, NULL); job_run(job); } if (job->data == NULL) diff --git a/tmux.h b/tmux.h index 01fc1bb8..45db29ab 100644 --- a/tmux.h +++ b/tmux.h @@ -1,4 +1,4 @@ -/* $Id: tmux.h,v 1.490 2009-11-02 21:32:52 tcunha Exp $ */ +/* $Id: tmux.h,v 1.491 2009-11-02 21:38:27 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -667,6 +667,7 @@ struct job { int flags; #define JOB_DONE 0x1 +#define JOB_PERSIST 0x2 /* don't free after callback */ RB_ENTRY(job) entry; SLIST_ENTRY(job) lentry; @@ -1299,8 +1300,9 @@ RB_PROTOTYPE(jobs, job, entry, job_cmp); void job_tree_init(struct jobs *); void job_tree_free(struct jobs *); struct job *job_get(struct jobs *, const char *); -struct job *job_add(struct jobs *, struct client *, +struct job *job_add(struct jobs *, int, struct client *, const char *, void (*)(struct job *), void (*)(void *), void *); +void job_remove(struct jobs *, struct job *); void job_free(struct job *); int job_run(struct job *); void job_kill(struct job *);