Do not try to strdup NULL, from seL4 at disroot dot org in GitHub issue
[tmux-openbsd.git] / proc.c
blob9412cec047b14ab62f123bd9e3bb9390a78382cb
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/queue.h>
21 #include <sys/socket.h>
22 #include <sys/uio.h>
23 #include <sys/utsname.h>
25 #include <errno.h>
26 #include <event.h>
27 #include <imsg.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include "tmux.h"
35 struct tmuxproc {
36 const char *name;
37 int exit;
39 void (*signalcb)(int);
41 struct event ev_sigint;
42 struct event ev_sighup;
43 struct event ev_sigchld;
44 struct event ev_sigcont;
45 struct event ev_sigterm;
46 struct event ev_sigusr1;
47 struct event ev_sigusr2;
48 struct event ev_sigwinch;
50 TAILQ_HEAD(, tmuxpeer) peers;
53 struct tmuxpeer {
54 struct tmuxproc *parent;
56 struct imsgbuf ibuf;
57 struct event event;
59 int flags;
60 #define PEER_BAD 0x1
62 void (*dispatchcb)(struct imsg *, void *);
63 void *arg;
65 TAILQ_ENTRY(tmuxpeer) entry;
68 static int peer_check_version(struct tmuxpeer *, struct imsg *);
69 static void proc_update_event(struct tmuxpeer *);
71 static void
72 proc_event_cb(__unused int fd, short events, void *arg)
74 struct tmuxpeer *peer = arg;
75 ssize_t n;
76 struct imsg imsg;
78 if (!(peer->flags & PEER_BAD) && (events & EV_READ)) {
79 if (((n = imsg_read(&peer->ibuf)) == -1 && errno != EAGAIN) ||
80 n == 0) {
81 peer->dispatchcb(NULL, peer->arg);
82 return;
84 for (;;) {
85 if ((n = imsg_get(&peer->ibuf, &imsg)) == -1) {
86 peer->dispatchcb(NULL, peer->arg);
87 return;
89 if (n == 0)
90 break;
91 log_debug("peer %p message %d", peer, imsg.hdr.type);
93 if (peer_check_version(peer, &imsg) != 0) {
94 if (imsg.fd != -1)
95 close(imsg.fd);
96 imsg_free(&imsg);
97 break;
100 peer->dispatchcb(&imsg, peer->arg);
101 imsg_free(&imsg);
105 if (events & EV_WRITE) {
106 if (msgbuf_write(&peer->ibuf.w) <= 0 && errno != EAGAIN) {
107 peer->dispatchcb(NULL, peer->arg);
108 return;
112 if ((peer->flags & PEER_BAD) && peer->ibuf.w.queued == 0) {
113 peer->dispatchcb(NULL, peer->arg);
114 return;
117 proc_update_event(peer);
120 static void
121 proc_signal_cb(int signo, __unused short events, void *arg)
123 struct tmuxproc *tp = arg;
125 tp->signalcb(signo);
128 static int
129 peer_check_version(struct tmuxpeer *peer, struct imsg *imsg)
131 int version;
133 version = imsg->hdr.peerid & 0xff;
134 if (imsg->hdr.type != MSG_VERSION && version != PROTOCOL_VERSION) {
135 log_debug("peer %p bad version %d", peer, version);
137 proc_send(peer, MSG_VERSION, -1, NULL, 0);
138 peer->flags |= PEER_BAD;
140 return (-1);
142 return (0);
145 static void
146 proc_update_event(struct tmuxpeer *peer)
148 short events;
150 event_del(&peer->event);
152 events = EV_READ;
153 if (peer->ibuf.w.queued > 0)
154 events |= EV_WRITE;
155 event_set(&peer->event, peer->ibuf.fd, events, proc_event_cb, peer);
157 event_add(&peer->event, NULL);
161 proc_send(struct tmuxpeer *peer, enum msgtype type, int fd, const void *buf,
162 size_t len)
164 struct imsgbuf *ibuf = &peer->ibuf;
165 void *vp = (void *)buf;
166 int retval;
168 if (peer->flags & PEER_BAD)
169 return (-1);
170 log_debug("sending message %d to peer %p (%zu bytes)", type, peer, len);
172 retval = imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, fd, vp, len);
173 if (retval != 1)
174 return (-1);
175 proc_update_event(peer);
176 return (0);
179 struct tmuxproc *
180 proc_start(const char *name)
182 struct tmuxproc *tp;
183 struct utsname u;
185 log_open(name);
186 setproctitle("%s (%s)", name, socket_path);
188 if (uname(&u) < 0)
189 memset(&u, 0, sizeof u);
191 log_debug("%s started (%ld): version %s, socket %s, protocol %d", name,
192 (long)getpid(), getversion(), socket_path, PROTOCOL_VERSION);
193 log_debug("on %s %s %s; libevent %s (%s)", u.sysname, u.release,
194 u.version, event_get_version(), event_get_method());
196 tp = xcalloc(1, sizeof *tp);
197 tp->name = xstrdup(name);
198 TAILQ_INIT(&tp->peers);
200 return (tp);
203 void
204 proc_loop(struct tmuxproc *tp, int (*loopcb)(void))
206 log_debug("%s loop enter", tp->name);
208 event_loop(EVLOOP_ONCE);
209 while (!tp->exit && (loopcb == NULL || !loopcb ()));
210 log_debug("%s loop exit", tp->name);
213 void
214 proc_exit(struct tmuxproc *tp)
216 struct tmuxpeer *peer;
218 TAILQ_FOREACH(peer, &tp->peers, entry)
219 imsg_flush(&peer->ibuf);
220 tp->exit = 1;
223 void
224 proc_set_signals(struct tmuxproc *tp, void (*signalcb)(int))
226 struct sigaction sa;
228 tp->signalcb = signalcb;
230 memset(&sa, 0, sizeof sa);
231 sigemptyset(&sa.sa_mask);
232 sa.sa_flags = SA_RESTART;
233 sa.sa_handler = SIG_IGN;
235 sigaction(SIGPIPE, &sa, NULL);
236 sigaction(SIGTSTP, &sa, NULL);
237 sigaction(SIGTTIN, &sa, NULL);
238 sigaction(SIGTTOU, &sa, NULL);
239 sigaction(SIGQUIT, &sa, NULL);
241 signal_set(&tp->ev_sigint, SIGINT, proc_signal_cb, tp);
242 signal_add(&tp->ev_sigint, NULL);
243 signal_set(&tp->ev_sighup, SIGHUP, proc_signal_cb, tp);
244 signal_add(&tp->ev_sighup, NULL);
245 signal_set(&tp->ev_sigchld, SIGCHLD, proc_signal_cb, tp);
246 signal_add(&tp->ev_sigchld, NULL);
247 signal_set(&tp->ev_sigcont, SIGCONT, proc_signal_cb, tp);
248 signal_add(&tp->ev_sigcont, NULL);
249 signal_set(&tp->ev_sigterm, SIGTERM, proc_signal_cb, tp);
250 signal_add(&tp->ev_sigterm, NULL);
251 signal_set(&tp->ev_sigusr1, SIGUSR1, proc_signal_cb, tp);
252 signal_add(&tp->ev_sigusr1, NULL);
253 signal_set(&tp->ev_sigusr2, SIGUSR2, proc_signal_cb, tp);
254 signal_add(&tp->ev_sigusr2, NULL);
255 signal_set(&tp->ev_sigwinch, SIGWINCH, proc_signal_cb, tp);
256 signal_add(&tp->ev_sigwinch, NULL);
259 void
260 proc_clear_signals(struct tmuxproc *tp, int defaults)
262 struct sigaction sa;
264 memset(&sa, 0, sizeof sa);
265 sigemptyset(&sa.sa_mask);
266 sa.sa_flags = SA_RESTART;
267 sa.sa_handler = SIG_DFL;
269 sigaction(SIGPIPE, &sa, NULL);
270 sigaction(SIGTSTP, &sa, NULL);
272 signal_del(&tp->ev_sigint);
273 signal_del(&tp->ev_sighup);
274 signal_del(&tp->ev_sigchld);
275 signal_del(&tp->ev_sigcont);
276 signal_del(&tp->ev_sigterm);
277 signal_del(&tp->ev_sigusr1);
278 signal_del(&tp->ev_sigusr2);
279 signal_del(&tp->ev_sigwinch);
281 if (defaults) {
282 sigaction(SIGINT, &sa, NULL);
283 sigaction(SIGQUIT, &sa, NULL);
284 sigaction(SIGHUP, &sa, NULL);
285 sigaction(SIGCHLD, &sa, NULL);
286 sigaction(SIGCONT, &sa, NULL);
287 sigaction(SIGTERM, &sa, NULL);
288 sigaction(SIGUSR1, &sa, NULL);
289 sigaction(SIGUSR2, &sa, NULL);
290 sigaction(SIGWINCH, &sa, NULL);
294 struct tmuxpeer *
295 proc_add_peer(struct tmuxproc *tp, int fd,
296 void (*dispatchcb)(struct imsg *, void *), void *arg)
298 struct tmuxpeer *peer;
300 peer = xcalloc(1, sizeof *peer);
301 peer->parent = tp;
303 peer->dispatchcb = dispatchcb;
304 peer->arg = arg;
306 imsg_init(&peer->ibuf, fd);
307 event_set(&peer->event, fd, EV_READ, proc_event_cb, peer);
309 log_debug("add peer %p: %d (%p)", peer, fd, arg);
310 TAILQ_INSERT_TAIL(&tp->peers, peer, entry);
312 proc_update_event(peer);
313 return (peer);
316 void
317 proc_remove_peer(struct tmuxpeer *peer)
319 TAILQ_REMOVE(&peer->parent->peers, peer, entry);
320 log_debug("remove peer %p", peer);
322 event_del(&peer->event);
323 imsg_clear(&peer->ibuf);
325 close(peer->ibuf.fd);
326 free(peer);
329 void
330 proc_kill_peer(struct tmuxpeer *peer)
332 peer->flags |= PEER_BAD;
335 void
336 proc_toggle_log(struct tmuxproc *tp)
338 log_toggle(tp->name);
341 pid_t
342 proc_fork_and_daemon(int *fd)
344 pid_t pid;
345 int pair[2];
347 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
348 fatal("socketpair failed");
349 switch (pid = fork()) {
350 case -1:
351 fatal("fork failed");
352 case 0:
353 close(pair[0]);
354 *fd = pair[1];
355 if (daemon(1, 0) != 0)
356 fatal("daemon failed");
357 return (0);
358 default:
359 close(pair[1]);
360 *fd = pair[0];
361 return (pid);