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