1 |
|
|
/* $OpenBSD: cmd-pipe-pane.c,v 1.46 2017/07/14 18:49:07 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_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 |
|
|
.alias = "pipep", |
45 |
|
|
|
46 |
|
|
.args = { "ot:", 0, 1 }, |
47 |
|
|
.usage = "[-o] " CMD_TARGET_PANE_USAGE " [command]", |
48 |
|
|
|
49 |
|
|
.target = { 't', CMD_FIND_PANE, 0 }, |
50 |
|
|
|
51 |
|
|
.flags = CMD_AFTERHOOK, |
52 |
|
|
.exec = cmd_pipe_pane_exec |
53 |
|
|
}; |
54 |
|
|
|
55 |
|
|
static enum cmd_retval |
56 |
|
|
cmd_pipe_pane_exec(struct cmd *self, struct cmdq_item *item) |
57 |
|
|
{ |
58 |
|
|
struct args *args = self->args; |
59 |
|
|
struct client *c = cmd_find_client(item, NULL, 1); |
60 |
|
|
struct window_pane *wp = item->target.wp; |
61 |
|
|
struct session *s = item->target.s; |
62 |
|
|
struct winlink *wl = item->target.wl; |
63 |
|
|
char *cmd; |
64 |
|
|
int old_fd, pipe_fd[2], null_fd; |
65 |
|
|
struct format_tree *ft; |
66 |
|
|
sigset_t set, oldset; |
67 |
|
|
|
68 |
|
|
/* Destroy the old pipe. */ |
69 |
|
|
old_fd = wp->pipe_fd; |
70 |
|
|
if (wp->pipe_fd != -1) { |
71 |
|
|
bufferevent_free(wp->pipe_event); |
72 |
|
|
close(wp->pipe_fd); |
73 |
|
|
wp->pipe_fd = -1; |
74 |
|
|
|
75 |
|
|
if (window_pane_destroy_ready(wp)) { |
76 |
|
|
server_destroy_pane(wp, 1); |
77 |
|
|
return (CMD_RETURN_NORMAL); |
78 |
|
|
} |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
/* If no pipe command, that is enough. */ |
82 |
|
|
if (args->argc == 0 || *args->argv[0] == '\0') |
83 |
|
|
return (CMD_RETURN_NORMAL); |
84 |
|
|
|
85 |
|
|
/* |
86 |
|
|
* With -o, only open the new pipe if there was no previous one. This |
87 |
|
|
* allows a pipe to be toggled with a single key, for example: |
88 |
|
|
* |
89 |
|
|
* bind ^p pipep -o 'cat >>~/output' |
90 |
|
|
*/ |
91 |
|
|
if (args_has(self->args, 'o') && old_fd != -1) |
92 |
|
|
return (CMD_RETURN_NORMAL); |
93 |
|
|
|
94 |
|
|
/* Open the new pipe. */ |
95 |
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) { |
96 |
|
|
cmdq_error(item, "socketpair error: %s", strerror(errno)); |
97 |
|
|
return (CMD_RETURN_ERROR); |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
/* Expand the command. */ |
101 |
|
|
ft = format_create(item->client, item, FORMAT_NONE, 0); |
102 |
|
|
format_defaults(ft, c, s, wl, wp); |
103 |
|
|
cmd = format_expand_time(ft, args->argv[0], time(NULL)); |
104 |
|
|
format_free(ft); |
105 |
|
|
|
106 |
|
|
/* Fork the child. */ |
107 |
|
|
sigfillset(&set); |
108 |
|
|
sigprocmask(SIG_BLOCK, &set, &oldset); |
109 |
|
|
switch (fork()) { |
110 |
|
|
case -1: |
111 |
|
|
sigprocmask(SIG_SETMASK, &oldset, NULL); |
112 |
|
|
cmdq_error(item, "fork error: %s", strerror(errno)); |
113 |
|
|
|
114 |
|
|
free(cmd); |
115 |
|
|
return (CMD_RETURN_ERROR); |
116 |
|
|
case 0: |
117 |
|
|
/* Child process. */ |
118 |
|
|
proc_clear_signals(server_proc, 1); |
119 |
|
|
sigprocmask(SIG_SETMASK, &oldset, NULL); |
120 |
|
|
close(pipe_fd[0]); |
121 |
|
|
|
122 |
|
|
if (dup2(pipe_fd[1], STDIN_FILENO) == -1) |
123 |
|
|
_exit(1); |
124 |
|
|
if (pipe_fd[1] != STDIN_FILENO) |
125 |
|
|
close(pipe_fd[1]); |
126 |
|
|
|
127 |
|
|
null_fd = open(_PATH_DEVNULL, O_WRONLY, 0); |
128 |
|
|
if (dup2(null_fd, STDOUT_FILENO) == -1) |
129 |
|
|
_exit(1); |
130 |
|
|
if (dup2(null_fd, STDERR_FILENO) == -1) |
131 |
|
|
_exit(1); |
132 |
|
|
if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO) |
133 |
|
|
close(null_fd); |
134 |
|
|
|
135 |
|
|
closefrom(STDERR_FILENO + 1); |
136 |
|
|
|
137 |
|
|
execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL); |
138 |
|
|
_exit(1); |
139 |
|
|
default: |
140 |
|
|
/* Parent process. */ |
141 |
|
|
sigprocmask(SIG_SETMASK, &oldset, NULL); |
142 |
|
|
close(pipe_fd[1]); |
143 |
|
|
|
144 |
|
|
wp->pipe_fd = pipe_fd[0]; |
145 |
|
|
wp->pipe_off = EVBUFFER_LENGTH(wp->event->input); |
146 |
|
|
|
147 |
|
|
wp->pipe_event = bufferevent_new(wp->pipe_fd, NULL, |
148 |
|
|
cmd_pipe_pane_write_callback, cmd_pipe_pane_error_callback, |
149 |
|
|
wp); |
150 |
|
|
bufferevent_enable(wp->pipe_event, EV_WRITE); |
151 |
|
|
|
152 |
|
|
setblocking(wp->pipe_fd, 0); |
153 |
|
|
|
154 |
|
|
free(cmd); |
155 |
|
|
return (CMD_RETURN_NORMAL); |
156 |
|
|
} |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
static void |
160 |
|
|
cmd_pipe_pane_write_callback(__unused struct bufferevent *bufev, void *data) |
161 |
|
|
{ |
162 |
|
|
struct window_pane *wp = data; |
163 |
|
|
|
164 |
|
|
log_debug("%%%u pipe empty", wp->id); |
165 |
|
|
if (window_pane_destroy_ready(wp)) |
166 |
|
|
server_destroy_pane(wp, 1); |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
static void |
170 |
|
|
cmd_pipe_pane_error_callback(__unused struct bufferevent *bufev, |
171 |
|
|
__unused short what, void *data) |
172 |
|
|
{ |
173 |
|
|
struct window_pane *wp = data; |
174 |
|
|
|
175 |
|
|
log_debug("%%%u pipe error", wp->id); |
176 |
|
|
|
177 |
|
|
bufferevent_free(wp->pipe_event); |
178 |
|
|
close(wp->pipe_fd); |
179 |
|
|
wp->pipe_fd = -1; |
180 |
|
|
|
181 |
|
|
if (window_pane_destroy_ready(wp)) |
182 |
|
|
server_destroy_pane(wp, 1); |
183 |
|
|
} |