-/* $OpenBSD: client.c,v 1.152 2021/02/11 08:28:45 nicm Exp $ */
+/* $OpenBSD: client.c,v 1.153 2021/02/11 09:39:29 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
static void
client_exit(void)
{
- struct client_file *cf;
- size_t left;
- int waiting = 0;
-
- RB_FOREACH (cf, client_files, &client_files) {
- if (cf->event == NULL)
- continue;
- left = EVBUFFER_LENGTH(cf->event->output);
- if (left != 0) {
- waiting++;
- log_debug("file %u %zu bytes left", cf->stream, left);
- }
- }
- if (waiting == 0)
+ if (!file_write_left(&client_files))
proc_exit(client_proc);
}
-/* $OpenBSD: file.c,v 1.8 2021/02/11 08:28:45 nicm Exp $ */
+/* $OpenBSD: file.c,v 1.9 2021/02/11 09:39:29 nicm Exp $ */
/*
* Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
free(msg);
}
+/* Check if any files have data left to write. */
+int
+file_write_left(struct client_files *files)
+{
+ struct client_file *cf;
+ size_t left;
+ int waiting = 0;
+
+ RB_FOREACH (cf, client_files, files) {
+ if (cf->event == NULL)
+ continue;
+ left = EVBUFFER_LENGTH(cf->event->output);
+ if (left != 0) {
+ waiting++;
+ log_debug("file %u %zu bytes left", cf->stream, left);
+ }
+ }
+ return (waiting != 0);
+}
+
/* Client file write error callback. */
static void
file_write_error_callback(__unused struct bufferevent *bev, __unused short what,
-/* $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>
#include <sys/types.h>
#include <sys/queue.h>
+#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/utsname.h>
struct event ev_sigusr1;
struct event ev_sigusr2;
struct event ev_sigwinch;
+
+ TAILQ_HEAD(, tmuxpeer) peers;
};
struct tmuxpeer {
void (*dispatchcb)(struct imsg *, void *);
void *arg;
+
+ TAILQ_ENTRY(tmuxpeer) entry;
};
static int peer_check_version(struct tmuxpeer *, struct imsg *);
tp = xcalloc(1, sizeof *tp);
tp->name = xstrdup(name);
+ TAILQ_INIT(&tp->peers);
return (tp);
}
void
proc_exit(struct tmuxproc *tp)
{
+ struct tmuxpeer *peer;
+
+ TAILQ_FOREACH(peer, &tp->peers, entry)
+ imsg_flush(&peer->ibuf);
tp->exit = 1;
}
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);
void
proc_remove_peer(struct tmuxpeer *peer)
{
+ TAILQ_REMOVE(&peer->parent->peers, peer, entry);
log_debug("remove peer %p", peer);
event_del(&peer->event);
{
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);
+ }
+}
-/* $OpenBSD: server.c,v 1.195 2020/09/16 18:37:55 nicm Exp $ */
+/* $OpenBSD: server.c,v 1.196 2021/02/11 09:39:29 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
server_start(struct tmuxproc *client, int flags, struct event_base *base,
int lockfd, char *lockfile)
{
- int pair[2];
- sigset_t set, oldset;
- struct client *c = NULL;
- char *cause = NULL;
+ int fd;
+ sigset_t set, oldset;
+ struct client *c = NULL;
+ char *cause = NULL;
sigfillset(&set);
sigprocmask(SIG_BLOCK, &set, &oldset);
if (~flags & CLIENT_NOFORK) {
- if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
- fatal("socketpair failed");
-
- switch (fork()) {
- case -1:
- fatal("fork failed");
- case 0:
- break;
- default:
+ if (proc_fork_and_daemon(&fd) != 0) {
sigprocmask(SIG_SETMASK, &oldset, NULL);
- close(pair[1]);
- return (pair[0]);
+ return (fd);
}
- close(pair[0]);
- if (daemon(1, 0) != 0)
- fatal("daemon failed");
}
-
- server_client_flags = flags;
proc_clear_signals(client, 0);
+ server_client_flags = flags;
if (event_reinit(base) != 0)
fatalx("event_reinit failed");
if (server_fd != -1)
server_update_socket();
if (~flags & CLIENT_NOFORK)
- c = server_client_create(pair[1]);
+ c = server_client_create(fd);
else
options_set_number(global_options, "exit-empty", 0);
-/* $OpenBSD: tmux.h,v 1.1089 2021/02/11 08:28:45 nicm Exp $ */
+/* $OpenBSD: tmux.h,v 1.1090 2021/02/11 09:39:29 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
void proc_remove_peer(struct tmuxpeer *);
void proc_kill_peer(struct tmuxpeer *);
void proc_toggle_log(struct tmuxproc *);
+pid_t proc_fork_and_daemon(int *);
/* cfg.c */
extern int cfg_finished;
client_file_cb, void *);
void file_read(struct client *, const char *, client_file_cb, void *);
void file_push(struct client_file *);
+int file_write_left(struct client_files *);
void file_write_open(struct client_files *, struct tmuxpeer *,
struct imsg *, int, int, client_file_cb, void *);
void file_write_data(struct client_files *, struct imsg *);