2009-10-23 19:49:47 +02:00
|
|
|
/* $Id: server.c,v 1.211 2009-10-23 17:49:47 tcunha Exp $ */
|
2007-07-09 21:04:12 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* 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>
|
2009-02-08 17:11:26 +01:00
|
|
|
#include <sys/wait.h>
|
2007-07-09 21:04:12 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <termios.h>
|
2008-06-06 19:20:30 +02:00
|
|
|
#include <time.h>
|
2007-07-09 21:04:12 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Main server functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Client list. */
|
|
|
|
struct clients clients;
|
2009-09-08 01:59:19 +02:00
|
|
|
struct clients dead_clients;
|
2007-07-09 21:04:12 +02:00
|
|
|
|
2009-10-12 01:25:44 +02:00
|
|
|
/* Mapping of a pollfd to an fd independent of its position in the array. */
|
|
|
|
struct poll_item {
|
2009-10-23 19:49:47 +02:00
|
|
|
int fd;
|
|
|
|
int events;
|
|
|
|
|
|
|
|
void (*fn)(int, int, void *);
|
|
|
|
void *data;
|
2009-10-12 01:25:44 +02:00
|
|
|
|
|
|
|
RB_ENTRY(poll_item) entry;
|
|
|
|
};
|
|
|
|
RB_HEAD(poll_items, poll_item) poll_items;
|
|
|
|
|
|
|
|
int server_poll_cmp(struct poll_item *, struct poll_item *);
|
2009-10-23 19:49:47 +02:00
|
|
|
struct poll_item*server_poll_lookup(int);
|
|
|
|
void server_poll_add(int, int, void (*)(int, int, void *), void *);
|
2009-10-12 01:25:44 +02:00
|
|
|
struct pollfd *server_poll_flatten(int *);
|
2009-10-23 19:49:47 +02:00
|
|
|
void server_poll_dispatch(struct pollfd *, int);
|
2009-10-12 01:25:44 +02:00
|
|
|
void server_poll_reset(void);
|
|
|
|
RB_PROTOTYPE(poll_items, poll_item, entry, server_poll_cmp);
|
|
|
|
RB_GENERATE(poll_items, poll_item, entry, server_poll_cmp);
|
|
|
|
|
2009-05-16 12:02:51 +02:00
|
|
|
int server_create_socket(void);
|
2009-10-23 19:49:47 +02:00
|
|
|
void server_callback(int, int, void *);
|
2009-03-27 16:57:10 +01:00
|
|
|
int server_main(int);
|
2009-01-21 23:47:31 +01:00
|
|
|
void server_shutdown(void);
|
2009-09-23 17:10:37 +02:00
|
|
|
int server_should_shutdown(void);
|
2009-02-08 17:11:26 +01:00
|
|
|
void server_child_signal(void);
|
2009-10-12 01:25:44 +02:00
|
|
|
void server_fill_windows(void);
|
|
|
|
void server_fill_clients(void);
|
2009-10-12 01:55:26 +02:00
|
|
|
void server_fill_jobs(void);
|
2009-09-08 01:59:19 +02:00
|
|
|
void server_clean_dead(void);
|
2009-10-23 19:49:47 +02:00
|
|
|
void server_second_timers(void);
|
2009-10-12 01:30:28 +02:00
|
|
|
void server_lock_server(void);
|
|
|
|
void server_lock_sessions(void);
|
2009-03-27 16:57:10 +01:00
|
|
|
int server_update_socket(void);
|
2007-09-26 20:09:23 +02:00
|
|
|
|
2009-10-12 01:25:44 +02:00
|
|
|
int
|
|
|
|
server_poll_cmp(struct poll_item *pitem1, struct poll_item *pitem2)
|
|
|
|
{
|
2009-10-23 19:49:47 +02:00
|
|
|
return (pitem1->fd - pitem2->fd);
|
2009-10-12 01:25:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-10-23 19:49:47 +02:00
|
|
|
server_poll_add(int fd, int events, void (*fn)(int, int, void *), void *data)
|
2009-10-12 01:25:44 +02:00
|
|
|
{
|
|
|
|
struct poll_item *pitem;
|
|
|
|
|
|
|
|
pitem = xmalloc(sizeof *pitem);
|
2009-10-23 19:49:47 +02:00
|
|
|
pitem->fd = fd;
|
|
|
|
pitem->events = events;
|
|
|
|
|
|
|
|
pitem->fn = fn;
|
|
|
|
pitem->data = data;
|
|
|
|
|
2009-10-12 01:25:44 +02:00
|
|
|
RB_INSERT(poll_items, &poll_items, pitem);
|
|
|
|
}
|
|
|
|
|
2009-10-23 19:49:47 +02:00
|
|
|
struct poll_item *
|
|
|
|
server_poll_lookup(int fd)
|
|
|
|
{
|
|
|
|
struct poll_item pitem;
|
|
|
|
|
|
|
|
pitem.fd = fd;
|
|
|
|
return (RB_FIND(poll_items, &poll_items, &pitem));
|
|
|
|
}
|
|
|
|
|
2009-10-12 01:25:44 +02:00
|
|
|
struct pollfd *
|
|
|
|
server_poll_flatten(int *nfds)
|
|
|
|
{
|
|
|
|
struct poll_item *pitem;
|
|
|
|
struct pollfd *pfds;
|
|
|
|
|
|
|
|
pfds = NULL;
|
|
|
|
*nfds = 0;
|
|
|
|
RB_FOREACH(pitem, poll_items, &poll_items) {
|
|
|
|
pfds = xrealloc(pfds, (*nfds) + 1, sizeof *pfds);
|
2009-10-23 19:49:47 +02:00
|
|
|
pfds[*nfds].fd = pitem->fd;
|
|
|
|
pfds[*nfds].events = pitem->events;
|
2009-10-12 01:25:44 +02:00
|
|
|
(*nfds)++;
|
|
|
|
}
|
|
|
|
return (pfds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-10-23 19:49:47 +02:00
|
|
|
server_poll_dispatch(struct pollfd *pfds, int nfds)
|
2009-10-12 01:25:44 +02:00
|
|
|
{
|
|
|
|
struct poll_item *pitem;
|
2009-10-23 19:49:47 +02:00
|
|
|
struct pollfd *pfd;
|
2009-10-12 01:25:44 +02:00
|
|
|
|
2009-10-23 19:49:47 +02:00
|
|
|
while (nfds > 0) {
|
|
|
|
pfd = &pfds[--nfds];
|
|
|
|
if (pfd->revents != 0) {
|
|
|
|
pitem = server_poll_lookup(pfd->fd);
|
|
|
|
pitem->fn(pitem->fd, pfd->revents, pitem->data);
|
|
|
|
}
|
2009-10-12 01:25:44 +02:00
|
|
|
}
|
|
|
|
xfree(pfds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
server_poll_reset(void)
|
|
|
|
{
|
|
|
|
struct poll_item *pitem;
|
|
|
|
|
|
|
|
while (!RB_EMPTY(&poll_items)) {
|
|
|
|
pitem = RB_ROOT(&poll_items);
|
|
|
|
RB_REMOVE(poll_items, &poll_items, pitem);
|
|
|
|
xfree(pitem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-23 19:49:47 +02:00
|
|
|
/* Create server socket. */
|
|
|
|
int
|
|
|
|
server_create_socket(void)
|
2009-01-19 18:16:09 +01:00
|
|
|
{
|
2009-10-23 19:49:47 +02:00
|
|
|
struct sockaddr_un sa;
|
|
|
|
size_t size;
|
|
|
|
mode_t mask;
|
|
|
|
int fd, mode;
|
|
|
|
|
|
|
|
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;
|
|
|
|
fatal("socket failed");
|
|
|
|
}
|
|
|
|
unlink(sa.sun_path);
|
|
|
|
|
|
|
|
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
|
|
|
|
fatal("socket failed");
|
|
|
|
|
|
|
|
mask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
|
|
|
|
if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1)
|
|
|
|
fatal("bind failed");
|
|
|
|
umask(mask);
|
|
|
|
|
|
|
|
if (listen(fd, 16) == -1)
|
|
|
|
fatal("listen failed");
|
2009-01-19 18:16:09 +01:00
|
|
|
|
|
|
|
if ((mode = fcntl(fd, F_GETFL)) == -1)
|
|
|
|
fatal("fcntl failed");
|
|
|
|
if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
|
|
|
|
fatal("fcntl failed");
|
2009-04-01 00:20:42 +02:00
|
|
|
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
|
|
|
|
fatal("fcntl failed");
|
2009-01-19 18:16:09 +01:00
|
|
|
|
2009-10-23 19:49:47 +02:00
|
|
|
return (fd);
|
|
|
|
}
|
2009-01-19 18:16:09 +01:00
|
|
|
|
2009-10-23 19:49:47 +02:00
|
|
|
/* Callback for server socket. */
|
|
|
|
void
|
|
|
|
server_callback(int fd, int events, unused void *data)
|
|
|
|
{
|
|
|
|
struct sockaddr_storage sa;
|
|
|
|
socklen_t slen = sizeof sa;
|
|
|
|
int newfd;
|
2009-01-19 18:16:09 +01:00
|
|
|
|
2009-10-23 19:49:47 +02:00
|
|
|
if (events & (POLLERR|POLLNVAL|POLLHUP))
|
|
|
|
fatalx("lost server socket");
|
|
|
|
if (!(events & POLLIN))
|
|
|
|
return;
|
2009-01-19 18:16:09 +01:00
|
|
|
|
2009-10-23 19:49:47 +02:00
|
|
|
newfd = accept(fd, (struct sockaddr *) &sa, &slen);
|
|
|
|
if (newfd == -1) {
|
|
|
|
if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
|
2009-07-22 19:46:53 +02:00
|
|
|
return;
|
2009-10-23 19:49:47 +02:00
|
|
|
fatal("accept failed");
|
2009-01-19 18:16:09 +01:00
|
|
|
}
|
2009-10-23 19:49:47 +02:00
|
|
|
if (sigterm) {
|
|
|
|
close(newfd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
server_client_create(newfd);
|
2009-01-19 18:16:09 +01:00
|
|
|
}
|
|
|
|
|
2007-09-26 20:09:23 +02:00
|
|
|
/* Fork new server. */
|
2008-06-07 09:27:28 +02:00
|
|
|
int
|
2009-03-27 18:04:04 +01:00
|
|
|
server_start(char *path)
|
2007-07-09 21:04:12 +02:00
|
|
|
{
|
2009-08-09 19:19:18 +02:00
|
|
|
struct client *c;
|
|
|
|
int pair[2], srv_fd;
|
|
|
|
char *cause;
|
2009-05-14 01:27:00 +02:00
|
|
|
#ifdef HAVE_SETPROCTITLE
|
2009-08-09 19:19:18 +02:00
|
|
|
char rpathbuf[MAXPATHLEN];
|
2009-03-27 16:57:10 +01:00
|
|
|
#endif
|
2008-06-07 09:27:28 +02:00
|
|
|
|
2009-01-19 18:16:09 +01:00
|
|
|
/* The first client is special and gets a socketpair; create it. */
|
2008-06-07 09:27:28 +02:00
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
|
|
|
|
fatal("socketpair failed");
|
2007-07-09 21:04:12 +02:00
|
|
|
|
2008-06-07 09:27:28 +02:00
|
|
|
switch (fork()) {
|
2007-07-09 21:04:12 +02:00
|
|
|
case -1:
|
2008-06-07 09:27:28 +02:00
|
|
|
fatal("fork failed");
|
2007-07-09 21:04:12 +02:00
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
2008-06-07 09:27:28 +02:00
|
|
|
close(pair[1]);
|
2009-01-19 18:16:09 +01:00
|
|
|
return (pair[0]);
|
2007-07-09 21:04:12 +02:00
|
|
|
}
|
2008-06-07 09:27:28 +02:00
|
|
|
close(pair[0]);
|
2007-07-09 21:04:12 +02:00
|
|
|
|
2008-06-19 00:21:51 +02:00
|
|
|
/*
|
2008-06-02 23:16:21 +02:00
|
|
|
* Must daemonise before loading configuration as the PID changes so
|
|
|
|
* $TMUX would be wrong for sessions created in the config file.
|
|
|
|
*/
|
2009-08-09 19:19:18 +02:00
|
|
|
if (daemon(1, 0) != 0)
|
2008-06-02 23:16:21 +02:00
|
|
|
fatal("daemon failed");
|
|
|
|
|
2009-08-09 19:19:18 +02:00
|
|
|
logfile("server");
|
|
|
|
log_debug("server started, pid %ld", (long) getpid());
|
|
|
|
|
2008-06-02 23:08:36 +02:00
|
|
|
ARRAY_INIT(&windows);
|
|
|
|
ARRAY_INIT(&clients);
|
2009-09-08 01:59:19 +02:00
|
|
|
ARRAY_INIT(&dead_clients);
|
2008-06-02 23:08:36 +02:00
|
|
|
ARRAY_INIT(&sessions);
|
2009-09-08 01:59:19 +02:00
|
|
|
ARRAY_INIT(&dead_sessions);
|
2009-10-12 01:38:16 +02:00
|
|
|
TAILQ_INIT(&session_groups);
|
2009-07-29 01:11:18 +02:00
|
|
|
mode_key_init_trees();
|
2008-06-02 23:08:36 +02:00
|
|
|
key_bindings_init();
|
2009-04-30 22:54:53 +02:00
|
|
|
utf8_build();
|
2008-06-02 23:08:36 +02:00
|
|
|
|
2009-03-04 18:33:30 +01:00
|
|
|
start_time = time(NULL);
|
|
|
|
socket_path = path;
|
|
|
|
|
2009-05-14 01:27:00 +02:00
|
|
|
#ifdef HAVE_SETPROCTITLE
|
2009-03-27 16:57:10 +01:00
|
|
|
if (realpath(socket_path, rpathbuf) == NULL)
|
|
|
|
strlcpy(rpathbuf, socket_path, sizeof rpathbuf);
|
2009-08-09 19:19:18 +02:00
|
|
|
log_debug("socket path %s", socket_path);
|
2009-03-27 16:57:10 +01:00
|
|
|
setproctitle("server (%s)", rpathbuf);
|
|
|
|
#endif
|
2009-05-04 19:58:27 +02:00
|
|
|
|
2009-05-16 12:02:51 +02:00
|
|
|
srv_fd = server_create_socket();
|
2009-10-23 19:49:47 +02:00
|
|
|
server_client_create(pair[1]);
|
2009-05-16 12:02:51 +02:00
|
|
|
|
2009-08-09 19:19:18 +02:00
|
|
|
if (access(SYSTEM_CFG, R_OK) != 0) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
xasprintf(
|
|
|
|
&cause, "%s: %s", strerror(errno), SYSTEM_CFG);
|
|
|
|
goto error;
|
|
|
|
}
|
2009-08-24 18:27:03 +02:00
|
|
|
} else if (load_cfg(SYSTEM_CFG, NULL, &cause) != 0)
|
2009-08-09 19:19:18 +02:00
|
|
|
goto error;
|
2009-08-24 18:27:03 +02:00
|
|
|
if (cfg_file != NULL && load_cfg(cfg_file, NULL, &cause) != 0)
|
2009-08-09 19:19:18 +02:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
exit(server_main(srv_fd));
|
|
|
|
|
|
|
|
error:
|
|
|
|
/* Write the error and shutdown the server. */
|
|
|
|
c = ARRAY_FIRST(&clients);
|
|
|
|
|
|
|
|
server_write_error(c, cause);
|
|
|
|
xfree(cause);
|
|
|
|
|
2009-09-02 23:36:00 +02:00
|
|
|
sigterm = 1;
|
2009-08-09 19:19:18 +02:00
|
|
|
server_shutdown();
|
|
|
|
|
2009-06-25 18:34:50 +02:00
|
|
|
exit(server_main(srv_fd));
|
2009-05-16 12:02:51 +02:00
|
|
|
}
|
|
|
|
|
2007-07-09 21:04:12 +02:00
|
|
|
/* Main server loop. */
|
|
|
|
int
|
2009-03-27 16:57:10 +01:00
|
|
|
server_main(int srv_fd)
|
2007-07-09 21:04:12 +02:00
|
|
|
{
|
2009-10-23 19:49:47 +02:00
|
|
|
struct pollfd *pfds;
|
2008-12-13 19:06:08 +01:00
|
|
|
int nfds, xtimeout;
|
2009-09-23 17:10:37 +02:00
|
|
|
u_int i;
|
2009-01-10 20:35:40 +01:00
|
|
|
time_t now, last;
|
2007-09-26 15:43:15 +02:00
|
|
|
|
|
|
|
siginit();
|
2009-08-14 23:24:46 +02:00
|
|
|
log_debug("server socket is %d", srv_fd);
|
2008-06-19 00:21:51 +02:00
|
|
|
|
2009-01-10 20:35:40 +01:00
|
|
|
last = time(NULL);
|
|
|
|
|
2007-07-09 21:04:12 +02:00
|
|
|
pfds = NULL;
|
2009-01-21 23:47:31 +01:00
|
|
|
for (;;) {
|
|
|
|
/* If sigterm, kill all windows and clients. */
|
|
|
|
if (sigterm)
|
|
|
|
server_shutdown();
|
|
|
|
|
2009-09-23 17:10:37 +02:00
|
|
|
/* Stop if no sessions or clients left. */
|
|
|
|
if (server_should_shutdown())
|
|
|
|
break;
|
|
|
|
|
2009-02-08 17:11:26 +01:00
|
|
|
/* Handle child exit. */
|
|
|
|
if (sigchld) {
|
|
|
|
server_child_signal();
|
|
|
|
sigchld = 0;
|
|
|
|
}
|
2009-05-16 12:02:51 +02:00
|
|
|
|
|
|
|
/* Recreate socket on SIGUSR1. */
|
|
|
|
if (sigusr1) {
|
|
|
|
close(srv_fd);
|
|
|
|
srv_fd = server_create_socket();
|
|
|
|
sigusr1 = 0;
|
|
|
|
}
|
2009-02-08 17:11:26 +01:00
|
|
|
|
2009-10-12 01:25:44 +02:00
|
|
|
/* Initialise pollfd array and add server socket. */
|
|
|
|
server_poll_reset();
|
2009-10-23 19:49:47 +02:00
|
|
|
server_poll_add(srv_fd, POLLIN, server_callback, NULL);
|
2007-07-09 21:04:12 +02:00
|
|
|
|
|
|
|
/* Fill window and client sockets. */
|
2009-10-12 01:55:26 +02:00
|
|
|
server_fill_jobs();
|
2009-10-12 01:25:44 +02:00
|
|
|
server_fill_windows();
|
|
|
|
server_fill_clients();
|
2007-07-09 21:04:12 +02:00
|
|
|
|
2008-12-13 19:06:08 +01:00
|
|
|
/* Update socket permissions. */
|
|
|
|
xtimeout = INFTIM;
|
2009-09-02 23:36:00 +02:00
|
|
|
if (server_update_socket() != 0)
|
2009-05-02 10:34:39 +02:00
|
|
|
xtimeout = POLL_TIMEOUT;
|
2008-12-13 19:06:08 +01:00
|
|
|
|
2007-07-09 21:04:12 +02:00
|
|
|
/* Do the poll. */
|
2009-10-12 01:25:44 +02:00
|
|
|
pfds = server_poll_flatten(&nfds);
|
2009-06-26 17:32:00 +02:00
|
|
|
if (poll(pfds, nfds, xtimeout) == -1) {
|
2007-07-09 21:04:12 +02:00
|
|
|
if (errno == EAGAIN || errno == EINTR)
|
|
|
|
continue;
|
2007-07-26 01:13:18 +02:00
|
|
|
fatal("poll failed");
|
2007-07-09 21:04:12 +02:00
|
|
|
}
|
2009-10-23 19:49:47 +02:00
|
|
|
server_poll_dispatch(pfds, nfds);
|
2007-07-09 21:04:12 +02:00
|
|
|
|
2009-01-11 01:48:42 +01:00
|
|
|
/* Call second-based timers. */
|
2009-01-10 20:35:40 +01:00
|
|
|
now = time(NULL);
|
|
|
|
if (now != last) {
|
|
|
|
last = now;
|
|
|
|
server_second_timers();
|
|
|
|
}
|
|
|
|
|
2009-10-23 19:49:47 +02:00
|
|
|
/* Run once-per-loop events. */
|
|
|
|
server_job_loop();
|
|
|
|
server_window_loop();
|
|
|
|
server_client_loop();
|
2008-06-06 19:55:27 +02:00
|
|
|
|
2009-07-14 08:39:25 +02:00
|
|
|
/* Collect any unset key bindings. */
|
|
|
|
key_bindings_clean();
|
2009-09-08 01:59:19 +02:00
|
|
|
|
|
|
|
/* Collect dead clients and sessions. */
|
|
|
|
server_clean_dead();
|
2007-07-09 21:04:12 +02:00
|
|
|
}
|
2009-10-12 01:25:44 +02:00
|
|
|
server_poll_reset();
|
2007-07-09 21:04:12 +02:00
|
|
|
|
2007-10-24 13:05:59 +02:00
|
|
|
for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
|
|
|
|
if (ARRAY_ITEM(&sessions, i) != NULL)
|
|
|
|
session_destroy(ARRAY_ITEM(&sessions, i));
|
|
|
|
}
|
|
|
|
ARRAY_FREE(&sessions);
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
|
|
|
|
if (ARRAY_ITEM(&clients, i) != NULL)
|
2009-10-23 19:49:47 +02:00
|
|
|
server_client_lost(ARRAY_ITEM(&clients, i));
|
2007-10-24 13:05:59 +02:00
|
|
|
}
|
|
|
|
ARRAY_FREE(&clients);
|
|
|
|
|
2009-07-29 01:11:18 +02:00
|
|
|
mode_key_free_trees();
|
2007-10-03 23:31:07 +02:00
|
|
|
key_bindings_free();
|
2007-10-03 13:26:34 +02:00
|
|
|
|
2007-07-09 21:04:12 +02:00
|
|
|
close(srv_fd);
|
2009-03-27 16:57:10 +01:00
|
|
|
|
|
|
|
unlink(socket_path);
|
|
|
|
xfree(socket_path);
|
2007-07-09 21:04:12 +02:00
|
|
|
|
2009-07-08 20:01:31 +02:00
|
|
|
options_free(&global_s_options);
|
|
|
|
options_free(&global_w_options);
|
2009-02-16 20:29:17 +01:00
|
|
|
|
2007-07-09 21:04:12 +02:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2009-01-21 23:47:31 +01:00
|
|
|
/* Kill all clients. */
|
|
|
|
void
|
|
|
|
server_shutdown(void)
|
|
|
|
{
|
|
|
|
struct session *s;
|
|
|
|
struct client *c;
|
2009-01-29 21:13:12 +01:00
|
|
|
u_int i, j;
|
2009-01-21 23:47:31 +01:00
|
|
|
|
2009-09-23 17:10:37 +02:00
|
|
|
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
|
|
|
|
c = ARRAY_ITEM(&clients, i);
|
|
|
|
if (c != NULL) {
|
|
|
|
if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
|
2009-10-23 19:49:47 +02:00
|
|
|
server_client_lost(c);
|
2009-09-23 17:10:37 +02:00
|
|
|
else
|
|
|
|
server_write_client(c, MSG_SHUTDOWN, NULL, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-21 23:47:31 +01:00
|
|
|
for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
|
|
|
|
s = ARRAY_ITEM(&sessions, i);
|
2009-01-29 21:13:12 +01:00
|
|
|
for (j = 0; j < ARRAY_LENGTH(&clients); j++) {
|
|
|
|
c = ARRAY_ITEM(&clients, j);
|
2009-01-21 23:47:31 +01:00
|
|
|
if (c != NULL && c->session == s) {
|
|
|
|
s = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (s != NULL)
|
|
|
|
session_destroy(s);
|
|
|
|
}
|
2009-09-23 17:10:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the server should be shutting down (no more clients or windows). */
|
|
|
|
int
|
|
|
|
server_should_shutdown(void)
|
|
|
|
{
|
|
|
|
u_int i;
|
2009-01-21 23:47:31 +01:00
|
|
|
|
2009-09-23 17:10:37 +02:00
|
|
|
for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
|
|
|
|
if (ARRAY_ITEM(&sessions, i) != NULL)
|
|
|
|
return (0);
|
|
|
|
}
|
2009-01-21 23:47:31 +01:00
|
|
|
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
|
2009-09-23 17:10:37 +02:00
|
|
|
if (ARRAY_ITEM(&clients, i) != NULL)
|
|
|
|
return (0);
|
2009-01-21 23:47:31 +01:00
|
|
|
}
|
2009-09-23 17:10:37 +02:00
|
|
|
return (1);
|
2009-01-21 23:47:31 +01:00
|
|
|
}
|
2009-02-08 17:11:26 +01:00
|
|
|
|
|
|
|
/* Handle SIGCHLD. */
|
|
|
|
void
|
|
|
|
server_child_signal(void)
|
|
|
|
{
|
|
|
|
struct window *w;
|
|
|
|
struct window_pane *wp;
|
2009-10-12 02:21:08 +02:00
|
|
|
struct job *job;
|
2009-02-08 17:11:26 +01:00
|
|
|
int status;
|
|
|
|
pid_t pid;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
|
|
|
|
case -1:
|
|
|
|
if (errno == ECHILD)
|
|
|
|
return;
|
2009-09-21 00:11:27 +02:00
|
|
|
fatal("waitpid failed");
|
2009-02-08 17:11:26 +01:00
|
|
|
case 0:
|
|
|
|
return;
|
|
|
|
}
|
2009-10-12 02:21:08 +02:00
|
|
|
if (!WIFSTOPPED(status)) {
|
|
|
|
SLIST_FOREACH(job, &all_jobs, lentry) {
|
|
|
|
if (pid == job->pid) {
|
|
|
|
job->pid = -1;
|
|
|
|
job->status = status;
|
|
|
|
}
|
|
|
|
}
|
2009-02-08 17:11:26 +01:00
|
|
|
continue;
|
2009-10-12 02:21:08 +02:00
|
|
|
}
|
2009-02-08 17:11:26 +01:00
|
|
|
if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
|
|
|
|
w = ARRAY_ITEM(&windows, i);
|
|
|
|
if (w == NULL)
|
|
|
|
continue;
|
|
|
|
TAILQ_FOREACH(wp, &w->panes, entry) {
|
|
|
|
if (wp->pid == pid) {
|
|
|
|
if (killpg(pid, SIGCONT) != 0)
|
|
|
|
kill(pid, SIGCONT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-04 19:58:27 +02:00
|
|
|
|
2007-07-09 21:04:12 +02:00
|
|
|
/* Fill window pollfds. */
|
|
|
|
void
|
2009-10-12 01:25:44 +02:00
|
|
|
server_fill_windows(void)
|
2007-07-09 21:04:12 +02:00
|
|
|
{
|
2009-01-12 00:31:46 +01:00
|
|
|
struct window *w;
|
|
|
|
struct window_pane *wp;
|
2009-01-14 20:29:32 +01:00
|
|
|
u_int i;
|
2009-10-12 01:25:44 +02:00
|
|
|
int events;
|
2007-07-09 21:04:12 +02:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
|
2009-01-12 00:31:46 +01:00
|
|
|
w = ARRAY_ITEM(&windows, i);
|
2009-01-14 20:29:32 +01:00
|
|
|
if (w == NULL)
|
|
|
|
continue;
|
|
|
|
|
2009-10-12 01:25:44 +02:00
|
|
|
TAILQ_FOREACH(wp, &w->panes, entry) {
|
|
|
|
if (wp->fd == -1)
|
|
|
|
continue;
|
|
|
|
events = POLLIN;
|
|
|
|
if (BUFFER_USED(wp->out) > 0)
|
|
|
|
events |= POLLOUT;
|
2009-10-23 19:49:47 +02:00
|
|
|
server_poll_add(
|
|
|
|
wp->fd, events, server_window_callback, wp);
|
2009-10-12 02:35:08 +02:00
|
|
|
|
|
|
|
if (wp->pipe_fd == -1)
|
|
|
|
continue;
|
|
|
|
events = 0;
|
|
|
|
if (BUFFER_USED(wp->pipe_buf) > 0)
|
|
|
|
events |= POLLOUT;
|
2009-10-23 19:49:47 +02:00
|
|
|
server_poll_add(
|
|
|
|
wp->pipe_fd, events, server_window_callback, wp);
|
2009-04-02 23:08:15 +02:00
|
|
|
}
|
2009-01-12 00:31:46 +01:00
|
|
|
}
|
2008-06-07 08:13:21 +02:00
|
|
|
}
|
|
|
|
|
2007-07-09 21:04:12 +02:00
|
|
|
/* Fill client pollfds. */
|
|
|
|
void
|
2009-10-12 01:25:44 +02:00
|
|
|
server_fill_clients(void)
|
2007-07-09 21:04:12 +02:00
|
|
|
{
|
2009-10-12 02:14:44 +02:00
|
|
|
struct client *c;
|
|
|
|
u_int i;
|
|
|
|
int events;
|
2007-07-09 21:04:12 +02:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
|
2007-11-27 20:23:34 +01:00
|
|
|
c = ARRAY_ITEM(&clients, i);
|
|
|
|
|
2009-10-12 01:25:44 +02:00
|
|
|
if (c != NULL) {
|
|
|
|
events = 0;
|
2009-08-09 19:19:18 +02:00
|
|
|
if (!(c->flags & CLIENT_BAD))
|
2009-10-12 01:25:44 +02:00
|
|
|
events |= POLLIN;
|
2009-08-14 23:04:04 +02:00
|
|
|
if (c->ibuf.w.queued > 0)
|
2009-10-12 01:25:44 +02:00
|
|
|
events |= POLLOUT;
|
2009-10-23 19:49:47 +02:00
|
|
|
server_poll_add(
|
|
|
|
c->ibuf.fd, events, server_client_callback, c);
|
2007-07-09 21:04:12 +02:00
|
|
|
}
|
2009-10-12 01:25:44 +02:00
|
|
|
|
|
|
|
if (c != NULL && !(c->flags & CLIENT_SUSPENDED) &&
|
|
|
|
c->tty.fd != -1 && c->session != NULL) {
|
|
|
|
events = POLLIN;
|
2007-11-27 20:23:34 +01:00
|
|
|
if (BUFFER_USED(c->tty.out) > 0)
|
2009-10-12 01:25:44 +02:00
|
|
|
events |= POLLOUT;
|
2009-10-23 19:49:47 +02:00
|
|
|
server_poll_add(
|
|
|
|
c->tty.fd, events, server_client_callback, c);
|
2007-11-27 20:23:34 +01:00
|
|
|
}
|
2007-07-09 21:04:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-12 01:55:26 +02:00
|
|
|
/* Fill in job fds. */
|
|
|
|
void
|
|
|
|
server_fill_jobs(void)
|
|
|
|
{
|
|
|
|
struct job *job;
|
|
|
|
|
2009-10-12 02:12:33 +02:00
|
|
|
SLIST_FOREACH(job, &all_jobs, lentry) {
|
2009-10-12 01:55:26 +02:00
|
|
|
if (job->fd == -1)
|
|
|
|
continue;
|
2009-10-23 19:49:47 +02:00
|
|
|
server_poll_add(job->fd, POLLIN, server_job_callback, job);
|
2009-10-12 01:55:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-08 01:59:19 +02:00
|
|
|
/* Free dead, unreferenced clients and sessions. */
|
|
|
|
void
|
|
|
|
server_clean_dead(void)
|
|
|
|
{
|
|
|
|
struct session *s;
|
|
|
|
struct client *c;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&dead_sessions); i++) {
|
|
|
|
s = ARRAY_ITEM(&dead_sessions, i);
|
|
|
|
if (s == NULL || s->references != 0)
|
|
|
|
continue;
|
|
|
|
ARRAY_SET(&dead_sessions, i, NULL);
|
|
|
|
xfree(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
|
|
|
|
c = ARRAY_ITEM(&dead_clients, i);
|
|
|
|
if (c == NULL || c->references != 0)
|
|
|
|
continue;
|
|
|
|
ARRAY_SET(&dead_clients, i, NULL);
|
|
|
|
xfree(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-23 19:49:47 +02:00
|
|
|
/* Call any once-per-second timers. */
|
2009-01-14 20:29:32 +01:00
|
|
|
void
|
2009-10-23 19:49:47 +02:00
|
|
|
server_second_timers(void)
|
2007-07-09 21:04:12 +02:00
|
|
|
{
|
2009-10-23 19:49:47 +02:00
|
|
|
struct window *w;
|
|
|
|
struct window_pane *wp;
|
2009-09-13 22:43:21 +02:00
|
|
|
u_int i;
|
2009-01-14 20:29:32 +01:00
|
|
|
|
2009-10-23 19:49:47 +02:00
|
|
|
if (options_get_number(&global_s_options, "lock-server"))
|
|
|
|
server_lock_server();
|
|
|
|
else
|
|
|
|
server_lock_sessions();
|
2008-06-29 09:04:31 +02:00
|
|
|
|
2009-10-23 19:49:47 +02:00
|
|
|
for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
|
|
|
|
w = ARRAY_ITEM(&windows, i);
|
|
|
|
if (w == NULL)
|
2007-08-27 14:05:15 +02:00
|
|
|
continue;
|
|
|
|
|
2009-10-23 19:49:47 +02:00
|
|
|
TAILQ_FOREACH(wp, &w->panes, entry) {
|
|
|
|
if (wp->mode != NULL && wp->mode->timer != NULL)
|
|
|
|
wp->mode->timer(wp);
|
2007-07-09 21:04:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-12 01:30:28 +02:00
|
|
|
/* Lock the server if ALL sessions have hit the time limit. */
|
|
|
|
void
|
|
|
|
server_lock_server(void)
|
|
|
|
{
|
|
|
|
struct session *s;
|
|
|
|
u_int i;
|
|
|
|
int timeout;
|
|
|
|
time_t t;
|
|
|
|
|
|
|
|
t = time(NULL);
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
|
|
|
|
if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
|
|
|
|
continue;
|
|
|
|
|
2009-10-15 03:43:16 +02:00
|
|
|
if (s->flags & SESSION_UNATTACHED) {
|
|
|
|
s->activity = time(NULL);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-10-12 01:30:28 +02:00
|
|
|
timeout = options_get_number(&s->options, "lock-after-time");
|
|
|
|
if (timeout <= 0 || t <= s->activity + timeout)
|
|
|
|
return; /* not timed out */
|
|
|
|
}
|
|
|
|
|
|
|
|
server_lock();
|
|
|
|
recalculate_sizes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lock any sessions which have timed out. */
|
|
|
|
void
|
|
|
|
server_lock_sessions(void)
|
|
|
|
{
|
|
|
|
struct session *s;
|
|
|
|
u_int i;
|
|
|
|
int timeout;
|
|
|
|
time_t t;
|
|
|
|
|
|
|
|
t = time(NULL);
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
|
|
|
|
if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
|
|
|
|
continue;
|
|
|
|
|
2009-10-15 03:43:16 +02:00
|
|
|
if (s->flags & SESSION_UNATTACHED) {
|
|
|
|
s->activity = time(NULL);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-10-12 01:30:28 +02:00
|
|
|
timeout = options_get_number(&s->options, "lock-after-time");
|
|
|
|
if (timeout > 0 && t > s->activity + timeout) {
|
|
|
|
server_lock_session(s);
|
|
|
|
recalculate_sizes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-10 20:35:40 +01:00
|
|
|
/* Update socket execute permissions based on whether sessions are attached. */
|
2008-12-13 19:06:08 +01:00
|
|
|
int
|
2009-05-19 10:48:49 +02:00
|
|
|
server_update_socket(void)
|
2008-06-08 21:49:04 +02:00
|
|
|
{
|
|
|
|
struct session *s;
|
|
|
|
u_int i;
|
|
|
|
static int last = -1;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
|
|
|
|
s = ARRAY_ITEM(&sessions, i);
|
2008-06-17 21:26:19 +02:00
|
|
|
if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
|
2008-06-08 21:49:04 +02:00
|
|
|
n++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n != last) {
|
|
|
|
last = n;
|
|
|
|
if (n != 0)
|
2009-03-27 16:57:10 +01:00
|
|
|
chmod(socket_path, S_IRWXU);
|
2008-06-08 21:49:04 +02:00
|
|
|
else
|
2009-03-27 16:57:10 +01:00
|
|
|
chmod(socket_path, S_IRUSR|S_IWUSR);
|
2008-06-08 21:49:04 +02:00
|
|
|
}
|
2008-12-13 19:06:08 +01:00
|
|
|
|
|
|
|
return (n);
|
2008-06-08 21:49:04 +02:00
|
|
|
}
|