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