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.
|
|
|
|
*/
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
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-01-01 08:36:51 +00:00
|
|
|
.args = { "bd:Ct:", 0, 1 },
|
|
|
|
.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-01-01 08:36:51 +00:00
|
|
|
struct client *client;
|
2016-10-16 19:04:05 +00:00
|
|
|
char *cmd;
|
2021-01-01 08:36:51 +00:00
|
|
|
int shell;
|
2020-03-12 13:25:45 +00:00
|
|
|
char *cwd;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_item *item;
|
2020-03-12 13:25:45 +00:00
|
|
|
struct session *s;
|
2016-10-16 19:04:05 +00:00
|
|
|
int wp_id;
|
2020-03-12 13:25:45 +00:00
|
|
|
struct event timer;
|
2020-06-12 10:31:12 +00:00
|
|
|
int flags;
|
2021-01-01 08:36:51 +00:00
|
|
|
struct cmd_parse_input pi;
|
2009-10-11 08:58:05 +00:00
|
|
|
};
|
|
|
|
|
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);
|
2016-11-12 19:05:53 +00:00
|
|
|
if (wp == NULL) {
|
|
|
|
if (cdata->item != NULL) {
|
|
|
|
cmdq_print(cdata->item, "%s", msg);
|
|
|
|
return;
|
|
|
|
}
|
2017-08-30 10:33:57 +00:00
|
|
|
if (cmd_find_from_nothing(&fs, 0) != 0)
|
2016-11-12 19:05:53 +00:00
|
|
|
return;
|
|
|
|
wp = fs.wp;
|
|
|
|
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);
|
2019-03-07 19:34:22 +00:00
|
|
|
window_copy_add(wp, "%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;
|
2020-03-12 13:25:45 +00:00
|
|
|
const char *delay;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
} else if (args->argc == 0)
|
|
|
|
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);
|
2020-03-12 13:25:45 +00:00
|
|
|
if (args->argc != 0)
|
2020-04-13 20:51:57 +00:00
|
|
|
cdata->cmd = format_single_from_target(item, args->argv[0]);
|
2016-10-15 23:06:39 +00:00
|
|
|
|
2021-01-01 08:36:51 +00:00
|
|
|
cdata->shell = !args_has(args, 'C');
|
|
|
|
if (!cdata->shell) {
|
|
|
|
memset(&cdata->pi, 0, sizeof cdata->pi);
|
|
|
|
cmd_get_source(self, &cdata->pi.file, &cdata->pi.line);
|
|
|
|
if (wait)
|
|
|
|
cdata->pi.item = item;
|
|
|
|
cdata->pi.c = tc;
|
|
|
|
cmd_find_copy_state(&cdata->pi.fs, target);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
char *error;
|
|
|
|
struct cmdq_item *item = cdata->item;
|
|
|
|
enum cmd_parse_status status;
|
2020-03-12 13:25:45 +00:00
|
|
|
|
2021-01-01 08:36:51 +00:00
|
|
|
if (cmd != NULL && cdata->shell) {
|
2021-03-02 10:56:45 +00:00
|
|
|
if (job_run(cmd, 0, 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
|
|
|
|
|
|
|
if (cmd != NULL) {
|
|
|
|
if (item != NULL) {
|
|
|
|
status = cmd_parse_and_insert(cmd, &cdata->pi, item,
|
|
|
|
cmdq_get_state(item), &error);
|
|
|
|
} else {
|
|
|
|
status = cmd_parse_and_append(cmd, &cdata->pi, c, NULL,
|
|
|
|
&error);
|
|
|
|
}
|
|
|
|
if (status == CMD_PARSE_ERROR) {
|
2021-03-15 13:06:33 +00:00
|
|
|
if (cdata->item == NULL) {
|
|
|
|
*error = toupper((u_char)*error);
|
2021-04-12 09:36:12 +00:00
|
|
|
status_message_set(c, -1, 1, 0, "%s", error);
|
2021-03-15 13:06:33 +00:00
|
|
|
} else
|
|
|
|
cmdq_error(cdata->item, "%s", error);
|
|
|
|
free(error);
|
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 {
|
2018-08-23 15:45:05 +00:00
|
|
|
if ((line = evbuffer_readline(event->input)) != 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);
|
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
|
|
|
}
|