Annotation of src/usr.bin/tmux/server.c, Revision 1.85
1.85 ! nicm 1: /* $OpenBSD: server.c,v 1.84 2010/03/22 19:11:54 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_sigterm;
53: struct event server_ev_sigusr1;
54: struct event server_ev_sigchld;
55: struct event server_ev_second;
1.45 nicm 56:
1.1 nicm 57: int server_create_socket(void);
1.66 nicm 58: void server_loop(void);
1.42 nicm 59: int server_should_shutdown(void);
1.66 nicm 60: void server_send_shutdown(void);
61: void server_clean_dead(void);
62: void server_accept_callback(int, short, void *);
63: void server_signal_callback(int, short, void *);
1.1 nicm 64: void server_child_signal(void);
1.66 nicm 65: void server_child_exited(pid_t, int);
66: void server_child_stopped(pid_t, int);
67: void server_second_callback(int, short, void *);
1.46 nicm 68: void server_lock_server(void);
69: void server_lock_sessions(void);
1.45 nicm 70:
1.60 nicm 71: /* Create server socket. */
72: int
73: server_create_socket(void)
1.1 nicm 74: {
1.60 nicm 75: struct sockaddr_un sa;
76: size_t size;
77: mode_t mask;
78: int fd, mode;
79:
80: memset(&sa, 0, sizeof sa);
81: sa.sun_family = AF_UNIX;
82: size = strlcpy(sa.sun_path, socket_path, sizeof sa.sun_path);
83: if (size >= sizeof sa.sun_path) {
84: errno = ENAMETOOLONG;
85: fatal("socket failed");
86: }
87: unlink(sa.sun_path);
88:
89: if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
90: fatal("socket failed");
91:
92: mask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
93: if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1)
94: fatal("bind failed");
95: umask(mask);
96:
97: if (listen(fd, 16) == -1)
98: fatal("listen failed");
1.1 nicm 99:
100: if ((mode = fcntl(fd, F_GETFL)) == -1)
101: fatal("fcntl failed");
102: if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
103: fatal("fcntl failed");
104: if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
105: fatal("fcntl failed");
106:
1.75 nicm 107: server_update_socket();
108:
1.60 nicm 109: return (fd);
110: }
111:
1.1 nicm 112: /* Fork new server. */
113: int
114: server_start(char *path)
115: {
1.82 nicm 116: struct window_pane *wp;
1.84 nicm 117: int pair[2];
1.83 nicm 118: char rpathbuf[MAXPATHLEN], *cause;
1.82 nicm 119: struct timeval tv;
120: u_int i;
1.1 nicm 121:
122: /* The first client is special and gets a socketpair; create it. */
123: if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
124: fatal("socketpair failed");
125:
126: switch (fork()) {
127: case -1:
128: fatal("fork failed");
129: case 0:
130: break;
131: default:
132: close(pair[1]);
133: return (pair[0]);
134: }
135: close(pair[0]);
136:
137: /*
138: * Must daemonise before loading configuration as the PID changes so
139: * $TMUX would be wrong for sessions created in the config file.
140: */
1.16 nicm 141: if (daemon(1, 0) != 0)
1.1 nicm 142: fatal("daemon failed");
143:
1.16 nicm 144: logfile("server");
145: log_debug("server started, pid %ld", (long) getpid());
146:
1.1 nicm 147: ARRAY_INIT(&windows);
148: ARRAY_INIT(&clients);
1.31 nicm 149: ARRAY_INIT(&dead_clients);
1.1 nicm 150: ARRAY_INIT(&sessions);
1.31 nicm 151: ARRAY_INIT(&dead_sessions);
1.47 nicm 152: TAILQ_INIT(&session_groups);
1.15 nicm 153: mode_key_init_trees();
1.1 nicm 154: key_bindings_init();
155: utf8_build();
156:
157: start_time = time(NULL);
158: socket_path = path;
159:
1.16 nicm 160: if (realpath(socket_path, rpathbuf) == NULL)
161: strlcpy(rpathbuf, socket_path, sizeof rpathbuf);
162: log_debug("socket path %s", socket_path);
163: setproctitle("server (%s)", rpathbuf);
164:
1.71 nicm 165: event_init();
166:
1.66 nicm 167: server_fd = server_create_socket();
1.60 nicm 168: server_client_create(pair[1]);
1.16 nicm 169:
1.82 nicm 170: if (access(SYSTEM_CFG, R_OK) == 0)
1.83 nicm 171: load_cfg(SYSTEM_CFG, NULL, &cfg_causes);
1.82 nicm 172: else if (errno != ENOENT) {
1.83 nicm 173: cfg_add_cause(
174: &cfg_causes, "%s: %s", strerror(errno), SYSTEM_CFG);
1.64 nicm 175: }
1.82 nicm 176: if (cfg_file != NULL)
1.83 nicm 177: load_cfg(cfg_file, NULL, &cfg_causes);
1.82 nicm 178:
179: /*
180: * If there is a session already, put the current window and pane into
181: * more mode.
182: */
1.83 nicm 183: if (!ARRAY_EMPTY(&sessions) && !ARRAY_EMPTY(&cfg_causes)) {
1.82 nicm 184: wp = ARRAY_FIRST(&sessions)->curw->window->active;
1.85 ! nicm 185: window_pane_set_mode(wp, &window_copy_mode);
! 186: window_copy_init_for_output(wp);
1.83 nicm 187: for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
188: cause = ARRAY_ITEM(&cfg_causes, i);
1.85 ! nicm 189: window_copy_add(wp, "%s", cause);
1.83 nicm 190: xfree(cause);
1.82 nicm 191: }
1.83 nicm 192: ARRAY_FREE(&cfg_causes);
1.82 nicm 193: }
194: cfg_finished = 1;
1.66 nicm 195:
196: event_set(&server_ev_accept,
197: server_fd, EV_READ|EV_PERSIST, server_accept_callback, NULL);
198: event_add(&server_ev_accept, NULL);
199:
200: memset(&tv, 0, sizeof tv);
201: tv.tv_sec = 1;
202: evtimer_set(&server_ev_second, server_second_callback, NULL);
203: evtimer_add(&server_ev_second, &tv);
204:
205: server_signal_set();
206: server_loop();
207: exit(0);
1.1 nicm 208: }
209:
210: /* Main server loop. */
1.66 nicm 211: void
212: server_loop(void)
1.1 nicm 213: {
1.66 nicm 214: while (!server_should_shutdown()) {
215: event_loop(EVLOOP_ONCE);
1.1 nicm 216:
1.60 nicm 217: server_window_loop();
218: server_client_loop();
1.1 nicm 219:
1.8 nicm 220: key_bindings_clean();
1.66 nicm 221: server_clean_dead();
1.80 nicm 222: }
1.66 nicm 223: }
1.31 nicm 224:
1.66 nicm 225: /* Check if the server should be shutting down (no more clients or windows). */
226: int
227: server_should_shutdown(void)
228: {
229: u_int i;
1.1 nicm 230:
231: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
232: if (ARRAY_ITEM(&sessions, i) != NULL)
1.66 nicm 233: return (0);
1.1 nicm 234: }
235: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
236: if (ARRAY_ITEM(&clients, i) != NULL)
1.66 nicm 237: return (0);
1.1 nicm 238: }
1.66 nicm 239: return (1);
1.1 nicm 240: }
241:
1.66 nicm 242: /* Shutdown the server by killing all clients and windows. */
1.1 nicm 243: void
1.66 nicm 244: server_send_shutdown(void)
1.1 nicm 245: {
1.66 nicm 246: struct client *c;
1.1 nicm 247: struct session *s;
1.66 nicm 248: u_int i;
1.1 nicm 249:
1.42 nicm 250: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
251: c = ARRAY_ITEM(&clients, i);
252: if (c != NULL) {
253: if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
1.60 nicm 254: server_client_lost(c);
1.42 nicm 255: else
256: server_write_client(c, MSG_SHUTDOWN, NULL, 0);
1.66 nicm 257: c->session = NULL;
1.42 nicm 258: }
259: }
260:
1.1 nicm 261: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.66 nicm 262: if ((s = ARRAY_ITEM(&sessions, i)) != NULL)
1.1 nicm 263: session_destroy(s);
264: }
1.42 nicm 265: }
266:
1.66 nicm 267: /* Free dead, unreferenced clients and sessions. */
268: void
269: server_clean_dead(void)
270: {
271: struct session *s;
272: struct client *c;
273: u_int i;
274:
275: for (i = 0; i < ARRAY_LENGTH(&dead_sessions); i++) {
276: s = ARRAY_ITEM(&dead_sessions, i);
277: if (s == NULL || s->references != 0)
278: continue;
279: ARRAY_SET(&dead_sessions, i, NULL);
280: xfree(s);
281: }
282:
283: for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
284: c = ARRAY_ITEM(&dead_clients, i);
285: if (c == NULL || c->references != 0)
286: continue;
287: ARRAY_SET(&dead_clients, i, NULL);
288: xfree(c);
289: }
290: }
291:
292: /* Update socket execute permissions based on whether sessions are attached. */
1.75 nicm 293: void
1.66 nicm 294: server_update_socket(void)
1.42 nicm 295: {
1.66 nicm 296: struct session *s;
297: u_int i;
298: static int last = -1;
299: int n;
1.1 nicm 300:
1.66 nicm 301: n = 0;
1.42 nicm 302: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.66 nicm 303: s = ARRAY_ITEM(&sessions, i);
304: if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
305: n++;
306: break;
307: }
308: }
309:
310: if (n != last) {
311: last = n;
312: if (n != 0)
313: chmod(socket_path, S_IRWXU);
314: else
315: chmod(socket_path, S_IRUSR|S_IWUSR);
316: }
317: }
318:
319: /* Callback for server socket. */
1.78 nicm 320: /* ARGSUSED */
1.66 nicm 321: void
322: server_accept_callback(int fd, short events, unused void *data)
323: {
324: struct sockaddr_storage sa;
325: socklen_t slen = sizeof sa;
326: int newfd;
327:
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;
335: fatal("accept failed");
336: }
337: if (server_shutdown) {
338: close(newfd);
339: return;
1.42 nicm 340: }
1.66 nicm 341: server_client_create(newfd);
342: }
343:
344: /* Set up server signal handling. */
345: void
346: server_signal_set(void)
347: {
348: struct sigaction sigact;
349:
350: memset(&sigact, 0, sizeof sigact);
351: sigemptyset(&sigact.sa_mask);
352: sigact.sa_flags = SA_RESTART;
353: sigact.sa_handler = SIG_IGN;
354: if (sigaction(SIGINT, &sigact, NULL) != 0)
355: fatal("sigaction failed");
356: if (sigaction(SIGPIPE, &sigact, NULL) != 0)
357: fatal("sigaction failed");
358: if (sigaction(SIGUSR2, &sigact, NULL) != 0)
359: fatal("sigaction failed");
360: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
361: fatal("sigaction failed");
1.81 nicm 362: if (sigaction(SIGHUP, &sigact, NULL) != 0)
363: fatal("sigaction failed");
1.66 nicm 364:
365: signal_set(&server_ev_sigchld, SIGCHLD, server_signal_callback, NULL);
366: signal_add(&server_ev_sigchld, NULL);
367: signal_set(&server_ev_sigterm, SIGTERM, server_signal_callback, NULL);
368: signal_add(&server_ev_sigterm, NULL);
369: signal_set(&server_ev_sigusr1, SIGUSR1, server_signal_callback, NULL);
370: signal_add(&server_ev_sigusr1, NULL);
371: }
372:
373: /* Destroy server signal events. */
374: void
375: server_signal_clear(void)
376: {
377: struct sigaction sigact;
378:
379: memset(&sigact, 0, sizeof sigact);
380: sigemptyset(&sigact.sa_mask);
381: sigact.sa_flags = SA_RESTART;
382: sigact.sa_handler = SIG_DFL;
383: if (sigaction(SIGINT, &sigact, NULL) != 0)
384: fatal("sigaction failed");
385: if (sigaction(SIGPIPE, &sigact, NULL) != 0)
386: fatal("sigaction failed");
387: if (sigaction(SIGUSR2, &sigact, NULL) != 0)
388: fatal("sigaction failed");
389: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
1.81 nicm 390: fatal("sigaction failed");
391: if (sigaction(SIGHUP, &sigact, NULL) != 0)
1.66 nicm 392: fatal("sigaction failed");
393:
394: signal_del(&server_ev_sigchld);
395: signal_del(&server_ev_sigterm);
396: signal_del(&server_ev_sigusr1);
397: }
398:
399: /* Signal handler. */
1.78 nicm 400: /* ARGSUSED */
1.66 nicm 401: void
402: server_signal_callback(int sig, unused short events, unused void *data)
403: {
404: switch (sig) {
405: case SIGTERM:
406: server_shutdown = 1;
407: server_send_shutdown();
408: break;
409: case SIGCHLD:
410: server_child_signal();
411: break;
412: case SIGUSR1:
413: event_del(&server_ev_accept);
414: close(server_fd);
415: server_fd = server_create_socket();
416: event_set(&server_ev_accept, server_fd,
417: EV_READ|EV_PERSIST, server_accept_callback, NULL);
418: event_add(&server_ev_accept, NULL);
419: break;
1.1 nicm 420: }
421: }
422:
423: /* Handle SIGCHLD. */
424: void
425: server_child_signal(void)
426: {
1.66 nicm 427: int status;
428: pid_t pid;
1.1 nicm 429:
430: for (;;) {
431: switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
432: case -1:
433: if (errno == ECHILD)
434: return;
1.39 nicm 435: fatal("waitpid failed");
1.1 nicm 436: case 0:
437: return;
438: }
1.66 nicm 439: if (WIFSTOPPED(status))
440: server_child_stopped(pid, status);
1.79 nicm 441: else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66 nicm 442: server_child_exited(pid, status);
443: }
444: }
445:
446: /* Handle exited children. */
447: void
448: server_child_exited(pid_t pid, int status)
449: {
450: struct window *w;
451: struct window_pane *wp;
452: struct job *job;
453: u_int i;
454:
455: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
456: if ((w = ARRAY_ITEM(&windows, i)) == NULL)
457: continue;
458: TAILQ_FOREACH(wp, &w->panes, entry) {
459: if (wp->pid == pid) {
1.77 nicm 460: server_destroy_pane(wp);
461: break;
1.53 nicm 462: }
463: }
1.80 nicm 464: }
1.1 nicm 465:
1.66 nicm 466: SLIST_FOREACH(job, &all_jobs, lentry) {
467: if (pid == job->pid) {
1.67 nicm 468: job_died(job, status); /* might free job */
469: break;
1.1 nicm 470: }
1.53 nicm 471: }
472: }
473:
1.66 nicm 474: /* Handle stopped children. */
1.31 nicm 475: void
1.66 nicm 476: server_child_stopped(pid_t pid, int status)
1.31 nicm 477: {
1.66 nicm 478: struct window *w;
479: struct window_pane *wp;
480: u_int i;
1.31 nicm 481:
1.66 nicm 482: if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
483: return;
1.31 nicm 484:
1.66 nicm 485: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
486: if ((w = ARRAY_ITEM(&windows, i)) == NULL)
1.31 nicm 487: continue;
1.66 nicm 488: TAILQ_FOREACH(wp, &w->panes, entry) {
489: if (wp->pid == pid) {
490: if (killpg(pid, SIGCONT) != 0)
491: kill(pid, SIGCONT);
492: }
493: }
1.31 nicm 494: }
1.1 nicm 495: }
496:
1.66 nicm 497: /* Handle once-per-second timer events. */
1.78 nicm 498: /* ARGSUSED */
1.1 nicm 499: void
1.66 nicm 500: server_second_callback(unused int fd, unused short events, unused void *arg)
1.1 nicm 501: {
1.60 nicm 502: struct window *w;
503: struct window_pane *wp;
1.66 nicm 504: struct timeval tv;
1.35 nicm 505: u_int i;
1.1 nicm 506:
1.60 nicm 507: if (options_get_number(&global_s_options, "lock-server"))
508: server_lock_server();
509: else
510: server_lock_sessions();
1.1 nicm 511:
1.60 nicm 512: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
513: w = ARRAY_ITEM(&windows, i);
514: if (w == NULL)
1.1 nicm 515: continue;
516:
1.60 nicm 517: TAILQ_FOREACH(wp, &w->panes, entry) {
518: if (wp->mode != NULL && wp->mode->timer != NULL)
519: wp->mode->timer(wp);
1.1 nicm 520: }
521: }
1.72 nicm 522:
523: server_client_status_timer();
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: u_int i;
537: int timeout;
538: time_t t;
539:
540: t = time(NULL);
541: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
542: if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
543: continue;
544:
1.59 nicm 545: if (s->flags & SESSION_UNATTACHED) {
1.65 nicm 546: if (gettimeofday(&s->activity_time, NULL) != 0)
547: fatal("gettimeofday failed");
1.59 nicm 548: continue;
549: }
550:
1.46 nicm 551: timeout = options_get_number(&s->options, "lock-after-time");
1.65 nicm 552: if (timeout <= 0 || t <= s->activity_time.tv_sec + timeout)
1.46 nicm 553: return; /* not timed out */
554: }
555:
556: server_lock();
557: recalculate_sizes();
558: }
559:
560: /* Lock any sessions which have timed out. */
561: void
562: server_lock_sessions(void)
563: {
1.80 nicm 564: struct session *s;
1.62 deraadt 565: u_int i;
1.46 nicm 566: int timeout;
1.62 deraadt 567: time_t t;
1.46 nicm 568:
1.62 deraadt 569: t = time(NULL);
570: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.46 nicm 571: if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
572: continue;
1.59 nicm 573:
574: if (s->flags & SESSION_UNATTACHED) {
1.65 nicm 575: if (gettimeofday(&s->activity_time, NULL) != 0)
576: fatal("gettimeofday failed");
1.59 nicm 577: continue;
578: }
1.46 nicm 579:
580: timeout = options_get_number(&s->options, "lock-after-time");
1.65 nicm 581: if (timeout > 0 && t > s->activity_time.tv_sec + timeout) {
1.46 nicm 582: server_lock_session(s);
583: recalculate_sizes();
1.1 nicm 584: }
585: }
586: }