1*d39fdc49Snicm /* $OpenBSD: cmd-pipe-pane.c,v 1.61 2024/02/13 08:03:50 nicm Exp $ */
2095994acSnicm
3095994acSnicm /*
498ca8272Snicm * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
5095994acSnicm *
6095994acSnicm * Permission to use, copy, modify, and distribute this software for any
7095994acSnicm * purpose with or without fee is hereby granted, provided that the above
8095994acSnicm * copyright notice and this permission notice appear in all copies.
9095994acSnicm *
10095994acSnicm * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11095994acSnicm * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12095994acSnicm * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13095994acSnicm * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14095994acSnicm * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15095994acSnicm * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16095994acSnicm * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17095994acSnicm */
18095994acSnicm
19095994acSnicm #include <sys/types.h>
20e1e559afSnicm #include <sys/socket.h>
21095994acSnicm
22095994acSnicm #include <errno.h>
23095994acSnicm #include <fcntl.h>
24095994acSnicm #include <paths.h>
25189d1393Snicm #include <signal.h>
26fb0b819bSnicm #include <stdlib.h>
27095994acSnicm #include <string.h>
28ac788fa3Snicm #include <time.h>
29095994acSnicm #include <unistd.h>
30095994acSnicm
31095994acSnicm #include "tmux.h"
32095994acSnicm
33095994acSnicm /*
34095994acSnicm * Open pipe to redirect pane output. If already open, close first.
35095994acSnicm */
36095994acSnicm
3768e0a7f2Snicm static enum cmd_retval cmd_pipe_pane_exec(struct cmd *, struct cmdq_item *);
38095994acSnicm
39c10e3970Snicm static void cmd_pipe_pane_read_callback(struct bufferevent *, void *);
40f35e5f52Snicm static void cmd_pipe_pane_write_callback(struct bufferevent *, void *);
41dc1f0f5fSnicm static void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
42e1e559afSnicm
43095994acSnicm const struct cmd_entry cmd_pipe_pane_entry = {
44c057646bSnicm .name = "pipe-pane",
45c057646bSnicm .alias = "pipep",
46c057646bSnicm
47a51dead1Snicm .args = { "IOot:", 0, 1, NULL },
48bf890124Snicm .usage = "[-IOo] " CMD_TARGET_PANE_USAGE " [shell-command]",
49c057646bSnicm
50bf0d297eSnicm .target = { 't', CMD_FIND_PANE, 0 },
518d471e80Snicm
527a61a8ddSnicm .flags = CMD_AFTERHOOK,
53c057646bSnicm .exec = cmd_pipe_pane_exec
54095994acSnicm };
55095994acSnicm
56dc1f0f5fSnicm static enum cmd_retval
cmd_pipe_pane_exec(struct cmd * self,struct cmdq_item * item)5768e0a7f2Snicm cmd_pipe_pane_exec(struct cmd *self, struct cmdq_item *item)
58095994acSnicm {
5990d7ba38Snicm struct args *args = cmd_get_args(self);
60040343aeSnicm struct cmd_find_state *target = cmdq_get_target(item);
61035dc73dSnicm struct client *tc = cmdq_get_target_client(item);
62040343aeSnicm struct window_pane *wp = target->wp;
63040343aeSnicm struct session *s = target->s;
64040343aeSnicm struct winlink *wl = target->wl;
652920028dSnicm struct window_pane_offset *wpo = &wp->pipe_offset;
66fb0b819bSnicm char *cmd;
67c10e3970Snicm int old_fd, pipe_fd[2], null_fd, in, out;
68fb0b819bSnicm struct format_tree *ft;
69189d1393Snicm sigset_t set, oldset;
70095994acSnicm
710e69fe99Snicm /* Do nothing if pane is dead. */
72*d39fdc49Snicm if (window_pane_exited(wp)) {
730e69fe99Snicm cmdq_error(item, "target pane has exited");
740e69fe99Snicm return (CMD_RETURN_ERROR);
750e69fe99Snicm }
760e69fe99Snicm
77095994acSnicm /* Destroy the old pipe. */
78095994acSnicm old_fd = wp->pipe_fd;
79095994acSnicm if (wp->pipe_fd != -1) {
80e1e559afSnicm bufferevent_free(wp->pipe_event);
81095994acSnicm close(wp->pipe_fd);
82095994acSnicm wp->pipe_fd = -1;
83f35e5f52Snicm
84f35e5f52Snicm if (window_pane_destroy_ready(wp)) {
85f35e5f52Snicm server_destroy_pane(wp, 1);
86f35e5f52Snicm return (CMD_RETURN_NORMAL);
87f35e5f52Snicm }
88095994acSnicm }
89095994acSnicm
90095994acSnicm /* If no pipe command, that is enough. */
911693b10bSnicm if (args_count(args) == 0 || *args_string(args, 0) == '\0')
92a224d0d3Snicm return (CMD_RETURN_NORMAL);
93095994acSnicm
94095994acSnicm /*
95095994acSnicm * With -o, only open the new pipe if there was no previous one. This
96095994acSnicm * allows a pipe to be toggled with a single key, for example:
97095994acSnicm *
98095994acSnicm * bind ^p pipep -o 'cat >>~/output'
99095994acSnicm */
10090d7ba38Snicm if (args_has(args, 'o') && old_fd != -1)
101a224d0d3Snicm return (CMD_RETURN_NORMAL);
102095994acSnicm
103c10e3970Snicm /* What do we want to do? Neither -I or -O is -O. */
10490d7ba38Snicm if (args_has(args, 'I')) {
105c10e3970Snicm in = 1;
10690d7ba38Snicm out = args_has(args, 'O');
107c10e3970Snicm } else {
108c10e3970Snicm in = 0;
109c10e3970Snicm out = 1;
110c10e3970Snicm }
111c10e3970Snicm
112095994acSnicm /* Open the new pipe. */
113e1e559afSnicm if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
11468e0a7f2Snicm cmdq_error(item, "socketpair error: %s", strerror(errno));
115a224d0d3Snicm return (CMD_RETURN_ERROR);
116095994acSnicm }
117095994acSnicm
118fb0b819bSnicm /* Expand the command. */
119040343aeSnicm ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
120035dc73dSnicm format_defaults(ft, tc, s, wl, wp);
1211693b10bSnicm cmd = format_expand_time(ft, args_string(args, 0));
122fb0b819bSnicm format_free(ft);
123fb0b819bSnicm
124095994acSnicm /* Fork the child. */
125189d1393Snicm sigfillset(&set);
126189d1393Snicm sigprocmask(SIG_BLOCK, &set, &oldset);
127095994acSnicm switch (fork()) {
128095994acSnicm case -1:
129189d1393Snicm sigprocmask(SIG_SETMASK, &oldset, NULL);
13068e0a7f2Snicm cmdq_error(item, "fork error: %s", strerror(errno));
131fb0b819bSnicm
132fb0b819bSnicm free(cmd);
133a224d0d3Snicm return (CMD_RETURN_ERROR);
134095994acSnicm case 0:
135095994acSnicm /* Child process. */
13689b52a5bSnicm proc_clear_signals(server_proc, 1);
137189d1393Snicm sigprocmask(SIG_SETMASK, &oldset, NULL);
138189d1393Snicm close(pipe_fd[0]);
139095994acSnicm
140b7041c07Sderaadt null_fd = open(_PATH_DEVNULL, O_WRONLY);
141c10e3970Snicm if (out) {
142095994acSnicm if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
143095994acSnicm _exit(1);
144c10e3970Snicm } else {
145c10e3970Snicm if (dup2(null_fd, STDIN_FILENO) == -1)
146c10e3970Snicm _exit(1);
147c10e3970Snicm }
148c10e3970Snicm if (in) {
149c10e3970Snicm if (dup2(pipe_fd[1], STDOUT_FILENO) == -1)
150c10e3970Snicm _exit(1);
151c10e3970Snicm if (pipe_fd[1] != STDOUT_FILENO)
152095994acSnicm close(pipe_fd[1]);
153c10e3970Snicm } else {
154095994acSnicm if (dup2(null_fd, STDOUT_FILENO) == -1)
155095994acSnicm _exit(1);
156c10e3970Snicm }
157095994acSnicm if (dup2(null_fd, STDERR_FILENO) == -1)
158095994acSnicm _exit(1);
159498ee386Snicm closefrom(STDERR_FILENO + 1);
160498ee386Snicm
161fb0b819bSnicm execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL);
162095994acSnicm _exit(1);
163095994acSnicm default:
164095994acSnicm /* Parent process. */
165189d1393Snicm sigprocmask(SIG_SETMASK, &oldset, NULL);
166095994acSnicm close(pipe_fd[1]);
167095994acSnicm
168095994acSnicm wp->pipe_fd = pipe_fd[0];
1692920028dSnicm memcpy(wpo, &wp->offset, sizeof *wpo);
170095994acSnicm
171a0ee4254Snicm setblocking(wp->pipe_fd, 0);
172c10e3970Snicm wp->pipe_event = bufferevent_new(wp->pipe_fd,
173c10e3970Snicm cmd_pipe_pane_read_callback,
174c10e3970Snicm cmd_pipe_pane_write_callback,
175c10e3970Snicm cmd_pipe_pane_error_callback,
176c10e3970Snicm wp);
177b32e1d34Snicm if (wp->pipe_event == NULL)
178b32e1d34Snicm fatalx("out of memory");
179c10e3970Snicm if (out)
180c10e3970Snicm bufferevent_enable(wp->pipe_event, EV_WRITE);
181c10e3970Snicm if (in)
182c10e3970Snicm bufferevent_enable(wp->pipe_event, EV_READ);
183fb0b819bSnicm
184fb0b819bSnicm free(cmd);
185a224d0d3Snicm return (CMD_RETURN_NORMAL);
186095994acSnicm }
187095994acSnicm }
188e1e559afSnicm
189dc1f0f5fSnicm static void
cmd_pipe_pane_read_callback(__unused struct bufferevent * bufev,void * data)190c10e3970Snicm cmd_pipe_pane_read_callback(__unused struct bufferevent *bufev, void *data)
191c10e3970Snicm {
192c10e3970Snicm struct window_pane *wp = data;
193c10e3970Snicm struct evbuffer *evb = wp->pipe_event->input;
194c10e3970Snicm size_t available;
195c10e3970Snicm
196c10e3970Snicm available = EVBUFFER_LENGTH(evb);
197c10e3970Snicm log_debug("%%%u pipe read %zu", wp->id, available);
198c10e3970Snicm
199c10e3970Snicm bufferevent_write(wp->event, EVBUFFER_DATA(evb), available);
200c10e3970Snicm evbuffer_drain(evb, available);
201c10e3970Snicm
202c10e3970Snicm if (window_pane_destroy_ready(wp))
203c10e3970Snicm server_destroy_pane(wp, 1);
204c10e3970Snicm }
205c10e3970Snicm
206c10e3970Snicm static void
cmd_pipe_pane_write_callback(__unused struct bufferevent * bufev,void * data)207f35e5f52Snicm cmd_pipe_pane_write_callback(__unused struct bufferevent *bufev, void *data)
208f35e5f52Snicm {
209f35e5f52Snicm struct window_pane *wp = data;
210f35e5f52Snicm
211f35e5f52Snicm log_debug("%%%u pipe empty", wp->id);
212c10e3970Snicm
213f35e5f52Snicm if (window_pane_destroy_ready(wp))
214f35e5f52Snicm server_destroy_pane(wp, 1);
215f35e5f52Snicm }
216f35e5f52Snicm
217f35e5f52Snicm static void
cmd_pipe_pane_error_callback(__unused struct bufferevent * bufev,__unused short what,void * data)218d0e2e7f1Snicm cmd_pipe_pane_error_callback(__unused struct bufferevent *bufev,
219d0e2e7f1Snicm __unused short what, void *data)
220e1e559afSnicm {
221e1e559afSnicm struct window_pane *wp = data;
222e1e559afSnicm
223f35e5f52Snicm log_debug("%%%u pipe error", wp->id);
224f35e5f52Snicm
225e1e559afSnicm bufferevent_free(wp->pipe_event);
226e1e559afSnicm close(wp->pipe_fd);
227e1e559afSnicm wp->pipe_fd = -1;
228f35e5f52Snicm
229f35e5f52Snicm if (window_pane_destroy_ready(wp))
230f35e5f52Snicm server_destroy_pane(wp, 1);
231e1e559afSnicm }
232