Annotation of src/usr.bin/tmux/server.c, Revision 1.109
1.109 ! nicm 1: /* $OpenBSD: server.c,v 1.108 2013/03/22 10:31:22 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;
1.100 nicm 77: int fd;
1.60 nicm 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.100 nicm 98: setblocking(fd, 0);
1.1 nicm 99:
1.75 nicm 100: server_update_socket();
101:
1.60 nicm 102: return (fd);
103: }
104:
1.1 nicm 105: /* Fork new server. */
106: int
1.103 nicm 107: server_start(int lockfd, char *lockfile)
1.1 nicm 108: {
1.109 ! nicm 109: int pair[2];
! 110: struct timeval tv;
! 111: char *cause;
1.1 nicm 112:
113: /* The first client is special and gets a socketpair; create it. */
114: if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
115: fatal("socketpair failed");
116:
117: switch (fork()) {
118: case -1:
119: fatal("fork failed");
120: case 0:
121: break;
122: default:
123: close(pair[1]);
124: return (pair[0]);
125: }
126: close(pair[0]);
127:
128: /*
129: * Must daemonise before loading configuration as the PID changes so
130: * $TMUX would be wrong for sessions created in the config file.
131: */
1.16 nicm 132: if (daemon(1, 0) != 0)
1.1 nicm 133: fatal("daemon failed");
134:
1.88 nicm 135: /* event_init() was called in our parent, need to reinit. */
136: if (event_reinit(ev_base) != 0)
137: fatal("event_reinit failed");
1.92 nicm 138: clear_signals(0);
1.88 nicm 139:
1.16 nicm 140: logfile("server");
141: log_debug("server started, pid %ld", (long) getpid());
142:
1.1 nicm 143: ARRAY_INIT(&windows);
1.102 nicm 144: RB_INIT(&all_window_panes);
1.1 nicm 145: ARRAY_INIT(&clients);
1.31 nicm 146: ARRAY_INIT(&dead_clients);
1.97 nicm 147: RB_INIT(&sessions);
148: RB_INIT(&dead_sessions);
1.47 nicm 149: TAILQ_INIT(&session_groups);
1.98 nicm 150: ARRAY_INIT(&global_buffers);
1.15 nicm 151: mode_key_init_trees();
1.1 nicm 152: key_bindings_init();
153: utf8_build();
154:
155: start_time = time(NULL);
1.16 nicm 156: log_debug("socket path %s", socket_path);
1.96 nicm 157: setproctitle("server (%s)", socket_path);
1.16 nicm 158:
1.66 nicm 159: server_fd = server_create_socket();
1.60 nicm 160: server_client_create(pair[1]);
1.103 nicm 161:
162: unlink(lockfile);
1.105 nicm 163: free(lockfile);
1.103 nicm 164: close(lockfd);
1.16 nicm 165:
1.109 ! nicm 166: cfg_cmd_q = cmdq_new(NULL);
! 167: cfg_cmd_q->emptyfn = cfg_default_done;
! 168: cfg_finished = 0;
! 169: cfg_references = 1;
! 170: ARRAY_INIT(&cfg_causes);
! 171:
! 172: if (access(SYSTEM_CFG, R_OK) == 0) {
! 173: if (load_cfg(SYSTEM_CFG, cfg_cmd_q, &cause) == -1) {
! 174: xasprintf(&cause, "%s: %s", SYSTEM_CFG, cause);
! 175: ARRAY_ADD(&cfg_causes, cause);
! 176: }
! 177: } else if (errno != ENOENT) {
! 178: xasprintf(&cause, "%s: %s", SYSTEM_CFG, strerror(errno));
! 179: ARRAY_ADD(&cfg_causes, cause);
1.64 nicm 180: }
1.109 ! nicm 181: if (cfg_file != NULL) {
! 182: if (load_cfg(cfg_file, cfg_cmd_q, &cause) == -1) {
! 183: xasprintf(&cause, "%s: %s", cfg_file, cause);
! 184: ARRAY_ADD(&cfg_causes, cause);
! 185: }
! 186: }
! 187: cmdq_continue(cfg_cmd_q);
1.66 nicm 188:
1.104 nicm 189: server_add_accept(0);
1.66 nicm 190:
191: memset(&tv, 0, sizeof tv);
192: tv.tv_sec = 1;
193: evtimer_set(&server_ev_second, server_second_callback, NULL);
194: evtimer_add(&server_ev_second, &tv);
195:
1.88 nicm 196: set_signals(server_signal_callback);
1.66 nicm 197: server_loop();
198: exit(0);
1.1 nicm 199: }
200:
201: /* Main server loop. */
1.66 nicm 202: void
203: server_loop(void)
1.1 nicm 204: {
1.66 nicm 205: while (!server_should_shutdown()) {
206: event_loop(EVLOOP_ONCE);
1.1 nicm 207:
1.60 nicm 208: server_window_loop();
209: server_client_loop();
1.1 nicm 210:
1.8 nicm 211: key_bindings_clean();
1.66 nicm 212: server_clean_dead();
1.80 nicm 213: }
1.66 nicm 214: }
1.31 nicm 215:
1.94 nicm 216: /* Check if the server should be shutting down (no more clients or sessions). */
1.66 nicm 217: int
218: server_should_shutdown(void)
219: {
220: u_int i;
1.1 nicm 221:
1.94 nicm 222: if (!options_get_number(&global_options, "exit-unattached")) {
1.97 nicm 223: if (!RB_EMPTY(&sessions))
224: return (0);
1.1 nicm 225: }
226: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
227: if (ARRAY_ITEM(&clients, i) != NULL)
1.66 nicm 228: return (0);
1.1 nicm 229: }
1.66 nicm 230: return (1);
1.1 nicm 231: }
232:
1.66 nicm 233: /* Shutdown the server by killing all clients and windows. */
1.1 nicm 234: void
1.66 nicm 235: server_send_shutdown(void)
1.1 nicm 236: {
1.66 nicm 237: struct client *c;
1.97 nicm 238: struct session *s, *next_s;
1.66 nicm 239: u_int i;
1.1 nicm 240:
1.42 nicm 241: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
242: c = ARRAY_ITEM(&clients, i);
243: if (c != NULL) {
244: if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
1.60 nicm 245: server_client_lost(c);
1.42 nicm 246: else
247: server_write_client(c, MSG_SHUTDOWN, NULL, 0);
1.66 nicm 248: c->session = NULL;
1.42 nicm 249: }
250: }
251:
1.97 nicm 252: s = RB_MIN(sessions, &sessions);
253: while (s != NULL) {
254: next_s = RB_NEXT(sessions, &sessions, s);
255: session_destroy(s);
256: s = next_s;
1.1 nicm 257: }
1.42 nicm 258: }
259:
1.66 nicm 260: /* Free dead, unreferenced clients and sessions. */
261: void
262: server_clean_dead(void)
263: {
1.97 nicm 264: struct session *s, *next_s;
1.66 nicm 265: struct client *c;
266: u_int i;
267:
1.97 nicm 268: s = RB_MIN(sessions, &dead_sessions);
269: while (s != NULL) {
270: next_s = RB_NEXT(sessions, &dead_sessions, s);
271: if (s->references == 0) {
272: RB_REMOVE(sessions, &dead_sessions, s);
1.105 nicm 273: free(s->name);
274: free(s);
1.97 nicm 275: }
276: s = next_s;
1.66 nicm 277: }
278:
279: for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
280: c = ARRAY_ITEM(&dead_clients, i);
281: if (c == NULL || c->references != 0)
282: continue;
283: ARRAY_SET(&dead_clients, i, NULL);
1.105 nicm 284: free(c);
1.66 nicm 285: }
286: }
287:
288: /* Update socket execute permissions based on whether sessions are attached. */
1.75 nicm 289: void
1.66 nicm 290: server_update_socket(void)
1.42 nicm 291: {
1.66 nicm 292: struct session *s;
293: static int last = -1;
1.93 nicm 294: int n, mode;
295: struct stat sb;
1.1 nicm 296:
1.66 nicm 297: n = 0;
1.97 nicm 298: RB_FOREACH(s, sessions, &sessions) {
299: if (!(s->flags & SESSION_UNATTACHED)) {
1.66 nicm 300: n++;
301: break;
302: }
303: }
304:
305: if (n != last) {
306: last = n;
1.93 nicm 307:
308: if (stat(socket_path, &sb) != 0)
309: return;
310: mode = sb.st_mode;
311: if (n != 0) {
312: if (mode & S_IRUSR)
313: mode |= S_IXUSR;
314: if (mode & S_IRGRP)
315: mode |= S_IXGRP;
316: if (mode & S_IROTH)
317: mode |= S_IXOTH;
318: } else
319: mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
320: chmod(socket_path, mode);
1.66 nicm 321: }
322: }
323:
324: /* Callback for server socket. */
325: void
326: server_accept_callback(int fd, short events, unused void *data)
327: {
328: struct sockaddr_storage sa;
329: socklen_t slen = sizeof sa;
330: int newfd;
331:
1.104 nicm 332: server_add_accept(0);
1.66 nicm 333: if (!(events & EV_READ))
334: return;
335:
336: newfd = accept(fd, (struct sockaddr *) &sa, &slen);
337: if (newfd == -1) {
338: if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
339: return;
1.104 nicm 340: if (errno == ENFILE || errno == EMFILE) {
341: /* Delete and don't try again for 1 second. */
342: server_add_accept(1);
343: return;
344: }
1.66 nicm 345: fatal("accept failed");
346: }
347: if (server_shutdown) {
348: close(newfd);
349: return;
1.42 nicm 350: }
1.66 nicm 351: server_client_create(newfd);
352: }
353:
1.104 nicm 354: /*
355: * Add accept event. If timeout is nonzero, add as a timeout instead of a read
356: * event - used to backoff when running out of file descriptors.
357: */
358: void
359: server_add_accept(int timeout)
360: {
361: struct timeval tv = { timeout, 0 };
362:
363: if (event_initialized(&server_ev_accept))
364: event_del(&server_ev_accept);
365:
366: if (timeout == 0) {
367: event_set(&server_ev_accept,
368: server_fd, EV_READ, server_accept_callback, NULL);
369: event_add(&server_ev_accept, NULL);
370: } else {
371: event_set(&server_ev_accept,
372: server_fd, EV_TIMEOUT, server_accept_callback, NULL);
373: event_add(&server_ev_accept, &tv);
374: }
375: }
376:
1.66 nicm 377: /* Signal handler. */
378: void
379: server_signal_callback(int sig, unused short events, unused void *data)
380: {
381: switch (sig) {
382: case SIGTERM:
383: server_shutdown = 1;
384: server_send_shutdown();
385: break;
386: case SIGCHLD:
387: server_child_signal();
388: break;
389: case SIGUSR1:
390: event_del(&server_ev_accept);
391: close(server_fd);
392: server_fd = server_create_socket();
1.104 nicm 393: server_add_accept(0);
1.66 nicm 394: break;
1.1 nicm 395: }
396: }
397:
398: /* Handle SIGCHLD. */
399: void
400: server_child_signal(void)
401: {
1.66 nicm 402: int status;
403: pid_t pid;
1.1 nicm 404:
405: for (;;) {
406: switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
407: case -1:
408: if (errno == ECHILD)
409: return;
1.39 nicm 410: fatal("waitpid failed");
1.1 nicm 411: case 0:
412: return;
413: }
1.66 nicm 414: if (WIFSTOPPED(status))
415: server_child_stopped(pid, status);
1.79 nicm 416: else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66 nicm 417: server_child_exited(pid, status);
418: }
419: }
420:
421: /* Handle exited children. */
422: void
423: server_child_exited(pid_t pid, int status)
424: {
425: struct window *w;
426: struct window_pane *wp;
427: struct job *job;
428: u_int i;
429:
430: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
431: if ((w = ARRAY_ITEM(&windows, i)) == NULL)
432: continue;
433: TAILQ_FOREACH(wp, &w->panes, entry) {
434: if (wp->pid == pid) {
1.77 nicm 435: server_destroy_pane(wp);
436: break;
1.53 nicm 437: }
438: }
1.80 nicm 439: }
1.1 nicm 440:
1.101 nicm 441: LIST_FOREACH(job, &all_jobs, lentry) {
1.66 nicm 442: if (pid == job->pid) {
1.67 nicm 443: job_died(job, status); /* might free job */
444: break;
1.1 nicm 445: }
1.53 nicm 446: }
447: }
448:
1.66 nicm 449: /* Handle stopped children. */
1.31 nicm 450: void
1.66 nicm 451: server_child_stopped(pid_t pid, int status)
1.31 nicm 452: {
1.66 nicm 453: struct window *w;
454: struct window_pane *wp;
455: u_int i;
1.31 nicm 456:
1.66 nicm 457: if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
458: return;
1.31 nicm 459:
1.66 nicm 460: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
461: if ((w = ARRAY_ITEM(&windows, i)) == NULL)
1.31 nicm 462: continue;
1.66 nicm 463: TAILQ_FOREACH(wp, &w->panes, entry) {
464: if (wp->pid == pid) {
465: if (killpg(pid, SIGCONT) != 0)
466: kill(pid, SIGCONT);
467: }
468: }
1.31 nicm 469: }
1.1 nicm 470: }
471:
1.66 nicm 472: /* Handle once-per-second timer events. */
1.1 nicm 473: void
1.66 nicm 474: server_second_callback(unused int fd, unused short events, unused void *arg)
1.1 nicm 475: {
1.60 nicm 476: struct window *w;
477: struct window_pane *wp;
1.66 nicm 478: struct timeval tv;
1.35 nicm 479: u_int i;
1.1 nicm 480:
1.60 nicm 481: if (options_get_number(&global_s_options, "lock-server"))
482: server_lock_server();
483: else
484: server_lock_sessions();
1.1 nicm 485:
1.60 nicm 486: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
487: w = ARRAY_ITEM(&windows, i);
488: if (w == NULL)
1.1 nicm 489: continue;
490:
1.60 nicm 491: TAILQ_FOREACH(wp, &w->panes, entry) {
492: if (wp->mode != NULL && wp->mode->timer != NULL)
493: wp->mode->timer(wp);
1.1 nicm 494: }
495: }
1.72 nicm 496:
497: server_client_status_timer();
1.66 nicm 498:
499: evtimer_del(&server_ev_second);
500: memset(&tv, 0, sizeof tv);
501: tv.tv_sec = 1;
502: evtimer_add(&server_ev_second, &tv);
1.1 nicm 503: }
504:
1.46 nicm 505: /* Lock the server if ALL sessions have hit the time limit. */
506: void
507: server_lock_server(void)
508: {
509: struct session *s;
510: int timeout;
511: time_t t;
512:
513: t = time(NULL);
1.97 nicm 514: RB_FOREACH(s, sessions, &sessions) {
1.99 nicm 515: if (s->flags & SESSION_UNATTACHED)
1.59 nicm 516: continue;
1.46 nicm 517: timeout = options_get_number(&s->options, "lock-after-time");
1.65 nicm 518: if (timeout <= 0 || t <= s->activity_time.tv_sec + timeout)
1.46 nicm 519: return; /* not timed out */
520: }
521:
522: server_lock();
523: recalculate_sizes();
524: }
525:
526: /* Lock any sessions which have timed out. */
527: void
528: server_lock_sessions(void)
529: {
1.80 nicm 530: struct session *s;
1.46 nicm 531: int timeout;
1.62 deraadt 532: time_t t;
1.46 nicm 533:
1.62 deraadt 534: t = time(NULL);
1.97 nicm 535: RB_FOREACH(s, sessions, &sessions) {
1.99 nicm 536: if (s->flags & SESSION_UNATTACHED)
1.59 nicm 537: continue;
1.46 nicm 538: timeout = options_get_number(&s->options, "lock-after-time");
1.65 nicm 539: if (timeout > 0 && t > s->activity_time.tv_sec + timeout) {
1.46 nicm 540: server_lock_session(s);
541: recalculate_sizes();
1.1 nicm 542: }
543: }
544: }