4 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
20 #include <sys/socket.h>
33 * Open pipe to redirect pane output. If already open, close first.
36 static enum cmd_retval
cmd_pipe_pane_exec(struct cmd
*, struct cmdq_item
*);
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 *);
42 const struct cmd_entry cmd_pipe_pane_entry
= {
46 .args
= { "IOot:", 0, 1, NULL
},
47 .usage
= "[-IOo] " CMD_TARGET_PANE_USAGE
" [shell-command]",
49 .target
= { 't', CMD_FIND_PANE
, 0 },
51 .flags
= CMD_AFTERHOOK
,
52 .exec
= cmd_pipe_pane_exec
55 static enum cmd_retval
56 cmd_pipe_pane_exec(struct cmd
*self
, struct cmdq_item
*item
)
58 struct args
*args
= cmd_get_args(self
);
59 struct cmd_find_state
*target
= cmdq_get_target(item
);
60 struct client
*tc
= cmdq_get_target_client(item
);
61 struct window_pane
*wp
= target
->wp
;
62 struct session
*s
= target
->s
;
63 struct winlink
*wl
= target
->wl
;
64 struct window_pane_offset
*wpo
= &wp
->pipe_offset
;
66 int old_fd
, pipe_fd
[2], null_fd
, in
, out
;
67 struct format_tree
*ft
;
70 /* Destroy the old pipe. */
72 if (wp
->pipe_fd
!= -1) {
73 bufferevent_free(wp
->pipe_event
);
77 if (window_pane_destroy_ready(wp
)) {
78 server_destroy_pane(wp
, 1);
79 return (CMD_RETURN_NORMAL
);
83 /* If no pipe command, that is enough. */
84 if (args_count(args
) == 0 || *args_string(args
, 0) == '\0')
85 return (CMD_RETURN_NORMAL
);
88 * With -o, only open the new pipe if there was no previous one. This
89 * allows a pipe to be toggled with a single key, for example:
91 * bind ^p pipep -o 'cat >>~/output'
93 if (args_has(args
, 'o') && old_fd
!= -1)
94 return (CMD_RETURN_NORMAL
);
96 /* What do we want to do? Neither -I or -O is -O. */
97 if (args_has(args
, 'I')) {
99 out
= args_has(args
, 'O');
105 /* Open the new pipe. */
106 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, pipe_fd
) != 0) {
107 cmdq_error(item
, "socketpair error: %s", strerror(errno
));
108 return (CMD_RETURN_ERROR
);
111 /* Expand the command. */
112 ft
= format_create(cmdq_get_client(item
), item
, FORMAT_NONE
, 0);
113 format_defaults(ft
, tc
, s
, wl
, wp
);
114 cmd
= format_expand_time(ft
, args_string(args
, 0));
117 /* Fork the child. */
119 sigprocmask(SIG_BLOCK
, &set
, &oldset
);
122 sigprocmask(SIG_SETMASK
, &oldset
, NULL
);
123 cmdq_error(item
, "fork error: %s", strerror(errno
));
126 return (CMD_RETURN_ERROR
);
129 proc_clear_signals(server_proc
, 1);
130 sigprocmask(SIG_SETMASK
, &oldset
, NULL
);
133 null_fd
= open(_PATH_DEVNULL
, O_WRONLY
);
135 if (dup2(pipe_fd
[1], STDIN_FILENO
) == -1)
138 if (dup2(null_fd
, STDIN_FILENO
) == -1)
142 if (dup2(pipe_fd
[1], STDOUT_FILENO
) == -1)
144 if (pipe_fd
[1] != STDOUT_FILENO
)
147 if (dup2(null_fd
, STDOUT_FILENO
) == -1)
150 if (dup2(null_fd
, STDERR_FILENO
) == -1)
152 closefrom(STDERR_FILENO
+ 1);
154 execl(_PATH_BSHELL
, "sh", "-c", cmd
, (char *) NULL
);
157 /* Parent process. */
158 sigprocmask(SIG_SETMASK
, &oldset
, NULL
);
161 wp
->pipe_fd
= pipe_fd
[0];
162 memcpy(wpo
, &wp
->offset
, sizeof *wpo
);
164 setblocking(wp
->pipe_fd
, 0);
165 wp
->pipe_event
= bufferevent_new(wp
->pipe_fd
,
166 cmd_pipe_pane_read_callback
,
167 cmd_pipe_pane_write_callback
,
168 cmd_pipe_pane_error_callback
,
170 if (wp
->pipe_event
== NULL
)
171 fatalx("out of memory");
173 bufferevent_enable(wp
->pipe_event
, EV_WRITE
);
175 bufferevent_enable(wp
->pipe_event
, EV_READ
);
178 return (CMD_RETURN_NORMAL
);
183 cmd_pipe_pane_read_callback(__unused
struct bufferevent
*bufev
, void *data
)
185 struct window_pane
*wp
= data
;
186 struct evbuffer
*evb
= wp
->pipe_event
->input
;
189 available
= EVBUFFER_LENGTH(evb
);
190 log_debug("%%%u pipe read %zu", wp
->id
, available
);
192 bufferevent_write(wp
->event
, EVBUFFER_DATA(evb
), available
);
193 evbuffer_drain(evb
, available
);
195 if (window_pane_destroy_ready(wp
))
196 server_destroy_pane(wp
, 1);
200 cmd_pipe_pane_write_callback(__unused
struct bufferevent
*bufev
, void *data
)
202 struct window_pane
*wp
= data
;
204 log_debug("%%%u pipe empty", wp
->id
);
206 if (window_pane_destroy_ready(wp
))
207 server_destroy_pane(wp
, 1);
211 cmd_pipe_pane_error_callback(__unused
struct bufferevent
*bufev
,
212 __unused
short what
, void *data
)
214 struct window_pane
*wp
= data
;
216 log_debug("%%%u pipe error", wp
->id
);
218 bufferevent_free(wp
->pipe_event
);
222 if (window_pane_destroy_ready(wp
))
223 server_destroy_pane(wp
, 1);