xref: /netbsd-src/external/bsd/tmux/dist/proc.c (revision f844e94ef29eebc7999c12636b87f541bb86868b)
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 	uid_t		 uid;
60 
61 	int		 flags;
62 #define PEER_BAD 0x1
63 
64 	void		(*dispatchcb)(struct imsg *, void *);
65 	void		 *arg;
66 
67 	TAILQ_ENTRY(tmuxpeer) entry;
68 };
69 
70 static int	peer_check_version(struct tmuxpeer *, struct imsg *);
71 static void	proc_update_event(struct tmuxpeer *);
72 
73 static void
proc_event_cb(__unused int fd,short events,void * arg)74 proc_event_cb(__unused int fd, short events, void *arg)
75 {
76 	struct tmuxpeer	*peer = arg;
77 	ssize_t		 n;
78 	struct imsg	 imsg;
79 
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;
85 		}
86 		for (;;) {
87 			if ((n = imsg_get(&peer->ibuf, &imsg)) == -1) {
88 				peer->dispatchcb(NULL, peer->arg);
89 				return;
90 			}
91 			if (n == 0)
92 				break;
93 			log_debug("peer %p message %d", peer, imsg.hdr.type);
94 
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;
101 			}
102 
103 			peer->dispatchcb(&imsg, peer->arg);
104 			imsg_free(&imsg);
105 		}
106 	}
107 
108 	if (events & EV_WRITE) {
109 		if (msgbuf_write(&peer->ibuf.w) <= 0 && errno != EAGAIN) {
110 			peer->dispatchcb(NULL, peer->arg);
111 			return;
112 		}
113 	}
114 
115 	if ((peer->flags & PEER_BAD) && peer->ibuf.w.queued == 0) {
116 		peer->dispatchcb(NULL, peer->arg);
117 		return;
118 	}
119 
120 	proc_update_event(peer);
121 }
122 
123 static void
proc_signal_cb(int signo,__unused short events,void * arg)124 proc_signal_cb(int signo, __unused short events, void *arg)
125 {
126 	struct tmuxproc	*tp = arg;
127 
128 	tp->signalcb(signo);
129 }
130 
131 static int
peer_check_version(struct tmuxpeer * peer,struct imsg * imsg)132 peer_check_version(struct tmuxpeer *peer, struct imsg *imsg)
133 {
134 	int	version;
135 
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);
139 
140 		proc_send(peer, MSG_VERSION, -1, NULL, 0);
141 		peer->flags |= PEER_BAD;
142 
143 		return (-1);
144 	}
145 	return (0);
146 }
147 
148 static void
proc_update_event(struct tmuxpeer * peer)149 proc_update_event(struct tmuxpeer *peer)
150 {
151 	short	events;
152 
153 	event_del(&peer->event);
154 
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);
159 
160 	event_add(&peer->event, NULL);
161 }
162 
163 int
proc_send(struct tmuxpeer * peer,enum msgtype type,int fd,const void * buf,size_t len)164 proc_send(struct tmuxpeer *peer, enum msgtype type, int fd, const void *buf,
165     size_t len)
166 {
167 	struct imsgbuf	*ibuf = &peer->ibuf;
168 	void		*vp = __UNCONST(buf);
169 	int		 retval;
170 
171 	if (peer->flags & PEER_BAD)
172 		return (-1);
173 	log_debug("sending message %d to peer %p (%zu bytes)", type, peer, len);
174 
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);
180 }
181 
182 struct tmuxproc *
proc_start(const char * name)183 proc_start(const char *name)
184 {
185 	struct tmuxproc	*tp;
186 	struct utsname	 u;
187 
188 	log_open(name);
189 	setproctitle("%s (%s)", name, socket_path);
190 
191 	if (uname(&u) < 0)
192 		memset(&u, 0, sizeof u);
193 
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
204 
205 	tp = xcalloc(1, sizeof *tp);
206 	tp->name = xstrdup(name);
207 	TAILQ_INIT(&tp->peers);
208 
209 	return (tp);
210 }
211 
212 void
proc_loop(struct tmuxproc * tp,int (* loopcb)(void))213 proc_loop(struct tmuxproc *tp, int (*loopcb)(void))
214 {
215 	log_debug("%s loop enter", tp->name);
216 	do
217 		event_loop(EVLOOP_ONCE);
218 	while (!tp->exit && (loopcb == NULL || !loopcb ()));
219 	log_debug("%s loop exit", tp->name);
220 }
221 
222 void
proc_exit(struct tmuxproc * tp)223 proc_exit(struct tmuxproc *tp)
224 {
225 	struct tmuxpeer	*peer;
226 
227 	TAILQ_FOREACH(peer, &tp->peers, entry)
228 	    imsg_flush(&peer->ibuf);
229 	tp->exit = 1;
230 }
231 
232 void
proc_set_signals(struct tmuxproc * tp,void (* signalcb)(int))233 proc_set_signals(struct tmuxproc *tp, void (*signalcb)(int))
234 {
235 	struct sigaction	sa;
236 
237 	tp->signalcb = signalcb;
238 
239 	memset(&sa, 0, sizeof sa);
240 	sigemptyset(&sa.sa_mask);
241 	sa.sa_flags = SA_RESTART;
242 	sa.sa_handler = SIG_IGN;
243 
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);
249 
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);
266 }
267 
268 void
proc_clear_signals(struct tmuxproc * tp,int defaults)269 proc_clear_signals(struct tmuxproc *tp, int defaults)
270 {
271 	struct sigaction	sa;
272 
273 	memset(&sa, 0, sizeof sa);
274 	sigemptyset(&sa.sa_mask);
275 	sa.sa_flags = SA_RESTART;
276 	sa.sa_handler = SIG_DFL;
277 
278 	sigaction(SIGPIPE, &sa, NULL);
279 	sigaction(SIGTSTP, &sa, NULL);
280 
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);
289 
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);
300 	}
301 }
302 
303 struct tmuxpeer *
proc_add_peer(struct tmuxproc * tp,int fd,void (* dispatchcb)(struct imsg *,void *),void * arg)304 proc_add_peer(struct tmuxproc *tp, int fd,
305     void (*dispatchcb)(struct imsg *, void *), void *arg)
306 {
307 	struct tmuxpeer	*peer;
308 	gid_t		 gid;
309 
310 	peer = xcalloc(1, sizeof *peer);
311 	peer->parent = tp;
312 
313 	peer->dispatchcb = dispatchcb;
314 	peer->arg = arg;
315 
316 	imsg_init(&peer->ibuf, fd);
317 	event_set(&peer->event, fd, EV_READ, proc_event_cb, peer);
318 
319 	if (getpeereid(fd, &peer->uid, &gid) != 0)
320 		peer->uid = (uid_t)-1;
321 
322 	log_debug("add peer %p: %d (%p)", peer, fd, arg);
323 	TAILQ_INSERT_TAIL(&tp->peers, peer, entry);
324 
325 	proc_update_event(peer);
326 	return (peer);
327 }
328 
329 void
proc_remove_peer(struct tmuxpeer * peer)330 proc_remove_peer(struct tmuxpeer *peer)
331 {
332 	TAILQ_REMOVE(&peer->parent->peers, peer, entry);
333 	log_debug("remove peer %p", peer);
334 
335 	event_del(&peer->event);
336 	imsg_clear(&peer->ibuf);
337 
338 	close(peer->ibuf.fd);
339 	free(peer);
340 }
341 
342 void
proc_kill_peer(struct tmuxpeer * peer)343 proc_kill_peer(struct tmuxpeer *peer)
344 {
345 	peer->flags |= PEER_BAD;
346 }
347 
348 void
proc_flush_peer(struct tmuxpeer * peer)349 proc_flush_peer(struct tmuxpeer *peer)
350 {
351 	imsg_flush(&peer->ibuf);
352 }
353 
354 void
proc_toggle_log(struct tmuxproc * tp)355 proc_toggle_log(struct tmuxproc *tp)
356 {
357 	log_toggle(tp->name);
358 }
359 
360 pid_t
proc_fork_and_daemon(int * fd)361 proc_fork_and_daemon(int *fd)
362 {
363 	pid_t	pid;
364 	int	pair[2];
365 
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);
381 	}
382 }
383 
384 uid_t
proc_get_peer_uid(struct tmuxpeer * peer)385 proc_get_peer_uid(struct tmuxpeer *peer)
386 {
387 	return (peer->uid);
388 }
389