1 /* $OpenBSD: cmd-run-shell.c,v 1.80 2021/09/15 07:38:30 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org> 5 * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 16 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 17 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/types.h> 21 #include <sys/wait.h> 22 23 #include <ctype.h> 24 #include <stdlib.h> 25 #include <string.h> 26 27 #include "tmux.h" 28 29 /* 30 * Runs a command without a window. 31 */ 32 33 static enum args_parse_type cmd_run_shell_args_parse(struct args *, u_int, 34 char **); 35 static enum cmd_retval cmd_run_shell_exec(struct cmd *, 36 struct cmdq_item *); 37 38 static void cmd_run_shell_timer(int, short, void *); 39 static void cmd_run_shell_callback(struct job *); 40 static void cmd_run_shell_free(void *); 41 static void cmd_run_shell_print(struct job *, const char *); 42 43 const struct cmd_entry cmd_run_shell_entry = { 44 .name = "run-shell", 45 .alias = "run", 46 47 .args = { "bd:Ct:", 0, 1, cmd_run_shell_args_parse }, 48 .usage = "[-bC] [-d delay] " CMD_TARGET_PANE_USAGE " [shell-command]", 49 50 .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL }, 51 52 .flags = 0, 53 .exec = cmd_run_shell_exec 54 }; 55 56 struct cmd_run_shell_data { 57 struct client *client; 58 char *cmd; 59 struct args_command_state *state; 60 char *cwd; 61 struct cmdq_item *item; 62 struct session *s; 63 int wp_id; 64 struct event timer; 65 int flags; 66 }; 67 68 static enum args_parse_type 69 cmd_run_shell_args_parse(struct args *args, __unused u_int idx, 70 __unused char **cause) 71 { 72 if (args_has(args, 'C')) 73 return (ARGS_PARSE_COMMANDS_OR_STRING); 74 return (ARGS_PARSE_STRING); 75 } 76 77 static void 78 cmd_run_shell_print(struct job *job, const char *msg) 79 { 80 struct cmd_run_shell_data *cdata = job_get_data(job); 81 struct window_pane *wp = NULL; 82 struct cmd_find_state fs; 83 struct window_mode_entry *wme; 84 85 if (cdata->wp_id != -1) 86 wp = window_pane_find_by_id(cdata->wp_id); 87 if (wp == NULL) { 88 if (cdata->item != NULL) { 89 cmdq_print(cdata->item, "%s", msg); 90 return; 91 } 92 if (cmd_find_from_nothing(&fs, 0) != 0) 93 return; 94 wp = fs.wp; 95 if (wp == NULL) 96 return; 97 } 98 99 wme = TAILQ_FIRST(&wp->modes); 100 if (wme == NULL || wme->mode != &window_view_mode) 101 window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL); 102 window_copy_add(wp, "%s", msg); 103 } 104 105 static enum cmd_retval 106 cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item) 107 { 108 struct args *args = cmd_get_args(self); 109 struct cmd_find_state *target = cmdq_get_target(item); 110 struct cmd_run_shell_data *cdata; 111 struct client *tc = cmdq_get_target_client(item); 112 struct session *s = target->s; 113 struct window_pane *wp = target->wp; 114 const char *delay, *cmd; 115 double d; 116 struct timeval tv; 117 char *end; 118 int wait = !args_has(args, 'b'); 119 120 if ((delay = args_get(args, 'd')) != NULL) { 121 d = strtod(delay, &end); 122 if (*end != '\0') { 123 cmdq_error(item, "invalid delay time: %s", delay); 124 return (CMD_RETURN_ERROR); 125 } 126 } else if (args_count(args) == 0) 127 return (CMD_RETURN_NORMAL); 128 129 cdata = xcalloc(1, sizeof *cdata); 130 if (!args_has(args, 'C')) { 131 cmd = args_string(args, 0); 132 if (cmd != NULL) 133 cdata->cmd = format_single_from_target(item, cmd); 134 } else { 135 cdata->state = args_make_commands_prepare(self, item, 0, NULL, 136 wait, 1); 137 } 138 139 if (args_has(args, 't') && wp != NULL) 140 cdata->wp_id = wp->id; 141 else 142 cdata->wp_id = -1; 143 144 if (wait) { 145 cdata->client = cmdq_get_client(item); 146 cdata->item = item; 147 } else { 148 cdata->client = tc; 149 cdata->flags |= JOB_NOWAIT; 150 } 151 if (cdata->client != NULL) 152 cdata->client->references++; 153 154 cdata->cwd = xstrdup(server_client_get_cwd(cmdq_get_client(item), s)); 155 156 cdata->s = s; 157 if (s != NULL) 158 session_add_ref(s, __func__); 159 160 evtimer_set(&cdata->timer, cmd_run_shell_timer, cdata); 161 if (delay != NULL) { 162 timerclear(&tv); 163 tv.tv_sec = (time_t)d; 164 tv.tv_usec = (d - (double)tv.tv_sec) * 1000000U; 165 evtimer_add(&cdata->timer, &tv); 166 } else 167 event_active(&cdata->timer, EV_TIMEOUT, 1); 168 169 if (!wait) 170 return (CMD_RETURN_NORMAL); 171 return (CMD_RETURN_WAIT); 172 } 173 174 static void 175 cmd_run_shell_timer(__unused int fd, __unused short events, void* arg) 176 { 177 struct cmd_run_shell_data *cdata = arg; 178 struct client *c = cdata->client; 179 const char *cmd = cdata->cmd; 180 struct cmdq_item *item = cdata->item, *new_item; 181 struct cmd_list *cmdlist; 182 char *error; 183 184 if (cdata->state == NULL && cmd != NULL) { 185 if (job_run(cmd, 0, NULL, cdata->s, cdata->cwd, NULL, 186 cmd_run_shell_callback, cmd_run_shell_free, cdata, 187 cdata->flags, -1, -1) == NULL) 188 cmd_run_shell_free(cdata); 189 return; 190 } 191 192 cmdlist = args_make_commands(cdata->state, 0, NULL, &error); 193 if (cmdlist == NULL) { 194 if (cdata->item == NULL) { 195 *error = toupper((u_char)*error); 196 status_message_set(c, -1, 1, 0, "%s", error); 197 } else 198 cmdq_error(cdata->item, "%s", error); 199 free(error); 200 } else if (item == NULL) { 201 new_item = cmdq_get_command(cmdlist, NULL); 202 cmdq_append(c, new_item); 203 } else { 204 new_item = cmdq_get_command(cmdlist, cmdq_get_state(item)); 205 cmdq_insert_after(item, new_item); 206 } 207 208 if (cdata->item != NULL) 209 cmdq_continue(cdata->item); 210 cmd_run_shell_free(cdata); 211 } 212 213 static void 214 cmd_run_shell_callback(struct job *job) 215 { 216 struct cmd_run_shell_data *cdata = job_get_data(job); 217 struct bufferevent *event = job_get_event(job); 218 struct cmdq_item *item = cdata->item; 219 char *cmd = cdata->cmd, *msg = NULL, *line; 220 size_t size; 221 int retcode, status; 222 223 do { 224 if ((line = evbuffer_readline(event->input)) != NULL) { 225 cmd_run_shell_print(job, line); 226 free(line); 227 } 228 } while (line != NULL); 229 230 size = EVBUFFER_LENGTH(event->input); 231 if (size != 0) { 232 line = xmalloc(size + 1); 233 memcpy(line, EVBUFFER_DATA(event->input), size); 234 line[size] = '\0'; 235 236 cmd_run_shell_print(job, line); 237 238 free(line); 239 } 240 241 status = job_get_status(job); 242 if (WIFEXITED(status)) { 243 if ((retcode = WEXITSTATUS(status)) != 0) 244 xasprintf(&msg, "'%s' returned %d", cmd, retcode); 245 } else if (WIFSIGNALED(status)) { 246 retcode = WTERMSIG(status); 247 xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode); 248 retcode += 128; 249 } else 250 retcode = 0; 251 if (msg != NULL) 252 cmd_run_shell_print(job, msg); 253 free(msg); 254 255 if (item != NULL) { 256 if (cmdq_get_client(item) != NULL && 257 cmdq_get_client(item)->session == NULL) 258 cmdq_get_client(item)->retval = retcode; 259 cmdq_continue(item); 260 } 261 } 262 263 static void 264 cmd_run_shell_free(void *data) 265 { 266 struct cmd_run_shell_data *cdata = data; 267 268 evtimer_del(&cdata->timer); 269 if (cdata->s != NULL) 270 session_remove_ref(cdata->s, __func__); 271 if (cdata->client != NULL) 272 server_client_unref(cdata->client); 273 if (cdata->state != NULL) 274 args_make_commands_free(cdata->state); 275 free(cdata->cwd); 276 free(cdata->cmd); 277 free(cdata); 278 } 279