/src/tmux/cmd-pipe-pane.c
Line | Count | Source |
1 | | /* $OpenBSD: cmd-pipe-pane.c,v 1.65 2026/10/09 13:12:14 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 <signal.h> |
25 | | #include <stdlib.h> |
26 | | #include <string.h> |
27 | | #include <time.h> |
28 | | #include <unistd.h> |
29 | | |
30 | | #include "tmux.h" |
31 | | |
32 | | /* |
33 | | * Open pipe to redirect pane output. If already open, close first. |
34 | | */ |
35 | | |
36 | | static enum cmd_retval cmd_pipe_pane_exec(struct cmd *, struct cmdq_item *); |
37 | | |
38 | | static void cmd_pipe_pane_read_callback(struct bufferevent *, void *); |
39 | | static void cmd_pipe_pane_write_callback(struct bufferevent *, void *); |
40 | | static void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *); |
41 | | |
42 | | const struct cmd_entry cmd_pipe_pane_entry = { |
43 | | .name = "pipe-pane", |
44 | | .description = "Pipe pane input or output to a command.", |
45 | | .alias = "pipep", |
46 | | |
47 | | .args = { "IOot:", 0, 1, NULL }, |
48 | | .usage = "[-IOo] " CMD_TARGET_PANE_USAGE " [shell-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 | 0 | { |
59 | 0 | struct args *args = cmd_get_args(self); |
60 | 0 | struct cmd_find_state *target = cmdq_get_target(item); |
61 | 0 | struct client *tc = cmdq_get_target_client(item); |
62 | 0 | struct window_pane *wp = target->wp; |
63 | 0 | struct session *s = target->s; |
64 | 0 | struct winlink *wl = target->wl; |
65 | 0 | struct window_pane_offset *wpo = &wp->pipe_offset; |
66 | 0 | char *cmd; |
67 | 0 | int old_fd, pipe_fd[2], null_fd, in, out; |
68 | 0 | struct format_tree *ft; |
69 | 0 | sigset_t set, oldset; |
70 | | |
71 | | /* Do nothing if pane is dead. */ |
72 | 0 | if (window_pane_exited(wp)) { |
73 | 0 | cmdq_error(item, "target pane has exited"); |
74 | 0 | return (CMD_RETURN_ERROR); |
75 | 0 | } |
76 | | |
77 | | /* Destroy the old pipe. */ |
78 | 0 | old_fd = wp->pipe_fd; |
79 | 0 | if (wp->pipe_fd != -1) { |
80 | 0 | bufferevent_free(wp->pipe_event); |
81 | 0 | close(wp->pipe_fd); |
82 | 0 | wp->pipe_fd = -1; |
83 | |
|
84 | 0 | if (window_pane_destroy_ready(wp)) { |
85 | 0 | server_destroy_pane(wp, 1); |
86 | 0 | return (CMD_RETURN_NORMAL); |
87 | 0 | } |
88 | 0 | } |
89 | | |
90 | | /* If no pipe command, that is enough. */ |
91 | 0 | if (args_count(args) == 0 || *args_string(args, 0) == '\0') |
92 | 0 | return (CMD_RETURN_NORMAL); |
93 | | |
94 | | /* |
95 | | * With -o, only open the new pipe if there was no previous one. This |
96 | | * allows a pipe to be toggled with a single key, for example: |
97 | | * |
98 | | * bind ^p pipep -o 'cat >>~/output' |
99 | | */ |
100 | 0 | if (args_has(args, 'o') && old_fd != -1) |
101 | 0 | return (CMD_RETURN_NORMAL); |
102 | | |
103 | | /* What do we want to do? Neither -I or -O is -O. */ |
104 | 0 | if (args_has(args, 'I')) { |
105 | 0 | in = 1; |
106 | 0 | out = args_has(args, 'O'); |
107 | 0 | } else { |
108 | 0 | in = 0; |
109 | 0 | out = 1; |
110 | 0 | } |
111 | | |
112 | | /* Open the new pipe. */ |
113 | 0 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) { |
114 | 0 | cmdq_error(item, "socketpair error: %s", strerror(errno)); |
115 | 0 | return (CMD_RETURN_ERROR); |
116 | 0 | } |
117 | | |
118 | | /* Expand the command. */ |
119 | 0 | ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0); |
120 | 0 | format_defaults(ft, tc, s, wl, wp); |
121 | 0 | cmd = format_expand_time(ft, args_string(args, 0)); |
122 | 0 | format_free(ft); |
123 | | |
124 | | /* Fork the child. */ |
125 | 0 | sigfillset(&set); |
126 | 0 | sigprocmask(SIG_BLOCK, &set, &oldset); |
127 | 0 | switch ((wp->pipe_pid = fork())) { |
128 | 0 | case -1: |
129 | 0 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
130 | 0 | cmdq_error(item, "fork error: %s", strerror(errno)); |
131 | |
|
132 | 0 | close(pipe_fd[0]); |
133 | 0 | close(pipe_fd[1]); |
134 | 0 | free(cmd); |
135 | 0 | return (CMD_RETURN_ERROR); |
136 | 0 | case 0: |
137 | | /* Child process. */ |
138 | 0 | proc_clear_signals(server_proc, 1); |
139 | 0 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
140 | 0 | close(pipe_fd[0]); |
141 | |
|
142 | 0 | if (setpgid(0, 0) == -1) |
143 | 0 | _exit(1); |
144 | | |
145 | 0 | null_fd = open(_PATH_DEVNULL, O_WRONLY); |
146 | 0 | if (out) { |
147 | 0 | if (dup2(pipe_fd[1], STDIN_FILENO) == -1) |
148 | 0 | _exit(1); |
149 | 0 | } else { |
150 | 0 | if (dup2(null_fd, STDIN_FILENO) == -1) |
151 | 0 | _exit(1); |
152 | 0 | } |
153 | 0 | if (in) { |
154 | 0 | if (dup2(pipe_fd[1], STDOUT_FILENO) == -1) |
155 | 0 | _exit(1); |
156 | 0 | if (pipe_fd[1] != STDOUT_FILENO) |
157 | 0 | close(pipe_fd[1]); |
158 | 0 | } else { |
159 | 0 | if (dup2(null_fd, STDOUT_FILENO) == -1) |
160 | 0 | _exit(1); |
161 | 0 | } |
162 | 0 | if (dup2(null_fd, STDERR_FILENO) == -1) |
163 | 0 | _exit(1); |
164 | 0 | closefrom(STDERR_FILENO + 1); |
165 | |
|
166 | 0 | execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL); |
167 | 0 | _exit(1); |
168 | 0 | default: |
169 | | /* Parent process. */ |
170 | 0 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
171 | 0 | close(pipe_fd[1]); |
172 | |
|
173 | 0 | wp->pipe_fd = pipe_fd[0]; |
174 | 0 | memcpy(wpo, &wp->offset, sizeof *wpo); |
175 | |
|
176 | 0 | setblocking(wp->pipe_fd, 0); |
177 | 0 | wp->pipe_event = bufferevent_new(wp->pipe_fd, |
178 | 0 | cmd_pipe_pane_read_callback, |
179 | 0 | cmd_pipe_pane_write_callback, |
180 | 0 | cmd_pipe_pane_error_callback, |
181 | 0 | wp); |
182 | 0 | if (wp->pipe_event == NULL) |
183 | 0 | fatalx("out of memory"); |
184 | 0 | if (out) |
185 | 0 | bufferevent_enable(wp->pipe_event, EV_WRITE); |
186 | 0 | if (in) |
187 | 0 | bufferevent_enable(wp->pipe_event, EV_READ); |
188 | |
|
189 | 0 | free(cmd); |
190 | 0 | return (CMD_RETURN_NORMAL); |
191 | 0 | } |
192 | 0 | } |
193 | | |
194 | | static void |
195 | | cmd_pipe_pane_read_callback(__unused struct bufferevent *bufev, void *data) |
196 | 0 | { |
197 | 0 | struct window_pane *wp = data; |
198 | 0 | struct evbuffer *evb; |
199 | 0 | size_t available; |
200 | |
|
201 | 0 | if (wp->pipe_event == NULL) |
202 | 0 | return; |
203 | 0 | evb = wp->pipe_event->input; |
204 | |
|
205 | 0 | available = EVBUFFER_LENGTH(evb); |
206 | 0 | log_debug("%%%u pipe read %zu", wp->id, available); |
207 | |
|
208 | 0 | bufferevent_write(wp->event, EVBUFFER_DATA(evb), available); |
209 | 0 | evbuffer_drain(evb, available); |
210 | |
|
211 | 0 | if (window_pane_destroy_ready(wp)) |
212 | 0 | server_destroy_pane(wp, 1); |
213 | 0 | } |
214 | | |
215 | | static void |
216 | | cmd_pipe_pane_write_callback(__unused struct bufferevent *bufev, void *data) |
217 | 0 | { |
218 | 0 | struct window_pane *wp = data; |
219 | |
|
220 | 0 | log_debug("%%%u pipe empty", wp->id); |
221 | |
|
222 | 0 | if (window_pane_destroy_ready(wp)) |
223 | 0 | server_destroy_pane(wp, 1); |
224 | 0 | } |
225 | | |
226 | | static void |
227 | | cmd_pipe_pane_error_callback(__unused struct bufferevent *bufev, |
228 | | __unused short what, void *data) |
229 | 0 | { |
230 | 0 | struct window_pane *wp = data; |
231 | |
|
232 | 0 | log_debug("%%%u pipe error", wp->id); |
233 | |
|
234 | 0 | bufferevent_free(wp->pipe_event); |
235 | 0 | close(wp->pipe_fd); |
236 | 0 | wp->pipe_fd = -1; |
237 | |
|
238 | 0 | if (window_pane_destroy_ready(wp)) |
239 | 0 | server_destroy_pane(wp, 1); |
240 | 0 | } |