Annotation of src/usr.bin/tmux/server.c, Revision 1.68
1.68 ! nicm 1: /* $OpenBSD: server.c,v 1.67 2009/11/04 21:04:43 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.66 nicm 163: server_fd = server_create_socket();
1.60 nicm 164: server_client_create(pair[1]);
1.16 nicm 165:
1.68 ! nicm 166: event_init();
! 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);
196: xfree(cause);
1.1 nicm 197:
1.66 nicm 198: server_shutdown = 1;
1.1 nicm 199:
1.66 nicm 200: server_signal_set();
201: server_loop();
202: exit(1);
1.1 nicm 203: }
204:
205: /* Main server loop. */
1.66 nicm 206: void
207: server_loop(void)
1.1 nicm 208: {
1.66 nicm 209: struct timeval tv;
1.42 nicm 210:
1.66 nicm 211: memset(&tv, 0, sizeof tv);
212: tv.tv_usec = POLL_TIMEOUT * 1000;
1.1 nicm 213:
1.66 nicm 214: while (!server_should_shutdown()) {
215: server_update_socket();
1.1 nicm 216:
1.63 nicm 217: server_window_prepare();
218: server_client_prepare();
1.1 nicm 219:
1.66 nicm 220: event_loopexit(&tv);
221: event_loop(EVLOOP_ONCE);
1.1 nicm 222:
1.60 nicm 223: server_window_loop();
224: server_client_loop();
1.1 nicm 225:
1.8 nicm 226: key_bindings_clean();
1.66 nicm 227: server_clean_dead();
228: }
229: }
1.31 nicm 230:
1.66 nicm 231: /* Check if the server should be shutting down (no more clients or windows). */
232: int
233: server_should_shutdown(void)
234: {
235: u_int i;
1.1 nicm 236:
237: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
238: if (ARRAY_ITEM(&sessions, i) != NULL)
1.66 nicm 239: return (0);
1.1 nicm 240: }
241: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
242: if (ARRAY_ITEM(&clients, i) != NULL)
1.66 nicm 243: return (0);
1.1 nicm 244: }
1.66 nicm 245: return (1);
1.1 nicm 246: }
247:
1.66 nicm 248: /* Shutdown the server by killing all clients and windows. */
1.1 nicm 249: void
1.66 nicm 250: server_send_shutdown(void)
1.1 nicm 251: {
1.66 nicm 252: struct client *c;
1.1 nicm 253: struct session *s;
1.66 nicm 254: u_int i;
1.1 nicm 255:
1.42 nicm 256: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
257: c = ARRAY_ITEM(&clients, i);
258: if (c != NULL) {
259: if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
1.60 nicm 260: server_client_lost(c);
1.42 nicm 261: else
262: server_write_client(c, MSG_SHUTDOWN, NULL, 0);
1.66 nicm 263: c->session = NULL;
1.42 nicm 264: }
265: }
266:
1.1 nicm 267: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.66 nicm 268: if ((s = ARRAY_ITEM(&sessions, i)) != NULL)
1.1 nicm 269: session_destroy(s);
270: }
1.42 nicm 271: }
272:
1.66 nicm 273: /* Free dead, unreferenced clients and sessions. */
274: void
275: server_clean_dead(void)
276: {
277: struct session *s;
278: struct client *c;
279: u_int i;
280:
281: for (i = 0; i < ARRAY_LENGTH(&dead_sessions); i++) {
282: s = ARRAY_ITEM(&dead_sessions, i);
283: if (s == NULL || s->references != 0)
284: continue;
285: ARRAY_SET(&dead_sessions, i, NULL);
286: xfree(s);
287: }
288:
289: for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
290: c = ARRAY_ITEM(&dead_clients, i);
291: if (c == NULL || c->references != 0)
292: continue;
293: ARRAY_SET(&dead_clients, i, NULL);
294: xfree(c);
295: }
296: }
297:
298: /* Update socket execute permissions based on whether sessions are attached. */
1.42 nicm 299: int
1.66 nicm 300: server_update_socket(void)
1.42 nicm 301: {
1.66 nicm 302: struct session *s;
303: u_int i;
304: static int last = -1;
305: int n;
1.1 nicm 306:
1.66 nicm 307: n = 0;
1.42 nicm 308: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.66 nicm 309: s = ARRAY_ITEM(&sessions, i);
310: if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
311: n++;
312: break;
313: }
314: }
315:
316: if (n != last) {
317: last = n;
318: if (n != 0)
319: chmod(socket_path, S_IRWXU);
320: else
321: chmod(socket_path, S_IRUSR|S_IWUSR);
322: }
323:
324: return (n);
325: }
326:
327: /* Callback for server socket. */
328: void
329: server_accept_callback(int fd, short events, unused void *data)
330: {
331: struct sockaddr_storage sa;
332: socklen_t slen = sizeof sa;
333: int newfd;
334:
335: if (!(events & EV_READ))
336: return;
337:
338: newfd = accept(fd, (struct sockaddr *) &sa, &slen);
339: if (newfd == -1) {
340: if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
341: return;
342: fatal("accept failed");
343: }
344: if (server_shutdown) {
345: close(newfd);
346: return;
1.42 nicm 347: }
1.66 nicm 348: server_client_create(newfd);
349:
350: }
351:
352: /* Set up server signal handling. */
353: void
354: server_signal_set(void)
355: {
356: struct sigaction sigact;
357:
358: memset(&sigact, 0, sizeof sigact);
359: sigemptyset(&sigact.sa_mask);
360: sigact.sa_flags = SA_RESTART;
361: sigact.sa_handler = SIG_IGN;
362: if (sigaction(SIGINT, &sigact, NULL) != 0)
363: fatal("sigaction failed");
364: if (sigaction(SIGPIPE, &sigact, NULL) != 0)
365: fatal("sigaction failed");
366: if (sigaction(SIGUSR2, &sigact, NULL) != 0)
367: fatal("sigaction failed");
368: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
369: fatal("sigaction failed");
370:
371: signal_set(&server_ev_sigchld, SIGCHLD, server_signal_callback, NULL);
372: signal_add(&server_ev_sigchld, NULL);
373: signal_set(&server_ev_sigterm, SIGTERM, server_signal_callback, NULL);
374: signal_add(&server_ev_sigterm, NULL);
375: signal_set(&server_ev_sigusr1, SIGUSR1, server_signal_callback, NULL);
376: signal_add(&server_ev_sigusr1, NULL);
377: }
378:
379: /* Destroy server signal events. */
380: void
381: server_signal_clear(void)
382: {
383: struct sigaction sigact;
384:
385: memset(&sigact, 0, sizeof sigact);
386: sigemptyset(&sigact.sa_mask);
387: sigact.sa_flags = SA_RESTART;
388: sigact.sa_handler = SIG_DFL;
389: if (sigaction(SIGINT, &sigact, NULL) != 0)
390: fatal("sigaction failed");
391: if (sigaction(SIGPIPE, &sigact, NULL) != 0)
392: fatal("sigaction failed");
393: if (sigaction(SIGUSR2, &sigact, NULL) != 0)
394: fatal("sigaction failed");
395: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
396: fatal("sigaction failed");
397:
398: signal_del(&server_ev_sigchld);
399: signal_del(&server_ev_sigterm);
400: signal_del(&server_ev_sigusr1);
401: }
402:
403: /* Signal handler. */
404: void
405: server_signal_callback(int sig, unused short events, unused void *data)
406: {
407: switch (sig) {
408: case SIGTERM:
409: server_shutdown = 1;
410: server_send_shutdown();
411: break;
412: case SIGCHLD:
413: server_child_signal();
414: break;
415: case SIGUSR1:
416: event_del(&server_ev_accept);
417: close(server_fd);
418: server_fd = server_create_socket();
419: event_set(&server_ev_accept, server_fd,
420: EV_READ|EV_PERSIST, server_accept_callback, NULL);
421: event_add(&server_ev_accept, NULL);
422: break;
1.1 nicm 423: }
424: }
425:
426: /* Handle SIGCHLD. */
427: void
428: server_child_signal(void)
429: {
1.66 nicm 430: int status;
431: pid_t pid;
1.1 nicm 432:
433: for (;;) {
434: switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
435: case -1:
436: if (errno == ECHILD)
437: return;
1.39 nicm 438: fatal("waitpid failed");
1.1 nicm 439: case 0:
440: return;
441: }
1.66 nicm 442: if (WIFSTOPPED(status))
443: server_child_stopped(pid, status);
444: else if (WIFEXITED(status))
445: server_child_exited(pid, status);
446: }
447: }
448:
449: /* Handle exited children. */
450: void
451: server_child_exited(pid_t pid, int status)
452: {
453: struct window *w;
454: struct window_pane *wp;
455: struct job *job;
456: u_int i;
457:
458: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
459: if ((w = ARRAY_ITEM(&windows, i)) == NULL)
460: continue;
461: TAILQ_FOREACH(wp, &w->panes, entry) {
462: if (wp->pid == pid) {
463: close(wp->fd);
464: wp->fd = -1;
1.53 nicm 465: }
466: }
1.66 nicm 467: }
1.1 nicm 468:
1.66 nicm 469: SLIST_FOREACH(job, &all_jobs, lentry) {
470: if (pid == job->pid) {
1.67 nicm 471: job_died(job, status); /* might free job */
472: break;
1.1 nicm 473: }
1.53 nicm 474: }
475: }
476:
1.66 nicm 477: /* Handle stopped children. */
1.31 nicm 478: void
1.66 nicm 479: server_child_stopped(pid_t pid, int status)
1.31 nicm 480: {
1.66 nicm 481: struct window *w;
482: struct window_pane *wp;
483: u_int i;
1.31 nicm 484:
1.66 nicm 485: if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
486: return;
1.31 nicm 487:
1.66 nicm 488: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
489: if ((w = ARRAY_ITEM(&windows, i)) == NULL)
1.31 nicm 490: continue;
1.66 nicm 491: TAILQ_FOREACH(wp, &w->panes, entry) {
492: if (wp->pid == pid) {
493: if (killpg(pid, SIGCONT) != 0)
494: kill(pid, SIGCONT);
495: }
496: }
1.31 nicm 497: }
1.1 nicm 498: }
499:
1.66 nicm 500: /* Handle once-per-second timer events. */
1.1 nicm 501: void
1.66 nicm 502: server_second_callback(unused int fd, unused short events, unused void *arg)
1.1 nicm 503: {
1.60 nicm 504: struct window *w;
505: struct window_pane *wp;
1.66 nicm 506: struct timeval tv;
1.35 nicm 507: u_int i;
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.60 nicm 514: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
515: w = ARRAY_ITEM(&windows, i);
516: if (w == NULL)
1.1 nicm 517: continue;
518:
1.60 nicm 519: TAILQ_FOREACH(wp, &w->panes, entry) {
520: if (wp->mode != NULL && wp->mode->timer != NULL)
521: wp->mode->timer(wp);
1.1 nicm 522: }
523: }
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: {
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: }