xref: /openbsd-src/usr.bin/tmux/cmd-pipe-pane.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /* $OpenBSD: cmd-pipe-pane.c,v 1.51 2019/03/14 23:14:27 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
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 <signal.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
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 
37 static enum cmd_retval	cmd_pipe_pane_exec(struct cmd *, struct cmdq_item *);
38 
39 static void cmd_pipe_pane_read_callback(struct bufferevent *, void *);
40 static void cmd_pipe_pane_write_callback(struct bufferevent *, void *);
41 static void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
42 
43 const struct cmd_entry cmd_pipe_pane_entry = {
44 	.name = "pipe-pane",
45 	.alias = "pipep",
46 
47 	.args = { "IOot:", 0, 1 },
48 	.usage = "[-IOo] " CMD_TARGET_PANE_USAGE " [command]",
49 
50 	.target = { 't', CMD_FIND_PANE, 0 },
51 
52 	.flags = CMD_AFTERHOOK,
53 	.exec = cmd_pipe_pane_exec
54 };
55 
56 static enum cmd_retval
57 cmd_pipe_pane_exec(struct cmd *self, struct cmdq_item *item)
58 {
59 	struct args		*args = self->args;
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;
64 	char			*cmd;
65 	int			 old_fd, pipe_fd[2], null_fd, in, out;
66 	struct format_tree	*ft;
67 	sigset_t		 set, oldset;
68 
69 	/* Destroy the old pipe. */
70 	old_fd = wp->pipe_fd;
71 	if (wp->pipe_fd != -1) {
72 		bufferevent_free(wp->pipe_event);
73 		close(wp->pipe_fd);
74 		wp->pipe_fd = -1;
75 
76 		if (window_pane_destroy_ready(wp)) {
77 			server_destroy_pane(wp, 1);
78 			return (CMD_RETURN_NORMAL);
79 		}
80 	}
81 
82 	/* If no pipe command, that is enough. */
83 	if (args->argc == 0 || *args->argv[0] == '\0')
84 		return (CMD_RETURN_NORMAL);
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 	 */
92 	if (args_has(self->args, 'o') && old_fd != -1)
93 		return (CMD_RETURN_NORMAL);
94 
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 
104 	/* Open the new pipe. */
105 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
106 		cmdq_error(item, "socketpair error: %s", strerror(errno));
107 		return (CMD_RETURN_ERROR);
108 	}
109 
110 	/* Expand the command. */
111 	ft = format_create(item->client, item, FORMAT_NONE, 0);
112 	format_defaults(ft, c, s, wl, wp);
113 	cmd = format_expand_time(ft, args->argv[0]);
114 	format_free(ft);
115 
116 	/* Fork the child. */
117 	sigfillset(&set);
118 	sigprocmask(SIG_BLOCK, &set, &oldset);
119 	switch (fork()) {
120 	case -1:
121 		sigprocmask(SIG_SETMASK, &oldset, NULL);
122 		cmdq_error(item, "fork error: %s", strerror(errno));
123 
124 		free(cmd);
125 		return (CMD_RETURN_ERROR);
126 	case 0:
127 		/* Child process. */
128 		proc_clear_signals(server_proc, 1);
129 		sigprocmask(SIG_SETMASK, &oldset, NULL);
130 		close(pipe_fd[0]);
131 
132 		null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
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 		}
149 		if (dup2(null_fd, STDERR_FILENO) == -1)
150 			_exit(1);
151 		closefrom(STDERR_FILENO + 1);
152 
153 		execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL);
154 		_exit(1);
155 	default:
156 		/* Parent process. */
157 		sigprocmask(SIG_SETMASK, &oldset, NULL);
158 		close(pipe_fd[1]);
159 
160 		wp->pipe_fd = pipe_fd[0];
161 		if (wp->fd != -1)
162 			wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
163 		else
164 			wp->pipe_off = 0;
165 
166 		setblocking(wp->pipe_fd, 0);
167 		wp->pipe_event = bufferevent_new(wp->pipe_fd,
168 		    cmd_pipe_pane_read_callback,
169 		    cmd_pipe_pane_write_callback,
170 		    cmd_pipe_pane_error_callback,
171 		    wp);
172 		if (wp->pipe_event == NULL)
173 			fatalx("out of memory");
174 		if (out)
175 			bufferevent_enable(wp->pipe_event, EV_WRITE);
176 		if (in)
177 			bufferevent_enable(wp->pipe_event, EV_READ);
178 
179 		free(cmd);
180 		return (CMD_RETURN_NORMAL);
181 	}
182 }
183 
184 static void
185 cmd_pipe_pane_read_callback(__unused struct bufferevent *bufev, void *data)
186 {
187 	struct window_pane	*wp = data;
188 	struct evbuffer		*evb = wp->pipe_event->input;
189 	size_t			 available;
190 
191 	available = EVBUFFER_LENGTH(evb);
192 	log_debug("%%%u pipe read %zu", wp->id, available);
193 
194 	bufferevent_write(wp->event, EVBUFFER_DATA(evb), available);
195 	evbuffer_drain(evb, available);
196 
197 	if (window_pane_destroy_ready(wp))
198 		server_destroy_pane(wp, 1);
199 }
200 
201 static void
202 cmd_pipe_pane_write_callback(__unused struct bufferevent *bufev, void *data)
203 {
204 	struct window_pane	*wp = data;
205 
206 	log_debug("%%%u pipe empty", wp->id);
207 
208 	if (window_pane_destroy_ready(wp))
209 		server_destroy_pane(wp, 1);
210 }
211 
212 static void
213 cmd_pipe_pane_error_callback(__unused struct bufferevent *bufev,
214     __unused short what, void *data)
215 {
216 	struct window_pane	*wp = data;
217 
218 	log_debug("%%%u pipe error", wp->id);
219 
220 	bufferevent_free(wp->pipe_event);
221 	close(wp->pipe_fd);
222 	wp->pipe_fd = -1;
223 
224 	if (window_pane_destroy_ready(wp))
225 		server_destroy_pane(wp, 1);
226 }
227