Merge branch 'obsd-master'
[tmux.git] / proc.c
blob7c4805f28819905d06b97130f2fb8798ce3d8a5f
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 fd = imsg_get_fd(&imsg);
97 if (fd != -1)
98 close(fd);
99 imsg_free(&imsg);
100 break;
103 peer->dispatchcb(&imsg, peer->arg);
104 imsg_free(&imsg);
108 if (events & EV_WRITE) {
109 if (msgbuf_write(&peer->ibuf.w) <= 0 && errno != EAGAIN) {
110 peer->dispatchcb(NULL, peer->arg);
111 return;
115 if ((peer->flags & PEER_BAD) && peer->ibuf.w.queued == 0) {
116 peer->dispatchcb(NULL, peer->arg);
117 return;
120 proc_update_event(peer);
123 static void
124 proc_signal_cb(int signo, __unused short events, void *arg)
126 struct tmuxproc *tp = arg;
128 tp->signalcb(signo);
131 static int
132 peer_check_version(struct tmuxpeer *peer, struct imsg *imsg)
134 int version;
136 version = imsg->hdr.peerid & 0xff;
137 if (imsg->hdr.type != MSG_VERSION && version != PROTOCOL_VERSION) {
138 log_debug("peer %p bad version %d", peer, version);
140 proc_send(peer, MSG_VERSION, -1, NULL, 0);
141 peer->flags |= PEER_BAD;
143 return (-1);
145 return (0);
148 static void
149 proc_update_event(struct tmuxpeer *peer)
151 short events;
153 event_del(&peer->event);
155 events = EV_READ;
156 if (peer->ibuf.w.queued > 0)
157 events |= EV_WRITE;
158 event_set(&peer->event, peer->ibuf.fd, events, proc_event_cb, peer);
160 event_add(&peer->event, NULL);
164 proc_send(struct tmuxpeer *peer, enum msgtype type, int fd, const void *buf,
165 size_t len)
167 struct imsgbuf *ibuf = &peer->ibuf;
168 void *vp = (void *)buf;
169 int retval;
171 if (peer->flags & PEER_BAD)
172 return (-1);
173 log_debug("sending message %d to peer %p (%zu bytes)", type, peer, len);
175 retval = imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, fd, vp, len);
176 if (retval != 1)
177 return (-1);
178 proc_update_event(peer);
179 return (0);
182 struct tmuxproc *
183 proc_start(const char *name)
185 struct tmuxproc *tp;
186 struct utsname u;
188 log_open(name);
189 setproctitle("%s (%s)", name, socket_path);
191 if (uname(&u) < 0)
192 memset(&u, 0, sizeof u);
194 log_debug("%s started (%ld): version %s, socket %s, protocol %d", name,
195 (long)getpid(), getversion(), socket_path, PROTOCOL_VERSION);
196 log_debug("on %s %s %s", u.sysname, u.release, u.version);
197 log_debug("using libevent %s %s", event_get_version(), event_get_method());
198 #ifdef HAVE_UTF8PROC
199 log_debug("using utf8proc %s", utf8proc_version());
200 #endif
201 #ifdef NCURSES_VERSION
202 log_debug("using ncurses %s %06u", NCURSES_VERSION, NCURSES_VERSION_PATCH);
203 #endif
205 tp = xcalloc(1, sizeof *tp);
206 tp->name = xstrdup(name);
207 TAILQ_INIT(&tp->peers);
209 return (tp);
212 void
213 proc_loop(struct tmuxproc *tp, int (*loopcb)(void))
215 log_debug("%s loop enter", tp->name);
217 event_loop(EVLOOP_ONCE);
218 while (!tp->exit && (loopcb == NULL || !loopcb ()));
219 log_debug("%s loop exit", tp->name);
222 void
223 proc_exit(struct tmuxproc *tp)
225 struct tmuxpeer *peer;
227 TAILQ_FOREACH(peer, &tp->peers, entry)
228 imsg_flush(&peer->ibuf);
229 tp->exit = 1;
232 void
233 proc_set_signals(struct tmuxproc *tp, void (*signalcb)(int))
235 struct sigaction sa;
237 tp->signalcb = signalcb;
239 memset(&sa, 0, sizeof sa);
240 sigemptyset(&sa.sa_mask);
241 sa.sa_flags = SA_RESTART;
242 sa.sa_handler = SIG_IGN;
244 sigaction(SIGPIPE, &sa, NULL);
245 sigaction(SIGTSTP, &sa, NULL);
246 sigaction(SIGTTIN, &sa, NULL);
247 sigaction(SIGTTOU, &sa, NULL);
248 sigaction(SIGQUIT, &sa, NULL);
250 signal_set(&tp->ev_sigint, SIGINT, proc_signal_cb, tp);
251 signal_add(&tp->ev_sigint, NULL);
252 signal_set(&tp->ev_sighup, SIGHUP, proc_signal_cb, tp);
253 signal_add(&tp->ev_sighup, NULL);
254 signal_set(&tp->ev_sigchld, SIGCHLD, proc_signal_cb, tp);
255 signal_add(&tp->ev_sigchld, NULL);
256 signal_set(&tp->ev_sigcont, SIGCONT, proc_signal_cb, tp);
257 signal_add(&tp->ev_sigcont, NULL);
258 signal_set(&tp->ev_sigterm, SIGTERM, proc_signal_cb, tp);
259 signal_add(&tp->ev_sigterm, NULL);
260 signal_set(&tp->ev_sigusr1, SIGUSR1, proc_signal_cb, tp);
261 signal_add(&tp->ev_sigusr1, NULL);
262 signal_set(&tp->ev_sigusr2, SIGUSR2, proc_signal_cb, tp);
263 signal_add(&tp->ev_sigusr2, NULL);
264 signal_set(&tp->ev_sigwinch, SIGWINCH, proc_signal_cb, tp);
265 signal_add(&tp->ev_sigwinch, NULL);
268 void
269 proc_clear_signals(struct tmuxproc *tp, int defaults)
271 struct sigaction sa;
273 memset(&sa, 0, sizeof sa);
274 sigemptyset(&sa.sa_mask);
275 sa.sa_flags = SA_RESTART;
276 sa.sa_handler = SIG_DFL;
278 sigaction(SIGPIPE, &sa, NULL);
279 sigaction(SIGTSTP, &sa, NULL);
281 signal_del(&tp->ev_sigint);
282 signal_del(&tp->ev_sighup);
283 signal_del(&tp->ev_sigchld);
284 signal_del(&tp->ev_sigcont);
285 signal_del(&tp->ev_sigterm);
286 signal_del(&tp->ev_sigusr1);
287 signal_del(&tp->ev_sigusr2);
288 signal_del(&tp->ev_sigwinch);
290 if (defaults) {
291 sigaction(SIGINT, &sa, NULL);
292 sigaction(SIGQUIT, &sa, NULL);
293 sigaction(SIGHUP, &sa, NULL);
294 sigaction(SIGCHLD, &sa, NULL);
295 sigaction(SIGCONT, &sa, NULL);
296 sigaction(SIGTERM, &sa, NULL);
297 sigaction(SIGUSR1, &sa, NULL);
298 sigaction(SIGUSR2, &sa, NULL);
299 sigaction(SIGWINCH, &sa, NULL);
303 struct tmuxpeer *
304 proc_add_peer(struct tmuxproc *tp, int fd,
305 void (*dispatchcb)(struct imsg *, void *), void *arg)
307 struct tmuxpeer *peer;
308 gid_t gid;
310 peer = xcalloc(1, sizeof *peer);
311 peer->parent = tp;
313 peer->dispatchcb = dispatchcb;
314 peer->arg = arg;
316 imsg_init(&peer->ibuf, fd);
317 event_set(&peer->event, fd, EV_READ, proc_event_cb, peer);
319 if (getpeereid(fd, &peer->uid, &gid) != 0)
320 peer->uid = (uid_t)-1;
322 log_debug("add peer %p: %d (%p)", peer, fd, arg);
323 TAILQ_INSERT_TAIL(&tp->peers, peer, entry);
325 proc_update_event(peer);
326 return (peer);
329 void
330 proc_remove_peer(struct tmuxpeer *peer)
332 TAILQ_REMOVE(&peer->parent->peers, peer, entry);
333 log_debug("remove peer %p", peer);
335 event_del(&peer->event);
336 imsg_clear(&peer->ibuf);
338 close(peer->ibuf.fd);
339 free(peer);
342 void
343 proc_kill_peer(struct tmuxpeer *peer)
345 peer->flags |= PEER_BAD;
348 void
349 proc_flush_peer(struct tmuxpeer *peer)
351 imsg_flush(&peer->ibuf);
354 void
355 proc_toggle_log(struct tmuxproc *tp)
357 log_toggle(tp->name);
360 pid_t
361 proc_fork_and_daemon(int *fd)
363 pid_t pid;
364 int pair[2];
366 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
367 fatal("socketpair failed");
368 switch (pid = fork()) {
369 case -1:
370 fatal("fork failed");
371 case 0:
372 close(pair[0]);
373 *fd = pair[1];
374 if (daemon(1, 0) != 0)
375 fatal("daemon failed");
376 return (0);
377 default:
378 close(pair[1]);
379 *fd = pair[0];
380 return (pid);
384 uid_t
385 proc_get_peer_uid(struct tmuxpeer *peer)
387 return (peer->uid);