Annotation of src/usr.bin/tmux/cmd-pipe-pane.c, Revision 1.48
1.48 ! nicm 1: /* $OpenBSD: cmd-pipe-pane.c,v 1.47 2018/01/16 09:00:38 nicm Exp $ */
1.1 nicm 2:
3: /*
1.36 nicm 4: * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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>
1.4 nicm 20: #include <sys/socket.h>
1.1 nicm 21:
22: #include <errno.h>
23: #include <fcntl.h>
24: #include <paths.h>
1.45 nicm 25: #include <signal.h>
1.29 nicm 26: #include <stdlib.h>
1.1 nicm 27: #include <string.h>
1.13 nicm 28: #include <time.h>
1.1 nicm 29: #include <unistd.h>
30:
31: #include "tmux.h"
32:
33: /*
34: * Open pipe to redirect pane output. If already open, close first.
35: */
36:
1.39 nicm 37: static enum cmd_retval cmd_pipe_pane_exec(struct cmd *, struct cmdq_item *);
1.1 nicm 38:
1.47 nicm 39: static void cmd_pipe_pane_read_callback(struct bufferevent *, void *);
1.43 nicm 40: static void cmd_pipe_pane_write_callback(struct bufferevent *, void *);
1.37 nicm 41: static void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
1.4 nicm 42:
1.1 nicm 43: const struct cmd_entry cmd_pipe_pane_entry = {
1.34 nicm 44: .name = "pipe-pane",
45: .alias = "pipep",
46:
1.47 nicm 47: .args = { "IOot:", 0, 1 },
48: .usage = "[-IOo] " CMD_TARGET_PANE_USAGE " [command]",
1.34 nicm 49:
1.41 nicm 50: .target = { 't', CMD_FIND_PANE, 0 },
1.35 nicm 51:
1.38 nicm 52: .flags = CMD_AFTERHOOK,
1.34 nicm 53: .exec = cmd_pipe_pane_exec
1.1 nicm 54: };
55:
1.37 nicm 56: static enum cmd_retval
1.39 nicm 57: cmd_pipe_pane_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 58: {
1.17 nicm 59: struct args *args = self->args;
1.41 nicm 60: struct client *c = cmd_find_client(item, NULL, 1);
61: struct window_pane *wp = item->target.wp;
62: struct session *s = item->target.s;
63: struct winlink *wl = item->target.wl;
1.29 nicm 64: char *cmd;
1.47 nicm 65: int old_fd, pipe_fd[2], null_fd, in, out;
1.29 nicm 66: struct format_tree *ft;
1.45 nicm 67: sigset_t set, oldset;
1.1 nicm 68:
69: /* Destroy the old pipe. */
70: old_fd = wp->pipe_fd;
71: if (wp->pipe_fd != -1) {
1.4 nicm 72: bufferevent_free(wp->pipe_event);
1.1 nicm 73: close(wp->pipe_fd);
74: wp->pipe_fd = -1;
1.43 nicm 75:
76: if (window_pane_destroy_ready(wp)) {
77: server_destroy_pane(wp, 1);
78: return (CMD_RETURN_NORMAL);
79: }
1.1 nicm 80: }
81:
82: /* If no pipe command, that is enough. */
1.17 nicm 83: if (args->argc == 0 || *args->argv[0] == '\0')
1.22 nicm 84: return (CMD_RETURN_NORMAL);
1.1 nicm 85:
86: /*
87: * With -o, only open the new pipe if there was no previous one. This
88: * allows a pipe to be toggled with a single key, for example:
89: *
90: * bind ^p pipep -o 'cat >>~/output'
91: */
1.17 nicm 92: if (args_has(self->args, 'o') && old_fd != -1)
1.22 nicm 93: return (CMD_RETURN_NORMAL);
1.1 nicm 94:
1.47 nicm 95: /* What do we want to do? Neither -I or -O is -O. */
96: if (args_has(self->args, 'I')) {
97: in = 1;
98: out = args_has(self->args, 'O');
99: } else {
100: in = 0;
101: out = 1;
102: }
103:
1.1 nicm 104: /* Open the new pipe. */
1.4 nicm 105: if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
1.39 nicm 106: cmdq_error(item, "socketpair error: %s", strerror(errno));
1.22 nicm 107: return (CMD_RETURN_ERROR);
1.1 nicm 108: }
109:
1.29 nicm 110: /* Expand the command. */
1.42 nicm 111: ft = format_create(item->client, item, FORMAT_NONE, 0);
1.29 nicm 112: format_defaults(ft, c, s, wl, wp);
113: cmd = format_expand_time(ft, args->argv[0], time(NULL));
114: format_free(ft);
115:
1.1 nicm 116: /* Fork the child. */
1.45 nicm 117: sigfillset(&set);
118: sigprocmask(SIG_BLOCK, &set, &oldset);
1.1 nicm 119: switch (fork()) {
120: case -1:
1.45 nicm 121: sigprocmask(SIG_SETMASK, &oldset, NULL);
1.39 nicm 122: cmdq_error(item, "fork error: %s", strerror(errno));
1.29 nicm 123:
124: free(cmd);
1.22 nicm 125: return (CMD_RETURN_ERROR);
1.1 nicm 126: case 0:
127: /* Child process. */
1.46 nicm 128: proc_clear_signals(server_proc, 1);
1.45 nicm 129: sigprocmask(SIG_SETMASK, &oldset, NULL);
1.1 nicm 130: close(pipe_fd[0]);
131:
132: null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
1.47 nicm 133: if (out) {
134: if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
135: _exit(1);
136: } else {
137: if (dup2(null_fd, STDIN_FILENO) == -1)
138: _exit(1);
139: }
140: if (in) {
141: if (dup2(pipe_fd[1], STDOUT_FILENO) == -1)
142: _exit(1);
143: if (pipe_fd[1] != STDOUT_FILENO)
144: close(pipe_fd[1]);
145: } else {
146: if (dup2(null_fd, STDOUT_FILENO) == -1)
147: _exit(1);
148: }
1.1 nicm 149: if (dup2(null_fd, STDERR_FILENO) == -1)
150: _exit(1);
1.16 nicm 151: closefrom(STDERR_FILENO + 1);
152:
1.29 nicm 153: execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL);
1.1 nicm 154: _exit(1);
155: default:
156: /* Parent process. */
1.45 nicm 157: sigprocmask(SIG_SETMASK, &oldset, NULL);
1.1 nicm 158: close(pipe_fd[1]);
159:
160: wp->pipe_fd = pipe_fd[0];
1.5 nicm 161: wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
1.9 nicm 162:
1.47 nicm 163: setblocking(wp->pipe_fd, 0);
164: wp->pipe_event = bufferevent_new(wp->pipe_fd,
165: cmd_pipe_pane_read_callback,
166: cmd_pipe_pane_write_callback,
167: cmd_pipe_pane_error_callback,
1.43 nicm 168: wp);
1.48 ! nicm 169: if (wp->pipe_event == NULL)
! 170: fatalx("out of memory");
1.47 nicm 171: if (out)
172: bufferevent_enable(wp->pipe_event, EV_WRITE);
173: if (in)
174: bufferevent_enable(wp->pipe_event, EV_READ);
1.29 nicm 175:
176: free(cmd);
1.22 nicm 177: return (CMD_RETURN_NORMAL);
1.1 nicm 178: }
1.4 nicm 179: }
180:
1.37 nicm 181: static void
1.47 nicm 182: cmd_pipe_pane_read_callback(__unused struct bufferevent *bufev, void *data)
183: {
184: struct window_pane *wp = data;
185: struct evbuffer *evb = wp->pipe_event->input;
186: size_t available;
187:
188: available = EVBUFFER_LENGTH(evb);
189: log_debug("%%%u pipe read %zu", wp->id, available);
190:
191: bufferevent_write(wp->event, EVBUFFER_DATA(evb), available);
192: evbuffer_drain(evb, available);
193:
194: if (window_pane_destroy_ready(wp))
195: server_destroy_pane(wp, 1);
196: }
197:
198: static void
1.43 nicm 199: cmd_pipe_pane_write_callback(__unused struct bufferevent *bufev, void *data)
200: {
201: struct window_pane *wp = data;
202:
203: log_debug("%%%u pipe empty", wp->id);
1.47 nicm 204:
1.43 nicm 205: if (window_pane_destroy_ready(wp))
206: server_destroy_pane(wp, 1);
207: }
208:
209: static void
1.30 nicm 210: cmd_pipe_pane_error_callback(__unused struct bufferevent *bufev,
211: __unused short what, void *data)
1.4 nicm 212: {
213: struct window_pane *wp = data;
214:
1.43 nicm 215: log_debug("%%%u pipe error", wp->id);
216:
1.4 nicm 217: bufferevent_free(wp->pipe_event);
218: close(wp->pipe_fd);
219: wp->pipe_fd = -1;
1.43 nicm 220:
221: if (window_pane_destroy_ready(wp))
222: server_destroy_pane(wp, 1);
1.1 nicm 223: }