1ed4e6cd4Schristos /* $OpenBSD$ */
2ed4e6cd4Schristos
3ed4e6cd4Schristos /*
4ed4e6cd4Schristos * Copyright (c) 2015 Nicholas Marriott <nicholas.marriott@gmail.com>
5ed4e6cd4Schristos *
6ed4e6cd4Schristos * Permission to use, copy, modify, and distribute this software for any
7ed4e6cd4Schristos * purpose with or without fee is hereby granted, provided that the above
8ed4e6cd4Schristos * copyright notice and this permission notice appear in all copies.
9ed4e6cd4Schristos *
10ed4e6cd4Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11ed4e6cd4Schristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12ed4e6cd4Schristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13ed4e6cd4Schristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14ed4e6cd4Schristos * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15ed4e6cd4Schristos * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16ed4e6cd4Schristos * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17ed4e6cd4Schristos */
18ed4e6cd4Schristos
19ed4e6cd4Schristos #include <sys/types.h>
20e271dbb8Schristos #include <sys/socket.h>
21ed4e6cd4Schristos #include <sys/uio.h>
22ed4e6cd4Schristos #include <sys/utsname.h>
23ed4e6cd4Schristos
24ed4e6cd4Schristos #include <errno.h>
25fe99a117Schristos #include <signal.h>
26ed4e6cd4Schristos #include <stdlib.h>
27ed4e6cd4Schristos #include <string.h>
28ed4e6cd4Schristos #include <unistd.h>
29ed4e6cd4Schristos
30e271dbb8Schristos #if defined(HAVE_NCURSES_H)
31e271dbb8Schristos #include <ncurses.h>
32e271dbb8Schristos #endif
33e271dbb8Schristos
34ed4e6cd4Schristos #include "tmux.h"
35ed4e6cd4Schristos
36ed4e6cd4Schristos struct tmuxproc {
37ed4e6cd4Schristos const char *name;
38ed4e6cd4Schristos int exit;
39ed4e6cd4Schristos
40ed4e6cd4Schristos void (*signalcb)(int);
41fe99a117Schristos
42e271dbb8Schristos struct event ev_sigint;
43fe99a117Schristos struct event ev_sighup;
44fe99a117Schristos struct event ev_sigchld;
45fe99a117Schristos struct event ev_sigcont;
46fe99a117Schristos struct event ev_sigterm;
47fe99a117Schristos struct event ev_sigusr1;
48fe99a117Schristos struct event ev_sigusr2;
49fe99a117Schristos struct event ev_sigwinch;
50e271dbb8Schristos
51e271dbb8Schristos TAILQ_HEAD(, tmuxpeer) peers;
52ed4e6cd4Schristos };
53ed4e6cd4Schristos
54ed4e6cd4Schristos struct tmuxpeer {
55ed4e6cd4Schristos struct tmuxproc *parent;
56ed4e6cd4Schristos
57ed4e6cd4Schristos struct imsgbuf ibuf;
58ed4e6cd4Schristos struct event event;
5946548964Swiz uid_t uid;
60ed4e6cd4Schristos
61ed4e6cd4Schristos int flags;
62ed4e6cd4Schristos #define PEER_BAD 0x1
63ed4e6cd4Schristos
64ed4e6cd4Schristos void (*dispatchcb)(struct imsg *, void *);
65ed4e6cd4Schristos void *arg;
66e271dbb8Schristos
67e271dbb8Schristos TAILQ_ENTRY(tmuxpeer) entry;
68ed4e6cd4Schristos };
69ed4e6cd4Schristos
70ed4e6cd4Schristos static int peer_check_version(struct tmuxpeer *, struct imsg *);
71ed4e6cd4Schristos static void proc_update_event(struct tmuxpeer *);
72ed4e6cd4Schristos
73ed4e6cd4Schristos static void
proc_event_cb(__unused int fd,short events,void * arg)74ed4e6cd4Schristos proc_event_cb(__unused int fd, short events, void *arg)
75ed4e6cd4Schristos {
76ed4e6cd4Schristos struct tmuxpeer *peer = arg;
77ed4e6cd4Schristos ssize_t n;
78ed4e6cd4Schristos struct imsg imsg;
79ed4e6cd4Schristos
80ed4e6cd4Schristos if (!(peer->flags & PEER_BAD) && (events & EV_READ)) {
81ed4e6cd4Schristos if (((n = imsg_read(&peer->ibuf)) == -1 && errno != EAGAIN) ||
82ed4e6cd4Schristos n == 0) {
83ed4e6cd4Schristos peer->dispatchcb(NULL, peer->arg);
84ed4e6cd4Schristos return;
85ed4e6cd4Schristos }
86ed4e6cd4Schristos for (;;) {
87ed4e6cd4Schristos if ((n = imsg_get(&peer->ibuf, &imsg)) == -1) {
88ed4e6cd4Schristos peer->dispatchcb(NULL, peer->arg);
89ed4e6cd4Schristos return;
90ed4e6cd4Schristos }
91ed4e6cd4Schristos if (n == 0)
92ed4e6cd4Schristos break;
93ed4e6cd4Schristos log_debug("peer %p message %d", peer, imsg.hdr.type);
94ed4e6cd4Schristos
95ed4e6cd4Schristos if (peer_check_version(peer, &imsg) != 0) {
96*f844e94eSwiz fd = imsg_get_fd(&imsg);
97*f844e94eSwiz if (fd != -1)
98*f844e94eSwiz close(fd);
99ed4e6cd4Schristos imsg_free(&imsg);
100ed4e6cd4Schristos break;
101ed4e6cd4Schristos }
102ed4e6cd4Schristos
103ed4e6cd4Schristos peer->dispatchcb(&imsg, peer->arg);
104ed4e6cd4Schristos imsg_free(&imsg);
105ed4e6cd4Schristos }
106ed4e6cd4Schristos }
107ed4e6cd4Schristos
108ed4e6cd4Schristos if (events & EV_WRITE) {
109ed4e6cd4Schristos if (msgbuf_write(&peer->ibuf.w) <= 0 && errno != EAGAIN) {
110ed4e6cd4Schristos peer->dispatchcb(NULL, peer->arg);
111ed4e6cd4Schristos return;
112ed4e6cd4Schristos }
113ed4e6cd4Schristos }
114ed4e6cd4Schristos
115ed4e6cd4Schristos if ((peer->flags & PEER_BAD) && peer->ibuf.w.queued == 0) {
116ed4e6cd4Schristos peer->dispatchcb(NULL, peer->arg);
117ed4e6cd4Schristos return;
118ed4e6cd4Schristos }
119ed4e6cd4Schristos
120ed4e6cd4Schristos proc_update_event(peer);
121ed4e6cd4Schristos }
122ed4e6cd4Schristos
123ed4e6cd4Schristos static void
proc_signal_cb(int signo,__unused short events,void * arg)124ed4e6cd4Schristos proc_signal_cb(int signo, __unused short events, void *arg)
125ed4e6cd4Schristos {
126ed4e6cd4Schristos struct tmuxproc *tp = arg;
127ed4e6cd4Schristos
128ed4e6cd4Schristos tp->signalcb(signo);
129ed4e6cd4Schristos }
130ed4e6cd4Schristos
131ed4e6cd4Schristos static int
peer_check_version(struct tmuxpeer * peer,struct imsg * imsg)132ed4e6cd4Schristos peer_check_version(struct tmuxpeer *peer, struct imsg *imsg)
133ed4e6cd4Schristos {
134ed4e6cd4Schristos int version;
135ed4e6cd4Schristos
136ed4e6cd4Schristos version = imsg->hdr.peerid & 0xff;
137ed4e6cd4Schristos if (imsg->hdr.type != MSG_VERSION && version != PROTOCOL_VERSION) {
138ed4e6cd4Schristos log_debug("peer %p bad version %d", peer, version);
139ed4e6cd4Schristos
140ed4e6cd4Schristos proc_send(peer, MSG_VERSION, -1, NULL, 0);
141ed4e6cd4Schristos peer->flags |= PEER_BAD;
142ed4e6cd4Schristos
143ed4e6cd4Schristos return (-1);
144ed4e6cd4Schristos }
145ed4e6cd4Schristos return (0);
146ed4e6cd4Schristos }
147ed4e6cd4Schristos
148ed4e6cd4Schristos static void
proc_update_event(struct tmuxpeer * peer)149ed4e6cd4Schristos proc_update_event(struct tmuxpeer *peer)
150ed4e6cd4Schristos {
151ed4e6cd4Schristos short events;
152ed4e6cd4Schristos
153ed4e6cd4Schristos event_del(&peer->event);
154ed4e6cd4Schristos
155ed4e6cd4Schristos events = EV_READ;
156ed4e6cd4Schristos if (peer->ibuf.w.queued > 0)
157ed4e6cd4Schristos events |= EV_WRITE;
158ed4e6cd4Schristos event_set(&peer->event, peer->ibuf.fd, events, proc_event_cb, peer);
159ed4e6cd4Schristos
160ed4e6cd4Schristos event_add(&peer->event, NULL);
161ed4e6cd4Schristos }
162ed4e6cd4Schristos
163ed4e6cd4Schristos int
proc_send(struct tmuxpeer * peer,enum msgtype type,int fd,const void * buf,size_t len)164ed4e6cd4Schristos proc_send(struct tmuxpeer *peer, enum msgtype type, int fd, const void *buf,
165ed4e6cd4Schristos size_t len)
166ed4e6cd4Schristos {
167ed4e6cd4Schristos struct imsgbuf *ibuf = &peer->ibuf;
168f26e8bc9Schristos void *vp = __UNCONST(buf);
169ed4e6cd4Schristos int retval;
170ed4e6cd4Schristos
171ed4e6cd4Schristos if (peer->flags & PEER_BAD)
172ed4e6cd4Schristos return (-1);
173ed4e6cd4Schristos log_debug("sending message %d to peer %p (%zu bytes)", type, peer, len);
174ed4e6cd4Schristos
175ed4e6cd4Schristos retval = imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, fd, vp, len);
176ed4e6cd4Schristos if (retval != 1)
177ed4e6cd4Schristos return (-1);
178ed4e6cd4Schristos proc_update_event(peer);
179ed4e6cd4Schristos return (0);
180ed4e6cd4Schristos }
181ed4e6cd4Schristos
182ed4e6cd4Schristos struct tmuxproc *
proc_start(const char * name)183fe99a117Schristos proc_start(const char *name)
184ed4e6cd4Schristos {
185ed4e6cd4Schristos struct tmuxproc *tp;
186ed4e6cd4Schristos struct utsname u;
187ed4e6cd4Schristos
188ed4e6cd4Schristos log_open(name);
189ed4e6cd4Schristos setproctitle("%s (%s)", name, socket_path);
190ed4e6cd4Schristos
191ed4e6cd4Schristos if (uname(&u) < 0)
192ed4e6cd4Schristos memset(&u, 0, sizeof u);
193ed4e6cd4Schristos
194e9a2d6faSchristos log_debug("%s started (%ld): version %s, socket %s, protocol %d", name,
19568e6ba84Schristos (long)getpid(), getversion(), socket_path, PROTOCOL_VERSION);
196e271dbb8Schristos log_debug("on %s %s %s", u.sysname, u.release, u.version);
197*f844e94eSwiz log_debug("using libevent %s %s", event_get_version(), event_get_method());
198e271dbb8Schristos #ifdef HAVE_UTF8PROC
199*f844e94eSwiz log_debug("using utf8proc %s", utf8proc_version());
200e271dbb8Schristos #endif
201e271dbb8Schristos #ifdef NCURSES_VERSION
202*f844e94eSwiz log_debug("using ncurses %s %06u", NCURSES_VERSION, NCURSES_VERSION_PATCH);
203e271dbb8Schristos #endif
204ed4e6cd4Schristos
205ed4e6cd4Schristos tp = xcalloc(1, sizeof *tp);
206ed4e6cd4Schristos tp->name = xstrdup(name);
207e271dbb8Schristos TAILQ_INIT(&tp->peers);
208ed4e6cd4Schristos
209ed4e6cd4Schristos return (tp);
210ed4e6cd4Schristos }
211ed4e6cd4Schristos
212ed4e6cd4Schristos void
proc_loop(struct tmuxproc * tp,int (* loopcb)(void))213ed4e6cd4Schristos proc_loop(struct tmuxproc *tp, int (*loopcb)(void))
214ed4e6cd4Schristos {
215ed4e6cd4Schristos log_debug("%s loop enter", tp->name);
216ed4e6cd4Schristos do
217ed4e6cd4Schristos event_loop(EVLOOP_ONCE);
218ed4e6cd4Schristos while (!tp->exit && (loopcb == NULL || !loopcb ()));
219ed4e6cd4Schristos log_debug("%s loop exit", tp->name);
220ed4e6cd4Schristos }
221ed4e6cd4Schristos
222ed4e6cd4Schristos void
proc_exit(struct tmuxproc * tp)223ed4e6cd4Schristos proc_exit(struct tmuxproc *tp)
224ed4e6cd4Schristos {
225e271dbb8Schristos struct tmuxpeer *peer;
226e271dbb8Schristos
227e271dbb8Schristos TAILQ_FOREACH(peer, &tp->peers, entry)
228e271dbb8Schristos imsg_flush(&peer->ibuf);
229ed4e6cd4Schristos tp->exit = 1;
230ed4e6cd4Schristos }
231ed4e6cd4Schristos
232fe99a117Schristos void
proc_set_signals(struct tmuxproc * tp,void (* signalcb)(int))233fe99a117Schristos proc_set_signals(struct tmuxproc *tp, void (*signalcb)(int))
234fe99a117Schristos {
235fe99a117Schristos struct sigaction sa;
236fe99a117Schristos
237fe99a117Schristos tp->signalcb = signalcb;
238fe99a117Schristos
239fe99a117Schristos memset(&sa, 0, sizeof sa);
240fe99a117Schristos sigemptyset(&sa.sa_mask);
241fe99a117Schristos sa.sa_flags = SA_RESTART;
242fe99a117Schristos sa.sa_handler = SIG_IGN;
243fe99a117Schristos
244fe99a117Schristos sigaction(SIGPIPE, &sa, NULL);
245fe99a117Schristos sigaction(SIGTSTP, &sa, NULL);
246e271dbb8Schristos sigaction(SIGTTIN, &sa, NULL);
247e271dbb8Schristos sigaction(SIGTTOU, &sa, NULL);
248e271dbb8Schristos sigaction(SIGQUIT, &sa, NULL);
249fe99a117Schristos
250e271dbb8Schristos signal_set(&tp->ev_sigint, SIGINT, proc_signal_cb, tp);
251e271dbb8Schristos signal_add(&tp->ev_sigint, NULL);
252fe99a117Schristos signal_set(&tp->ev_sighup, SIGHUP, proc_signal_cb, tp);
253fe99a117Schristos signal_add(&tp->ev_sighup, NULL);
254fe99a117Schristos signal_set(&tp->ev_sigchld, SIGCHLD, proc_signal_cb, tp);
255fe99a117Schristos signal_add(&tp->ev_sigchld, NULL);
256fe99a117Schristos signal_set(&tp->ev_sigcont, SIGCONT, proc_signal_cb, tp);
257fe99a117Schristos signal_add(&tp->ev_sigcont, NULL);
258fe99a117Schristos signal_set(&tp->ev_sigterm, SIGTERM, proc_signal_cb, tp);
259fe99a117Schristos signal_add(&tp->ev_sigterm, NULL);
260fe99a117Schristos signal_set(&tp->ev_sigusr1, SIGUSR1, proc_signal_cb, tp);
261fe99a117Schristos signal_add(&tp->ev_sigusr1, NULL);
262fe99a117Schristos signal_set(&tp->ev_sigusr2, SIGUSR2, proc_signal_cb, tp);
263fe99a117Schristos signal_add(&tp->ev_sigusr2, NULL);
264fe99a117Schristos signal_set(&tp->ev_sigwinch, SIGWINCH, proc_signal_cb, tp);
265fe99a117Schristos signal_add(&tp->ev_sigwinch, NULL);
266fe99a117Schristos }
267fe99a117Schristos
268fe99a117Schristos void
proc_clear_signals(struct tmuxproc * tp,int defaults)269fe99a117Schristos proc_clear_signals(struct tmuxproc *tp, int defaults)
270fe99a117Schristos {
271fe99a117Schristos struct sigaction sa;
272fe99a117Schristos
273fe99a117Schristos memset(&sa, 0, sizeof sa);
274fe99a117Schristos sigemptyset(&sa.sa_mask);
275fe99a117Schristos sa.sa_flags = SA_RESTART;
276fe99a117Schristos sa.sa_handler = SIG_DFL;
277fe99a117Schristos
278fe99a117Schristos sigaction(SIGPIPE, &sa, NULL);
279fe99a117Schristos sigaction(SIGTSTP, &sa, NULL);
280fe99a117Schristos
281e271dbb8Schristos signal_del(&tp->ev_sigint);
282fe99a117Schristos signal_del(&tp->ev_sighup);
283fe99a117Schristos signal_del(&tp->ev_sigchld);
284fe99a117Schristos signal_del(&tp->ev_sigcont);
285fe99a117Schristos signal_del(&tp->ev_sigterm);
286fe99a117Schristos signal_del(&tp->ev_sigusr1);
287fe99a117Schristos signal_del(&tp->ev_sigusr2);
288fe99a117Schristos signal_del(&tp->ev_sigwinch);
289fe99a117Schristos
290fe99a117Schristos if (defaults) {
291e271dbb8Schristos sigaction(SIGINT, &sa, NULL);
292e271dbb8Schristos sigaction(SIGQUIT, &sa, NULL);
293fe99a117Schristos sigaction(SIGHUP, &sa, NULL);
294fe99a117Schristos sigaction(SIGCHLD, &sa, NULL);
295fe99a117Schristos sigaction(SIGCONT, &sa, NULL);
296fe99a117Schristos sigaction(SIGTERM, &sa, NULL);
297fe99a117Schristos sigaction(SIGUSR1, &sa, NULL);
298fe99a117Schristos sigaction(SIGUSR2, &sa, NULL);
299fe99a117Schristos sigaction(SIGWINCH, &sa, NULL);
300fe99a117Schristos }
301fe99a117Schristos }
302fe99a117Schristos
303ed4e6cd4Schristos struct tmuxpeer *
proc_add_peer(struct tmuxproc * tp,int fd,void (* dispatchcb)(struct imsg *,void *),void * arg)304ed4e6cd4Schristos proc_add_peer(struct tmuxproc *tp, int fd,
305ed4e6cd4Schristos void (*dispatchcb)(struct imsg *, void *), void *arg)
306ed4e6cd4Schristos {
307ed4e6cd4Schristos struct tmuxpeer *peer;
30846548964Swiz gid_t gid;
309ed4e6cd4Schristos
310ed4e6cd4Schristos peer = xcalloc(1, sizeof *peer);
311ed4e6cd4Schristos peer->parent = tp;
312ed4e6cd4Schristos
313ed4e6cd4Schristos peer->dispatchcb = dispatchcb;
314ed4e6cd4Schristos peer->arg = arg;
315ed4e6cd4Schristos
316ed4e6cd4Schristos imsg_init(&peer->ibuf, fd);
317ed4e6cd4Schristos event_set(&peer->event, fd, EV_READ, proc_event_cb, peer);
318ed4e6cd4Schristos
31946548964Swiz if (getpeereid(fd, &peer->uid, &gid) != 0)
32046548964Swiz peer->uid = (uid_t)-1;
32146548964Swiz
322ed4e6cd4Schristos log_debug("add peer %p: %d (%p)", peer, fd, arg);
323e271dbb8Schristos TAILQ_INSERT_TAIL(&tp->peers, peer, entry);
324ed4e6cd4Schristos
325ed4e6cd4Schristos proc_update_event(peer);
326ed4e6cd4Schristos return (peer);
327ed4e6cd4Schristos }
328ed4e6cd4Schristos
329ed4e6cd4Schristos void
proc_remove_peer(struct tmuxpeer * peer)330ed4e6cd4Schristos proc_remove_peer(struct tmuxpeer *peer)
331ed4e6cd4Schristos {
332e271dbb8Schristos TAILQ_REMOVE(&peer->parent->peers, peer, entry);
333ed4e6cd4Schristos log_debug("remove peer %p", peer);
334ed4e6cd4Schristos
335ed4e6cd4Schristos event_del(&peer->event);
336ed4e6cd4Schristos imsg_clear(&peer->ibuf);
337ed4e6cd4Schristos
338ed4e6cd4Schristos close(peer->ibuf.fd);
339ed4e6cd4Schristos free(peer);
340ed4e6cd4Schristos }
341ed4e6cd4Schristos
342ed4e6cd4Schristos void
proc_kill_peer(struct tmuxpeer * peer)343ed4e6cd4Schristos proc_kill_peer(struct tmuxpeer *peer)
344ed4e6cd4Schristos {
345ed4e6cd4Schristos peer->flags |= PEER_BAD;
346ed4e6cd4Schristos }
347fe99a117Schristos
348fe99a117Schristos void
proc_flush_peer(struct tmuxpeer * peer)34946548964Swiz proc_flush_peer(struct tmuxpeer *peer)
35046548964Swiz {
35146548964Swiz imsg_flush(&peer->ibuf);
35246548964Swiz }
35346548964Swiz
35446548964Swiz void
proc_toggle_log(struct tmuxproc * tp)355fe99a117Schristos proc_toggle_log(struct tmuxproc *tp)
356fe99a117Schristos {
357fe99a117Schristos log_toggle(tp->name);
358fe99a117Schristos }
359e271dbb8Schristos
360e271dbb8Schristos pid_t
proc_fork_and_daemon(int * fd)361e271dbb8Schristos proc_fork_and_daemon(int *fd)
362e271dbb8Schristos {
363e271dbb8Schristos pid_t pid;
364e271dbb8Schristos int pair[2];
365e271dbb8Schristos
366e271dbb8Schristos if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
367e271dbb8Schristos fatal("socketpair failed");
368e271dbb8Schristos switch (pid = fork()) {
369e271dbb8Schristos case -1:
370e271dbb8Schristos fatal("fork failed");
371e271dbb8Schristos case 0:
372e271dbb8Schristos close(pair[0]);
373e271dbb8Schristos *fd = pair[1];
374e271dbb8Schristos if (daemon(1, 0) != 0)
375e271dbb8Schristos fatal("daemon failed");
376e271dbb8Schristos return (0);
377e271dbb8Schristos default:
378e271dbb8Schristos close(pair[1]);
379e271dbb8Schristos *fd = pair[0];
380e271dbb8Schristos return (pid);
381e271dbb8Schristos }
382e271dbb8Schristos }
38346548964Swiz
38446548964Swiz uid_t
proc_get_peer_uid(struct tmuxpeer * peer)38546548964Swiz proc_get_peer_uid(struct tmuxpeer *peer)
38646548964Swiz {
38746548964Swiz return (peer->uid);
38846548964Swiz }
389