2015-10-27 13:23:24 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2015 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2015-10-27 13:23:24 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2021-02-11 09:39:29 +00:00
|
|
|
#include <sys/socket.h>
|
2015-10-27 13:23:24 +00:00
|
|
|
#include <sys/uio.h>
|
2015-11-24 21:32:36 +00:00
|
|
|
#include <sys/utsname.h>
|
2015-10-27 13:23:24 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
2017-07-12 09:24:17 +00:00
|
|
|
#include <signal.h>
|
2015-10-27 13:23:24 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-05-22 16:27:07 +00:00
|
|
|
#if defined(HAVE_NCURSES_H)
|
|
|
|
#include <ncurses.h>
|
|
|
|
#endif
|
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
struct tmuxproc {
|
|
|
|
const char *name;
|
|
|
|
int exit;
|
|
|
|
|
|
|
|
void (*signalcb)(int);
|
2017-07-12 09:24:17 +00:00
|
|
|
|
2020-05-10 15:52:46 +00:00
|
|
|
struct event ev_sigint;
|
2017-07-12 09:24:17 +00:00
|
|
|
struct event ev_sighup;
|
|
|
|
struct event ev_sigchld;
|
|
|
|
struct event ev_sigcont;
|
|
|
|
struct event ev_sigterm;
|
|
|
|
struct event ev_sigusr1;
|
|
|
|
struct event ev_sigusr2;
|
|
|
|
struct event ev_sigwinch;
|
2021-02-11 09:39:29 +00:00
|
|
|
|
|
|
|
TAILQ_HEAD(, tmuxpeer) peers;
|
2015-10-27 13:23:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct tmuxpeer {
|
|
|
|
struct tmuxproc *parent;
|
|
|
|
|
|
|
|
struct imsgbuf ibuf;
|
|
|
|
struct event event;
|
2022-03-08 11:28:40 +00:00
|
|
|
uid_t uid;
|
2015-10-27 13:23:24 +00:00
|
|
|
|
|
|
|
int flags;
|
|
|
|
#define PEER_BAD 0x1
|
|
|
|
|
|
|
|
void (*dispatchcb)(struct imsg *, void *);
|
2017-06-07 15:27:46 +00:00
|
|
|
void *arg;
|
2021-02-11 09:39:29 +00:00
|
|
|
|
|
|
|
TAILQ_ENTRY(tmuxpeer) entry;
|
2015-10-27 13:23:24 +00:00
|
|
|
};
|
|
|
|
|
2015-10-29 09:35:31 +00:00
|
|
|
static int peer_check_version(struct tmuxpeer *, struct imsg *);
|
|
|
|
static void proc_update_event(struct tmuxpeer *);
|
2015-10-27 13:23:24 +00:00
|
|
|
|
|
|
|
static void
|
2024-11-27 10:30:52 +00:00
|
|
|
proc_event_cb(__unused int fd, short events, void *arg)
|
2015-10-27 13:23:24 +00:00
|
|
|
{
|
|
|
|
struct tmuxpeer *peer = arg;
|
|
|
|
ssize_t n;
|
|
|
|
struct imsg imsg;
|
|
|
|
|
|
|
|
if (!(peer->flags & PEER_BAD) && (events & EV_READ)) {
|
2024-11-21 13:35:20 +00:00
|
|
|
if (imsgbuf_read(&peer->ibuf) != 1) {
|
2015-10-27 13:23:24 +00:00
|
|
|
peer->dispatchcb(NULL, peer->arg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
if ((n = imsg_get(&peer->ibuf, &imsg)) == -1) {
|
|
|
|
peer->dispatchcb(NULL, peer->arg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (n == 0)
|
|
|
|
break;
|
2024-11-27 10:30:52 +00:00
|
|
|
log_debug("peer %p message %d", peer, imsg.hdr.type);
|
2015-10-27 13:23:24 +00:00
|
|
|
|
2015-10-29 09:35:31 +00:00
|
|
|
if (peer_check_version(peer, &imsg) != 0) {
|
2015-10-27 13:23:24 +00:00
|
|
|
imsg_free(&imsg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
peer->dispatchcb(&imsg, peer->arg);
|
|
|
|
imsg_free(&imsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (events & EV_WRITE) {
|
2024-11-21 13:35:20 +00:00
|
|
|
if (imsgbuf_write(&peer->ibuf) == -1) {
|
2015-10-27 13:23:24 +00:00
|
|
|
peer->dispatchcb(NULL, peer->arg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-21 13:35:20 +00:00
|
|
|
if ((peer->flags & PEER_BAD) && imsgbuf_queuelen(&peer->ibuf) == 0) {
|
2015-10-27 13:23:24 +00:00
|
|
|
peer->dispatchcb(NULL, peer->arg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
proc_update_event(peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-11-18 14:27:44 +00:00
|
|
|
proc_signal_cb(int signo, __unused short events, void *arg)
|
2015-10-27 13:23:24 +00:00
|
|
|
{
|
|
|
|
struct tmuxproc *tp = arg;
|
|
|
|
|
|
|
|
tp->signalcb(signo);
|
|
|
|
}
|
|
|
|
|
2015-10-29 09:35:31 +00:00
|
|
|
static int
|
|
|
|
peer_check_version(struct tmuxpeer *peer, struct imsg *imsg)
|
|
|
|
{
|
|
|
|
int version;
|
|
|
|
|
|
|
|
version = imsg->hdr.peerid & 0xff;
|
|
|
|
if (imsg->hdr.type != MSG_VERSION && version != PROTOCOL_VERSION) {
|
|
|
|
log_debug("peer %p bad version %d", peer, version);
|
|
|
|
|
|
|
|
proc_send(peer, MSG_VERSION, -1, NULL, 0);
|
|
|
|
peer->flags |= PEER_BAD;
|
|
|
|
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
static void
|
|
|
|
proc_update_event(struct tmuxpeer *peer)
|
|
|
|
{
|
|
|
|
short events;
|
|
|
|
|
|
|
|
event_del(&peer->event);
|
|
|
|
|
|
|
|
events = EV_READ;
|
2024-11-21 13:35:20 +00:00
|
|
|
if (imsgbuf_queuelen(&peer->ibuf) > 0)
|
2015-10-27 13:23:24 +00:00
|
|
|
events |= EV_WRITE;
|
|
|
|
event_set(&peer->event, peer->ibuf.fd, events, proc_event_cb, peer);
|
|
|
|
|
|
|
|
event_add(&peer->event, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
proc_send(struct tmuxpeer *peer, enum msgtype type, int fd, const void *buf,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
struct imsgbuf *ibuf = &peer->ibuf;
|
|
|
|
void *vp = (void *)buf;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (peer->flags & PEER_BAD)
|
|
|
|
return (-1);
|
|
|
|
log_debug("sending message %d to peer %p (%zu bytes)", type, peer, len);
|
|
|
|
|
|
|
|
retval = imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, fd, vp, len);
|
|
|
|
if (retval != 1)
|
|
|
|
return (-1);
|
|
|
|
proc_update_event(peer);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tmuxproc *
|
2017-07-12 09:24:17 +00:00
|
|
|
proc_start(const char *name)
|
2015-10-27 13:23:24 +00:00
|
|
|
{
|
|
|
|
struct tmuxproc *tp;
|
2015-11-24 21:32:36 +00:00
|
|
|
struct utsname u;
|
2015-10-27 13:23:24 +00:00
|
|
|
|
2015-11-24 21:19:46 +00:00
|
|
|
log_open(name);
|
2015-10-27 13:23:24 +00:00
|
|
|
setproctitle("%s (%s)", name, socket_path);
|
|
|
|
|
2015-11-24 21:32:36 +00:00
|
|
|
if (uname(&u) < 0)
|
|
|
|
memset(&u, 0, sizeof u);
|
|
|
|
|
2017-01-18 17:35:41 +00:00
|
|
|
log_debug("%s started (%ld): version %s, socket %s, protocol %d", name,
|
2020-01-28 10:44:30 +00:00
|
|
|
(long)getpid(), getversion(), socket_path, PROTOCOL_VERSION);
|
2020-05-22 16:20:35 +00:00
|
|
|
log_debug("on %s %s %s", u.sysname, u.release, u.version);
|
2023-04-25 15:48:49 +00:00
|
|
|
log_debug("using libevent %s %s", event_get_version(), event_get_method());
|
2020-05-22 16:14:35 +00:00
|
|
|
#ifdef HAVE_UTF8PROC
|
2023-04-25 15:48:49 +00:00
|
|
|
log_debug("using utf8proc %s", utf8proc_version());
|
2020-05-22 16:14:35 +00:00
|
|
|
#endif
|
|
|
|
#ifdef NCURSES_VERSION
|
2023-04-25 15:48:49 +00:00
|
|
|
log_debug("using ncurses %s %06u", NCURSES_VERSION, NCURSES_VERSION_PATCH);
|
2020-05-22 16:14:35 +00:00
|
|
|
#endif
|
2015-10-27 13:23:24 +00:00
|
|
|
|
|
|
|
tp = xcalloc(1, sizeof *tp);
|
|
|
|
tp->name = xstrdup(name);
|
2021-02-11 09:39:29 +00:00
|
|
|
TAILQ_INIT(&tp->peers);
|
2015-10-27 13:23:24 +00:00
|
|
|
|
|
|
|
return (tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
proc_loop(struct tmuxproc *tp, int (*loopcb)(void))
|
|
|
|
{
|
|
|
|
log_debug("%s loop enter", tp->name);
|
|
|
|
do
|
|
|
|
event_loop(EVLOOP_ONCE);
|
|
|
|
while (!tp->exit && (loopcb == NULL || !loopcb ()));
|
|
|
|
log_debug("%s loop exit", tp->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
proc_exit(struct tmuxproc *tp)
|
|
|
|
{
|
2021-02-11 09:39:29 +00:00
|
|
|
struct tmuxpeer *peer;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(peer, &tp->peers, entry)
|
2024-11-21 13:35:20 +00:00
|
|
|
imsgbuf_flush(&peer->ibuf);
|
2015-10-27 13:23:24 +00:00
|
|
|
tp->exit = 1;
|
|
|
|
}
|
|
|
|
|
2017-07-12 09:24:17 +00:00
|
|
|
void
|
|
|
|
proc_set_signals(struct tmuxproc *tp, void (*signalcb)(int))
|
|
|
|
{
|
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
tp->signalcb = signalcb;
|
|
|
|
|
|
|
|
memset(&sa, 0, sizeof sa);
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = SA_RESTART;
|
|
|
|
sa.sa_handler = SIG_IGN;
|
|
|
|
|
|
|
|
sigaction(SIGPIPE, &sa, NULL);
|
|
|
|
sigaction(SIGTSTP, &sa, NULL);
|
2020-05-10 15:52:46 +00:00
|
|
|
sigaction(SIGTTIN, &sa, NULL);
|
|
|
|
sigaction(SIGTTOU, &sa, NULL);
|
2020-08-04 08:50:01 +00:00
|
|
|
sigaction(SIGQUIT, &sa, NULL);
|
2017-07-12 09:24:17 +00:00
|
|
|
|
2020-05-10 15:52:46 +00:00
|
|
|
signal_set(&tp->ev_sigint, SIGINT, proc_signal_cb, tp);
|
|
|
|
signal_add(&tp->ev_sigint, NULL);
|
2017-07-12 09:24:17 +00:00
|
|
|
signal_set(&tp->ev_sighup, SIGHUP, proc_signal_cb, tp);
|
|
|
|
signal_add(&tp->ev_sighup, NULL);
|
|
|
|
signal_set(&tp->ev_sigchld, SIGCHLD, proc_signal_cb, tp);
|
|
|
|
signal_add(&tp->ev_sigchld, NULL);
|
|
|
|
signal_set(&tp->ev_sigcont, SIGCONT, proc_signal_cb, tp);
|
|
|
|
signal_add(&tp->ev_sigcont, NULL);
|
|
|
|
signal_set(&tp->ev_sigterm, SIGTERM, proc_signal_cb, tp);
|
|
|
|
signal_add(&tp->ev_sigterm, NULL);
|
|
|
|
signal_set(&tp->ev_sigusr1, SIGUSR1, proc_signal_cb, tp);
|
|
|
|
signal_add(&tp->ev_sigusr1, NULL);
|
|
|
|
signal_set(&tp->ev_sigusr2, SIGUSR2, proc_signal_cb, tp);
|
|
|
|
signal_add(&tp->ev_sigusr2, NULL);
|
|
|
|
signal_set(&tp->ev_sigwinch, SIGWINCH, proc_signal_cb, tp);
|
|
|
|
signal_add(&tp->ev_sigwinch, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-07-14 18:49:07 +00:00
|
|
|
proc_clear_signals(struct tmuxproc *tp, int defaults)
|
2017-07-12 09:24:17 +00:00
|
|
|
{
|
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
memset(&sa, 0, sizeof sa);
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = SA_RESTART;
|
|
|
|
sa.sa_handler = SIG_DFL;
|
|
|
|
|
|
|
|
sigaction(SIGPIPE, &sa, NULL);
|
|
|
|
sigaction(SIGTSTP, &sa, NULL);
|
|
|
|
|
2020-05-10 15:52:46 +00:00
|
|
|
signal_del(&tp->ev_sigint);
|
2017-07-14 08:04:23 +00:00
|
|
|
signal_del(&tp->ev_sighup);
|
|
|
|
signal_del(&tp->ev_sigchld);
|
|
|
|
signal_del(&tp->ev_sigcont);
|
|
|
|
signal_del(&tp->ev_sigterm);
|
|
|
|
signal_del(&tp->ev_sigusr1);
|
|
|
|
signal_del(&tp->ev_sigusr2);
|
|
|
|
signal_del(&tp->ev_sigwinch);
|
2017-07-14 18:49:07 +00:00
|
|
|
|
|
|
|
if (defaults) {
|
2020-05-10 15:52:46 +00:00
|
|
|
sigaction(SIGINT, &sa, NULL);
|
2020-10-26 19:00:37 +00:00
|
|
|
sigaction(SIGQUIT, &sa, NULL);
|
2017-07-14 18:49:07 +00:00
|
|
|
sigaction(SIGHUP, &sa, NULL);
|
|
|
|
sigaction(SIGCHLD, &sa, NULL);
|
|
|
|
sigaction(SIGCONT, &sa, NULL);
|
|
|
|
sigaction(SIGTERM, &sa, NULL);
|
|
|
|
sigaction(SIGUSR1, &sa, NULL);
|
|
|
|
sigaction(SIGUSR2, &sa, NULL);
|
|
|
|
sigaction(SIGWINCH, &sa, NULL);
|
|
|
|
}
|
2017-07-12 09:24:17 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
struct tmuxpeer *
|
|
|
|
proc_add_peer(struct tmuxproc *tp, int fd,
|
|
|
|
void (*dispatchcb)(struct imsg *, void *), void *arg)
|
|
|
|
{
|
|
|
|
struct tmuxpeer *peer;
|
2022-03-08 11:28:40 +00:00
|
|
|
gid_t gid;
|
2015-10-27 13:23:24 +00:00
|
|
|
|
|
|
|
peer = xcalloc(1, sizeof *peer);
|
|
|
|
peer->parent = tp;
|
|
|
|
|
|
|
|
peer->dispatchcb = dispatchcb;
|
|
|
|
peer->arg = arg;
|
|
|
|
|
2024-11-21 13:35:20 +00:00
|
|
|
if (imsgbuf_init(&peer->ibuf, fd) == -1)
|
|
|
|
fatal("imsgbuf_init");
|
|
|
|
imsgbuf_allow_fdpass(&peer->ibuf);
|
2015-10-27 13:23:24 +00:00
|
|
|
event_set(&peer->event, fd, EV_READ, proc_event_cb, peer);
|
|
|
|
|
2022-03-08 11:28:40 +00:00
|
|
|
if (getpeereid(fd, &peer->uid, &gid) != 0)
|
|
|
|
peer->uid = (uid_t)-1;
|
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
log_debug("add peer %p: %d (%p)", peer, fd, arg);
|
2021-02-11 09:39:29 +00:00
|
|
|
TAILQ_INSERT_TAIL(&tp->peers, peer, entry);
|
2015-10-27 13:23:24 +00:00
|
|
|
|
|
|
|
proc_update_event(peer);
|
|
|
|
return (peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
proc_remove_peer(struct tmuxpeer *peer)
|
|
|
|
{
|
2021-02-11 09:39:29 +00:00
|
|
|
TAILQ_REMOVE(&peer->parent->peers, peer, entry);
|
2015-10-27 13:23:24 +00:00
|
|
|
log_debug("remove peer %p", peer);
|
|
|
|
|
|
|
|
event_del(&peer->event);
|
2024-11-21 13:35:20 +00:00
|
|
|
imsgbuf_clear(&peer->ibuf);
|
2015-10-27 13:23:24 +00:00
|
|
|
|
|
|
|
close(peer->ibuf.fd);
|
|
|
|
free(peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
proc_kill_peer(struct tmuxpeer *peer)
|
|
|
|
{
|
|
|
|
peer->flags |= PEER_BAD;
|
|
|
|
}
|
2017-06-04 08:25:57 +00:00
|
|
|
|
2022-04-06 13:28:50 +00:00
|
|
|
void
|
|
|
|
proc_flush_peer(struct tmuxpeer *peer)
|
|
|
|
{
|
2024-11-21 13:35:20 +00:00
|
|
|
imsgbuf_flush(&peer->ibuf);
|
2022-04-06 13:28:50 +00:00
|
|
|
}
|
|
|
|
|
2017-06-04 08:25:57 +00:00
|
|
|
void
|
|
|
|
proc_toggle_log(struct tmuxproc *tp)
|
|
|
|
{
|
|
|
|
log_toggle(tp->name);
|
|
|
|
}
|
2021-02-11 09:39:29 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2022-03-08 11:28:40 +00:00
|
|
|
|
|
|
|
uid_t
|
|
|
|
proc_get_peer_uid(struct tmuxpeer *peer)
|
|
|
|
{
|
|
|
|
return (peer->uid);
|
|
|
|
}
|