2009-09-20 19:15:01 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
|
2009-10-11 09:04:33 +00:00
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
|
2009-09-20 19:15:01 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2021-03-15 13:06:33 +00:00
|
|
|
#include <ctype.h>
|
2012-07-10 11:53:01 +00:00
|
|
|
#include <stdlib.h>
|
2009-09-20 19:15:01 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Runs a command without a window.
|
|
|
|
*/
|
|
|
|
|
2021-08-25 08:51:55 +00:00
|
|
|
static enum args_parse_type cmd_run_shell_args_parse(struct args *, u_int,
|
|
|
|
char **);
|
|
|
|
static enum cmd_retval cmd_run_shell_exec(struct cmd *,
|
|
|
|
struct cmdq_item *);
|
2012-11-27 15:09:35 +00:00
|
|
|
|
2020-03-12 13:25:45 +00:00
|
|
|
static void cmd_run_shell_timer(int, short, void *);
|
2016-10-10 21:51:39 +00:00
|
|
|
static void cmd_run_shell_callback(struct job *);
|
|
|
|
static void cmd_run_shell_free(void *);
|
|
|
|
static void cmd_run_shell_print(struct job *, const char *);
|
2009-10-11 08:58:05 +00:00
|
|
|
|
2009-09-20 19:15:01 +00:00
|
|
|
const struct cmd_entry cmd_run_shell_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "run-shell",
|
|
|
|
.alias = "run",
|
|
|
|
|
2021-08-25 08:51:55 +00:00
|
|
|
.args = { "bd:Ct:", 0, 1, cmd_run_shell_args_parse },
|
2021-01-01 08:36:51 +00:00
|
|
|
.usage = "[-bC] [-d delay] " CMD_TARGET_PANE_USAGE " [shell-command]",
|
2015-12-13 21:53:57 +00:00
|
|
|
|
2017-04-22 10:22:39 +00:00
|
|
|
.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
|
2015-12-14 00:31:54 +00:00
|
|
|
|
|
|
|
.flags = 0,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_run_shell_exec
|
2009-09-20 19:15:01 +00:00
|
|
|
};
|
|
|
|
|
2009-10-11 08:58:05 +00:00
|
|
|
struct cmd_run_shell_data {
|
2021-09-15 07:38:30 +00:00
|
|
|
struct client *client;
|
|
|
|
char *cmd;
|
|
|
|
struct args_command_state *state;
|
|
|
|
char *cwd;
|
|
|
|
struct cmdq_item *item;
|
|
|
|
struct session *s;
|
|
|
|
int wp_id;
|
|
|
|
struct event timer;
|
|
|
|
int flags;
|
2009-10-11 08:58:05 +00:00
|
|
|
};
|
|
|
|
|
2021-08-25 08:51:55 +00:00
|
|
|
static enum args_parse_type
|
|
|
|
cmd_run_shell_args_parse(struct args *args, __unused u_int idx,
|
|
|
|
__unused char **cause)
|
|
|
|
{
|
|
|
|
if (args_has(args, 'C'))
|
|
|
|
return (ARGS_PARSE_COMMANDS_OR_STRING);
|
|
|
|
return (ARGS_PARSE_STRING);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static void
|
2012-11-27 15:09:35 +00:00
|
|
|
cmd_run_shell_print(struct job *job, const char *msg)
|
|
|
|
{
|
2018-08-23 15:45:05 +00:00
|
|
|
struct cmd_run_shell_data *cdata = job_get_data(job);
|
2013-03-25 11:43:01 +00:00
|
|
|
struct window_pane *wp = NULL;
|
2016-11-12 19:05:53 +00:00
|
|
|
struct cmd_find_state fs;
|
2019-03-12 11:16:49 +00:00
|
|
|
struct window_mode_entry *wme;
|
2012-11-27 15:09:35 +00:00
|
|
|
|
2013-03-25 11:43:01 +00:00
|
|
|
if (cdata->wp_id != -1)
|
|
|
|
wp = window_pane_find_by_id(cdata->wp_id);
|
2022-06-02 21:19:32 +00:00
|
|
|
if (wp == NULL && cdata->item != NULL && cdata->client != NULL)
|
2022-05-30 13:00:18 +00:00
|
|
|
wp = server_client_get_pane(cdata->client);
|
|
|
|
if (wp == NULL && cmd_find_from_nothing(&fs, 0) == 0)
|
2016-11-12 19:05:53 +00:00
|
|
|
wp = fs.wp;
|
2022-05-30 13:00:18 +00:00
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
2012-11-27 15:09:35 +00:00
|
|
|
|
2019-03-12 11:16:49 +00:00
|
|
|
wme = TAILQ_FIRST(&wp->modes);
|
|
|
|
if (wme == NULL || wme->mode != &window_view_mode)
|
2020-04-10 07:44:26 +00:00
|
|
|
window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL);
|
2022-05-30 13:00:18 +00:00
|
|
|
window_copy_add(wp, 1, "%s", msg);
|
2012-11-27 15:09:35 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
|
2009-09-20 19:15:01 +00:00
|
|
|
{
|
2020-04-13 08:26:27 +00:00
|
|
|
struct args *args = cmd_get_args(self);
|
2020-04-13 10:59:58 +00:00
|
|
|
struct cmd_find_state *target = cmdq_get_target(item);
|
2009-10-11 08:58:05 +00:00
|
|
|
struct cmd_run_shell_data *cdata;
|
2021-01-01 08:36:51 +00:00
|
|
|
struct client *tc = cmdq_get_target_client(item);
|
2020-04-13 10:59:58 +00:00
|
|
|
struct session *s = target->s;
|
|
|
|
struct window_pane *wp = target->wp;
|
2021-08-23 12:33:55 +00:00
|
|
|
const char *delay, *cmd;
|
2020-03-12 13:25:45 +00:00
|
|
|
double d;
|
|
|
|
struct timeval tv;
|
|
|
|
char *end;
|
2021-01-01 08:36:51 +00:00
|
|
|
int wait = !args_has(args, 'b');
|
|
|
|
|
|
|
|
if ((delay = args_get(args, 'd')) != NULL) {
|
|
|
|
d = strtod(delay, &end);
|
|
|
|
if (*end != '\0') {
|
|
|
|
cmdq_error(item, "invalid delay time: %s", delay);
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
2021-08-20 19:50:16 +00:00
|
|
|
} else if (args_count(args) == 0)
|
2021-01-01 08:36:51 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2016-10-09 08:06:51 +00:00
|
|
|
cdata = xcalloc(1, sizeof *cdata);
|
2021-08-23 12:33:55 +00:00
|
|
|
if (!args_has(args, 'C')) {
|
|
|
|
cmd = args_string(args, 0);
|
|
|
|
if (cmd != NULL)
|
|
|
|
cdata->cmd = format_single_from_target(item, cmd);
|
|
|
|
} else {
|
2021-09-15 07:38:30 +00:00
|
|
|
cdata->state = args_make_commands_prepare(self, item, 0, NULL,
|
|
|
|
wait, 1);
|
2021-01-01 08:36:51 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
if (args_has(args, 't') && wp != NULL)
|
|
|
|
cdata->wp_id = wp->id;
|
|
|
|
else
|
|
|
|
cdata->wp_id = -1;
|
|
|
|
|
2021-01-01 08:36:51 +00:00
|
|
|
if (wait) {
|
|
|
|
cdata->client = cmdq_get_client(item);
|
2016-10-16 19:04:05 +00:00
|
|
|
cdata->item = item;
|
2021-01-01 08:36:51 +00:00
|
|
|
} else {
|
|
|
|
cdata->client = tc;
|
2020-06-12 10:31:12 +00:00
|
|
|
cdata->flags |= JOB_NOWAIT;
|
2021-01-01 08:36:51 +00:00
|
|
|
}
|
|
|
|
if (cdata->client != NULL)
|
|
|
|
cdata->client->references++;
|
2009-10-11 08:58:05 +00:00
|
|
|
|
2020-04-13 10:59:58 +00:00
|
|
|
cdata->cwd = xstrdup(server_client_get_cwd(cmdq_get_client(item), s));
|
2021-01-01 08:36:51 +00:00
|
|
|
|
2020-03-12 13:25:45 +00:00
|
|
|
cdata->s = s;
|
2020-03-13 06:19:33 +00:00
|
|
|
if (s != NULL)
|
|
|
|
session_add_ref(s, __func__);
|
2020-03-12 13:25:45 +00:00
|
|
|
|
|
|
|
evtimer_set(&cdata->timer, cmd_run_shell_timer, cdata);
|
2021-01-01 08:36:51 +00:00
|
|
|
if (delay != NULL) {
|
2020-03-12 13:25:45 +00:00
|
|
|
timerclear(&tv);
|
|
|
|
tv.tv_sec = (time_t)d;
|
|
|
|
tv.tv_usec = (d - (double)tv.tv_sec) * 1000000U;
|
|
|
|
evtimer_add(&cdata->timer, &tv);
|
|
|
|
} else
|
2021-01-01 08:36:51 +00:00
|
|
|
event_active(&cdata->timer, EV_TIMEOUT, 1);
|
2009-10-11 08:58:05 +00:00
|
|
|
|
2021-01-01 08:36:51 +00:00
|
|
|
if (!wait)
|
2013-03-24 09:54:10 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
return (CMD_RETURN_WAIT);
|
2009-10-11 08:58:05 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 13:25:45 +00:00
|
|
|
static void
|
|
|
|
cmd_run_shell_timer(__unused int fd, __unused short events, void* arg)
|
|
|
|
{
|
|
|
|
struct cmd_run_shell_data *cdata = arg;
|
2021-01-01 08:36:51 +00:00
|
|
|
struct client *c = cdata->client;
|
|
|
|
const char *cmd = cdata->cmd;
|
2021-08-23 12:33:55 +00:00
|
|
|
struct cmdq_item *item = cdata->item, *new_item;
|
2021-09-15 07:38:30 +00:00
|
|
|
struct cmd_list *cmdlist;
|
|
|
|
char *error;
|
2020-03-12 13:25:45 +00:00
|
|
|
|
2021-09-16 06:39:22 +00:00
|
|
|
if (cdata->state == NULL) {
|
|
|
|
if (cmd == NULL) {
|
|
|
|
if (cdata->item != NULL)
|
|
|
|
cmdq_continue(cdata->item);
|
|
|
|
cmd_run_shell_free(cdata);
|
|
|
|
return;
|
|
|
|
}
|
2021-10-11 10:55:30 +00:00
|
|
|
if (job_run(cmd, 0, NULL, NULL, cdata->s, cdata->cwd, NULL,
|
2020-06-12 10:31:12 +00:00
|
|
|
cmd_run_shell_callback, cmd_run_shell_free, cdata,
|
|
|
|
cdata->flags, -1, -1) == NULL)
|
2020-03-12 13:25:45 +00:00
|
|
|
cmd_run_shell_free(cdata);
|
2021-01-01 08:36:51 +00:00
|
|
|
return;
|
2020-03-12 13:25:45 +00:00
|
|
|
}
|
2021-01-01 08:36:51 +00:00
|
|
|
|
2021-09-15 07:38:30 +00:00
|
|
|
cmdlist = args_make_commands(cdata->state, 0, NULL, &error);
|
|
|
|
if (cmdlist == NULL) {
|
|
|
|
if (cdata->item == NULL) {
|
|
|
|
*error = toupper((u_char)*error);
|
|
|
|
status_message_set(c, -1, 1, 0, "%s", error);
|
|
|
|
} else
|
|
|
|
cmdq_error(cdata->item, "%s", error);
|
|
|
|
free(error);
|
|
|
|
} else if (item == NULL) {
|
|
|
|
new_item = cmdq_get_command(cmdlist, NULL);
|
|
|
|
cmdq_append(c, new_item);
|
|
|
|
} else {
|
|
|
|
new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
|
|
|
|
cmdq_insert_after(item, new_item);
|
2021-01-01 08:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cdata->item != NULL)
|
|
|
|
cmdq_continue(cdata->item);
|
|
|
|
cmd_run_shell_free(cdata);
|
2020-03-12 13:25:45 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static void
|
2009-10-11 08:58:05 +00:00
|
|
|
cmd_run_shell_callback(struct job *job)
|
|
|
|
{
|
2018-08-23 15:45:05 +00:00
|
|
|
struct cmd_run_shell_data *cdata = job_get_data(job);
|
|
|
|
struct bufferevent *event = job_get_event(job);
|
2020-03-21 13:15:38 +00:00
|
|
|
struct cmdq_item *item = cdata->item;
|
2018-08-23 15:45:05 +00:00
|
|
|
char *cmd = cdata->cmd, *msg = NULL, *line;
|
2009-11-04 21:04:43 +00:00
|
|
|
size_t size;
|
2018-08-23 15:45:05 +00:00
|
|
|
int retcode, status;
|
2009-10-11 08:58:05 +00:00
|
|
|
|
2009-11-04 21:04:43 +00:00
|
|
|
do {
|
2022-05-30 13:00:18 +00:00
|
|
|
line = evbuffer_readln(event->input, NULL, EVBUFFER_EOL_LF);
|
|
|
|
if (line != NULL) {
|
2013-03-24 09:54:10 +00:00
|
|
|
cmd_run_shell_print(job, line);
|
2013-03-22 15:55:22 +00:00
|
|
|
free(line);
|
2009-11-04 21:04:43 +00:00
|
|
|
}
|
|
|
|
} while (line != NULL);
|
2009-10-11 08:58:05 +00:00
|
|
|
|
2018-08-23 15:45:05 +00:00
|
|
|
size = EVBUFFER_LENGTH(event->input);
|
2009-11-04 21:04:43 +00:00
|
|
|
if (size != 0) {
|
|
|
|
line = xmalloc(size + 1);
|
2018-08-23 15:45:05 +00:00
|
|
|
memcpy(line, EVBUFFER_DATA(event->input), size);
|
2009-11-04 21:04:43 +00:00
|
|
|
line[size] = '\0';
|
2009-09-20 19:15:01 +00:00
|
|
|
|
2012-11-27 15:09:35 +00:00
|
|
|
cmd_run_shell_print(job, line);
|
2009-11-04 21:04:43 +00:00
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(line);
|
2009-09-20 19:15:01 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 15:45:05 +00:00
|
|
|
status = job_get_status(job);
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
if ((retcode = WEXITSTATUS(status)) != 0)
|
2009-10-11 08:58:05 +00:00
|
|
|
xasprintf(&msg, "'%s' returned %d", cmd, retcode);
|
2018-08-23 15:45:05 +00:00
|
|
|
} else if (WIFSIGNALED(status)) {
|
|
|
|
retcode = WTERMSIG(status);
|
2009-10-11 08:58:05 +00:00
|
|
|
xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
|
2020-03-21 13:15:38 +00:00
|
|
|
retcode += 128;
|
2020-05-16 16:02:24 +00:00
|
|
|
} else
|
|
|
|
retcode = 0;
|
2014-04-17 07:55:43 +00:00
|
|
|
if (msg != NULL)
|
|
|
|
cmd_run_shell_print(job, msg);
|
|
|
|
free(msg);
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2020-03-21 13:15:38 +00:00
|
|
|
if (item != NULL) {
|
2020-04-13 10:59:58 +00:00
|
|
|
if (cmdq_get_client(item) != NULL &&
|
|
|
|
cmdq_get_client(item)->session == NULL)
|
|
|
|
cmdq_get_client(item)->retval = retcode;
|
2020-03-21 13:15:38 +00:00
|
|
|
cmdq_continue(item);
|
|
|
|
}
|
2009-10-11 08:58:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static void
|
2009-10-11 08:58:05 +00:00
|
|
|
cmd_run_shell_free(void *data)
|
|
|
|
{
|
|
|
|
struct cmd_run_shell_data *cdata = data;
|
|
|
|
|
2020-03-12 13:25:45 +00:00
|
|
|
evtimer_del(&cdata->timer);
|
2020-03-13 06:19:33 +00:00
|
|
|
if (cdata->s != NULL)
|
|
|
|
session_remove_ref(cdata->s, __func__);
|
2021-01-01 08:36:51 +00:00
|
|
|
if (cdata->client != NULL)
|
|
|
|
server_client_unref(cdata->client);
|
2021-09-15 07:38:30 +00:00
|
|
|
if (cdata->state != NULL)
|
|
|
|
args_make_commands_free(cdata->state);
|
2020-03-12 13:25:45 +00:00
|
|
|
free(cdata->cwd);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cdata->cmd);
|
|
|
|
free(cdata);
|
2009-09-20 19:15:01 +00:00
|
|
|
}
|