Merge branch 'obsd-master'
[tmux.git] / proc.c
blob30eedadb41f0a720f87f3eeec6a2136b8fe10d1b
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2015 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>
21 #include <sys/uio.h>
22 #include <sys/utsname.h>
24 #include <errno.h>
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #if defined(HAVE_NCURSES_H)
31 #include <ncurses.h>
32 #endif
34 #include "tmux.h"
36 struct tmuxproc {
37 const char *name;
38 int exit;
40 void (*signalcb)(int);
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;
51 TAILQ_HEAD(, tmuxpeer) peers;
54 struct tmuxpeer {
55 struct tmuxproc *parent;
57 struct imsgbuf ibuf;
58 struct event event;
59 uid_t uid;
61 int flags;
62 #define PEER_BAD 0x1
64 void (*dispatchcb)(struct imsg *, void *);
65 void *arg;
67 TAILQ_ENTRY(tmuxpeer) entry;
70 static int peer_check_version(struct tmuxpeer *, struct imsg *);
71 static void proc_update_event(struct tmuxpeer *);
73 static void
74 proc_event_cb(__unused int fd, short events, void *arg)
76 struct tmuxpeer *peer = arg;
77 ssize_t n;
78 struct imsg imsg;
80 if (!(peer->flags & PEER_BAD) && (events & EV_READ)) {
81 if (((n = imsg_read(&peer->ibuf)) == -1 && errno != EAGAIN) ||
82 n == 0) {
83 peer->dispatchcb(NULL, peer->arg);
84 return;
86 for (;;) {
87 if ((n = imsg_get(&peer->ibuf, &imsg)) == -1) {
88 peer->dispatchcb(NULL, peer->arg);
89 return;
91 if (n == 0)
92 break;
93 log_debug("peer %p message %d", peer, imsg.hdr.type);
95 if (peer_check_version(peer, &imsg) != 0) {
96 if (imsg.fd != -1)
97 close(imsg.fd);
98 imsg_free(&imsg);
99 break;
102 peer->dispatchcb(&imsg, peer->arg);
103 imsg_free(&imsg);
107 if (events & EV_WRITE) {
108 if (msgbuf_write(&peer->ibuf.w) <= 0 && errno != EAGAIN) {
109 peer->dispatchcb(NULL, peer->arg);
110 return;
114 if ((peer->flags & PEER_BAD) && peer->ibuf.w.queued == 0) {
115 peer->dispatchcb(NULL, peer->arg);
116 return;
119 proc_update_event(peer);
122 static void
123 proc_signal_cb(int signo, __unused short events, void *arg)
125 struct tmuxproc *tp = arg;
127 tp->signalcb(signo);
130 static int
131 peer_check_version(struct tmuxpeer *peer, struct imsg *imsg)
133 int version;
135 version = imsg->hdr.peerid & 0xff;
136 if (imsg->hdr.type != MSG_VERSION && version != PROTOCOL_VERSION) {
137 log_debug("peer %p bad version %d", peer, version);
139 proc_send(peer, MSG_VERSION, -1, NULL, 0);
140 peer->flags |= PEER_BAD;
142 return (-1);
144 return (0);
147 static void
148 proc_update_event(struct tmuxpeer *peer)
150 short events;
152 event_del(&peer->event);
154 events = EV_READ;
155 if (peer->ibuf.w.queued > 0)
156 events |= EV_WRITE;
157 event_set(&peer->event, peer->ibuf.fd, events, proc_event_cb, peer);
159 event_add(&peer->event, NULL);
163 proc_send(struct tmuxpeer *peer, enum msgtype type, int fd, const void *buf,
164 size_t len)
166 struct imsgbuf *ibuf = &peer->ibuf;
167 void *vp = (void *)buf;
168 int retval;
170 if (peer->flags & PEER_BAD)
171 return (-1);
172 log_debug("sending message %d to peer %p (%zu bytes)", type, peer, len);
174 retval = imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, fd, vp, len);
175 if (retval != 1)
176 return (-1);
177 proc_update_event(peer);
178 return (0);
181 struct tmuxproc *
182 proc_start(const char *name)
184 struct tmuxproc *tp;
185 struct utsname u;
187 log_open(name);
188 setproctitle("%s (%s)", name, socket_path);
190 if (uname(&u) < 0)
191 memset(&u, 0, sizeof u);
193 log_debug("%s started (%ld): version %s, socket %s, protocol %d", name,
194 (long)getpid(), getversion(), socket_path, PROTOCOL_VERSION);
195 log_debug("on %s %s %s", u.sysname, u.release, u.version);
196 log_debug("using libevent %s %s", event_get_version(), event_get_method());
197 #ifdef HAVE_UTF8PROC
198 log_debug("using utf8proc %s", utf8proc_version());
199 #endif
200 #ifdef NCURSES_VERSION
201 log_debug("using ncurses %s %06u", NCURSES_VERSION, NCURSES_VERSION_PATCH);
202 #endif
204 tp = xcalloc(1, sizeof *tp);
205 tp->name = xstrdup(name);
206 TAILQ_INIT(&tp->peers);
208 return (tp);
211 void
212 proc_loop(struct tmuxproc *tp, int (*loopcb)(void))
214 log_debug("%s loop enter", tp->name);
216 event_loop(EVLOOP_ONCE);
217 while (!tp->exit && (loopcb == NULL || !loopcb ()));
218 log_debug("%s loop exit", tp->name);
221 void
222 proc_exit(struct tmuxproc *tp)
224 struct tmuxpeer *peer;
226 TAILQ_FOREACH(peer, &tp->peers, entry)
227 imsg_flush(&peer->ibuf);
228 tp->exit = 1;
231 void
232 proc_set_signals(struct tmuxproc *tp, void (*signalcb)(int))
234 struct sigaction sa;
236 tp->signalcb = signalcb;
238 memset(&sa, 0, sizeof sa);
239 sigemptyset(&sa.sa_mask);
240 sa.sa_flags = SA_RESTART;
241 sa.sa_handler = SIG_IGN;
243 sigaction(SIGPIPE, &sa, NULL);
244 sigaction(SIGTSTP, &sa, NULL);
245 sigaction(SIGTTIN, &sa, NULL);
246 sigaction(SIGTTOU, &sa, NULL);
247 sigaction(SIGQUIT, &sa, NULL);
249 signal_set(&tp->ev_sigint, SIGINT, proc_signal_cb, tp);
250 signal_add(&tp->ev_sigint, NULL);
251 signal_set(&tp->ev_sighup, SIGHUP, proc_signal_cb, tp);
252 signal_add(&tp->ev_sighup, NULL);
253 signal_set(&tp->ev_sigchld, SIGCHLD, proc_signal_cb, tp);
254 signal_add(&tp->ev_sigchld, NULL);
255 signal_set(&tp->ev_sigcont, SIGCONT, proc_signal_cb, tp);
256 signal_add(&tp->ev_sigcont, NULL);
257 signal_set(&tp->ev_sigterm, SIGTERM, proc_signal_cb, tp);
258 signal_add(&tp->ev_sigterm, NULL);
259 signal_set(&tp->ev_sigusr1, SIGUSR1, proc_signal_cb, tp);
260 signal_add(&tp->ev_sigusr1, NULL);
261 signal_set(&tp->ev_sigusr2, SIGUSR2, proc_signal_cb, tp);
262 signal_add(&tp->ev_sigusr2, NULL);
263 signal_set(&tp->ev_sigwinch, SIGWINCH, proc_signal_cb, tp);
264 signal_add(&tp->ev_sigwinch, NULL);
267 void
268 proc_clear_signals(struct tmuxproc *tp, int defaults)
270 struct sigaction sa;
272 memset(&sa, 0, sizeof sa);
273 sigemptyset(&sa.sa_mask);
274 sa.sa_flags = SA_RESTART;
275 sa.sa_handler = SIG_DFL;
277 sigaction(SIGPIPE, &sa, NULL);
278 sigaction(SIGTSTP, &sa, NULL);
280 signal_del(&tp->ev_sigint);
281 signal_del(&tp->ev_sighup);
282 signal_del(&tp->ev_sigchld);
283 signal_del(&tp->ev_sigcont);
284 signal_del(&tp->ev_sigterm);
285 signal_del(&tp->ev_sigusr1);
286 signal_del(&tp->ev_sigusr2);
287 signal_del(&tp->ev_sigwinch);
289 if (defaults) {
290 sigaction(SIGINT, &sa, NULL);
291 sigaction(SIGQUIT, &sa, NULL);
292 sigaction(SIGHUP, &sa, NULL);
293 sigaction(SIGCHLD, &sa, NULL);
294 sigaction(SIGCONT, &sa, NULL);
295 sigaction(SIGTERM, &sa, NULL);
296 sigaction(SIGUSR1, &sa, NULL);
297 sigaction(SIGUSR2, &sa, NULL);
298 sigaction(SIGWINCH, &sa, NULL);
302 struct tmuxpeer *
303 proc_add_peer(struct tmuxproc *tp, int fd,
304 void (*dispatchcb)(struct imsg *, void *), void *arg)
306 struct tmuxpeer *peer;
307 gid_t gid;
309 peer = xcalloc(1, sizeof *peer);
310 peer->parent = tp;
312 peer->dispatchcb = dispatchcb;
313 peer->arg = arg;
315 imsg_init(&peer->ibuf, fd);
316 event_set(&peer->event, fd, EV_READ, proc_event_cb, peer);
318 if (getpeereid(fd, &peer->uid, &gid) != 0)
319 peer->uid = (uid_t)-1;
321 log_debug("add peer %p: %d (%p)", peer, fd, arg);
322 TAILQ_INSERT_TAIL(&tp->peers, peer, entry);
324 proc_update_event(peer);
325 return (peer);
328 void
329 proc_remove_peer(struct tmuxpeer *peer)
331 TAILQ_REMOVE(&peer->parent->peers, peer, entry);
332 log_debug("remove peer %p", peer);
334 event_del(&peer->event);
335 imsg_clear(&peer->ibuf);
337 close(peer->ibuf.fd);
338 free(peer);
341 void
342 proc_kill_peer(struct tmuxpeer *peer)
344 peer->flags |= PEER_BAD;
347 void
348 proc_flush_peer(struct tmuxpeer *peer)
350 imsg_flush(&peer->ibuf);
353 void
354 proc_toggle_log(struct tmuxproc *tp)
356 log_toggle(tp->name);
359 pid_t
360 proc_fork_and_daemon(int *fd)
362 pid_t pid;
363 int pair[2];
365 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
366 fatal("socketpair failed");
367 switch (pid = fork()) {
368 case -1:
369 fatal("fork failed");
370 case 0:
371 close(pair[0]);
372 *fd = pair[1];
373 if (daemon(1, 0) != 0)
374 fatal("daemon failed");
375 return (0);
376 default:
377 close(pair[1]);
378 *fd = pair[0];
379 return (pid);
383 uid_t
384 proc_get_peer_uid(struct tmuxpeer *peer)
386 return (peer->uid);