Annotation of src/usr.bin/tmux/server.c, Revision 1.94
1.94 ! nicm 1: /* $OpenBSD: server.c,v 1.93 2010/09/26 18:51:48 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_second;
1.45 nicm 53:
1.1 nicm 54: int server_create_socket(void);
1.66 nicm 55: void server_loop(void);
1.42 nicm 56: int server_should_shutdown(void);
1.66 nicm 57: void server_send_shutdown(void);
58: void server_clean_dead(void);
59: void server_accept_callback(int, short, void *);
60: void server_signal_callback(int, short, void *);
1.1 nicm 61: void server_child_signal(void);
1.66 nicm 62: void server_child_exited(pid_t, int);
63: void server_child_stopped(pid_t, int);
64: void server_second_callback(int, short, void *);
1.46 nicm 65: void server_lock_server(void);
66: void server_lock_sessions(void);
1.45 nicm 67:
1.60 nicm 68: /* Create server socket. */
69: int
70: server_create_socket(void)
1.1 nicm 71: {
1.60 nicm 72: struct sockaddr_un sa;
73: size_t size;
74: mode_t mask;
75: int fd, mode;
76:
77: memset(&sa, 0, sizeof sa);
78: sa.sun_family = AF_UNIX;
79: size = strlcpy(sa.sun_path, socket_path, sizeof sa.sun_path);
80: if (size >= sizeof sa.sun_path) {
81: errno = ENAMETOOLONG;
82: fatal("socket failed");
83: }
84: unlink(sa.sun_path);
85:
86: if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
87: fatal("socket failed");
88:
1.89 nicm 89: mask = umask(S_IXUSR|S_IXGRP|S_IRWXO);
1.60 nicm 90: if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1)
91: fatal("bind failed");
92: umask(mask);
93:
94: if (listen(fd, 16) == -1)
95: fatal("listen failed");
1.1 nicm 96:
97: if ((mode = fcntl(fd, F_GETFL)) == -1)
98: fatal("fcntl failed");
99: if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
100: fatal("fcntl failed");
101: if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
102: fatal("fcntl failed");
103:
1.75 nicm 104: server_update_socket();
105:
1.60 nicm 106: return (fd);
107: }
108:
1.1 nicm 109: /* Fork new server. */
110: int
111: server_start(char *path)
112: {
1.82 nicm 113: struct window_pane *wp;
1.84 nicm 114: int pair[2];
1.83 nicm 115: char rpathbuf[MAXPATHLEN], *cause;
1.82 nicm 116: struct timeval tv;
117: u_int i;
1.1 nicm 118:
119: /* The first client is special and gets a socketpair; create it. */
120: if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
121: fatal("socketpair failed");
122:
123: switch (fork()) {
124: case -1:
125: fatal("fork failed");
126: case 0:
127: break;
128: default:
129: close(pair[1]);
130: return (pair[0]);
131: }
132: close(pair[0]);
133:
134: /*
135: * Must daemonise before loading configuration as the PID changes so
136: * $TMUX would be wrong for sessions created in the config file.
137: */
1.16 nicm 138: if (daemon(1, 0) != 0)
1.1 nicm 139: fatal("daemon failed");
140:
1.88 nicm 141: /* event_init() was called in our parent, need to reinit. */
142: if (event_reinit(ev_base) != 0)
143: fatal("event_reinit failed");
1.92 nicm 144: clear_signals(0);
1.88 nicm 145:
1.16 nicm 146: logfile("server");
147: log_debug("server started, pid %ld", (long) getpid());
148:
1.1 nicm 149: ARRAY_INIT(&windows);
150: ARRAY_INIT(&clients);
1.31 nicm 151: ARRAY_INIT(&dead_clients);
1.1 nicm 152: ARRAY_INIT(&sessions);
1.31 nicm 153: ARRAY_INIT(&dead_sessions);
1.47 nicm 154: TAILQ_INIT(&session_groups);
1.15 nicm 155: mode_key_init_trees();
1.1 nicm 156: key_bindings_init();
157: utf8_build();
158:
159: start_time = time(NULL);
160: socket_path = path;
161:
1.16 nicm 162: if (realpath(socket_path, rpathbuf) == NULL)
163: strlcpy(rpathbuf, socket_path, sizeof rpathbuf);
164: log_debug("socket path %s", socket_path);
165: setproctitle("server (%s)", rpathbuf);
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:
1.88 nicm 205: set_signals(server_signal_callback);
1.66 nicm 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.94 ! nicm 225: /* Check if the server should be shutting down (no more clients or sessions). */
1.66 nicm 226: int
227: server_should_shutdown(void)
228: {
229: u_int i;
1.1 nicm 230:
1.94 ! nicm 231: if (!options_get_number(&global_options, "exit-unattached")) {
! 232: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
! 233: if (ARRAY_ITEM(&sessions, i) != NULL)
! 234: return (0);
! 235: }
1.1 nicm 236: }
237: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
238: if (ARRAY_ITEM(&clients, i) != NULL)
1.66 nicm 239: return (0);
1.1 nicm 240: }
1.66 nicm 241: return (1);
1.1 nicm 242: }
243:
1.66 nicm 244: /* Shutdown the server by killing all clients and windows. */
1.1 nicm 245: void
1.66 nicm 246: server_send_shutdown(void)
1.1 nicm 247: {
1.66 nicm 248: struct client *c;
1.1 nicm 249: struct session *s;
1.66 nicm 250: u_int i;
1.1 nicm 251:
1.42 nicm 252: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
253: c = ARRAY_ITEM(&clients, i);
254: if (c != NULL) {
255: if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
1.60 nicm 256: server_client_lost(c);
1.42 nicm 257: else
258: server_write_client(c, MSG_SHUTDOWN, NULL, 0);
1.66 nicm 259: c->session = NULL;
1.42 nicm 260: }
261: }
262:
1.1 nicm 263: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.66 nicm 264: if ((s = ARRAY_ITEM(&sessions, i)) != NULL)
1.1 nicm 265: session_destroy(s);
266: }
1.42 nicm 267: }
268:
1.66 nicm 269: /* Free dead, unreferenced clients and sessions. */
270: void
271: server_clean_dead(void)
272: {
273: struct session *s;
274: struct client *c;
275: u_int i;
276:
277: for (i = 0; i < ARRAY_LENGTH(&dead_sessions); i++) {
278: s = ARRAY_ITEM(&dead_sessions, i);
279: if (s == NULL || s->references != 0)
280: continue;
281: ARRAY_SET(&dead_sessions, i, NULL);
282: xfree(s);
283: }
284:
285: for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
286: c = ARRAY_ITEM(&dead_clients, i);
287: if (c == NULL || c->references != 0)
288: continue;
289: ARRAY_SET(&dead_clients, i, NULL);
290: xfree(c);
291: }
292: }
293:
294: /* Update socket execute permissions based on whether sessions are attached. */
1.75 nicm 295: void
1.66 nicm 296: server_update_socket(void)
1.42 nicm 297: {
1.66 nicm 298: struct session *s;
299: u_int i;
300: static int last = -1;
1.93 nicm 301: int n, mode;
302: struct stat sb;
1.1 nicm 303:
1.66 nicm 304: n = 0;
1.42 nicm 305: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.66 nicm 306: s = ARRAY_ITEM(&sessions, i);
307: if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
308: n++;
309: break;
310: }
311: }
312:
313: if (n != last) {
314: last = n;
1.93 nicm 315:
316: if (stat(socket_path, &sb) != 0)
317: return;
318: mode = sb.st_mode;
319: if (n != 0) {
320: if (mode & S_IRUSR)
321: mode |= S_IXUSR;
322: if (mode & S_IRGRP)
323: mode |= S_IXGRP;
324: if (mode & S_IROTH)
325: mode |= S_IXOTH;
326: } else
327: mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
328: chmod(socket_path, mode);
1.66 nicm 329: }
330: }
331:
332: /* Callback for server socket. */
1.78 nicm 333: /* ARGSUSED */
1.66 nicm 334: void
335: server_accept_callback(int fd, short events, unused void *data)
336: {
337: struct sockaddr_storage sa;
338: socklen_t slen = sizeof sa;
339: int newfd;
340:
341: if (!(events & EV_READ))
342: return;
343:
344: newfd = accept(fd, (struct sockaddr *) &sa, &slen);
345: if (newfd == -1) {
346: if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
347: return;
348: fatal("accept failed");
349: }
350: if (server_shutdown) {
351: close(newfd);
352: return;
1.42 nicm 353: }
1.66 nicm 354: server_client_create(newfd);
355: }
356:
357: /* Signal handler. */
1.78 nicm 358: /* ARGSUSED */
1.66 nicm 359: void
360: server_signal_callback(int sig, unused short events, unused void *data)
361: {
362: switch (sig) {
363: case SIGTERM:
364: server_shutdown = 1;
365: server_send_shutdown();
366: break;
367: case SIGCHLD:
368: server_child_signal();
369: break;
370: case SIGUSR1:
371: event_del(&server_ev_accept);
372: close(server_fd);
373: server_fd = server_create_socket();
374: event_set(&server_ev_accept, server_fd,
375: EV_READ|EV_PERSIST, server_accept_callback, NULL);
376: event_add(&server_ev_accept, NULL);
377: break;
1.1 nicm 378: }
379: }
380:
381: /* Handle SIGCHLD. */
382: void
383: server_child_signal(void)
384: {
1.66 nicm 385: int status;
386: pid_t pid;
1.1 nicm 387:
388: for (;;) {
389: switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
390: case -1:
391: if (errno == ECHILD)
392: return;
1.39 nicm 393: fatal("waitpid failed");
1.1 nicm 394: case 0:
395: return;
396: }
1.66 nicm 397: if (WIFSTOPPED(status))
398: server_child_stopped(pid, status);
1.79 nicm 399: else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66 nicm 400: server_child_exited(pid, status);
401: }
402: }
403:
404: /* Handle exited children. */
405: void
406: server_child_exited(pid_t pid, int status)
407: {
408: struct window *w;
409: struct window_pane *wp;
410: struct job *job;
411: u_int i;
412:
413: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
414: if ((w = ARRAY_ITEM(&windows, i)) == NULL)
415: continue;
416: TAILQ_FOREACH(wp, &w->panes, entry) {
417: if (wp->pid == pid) {
1.77 nicm 418: server_destroy_pane(wp);
419: break;
1.53 nicm 420: }
421: }
1.80 nicm 422: }
1.1 nicm 423:
1.66 nicm 424: SLIST_FOREACH(job, &all_jobs, lentry) {
425: if (pid == job->pid) {
1.67 nicm 426: job_died(job, status); /* might free job */
427: break;
1.1 nicm 428: }
1.53 nicm 429: }
430: }
431:
1.66 nicm 432: /* Handle stopped children. */
1.31 nicm 433: void
1.66 nicm 434: server_child_stopped(pid_t pid, int status)
1.31 nicm 435: {
1.66 nicm 436: struct window *w;
437: struct window_pane *wp;
438: u_int i;
1.31 nicm 439:
1.66 nicm 440: if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
441: return;
1.31 nicm 442:
1.66 nicm 443: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
444: if ((w = ARRAY_ITEM(&windows, i)) == NULL)
1.31 nicm 445: continue;
1.66 nicm 446: TAILQ_FOREACH(wp, &w->panes, entry) {
447: if (wp->pid == pid) {
448: if (killpg(pid, SIGCONT) != 0)
449: kill(pid, SIGCONT);
450: }
451: }
1.31 nicm 452: }
1.1 nicm 453: }
454:
1.66 nicm 455: /* Handle once-per-second timer events. */
1.78 nicm 456: /* ARGSUSED */
1.1 nicm 457: void
1.66 nicm 458: server_second_callback(unused int fd, unused short events, unused void *arg)
1.1 nicm 459: {
1.60 nicm 460: struct window *w;
461: struct window_pane *wp;
1.66 nicm 462: struct timeval tv;
1.35 nicm 463: u_int i;
1.1 nicm 464:
1.60 nicm 465: if (options_get_number(&global_s_options, "lock-server"))
466: server_lock_server();
467: else
468: server_lock_sessions();
1.1 nicm 469:
1.60 nicm 470: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
471: w = ARRAY_ITEM(&windows, i);
472: if (w == NULL)
1.1 nicm 473: continue;
474:
1.60 nicm 475: TAILQ_FOREACH(wp, &w->panes, entry) {
476: if (wp->mode != NULL && wp->mode->timer != NULL)
477: wp->mode->timer(wp);
1.1 nicm 478: }
479: }
1.72 nicm 480:
481: server_client_status_timer();
1.66 nicm 482:
483: evtimer_del(&server_ev_second);
484: memset(&tv, 0, sizeof tv);
485: tv.tv_sec = 1;
486: evtimer_add(&server_ev_second, &tv);
1.1 nicm 487: }
488:
1.46 nicm 489: /* Lock the server if ALL sessions have hit the time limit. */
490: void
491: server_lock_server(void)
492: {
493: struct session *s;
494: u_int i;
495: int timeout;
496: time_t t;
497:
498: t = time(NULL);
499: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
500: if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
501: continue;
502:
1.59 nicm 503: if (s->flags & SESSION_UNATTACHED) {
1.65 nicm 504: if (gettimeofday(&s->activity_time, NULL) != 0)
505: fatal("gettimeofday failed");
1.59 nicm 506: continue;
507: }
508:
1.46 nicm 509: timeout = options_get_number(&s->options, "lock-after-time");
1.65 nicm 510: if (timeout <= 0 || t <= s->activity_time.tv_sec + timeout)
1.46 nicm 511: return; /* not timed out */
512: }
513:
514: server_lock();
515: recalculate_sizes();
516: }
517:
518: /* Lock any sessions which have timed out. */
519: void
520: server_lock_sessions(void)
521: {
1.80 nicm 522: struct session *s;
1.62 deraadt 523: u_int i;
1.46 nicm 524: int timeout;
1.62 deraadt 525: time_t t;
1.46 nicm 526:
1.62 deraadt 527: t = time(NULL);
528: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.46 nicm 529: if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
530: continue;
1.59 nicm 531:
532: if (s->flags & SESSION_UNATTACHED) {
1.65 nicm 533: if (gettimeofday(&s->activity_time, NULL) != 0)
534: fatal("gettimeofday failed");
1.59 nicm 535: continue;
536: }
1.46 nicm 537:
538: timeout = options_get_number(&s->options, "lock-after-time");
1.65 nicm 539: if (timeout > 0 && t > s->activity_time.tv_sec + timeout) {
1.46 nicm 540: server_lock_session(s);
541: recalculate_sizes();
1.1 nicm 542: }
543: }
544: }