From 0f308bd18f8051ac7a725e02e18ad234b4f467fa Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Fri, 22 Nov 2024 09:58:47 +0000 Subject: [PATCH] imsg no longer associates file descriptors with the imsg they were sent with, work around this for the moment (it is not clear if this is intentional). --- proc.c | 27 ++++++++++++++++++++++----- server-client.c | 4 ++-- tmux.h | 1 + 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/proc.c b/proc.c index eb73ec31..587da26f 100644 --- a/proc.c +++ b/proc.c @@ -55,6 +55,7 @@ struct tmuxpeer { struct tmuxproc *parent; struct imsgbuf ibuf; + int lastfd; struct event event; uid_t uid; @@ -71,7 +72,7 @@ static int peer_check_version(struct tmuxpeer *, struct imsg *); static void proc_update_event(struct tmuxpeer *); static void -proc_event_cb(__unused int fd, short events, void *arg) +proc_event_cb(int fd, short events, void *arg) { struct tmuxpeer *peer = arg; ssize_t n; @@ -89,12 +90,16 @@ proc_event_cb(__unused int fd, short events, void *arg) } if (n == 0) break; - log_debug("peer %p message %d", peer, imsg.hdr.type); + fd = imsg_get_fd(&imsg); + log_debug("peer %p message %d fd %d", peer, + imsg.hdr.type, fd); + if (fd != -1) { + if (peer->lastfd != -1) + close(peer->lastfd); + peer->lastfd = fd; + } if (peer_check_version(peer, &imsg) != 0) { - fd = imsg_get_fd(&imsg); - if (fd != -1) - close(fd); imsg_free(&imsg); break; } @@ -308,6 +313,7 @@ proc_add_peer(struct tmuxproc *tp, int fd, peer = xcalloc(1, sizeof *peer); peer->parent = tp; + peer->lastfd = -1; peer->dispatchcb = dispatchcb; peer->arg = arg; @@ -336,6 +342,8 @@ proc_remove_peer(struct tmuxpeer *peer) event_del(&peer->event); imsgbuf_clear(&peer->ibuf); + if (peer->lastfd != -1) + close(peer->lastfd); close(peer->ibuf.fd); free(peer); } @@ -387,3 +395,12 @@ proc_get_peer_uid(struct tmuxpeer *peer) { return (peer->uid); } + +int +proc_get_last_fd(struct tmuxpeer *peer) +{ + int fd = peer->lastfd; + + peer->lastfd = -1; + return (fd); +} diff --git a/server-client.c b/server-client.c index a4b961a8..3053a51b 100644 --- a/server-client.c +++ b/server-client.c @@ -3559,13 +3559,13 @@ server_client_dispatch_identify(struct client *c, struct imsg *imsg) case MSG_IDENTIFY_STDIN: if (datalen != 0) fatalx("bad MSG_IDENTIFY_STDIN size"); - c->fd = imsg_get_fd(imsg); + c->fd = proc_get_last_fd(c->peer); log_debug("client %p IDENTIFY_STDIN %d", c, c->fd); break; case MSG_IDENTIFY_STDOUT: if (datalen != 0) fatalx("bad MSG_IDENTIFY_STDOUT size"); - c->out_fd = imsg_get_fd(imsg); + c->out_fd = proc_get_last_fd(c->peer); log_debug("client %p IDENTIFY_STDOUT %d", c, c->out_fd); break; case MSG_IDENTIFY_ENVIRON: diff --git a/tmux.h b/tmux.h index c3d941c2..a4fc5a77 100644 --- a/tmux.h +++ b/tmux.h @@ -2225,6 +2225,7 @@ void proc_flush_peer(struct tmuxpeer *); void proc_toggle_log(struct tmuxproc *); pid_t proc_fork_and_daemon(int *); uid_t proc_get_peer_uid(struct tmuxpeer *); +int proc_get_last_fd(struct tmuxpeer *); /* cfg.c */ extern int cfg_finished;