tmux  3.2a
About: tmux is a terminal multiplexer that lets you switch easily between several programs in one terminal.
  Fossies Dox: tmux-3.2a.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

cmd-pipe-pane.c
Go to the documentation of this file.
1 /* $OpenBSD$ */
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  .alias = "pipep",
45 
46  .args = { "IOot:", 0, 1 },
47  .usage = "[-IOo] " 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 = 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;
65  char *cmd;
66  int old_fd, pipe_fd[2], null_fd, in, out;
67  struct format_tree *ft;
68  sigset_t set, oldset;
69 
70  /* Destroy the old pipe. */
71  old_fd = wp->pipe_fd;
72  if (wp->pipe_fd != -1) {
73  bufferevent_free(wp->pipe_event);
74  close(wp->pipe_fd);
75  wp->pipe_fd = -1;
76 
79  return (CMD_RETURN_NORMAL);
80  }
81  }
82 
83  /* If no pipe command, that is enough. */
84  if (args->argc == 0 || *args->argv[0] == '\0')
85  return (CMD_RETURN_NORMAL);
86 
87  /*
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:
90  *
91  * bind ^p pipep -o 'cat >>~/output'
92  */
93  if (args_has(args, 'o') && old_fd != -1)
94  return (CMD_RETURN_NORMAL);
95 
96  /* What do we want to do? Neither -I or -O is -O. */
97  if (args_has(args, 'I')) {
98  in = 1;
99  out = args_has(args, 'O');
100  } else {
101  in = 0;
102  out = 1;
103  }
104 
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);
109  }
110 
111  /* Expand the command. */
113  format_defaults(ft, tc, s, wl, wp);
114  cmd = format_expand_time(ft, args->argv[0]);
115  format_free(ft);
116 
117  /* Fork the child. */
118  sigfillset(&set);
119  sigprocmask(SIG_BLOCK, &set, &oldset);
120  switch (fork()) {
121  case -1:
122  sigprocmask(SIG_SETMASK, &oldset, NULL);
123  cmdq_error(item, "fork error: %s", strerror(errno));
124 
125  free(cmd);
126  return (CMD_RETURN_ERROR);
127  case 0:
128  /* Child process. */
130  sigprocmask(SIG_SETMASK, &oldset, NULL);
131  close(pipe_fd[0]);
132 
133  null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
134  if (out) {
135  if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
136  _exit(1);
137  } else {
138  if (dup2(null_fd, STDIN_FILENO) == -1)
139  _exit(1);
140  }
141  if (in) {
142  if (dup2(pipe_fd[1], STDOUT_FILENO) == -1)
143  _exit(1);
144  if (pipe_fd[1] != STDOUT_FILENO)
145  close(pipe_fd[1]);
146  } else {
147  if (dup2(null_fd, STDOUT_FILENO) == -1)
148  _exit(1);
149  }
150  if (dup2(null_fd, STDERR_FILENO) == -1)
151  _exit(1);
152  closefrom(STDERR_FILENO + 1);
153 
154  execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL);
155  _exit(1);
156  default:
157  /* Parent process. */
158  sigprocmask(SIG_SETMASK, &oldset, NULL);
159  close(pipe_fd[1]);
160 
161  wp->pipe_fd = pipe_fd[0];
162  memcpy(wpo, &wp->offset, sizeof *wpo);
163 
164  setblocking(wp->pipe_fd, 0);
165  wp->pipe_event = bufferevent_new(wp->pipe_fd,
169  wp);
170  if (wp->pipe_event == NULL)
171  fatalx("out of memory");
172  if (out)
173  bufferevent_enable(wp->pipe_event, EV_WRITE);
174  if (in)
175  bufferevent_enable(wp->pipe_event, EV_READ);
176 
177  free(cmd);
178  return (CMD_RETURN_NORMAL);
179  }
180 }
181 
182 static void
183 cmd_pipe_pane_read_callback(__unused struct bufferevent *bufev, void *data)
184 {
185  struct window_pane *wp = data;
186  struct evbuffer *evb = wp->pipe_event->input;
187  size_t available;
188 
189  available = EVBUFFER_LENGTH(evb);
190  log_debug("%%%u pipe read %zu", wp->id, available);
191 
192  bufferevent_write(wp->event, EVBUFFER_DATA(evb), available);
193  evbuffer_drain(evb, available);
194 
196  server_destroy_pane(wp, 1);
197 }
198 
199 static void
200 cmd_pipe_pane_write_callback(__unused struct bufferevent *bufev, void *data)
201 {
202  struct window_pane *wp = data;
203 
204  log_debug("%%%u pipe empty", wp->id);
205 
207  server_destroy_pane(wp, 1);
208 }
209 
210 static void
211 cmd_pipe_pane_error_callback(__unused struct bufferevent *bufev,
212  __unused short what, void *data)
213 {
214  struct window_pane *wp = data;
215 
216  log_debug("%%%u pipe error", wp->id);
217 
218  bufferevent_free(wp->pipe_event);
219  close(wp->pipe_fd);
220  wp->pipe_fd = -1;
221 
223  server_destroy_pane(wp, 1);
224 }
int args_has(struct args *args, u_char flag)
Definition: arguments.c:259
static void cmd_pipe_pane_write_callback(struct bufferevent *, void *)
const struct cmd_entry cmd_pipe_pane_entry
Definition: cmd-pipe-pane.c:42
static enum cmd_retval cmd_pipe_pane_exec(struct cmd *, struct cmdq_item *)
Definition: cmd-pipe-pane.c:56
static void cmd_pipe_pane_read_callback(struct bufferevent *, void *)
static void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *)
struct client * cmdq_get_target_client(struct cmdq_item *item)
Definition: cmd-queue.c:157
struct client * cmdq_get_client(struct cmdq_item *item)
Definition: cmd-queue.c:150
struct cmd_find_state * cmdq_get_target(struct cmdq_item *item)
Definition: cmd-queue.c:171
void cmdq_error(struct cmdq_item *item, const char *fmt,...)
Definition: cmd-queue.c:833
struct args * cmd_get_args(struct cmd *cmd)
Definition: cmd.c:393
#define _PATH_DEVNULL
Definition: compat.h:106
#define __unused
Definition: compat.h:60
void closefrom(int)
#define _PATH_BSHELL
Definition: compat.h:98
void format_defaults(struct format_tree *ft, struct client *c, struct session *s, struct winlink *wl, struct window_pane *wp)
Definition: format.c:4684
struct format_tree * format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
Definition: format.c:3042
void format_free(struct format_tree *ft)
Definition: format.c:3066
char * format_expand_time(struct format_tree *ft, const char *fmt)
Definition: format.c:4597
void fatalx(const char *msg,...)
Definition: log.c:159
void log_debug(const char *msg,...)
Definition: log.c:130
void proc_clear_signals(struct tmuxproc *tp, int defaults)
Definition: proc.c:272
void server_destroy_pane(struct window_pane *wp, int notify)
Definition: server-fn.c:308
struct tmuxproc * server_proc
Definition: server.c:44
Definition: tmux.h:1435
int argc
Definition: tmux.h:1437
char ** argv
Definition: tmux.h:1438
Definition: tmux.h:1608
Definition: tmux.h:1525
const char * name
Definition: tmux.h:1526
struct window_pane * wp
Definition: tmux.h:1454
struct session * s
Definition: tmux.h:1451
struct winlink * wl
Definition: tmux.h:1452
Definition: cmd.c:212
struct window_pane * wp
Definition: format.c:136
struct session * s
Definition: format.c:133
struct winlink * wl
Definition: format.c:134
struct cmdq_item * item
Definition: format.c:139
Definition: tmux.h:1179
struct window_pane_offset pipe_offset
Definition: tmux.h:1018
struct window_pane_offset offset
Definition: tmux.h:1004
int pipe_fd
Definition: tmux.h:1016
struct bufferevent * event
Definition: tmux.h:1002
struct bufferevent * pipe_event
Definition: tmux.h:1017
u_int id
Definition: tmux.h:959
void setblocking(int fd, int state)
Definition: tmux.c:233
cmd_retval
Definition: tmux.h:1475
@ CMD_RETURN_NORMAL
Definition: tmux.h:1477
@ CMD_RETURN_ERROR
Definition: tmux.h:1476
#define CMD_AFTERHOOK
Definition: tmux.h:1541
#define CMD_TARGET_PANE_USAGE
Definition: tmux.h:1858
#define FORMAT_NONE
Definition: tmux.h:1973
@ CMD_FIND_PANE
Definition: tmux.h:1443
int window_pane_destroy_ready(struct window_pane *)
Definition: window.c:367