2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
#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 <syslog.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Main server functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Client list. */
|
|
|
|
struct clients clients;
|
2009-09-07 21:01:50 +00:00
|
|
|
struct clients dead_clients;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
int server_fd;
|
|
|
|
int server_shutdown;
|
|
|
|
struct event server_ev_accept;
|
|
|
|
struct event server_ev_second;
|
2009-10-10 09:31:39 +00:00
|
|
|
|
2010-12-30 23:16:18 +00:00
|
|
|
struct paste_stack global_buffers;
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
int server_create_socket(void);
|
2009-11-04 20:50:11 +00:00
|
|
|
void server_loop(void);
|
2009-09-23 08:21:57 +00:00
|
|
|
int server_should_shutdown(void);
|
2009-11-04 20:50:11 +00:00
|
|
|
void server_send_shutdown(void);
|
2009-09-07 21:01:50 +00:00
|
|
|
void server_clean_dead(void);
|
2009-11-04 20:50:11 +00:00
|
|
|
void server_accept_callback(int, short, void *);
|
|
|
|
void server_signal_callback(int, short, void *);
|
|
|
|
void server_child_signal(void);
|
|
|
|
void server_child_exited(pid_t, int);
|
|
|
|
void server_child_stopped(pid_t, int);
|
|
|
|
void server_second_callback(int, short, void *);
|
2009-10-10 09:46:11 +00:00
|
|
|
void server_lock_server(void);
|
|
|
|
void server_lock_sessions(void);
|
2009-10-10 09:31:39 +00:00
|
|
|
|
2009-10-22 19:41:51 +00:00
|
|
|
/* Create server socket. */
|
|
|
|
int
|
|
|
|
server_create_socket(void)
|
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;
|
2011-01-08 01:52:36 +00:00
|
|
|
int fd;
|
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;
|
|
|
|
fatal("socket failed");
|
|
|
|
}
|
|
|
|
unlink(sa.sun_path);
|
|
|
|
|
|
|
|
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
|
|
|
|
fatal("socket failed");
|
|
|
|
|
2010-06-21 00:18:57 +00:00
|
|
|
mask = umask(S_IXUSR|S_IXGRP|S_IRWXO);
|
2009-10-22 19:41:51 +00:00
|
|
|
if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1)
|
|
|
|
fatal("bind failed");
|
|
|
|
umask(mask);
|
|
|
|
|
|
|
|
if (listen(fd, 16) == -1)
|
|
|
|
fatal("listen failed");
|
2011-01-08 01:52:36 +00:00
|
|
|
setblocking(fd, 0);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-11-11 08:00:42 +00:00
|
|
|
server_update_socket();
|
|
|
|
|
2009-10-22 19:41:51 +00:00
|
|
|
return (fd);
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
/* Fork new server. */
|
|
|
|
int
|
2010-10-18 20:00:02 +00:00
|
|
|
server_start(void)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2010-02-06 17:15:33 +00:00
|
|
|
struct window_pane *wp;
|
2010-03-22 19:11:54 +00:00
|
|
|
int pair[2];
|
2010-10-18 20:00:02 +00:00
|
|
|
char *cause;
|
2010-02-06 17:15:33 +00:00
|
|
|
struct timeval tv;
|
|
|
|
u_int i;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
/* The first client is special and gets a socketpair; create it. */
|
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
|
|
|
|
fatal("socketpair failed");
|
|
|
|
|
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
|
|
|
fatal("fork failed");
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
close(pair[1]);
|
|
|
|
return (pair[0]);
|
|
|
|
}
|
|
|
|
close(pair[0]);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Must daemonise before loading configuration as the PID changes so
|
|
|
|
* $TMUX would be wrong for sessions created in the config file.
|
|
|
|
*/
|
2009-08-07 15:39:10 +00:00
|
|
|
if (daemon(1, 0) != 0)
|
2009-06-01 22:58:49 +00:00
|
|
|
fatal("daemon failed");
|
|
|
|
|
2010-05-04 17:28:16 +00:00
|
|
|
/* event_init() was called in our parent, need to reinit. */
|
|
|
|
if (event_reinit(ev_base) != 0)
|
|
|
|
fatal("event_reinit failed");
|
2010-08-19 18:29:01 +00:00
|
|
|
clear_signals(0);
|
2010-05-04 17:28:16 +00:00
|
|
|
|
2009-08-07 15:39:10 +00:00
|
|
|
logfile("server");
|
|
|
|
log_debug("server started, pid %ld", (long) getpid());
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
ARRAY_INIT(&windows);
|
|
|
|
ARRAY_INIT(&clients);
|
2009-09-07 21:01:50 +00:00
|
|
|
ARRAY_INIT(&dead_clients);
|
2010-12-21 22:37:59 +00:00
|
|
|
RB_INIT(&sessions);
|
|
|
|
RB_INIT(&dead_sessions);
|
2009-10-10 10:02:48 +00:00
|
|
|
TAILQ_INIT(&session_groups);
|
2010-12-30 23:16:18 +00:00
|
|
|
ARRAY_INIT(&global_buffers);
|
2009-07-28 07:03:32 +00:00
|
|
|
mode_key_init_trees();
|
2009-06-01 22:58:49 +00:00
|
|
|
key_bindings_init();
|
|
|
|
utf8_build();
|
|
|
|
|
|
|
|
start_time = time(NULL);
|
2009-08-07 15:39:10 +00:00
|
|
|
log_debug("socket path %s", socket_path);
|
2010-10-18 20:00:02 +00:00
|
|
|
setproctitle("server (%s)", socket_path);
|
2009-08-07 15:39:10 +00:00
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
server_fd = server_create_socket();
|
2009-10-22 19:41:51 +00:00
|
|
|
server_client_create(pair[1]);
|
2009-08-07 15:39:10 +00:00
|
|
|
|
2010-02-06 17:15:33 +00:00
|
|
|
if (access(SYSTEM_CFG, R_OK) == 0)
|
2010-02-06 23:22:27 +00:00
|
|
|
load_cfg(SYSTEM_CFG, NULL, &cfg_causes);
|
2010-02-06 17:15:33 +00:00
|
|
|
else if (errno != ENOENT) {
|
2010-02-06 23:22:27 +00:00
|
|
|
cfg_add_cause(
|
|
|
|
&cfg_causes, "%s: %s", strerror(errno), SYSTEM_CFG);
|
2010-02-06 17:15:33 +00:00
|
|
|
}
|
|
|
|
if (cfg_file != NULL)
|
2010-02-06 23:22:27 +00:00
|
|
|
load_cfg(cfg_file, NULL, &cfg_causes);
|
2010-02-06 17:15:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is a session already, put the current window and pane into
|
|
|
|
* more mode.
|
|
|
|
*/
|
2010-12-21 22:37:59 +00:00
|
|
|
if (!RB_EMPTY(&sessions) && !ARRAY_EMPTY(&cfg_causes)) {
|
|
|
|
wp = RB_MIN(sessions, &sessions)->curw->window->active;
|
2010-04-06 21:35:44 +00:00
|
|
|
window_pane_set_mode(wp, &window_copy_mode);
|
|
|
|
window_copy_init_for_output(wp);
|
2010-02-06 23:22:27 +00:00
|
|
|
for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
|
|
|
|
cause = ARRAY_ITEM(&cfg_causes, i);
|
2010-04-06 21:35:44 +00:00
|
|
|
window_copy_add(wp, "%s", cause);
|
2010-02-06 23:22:27 +00:00
|
|
|
xfree(cause);
|
2010-02-06 17:15:33 +00:00
|
|
|
}
|
2010-02-06 23:22:27 +00:00
|
|
|
ARRAY_FREE(&cfg_causes);
|
2009-11-02 12:48:44 +00:00
|
|
|
}
|
2010-02-06 17:15:33 +00:00
|
|
|
cfg_finished = 1;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
event_set(&server_ev_accept,
|
|
|
|
server_fd, EV_READ|EV_PERSIST, server_accept_callback, NULL);
|
|
|
|
event_add(&server_ev_accept, NULL);
|
|
|
|
|
|
|
|
memset(&tv, 0, sizeof tv);
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
evtimer_set(&server_ev_second, server_second_callback, NULL);
|
|
|
|
evtimer_add(&server_ev_second, &tv);
|
|
|
|
|
2010-05-04 17:28:16 +00:00
|
|
|
set_signals(server_signal_callback);
|
2009-11-04 20:50:11 +00:00
|
|
|
server_loop();
|
|
|
|
exit(0);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Main server loop. */
|
2009-11-04 20:50:11 +00:00
|
|
|
void
|
|
|
|
server_loop(void)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-11-04 20:50:11 +00:00
|
|
|
while (!server_should_shutdown()) {
|
|
|
|
event_loop(EVLOOP_ONCE);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-10-22 19:41:51 +00:00
|
|
|
server_window_loop();
|
|
|
|
server_client_loop();
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-07-12 17:33:18 +00:00
|
|
|
key_bindings_clean();
|
2009-09-07 21:01:50 +00:00
|
|
|
server_clean_dead();
|
2009-12-03 22:50:09 +00:00
|
|
|
}
|
2009-11-04 20:50:11 +00:00
|
|
|
}
|
|
|
|
|
2010-09-26 20:43:30 +00:00
|
|
|
/* Check if the server should be shutting down (no more clients or sessions). */
|
2009-11-04 20:50:11 +00:00
|
|
|
int
|
|
|
|
server_should_shutdown(void)
|
|
|
|
{
|
|
|
|
u_int i;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2010-09-26 20:43:30 +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
|
|
|
}
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
|
|
|
|
if (ARRAY_ITEM(&clients, i) != NULL)
|
2009-11-04 20:50:11 +00:00
|
|
|
return (0);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2009-11-04 20:50:11 +00:00
|
|
|
return (1);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
/* Shutdown the server by killing all clients and windows. */
|
2009-06-01 22:58:49 +00:00
|
|
|
void
|
2009-11-04 20:50:11 +00:00
|
|
|
server_send_shutdown(void)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
|
|
|
struct client *c;
|
2010-12-21 22:37:59 +00:00
|
|
|
struct session *s, *next_s;
|
2009-11-04 20:50:11 +00:00
|
|
|
u_int i;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-09-23 08:21:57 +00: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-22 19:41:51 +00:00
|
|
|
server_client_lost(c);
|
2009-09-23 08:21:57 +00:00
|
|
|
else
|
|
|
|
server_write_client(c, MSG_SHUTDOWN, NULL, 0);
|
2009-11-04 20:50:11 +00:00
|
|
|
c->session = NULL;
|
2009-09-23 08:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-21 22:37:59 +00:00
|
|
|
s = RB_MIN(sessions, &sessions);
|
|
|
|
while (s != NULL) {
|
|
|
|
next_s = RB_NEXT(sessions, &sessions, s);
|
|
|
|
session_destroy(s);
|
|
|
|
s = next_s;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2009-09-23 08:21:57 +00:00
|
|
|
}
|
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
/* Free dead, unreferenced clients and sessions. */
|
|
|
|
void
|
|
|
|
server_clean_dead(void)
|
|
|
|
{
|
2010-12-21 22:37:59 +00:00
|
|
|
struct session *s, *next_s;
|
2009-11-04 20:50:11 +00:00
|
|
|
struct client *c;
|
|
|
|
u_int i;
|
|
|
|
|
2010-12-21 22:37:59 +00:00
|
|
|
s = RB_MIN(sessions, &dead_sessions);
|
|
|
|
while (s != NULL) {
|
|
|
|
next_s = RB_NEXT(sessions, &dead_sessions, s);
|
|
|
|
if (s->references == 0) {
|
|
|
|
RB_REMOVE(sessions, &dead_sessions, s);
|
|
|
|
xfree(s->name);
|
|
|
|
xfree(s);
|
|
|
|
}
|
|
|
|
s = next_s;
|
2009-11-04 20:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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) {
|
|
|
|
if (!(s->flags & SESSION_UNATTACHED)) {
|
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;
|
|
|
|
mode = sb.st_mode;
|
|
|
|
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. */
|
2009-11-26 21:37:13 +00:00
|
|
|
/* ARGSUSED */
|
2009-11-04 20:50:11 +00:00
|
|
|
void
|
|
|
|
server_accept_callback(int fd, short events, unused void *data)
|
|
|
|
{
|
|
|
|
struct sockaddr_storage sa;
|
|
|
|
socklen_t slen = sizeof sa;
|
|
|
|
int newfd;
|
|
|
|
|
|
|
|
if (!(events & EV_READ))
|
|
|
|
return;
|
|
|
|
|
|
|
|
newfd = accept(fd, (struct sockaddr *) &sa, &slen);
|
|
|
|
if (newfd == -1) {
|
|
|
|
if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
|
|
|
|
return;
|
|
|
|
fatal("accept failed");
|
|
|
|
}
|
|
|
|
if (server_shutdown) {
|
|
|
|
close(newfd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
server_client_create(newfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Signal handler. */
|
2009-11-26 21:37:13 +00:00
|
|
|
/* ARGSUSED */
|
2009-11-04 20:50:11 +00:00
|
|
|
void
|
|
|
|
server_signal_callback(int sig, unused short events, unused void *data)
|
|
|
|
{
|
|
|
|
switch (sig) {
|
|
|
|
case SIGTERM:
|
|
|
|
server_shutdown = 1;
|
|
|
|
server_send_shutdown();
|
|
|
|
break;
|
|
|
|
case SIGCHLD:
|
|
|
|
server_child_signal();
|
|
|
|
break;
|
|
|
|
case SIGUSR1:
|
|
|
|
event_del(&server_ev_accept);
|
|
|
|
close(server_fd);
|
|
|
|
server_fd = server_create_socket();
|
|
|
|
event_set(&server_ev_accept, server_fd,
|
|
|
|
EV_READ|EV_PERSIST, server_accept_callback, NULL);
|
|
|
|
event_add(&server_ev_accept, NULL);
|
|
|
|
break;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle SIGCHLD. */
|
|
|
|
void
|
|
|
|
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. */
|
|
|
|
void
|
|
|
|
server_child_exited(pid_t pid, int status)
|
|
|
|
{
|
|
|
|
struct window *w;
|
|
|
|
struct window_pane *wp;
|
|
|
|
struct job *job;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
|
|
|
|
if ((w = ARRAY_ITEM(&windows, i)) == NULL)
|
2009-06-01 22:58:49 +00:00
|
|
|
continue;
|
2009-11-04 20:50:11 +00:00
|
|
|
TAILQ_FOREACH(wp, &w->panes, entry) {
|
|
|
|
if (wp->pid == pid) {
|
2009-11-13 17:33:07 +00:00
|
|
|
server_destroy_pane(wp);
|
|
|
|
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
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
SLIST_FOREACH(job, &all_jobs, lentry) {
|
|
|
|
if (pid == job->pid) {
|
2009-11-04 21:04:43 +00:00
|
|
|
job_died(job, status); /* might free job */
|
|
|
|
break;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
/* Handle stopped children. */
|
2009-09-07 21:01:50 +00:00
|
|
|
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;
|
|
|
|
u_int i;
|
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
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
|
|
|
|
if ((w = ARRAY_ITEM(&windows, i)) == NULL)
|
2009-09-07 21:01:50 +00:00
|
|
|
continue;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
/* Handle once-per-second timer events. */
|
2009-11-26 21:37:13 +00:00
|
|
|
/* ARGSUSED */
|
2009-06-01 22:58:49 +00:00
|
|
|
void
|
2009-11-04 20:50:11 +00:00
|
|
|
server_second_callback(unused int fd, unused short events, unused void *arg)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-10-22 19:41:51 +00:00
|
|
|
struct window *w;
|
|
|
|
struct window_pane *wp;
|
2009-11-04 20:50:11 +00:00
|
|
|
struct timeval tv;
|
2009-09-12 13:09:43 +00:00
|
|
|
u_int i;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-10-22 19:41:51 +00:00
|
|
|
if (options_get_number(&global_s_options, "lock-server"))
|
|
|
|
server_lock_server();
|
|
|
|
else
|
|
|
|
server_lock_sessions();
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-10-22 19:41:51 +00:00
|
|
|
for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
|
|
|
|
w = ARRAY_ITEM(&windows, i);
|
|
|
|
if (w == NULL)
|
2009-06-01 22:58:49 +00:00
|
|
|
continue;
|
|
|
|
|
2009-10-22 19:41:51 +00:00
|
|
|
TAILQ_FOREACH(wp, &w->panes, entry) {
|
|
|
|
if (wp->mode != NULL && wp->mode->timer != NULL)
|
|
|
|
wp->mode->timer(wp);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-04 20:50:11 +00:00
|
|
|
|
2009-11-04 23:42:51 +00:00
|
|
|
server_client_status_timer();
|
|
|
|
|
2009-11-04 20:50:11 +00:00
|
|
|
evtimer_del(&server_ev_second);
|
|
|
|
memset(&tv, 0, sizeof tv);
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
evtimer_add(&server_ev_second, &tv);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-10-10 09:46:11 +00:00
|
|
|
/* Lock the server if ALL sessions have hit the time limit. */
|
|
|
|
void
|
|
|
|
server_lock_server(void)
|
|
|
|
{
|
|
|
|
struct session *s;
|
|
|
|
int timeout;
|
|
|
|
time_t t;
|
|
|
|
|
|
|
|
t = time(NULL);
|
2010-12-21 22:37:59 +00:00
|
|
|
RB_FOREACH(s, sessions, &sessions) {
|
2011-01-01 01:12:09 +00:00
|
|
|
if (s->flags & SESSION_UNATTACHED)
|
2009-10-13 06:14:08 +00:00
|
|
|
continue;
|
2009-10-10 09:46:11 +00:00
|
|
|
timeout = options_get_number(&s->options, "lock-after-time");
|
2009-11-03 20:29:47 +00:00
|
|
|
if (timeout <= 0 || t <= s->activity_time.tv_sec + timeout)
|
2009-10-10 09:46:11 +00:00
|
|
|
return; /* not timed out */
|
|
|
|
}
|
|
|
|
|
|
|
|
server_lock();
|
|
|
|
recalculate_sizes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lock any sessions which have timed out. */
|
|
|
|
void
|
|
|
|
server_lock_sessions(void)
|
|
|
|
{
|
2009-12-03 22:50:09 +00:00
|
|
|
struct session *s;
|
2009-10-10 09:46:11 +00:00
|
|
|
int timeout;
|
2009-10-26 21:42:04 +00:00
|
|
|
time_t t;
|
2009-10-10 09:46:11 +00:00
|
|
|
|
2009-10-26 21:42:04 +00:00
|
|
|
t = time(NULL);
|
2010-12-21 22:37:59 +00:00
|
|
|
RB_FOREACH(s, sessions, &sessions) {
|
2011-01-01 01:12:09 +00:00
|
|
|
if (s->flags & SESSION_UNATTACHED)
|
2009-10-13 06:14:08 +00:00
|
|
|
continue;
|
2009-10-10 09:46:11 +00:00
|
|
|
timeout = options_get_number(&s->options, "lock-after-time");
|
2009-11-03 20:29:47 +00:00
|
|
|
if (timeout > 0 && t > s->activity_time.tv_sec + timeout) {
|
2009-10-10 09:46:11 +00:00
|
|
|
server_lock_session(s);
|
|
|
|
recalculate_sizes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|