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)  

proc.c
Go to the documentation of this file.
1 /* $OpenBSD$ */
2 
3 /*
4  * Copyright (c) 2015 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 #include <sys/uio.h>
22 #include <sys/utsname.h>
23 
24 #include <errno.h>
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 
30 #if defined(HAVE_NCURSES_H)
31 #include <ncurses.h>
32 #endif
33 
34 #include "tmux.h"
35 
36 struct tmuxproc {
37  const char *name;
38  int exit;
39 
40  void (*signalcb)(int);
41 
42  struct event ev_sigint;
43  struct event ev_sighup;
44  struct event ev_sigchld;
45  struct event ev_sigcont;
46  struct event ev_sigterm;
47  struct event ev_sigusr1;
48  struct event ev_sigusr2;
49  struct event ev_sigwinch;
50 
51  TAILQ_HEAD(, tmuxpeer) peers;
52 };
53 
54 struct tmuxpeer {
55  struct tmuxproc *parent;
56 
57  struct imsgbuf ibuf;
58  struct event event;
59 
60  int flags;
61 #define PEER_BAD 0x1
62 
63  void (*dispatchcb)(struct imsg *, void *);
64  void *arg;
65 
66  TAILQ_ENTRY(tmuxpeer) entry;
67 };
68 
69 static int peer_check_version(struct tmuxpeer *, struct imsg *);
70 static void proc_update_event(struct tmuxpeer *);
71 
72 static void
73 proc_event_cb(__unused int fd, short events, void *arg)
74 {
75  struct tmuxpeer *peer = arg;
76  ssize_t n;
77  struct imsg imsg;
78 
79  if (!(peer->flags & PEER_BAD) && (events & EV_READ)) {
80  if (((n = imsg_read(&peer->ibuf)) == -1 && errno != EAGAIN) ||
81  n == 0) {
82  peer->dispatchcb(NULL, peer->arg);
83  return;
84  }
85  for (;;) {
86  if ((n = imsg_get(&peer->ibuf, &imsg)) == -1) {
87  peer->dispatchcb(NULL, peer->arg);
88  return;
89  }
90  if (n == 0)
91  break;
92  log_debug("peer %p message %d", peer, imsg.hdr.type);
93 
94  if (peer_check_version(peer, &imsg) != 0) {
95  if (imsg.fd != -1)
96  close(imsg.fd);
97  imsg_free(&imsg);
98  break;
99  }
100 
101  peer->dispatchcb(&imsg, peer->arg);
102  imsg_free(&imsg);
103  }
104  }
105 
106  if (events & EV_WRITE) {
107  if (msgbuf_write(&peer->ibuf.w) <= 0 && errno != EAGAIN) {
108  peer->dispatchcb(NULL, peer->arg);
109  return;
110  }
111  }
112 
113  if ((peer->flags & PEER_BAD) && peer->ibuf.w.queued == 0) {
114  peer->dispatchcb(NULL, peer->arg);
115  return;
116  }
117 
118  proc_update_event(peer);
119 }
120 
121 static void
122 proc_signal_cb(int signo, __unused short events, void *arg)
123 {
124  struct tmuxproc *tp = arg;
125 
126  tp->signalcb(signo);
127 }
128 
129 static int
130 peer_check_version(struct tmuxpeer *peer, struct imsg *imsg)
131 {
132  int version;
133 
134  version = imsg->hdr.peerid & 0xff;
135  if (imsg->hdr.type != MSG_VERSION && version != PROTOCOL_VERSION) {
136  log_debug("peer %p bad version %d", peer, version);
137 
138  proc_send(peer, MSG_VERSION, -1, NULL, 0);
139  peer->flags |= PEER_BAD;
140 
141  return (-1);
142  }
143  return (0);
144 }
145 
146 static void
148 {
149  short events;
150 
151  event_del(&peer->event);
152 
153  events = EV_READ;
154  if (peer->ibuf.w.queued > 0)
155  events |= EV_WRITE;
156  event_set(&peer->event, peer->ibuf.fd, events, proc_event_cb, peer);
157 
158  event_add(&peer->event, NULL);
159 }
160 
161 int
162 proc_send(struct tmuxpeer *peer, enum msgtype type, int fd, const void *buf,
163  size_t len)
164 {
165  struct imsgbuf *ibuf = &peer->ibuf;
166  void *vp = (void *)buf;
167  int retval;
168 
169  if (peer->flags & PEER_BAD)
170  return (-1);
171  log_debug("sending message %d to peer %p (%zu bytes)", type, peer, len);
172 
173  retval = imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, fd, vp, len);
174  if (retval != 1)
175  return (-1);
176  proc_update_event(peer);
177  return (0);
178 }
179 
180 struct tmuxproc *
181 proc_start(const char *name)
182 {
183  struct tmuxproc *tp;
184  struct utsname u;
185 
186  log_open(name);
187  setproctitle("%s (%s)", name, socket_path);
188 
189  if (uname(&u) < 0)
190  memset(&u, 0, sizeof u);
191 
192  log_debug("%s started (%ld): version %s, socket %s, protocol %d", name,
193  (long)getpid(), getversion(), socket_path, PROTOCOL_VERSION);
194  log_debug("on %s %s %s", u.sysname, u.release, u.version);
195  log_debug("using libevent %s (%s)"
196 #ifdef HAVE_UTF8PROC
197  "; utf8proc %s"
198 #endif
199 #ifdef NCURSES_VERSION
200  "; ncurses " NCURSES_VERSION
201 #endif
202  , event_get_version(), event_get_method()
203 #ifdef HAVE_UTF8PROC
204  , utf8proc_version ()
205 #endif
206  );
207 
208  tp = xcalloc(1, sizeof *tp);
209  tp->name = xstrdup(name);
210  TAILQ_INIT(&tp->peers);
211 
212  return (tp);
213 }
214 
215 void
216 proc_loop(struct tmuxproc *tp, int (*loopcb)(void))
217 {
218  log_debug("%s loop enter", tp->name);
219  do
220  event_loop(EVLOOP_ONCE);
221  while (!tp->exit && (loopcb == NULL || !loopcb ()));
222  log_debug("%s loop exit", tp->name);
223 }
224 
225 void
226 proc_exit(struct tmuxproc *tp)
227 {
228  struct tmuxpeer *peer;
229 
230  TAILQ_FOREACH(peer, &tp->peers, entry)
231  imsg_flush(&peer->ibuf);
232  tp->exit = 1;
233 }
234 
235 void
236 proc_set_signals(struct tmuxproc *tp, void (*signalcb)(int))
237 {
238  struct sigaction sa;
239 
240  tp->signalcb = signalcb;
241 
242  memset(&sa, 0, sizeof sa);
243  sigemptyset(&sa.sa_mask);
244  sa.sa_flags = SA_RESTART;
245  sa.sa_handler = SIG_IGN;
246 
247  sigaction(SIGPIPE, &sa, NULL);
248  sigaction(SIGTSTP, &sa, NULL);
249  sigaction(SIGTTIN, &sa, NULL);
250  sigaction(SIGTTOU, &sa, NULL);
251  sigaction(SIGQUIT, &sa, NULL);
252 
253  signal_set(&tp->ev_sigint, SIGINT, proc_signal_cb, tp);
254  signal_add(&tp->ev_sigint, NULL);
255  signal_set(&tp->ev_sighup, SIGHUP, proc_signal_cb, tp);
256  signal_add(&tp->ev_sighup, NULL);
257  signal_set(&tp->ev_sigchld, SIGCHLD, proc_signal_cb, tp);
258  signal_add(&tp->ev_sigchld, NULL);
259  signal_set(&tp->ev_sigcont, SIGCONT, proc_signal_cb, tp);
260  signal_add(&tp->ev_sigcont, NULL);
261  signal_set(&tp->ev_sigterm, SIGTERM, proc_signal_cb, tp);
262  signal_add(&tp->ev_sigterm, NULL);
263  signal_set(&tp->ev_sigusr1, SIGUSR1, proc_signal_cb, tp);
264  signal_add(&tp->ev_sigusr1, NULL);
265  signal_set(&tp->ev_sigusr2, SIGUSR2, proc_signal_cb, tp);
266  signal_add(&tp->ev_sigusr2, NULL);
267  signal_set(&tp->ev_sigwinch, SIGWINCH, proc_signal_cb, tp);
268  signal_add(&tp->ev_sigwinch, NULL);
269 }
270 
271 void
272 proc_clear_signals(struct tmuxproc *tp, int defaults)
273 {
274  struct sigaction sa;
275 
276  memset(&sa, 0, sizeof sa);
277  sigemptyset(&sa.sa_mask);
278  sa.sa_flags = SA_RESTART;
279  sa.sa_handler = SIG_DFL;
280 
281  sigaction(SIGPIPE, &sa, NULL);
282  sigaction(SIGTSTP, &sa, NULL);
283 
284  signal_del(&tp->ev_sigint);
285  signal_del(&tp->ev_sighup);
286  signal_del(&tp->ev_sigchld);
287  signal_del(&tp->ev_sigcont);
288  signal_del(&tp->ev_sigterm);
289  signal_del(&tp->ev_sigusr1);
290  signal_del(&tp->ev_sigusr2);
291  signal_del(&tp->ev_sigwinch);
292 
293  if (defaults) {
294  sigaction(SIGINT, &sa, NULL);
295  sigaction(SIGQUIT, &sa, NULL);
296  sigaction(SIGHUP, &sa, NULL);
297  sigaction(SIGCHLD, &sa, NULL);
298  sigaction(SIGCONT, &sa, NULL);
299  sigaction(SIGTERM, &sa, NULL);
300  sigaction(SIGUSR1, &sa, NULL);
301  sigaction(SIGUSR2, &sa, NULL);
302  sigaction(SIGWINCH, &sa, NULL);
303  }
304 }
305 
306 struct tmuxpeer *
307 proc_add_peer(struct tmuxproc *tp, int fd,
308  void (*dispatchcb)(struct imsg *, void *), void *arg)
309 {
310  struct tmuxpeer *peer;
311 
312  peer = xcalloc(1, sizeof *peer);
313  peer->parent = tp;
314 
315  peer->dispatchcb = dispatchcb;
316  peer->arg = arg;
317 
318  imsg_init(&peer->ibuf, fd);
319  event_set(&peer->event, fd, EV_READ, proc_event_cb, peer);
320 
321  log_debug("add peer %p: %d (%p)", peer, fd, arg);
322  TAILQ_INSERT_TAIL(&tp->peers, peer, entry);
323 
324  proc_update_event(peer);
325  return (peer);
326 }
327 
328 void
330 {
331  TAILQ_REMOVE(&peer->parent->peers, peer, entry);
332  log_debug("remove peer %p", peer);
333 
334  event_del(&peer->event);
335  imsg_clear(&peer->ibuf);
336 
337  close(peer->ibuf.fd);
338  free(peer);
339 }
340 
341 void
343 {
344  peer->flags |= PEER_BAD;
345 }
346 
347 void
349 {
350  log_toggle(tp->name);
351 }
352 
353 pid_t
355 {
356  pid_t pid;
357  int pair[2];
358 
359  if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
360  fatal("socketpair failed");
361  switch (pid = fork()) {
362  case -1:
363  fatal("fork failed");
364  case 0:
365  close(pair[0]);
366  *fd = pair[1];
367  if (daemon(1, 0) != 0)
368  fatal("daemon failed");
369  return (0);
370  default:
371  close(pair[1]);
372  *fd = pair[0];
373  return (pid);
374  }
375 }
static TAILQ_HEAD(window)
Definition: alerts.c:40
#define __unused
Definition: compat.h:60
void setproctitle(const char *,...)
int daemon(int, int)
const char * name
Definition: layout-set.c:38
void fatal(const char *msg,...)
Definition: log.c:144
void log_toggle(const char *name)
Definition: log.c:78
void log_debug(const char *msg,...)
Definition: log.c:130
void log_open(const char *name)
Definition: log.c:58
static void proc_signal_cb(int signo, short events, void *arg)
Definition: proc.c:122
void proc_loop(struct tmuxproc *tp, int(*loopcb)(void))
Definition: proc.c:216
#define PEER_BAD
Definition: proc.c:61
void proc_clear_signals(struct tmuxproc *tp, int defaults)
Definition: proc.c:272
void proc_toggle_log(struct tmuxproc *tp)
Definition: proc.c:348
struct tmuxpeer * proc_add_peer(struct tmuxproc *tp, int fd, void(*dispatchcb)(struct imsg *, void *), void *arg)
Definition: proc.c:307
void proc_kill_peer(struct tmuxpeer *peer)
Definition: proc.c:342
int proc_send(struct tmuxpeer *peer, enum msgtype type, int fd, const void *buf, size_t len)
Definition: proc.c:162
void proc_remove_peer(struct tmuxpeer *peer)
Definition: proc.c:329
pid_t proc_fork_and_daemon(int *fd)
Definition: proc.c:354
static void proc_event_cb(int fd, short events, void *arg)
Definition: proc.c:73
struct tmuxproc * proc_start(const char *name)
Definition: proc.c:181
static void proc_update_event(struct tmuxpeer *)
Definition: proc.c:147
static int peer_check_version(struct tmuxpeer *, struct imsg *)
Definition: proc.c:130
void proc_set_signals(struct tmuxproc *tp, void(*signalcb)(int))
Definition: proc.c:236
void proc_exit(struct tmuxproc *tp)
Definition: proc.c:226
Definition: proc.c:54
struct imsgbuf ibuf
Definition: proc.c:57
void * arg
Definition: proc.c:64
int flags
Definition: proc.c:60
struct event event
Definition: proc.c:58
void(* dispatchcb)(struct imsg *, void *)
Definition: proc.c:63
struct tmuxproc * parent
Definition: proc.c:55
Definition: proc.c:36
int exit
Definition: proc.c:38
void(* signalcb)(int)
Definition: proc.c:40
struct event ev_sigwinch
Definition: proc.c:49
struct event ev_sigterm
Definition: proc.c:46
struct event ev_sigcont
Definition: proc.c:45
struct event ev_sigusr2
Definition: proc.c:48
struct event ev_sigchld
Definition: proc.c:44
struct event ev_sighup
Definition: proc.c:43
struct event ev_sigint
Definition: proc.c:42
const char * name
Definition: proc.c:37
struct event ev_sigusr1
Definition: proc.c:47
const char * socket_path
Definition: tmux.c:42
const char * getversion(void)
Definition: tmux.c:320
#define PROTOCOL_VERSION
Definition: tmux.h:69
msgtype
Definition: tmux.h:505
@ MSG_VERSION
Definition: tmux.h:506
void * xcalloc(size_t nmemb, size_t size)
Definition: xmalloc.c:41
char * xstrdup(const char *str)
Definition: xmalloc.c:89