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