2009-10-11 10:04:27 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-10-11 10:04:27 +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>
|
2009-11-04 22:02:38 +00:00
|
|
|
#include <sys/socket.h>
|
2009-10-11 10:04:27 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2017-07-12 10:04:51 +00:00
|
|
|
#include <signal.h>
|
2015-02-06 17:11:39 +00:00
|
|
|
#include <stdlib.h>
|
2009-10-11 10:04:27 +00:00
|
|
|
#include <string.h>
|
2010-06-05 16:34:30 +00:00
|
|
|
#include <time.h>
|
2009-10-11 10:04:27 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open pipe to redirect pane output. If already open, close first.
|
|
|
|
*/
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_pipe_pane_exec(struct cmd *, struct cmdq_item *);
|
2009-10-11 10:04:27 +00:00
|
|
|
|
2018-01-16 09:00:38 +00:00
|
|
|
static void cmd_pipe_pane_read_callback(struct bufferevent *, void *);
|
2017-07-03 08:16:03 +00:00
|
|
|
static void cmd_pipe_pane_write_callback(struct bufferevent *, void *);
|
2016-10-10 21:51:39 +00:00
|
|
|
static void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
|
2009-11-04 22:02:38 +00:00
|
|
|
|
2009-10-11 10:04:27 +00:00
|
|
|
const struct cmd_entry cmd_pipe_pane_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "pipe-pane",
|
|
|
|
.alias = "pipep",
|
|
|
|
|
2021-08-21 10:22:38 +00:00
|
|
|
.args = { "IOot:", 0, 1, NULL },
|
2021-08-17 19:37:55 +00:00
|
|
|
.usage = "[-IOo] " 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, 0 },
|
2015-12-14 00:31:54 +00:00
|
|
|
|
2016-10-14 22:14:22 +00:00
|
|
|
.flags = CMD_AFTERHOOK,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_pipe_pane_exec
|
2009-10-11 10:04:27 +00:00
|
|
|
};
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_pipe_pane_exec(struct cmd *self, struct cmdq_item *item)
|
2009-10-11 10:04:27 +00:00
|
|
|
{
|
2020-05-21 07:24:13 +00:00
|
|
|
struct args *args = cmd_get_args(self);
|
|
|
|
struct cmd_find_state *target = cmdq_get_target(item);
|
|
|
|
struct client *tc = cmdq_get_target_client(item);
|
|
|
|
struct window_pane *wp = target->wp;
|
|
|
|
struct session *s = target->s;
|
|
|
|
struct winlink *wl = target->wl;
|
|
|
|
struct window_pane_offset *wpo = &wp->pipe_offset;
|
|
|
|
char *cmd;
|
|
|
|
int old_fd, pipe_fd[2], null_fd, in, out;
|
|
|
|
struct format_tree *ft;
|
|
|
|
sigset_t set, oldset;
|
2009-10-11 10:04:27 +00:00
|
|
|
|
|
|
|
/* Destroy the old pipe. */
|
|
|
|
old_fd = wp->pipe_fd;
|
|
|
|
if (wp->pipe_fd != -1) {
|
2009-11-04 22:02:38 +00:00
|
|
|
bufferevent_free(wp->pipe_event);
|
2009-10-11 10:04:27 +00:00
|
|
|
close(wp->pipe_fd);
|
|
|
|
wp->pipe_fd = -1;
|
2017-07-03 08:16:03 +00:00
|
|
|
|
|
|
|
if (window_pane_destroy_ready(wp)) {
|
|
|
|
server_destroy_pane(wp, 1);
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
2009-10-11 10:04:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If no pipe command, that is enough. */
|
2021-08-20 19:50:16 +00:00
|
|
|
if (args_count(args) == 0 || *args_string(args, 0) == '\0')
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2009-10-11 10:04:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* With -o, only open the new pipe if there was no previous one. This
|
|
|
|
* allows a pipe to be toggled with a single key, for example:
|
|
|
|
*
|
|
|
|
* bind ^p pipep -o 'cat >>~/output'
|
|
|
|
*/
|
2020-04-13 08:26:27 +00:00
|
|
|
if (args_has(args, 'o') && old_fd != -1)
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2009-10-11 10:04:27 +00:00
|
|
|
|
2018-01-16 09:00:38 +00:00
|
|
|
/* What do we want to do? Neither -I or -O is -O. */
|
2020-04-13 08:26:27 +00:00
|
|
|
if (args_has(args, 'I')) {
|
2018-01-16 09:00:38 +00:00
|
|
|
in = 1;
|
2020-04-13 08:26:27 +00:00
|
|
|
out = args_has(args, 'O');
|
2018-01-16 09:00:38 +00:00
|
|
|
} else {
|
|
|
|
in = 0;
|
|
|
|
out = 1;
|
|
|
|
}
|
|
|
|
|
2009-10-11 10:04:27 +00:00
|
|
|
/* Open the new pipe. */
|
2009-11-04 22:02:38 +00:00
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "socketpair error: %s", strerror(errno));
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
2009-10-11 10:04:27 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 17:11:39 +00:00
|
|
|
/* Expand the command. */
|
2020-04-13 10:59:58 +00:00
|
|
|
ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
|
2020-04-13 20:51:57 +00:00
|
|
|
format_defaults(ft, tc, s, wl, wp);
|
2021-08-20 19:50:16 +00:00
|
|
|
cmd = format_expand_time(ft, args_string(args, 0));
|
2015-02-06 17:11:39 +00:00
|
|
|
format_free(ft);
|
|
|
|
|
2009-10-11 10:04:27 +00:00
|
|
|
/* Fork the child. */
|
2017-07-12 10:04:51 +00:00
|
|
|
sigfillset(&set);
|
|
|
|
sigprocmask(SIG_BLOCK, &set, &oldset);
|
2009-10-11 10:04:27 +00:00
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
2017-07-12 10:04:51 +00:00
|
|
|
sigprocmask(SIG_SETMASK, &oldset, NULL);
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "fork error: %s", strerror(errno));
|
2015-02-06 17:11:39 +00:00
|
|
|
|
|
|
|
free(cmd);
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
2009-10-11 10:04:27 +00:00
|
|
|
case 0:
|
|
|
|
/* Child process. */
|
2017-07-14 18:49:07 +00:00
|
|
|
proc_clear_signals(server_proc, 1);
|
2017-07-12 10:04:51 +00:00
|
|
|
sigprocmask(SIG_SETMASK, &oldset, NULL);
|
2009-10-11 10:04:27 +00:00
|
|
|
close(pipe_fd[0]);
|
|
|
|
|
For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert
2021-10-24 21:24:17 +00:00
|
|
|
null_fd = open(_PATH_DEVNULL, O_WRONLY);
|
2018-01-16 09:00:38 +00:00
|
|
|
if (out) {
|
|
|
|
if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
|
|
|
|
_exit(1);
|
|
|
|
} else {
|
|
|
|
if (dup2(null_fd, STDIN_FILENO) == -1)
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
if (in) {
|
|
|
|
if (dup2(pipe_fd[1], STDOUT_FILENO) == -1)
|
|
|
|
_exit(1);
|
|
|
|
if (pipe_fd[1] != STDOUT_FILENO)
|
|
|
|
close(pipe_fd[1]);
|
|
|
|
} else {
|
|
|
|
if (dup2(null_fd, STDOUT_FILENO) == -1)
|
|
|
|
_exit(1);
|
|
|
|
}
|
2009-10-11 10:04:27 +00:00
|
|
|
if (dup2(null_fd, STDERR_FILENO) == -1)
|
|
|
|
_exit(1);
|
2010-10-16 08:31:55 +00:00
|
|
|
closefrom(STDERR_FILENO + 1);
|
|
|
|
|
2015-02-06 17:11:39 +00:00
|
|
|
execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL);
|
2009-10-11 10:04:27 +00:00
|
|
|
_exit(1);
|
|
|
|
default:
|
|
|
|
/* Parent process. */
|
2017-07-12 10:04:51 +00:00
|
|
|
sigprocmask(SIG_SETMASK, &oldset, NULL);
|
2009-10-11 10:04:27 +00:00
|
|
|
close(pipe_fd[1]);
|
|
|
|
|
|
|
|
wp->pipe_fd = pipe_fd[0];
|
2020-05-21 07:24:13 +00:00
|
|
|
memcpy(wpo, &wp->offset, sizeof *wpo);
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2011-01-08 01:52:36 +00:00
|
|
|
setblocking(wp->pipe_fd, 0);
|
2018-01-16 09:00:38 +00:00
|
|
|
wp->pipe_event = bufferevent_new(wp->pipe_fd,
|
|
|
|
cmd_pipe_pane_read_callback,
|
|
|
|
cmd_pipe_pane_write_callback,
|
|
|
|
cmd_pipe_pane_error_callback,
|
|
|
|
wp);
|
2018-11-19 13:35:40 +00:00
|
|
|
if (wp->pipe_event == NULL)
|
|
|
|
fatalx("out of memory");
|
2018-01-16 09:00:38 +00:00
|
|
|
if (out)
|
|
|
|
bufferevent_enable(wp->pipe_event, EV_WRITE);
|
|
|
|
if (in)
|
|
|
|
bufferevent_enable(wp->pipe_event, EV_READ);
|
2015-02-06 17:11:39 +00:00
|
|
|
|
|
|
|
free(cmd);
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2009-10-11 10:04:27 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-04 22:02:38 +00:00
|
|
|
|
2018-01-16 09:00:38 +00:00
|
|
|
static void
|
|
|
|
cmd_pipe_pane_read_callback(__unused struct bufferevent *bufev, void *data)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = data;
|
|
|
|
struct evbuffer *evb = wp->pipe_event->input;
|
|
|
|
size_t available;
|
|
|
|
|
|
|
|
available = EVBUFFER_LENGTH(evb);
|
|
|
|
log_debug("%%%u pipe read %zu", wp->id, available);
|
|
|
|
|
|
|
|
bufferevent_write(wp->event, EVBUFFER_DATA(evb), available);
|
|
|
|
evbuffer_drain(evb, available);
|
|
|
|
|
|
|
|
if (window_pane_destroy_ready(wp))
|
|
|
|
server_destroy_pane(wp, 1);
|
|
|
|
}
|
|
|
|
|
2017-07-03 08:16:03 +00:00
|
|
|
static void
|
|
|
|
cmd_pipe_pane_write_callback(__unused struct bufferevent *bufev, void *data)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = data;
|
|
|
|
|
|
|
|
log_debug("%%%u pipe empty", wp->id);
|
2018-01-16 09:00:38 +00:00
|
|
|
|
2017-07-03 08:16:03 +00:00
|
|
|
if (window_pane_destroy_ready(wp))
|
|
|
|
server_destroy_pane(wp, 1);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static void
|
2015-11-18 14:27:44 +00:00
|
|
|
cmd_pipe_pane_error_callback(__unused struct bufferevent *bufev,
|
|
|
|
__unused short what, void *data)
|
2009-11-04 22:02:38 +00:00
|
|
|
{
|
|
|
|
struct window_pane *wp = data;
|
|
|
|
|
2017-07-03 08:16:03 +00:00
|
|
|
log_debug("%%%u pipe error", wp->id);
|
|
|
|
|
2009-11-04 22:02:38 +00:00
|
|
|
bufferevent_free(wp->pipe_event);
|
|
|
|
close(wp->pipe_fd);
|
|
|
|
wp->pipe_fd = -1;
|
2017-07-03 08:16:03 +00:00
|
|
|
|
|
|
|
if (window_pane_destroy_ready(wp))
|
|
|
|
server_destroy_pane(wp, 1);
|
2009-11-04 22:02:38 +00:00
|
|
|
}
|