xref: /openbsd/usr.bin/tmux/proc.c (revision 0e59d0d1)
1 /* $OpenBSD: proc.c,v 1.30 2024/11/21 13:35:20 claudio Exp $ */
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/queue.h>
21 #include <sys/socket.h>
22 #include <sys/uio.h>
23 #include <sys/utsname.h>
24 
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>
32 
33 #include "tmux.h"
34 
35 struct tmuxproc {
36 	const char	 *name;
37 	int		  exit;
38 
39 	void		(*signalcb)(int);
40 
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;
49 
50 	TAILQ_HEAD(, tmuxpeer) peers;
51 };
52 
53 struct tmuxpeer {
54 	struct tmuxproc	*parent;
55 
56 	struct imsgbuf	 ibuf;
57 	struct event	 event;
58 	uid_t		 uid;
59 
60 	int		 flags;
61 #define PEER_BAD 0x1
62 
63 	void		(*dispatchcb)(struct imsg *, void *);
64 	void		 *arg;
65 
66 	TAILQ_ENTRY(tmuxpeer) entry;
67 };
68 
69 static int	peer_check_version(struct tmuxpeer *, struct imsg *);
70 static void	proc_update_event(struct tmuxpeer *);
71 
72 static void
proc_event_cb(__unused int fd,short events,void * arg)73 proc_event_cb(__unused int fd, short events, void *arg)
74 {
75 	struct tmuxpeer	*peer = arg;
76 	ssize_t		 n;
77 	struct imsg	 imsg;
78 
79 	if (!(peer->flags & PEER_BAD) && (events & EV_READ)) {
80 		if (imsgbuf_read(&peer->ibuf) != 1) {
81 			peer->dispatchcb(NULL, peer->arg);
82 			return;
83 		}
84 		for (;;) {
85 			if ((n = imsg_get(&peer->ibuf, &imsg)) == -1) {
86 				peer->dispatchcb(NULL, peer->arg);
87 				return;
88 			}
89 			if (n == 0)
90 				break;
91 			log_debug("peer %p message %d", peer, imsg.hdr.type);
92 
93 			if (peer_check_version(peer, &imsg) != 0) {
94 				fd = imsg_get_fd(&imsg);
95 				if (fd != -1)
96 					close(fd);
97 				imsg_free(&imsg);
98 				break;
99 			}
100 
101 			peer->dispatchcb(&imsg, peer->arg);
102 			imsg_free(&imsg);
103 		}
104 	}
105 
106 	if (events & EV_WRITE) {
107 		if (imsgbuf_write(&peer->ibuf) == -1) {
108 			peer->dispatchcb(NULL, peer->arg);
109 			return;
110 		}
111 	}
112 
113 	if ((peer->flags & PEER_BAD) && imsgbuf_queuelen(&peer->ibuf) == 0) {
114 		peer->dispatchcb(NULL, peer->arg);
115 		return;
116 	}
117 
118 	proc_update_event(peer);
119 }
120 
121 static void
proc_signal_cb(int signo,__unused short events,void * arg)122 proc_signal_cb(int signo, __unused short events, void *arg)
123 {
124 	struct tmuxproc	*tp = arg;
125 
126 	tp->signalcb(signo);
127 }
128 
129 static int
peer_check_version(struct tmuxpeer * peer,struct imsg * imsg)130 peer_check_version(struct tmuxpeer *peer, struct imsg *imsg)
131 {
132 	int	version;
133 
134 	version = imsg->hdr.peerid & 0xff;
135 	if (imsg->hdr.type != MSG_VERSION && version != PROTOCOL_VERSION) {
136 		log_debug("peer %p bad version %d", peer, version);
137 
138 		proc_send(peer, MSG_VERSION, -1, NULL, 0);
139 		peer->flags |= PEER_BAD;
140 
141 		return (-1);
142 	}
143 	return (0);
144 }
145 
146 static void
proc_update_event(struct tmuxpeer * peer)147 proc_update_event(struct tmuxpeer *peer)
148 {
149 	short	events;
150 
151 	event_del(&peer->event);
152 
153 	events = EV_READ;
154 	if (imsgbuf_queuelen(&peer->ibuf) > 0)
155 		events |= EV_WRITE;
156 	event_set(&peer->event, peer->ibuf.fd, events, proc_event_cb, peer);
157 
158 	event_add(&peer->event, NULL);
159 }
160 
161 int
proc_send(struct tmuxpeer * peer,enum msgtype type,int fd,const void * buf,size_t len)162 proc_send(struct tmuxpeer *peer, enum msgtype type, int fd, const void *buf,
163     size_t len)
164 {
165 	struct imsgbuf	*ibuf = &peer->ibuf;
166 	void		*vp = (void *)buf;
167 	int		 retval;
168 
169 	if (peer->flags & PEER_BAD)
170 		return (-1);
171 	log_debug("sending message %d to peer %p (%zu bytes)", type, peer, len);
172 
173 	retval = imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, fd, vp, len);
174 	if (retval != 1)
175 		return (-1);
176 	proc_update_event(peer);
177 	return (0);
178 }
179 
180 struct tmuxproc *
proc_start(const char * name)181 proc_start(const char *name)
182 {
183 	struct tmuxproc	*tp;
184 	struct utsname	 u;
185 
186 	log_open(name);
187 	setproctitle("%s (%s)", name, socket_path);
188 
189 	if (uname(&u) < 0)
190 		memset(&u, 0, sizeof u);
191 
192 	log_debug("%s started (%ld): version %s, socket %s, protocol %d", name,
193 	    (long)getpid(), getversion(), socket_path, PROTOCOL_VERSION);
194 	log_debug("on %s %s %s; libevent %s (%s)", u.sysname, u.release,
195 	    u.version, event_get_version(), event_get_method());
196 
197 	tp = xcalloc(1, sizeof *tp);
198 	tp->name = xstrdup(name);
199 	TAILQ_INIT(&tp->peers);
200 
201 	return (tp);
202 }
203 
204 void
proc_loop(struct tmuxproc * tp,int (* loopcb)(void))205 proc_loop(struct tmuxproc *tp, int (*loopcb)(void))
206 {
207 	log_debug("%s loop enter", tp->name);
208 	do
209 		event_loop(EVLOOP_ONCE);
210 	while (!tp->exit && (loopcb == NULL || !loopcb ()));
211 	log_debug("%s loop exit", tp->name);
212 }
213 
214 void
proc_exit(struct tmuxproc * tp)215 proc_exit(struct tmuxproc *tp)
216 {
217 	struct tmuxpeer	*peer;
218 
219 	TAILQ_FOREACH(peer, &tp->peers, entry)
220 	    imsgbuf_flush(&peer->ibuf);
221 	tp->exit = 1;
222 }
223 
224 void
proc_set_signals(struct tmuxproc * tp,void (* signalcb)(int))225 proc_set_signals(struct tmuxproc *tp, void (*signalcb)(int))
226 {
227 	struct sigaction	sa;
228 
229 	tp->signalcb = signalcb;
230 
231 	memset(&sa, 0, sizeof sa);
232 	sigemptyset(&sa.sa_mask);
233 	sa.sa_flags = SA_RESTART;
234 	sa.sa_handler = SIG_IGN;
235 
236 	sigaction(SIGPIPE, &sa, NULL);
237 	sigaction(SIGTSTP, &sa, NULL);
238 	sigaction(SIGTTIN, &sa, NULL);
239 	sigaction(SIGTTOU, &sa, NULL);
240 	sigaction(SIGQUIT, &sa, NULL);
241 
242 	signal_set(&tp->ev_sigint, SIGINT, proc_signal_cb, tp);
243 	signal_add(&tp->ev_sigint, NULL);
244 	signal_set(&tp->ev_sighup, SIGHUP, proc_signal_cb, tp);
245 	signal_add(&tp->ev_sighup, NULL);
246 	signal_set(&tp->ev_sigchld, SIGCHLD, proc_signal_cb, tp);
247 	signal_add(&tp->ev_sigchld, NULL);
248 	signal_set(&tp->ev_sigcont, SIGCONT, proc_signal_cb, tp);
249 	signal_add(&tp->ev_sigcont, NULL);
250 	signal_set(&tp->ev_sigterm, SIGTERM, proc_signal_cb, tp);
251 	signal_add(&tp->ev_sigterm, NULL);
252 	signal_set(&tp->ev_sigusr1, SIGUSR1, proc_signal_cb, tp);
253 	signal_add(&tp->ev_sigusr1, NULL);
254 	signal_set(&tp->ev_sigusr2, SIGUSR2, proc_signal_cb, tp);
255 	signal_add(&tp->ev_sigusr2, NULL);
256 	signal_set(&tp->ev_sigwinch, SIGWINCH, proc_signal_cb, tp);
257 	signal_add(&tp->ev_sigwinch, NULL);
258 }
259 
260 void
proc_clear_signals(struct tmuxproc * tp,int defaults)261 proc_clear_signals(struct tmuxproc *tp, int defaults)
262 {
263 	struct sigaction	sa;
264 
265 	memset(&sa, 0, sizeof sa);
266 	sigemptyset(&sa.sa_mask);
267 	sa.sa_flags = SA_RESTART;
268 	sa.sa_handler = SIG_DFL;
269 
270 	sigaction(SIGPIPE, &sa, NULL);
271 	sigaction(SIGTSTP, &sa, NULL);
272 
273 	signal_del(&tp->ev_sigint);
274 	signal_del(&tp->ev_sighup);
275 	signal_del(&tp->ev_sigchld);
276 	signal_del(&tp->ev_sigcont);
277 	signal_del(&tp->ev_sigterm);
278 	signal_del(&tp->ev_sigusr1);
279 	signal_del(&tp->ev_sigusr2);
280 	signal_del(&tp->ev_sigwinch);
281 
282 	if (defaults) {
283 		sigaction(SIGINT, &sa, NULL);
284 		sigaction(SIGQUIT, &sa, NULL);
285 		sigaction(SIGHUP, &sa, NULL);
286 		sigaction(SIGCHLD, &sa, NULL);
287 		sigaction(SIGCONT, &sa, NULL);
288 		sigaction(SIGTERM, &sa, NULL);
289 		sigaction(SIGUSR1, &sa, NULL);
290 		sigaction(SIGUSR2, &sa, NULL);
291 		sigaction(SIGWINCH, &sa, NULL);
292 	}
293 }
294 
295 struct tmuxpeer *
proc_add_peer(struct tmuxproc * tp,int fd,void (* dispatchcb)(struct imsg *,void *),void * arg)296 proc_add_peer(struct tmuxproc *tp, int fd,
297     void (*dispatchcb)(struct imsg *, void *), void *arg)
298 {
299 	struct tmuxpeer	*peer;
300 	gid_t		 gid;
301 
302 	peer = xcalloc(1, sizeof *peer);
303 	peer->parent = tp;
304 
305 	peer->dispatchcb = dispatchcb;
306 	peer->arg = arg;
307 
308 	if (imsgbuf_init(&peer->ibuf, fd) == -1)
309 		fatal("imsgbuf_init");
310 	imsgbuf_allow_fdpass(&peer->ibuf);
311 	event_set(&peer->event, fd, EV_READ, proc_event_cb, peer);
312 
313 	if (getpeereid(fd, &peer->uid, &gid) != 0)
314 		peer->uid = (uid_t)-1;
315 
316 	log_debug("add peer %p: %d (%p)", peer, fd, arg);
317 	TAILQ_INSERT_TAIL(&tp->peers, peer, entry);
318 
319 	proc_update_event(peer);
320 	return (peer);
321 }
322 
323 void
proc_remove_peer(struct tmuxpeer * peer)324 proc_remove_peer(struct tmuxpeer *peer)
325 {
326 	TAILQ_REMOVE(&peer->parent->peers, peer, entry);
327 	log_debug("remove peer %p", peer);
328 
329 	event_del(&peer->event);
330 	imsgbuf_clear(&peer->ibuf);
331 
332 	close(peer->ibuf.fd);
333 	free(peer);
334 }
335 
336 void
proc_kill_peer(struct tmuxpeer * peer)337 proc_kill_peer(struct tmuxpeer *peer)
338 {
339 	peer->flags |= PEER_BAD;
340 }
341 
342 void
proc_flush_peer(struct tmuxpeer * peer)343 proc_flush_peer(struct tmuxpeer *peer)
344 {
345 	imsgbuf_flush(&peer->ibuf);
346 }
347 
348 void
proc_toggle_log(struct tmuxproc * tp)349 proc_toggle_log(struct tmuxproc *tp)
350 {
351 	log_toggle(tp->name);
352 }
353 
354 pid_t
proc_fork_and_daemon(int * fd)355 proc_fork_and_daemon(int *fd)
356 {
357 	pid_t	pid;
358 	int	pair[2];
359 
360 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
361 		fatal("socketpair failed");
362 	switch (pid = fork()) {
363 	case -1:
364 		fatal("fork failed");
365 	case 0:
366 		close(pair[0]);
367 		*fd = pair[1];
368 		if (daemon(1, 0) != 0)
369 			fatal("daemon failed");
370 		return (0);
371 	default:
372 		close(pair[1]);
373 		*fd = pair[0];
374 		return (pid);
375 	}
376 }
377 
378 uid_t
proc_get_peer_uid(struct tmuxpeer * peer)379 proc_get_peer_uid(struct tmuxpeer *peer)
380 {
381 	return (peer->uid);
382 }
383