2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-06-01 22:58:49 +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>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
2009-11-04 20:50:11 +00:00
|
|
|
#include <event.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <fcntl.h>
|
2009-06-26 22:12:19 +00:00
|
|
|
#include <paths.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Main server functions.
|
|
|
|
*/
|
|
|
|
|
2015-12-15 00:00:01 +00:00
|
|
|
struct clients clients;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2015-12-15 00:00:01 +00:00
|
|
|
struct tmuxproc *server_proc;
|
2019-06-07 20:09:17 +00:00
|
|
|
static int server_fd = -1;
|
2020-06-18 08:34:22 +00:00
|
|
|
static uint64_t server_client_flags;
|
2016-10-10 21:29:23 +00:00
|
|
|
static int server_exit;
|
|
|
|
static struct event server_ev_accept;
|
2021-03-11 07:08:18 +00:00
|
|
|
static struct event server_ev_tidy;
|
2009-10-10 09:31:39 +00:00
|
|
|
|
2015-12-15 00:00:01 +00:00
|
|
|
struct cmd_find_state marked_pane;
|
2015-06-04 11:43:51 +00:00
|
|
|
|
2020-05-16 15:47:22 +00:00
|
|
|
static u_int message_next;
|
|
|
|
struct message_list message_log;
|
|
|
|
|
2022-06-21 09:30:01 +00:00
|
|
|
time_t current_time;
|
|
|
|
|
2016-10-10 21:29:23 +00:00
|
|
|
static int server_loop(void);
|
|
|
|
static void server_send_exit(void);
|
|
|
|
static void server_accept(int, short, void *);
|
|
|
|
static void server_signal(int);
|
|
|
|
static void server_child_signal(void);
|
|
|
|
static void server_child_exited(pid_t, int);
|
|
|
|
static void server_child_stopped(pid_t, int);
|
2015-06-04 11:43:51 +00:00
|
|
|
|
|
|
|
/* Set marked pane. */
|
|
|
|
void
|
|
|
|
server_set_marked(struct session *s, struct winlink *wl, struct window_pane *wp)
|
|
|
|
{
|
2017-04-21 20:26:34 +00:00
|
|
|
cmd_find_clear_state(&marked_pane, 0);
|
2015-12-15 00:00:01 +00:00
|
|
|
marked_pane.s = s;
|
|
|
|
marked_pane.wl = wl;
|
|
|
|
marked_pane.w = wl->window;
|
|
|
|
marked_pane.wp = wp;
|
2015-06-04 11:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear marked pane. */
|
|
|
|
void
|
|
|
|
server_clear_marked(void)
|
|
|
|
{
|
2017-04-21 20:26:34 +00:00
|
|
|
cmd_find_clear_state(&marked_pane, 0);
|
2015-06-04 11:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Is this the marked pane? */
|
|
|
|
int
|
|
|
|
server_is_marked(struct session *s, struct winlink *wl, struct window_pane *wp)
|
|
|
|
{
|
|
|
|
if (s == NULL || wl == NULL || wp == NULL)
|
|
|
|
return (0);
|
2015-12-15 00:00:01 +00:00
|
|
|
if (marked_pane.s != s || marked_pane.wl != wl)
|
2015-06-04 11:43:51 +00:00
|
|
|
return (0);
|
2015-12-15 00:00:01 +00:00
|
|
|
if (marked_pane.wp != wp)
|
2015-06-04 11:43:51 +00:00
|
|
|
return (0);
|
|
|
|
return (server_check_marked());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the marked pane is still valid. */
|
|
|
|
int
|
|
|
|
server_check_marked(void)
|
|
|
|
{
|
2015-12-15 00:00:01 +00:00
|
|
|
return (cmd_find_valid_state(&marked_pane));
|
2015-06-04 11:43:51 +00:00
|
|
|
}
|
2009-10-10 09:31:39 +00:00
|
|
|
|
2009-10-22 19:41:51 +00:00
|
|
|
/* Create server socket. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static int
|
2020-03-12 09:26:34 +00:00
|
|
|
server_create_socket(int flags, char **cause)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-10-22 19:41:51 +00:00
|
|
|
struct sockaddr_un sa;
|
|
|
|
size_t size;
|
|
|
|
mode_t mask;
|
2017-12-19 15:00:39 +00:00
|
|
|
int fd, saved_errno;
|
2009-10-22 19:41:51 +00:00
|
|
|
|
|
|
|
memset(&sa, 0, sizeof sa);
|
|
|
|
sa.sun_family = AF_UNIX;
|
|
|
|
size = strlcpy(sa.sun_path, socket_path, sizeof sa.sun_path);
|
|
|
|
if (size >= sizeof sa.sun_path) {
|
|
|
|
errno = ENAMETOOLONG;
|
2017-12-19 15:00:39 +00:00
|
|
|
goto fail;
|
2009-10-22 19:41:51 +00:00
|
|
|
}
|
|
|
|
unlink(sa.sun_path);
|
|
|
|
|
|
|
|
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
|
2017-12-19 15:00:39 +00:00
|
|
|
goto fail;
|
2009-10-22 19:41:51 +00:00
|
|
|
|
2020-03-12 09:26:34 +00:00
|
|
|
if (flags & CLIENT_DEFAULTSOCKET)
|
|
|
|
mask = umask(S_IXUSR|S_IXGRP|S_IRWXO);
|
|
|
|
else
|
|
|
|
mask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
|
2017-12-19 15:00:39 +00:00
|
|
|
if (bind(fd, (struct sockaddr *)&sa, sizeof sa) == -1) {
|
|
|
|
saved_errno = errno;
|
2017-04-22 06:13:30 +00:00
|
|
|
close(fd);
|
2017-12-19 15:00:39 +00:00
|
|
|
errno = saved_errno;
|
|
|
|
goto fail;
|
2017-04-22 06:13:30 +00:00
|
|
|
}
|
2009-10-22 19:41:51 +00:00
|
|
|
umask(mask);
|
|
|
|
|
2017-04-22 06:13:30 +00:00
|
|
|
if (listen(fd, 128) == -1) {
|
2017-12-19 15:00:39 +00:00
|
|
|
saved_errno = errno;
|
2017-04-22 06:13:30 +00:00
|
|
|
close(fd);
|
2017-12-19 15:00:39 +00:00
|
|
|
errno = saved_errno;
|
|
|
|
goto fail;
|
2017-04-22 06:13:30 +00:00
|
|
|
}
|
2011-01-08 01:52:36 +00:00
|
|
|
setblocking(fd, 0);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-10-22 19:41:51 +00:00
|
|
|
return (fd);
|
2017-12-19 15:00:39 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if (cause != NULL) {
|
|
|
|
xasprintf(cause, "error creating %s (%s)", socket_path,
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2021-03-11 07:08:18 +00:00
|
|
|
/* Tidy up every hour. */
|
|
|
|
static void
|
|
|
|
server_tidy_event(__unused int fd, __unused short events, __unused void *data)
|
|
|
|
{
|
|
|
|
struct timeval tv = { .tv_sec = 3600 };
|
|
|
|
uint64_t t = get_timer();
|
|
|
|
|
|
|
|
format_tidy_jobs();
|
|
|
|
|
2021-06-10 07:45:43 +00:00
|
|
|
log_debug("%s: took %llu milliseconds", __func__,
|
|
|
|
(unsigned long long)(get_timer() - t));
|
2021-03-11 07:08:18 +00:00
|
|
|
evtimer_add(&server_ev_tidy, &tv);
|
|
|
|
}
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
/* Fork new server. */
|
|
|
|
int
|
2020-03-12 09:26:34 +00:00
|
|
|
server_start(struct tmuxproc *client, int flags, struct event_base *base,
|
|
|
|
int lockfd, char *lockfile)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2021-03-11 07:08:18 +00:00
|
|
|
int fd;
|
|
|
|
sigset_t set, oldset;
|
|
|
|
struct client *c = NULL;
|
|
|
|
char *cause = NULL;
|
|
|
|
struct timeval tv = { .tv_sec = 3600 };
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2017-07-12 10:04:51 +00:00
|
|
|
sigfillset(&set);
|
|
|
|
sigprocmask(SIG_BLOCK, &set, &oldset);
|
2020-05-16 16:07:55 +00:00
|
|
|
|
|
|
|
if (~flags & CLIENT_NOFORK) {
|
2021-02-11 09:39:29 +00:00
|
|
|
if (proc_fork_and_daemon(&fd) != 0) {
|
2020-05-16 16:07:55 +00:00
|
|
|
sigprocmask(SIG_SETMASK, &oldset, NULL);
|
2021-02-11 09:39:29 +00:00
|
|
|
return (fd);
|
2020-05-16 16:07:55 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2017-07-14 18:49:07 +00:00
|
|
|
proc_clear_signals(client, 0);
|
2021-02-11 09:39:29 +00:00
|
|
|
server_client_flags = flags;
|
2020-05-16 16:07:55 +00:00
|
|
|
|
2017-07-12 09:24:17 +00:00
|
|
|
if (event_reinit(base) != 0)
|
|
|
|
fatalx("event_reinit failed");
|
|
|
|
server_proc = proc_start("server");
|
2020-05-16 16:07:55 +00:00
|
|
|
|
2017-07-12 09:24:17 +00:00
|
|
|
proc_set_signals(server_proc, server_signal);
|
2017-07-12 10:04:51 +00:00
|
|
|
sigprocmask(SIG_SETMASK, &oldset, NULL);
|
2017-07-12 09:24:17 +00:00
|
|
|
|
2017-06-04 08:25:57 +00:00
|
|
|
if (log_get_level() > 1)
|
2015-10-31 13:12:03 +00:00
|
|
|
tty_create_log();
|
2015-11-22 19:41:19 +00:00
|
|
|
if (pledge("stdio rpath wpath cpath fattr unix getpw recvfd proc exec "
|
|
|
|
"tty ps", NULL) != 0)
|
2015-10-23 16:07:29 +00:00
|
|
|
fatal("pledge failed");
|
|
|
|
|
2020-06-01 09:43:00 +00:00
|
|
|
input_key_build();
|
2015-04-22 15:30:11 +00:00
|
|
|
RB_INIT(&windows);
|
2011-03-27 20:27:26 +00:00
|
|
|
RB_INIT(&all_window_panes);
|
2015-04-24 23:17:11 +00:00
|
|
|
TAILQ_INIT(&clients);
|
2010-12-21 22:37:59 +00:00
|
|
|
RB_INIT(&sessions);
|
2009-06-01 22:58:49 +00:00
|
|
|
key_bindings_init();
|
2020-05-16 15:47:22 +00:00
|
|
|
TAILQ_INIT(&message_log);
|
2015-11-24 21:52:06 +00:00
|
|
|
gettimeofday(&start_time, NULL);
|
2009-08-07 15:39:10 +00:00
|
|
|
|
2020-03-12 09:26:34 +00:00
|
|
|
server_fd = server_create_socket(flags, &cause);
|
2017-12-19 15:00:39 +00:00
|
|
|
if (server_fd != -1)
|
|
|
|
server_update_socket();
|
2020-05-16 16:07:55 +00:00
|
|
|
if (~flags & CLIENT_NOFORK)
|
2021-02-11 09:39:29 +00:00
|
|
|
c = server_client_create(fd);
|
2020-05-16 16:07:55 +00:00
|
|
|
else
|
|
|
|
options_set_number(global_options, "exit-empty", 0);
|
2009-08-07 15:39:10 +00:00
|
|
|
|
2015-11-24 23:01:51 +00:00
|
|
|
if (lockfd >= 0) {
|
|
|
|
unlink(lockfile);
|
|
|
|
free(lockfile);
|
|
|
|
close(lockfd);
|
|
|
|
}
|
2012-03-09 09:57:40 +00:00
|
|
|
|
2017-12-19 15:00:39 +00:00
|
|
|
if (cause != NULL) {
|
2020-09-16 18:37:55 +00:00
|
|
|
if (c != NULL) {
|
2022-03-25 06:14:42 +00:00
|
|
|
c->exit_message = cause;
|
2020-09-16 18:37:55 +00:00
|
|
|
c->flags |= CLIENT_EXIT;
|
2022-03-28 07:40:57 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s\n", cause);
|
|
|
|
exit(1);
|
|
|
|
}
|
2019-06-20 06:51:36 +00:00
|
|
|
}
|
2017-12-19 15:00:39 +00:00
|
|
|
|
2021-03-11 07:08:18 +00:00
|
|
|
evtimer_set(&server_ev_tidy, server_tidy_event, NULL);
|
|
|
|
evtimer_add(&server_ev_tidy, &tv);
|
|
|
|
|
2022-05-30 12:48:57 +00:00
|
|
|
server_acl_init();
|
|
|
|
|
2012-04-11 06:16:14 +00:00
|
|
|
server_add_accept(0);
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_loop(server_proc, server_loop);
|
2017-04-20 09:20:22 +00:00
|
|
|
|
2018-08-23 15:45:05 +00:00
|
|
|
job_kill_all();
|
2015-07-20 15:50:04 +00:00
|
|
|
status_prompt_save_history();
|
2018-08-23 15:45:05 +00:00
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
exit(0);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
/* Server loop callback. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static int
|
2015-10-27 13:23:24 +00:00
|
|
|
server_loop(void)
|
2009-11-04 20:50:11 +00:00
|
|
|
{
|
2014-09-01 21:58:41 +00:00
|
|
|
struct client *c;
|
2016-10-16 17:55:14 +00:00
|
|
|
u_int items;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2022-06-21 09:30:01 +00:00
|
|
|
current_time = time (NULL);
|
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
do {
|
|
|
|
items = cmdq_next(NULL);
|
2016-11-12 19:04:41 +00:00
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
|
|
|
if (c->flags & CLIENT_IDENTIFIED)
|
|
|
|
items += cmdq_next(c);
|
|
|
|
}
|
2016-10-16 17:55:14 +00:00
|
|
|
} while (items != 0);
|
|
|
|
|
|
|
|
server_client_loop();
|
|
|
|
|
2018-02-22 10:54:51 +00:00
|
|
|
if (!options_get_number(global_options, "exit-empty") && !server_exit)
|
|
|
|
return (0);
|
|
|
|
|
2015-10-27 15:58:42 +00:00
|
|
|
if (!options_get_number(global_options, "exit-unattached")) {
|
2010-12-21 22:37:59 +00:00
|
|
|
if (!RB_EMPTY(&sessions))
|
|
|
|
return (0);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2014-09-01 21:58:41 +00:00
|
|
|
|
2015-04-24 23:17:11 +00:00
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
|
|
|
if (c->session != NULL)
|
2014-09-01 21:58:41 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No attached clients therefore want to exit - flush any waiting
|
|
|
|
* clients but don't actually exit until they've gone.
|
|
|
|
*/
|
|
|
|
cmd_wait_for_flush();
|
2015-04-24 23:17:11 +00:00
|
|
|
if (!TAILQ_EMPTY(&clients))
|
|
|
|
return (0);
|
2014-09-01 21:58:41 +00:00
|
|
|
|
2018-08-23 15:45:05 +00:00
|
|
|
if (job_still_running())
|
|
|
|
return (0);
|
2018-03-08 08:09:10 +00:00
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
return (1);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 10:46:24 +00:00
|
|
|
/* Exit the server by killing all clients and windows. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-10-22 10:46:24 +00:00
|
|
|
server_send_exit(void)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2015-04-24 23:17:11 +00:00
|
|
|
struct client *c, *c1;
|
|
|
|
struct session *s, *s1;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2014-09-01 21:58:41 +00:00
|
|
|
cmd_wait_for_flush();
|
|
|
|
|
2015-04-24 23:17:11 +00:00
|
|
|
TAILQ_FOREACH_SAFE(c, &clients, entry, c1) {
|
2015-10-27 13:23:24 +00:00
|
|
|
if (c->flags & CLIENT_SUSPENDED)
|
2015-04-24 23:17:11 +00:00
|
|
|
server_client_lost(c);
|
2018-03-08 08:09:10 +00:00
|
|
|
else {
|
2020-06-01 09:43:00 +00:00
|
|
|
c->flags |= CLIENT_EXIT;
|
|
|
|
c->exit_type = CLIENT_EXIT_SHUTDOWN;
|
2018-03-08 08:09:10 +00:00
|
|
|
}
|
2015-04-24 23:17:11 +00:00
|
|
|
c->session = NULL;
|
2009-09-23 08:21:57 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 23:17:11 +00:00
|
|
|
RB_FOREACH_SAFE(s, sessions, &sessions, s1)
|
2019-04-17 14:37:48 +00:00
|
|
|
session_destroy(s, 1, __func__);
|
2009-09-23 08:21:57 +00:00
|
|
|
}
|
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
/* Update socket execute permissions based on whether sessions are attached. */
|
2009-11-11 08:00:42 +00:00
|
|
|
void
|
2009-11-04 20:50:11 +00:00
|
|
|
server_update_socket(void)
|
2009-09-23 08:21:57 +00:00
|
|
|
{
|
2009-11-04 20:50:11 +00:00
|
|
|
struct session *s;
|
|
|
|
static int last = -1;
|
2010-09-26 18:51:48 +00:00
|
|
|
int n, mode;
|
|
|
|
struct stat sb;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
n = 0;
|
2010-12-21 22:37:59 +00:00
|
|
|
RB_FOREACH(s, sessions, &sessions) {
|
2018-08-18 20:08:52 +00:00
|
|
|
if (s->attached != 0) {
|
2009-11-04 20:50:11 +00:00
|
|
|
n++;
|
|
|
|
break;
|
|
|
|
}
|
2009-09-23 08:21:57 +00:00
|
|
|
}
|
2009-11-04 20:50:11 +00:00
|
|
|
|
|
|
|
if (n != last) {
|
|
|
|
last = n;
|
2010-09-26 18:51:48 +00:00
|
|
|
|
|
|
|
if (stat(socket_path, &sb) != 0)
|
|
|
|
return;
|
2016-07-07 09:24:09 +00:00
|
|
|
mode = sb.st_mode & ACCESSPERMS;
|
2010-09-26 18:51:48 +00:00
|
|
|
if (n != 0) {
|
|
|
|
if (mode & S_IRUSR)
|
|
|
|
mode |= S_IXUSR;
|
|
|
|
if (mode & S_IRGRP)
|
|
|
|
mode |= S_IXGRP;
|
|
|
|
if (mode & S_IROTH)
|
|
|
|
mode |= S_IXOTH;
|
|
|
|
} else
|
|
|
|
mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
|
|
|
|
chmod(socket_path, mode);
|
2009-11-04 20:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for server socket. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-11-18 14:27:44 +00:00
|
|
|
server_accept(int fd, short events, __unused void *data)
|
2009-11-04 20:50:11 +00:00
|
|
|
{
|
2022-05-30 12:48:57 +00:00
|
|
|
struct sockaddr_storage sa;
|
|
|
|
socklen_t slen = sizeof sa;
|
|
|
|
int newfd;
|
|
|
|
struct client *c;
|
2009-11-04 20:50:11 +00:00
|
|
|
|
2012-04-11 06:16:14 +00:00
|
|
|
server_add_accept(0);
|
2009-11-04 20:50:11 +00:00
|
|
|
if (!(events & EV_READ))
|
|
|
|
return;
|
|
|
|
|
|
|
|
newfd = accept(fd, (struct sockaddr *) &sa, &slen);
|
|
|
|
if (newfd == -1) {
|
|
|
|
if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
|
|
|
|
return;
|
2012-04-11 06:16:14 +00:00
|
|
|
if (errno == ENFILE || errno == EMFILE) {
|
|
|
|
/* Delete and don't try again for 1 second. */
|
|
|
|
server_add_accept(1);
|
|
|
|
return;
|
|
|
|
}
|
2009-11-04 20:50:11 +00:00
|
|
|
fatal("accept failed");
|
|
|
|
}
|
2022-05-30 12:48:57 +00:00
|
|
|
|
2015-10-22 10:46:24 +00:00
|
|
|
if (server_exit) {
|
2009-11-04 20:50:11 +00:00
|
|
|
close(newfd);
|
|
|
|
return;
|
|
|
|
}
|
2022-05-30 12:48:57 +00:00
|
|
|
c = server_client_create(newfd);
|
|
|
|
if (!server_acl_join(c)) {
|
|
|
|
c->exit_message = xstrdup("access not allowed");
|
|
|
|
c->flags |= CLIENT_EXIT;
|
|
|
|
}
|
2009-11-04 20:50:11 +00:00
|
|
|
}
|
|
|
|
|
2012-04-11 06:16:14 +00:00
|
|
|
/*
|
|
|
|
* Add accept event. If timeout is nonzero, add as a timeout instead of a read
|
|
|
|
* event - used to backoff when running out of file descriptors.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
server_add_accept(int timeout)
|
|
|
|
{
|
|
|
|
struct timeval tv = { timeout, 0 };
|
|
|
|
|
2019-06-07 20:09:17 +00:00
|
|
|
if (server_fd == -1)
|
|
|
|
return;
|
|
|
|
|
2012-04-11 06:16:14 +00:00
|
|
|
if (event_initialized(&server_ev_accept))
|
|
|
|
event_del(&server_ev_accept);
|
|
|
|
|
|
|
|
if (timeout == 0) {
|
2015-10-27 13:23:24 +00:00
|
|
|
event_set(&server_ev_accept, server_fd, EV_READ, server_accept,
|
|
|
|
NULL);
|
2012-04-11 06:16:14 +00:00
|
|
|
event_add(&server_ev_accept, NULL);
|
|
|
|
} else {
|
2015-10-27 13:23:24 +00:00
|
|
|
event_set(&server_ev_accept, server_fd, EV_TIMEOUT,
|
|
|
|
server_accept, NULL);
|
2012-04-11 06:16:14 +00:00
|
|
|
event_add(&server_ev_accept, &tv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
/* Signal handler. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-10-27 13:23:24 +00:00
|
|
|
server_signal(int sig)
|
2009-11-04 20:50:11 +00:00
|
|
|
{
|
2015-04-21 22:32:40 +00:00
|
|
|
int fd;
|
2015-04-22 15:05:03 +00:00
|
|
|
|
2017-07-09 22:33:09 +00:00
|
|
|
log_debug("%s: %s", __func__, strsignal(sig));
|
2009-11-04 20:50:11 +00:00
|
|
|
switch (sig) {
|
2020-05-16 16:07:55 +00:00
|
|
|
case SIGINT:
|
2009-11-04 20:50:11 +00:00
|
|
|
case SIGTERM:
|
2015-10-22 10:46:24 +00:00
|
|
|
server_exit = 1;
|
|
|
|
server_send_exit();
|
2009-11-04 20:50:11 +00:00
|
|
|
break;
|
|
|
|
case SIGCHLD:
|
|
|
|
server_child_signal();
|
|
|
|
break;
|
|
|
|
case SIGUSR1:
|
|
|
|
event_del(&server_ev_accept);
|
2020-03-12 09:26:34 +00:00
|
|
|
fd = server_create_socket(server_client_flags, NULL);
|
2015-04-21 22:32:40 +00:00
|
|
|
if (fd != -1) {
|
|
|
|
close(server_fd);
|
|
|
|
server_fd = fd;
|
|
|
|
server_update_socket();
|
|
|
|
}
|
2012-04-11 06:16:14 +00:00
|
|
|
server_add_accept(0);
|
2009-11-04 20:50:11 +00:00
|
|
|
break;
|
2017-06-04 08:25:57 +00:00
|
|
|
case SIGUSR2:
|
|
|
|
proc_toggle_log(server_proc);
|
|
|
|
break;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle SIGCHLD. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2009-06-01 22:58:49 +00:00
|
|
|
server_child_signal(void)
|
|
|
|
{
|
2009-11-04 20:50:11 +00:00
|
|
|
int status;
|
|
|
|
pid_t pid;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
|
|
|
|
case -1:
|
|
|
|
if (errno == ECHILD)
|
|
|
|
return;
|
2009-09-20 14:58:12 +00:00
|
|
|
fatal("waitpid failed");
|
2009-06-01 22:58:49 +00:00
|
|
|
case 0:
|
|
|
|
return;
|
|
|
|
}
|
2009-11-04 20:50:11 +00:00
|
|
|
if (WIFSTOPPED(status))
|
|
|
|
server_child_stopped(pid, status);
|
2009-12-02 15:06:14 +00:00
|
|
|
else if (WIFEXITED(status) || WIFSIGNALED(status))
|
2009-11-04 20:50:11 +00:00
|
|
|
server_child_exited(pid, status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle exited children. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2009-11-04 20:50:11 +00:00
|
|
|
server_child_exited(pid_t pid, int status)
|
|
|
|
{
|
2015-04-22 15:30:11 +00:00
|
|
|
struct window *w, *w1;
|
2009-11-04 20:50:11 +00:00
|
|
|
struct window_pane *wp;
|
|
|
|
|
2015-04-22 15:30:11 +00:00
|
|
|
RB_FOREACH_SAFE(w, windows, &windows, w1) {
|
2009-11-04 20:50:11 +00:00
|
|
|
TAILQ_FOREACH(wp, &w->panes, entry) {
|
|
|
|
if (wp->pid == pid) {
|
2014-12-09 19:23:35 +00:00
|
|
|
wp->status = status;
|
2017-10-12 11:32:27 +00:00
|
|
|
wp->flags |= PANE_STATUSREADY;
|
2017-07-03 08:16:03 +00:00
|
|
|
|
|
|
|
log_debug("%%%u exited", wp->id);
|
|
|
|
wp->flags |= PANE_EXITED;
|
|
|
|
|
|
|
|
if (window_pane_destroy_ready(wp))
|
|
|
|
server_destroy_pane(wp, 1);
|
2009-11-13 17:33:07 +00:00
|
|
|
break;
|
2009-11-04 20:50:11 +00:00
|
|
|
}
|
2009-10-11 07:20:16 +00:00
|
|
|
}
|
2009-12-03 22:50:09 +00:00
|
|
|
}
|
2018-08-23 15:45:05 +00:00
|
|
|
job_check_died(pid, status);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
/* Handle stopped children. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2009-11-04 20:50:11 +00:00
|
|
|
server_child_stopped(pid_t pid, int status)
|
2009-09-07 21:01:50 +00:00
|
|
|
{
|
2009-11-04 20:50:11 +00:00
|
|
|
struct window *w;
|
|
|
|
struct window_pane *wp;
|
2009-09-07 21:01:50 +00:00
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
|
|
|
|
return;
|
2009-09-07 21:01:50 +00:00
|
|
|
|
2015-04-22 15:30:11 +00:00
|
|
|
RB_FOREACH(w, windows, &windows) {
|
2009-11-04 20:50:11 +00:00
|
|
|
TAILQ_FOREACH(wp, &w->panes, entry) {
|
|
|
|
if (wp->pid == pid) {
|
|
|
|
if (killpg(pid, SIGCONT) != 0)
|
|
|
|
kill(pid, SIGCONT);
|
|
|
|
}
|
|
|
|
}
|
2009-09-07 21:01:50 +00:00
|
|
|
}
|
2020-05-16 15:24:28 +00:00
|
|
|
job_check_died(pid, status);
|
2009-09-07 21:01:50 +00:00
|
|
|
}
|
2020-05-16 15:47:22 +00:00
|
|
|
|
|
|
|
/* Add to message log. */
|
|
|
|
void
|
|
|
|
server_add_message(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
struct message_entry *msg, *msg1;
|
|
|
|
char *s;
|
|
|
|
va_list ap;
|
|
|
|
u_int limit;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&s, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
log_debug("message: %s", s);
|
|
|
|
|
|
|
|
msg = xcalloc(1, sizeof *msg);
|
|
|
|
gettimeofday(&msg->msg_time, NULL);
|
|
|
|
msg->msg_num = message_next++;
|
|
|
|
msg->msg = s;
|
|
|
|
TAILQ_INSERT_TAIL(&message_log, msg, entry);
|
|
|
|
|
|
|
|
limit = options_get_number(global_options, "message-limit");
|
|
|
|
TAILQ_FOREACH_SAFE(msg, &message_log, entry, msg1) {
|
|
|
|
if (msg->msg_num + limit >= message_next)
|
|
|
|
break;
|
|
|
|
free(msg->msg);
|
|
|
|
TAILQ_REMOVE(&message_log, msg, entry);
|
|
|
|
free(msg);
|
|
|
|
}
|
|
|
|
}
|