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>
|
|
|
|
#include <paths.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
|
|
|
|
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",
|
|
|
|
|
|
|
|
.args = { "ot:", 0, 1 },
|
|
|
|
.usage = "[-o] " CMD_TARGET_PANE_USAGE " [command]",
|
|
|
|
|
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
|
|
|
{
|
2011-01-04 00:42:46 +00:00
|
|
|
struct args *args = self->args;
|
2017-04-22 10:22:39 +00:00
|
|
|
struct client *c = cmd_find_client(item, NULL, 1);
|
|
|
|
struct window_pane *wp = item->target.wp;
|
|
|
|
struct session *s = item->target.s;
|
|
|
|
struct winlink *wl = item->target.wl;
|
2015-02-06 17:11:39 +00:00
|
|
|
char *cmd;
|
2011-01-08 01:52:36 +00:00
|
|
|
int old_fd, pipe_fd[2], null_fd;
|
2015-02-06 17:11:39 +00:00
|
|
|
struct format_tree *ft;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no pipe command, that is enough. */
|
2011-01-04 00:42:46 +00:00
|
|
|
if (args->argc == 0 || *args->argv[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'
|
|
|
|
*/
|
2011-01-04 00:42:46 +00:00
|
|
|
if (args_has(self->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
|
|
|
|
|
|
|
/* 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. */
|
2017-02-03 11:57:27 +00:00
|
|
|
ft = format_create(item, FORMAT_NONE, 0);
|
2015-02-06 17:11:39 +00:00
|
|
|
format_defaults(ft, c, s, wl, wp);
|
|
|
|
cmd = format_expand_time(ft, args->argv[0], time(NULL));
|
|
|
|
format_free(ft);
|
|
|
|
|
2009-10-11 10:04:27 +00:00
|
|
|
/* Fork the child. */
|
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
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. */
|
|
|
|
close(pipe_fd[0]);
|
2010-08-19 18:29:01 +00:00
|
|
|
clear_signals(1);
|
2009-10-11 10:04:27 +00:00
|
|
|
|
|
|
|
if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
|
|
|
|
_exit(1);
|
|
|
|
if (pipe_fd[1] != STDIN_FILENO)
|
|
|
|
close(pipe_fd[1]);
|
|
|
|
|
|
|
|
null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
|
|
|
|
if (dup2(null_fd, STDOUT_FILENO) == -1)
|
|
|
|
_exit(1);
|
|
|
|
if (dup2(null_fd, STDERR_FILENO) == -1)
|
|
|
|
_exit(1);
|
|
|
|
if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO)
|
|
|
|
close(null_fd);
|
|
|
|
|
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. */
|
|
|
|
close(pipe_fd[1]);
|
|
|
|
|
|
|
|
wp->pipe_fd = pipe_fd[0];
|
2009-11-04 22:43:11 +00:00
|
|
|
wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2009-11-04 22:02:38 +00:00
|
|
|
wp->pipe_event = bufferevent_new(wp->pipe_fd,
|
|
|
|
NULL, NULL, cmd_pipe_pane_error_callback, wp);
|
|
|
|
bufferevent_enable(wp->pipe_event, EV_WRITE);
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2011-01-08 01:52:36 +00:00
|
|
|
setblocking(wp->pipe_fd, 0);
|
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
|
|
|
|
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;
|
|
|
|
|
|
|
|
bufferevent_free(wp->pipe_event);
|
|
|
|
close(wp->pipe_fd);
|
|
|
|
wp->pipe_fd = -1;
|
|
|
|
}
|