1 /* $OpenBSD$ */ 2 3 /* 4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 #include <sys/socket.h> 21 22 #include <errno.h> 23 #include <fcntl.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <time.h> 27 #include <unistd.h> 28 29 #include "tmux.h" 30 31 /* 32 * Open pipe to redirect pane output. If already open, close first. 33 */ 34 35 enum cmd_retval cmd_pipe_pane_exec(struct cmd *, struct cmd_q *); 36 37 void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *); 38 39 const struct cmd_entry cmd_pipe_pane_entry = { 40 "pipe-pane", "pipep", 41 "ot:", 0, 1, 42 "[-o] " CMD_TARGET_PANE_USAGE " [command]", 43 0, 44 cmd_pipe_pane_exec 45 }; 46 47 enum cmd_retval 48 cmd_pipe_pane_exec(struct cmd *self, struct cmd_q *cmdq) 49 { 50 struct args *args = self->args; 51 struct client *c; 52 struct session *s; 53 struct winlink *wl; 54 struct window_pane *wp; 55 char *cmd; 56 int old_fd, pipe_fd[2], null_fd; 57 struct format_tree *ft; 58 59 if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp)) == NULL) 60 return (CMD_RETURN_ERROR); 61 c = cmd_find_client(cmdq, NULL, 1); 62 63 /* Destroy the old pipe. */ 64 old_fd = wp->pipe_fd; 65 if (wp->pipe_fd != -1) { 66 bufferevent_free(wp->pipe_event); 67 close(wp->pipe_fd); 68 wp->pipe_fd = -1; 69 } 70 71 /* If no pipe command, that is enough. */ 72 if (args->argc == 0 || *args->argv[0] == '\0') 73 return (CMD_RETURN_NORMAL); 74 75 /* 76 * With -o, only open the new pipe if there was no previous one. This 77 * allows a pipe to be toggled with a single key, for example: 78 * 79 * bind ^p pipep -o 'cat >>~/output' 80 */ 81 if (args_has(self->args, 'o') && old_fd != -1) 82 return (CMD_RETURN_NORMAL); 83 84 /* Open the new pipe. */ 85 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) { 86 cmdq_error(cmdq, "socketpair error: %s", strerror(errno)); 87 return (CMD_RETURN_ERROR); 88 } 89 90 /* Expand the command. */ 91 ft = format_create(); 92 format_defaults(ft, c, s, wl, wp); 93 cmd = format_expand_time(ft, args->argv[0], time(NULL)); 94 format_free(ft); 95 96 /* Fork the child. */ 97 switch (fork()) { 98 case -1: 99 cmdq_error(cmdq, "fork error: %s", strerror(errno)); 100 101 free(cmd); 102 return (CMD_RETURN_ERROR); 103 case 0: 104 /* Child process. */ 105 close(pipe_fd[0]); 106 clear_signals(1); 107 108 if (dup2(pipe_fd[1], STDIN_FILENO) == -1) 109 _exit(1); 110 if (pipe_fd[1] != STDIN_FILENO) 111 close(pipe_fd[1]); 112 113 null_fd = open(_PATH_DEVNULL, O_WRONLY, 0); 114 if (dup2(null_fd, STDOUT_FILENO) == -1) 115 _exit(1); 116 if (dup2(null_fd, STDERR_FILENO) == -1) 117 _exit(1); 118 if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO) 119 close(null_fd); 120 121 closefrom(STDERR_FILENO + 1); 122 123 execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL); 124 _exit(1); 125 default: 126 /* Parent process. */ 127 close(pipe_fd[1]); 128 129 wp->pipe_fd = pipe_fd[0]; 130 wp->pipe_off = EVBUFFER_LENGTH(wp->event->input); 131 132 wp->pipe_event = bufferevent_new(wp->pipe_fd, 133 NULL, NULL, cmd_pipe_pane_error_callback, wp); 134 bufferevent_enable(wp->pipe_event, EV_WRITE); 135 136 setblocking(wp->pipe_fd, 0); 137 138 free(cmd); 139 return (CMD_RETURN_NORMAL); 140 } 141 } 142 143 void 144 cmd_pipe_pane_error_callback( 145 unused struct bufferevent *bufev, unused short what, void *data) 146 { 147 struct window_pane *wp = data; 148 149 bufferevent_free(wp->pipe_event); 150 close(wp->pipe_fd); 151 wp->pipe_fd = -1; 152 } 153