Annotation of src/usr.bin/tmux/server.c, Revision 1.98
1.98 ! nicm 1: /* $OpenBSD: server.c,v 1.97 2010/12/21 22:37:59 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
20: #include <sys/ioctl.h>
21: #include <sys/socket.h>
22: #include <sys/stat.h>
23: #include <sys/un.h>
24: #include <sys/wait.h>
25:
26: #include <errno.h>
1.66 nicm 27: #include <event.h>
1.1 nicm 28: #include <fcntl.h>
1.5 nicm 29: #include <paths.h>
1.1 nicm 30: #include <signal.h>
31: #include <stdio.h>
32: #include <stdlib.h>
33: #include <string.h>
34: #include <syslog.h>
35: #include <termios.h>
36: #include <time.h>
37: #include <unistd.h>
38:
39: #include "tmux.h"
40:
41: /*
42: * Main server functions.
43: */
44:
45: /* Client list. */
46: struct clients clients;
1.31 nicm 47: struct clients dead_clients;
1.1 nicm 48:
1.66 nicm 49: int server_fd;
50: int server_shutdown;
51: struct event server_ev_accept;
52: struct event server_ev_second;
1.45 nicm 53:
1.98 ! nicm 54: struct paste_stack global_buffers;
! 55:
1.1 nicm 56: int server_create_socket(void);
1.66 nicm 57: void server_loop(void);
1.42 nicm 58: int server_should_shutdown(void);
1.66 nicm 59: void server_send_shutdown(void);
60: void server_clean_dead(void);
61: void server_accept_callback(int, short, void *);
62: void server_signal_callback(int, short, void *);
1.1 nicm 63: void server_child_signal(void);
1.66 nicm 64: void server_child_exited(pid_t, int);
65: void server_child_stopped(pid_t, int);
66: void server_second_callback(int, short, void *);
1.46 nicm 67: void server_lock_server(void);
68: void server_lock_sessions(void);
1.45 nicm 69:
1.60 nicm 70: /* Create server socket. */
71: int
72: server_create_socket(void)
1.1 nicm 73: {
1.60 nicm 74: struct sockaddr_un sa;
75: size_t size;
76: mode_t mask;
77: int fd, mode;
78:
79: memset(&sa, 0, sizeof sa);
80: sa.sun_family = AF_UNIX;
81: size = strlcpy(sa.sun_path, socket_path, sizeof sa.sun_path);
82: if (size >= sizeof sa.sun_path) {
83: errno = ENAMETOOLONG;
84: fatal("socket failed");
85: }
86: unlink(sa.sun_path);
87:
88: if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
89: fatal("socket failed");
90:
1.89 nicm 91: mask = umask(S_IXUSR|S_IXGRP|S_IRWXO);
1.60 nicm 92: if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1)
93: fatal("bind failed");
94: umask(mask);
95:
96: if (listen(fd, 16) == -1)
97: fatal("listen failed");
1.1 nicm 98:
99: if ((mode = fcntl(fd, F_GETFL)) == -1)
100: fatal("fcntl failed");
101: if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
102: fatal("fcntl failed");
103:
1.75 nicm 104: server_update_socket();
105:
1.60 nicm 106: return (fd);
107: }
108:
1.1 nicm 109: /* Fork new server. */
110: int
1.96 nicm 111: server_start(void)
1.1 nicm 112: {
1.82 nicm 113: struct window_pane *wp;
1.84 nicm 114: int pair[2];
1.96 nicm 115: char *cause;
1.82 nicm 116: struct timeval tv;
117: u_int i;
1.1 nicm 118:
119: /* The first client is special and gets a socketpair; create it. */
120: if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
121: fatal("socketpair failed");
122:
123: switch (fork()) {
124: case -1:
125: fatal("fork failed");
126: case 0:
127: break;
128: default:
129: close(pair[1]);
130: return (pair[0]);
131: }
132: close(pair[0]);
133:
134: /*
135: * Must daemonise before loading configuration as the PID changes so
136: * $TMUX would be wrong for sessions created in the config file.
137: */
1.16 nicm 138: if (daemon(1, 0) != 0)
1.1 nicm 139: fatal("daemon failed");
140:
1.88 nicm 141: /* event_init() was called in our parent, need to reinit. */
142: if (event_reinit(ev_base) != 0)
143: fatal("event_reinit failed");
1.92 nicm 144: clear_signals(0);
1.88 nicm 145:
1.16 nicm 146: logfile("server");
147: log_debug("server started, pid %ld", (long) getpid());
148:
1.1 nicm 149: ARRAY_INIT(&windows);
150: ARRAY_INIT(&clients);
1.31 nicm 151: ARRAY_INIT(&dead_clients);
1.97 nicm 152: RB_INIT(&sessions);
153: RB_INIT(&dead_sessions);
1.47 nicm 154: TAILQ_INIT(&session_groups);
1.98 ! nicm 155: ARRAY_INIT(&global_buffers);
1.15 nicm 156: mode_key_init_trees();
1.1 nicm 157: key_bindings_init();
158: utf8_build();
159:
160: start_time = time(NULL);
1.16 nicm 161: log_debug("socket path %s", socket_path);
1.96 nicm 162: setproctitle("server (%s)", socket_path);
1.16 nicm 163:
1.66 nicm 164: server_fd = server_create_socket();
1.60 nicm 165: server_client_create(pair[1]);
1.16 nicm 166:
1.82 nicm 167: if (access(SYSTEM_CFG, R_OK) == 0)
1.83 nicm 168: load_cfg(SYSTEM_CFG, NULL, &cfg_causes);
1.82 nicm 169: else if (errno != ENOENT) {
1.83 nicm 170: cfg_add_cause(
171: &cfg_causes, "%s: %s", strerror(errno), SYSTEM_CFG);
1.64 nicm 172: }
1.82 nicm 173: if (cfg_file != NULL)
1.83 nicm 174: load_cfg(cfg_file, NULL, &cfg_causes);
1.82 nicm 175:
176: /*
177: * If there is a session already, put the current window and pane into
178: * more mode.
179: */
1.97 nicm 180: if (!RB_EMPTY(&sessions) && !ARRAY_EMPTY(&cfg_causes)) {
181: wp = RB_MIN(sessions, &sessions)->curw->window->active;
1.85 nicm 182: window_pane_set_mode(wp, &window_copy_mode);
183: window_copy_init_for_output(wp);
1.83 nicm 184: for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
185: cause = ARRAY_ITEM(&cfg_causes, i);
1.85 nicm 186: window_copy_add(wp, "%s", cause);
1.83 nicm 187: xfree(cause);
1.82 nicm 188: }
1.83 nicm 189: ARRAY_FREE(&cfg_causes);
1.82 nicm 190: }
191: cfg_finished = 1;
1.66 nicm 192:
193: event_set(&server_ev_accept,
194: server_fd, EV_READ|EV_PERSIST, server_accept_callback, NULL);
195: event_add(&server_ev_accept, NULL);
196:
197: memset(&tv, 0, sizeof tv);
198: tv.tv_sec = 1;
199: evtimer_set(&server_ev_second, server_second_callback, NULL);
200: evtimer_add(&server_ev_second, &tv);
201:
1.88 nicm 202: set_signals(server_signal_callback);
1.66 nicm 203: server_loop();
204: exit(0);
1.1 nicm 205: }
206:
207: /* Main server loop. */
1.66 nicm 208: void
209: server_loop(void)
1.1 nicm 210: {
1.66 nicm 211: while (!server_should_shutdown()) {
212: event_loop(EVLOOP_ONCE);
1.1 nicm 213:
1.60 nicm 214: server_window_loop();
215: server_client_loop();
1.1 nicm 216:
1.8 nicm 217: key_bindings_clean();
1.66 nicm 218: server_clean_dead();
1.80 nicm 219: }
1.66 nicm 220: }
1.31 nicm 221:
1.94 nicm 222: /* Check if the server should be shutting down (no more clients or sessions). */
1.66 nicm 223: int
224: server_should_shutdown(void)
225: {
226: u_int i;
1.1 nicm 227:
1.94 nicm 228: if (!options_get_number(&global_options, "exit-unattached")) {
1.97 nicm 229: if (!RB_EMPTY(&sessions))
230: return (0);
1.1 nicm 231: }
232: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
233: if (ARRAY_ITEM(&clients, i) != NULL)
1.66 nicm 234: return (0);
1.1 nicm 235: }
1.66 nicm 236: return (1);
1.1 nicm 237: }
238:
1.66 nicm 239: /* Shutdown the server by killing all clients and windows. */
1.1 nicm 240: void
1.66 nicm 241: server_send_shutdown(void)
1.1 nicm 242: {
1.66 nicm 243: struct client *c;
1.97 nicm 244: struct session *s, *next_s;
1.66 nicm 245: u_int i;
1.1 nicm 246:
1.42 nicm 247: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
248: c = ARRAY_ITEM(&clients, i);
249: if (c != NULL) {
250: if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
1.60 nicm 251: server_client_lost(c);
1.42 nicm 252: else
253: server_write_client(c, MSG_SHUTDOWN, NULL, 0);
1.66 nicm 254: c->session = NULL;
1.42 nicm 255: }
256: }
257:
1.97 nicm 258: s = RB_MIN(sessions, &sessions);
259: while (s != NULL) {
260: next_s = RB_NEXT(sessions, &sessions, s);
261: session_destroy(s);
262: s = next_s;
1.1 nicm 263: }
1.42 nicm 264: }
265:
1.66 nicm 266: /* Free dead, unreferenced clients and sessions. */
267: void
268: server_clean_dead(void)
269: {
1.97 nicm 270: struct session *s, *next_s;
1.66 nicm 271: struct client *c;
272: u_int i;
273:
1.97 nicm 274: s = RB_MIN(sessions, &dead_sessions);
275: while (s != NULL) {
276: next_s = RB_NEXT(sessions, &dead_sessions, s);
277: if (s->references == 0) {
278: RB_REMOVE(sessions, &dead_sessions, s);
279: xfree(s->name);
280: xfree(s);
281: }
282: s = next_s;
1.66 nicm 283: }
284:
285: for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
286: c = ARRAY_ITEM(&dead_clients, i);
287: if (c == NULL || c->references != 0)
288: continue;
289: ARRAY_SET(&dead_clients, i, NULL);
290: xfree(c);
291: }
292: }
293:
294: /* Update socket execute permissions based on whether sessions are attached. */
1.75 nicm 295: void
1.66 nicm 296: server_update_socket(void)
1.42 nicm 297: {
1.66 nicm 298: struct session *s;
299: static int last = -1;
1.93 nicm 300: int n, mode;
301: struct stat sb;
1.1 nicm 302:
1.66 nicm 303: n = 0;
1.97 nicm 304: RB_FOREACH(s, sessions, &sessions) {
305: if (!(s->flags & SESSION_UNATTACHED)) {
1.66 nicm 306: n++;
307: break;
308: }
309: }
310:
311: if (n != last) {
312: last = n;
1.93 nicm 313:
314: if (stat(socket_path, &sb) != 0)
315: return;
316: mode = sb.st_mode;
317: if (n != 0) {
318: if (mode & S_IRUSR)
319: mode |= S_IXUSR;
320: if (mode & S_IRGRP)
321: mode |= S_IXGRP;
322: if (mode & S_IROTH)
323: mode |= S_IXOTH;
324: } else
325: mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
326: chmod(socket_path, mode);
1.66 nicm 327: }
328: }
329:
330: /* Callback for server socket. */
1.78 nicm 331: /* ARGSUSED */
1.66 nicm 332: void
333: server_accept_callback(int fd, short events, unused void *data)
334: {
335: struct sockaddr_storage sa;
336: socklen_t slen = sizeof sa;
337: int newfd;
338:
339: if (!(events & EV_READ))
340: return;
341:
342: newfd = accept(fd, (struct sockaddr *) &sa, &slen);
343: if (newfd == -1) {
344: if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
345: return;
346: fatal("accept failed");
347: }
348: if (server_shutdown) {
349: close(newfd);
350: return;
1.42 nicm 351: }
1.66 nicm 352: server_client_create(newfd);
353: }
354:
355: /* Signal handler. */
1.78 nicm 356: /* ARGSUSED */
1.66 nicm 357: void
358: server_signal_callback(int sig, unused short events, unused void *data)
359: {
360: switch (sig) {
361: case SIGTERM:
362: server_shutdown = 1;
363: server_send_shutdown();
364: break;
365: case SIGCHLD:
366: server_child_signal();
367: break;
368: case SIGUSR1:
369: event_del(&server_ev_accept);
370: close(server_fd);
371: server_fd = server_create_socket();
372: event_set(&server_ev_accept, server_fd,
373: EV_READ|EV_PERSIST, server_accept_callback, NULL);
374: event_add(&server_ev_accept, NULL);
375: break;
1.1 nicm 376: }
377: }
378:
379: /* Handle SIGCHLD. */
380: void
381: server_child_signal(void)
382: {
1.66 nicm 383: int status;
384: pid_t pid;
1.1 nicm 385:
386: for (;;) {
387: switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
388: case -1:
389: if (errno == ECHILD)
390: return;
1.39 nicm 391: fatal("waitpid failed");
1.1 nicm 392: case 0:
393: return;
394: }
1.66 nicm 395: if (WIFSTOPPED(status))
396: server_child_stopped(pid, status);
1.79 nicm 397: else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66 nicm 398: server_child_exited(pid, status);
399: }
400: }
401:
402: /* Handle exited children. */
403: void
404: server_child_exited(pid_t pid, int status)
405: {
406: struct window *w;
407: struct window_pane *wp;
408: struct job *job;
409: u_int i;
410:
411: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
412: if ((w = ARRAY_ITEM(&windows, i)) == NULL)
413: continue;
414: TAILQ_FOREACH(wp, &w->panes, entry) {
415: if (wp->pid == pid) {
1.77 nicm 416: server_destroy_pane(wp);
417: break;
1.53 nicm 418: }
419: }
1.80 nicm 420: }
1.1 nicm 421:
1.66 nicm 422: SLIST_FOREACH(job, &all_jobs, lentry) {
423: if (pid == job->pid) {
1.67 nicm 424: job_died(job, status); /* might free job */
425: break;
1.1 nicm 426: }
1.53 nicm 427: }
428: }
429:
1.66 nicm 430: /* Handle stopped children. */
1.31 nicm 431: void
1.66 nicm 432: server_child_stopped(pid_t pid, int status)
1.31 nicm 433: {
1.66 nicm 434: struct window *w;
435: struct window_pane *wp;
436: u_int i;
1.31 nicm 437:
1.66 nicm 438: if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
439: return;
1.31 nicm 440:
1.66 nicm 441: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
442: if ((w = ARRAY_ITEM(&windows, i)) == NULL)
1.31 nicm 443: continue;
1.66 nicm 444: TAILQ_FOREACH(wp, &w->panes, entry) {
445: if (wp->pid == pid) {
446: if (killpg(pid, SIGCONT) != 0)
447: kill(pid, SIGCONT);
448: }
449: }
1.31 nicm 450: }
1.1 nicm 451: }
452:
1.66 nicm 453: /* Handle once-per-second timer events. */
1.78 nicm 454: /* ARGSUSED */
1.1 nicm 455: void
1.66 nicm 456: server_second_callback(unused int fd, unused short events, unused void *arg)
1.1 nicm 457: {
1.60 nicm 458: struct window *w;
459: struct window_pane *wp;
1.66 nicm 460: struct timeval tv;
1.35 nicm 461: u_int i;
1.1 nicm 462:
1.60 nicm 463: if (options_get_number(&global_s_options, "lock-server"))
464: server_lock_server();
465: else
466: server_lock_sessions();
1.1 nicm 467:
1.60 nicm 468: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
469: w = ARRAY_ITEM(&windows, i);
470: if (w == NULL)
1.1 nicm 471: continue;
472:
1.60 nicm 473: TAILQ_FOREACH(wp, &w->panes, entry) {
474: if (wp->mode != NULL && wp->mode->timer != NULL)
475: wp->mode->timer(wp);
1.1 nicm 476: }
477: }
1.72 nicm 478:
479: server_client_status_timer();
1.66 nicm 480:
481: evtimer_del(&server_ev_second);
482: memset(&tv, 0, sizeof tv);
483: tv.tv_sec = 1;
484: evtimer_add(&server_ev_second, &tv);
1.1 nicm 485: }
486:
1.46 nicm 487: /* Lock the server if ALL sessions have hit the time limit. */
488: void
489: server_lock_server(void)
490: {
491: struct session *s;
492: int timeout;
493: time_t t;
494:
495: t = time(NULL);
1.97 nicm 496: RB_FOREACH(s, sessions, &sessions) {
1.59 nicm 497: if (s->flags & SESSION_UNATTACHED) {
1.65 nicm 498: if (gettimeofday(&s->activity_time, NULL) != 0)
499: fatal("gettimeofday failed");
1.59 nicm 500: continue;
501: }
502:
1.46 nicm 503: timeout = options_get_number(&s->options, "lock-after-time");
1.65 nicm 504: if (timeout <= 0 || t <= s->activity_time.tv_sec + timeout)
1.46 nicm 505: return; /* not timed out */
506: }
507:
508: server_lock();
509: recalculate_sizes();
510: }
511:
512: /* Lock any sessions which have timed out. */
513: void
514: server_lock_sessions(void)
515: {
1.80 nicm 516: struct session *s;
1.46 nicm 517: int timeout;
1.62 deraadt 518: time_t t;
1.46 nicm 519:
1.62 deraadt 520: t = time(NULL);
1.97 nicm 521: RB_FOREACH(s, sessions, &sessions) {
1.59 nicm 522: if (s->flags & SESSION_UNATTACHED) {
1.65 nicm 523: if (gettimeofday(&s->activity_time, NULL) != 0)
524: fatal("gettimeofday failed");
1.59 nicm 525: continue;
526: }
1.46 nicm 527:
528: timeout = options_get_number(&s->options, "lock-after-time");
1.65 nicm 529: if (timeout > 0 && t > s->activity_time.tv_sec + timeout) {
1.46 nicm 530: server_lock_session(s);
531: recalculate_sizes();
1.1 nicm 532: }
533: }
534: }