2009-10-11 10:04:27 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
enum cmd_retval cmd_pipe_pane_exec(struct cmd *, struct cmd_q *);
|
2009-10-11 10:04:27 +00:00
|
|
|
|
2009-11-04 22:02:38 +00:00
|
|
|
void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
|
|
|
|
|
2009-10-11 10:04:27 +00:00
|
|
|
const struct cmd_entry cmd_pipe_pane_entry = {
|
|
|
|
"pipe-pane", "pipep",
|
2011-01-04 00:42:46 +00:00
|
|
|
"ot:", 0, 1,
|
2011-10-27 22:41:03 +00:00
|
|
|
"[-o] " CMD_TARGET_PANE_USAGE " [command]",
|
2011-01-04 00:42:46 +00:00
|
|
|
0,
|
|
|
|
cmd_pipe_pane_exec
|
2009-10-11 10:04:27 +00:00
|
|
|
};
|
|
|
|
|
2012-07-11 07:10:15 +00:00
|
|
|
enum cmd_retval
|
2013-03-24 09:54:10 +00:00
|
|
|
cmd_pipe_pane_exec(struct cmd *self, struct cmd_q *cmdq)
|
2009-10-11 10:04:27 +00:00
|
|
|
{
|
2011-01-04 00:42:46 +00:00
|
|
|
struct args *args = self->args;
|
2010-06-05 16:34:30 +00:00
|
|
|
struct client *c;
|
2015-02-06 17:11:39 +00:00
|
|
|
struct session *s;
|
|
|
|
struct winlink *wl;
|
2009-10-11 10:04:27 +00:00
|
|
|
struct window_pane *wp;
|
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
|
|
|
|
2015-02-06 17:11:39 +00:00
|
|
|
if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp)) == NULL)
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
2013-03-24 09:54:10 +00:00
|
|
|
c = cmd_find_client(cmdq, NULL, 1);
|
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) {
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_error(cmdq, "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. */
|
2015-12-11 12:27:36 +00:00
|
|
|
ft = format_create(cmdq, 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:
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_error(cmdq, "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
|
|
|
|
|
|
|
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;
|
|
|
|
}
|