xref: /openbsd-src/usr.bin/tmux/proc.c (revision 0a607e68121471034e862839247963a0dc80fceb)
1 /* $OpenBSD: proc.c,v 1.5 2015/11/24 21:19:46 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2015 Nicholas Marriott <nicm@users.sourceforge.net>
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/uio.h>
22 
23 #include <errno.h>
24 #include <event.h>
25 #include <imsg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 
30 #include "tmux.h"
31 
32 struct tmuxproc {
33 	const char	 *name;
34 	int		  exit;
35 
36 	void		(*signalcb)(int);
37 };
38 
39 struct tmuxpeer {
40 	struct tmuxproc	*parent;
41 
42 	struct imsgbuf	 ibuf;
43 	struct event	 event;
44 
45 	int		 flags;
46 #define PEER_BAD 0x1
47 
48 	void		(*dispatchcb)(struct imsg *, void *);
49 	void		*arg;
50 };
51 
52 static int	peer_check_version(struct tmuxpeer *, struct imsg *);
53 static void	proc_update_event(struct tmuxpeer *);
54 
55 static void
56 proc_event_cb(__unused int fd, short events, void *arg)
57 {
58 	struct tmuxpeer	*peer = arg;
59 	ssize_t		 n;
60 	struct imsg	 imsg;
61 
62 	if (!(peer->flags & PEER_BAD) && (events & EV_READ)) {
63 		if ((n = imsg_read(&peer->ibuf)) == -1 || n == 0) {
64 			peer->dispatchcb(NULL, peer->arg);
65 			return;
66 		}
67 		for (;;) {
68 			if ((n = imsg_get(&peer->ibuf, &imsg)) == -1) {
69 				peer->dispatchcb(NULL, peer->arg);
70 				return;
71 			}
72 			if (n == 0)
73 				break;
74 			log_debug("peer %p message %d", peer, imsg.hdr.type);
75 
76 			if (peer_check_version(peer, &imsg) != 0) {
77 				if (imsg.fd != -1)
78 					close(imsg.fd);
79 				imsg_free(&imsg);
80 				break;
81 			}
82 
83 			peer->dispatchcb(&imsg, peer->arg);
84 			imsg_free(&imsg);
85 		}
86 	}
87 
88 	if (events & EV_WRITE) {
89 		if (msgbuf_write(&peer->ibuf.w) <= 0 && errno != EAGAIN) {
90 			peer->dispatchcb(NULL, peer->arg);
91 			return;
92 		}
93 	}
94 
95 	if ((peer->flags & PEER_BAD) && peer->ibuf.w.queued == 0) {
96 		peer->dispatchcb(NULL, peer->arg);
97 		return;
98 	}
99 
100 	proc_update_event(peer);
101 }
102 
103 static void
104 proc_signal_cb(int signo, __unused short events, void *arg)
105 {
106 	struct tmuxproc	*tp = arg;
107 
108 	tp->signalcb(signo);
109 }
110 
111 static int
112 peer_check_version(struct tmuxpeer *peer, struct imsg *imsg)
113 {
114 	int	version;
115 
116 	version = imsg->hdr.peerid & 0xff;
117 	if (imsg->hdr.type != MSG_VERSION && version != PROTOCOL_VERSION) {
118 		log_debug("peer %p bad version %d", peer, version);
119 
120 		proc_send(peer, MSG_VERSION, -1, NULL, 0);
121 		peer->flags |= PEER_BAD;
122 
123 		return (-1);
124 	}
125 	return (0);
126 }
127 
128 static void
129 proc_update_event(struct tmuxpeer *peer)
130 {
131 	short	events;
132 
133 	event_del(&peer->event);
134 
135 	events = EV_READ;
136 	if (peer->ibuf.w.queued > 0)
137 		events |= EV_WRITE;
138 	event_set(&peer->event, peer->ibuf.fd, events, proc_event_cb, peer);
139 
140 	event_add(&peer->event, NULL);
141 }
142 
143 int
144 proc_send(struct tmuxpeer *peer, enum msgtype type, int fd, const void *buf,
145     size_t len)
146 {
147 	struct imsgbuf	*ibuf = &peer->ibuf;
148 	void		*vp = (void *)buf;
149 	int		 retval;
150 
151 	if (peer->flags & PEER_BAD)
152 		return (-1);
153 	log_debug("sending message %d to peer %p (%zu bytes)", type, peer, len);
154 
155 	retval = imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, fd, vp, len);
156 	if (retval != 1)
157 		return (-1);
158 	proc_update_event(peer);
159 	return (0);
160 }
161 
162 int
163 proc_send_s(struct tmuxpeer *peer, enum msgtype type, const char *s)
164 {
165 	return (proc_send(peer, type, -1, s, strlen(s) + 1));
166 }
167 
168 struct tmuxproc *
169 proc_start(const char *name, struct event_base *base, int forkflag,
170     void (*signalcb)(int))
171 {
172 	struct tmuxproc	*tp;
173 
174 	if (forkflag) {
175 		switch (fork()) {
176 		case -1:
177 			fatal("fork failed");
178 		case 0:
179 			break;
180 		default:
181 			return (NULL);
182 		}
183 		if (daemon(1, 0) != 0)
184 			fatal("daemon failed");
185 
186 		clear_signals(0);
187 		if (event_reinit(base) != 0)
188 			fatalx("event_reinit failed");
189 	}
190 
191 	log_open(name);
192 	setproctitle("%s (%s)", name, socket_path);
193 
194 	log_debug("%s started (%ld): socket %s, protocol %d", name,
195 	    (long)getpid(), socket_path, PROTOCOL_VERSION);
196 
197 	tp = xcalloc(1, sizeof *tp);
198 	tp->name = xstrdup(name);
199 
200 	tp->signalcb = signalcb;
201 	set_signals(proc_signal_cb, tp);
202 
203 	return (tp);
204 }
205 
206 void
207 proc_loop(struct tmuxproc *tp, int (*loopcb)(void))
208 {
209 	log_debug("%s loop enter", tp->name);
210 	do
211 		event_loop(EVLOOP_ONCE);
212 	while (!tp->exit && (loopcb == NULL || !loopcb ()));
213 	log_debug("%s loop exit", tp->name);
214 }
215 
216 void
217 proc_exit(struct tmuxproc *tp)
218 {
219 	tp->exit = 1;
220 }
221 
222 struct tmuxpeer *
223 proc_add_peer(struct tmuxproc *tp, int fd,
224     void (*dispatchcb)(struct imsg *, void *), void *arg)
225 {
226 	struct tmuxpeer	*peer;
227 
228 	peer = xcalloc(1, sizeof *peer);
229 	peer->parent = tp;
230 
231 	peer->dispatchcb = dispatchcb;
232 	peer->arg = arg;
233 
234 	imsg_init(&peer->ibuf, fd);
235 	event_set(&peer->event, fd, EV_READ, proc_event_cb, peer);
236 
237 	log_debug("add peer %p: %d (%p)", peer, fd, arg);
238 
239 	proc_update_event(peer);
240 	return (peer);
241 }
242 
243 void
244 proc_remove_peer(struct tmuxpeer *peer)
245 {
246 	log_debug("remove peer %p", peer);
247 
248 	event_del(&peer->event);
249 	imsg_clear(&peer->ibuf);
250 
251 	close(peer->ibuf.fd);
252 	free(peer);
253 }
254 
255 void
256 proc_kill_peer(struct tmuxpeer *peer)
257 {
258 	peer->flags |= PEER_BAD;
259 }
260