2014-11-08 13:27:43 +01:00
|
|
|
/* $OpenBSD$ */
|
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>
|
2009-11-08 23:40:36 +01:00
|
|
|
#include <event.h>
|
2007-07-09 21:04:12 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct clients clients;
|
|
|
|
|
2009-11-08 23:40:36 +01:00
|
|
|
int server_fd;
|
|
|
|
int server_shutdown;
|
|
|
|
struct event server_ev_accept;
|
2009-10-12 01:25:44 +02:00
|
|
|
|
2015-06-04 13:43:51 +02:00
|
|
|
struct session *marked_session;
|
|
|
|
struct winlink *marked_winlink;
|
|
|
|
struct window *marked_window;
|
|
|
|
struct window_pane *marked_window_pane;
|
|
|
|
struct layout_cell *marked_layout_cell;
|
|
|
|
|
|
|
|
int server_create_socket(void);
|
|
|
|
void server_loop(void);
|
|
|
|
int server_should_shutdown(void);
|
|
|
|
void server_send_shutdown(void);
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* Set marked pane. */
|
|
|
|
void
|
|
|
|
server_set_marked(struct session *s, struct winlink *wl, struct window_pane *wp)
|
|
|
|
{
|
|
|
|
marked_session = s;
|
|
|
|
marked_winlink = wl;
|
|
|
|
marked_window = wl->window;
|
|
|
|
marked_window_pane = wp;
|
|
|
|
marked_layout_cell = wp->layout_cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear marked pane. */
|
|
|
|
void
|
|
|
|
server_clear_marked(void)
|
|
|
|
{
|
|
|
|
marked_session = NULL;
|
|
|
|
marked_winlink = NULL;
|
|
|
|
marked_window = NULL;
|
|
|
|
marked_window_pane = NULL;
|
|
|
|
marked_layout_cell = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
if (marked_session != s || marked_winlink != wl)
|
|
|
|
return (0);
|
|
|
|
if (marked_window_pane != wp)
|
|
|
|
return (0);
|
|
|
|
return (server_check_marked());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the marked pane is still valid. */
|
|
|
|
int
|
|
|
|
server_check_marked(void)
|
|
|
|
{
|
|
|
|
struct winlink *wl;
|
|
|
|
|
|
|
|
if (marked_window_pane == NULL)
|
|
|
|
return (0);
|
|
|
|
if (marked_layout_cell != marked_window_pane->layout_cell)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (!session_alive(marked_session))
|
|
|
|
return (0);
|
|
|
|
RB_FOREACH(wl, winlinks, &marked_session->windows) {
|
|
|
|
if (wl->window == marked_window && wl == marked_winlink)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (wl == NULL)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (!window_has_pane(marked_window, marked_window_pane))
|
|
|
|
return (0);
|
|
|
|
return (window_pane_visible(marked_window_pane));
|
|
|
|
}
|
2009-10-12 01:25:44 +02:00
|
|
|
|
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;
|
2011-01-22 00:44:13 +01:00
|
|
|
int fd;
|
2009-10-23 19:49:47 +02: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;
|
2015-04-22 00:32:40 +02:00
|
|
|
return (-1);
|
2009-10-23 19:49:47 +02:00
|
|
|
}
|
|
|
|
unlink(sa.sun_path);
|
|
|
|
|
|
|
|
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
|
2015-04-22 00:32:40 +02:00
|
|
|
return (-1);
|
2009-10-23 19:49:47 +02:00
|
|
|
|
2010-06-23 01:21:39 +02:00
|
|
|
mask = umask(S_IXUSR|S_IXGRP|S_IRWXO);
|
2009-10-23 19:49:47 +02:00
|
|
|
if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1)
|
2015-04-22 00:32:40 +02:00
|
|
|
return (-1);
|
2009-10-23 19:49:47 +02:00
|
|
|
umask(mask);
|
|
|
|
|
|
|
|
if (listen(fd, 16) == -1)
|
2015-04-22 00:32:40 +02:00
|
|
|
return (-1);
|
2011-01-22 00:44:13 +01:00
|
|
|
setblocking(fd, 0);
|
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
|
|
|
|
2007-09-26 20:09:23 +02:00
|
|
|
/* Fork new server. */
|
2008-06-07 09:27:28 +02:00
|
|
|
int
|
2015-08-31 00:19:07 +02:00
|
|
|
server_start(struct event_base *base, int lockfd, char *lockfile)
|
2007-07-09 21:04:12 +02:00
|
|
|
{
|
2015-09-01 12:01:56 +02:00
|
|
|
int pair[2];
|
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");
|
2014-07-21 12:52:48 +02:00
|
|
|
log_debug("starting server");
|
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");
|
|
|
|
|
2010-05-14 16:30:01 +02:00
|
|
|
/* event_init() was called in our parent, need to reinit. */
|
2015-06-01 11:20:19 +02:00
|
|
|
clear_signals(0);
|
2015-08-31 00:19:07 +02:00
|
|
|
if (event_reinit(base) != 0)
|
2010-05-14 16:30:01 +02:00
|
|
|
fatal("event_reinit failed");
|
|
|
|
|
2009-08-09 19:19:18 +02:00
|
|
|
logfile("server");
|
|
|
|
log_debug("server started, pid %ld", (long) getpid());
|
|
|
|
|
2015-04-22 17:30:11 +02:00
|
|
|
RB_INIT(&windows);
|
2011-04-07 00:16:33 +02:00
|
|
|
RB_INIT(&all_window_panes);
|
2015-04-25 01:17:11 +02:00
|
|
|
TAILQ_INIT(&clients);
|
2010-12-22 16:36:44 +01:00
|
|
|
RB_INIT(&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);
|
2009-08-09 19:19:18 +02:00
|
|
|
log_debug("socket path %s", socket_path);
|
2010-10-24 21:54:41 +02:00
|
|
|
#ifdef HAVE_SETPROCTITLE
|
2010-10-24 03:31:08 +02:00
|
|
|
setproctitle("server (%s)", socket_path);
|
2010-10-24 21:54:41 +02:00
|
|
|
#endif
|
2009-05-04 19:58:27 +02:00
|
|
|
|
2009-11-08 23:40:36 +01:00
|
|
|
server_fd = server_create_socket();
|
2015-04-22 00:32:40 +02:00
|
|
|
if (server_fd == -1)
|
|
|
|
fatal("couldn't create socket");
|
|
|
|
server_update_socket();
|
2009-10-23 19:49:47 +02:00
|
|
|
server_client_create(pair[1]);
|
2009-05-16 12:02:51 +02:00
|
|
|
|
2012-03-18 02:28:10 +01:00
|
|
|
unlink(lockfile);
|
2012-07-11 21:34:16 +02:00
|
|
|
free(lockfile);
|
2012-03-18 02:28:10 +01:00
|
|
|
close(lockfd);
|
|
|
|
|
2015-09-01 12:01:56 +02:00
|
|
|
start_cfg();
|
|
|
|
|
2015-07-20 17:50:04 +02:00
|
|
|
status_prompt_load_history();
|
2009-08-09 19:19:18 +02:00
|
|
|
|
2012-04-12 14:43:40 +02:00
|
|
|
server_add_accept(0);
|
2009-11-08 23:40:36 +01:00
|
|
|
|
2010-05-14 16:30:01 +02:00
|
|
|
set_signals(server_signal_callback);
|
2009-11-08 23:40:36 +01:00
|
|
|
server_loop();
|
2015-07-20 17:50:04 +02:00
|
|
|
status_prompt_save_history();
|
2009-11-08 23:40:36 +01:00
|
|
|
exit(0);
|
2009-05-16 12:02:51 +02:00
|
|
|
}
|
|
|
|
|
2007-07-09 21:04:12 +02:00
|
|
|
/* Main server loop. */
|
2009-11-08 23:40:36 +01:00
|
|
|
void
|
|
|
|
server_loop(void)
|
2007-07-09 21:04:12 +02:00
|
|
|
{
|
2009-11-08 23:40:36 +01:00
|
|
|
while (!server_should_shutdown()) {
|
2015-08-29 10:54:41 +02:00
|
|
|
log_debug("event dispatch enter");
|
2009-11-08 23:40:36 +01:00
|
|
|
event_loop(EVLOOP_ONCE);
|
2015-08-29 10:54:41 +02:00
|
|
|
log_debug("event dispatch exit");
|
2009-01-10 20:35:40 +01:00
|
|
|
|
2009-10-23 19:49:47 +02:00
|
|
|
server_client_loop();
|
2009-12-04 23:14:47 +01:00
|
|
|
}
|
2009-11-08 23:40:36 +01:00
|
|
|
}
|
|
|
|
|
2014-04-17 16:45:49 +02:00
|
|
|
/* Check if the server should exit (no more clients or sessions). */
|
2009-11-08 23:40:36 +01:00
|
|
|
int
|
|
|
|
server_should_shutdown(void)
|
|
|
|
{
|
2014-09-01 23:58:41 +02:00
|
|
|
struct client *c;
|
2007-07-09 21:04:12 +02:00
|
|
|
|
2010-10-09 16:29:32 +02:00
|
|
|
if (!options_get_number(&global_options, "exit-unattached")) {
|
2010-12-22 16:36:44 +01:00
|
|
|
if (!RB_EMPTY(&sessions))
|
|
|
|
return (0);
|
2007-10-24 13:05:59 +02:00
|
|
|
}
|
2014-09-01 23:58:41 +02:00
|
|
|
|
2015-04-25 01:17:11 +02:00
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
|
|
|
if (c->session != NULL)
|
2014-09-01 23:58:41 +02: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-25 01:17:11 +02:00
|
|
|
if (!TAILQ_EMPTY(&clients))
|
|
|
|
return (0);
|
2014-09-01 23:58:41 +02:00
|
|
|
|
2009-11-08 23:40:36 +01:00
|
|
|
return (1);
|
2007-07-09 21:04:12 +02:00
|
|
|
}
|
|
|
|
|
2009-11-08 23:40:36 +01:00
|
|
|
/* Shutdown the server by killing all clients and windows. */
|
2009-01-21 23:47:31 +01:00
|
|
|
void
|
2009-11-08 23:40:36 +01:00
|
|
|
server_send_shutdown(void)
|
2009-01-21 23:47:31 +01:00
|
|
|
{
|
2015-04-25 01:17:11 +02:00
|
|
|
struct client *c, *c1;
|
|
|
|
struct session *s, *s1;
|
2009-01-21 23:47:31 +01:00
|
|
|
|
2014-09-01 23:58:41 +02:00
|
|
|
cmd_wait_for_flush();
|
|
|
|
|
2015-04-25 01:17:11 +02:00
|
|
|
TAILQ_FOREACH_SAFE(c, &clients, entry, c1) {
|
|
|
|
if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
|
|
|
|
server_client_lost(c);
|
|
|
|
else
|
|
|
|
server_write_client(c, MSG_SHUTDOWN, NULL, 0);
|
|
|
|
c->session = NULL;
|
2009-09-23 17:10:37 +02:00
|
|
|
}
|
|
|
|
|
2015-04-25 01:17:11 +02:00
|
|
|
RB_FOREACH_SAFE(s, sessions, &sessions, s1)
|
2010-12-22 16:36:44 +01:00
|
|
|
session_destroy(s);
|
2009-09-23 17:10:37 +02:00
|
|
|
}
|
|
|
|
|
2009-11-08 23:40:36 +01:00
|
|
|
/* Update socket execute permissions based on whether sessions are attached. */
|
2009-11-13 17:51:49 +01:00
|
|
|
void
|
2009-11-08 23:40:36 +01:00
|
|
|
server_update_socket(void)
|
2009-09-23 17:10:37 +02:00
|
|
|
{
|
2009-11-08 23:40:36 +01:00
|
|
|
struct session *s;
|
|
|
|
static int last = -1;
|
2010-10-09 16:26:29 +02:00
|
|
|
int n, mode;
|
|
|
|
struct stat sb;
|
2009-01-21 23:47:31 +01:00
|
|
|
|
2009-11-08 23:40:36 +01:00
|
|
|
n = 0;
|
2010-12-22 16:36:44 +01:00
|
|
|
RB_FOREACH(s, sessions, &sessions) {
|
|
|
|
if (!(s->flags & SESSION_UNATTACHED)) {
|
2009-11-08 23:40:36 +01:00
|
|
|
n++;
|
|
|
|
break;
|
|
|
|
}
|
2009-09-23 17:10:37 +02:00
|
|
|
}
|
2009-11-08 23:40:36 +01:00
|
|
|
|
|
|
|
if (n != last) {
|
|
|
|
last = n;
|
2010-10-09 16:26:29 +02: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-08 23:40:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for server socket. */
|
|
|
|
void
|
|
|
|
server_accept_callback(int fd, short events, unused void *data)
|
|
|
|
{
|
|
|
|
struct sockaddr_storage sa;
|
|
|
|
socklen_t slen = sizeof sa;
|
|
|
|
int newfd;
|
|
|
|
|
2012-04-12 14:43:40 +02:00
|
|
|
server_add_accept(0);
|
2009-11-08 23:40:36 +01: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-12 14:43:40 +02:00
|
|
|
if (errno == ENFILE || errno == EMFILE) {
|
|
|
|
/* Delete and don't try again for 1 second. */
|
|
|
|
server_add_accept(1);
|
|
|
|
return;
|
|
|
|
}
|
2009-11-08 23:40:36 +01:00
|
|
|
fatal("accept failed");
|
|
|
|
}
|
|
|
|
if (server_shutdown) {
|
|
|
|
close(newfd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
server_client_create(newfd);
|
|
|
|
}
|
|
|
|
|
2012-04-12 14:43:40 +02: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 };
|
|
|
|
|
|
|
|
if (event_initialized(&server_ev_accept))
|
|
|
|
event_del(&server_ev_accept);
|
|
|
|
|
|
|
|
if (timeout == 0) {
|
|
|
|
event_set(&server_ev_accept,
|
|
|
|
server_fd, EV_READ, server_accept_callback, NULL);
|
|
|
|
event_add(&server_ev_accept, NULL);
|
|
|
|
} else {
|
|
|
|
event_set(&server_ev_accept,
|
|
|
|
server_fd, EV_TIMEOUT, server_accept_callback, NULL);
|
|
|
|
event_add(&server_ev_accept, &tv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-08 23:40:36 +01:00
|
|
|
/* Signal handler. */
|
|
|
|
void
|
|
|
|
server_signal_callback(int sig, unused short events, unused void *data)
|
|
|
|
{
|
2015-04-22 00:32:40 +02:00
|
|
|
int fd;
|
2015-04-22 17:05:03 +02:00
|
|
|
|
2009-11-08 23:40:36 +01:00
|
|
|
switch (sig) {
|
|
|
|
case SIGTERM:
|
|
|
|
server_shutdown = 1;
|
|
|
|
server_send_shutdown();
|
|
|
|
break;
|
|
|
|
case SIGCHLD:
|
|
|
|
server_child_signal();
|
|
|
|
break;
|
|
|
|
case SIGUSR1:
|
|
|
|
event_del(&server_ev_accept);
|
2015-04-22 00:32:40 +02:00
|
|
|
fd = server_create_socket();
|
|
|
|
if (fd != -1) {
|
|
|
|
close(server_fd);
|
|
|
|
server_fd = fd;
|
|
|
|
server_update_socket();
|
|
|
|
}
|
2012-04-12 14:43:40 +02:00
|
|
|
server_add_accept(0);
|
2009-11-08 23:40:36 +01:00
|
|
|
break;
|
2009-01-21 23:47:31 +01:00
|
|
|
}
|
|
|
|
}
|
2009-02-08 17:11:26 +01:00
|
|
|
|
|
|
|
/* Handle SIGCHLD. */
|
|
|
|
void
|
|
|
|
server_child_signal(void)
|
|
|
|
{
|
2009-11-08 23:40:36 +01:00
|
|
|
int status;
|
|
|
|
pid_t pid;
|
2009-02-08 17:11:26 +01:00
|
|
|
|
|
|
|
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-11-08 23:40:36 +01:00
|
|
|
if (WIFSTOPPED(status))
|
|
|
|
server_child_stopped(pid, status);
|
2009-12-04 23:04:55 +01:00
|
|
|
else if (WIFEXITED(status) || WIFSIGNALED(status))
|
2009-11-08 23:40:36 +01:00
|
|
|
server_child_exited(pid, status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle exited children. */
|
|
|
|
void
|
|
|
|
server_child_exited(pid_t pid, int status)
|
|
|
|
{
|
2015-04-22 17:30:11 +02:00
|
|
|
struct window *w, *w1;
|
2009-11-08 23:40:36 +01:00
|
|
|
struct window_pane *wp;
|
|
|
|
struct job *job;
|
|
|
|
|
2015-04-22 17:30:11 +02:00
|
|
|
RB_FOREACH_SAFE(w, windows, &windows, w1) {
|
2009-11-08 23:40:36 +01:00
|
|
|
TAILQ_FOREACH(wp, &w->panes, entry) {
|
|
|
|
if (wp->pid == pid) {
|
2014-12-09 20:23:35 +01:00
|
|
|
wp->status = status;
|
2009-11-14 18:48:39 +01:00
|
|
|
server_destroy_pane(wp);
|
|
|
|
break;
|
2009-11-08 23:40:36 +01:00
|
|
|
}
|
2009-10-12 02:21:08 +02:00
|
|
|
}
|
2009-12-04 23:14:47 +01:00
|
|
|
}
|
2009-02-08 17:11:26 +01:00
|
|
|
|
2011-02-15 16:12:28 +01:00
|
|
|
LIST_FOREACH(job, &all_jobs, lentry) {
|
2009-11-08 23:40:36 +01:00
|
|
|
if (pid == job->pid) {
|
2009-11-08 23:56:04 +01:00
|
|
|
job_died(job, status); /* might free job */
|
|
|
|
break;
|
2009-02-08 17:11:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-04 19:58:27 +02:00
|
|
|
|
2009-11-08 23:40:36 +01:00
|
|
|
/* Handle stopped children. */
|
2009-09-08 01:59:19 +02:00
|
|
|
void
|
2009-11-08 23:40:36 +01:00
|
|
|
server_child_stopped(pid_t pid, int status)
|
2009-09-08 01:59:19 +02:00
|
|
|
{
|
2009-11-08 23:40:36 +01:00
|
|
|
struct window *w;
|
|
|
|
struct window_pane *wp;
|
2009-09-08 01:59:19 +02:00
|
|
|
|
2009-11-08 23:40:36 +01:00
|
|
|
if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
|
|
|
|
return;
|
2009-09-08 01:59:19 +02:00
|
|
|
|
2015-04-22 17:30:11 +02:00
|
|
|
RB_FOREACH(w, windows, &windows) {
|
2009-11-08 23:40:36 +01:00
|
|
|
TAILQ_FOREACH(wp, &w->panes, entry) {
|
|
|
|
if (wp->pid == pid) {
|
|
|
|
if (killpg(pid, SIGCONT) != 0)
|
|
|
|
kill(pid, SIGCONT);
|
|
|
|
}
|
|
|
|
}
|
2009-09-08 01:59:19 +02:00
|
|
|
}
|
|
|
|
}
|