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>
22 #include <sys/utsname.h>
30 #if defined(HAVE_NCURSES_H)
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
;
55 struct tmuxproc
*parent
;
64 void (*dispatchcb
)(struct imsg
*, void *);
67 TAILQ_ENTRY(tmuxpeer
) entry
;
70 static int peer_check_version(struct tmuxpeer
*, struct imsg
*);
71 static void proc_update_event(struct tmuxpeer
*);
74 proc_event_cb(__unused
int fd
, short events
, void *arg
)
76 struct tmuxpeer
*peer
= arg
;
80 if (!(peer
->flags
& PEER_BAD
) && (events
& EV_READ
)) {
81 if (((n
= imsg_read(&peer
->ibuf
)) == -1 && errno
!= EAGAIN
) ||
83 peer
->dispatchcb(NULL
, peer
->arg
);
87 if ((n
= imsg_get(&peer
->ibuf
, &imsg
)) == -1) {
88 peer
->dispatchcb(NULL
, peer
->arg
);
93 log_debug("peer %p message %d", peer
, imsg
.hdr
.type
);
95 if (peer_check_version(peer
, &imsg
) != 0) {
102 peer
->dispatchcb(&imsg
, peer
->arg
);
107 if (events
& EV_WRITE
) {
108 if (msgbuf_write(&peer
->ibuf
.w
) <= 0 && errno
!= EAGAIN
) {
109 peer
->dispatchcb(NULL
, peer
->arg
);
114 if ((peer
->flags
& PEER_BAD
) && peer
->ibuf
.w
.queued
== 0) {
115 peer
->dispatchcb(NULL
, peer
->arg
);
119 proc_update_event(peer
);
123 proc_signal_cb(int signo
, __unused
short events
, void *arg
)
125 struct tmuxproc
*tp
= arg
;
131 peer_check_version(struct tmuxpeer
*peer
, struct imsg
*imsg
)
135 version
= imsg
->hdr
.peerid
& 0xff;
136 if (imsg
->hdr
.type
!= MSG_VERSION
&& version
!= PROTOCOL_VERSION
) {
137 log_debug("peer %p bad version %d", peer
, version
);
139 proc_send(peer
, MSG_VERSION
, -1, NULL
, 0);
140 peer
->flags
|= PEER_BAD
;
148 proc_update_event(struct tmuxpeer
*peer
)
152 event_del(&peer
->event
);
155 if (peer
->ibuf
.w
.queued
> 0)
157 event_set(&peer
->event
, peer
->ibuf
.fd
, events
, proc_event_cb
, peer
);
159 event_add(&peer
->event
, NULL
);
163 proc_send(struct tmuxpeer
*peer
, enum msgtype type
, int fd
, const void *buf
,
166 struct imsgbuf
*ibuf
= &peer
->ibuf
;
167 void *vp
= (void *)buf
;
170 if (peer
->flags
& PEER_BAD
)
172 log_debug("sending message %d to peer %p (%zu bytes)", type
, peer
, len
);
174 retval
= imsg_compose(ibuf
, type
, PROTOCOL_VERSION
, -1, fd
, vp
, len
);
177 proc_update_event(peer
);
182 proc_start(const char *name
)
188 setproctitle("%s (%s)", name
, socket_path
);
191 memset(&u
, 0, sizeof u
);
193 log_debug("%s started (%ld): version %s, socket %s, protocol %d", name
,
194 (long)getpid(), getversion(), socket_path
, PROTOCOL_VERSION
);
195 log_debug("on %s %s %s", u
.sysname
, u
.release
, u
.version
);
196 log_debug("using libevent %s (%s)"
200 #ifdef NCURSES_VERSION
201 "; ncurses " NCURSES_VERSION
203 , event_get_version(), event_get_method()
205 , utf8proc_version ()
209 tp
= xcalloc(1, sizeof *tp
);
210 tp
->name
= xstrdup(name
);
211 TAILQ_INIT(&tp
->peers
);
217 proc_loop(struct tmuxproc
*tp
, int (*loopcb
)(void))
219 log_debug("%s loop enter", tp
->name
);
221 event_loop(EVLOOP_ONCE
);
222 while (!tp
->exit
&& (loopcb
== NULL
|| !loopcb ()));
223 log_debug("%s loop exit", tp
->name
);
227 proc_exit(struct tmuxproc
*tp
)
229 struct tmuxpeer
*peer
;
231 TAILQ_FOREACH(peer
, &tp
->peers
, entry
)
232 imsg_flush(&peer
->ibuf
);
237 proc_set_signals(struct tmuxproc
*tp
, void (*signalcb
)(int))
241 tp
->signalcb
= signalcb
;
243 memset(&sa
, 0, sizeof sa
);
244 sigemptyset(&sa
.sa_mask
);
245 sa
.sa_flags
= SA_RESTART
;
246 sa
.sa_handler
= SIG_IGN
;
248 sigaction(SIGPIPE
, &sa
, NULL
);
249 sigaction(SIGTSTP
, &sa
, NULL
);
250 sigaction(SIGTTIN
, &sa
, NULL
);
251 sigaction(SIGTTOU
, &sa
, NULL
);
252 sigaction(SIGQUIT
, &sa
, NULL
);
254 signal_set(&tp
->ev_sigint
, SIGINT
, proc_signal_cb
, tp
);
255 signal_add(&tp
->ev_sigint
, NULL
);
256 signal_set(&tp
->ev_sighup
, SIGHUP
, proc_signal_cb
, tp
);
257 signal_add(&tp
->ev_sighup
, NULL
);
258 signal_set(&tp
->ev_sigchld
, SIGCHLD
, proc_signal_cb
, tp
);
259 signal_add(&tp
->ev_sigchld
, NULL
);
260 signal_set(&tp
->ev_sigcont
, SIGCONT
, proc_signal_cb
, tp
);
261 signal_add(&tp
->ev_sigcont
, NULL
);
262 signal_set(&tp
->ev_sigterm
, SIGTERM
, proc_signal_cb
, tp
);
263 signal_add(&tp
->ev_sigterm
, NULL
);
264 signal_set(&tp
->ev_sigusr1
, SIGUSR1
, proc_signal_cb
, tp
);
265 signal_add(&tp
->ev_sigusr1
, NULL
);
266 signal_set(&tp
->ev_sigusr2
, SIGUSR2
, proc_signal_cb
, tp
);
267 signal_add(&tp
->ev_sigusr2
, NULL
);
268 signal_set(&tp
->ev_sigwinch
, SIGWINCH
, proc_signal_cb
, tp
);
269 signal_add(&tp
->ev_sigwinch
, NULL
);
273 proc_clear_signals(struct tmuxproc
*tp
, int defaults
)
277 memset(&sa
, 0, sizeof sa
);
278 sigemptyset(&sa
.sa_mask
);
279 sa
.sa_flags
= SA_RESTART
;
280 sa
.sa_handler
= SIG_DFL
;
282 sigaction(SIGPIPE
, &sa
, NULL
);
283 sigaction(SIGTSTP
, &sa
, NULL
);
285 signal_del(&tp
->ev_sigint
);
286 signal_del(&tp
->ev_sighup
);
287 signal_del(&tp
->ev_sigchld
);
288 signal_del(&tp
->ev_sigcont
);
289 signal_del(&tp
->ev_sigterm
);
290 signal_del(&tp
->ev_sigusr1
);
291 signal_del(&tp
->ev_sigusr2
);
292 signal_del(&tp
->ev_sigwinch
);
295 sigaction(SIGINT
, &sa
, NULL
);
296 sigaction(SIGQUIT
, &sa
, NULL
);
297 sigaction(SIGHUP
, &sa
, NULL
);
298 sigaction(SIGCHLD
, &sa
, NULL
);
299 sigaction(SIGCONT
, &sa
, NULL
);
300 sigaction(SIGTERM
, &sa
, NULL
);
301 sigaction(SIGUSR1
, &sa
, NULL
);
302 sigaction(SIGUSR2
, &sa
, NULL
);
303 sigaction(SIGWINCH
, &sa
, NULL
);
308 proc_add_peer(struct tmuxproc
*tp
, int fd
,
309 void (*dispatchcb
)(struct imsg
*, void *), void *arg
)
311 struct tmuxpeer
*peer
;
314 peer
= xcalloc(1, sizeof *peer
);
317 peer
->dispatchcb
= dispatchcb
;
320 imsg_init(&peer
->ibuf
, fd
);
321 event_set(&peer
->event
, fd
, EV_READ
, proc_event_cb
, peer
);
323 if (getpeereid(fd
, &peer
->uid
, &gid
) != 0)
324 peer
->uid
= (uid_t
)-1;
326 log_debug("add peer %p: %d (%p)", peer
, fd
, arg
);
327 TAILQ_INSERT_TAIL(&tp
->peers
, peer
, entry
);
329 proc_update_event(peer
);
334 proc_remove_peer(struct tmuxpeer
*peer
)
336 TAILQ_REMOVE(&peer
->parent
->peers
, peer
, entry
);
337 log_debug("remove peer %p", peer
);
339 event_del(&peer
->event
);
340 imsg_clear(&peer
->ibuf
);
342 close(peer
->ibuf
.fd
);
347 proc_kill_peer(struct tmuxpeer
*peer
)
349 peer
->flags
|= PEER_BAD
;
353 proc_toggle_log(struct tmuxproc
*tp
)
355 log_toggle(tp
->name
);
359 proc_fork_and_daemon(int *fd
)
364 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, pair
) != 0)
365 fatal("socketpair failed");
366 switch (pid
= fork()) {
368 fatal("fork failed");
372 if (daemon(1, 0) != 0)
373 fatal("daemon failed");
383 proc_get_peer_uid(struct tmuxpeer
*peer
)