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