Annotation of src/usr.bin/tmux/server.c, Revision 1.69
1.69 ! nicm 1: /* $OpenBSD: server.c,v 1.68 2009/11/04 22:40:36 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_client_prepare();
1.1 nicm 218:
1.66 nicm 219: event_loopexit(&tv);
220: event_loop(EVLOOP_ONCE);
1.1 nicm 221:
1.60 nicm 222: server_window_loop();
223: server_client_loop();
1.1 nicm 224:
1.8 nicm 225: key_bindings_clean();
1.66 nicm 226: server_clean_dead();
227: }
228: }
1.31 nicm 229:
1.66 nicm 230: /* Check if the server should be shutting down (no more clients or windows). */
231: int
232: server_should_shutdown(void)
233: {
234: u_int i;
1.1 nicm 235:
236: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
237: if (ARRAY_ITEM(&sessions, i) != NULL)
1.66 nicm 238: return (0);
1.1 nicm 239: }
240: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
241: if (ARRAY_ITEM(&clients, i) != NULL)
1.66 nicm 242: return (0);
1.1 nicm 243: }
1.66 nicm 244: return (1);
1.1 nicm 245: }
246:
1.66 nicm 247: /* Shutdown the server by killing all clients and windows. */
1.1 nicm 248: void
1.66 nicm 249: server_send_shutdown(void)
1.1 nicm 250: {
1.66 nicm 251: struct client *c;
1.1 nicm 252: struct session *s;
1.66 nicm 253: u_int i;
1.1 nicm 254:
1.42 nicm 255: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
256: c = ARRAY_ITEM(&clients, i);
257: if (c != NULL) {
258: if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
1.60 nicm 259: server_client_lost(c);
1.42 nicm 260: else
261: server_write_client(c, MSG_SHUTDOWN, NULL, 0);
1.66 nicm 262: c->session = NULL;
1.42 nicm 263: }
264: }
265:
1.1 nicm 266: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.66 nicm 267: if ((s = ARRAY_ITEM(&sessions, i)) != NULL)
1.1 nicm 268: session_destroy(s);
269: }
1.42 nicm 270: }
271:
1.66 nicm 272: /* Free dead, unreferenced clients and sessions. */
273: void
274: server_clean_dead(void)
275: {
276: struct session *s;
277: struct client *c;
278: u_int i;
279:
280: for (i = 0; i < ARRAY_LENGTH(&dead_sessions); i++) {
281: s = ARRAY_ITEM(&dead_sessions, i);
282: if (s == NULL || s->references != 0)
283: continue;
284: ARRAY_SET(&dead_sessions, i, NULL);
285: xfree(s);
286: }
287:
288: for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
289: c = ARRAY_ITEM(&dead_clients, i);
290: if (c == NULL || c->references != 0)
291: continue;
292: ARRAY_SET(&dead_clients, i, NULL);
293: xfree(c);
294: }
295: }
296:
297: /* Update socket execute permissions based on whether sessions are attached. */
1.42 nicm 298: int
1.66 nicm 299: server_update_socket(void)
1.42 nicm 300: {
1.66 nicm 301: struct session *s;
302: u_int i;
303: static int last = -1;
304: int n;
1.1 nicm 305:
1.66 nicm 306: n = 0;
1.42 nicm 307: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.66 nicm 308: s = ARRAY_ITEM(&sessions, i);
309: if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
310: n++;
311: break;
312: }
313: }
314:
315: if (n != last) {
316: last = n;
317: if (n != 0)
318: chmod(socket_path, S_IRWXU);
319: else
320: chmod(socket_path, S_IRUSR|S_IWUSR);
321: }
322:
323: return (n);
324: }
325:
326: /* Callback for server socket. */
327: void
328: server_accept_callback(int fd, short events, unused void *data)
329: {
330: struct sockaddr_storage sa;
331: socklen_t slen = sizeof sa;
332: int newfd;
333:
334: if (!(events & EV_READ))
335: return;
336:
337: newfd = accept(fd, (struct sockaddr *) &sa, &slen);
338: if (newfd == -1) {
339: if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
340: return;
341: fatal("accept failed");
342: }
343: if (server_shutdown) {
344: close(newfd);
345: return;
1.42 nicm 346: }
1.66 nicm 347: server_client_create(newfd);
348:
349: }
350:
351: /* Set up server signal handling. */
352: void
353: server_signal_set(void)
354: {
355: struct sigaction sigact;
356:
357: memset(&sigact, 0, sizeof sigact);
358: sigemptyset(&sigact.sa_mask);
359: sigact.sa_flags = SA_RESTART;
360: sigact.sa_handler = SIG_IGN;
361: if (sigaction(SIGINT, &sigact, NULL) != 0)
362: fatal("sigaction failed");
363: if (sigaction(SIGPIPE, &sigact, NULL) != 0)
364: fatal("sigaction failed");
365: if (sigaction(SIGUSR2, &sigact, NULL) != 0)
366: fatal("sigaction failed");
367: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
368: fatal("sigaction failed");
369:
370: signal_set(&server_ev_sigchld, SIGCHLD, server_signal_callback, NULL);
371: signal_add(&server_ev_sigchld, NULL);
372: signal_set(&server_ev_sigterm, SIGTERM, server_signal_callback, NULL);
373: signal_add(&server_ev_sigterm, NULL);
374: signal_set(&server_ev_sigusr1, SIGUSR1, server_signal_callback, NULL);
375: signal_add(&server_ev_sigusr1, NULL);
376: }
377:
378: /* Destroy server signal events. */
379: void
380: server_signal_clear(void)
381: {
382: struct sigaction sigact;
383:
384: memset(&sigact, 0, sizeof sigact);
385: sigemptyset(&sigact.sa_mask);
386: sigact.sa_flags = SA_RESTART;
387: sigact.sa_handler = SIG_DFL;
388: if (sigaction(SIGINT, &sigact, NULL) != 0)
389: fatal("sigaction failed");
390: if (sigaction(SIGPIPE, &sigact, NULL) != 0)
391: fatal("sigaction failed");
392: if (sigaction(SIGUSR2, &sigact, NULL) != 0)
393: fatal("sigaction failed");
394: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
395: fatal("sigaction failed");
396:
397: signal_del(&server_ev_sigchld);
398: signal_del(&server_ev_sigterm);
399: signal_del(&server_ev_sigusr1);
400: }
401:
402: /* Signal handler. */
403: void
404: server_signal_callback(int sig, unused short events, unused void *data)
405: {
406: switch (sig) {
407: case SIGTERM:
408: server_shutdown = 1;
409: server_send_shutdown();
410: break;
411: case SIGCHLD:
412: server_child_signal();
413: break;
414: case SIGUSR1:
415: event_del(&server_ev_accept);
416: close(server_fd);
417: server_fd = server_create_socket();
418: event_set(&server_ev_accept, server_fd,
419: EV_READ|EV_PERSIST, server_accept_callback, NULL);
420: event_add(&server_ev_accept, NULL);
421: break;
1.1 nicm 422: }
423: }
424:
425: /* Handle SIGCHLD. */
426: void
427: server_child_signal(void)
428: {
1.66 nicm 429: int status;
430: pid_t pid;
1.1 nicm 431:
432: for (;;) {
433: switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
434: case -1:
435: if (errno == ECHILD)
436: return;
1.39 nicm 437: fatal("waitpid failed");
1.1 nicm 438: case 0:
439: return;
440: }
1.66 nicm 441: if (WIFSTOPPED(status))
442: server_child_stopped(pid, status);
443: else if (WIFEXITED(status))
444: server_child_exited(pid, status);
445: }
446: }
447:
448: /* Handle exited children. */
449: void
450: server_child_exited(pid_t pid, int status)
451: {
452: struct window *w;
453: struct window_pane *wp;
454: struct job *job;
455: u_int i;
456:
457: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
458: if ((w = ARRAY_ITEM(&windows, i)) == NULL)
459: continue;
460: TAILQ_FOREACH(wp, &w->panes, entry) {
461: if (wp->pid == pid) {
462: close(wp->fd);
463: wp->fd = -1;
1.53 nicm 464: }
465: }
1.66 nicm 466: }
1.1 nicm 467:
1.66 nicm 468: SLIST_FOREACH(job, &all_jobs, lentry) {
469: if (pid == job->pid) {
1.67 nicm 470: job_died(job, status); /* might free job */
471: break;
1.1 nicm 472: }
1.53 nicm 473: }
474: }
475:
1.66 nicm 476: /* Handle stopped children. */
1.31 nicm 477: void
1.66 nicm 478: server_child_stopped(pid_t pid, int status)
1.31 nicm 479: {
1.66 nicm 480: struct window *w;
481: struct window_pane *wp;
482: u_int i;
1.31 nicm 483:
1.66 nicm 484: if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
485: return;
1.31 nicm 486:
1.66 nicm 487: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
488: if ((w = ARRAY_ITEM(&windows, i)) == NULL)
1.31 nicm 489: continue;
1.66 nicm 490: TAILQ_FOREACH(wp, &w->panes, entry) {
491: if (wp->pid == pid) {
492: if (killpg(pid, SIGCONT) != 0)
493: kill(pid, SIGCONT);
494: }
495: }
1.31 nicm 496: }
1.1 nicm 497: }
498:
1.66 nicm 499: /* Handle once-per-second timer events. */
1.1 nicm 500: void
1.66 nicm 501: server_second_callback(unused int fd, unused short events, unused void *arg)
1.1 nicm 502: {
1.60 nicm 503: struct window *w;
504: struct window_pane *wp;
1.66 nicm 505: struct timeval tv;
1.35 nicm 506: u_int i;
1.1 nicm 507:
1.60 nicm 508: if (options_get_number(&global_s_options, "lock-server"))
509: server_lock_server();
510: else
511: server_lock_sessions();
1.1 nicm 512:
1.60 nicm 513: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
514: w = ARRAY_ITEM(&windows, i);
515: if (w == NULL)
1.1 nicm 516: continue;
517:
1.60 nicm 518: TAILQ_FOREACH(wp, &w->panes, entry) {
519: if (wp->mode != NULL && wp->mode->timer != NULL)
520: wp->mode->timer(wp);
1.1 nicm 521: }
522: }
1.66 nicm 523:
524: evtimer_del(&server_ev_second);
525: memset(&tv, 0, sizeof tv);
526: tv.tv_sec = 1;
527: evtimer_add(&server_ev_second, &tv);
1.1 nicm 528: }
529:
1.46 nicm 530: /* Lock the server if ALL sessions have hit the time limit. */
531: void
532: server_lock_server(void)
533: {
534: struct session *s;
535: u_int i;
536: int timeout;
537: time_t t;
538:
539: t = time(NULL);
540: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
541: if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
542: continue;
543:
1.59 nicm 544: if (s->flags & SESSION_UNATTACHED) {
1.65 nicm 545: if (gettimeofday(&s->activity_time, NULL) != 0)
546: fatal("gettimeofday failed");
1.59 nicm 547: continue;
548: }
549:
1.46 nicm 550: timeout = options_get_number(&s->options, "lock-after-time");
1.65 nicm 551: if (timeout <= 0 || t <= s->activity_time.tv_sec + timeout)
1.46 nicm 552: return; /* not timed out */
553: }
554:
555: server_lock();
556: recalculate_sizes();
557: }
558:
559: /* Lock any sessions which have timed out. */
560: void
561: server_lock_sessions(void)
562: {
563: struct session *s;
1.62 deraadt 564: u_int i;
1.46 nicm 565: int timeout;
1.62 deraadt 566: time_t t;
1.46 nicm 567:
1.62 deraadt 568: t = time(NULL);
569: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.46 nicm 570: if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
571: continue;
1.59 nicm 572:
573: if (s->flags & SESSION_UNATTACHED) {
1.65 nicm 574: if (gettimeofday(&s->activity_time, NULL) != 0)
575: fatal("gettimeofday failed");
1.59 nicm 576: continue;
577: }
1.46 nicm 578:
579: timeout = options_get_number(&s->options, "lock-after-time");
1.65 nicm 580: if (timeout > 0 && t > s->activity_time.tv_sec + timeout) {
1.46 nicm 581: server_lock_session(s);
582: recalculate_sizes();
1.1 nicm 583: }
584: }
585: }