Annotation of src/usr.bin/tmux/server.c, Revision 1.122
1.122 ! nicm 1: /* $OpenBSD: server.c,v 1.121 2015/04/22 15:30:11 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.121 nicm 140: RB_INIT(&windows);
1.102 nicm 141: RB_INIT(&all_window_panes);
1.122 ! nicm 142: TAILQ_INIT(&clients);
! 143: TAILQ_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.122 ! nicm 169: cfg_client = TAILQ_FIRST(&clients);
1.111 nicm 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;
1.1 nicm 215:
1.94 nicm 216: if (!options_get_number(&global_options, "exit-unattached")) {
1.97 nicm 217: if (!RB_EMPTY(&sessions))
218: return (0);
1.1 nicm 219: }
1.116 nicm 220:
1.122 ! nicm 221: TAILQ_FOREACH(c, &clients, entry) {
! 222: if (c->session != NULL)
1.116 nicm 223: return (0);
224: }
225:
226: /*
227: * No attached clients therefore want to exit - flush any waiting
228: * clients but don't actually exit until they've gone.
229: */
230: cmd_wait_for_flush();
1.122 ! nicm 231: if (!TAILQ_EMPTY(&clients))
! 232: return (0);
1.116 nicm 233:
1.66 nicm 234: return (1);
1.1 nicm 235: }
236:
1.66 nicm 237: /* Shutdown the server by killing all clients and windows. */
1.1 nicm 238: void
1.66 nicm 239: server_send_shutdown(void)
1.1 nicm 240: {
1.122 ! nicm 241: struct client *c, *c1;
! 242: struct session *s, *s1;
1.116 nicm 243:
244: cmd_wait_for_flush();
1.1 nicm 245:
1.122 ! nicm 246: TAILQ_FOREACH_SAFE(c, &clients, entry, c1) {
! 247: if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
! 248: server_client_lost(c);
! 249: else
! 250: server_write_client(c, MSG_SHUTDOWN, NULL, 0);
! 251: c->session = NULL;
1.42 nicm 252: }
253:
1.122 ! nicm 254: RB_FOREACH_SAFE(s, sessions, &sessions, s1)
1.97 nicm 255: session_destroy(s);
1.42 nicm 256: }
257:
1.66 nicm 258: /* Free dead, unreferenced clients and sessions. */
259: void
260: server_clean_dead(void)
261: {
1.122 ! nicm 262: struct session *s, *s1;
! 263: struct client *c, *c1;
1.66 nicm 264:
1.122 ! nicm 265: RB_FOREACH_SAFE(s, sessions, &dead_sessions, s1) {
! 266: if (s->references != 0)
! 267: continue;
! 268: RB_REMOVE(sessions, &dead_sessions, s);
! 269: free(s->name);
! 270: free(s);
1.66 nicm 271: }
272:
1.122 ! nicm 273: TAILQ_FOREACH_SAFE(c, &dead_clients, entry, c1) {
! 274: if (c->references != 0)
1.66 nicm 275: continue;
1.122 ! nicm 276: TAILQ_REMOVE(&dead_clients, c, entry);
1.105 nicm 277: free(c);
1.66 nicm 278: }
279: }
280:
281: /* Update socket execute permissions based on whether sessions are attached. */
1.75 nicm 282: void
1.66 nicm 283: server_update_socket(void)
1.42 nicm 284: {
1.66 nicm 285: struct session *s;
286: static int last = -1;
1.93 nicm 287: int n, mode;
288: struct stat sb;
1.1 nicm 289:
1.66 nicm 290: n = 0;
1.97 nicm 291: RB_FOREACH(s, sessions, &sessions) {
292: if (!(s->flags & SESSION_UNATTACHED)) {
1.66 nicm 293: n++;
294: break;
295: }
296: }
297:
298: if (n != last) {
299: last = n;
1.93 nicm 300:
301: if (stat(socket_path, &sb) != 0)
302: return;
303: mode = sb.st_mode;
304: if (n != 0) {
305: if (mode & S_IRUSR)
306: mode |= S_IXUSR;
307: if (mode & S_IRGRP)
308: mode |= S_IXGRP;
309: if (mode & S_IROTH)
310: mode |= S_IXOTH;
311: } else
312: mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
313: chmod(socket_path, mode);
1.66 nicm 314: }
315: }
316:
317: /* Callback for server socket. */
318: void
319: server_accept_callback(int fd, short events, unused void *data)
320: {
321: struct sockaddr_storage sa;
322: socklen_t slen = sizeof sa;
323: int newfd;
324:
1.104 nicm 325: server_add_accept(0);
1.66 nicm 326: if (!(events & EV_READ))
327: return;
328:
329: newfd = accept(fd, (struct sockaddr *) &sa, &slen);
330: if (newfd == -1) {
331: if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
332: return;
1.104 nicm 333: if (errno == ENFILE || errno == EMFILE) {
334: /* Delete and don't try again for 1 second. */
335: server_add_accept(1);
336: return;
337: }
1.66 nicm 338: fatal("accept failed");
339: }
340: if (server_shutdown) {
341: close(newfd);
342: return;
1.42 nicm 343: }
1.66 nicm 344: server_client_create(newfd);
345: }
346:
1.104 nicm 347: /*
348: * Add accept event. If timeout is nonzero, add as a timeout instead of a read
349: * event - used to backoff when running out of file descriptors.
350: */
351: void
352: server_add_accept(int timeout)
353: {
354: struct timeval tv = { timeout, 0 };
355:
356: if (event_initialized(&server_ev_accept))
357: event_del(&server_ev_accept);
358:
359: if (timeout == 0) {
360: event_set(&server_ev_accept,
361: server_fd, EV_READ, server_accept_callback, NULL);
362: event_add(&server_ev_accept, NULL);
363: } else {
364: event_set(&server_ev_accept,
365: server_fd, EV_TIMEOUT, server_accept_callback, NULL);
366: event_add(&server_ev_accept, &tv);
367: }
368: }
369:
1.66 nicm 370: /* Signal handler. */
371: void
372: server_signal_callback(int sig, unused short events, unused void *data)
373: {
1.119 nicm 374: int fd;
1.120 nicm 375:
1.66 nicm 376: switch (sig) {
377: case SIGTERM:
378: server_shutdown = 1;
379: server_send_shutdown();
380: break;
381: case SIGCHLD:
382: server_child_signal();
383: break;
384: case SIGUSR1:
385: event_del(&server_ev_accept);
1.119 nicm 386: fd = server_create_socket();
387: if (fd != -1) {
388: close(server_fd);
389: server_fd = fd;
390: server_update_socket();
391: }
1.104 nicm 392: server_add_accept(0);
1.66 nicm 393: break;
1.1 nicm 394: }
395: }
396:
397: /* Handle SIGCHLD. */
398: void
399: server_child_signal(void)
400: {
1.66 nicm 401: int status;
402: pid_t pid;
1.1 nicm 403:
404: for (;;) {
405: switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
406: case -1:
407: if (errno == ECHILD)
408: return;
1.39 nicm 409: fatal("waitpid failed");
1.1 nicm 410: case 0:
411: return;
412: }
1.66 nicm 413: if (WIFSTOPPED(status))
414: server_child_stopped(pid, status);
1.79 nicm 415: else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66 nicm 416: server_child_exited(pid, status);
417: }
418: }
419:
420: /* Handle exited children. */
421: void
422: server_child_exited(pid_t pid, int status)
423: {
1.121 nicm 424: struct window *w, *w1;
1.66 nicm 425: struct window_pane *wp;
426: struct job *job;
427:
1.121 nicm 428: RB_FOREACH_SAFE(w, windows, &windows, w1) {
1.66 nicm 429: TAILQ_FOREACH(wp, &w->panes, entry) {
430: if (wp->pid == pid) {
1.118 nicm 431: wp->status = status;
1.77 nicm 432: server_destroy_pane(wp);
433: break;
1.53 nicm 434: }
435: }
1.80 nicm 436: }
1.1 nicm 437:
1.101 nicm 438: LIST_FOREACH(job, &all_jobs, lentry) {
1.66 nicm 439: if (pid == job->pid) {
1.67 nicm 440: job_died(job, status); /* might free job */
441: break;
1.1 nicm 442: }
1.53 nicm 443: }
444: }
445:
1.66 nicm 446: /* Handle stopped children. */
1.31 nicm 447: void
1.66 nicm 448: server_child_stopped(pid_t pid, int status)
1.31 nicm 449: {
1.66 nicm 450: struct window *w;
451: struct window_pane *wp;
1.31 nicm 452:
1.66 nicm 453: if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
454: return;
1.31 nicm 455:
1.121 nicm 456: RB_FOREACH(w, windows, &windows) {
1.66 nicm 457: TAILQ_FOREACH(wp, &w->panes, entry) {
458: if (wp->pid == pid) {
459: if (killpg(pid, SIGCONT) != 0)
460: kill(pid, SIGCONT);
461: }
462: }
1.31 nicm 463: }
1.1 nicm 464: }
465:
1.66 nicm 466: /* Handle once-per-second timer events. */
1.1 nicm 467: void
1.66 nicm 468: server_second_callback(unused int fd, unused short events, unused void *arg)
1.1 nicm 469: {
1.60 nicm 470: struct window *w;
471: struct window_pane *wp;
1.66 nicm 472: struct timeval tv;
1.1 nicm 473:
1.60 nicm 474: if (options_get_number(&global_s_options, "lock-server"))
475: server_lock_server();
476: else
477: server_lock_sessions();
1.1 nicm 478:
1.121 nicm 479: RB_FOREACH(w, windows, &windows) {
1.60 nicm 480: TAILQ_FOREACH(wp, &w->panes, entry) {
481: if (wp->mode != NULL && wp->mode->timer != NULL)
482: wp->mode->timer(wp);
1.1 nicm 483: }
484: }
1.72 nicm 485:
486: server_client_status_timer();
1.66 nicm 487:
488: evtimer_del(&server_ev_second);
489: memset(&tv, 0, sizeof tv);
490: tv.tv_sec = 1;
491: evtimer_add(&server_ev_second, &tv);
1.1 nicm 492: }
493:
1.46 nicm 494: /* Lock the server if ALL sessions have hit the time limit. */
495: void
496: server_lock_server(void)
497: {
498: struct session *s;
499: int timeout;
500: time_t t;
501:
502: t = time(NULL);
1.97 nicm 503: RB_FOREACH(s, sessions, &sessions) {
1.99 nicm 504: if (s->flags & SESSION_UNATTACHED)
1.59 nicm 505: continue;
1.46 nicm 506: timeout = options_get_number(&s->options, "lock-after-time");
1.65 nicm 507: if (timeout <= 0 || t <= s->activity_time.tv_sec + timeout)
1.46 nicm 508: return; /* not timed out */
509: }
510:
511: server_lock();
512: recalculate_sizes();
513: }
514:
515: /* Lock any sessions which have timed out. */
516: void
517: server_lock_sessions(void)
518: {
1.80 nicm 519: struct session *s;
1.46 nicm 520: int timeout;
1.62 deraadt 521: time_t t;
1.46 nicm 522:
1.62 deraadt 523: t = time(NULL);
1.97 nicm 524: RB_FOREACH(s, sessions, &sessions) {
1.99 nicm 525: if (s->flags & SESSION_UNATTACHED)
1.59 nicm 526: continue;
1.46 nicm 527: timeout = options_get_number(&s->options, "lock-after-time");
1.65 nicm 528: if (timeout > 0 && t > s->activity_time.tv_sec + timeout) {
1.46 nicm 529: server_lock_session(s);
530: recalculate_sizes();
1.1 nicm 531: }
532: }
533: }