1 /* $OpenBSD: cmd-pipe-pane.c,v 1.22 2012/07/11 07:10:15 nicm Exp $ */ 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 <paths.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_ctx *); 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 NULL, 45 NULL, 46 cmd_pipe_pane_exec 47 }; 48 49 enum cmd_retval 50 cmd_pipe_pane_exec(struct cmd *self, struct cmd_ctx *ctx) 51 { 52 struct args *args = self->args; 53 struct client *c; 54 struct window_pane *wp; 55 char *command; 56 int old_fd, pipe_fd[2], null_fd; 57 58 if (cmd_find_pane(ctx, args_get(args, 't'), NULL, &wp) == NULL) 59 return (CMD_RETURN_ERROR); 60 c = cmd_find_client(ctx, NULL); 61 62 /* Destroy the old pipe. */ 63 old_fd = wp->pipe_fd; 64 if (wp->pipe_fd != -1) { 65 bufferevent_free(wp->pipe_event); 66 close(wp->pipe_fd); 67 wp->pipe_fd = -1; 68 } 69 70 /* If no pipe command, that is enough. */ 71 if (args->argc == 0 || *args->argv[0] == '\0') 72 return (CMD_RETURN_NORMAL); 73 74 /* 75 * With -o, only open the new pipe if there was no previous one. This 76 * allows a pipe to be toggled with a single key, for example: 77 * 78 * bind ^p pipep -o 'cat >>~/output' 79 */ 80 if (args_has(self->args, 'o') && old_fd != -1) 81 return (CMD_RETURN_NORMAL); 82 83 /* Open the new pipe. */ 84 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) { 85 ctx->error(ctx, "socketpair error: %s", strerror(errno)); 86 return (CMD_RETURN_ERROR); 87 } 88 89 /* Fork the child. */ 90 switch (fork()) { 91 case -1: 92 ctx->error(ctx, "fork error: %s", strerror(errno)); 93 return (CMD_RETURN_ERROR); 94 case 0: 95 /* Child process. */ 96 close(pipe_fd[0]); 97 clear_signals(1); 98 99 if (dup2(pipe_fd[1], STDIN_FILENO) == -1) 100 _exit(1); 101 if (pipe_fd[1] != STDIN_FILENO) 102 close(pipe_fd[1]); 103 104 null_fd = open(_PATH_DEVNULL, O_WRONLY, 0); 105 if (dup2(null_fd, STDOUT_FILENO) == -1) 106 _exit(1); 107 if (dup2(null_fd, STDERR_FILENO) == -1) 108 _exit(1); 109 if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO) 110 close(null_fd); 111 112 closefrom(STDERR_FILENO + 1); 113 114 command = status_replace( 115 c, NULL, NULL, NULL, args->argv[0], time(NULL), 0); 116 execl(_PATH_BSHELL, "sh", "-c", command, (char *) NULL); 117 _exit(1); 118 default: 119 /* Parent process. */ 120 close(pipe_fd[1]); 121 122 wp->pipe_fd = pipe_fd[0]; 123 wp->pipe_off = EVBUFFER_LENGTH(wp->event->input); 124 125 wp->pipe_event = bufferevent_new(wp->pipe_fd, 126 NULL, NULL, cmd_pipe_pane_error_callback, wp); 127 bufferevent_enable(wp->pipe_event, EV_WRITE); 128 129 setblocking(wp->pipe_fd, 0); 130 return (CMD_RETURN_NORMAL); 131 } 132 } 133 134 /* ARGSUSED */ 135 void 136 cmd_pipe_pane_error_callback( 137 unused struct bufferevent *bufev, unused short what, void *data) 138 { 139 struct window_pane *wp = data; 140 141 bufferevent_free(wp->pipe_event); 142 close(wp->pipe_fd); 143 wp->pipe_fd = -1; 144 } 145