diff options
author | 2021-02-11 09:39:29 +0000 | |
---|---|---|
committer | 2021-02-11 09:39:29 +0000 | |
commit | ab26eabfedbc645ec78cced73ba1bb89adacf0c1 (patch) | |
tree | 6e852b40556e53b8e18215da28fec80d41eee062 /usr.bin/tmux/proc.c | |
parent | O_TRUNC is needed in case file exists. (diff) | |
download | wireguard-openbsd-ab26eabfedbc645ec78cced73ba1bb89adacf0c1.tar.xz wireguard-openbsd-ab26eabfedbc645ec78cced73ba1bb89adacf0c1.zip |
Add a couple of helper functions, and flush imsgs on exit.
Diffstat (limited to 'usr.bin/tmux/proc.c')
-rw-r--r-- | usr.bin/tmux/proc.c | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/usr.bin/tmux/proc.c b/usr.bin/tmux/proc.c index af8343f77e7..fd4ca6f97ee 100644 --- a/usr.bin/tmux/proc.c +++ b/usr.bin/tmux/proc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: proc.c,v 1.19 2020/10/26 19:00:37 nicm Exp $ */ +/* $OpenBSD: proc.c,v 1.20 2021/02/11 09:39:29 nicm Exp $ */ /* * Copyright (c) 2015 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -18,6 +18,7 @@ #include <sys/types.h> #include <sys/queue.h> +#include <sys/socket.h> #include <sys/uio.h> #include <sys/utsname.h> @@ -45,6 +46,8 @@ struct tmuxproc { struct event ev_sigusr1; struct event ev_sigusr2; struct event ev_sigwinch; + + TAILQ_HEAD(, tmuxpeer) peers; }; struct tmuxpeer { @@ -58,6 +61,8 @@ struct tmuxpeer { void (*dispatchcb)(struct imsg *, void *); void *arg; + + TAILQ_ENTRY(tmuxpeer) entry; }; static int peer_check_version(struct tmuxpeer *, struct imsg *); @@ -190,6 +195,7 @@ proc_start(const char *name) tp = xcalloc(1, sizeof *tp); tp->name = xstrdup(name); + TAILQ_INIT(&tp->peers); return (tp); } @@ -207,6 +213,10 @@ proc_loop(struct tmuxproc *tp, int (*loopcb)(void)) void proc_exit(struct tmuxproc *tp) { + struct tmuxpeer *peer; + + TAILQ_FOREACH(peer, &tp->peers, entry) + imsg_flush(&peer->ibuf); tp->exit = 1; } @@ -297,6 +307,7 @@ proc_add_peer(struct tmuxproc *tp, int fd, event_set(&peer->event, fd, EV_READ, proc_event_cb, peer); log_debug("add peer %p: %d (%p)", peer, fd, arg); + TAILQ_INSERT_TAIL(&tp->peers, peer, entry); proc_update_event(peer); return (peer); @@ -305,6 +316,7 @@ proc_add_peer(struct tmuxproc *tp, int fd, void proc_remove_peer(struct tmuxpeer *peer) { + TAILQ_REMOVE(&peer->parent->peers, peer, entry); log_debug("remove peer %p", peer); event_del(&peer->event); @@ -325,3 +337,27 @@ proc_toggle_log(struct tmuxproc *tp) { log_toggle(tp->name); } + +pid_t +proc_fork_and_daemon(int *fd) +{ + pid_t pid; + int pair[2]; + + if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0) + fatal("socketpair failed"); + switch (pid = fork()) { + case -1: + fatal("fork failed"); + case 0: + close(pair[0]); + *fd = pair[1]; + if (daemon(1, 0) != 0) + fatal("daemon failed"); + return (0); + default: + close(pair[1]); + *fd = pair[0]; + return (pid); + } +} |