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