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