Annotation of src/usr.bin/tmux/server.c, Revision 1.134
1.134 ! nicm 1: /* $OpenBSD: server.c,v 1.133 2015/08/28 12:31:55 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 <termios.h>
35: #include <time.h>
36: #include <unistd.h>
37:
38: #include "tmux.h"
39:
40: /*
41: * Main server functions.
42: */
43:
44: struct clients clients;
45:
1.66 nicm 46: int server_fd;
47: int server_shutdown;
48: struct event server_ev_accept;
1.45 nicm 49:
1.126 nicm 50: struct session *marked_session;
51: struct winlink *marked_winlink;
52: struct window *marked_window;
53: struct window_pane *marked_window_pane;
54: struct layout_cell *marked_layout_cell;
55:
56: int server_create_socket(void);
57: void server_loop(void);
58: int server_should_shutdown(void);
59: void server_send_shutdown(void);
60: void server_accept_callback(int, short, void *);
61: void server_signal_callback(int, short, void *);
62: void server_child_signal(void);
63: void server_child_exited(pid_t, int);
64: void server_child_stopped(pid_t, int);
65: void server_second_callback(int, short, void *);
66: void server_lock_sessions(void);
67:
68: /* Set marked pane. */
69: void
70: server_set_marked(struct session *s, struct winlink *wl, struct window_pane *wp)
71: {
72: marked_session = s;
73: marked_winlink = wl;
74: marked_window = wl->window;
75: marked_window_pane = wp;
76: marked_layout_cell = wp->layout_cell;
77: }
78:
79: /* Clear marked pane. */
80: void
81: server_clear_marked(void)
82: {
83: marked_session = NULL;
84: marked_winlink = NULL;
85: marked_window = NULL;
86: marked_window_pane = NULL;
87: marked_layout_cell = NULL;
88: }
89:
90: /* Is this the marked pane? */
91: int
92: server_is_marked(struct session *s, struct winlink *wl, struct window_pane *wp)
93: {
94: if (s == NULL || wl == NULL || wp == NULL)
95: return (0);
96: if (marked_session != s || marked_winlink != wl)
97: return (0);
98: if (marked_window_pane != wp)
99: return (0);
100: return (server_check_marked());
101: }
102:
103: /* Check if the marked pane is still valid. */
104: int
105: server_check_marked(void)
106: {
107: struct winlink *wl;
108:
109: if (marked_window_pane == NULL)
110: return (0);
111: if (marked_layout_cell != marked_window_pane->layout_cell)
112: return (0);
113:
114: if (!session_alive(marked_session))
115: return (0);
116: RB_FOREACH(wl, winlinks, &marked_session->windows) {
117: if (wl->window == marked_window && wl == marked_winlink)
118: break;
119: }
120: if (wl == NULL)
121: return (0);
122:
123: if (!window_has_pane(marked_window, marked_window_pane))
124: return (0);
125: return (window_pane_visible(marked_window_pane));
126: }
1.45 nicm 127:
1.60 nicm 128: /* Create server socket. */
129: int
130: server_create_socket(void)
1.1 nicm 131: {
1.60 nicm 132: struct sockaddr_un sa;
133: size_t size;
134: mode_t mask;
1.100 nicm 135: int fd;
1.60 nicm 136:
137: memset(&sa, 0, sizeof sa);
138: sa.sun_family = AF_UNIX;
139: size = strlcpy(sa.sun_path, socket_path, sizeof sa.sun_path);
140: if (size >= sizeof sa.sun_path) {
141: errno = ENAMETOOLONG;
1.119 nicm 142: return (-1);
1.60 nicm 143: }
144: unlink(sa.sun_path);
145:
146: if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1.119 nicm 147: return (-1);
1.60 nicm 148:
1.89 nicm 149: mask = umask(S_IXUSR|S_IXGRP|S_IRWXO);
1.60 nicm 150: if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1)
1.119 nicm 151: return (-1);
1.60 nicm 152: umask(mask);
153:
154: if (listen(fd, 16) == -1)
1.119 nicm 155: return (-1);
1.100 nicm 156: setblocking(fd, 0);
1.1 nicm 157:
1.60 nicm 158: return (fd);
159: }
160:
1.1 nicm 161: /* Fork new server. */
162: int
1.103 nicm 163: server_start(int lockfd, char *lockfile)
1.1 nicm 164: {
1.134 ! nicm 165: int pair[2];
! 166: char *cause;
1.1 nicm 167:
168: /* The first client is special and gets a socketpair; create it. */
169: if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
170: fatal("socketpair failed");
1.115 nicm 171: log_debug("starting server");
1.1 nicm 172:
173: switch (fork()) {
174: case -1:
175: fatal("fork failed");
176: case 0:
177: break;
178: default:
179: close(pair[1]);
180: return (pair[0]);
181: }
182: close(pair[0]);
183:
184: /*
185: * Must daemonise before loading configuration as the PID changes so
186: * $TMUX would be wrong for sessions created in the config file.
187: */
1.16 nicm 188: if (daemon(1, 0) != 0)
1.1 nicm 189: fatal("daemon failed");
190:
1.88 nicm 191: /* event_init() was called in our parent, need to reinit. */
1.125 nicm 192: clear_signals(0);
1.88 nicm 193: if (event_reinit(ev_base) != 0)
194: fatal("event_reinit failed");
195:
1.16 nicm 196: logfile("server");
197: log_debug("server started, pid %ld", (long) getpid());
198:
1.121 nicm 199: RB_INIT(&windows);
1.102 nicm 200: RB_INIT(&all_window_panes);
1.122 nicm 201: TAILQ_INIT(&clients);
1.97 nicm 202: RB_INIT(&sessions);
1.47 nicm 203: TAILQ_INIT(&session_groups);
1.15 nicm 204: mode_key_init_trees();
1.1 nicm 205: key_bindings_init();
206: utf8_build();
207:
208: start_time = time(NULL);
1.16 nicm 209: log_debug("socket path %s", socket_path);
1.96 nicm 210: setproctitle("server (%s)", socket_path);
1.16 nicm 211:
1.66 nicm 212: server_fd = server_create_socket();
1.119 nicm 213: if (server_fd == -1)
214: fatal("couldn't create socket");
215: server_update_socket();
1.60 nicm 216: server_client_create(pair[1]);
1.103 nicm 217:
218: unlink(lockfile);
1.105 nicm 219: free(lockfile);
1.103 nicm 220: close(lockfd);
1.16 nicm 221:
1.109 nicm 222: cfg_cmd_q = cmdq_new(NULL);
223: cfg_cmd_q->emptyfn = cfg_default_done;
224: cfg_finished = 0;
225: cfg_references = 1;
1.122 nicm 226: cfg_client = TAILQ_FIRST(&clients);
1.111 nicm 227: if (cfg_client != NULL)
228: cfg_client->references++;
1.109 nicm 229:
1.110 nicm 230: if (access(TMUX_CONF, R_OK) == 0) {
1.117 nicm 231: if (load_cfg(TMUX_CONF, cfg_cmd_q, &cause) == -1)
232: cfg_add_cause("%s: %s", TMUX_CONF, cause);
233: } else if (errno != ENOENT)
234: cfg_add_cause("%s: %s", TMUX_CONF, strerror(errno));
1.109 nicm 235: if (cfg_file != NULL) {
1.117 nicm 236: if (load_cfg(cfg_file, cfg_cmd_q, &cause) == -1)
237: cfg_add_cause("%s: %s", cfg_file, cause);
1.109 nicm 238: }
239: cmdq_continue(cfg_cmd_q);
1.129 nicm 240: status_prompt_load_history();
1.66 nicm 241:
1.104 nicm 242: server_add_accept(0);
1.66 nicm 243:
1.88 nicm 244: set_signals(server_signal_callback);
1.66 nicm 245: server_loop();
1.129 nicm 246: status_prompt_save_history();
1.66 nicm 247: exit(0);
1.1 nicm 248: }
249:
250: /* Main server loop. */
1.66 nicm 251: void
252: server_loop(void)
1.1 nicm 253: {
1.66 nicm 254: while (!server_should_shutdown()) {
255: event_loop(EVLOOP_ONCE);
1.1 nicm 256:
1.60 nicm 257: server_window_loop();
258: server_client_loop();
1.80 nicm 259: }
1.66 nicm 260: }
1.31 nicm 261:
1.112 nicm 262: /* Check if the server should exit (no more clients or sessions). */
1.66 nicm 263: int
264: server_should_shutdown(void)
265: {
1.116 nicm 266: struct client *c;
1.1 nicm 267:
1.94 nicm 268: if (!options_get_number(&global_options, "exit-unattached")) {
1.97 nicm 269: if (!RB_EMPTY(&sessions))
270: return (0);
1.1 nicm 271: }
1.116 nicm 272:
1.122 nicm 273: TAILQ_FOREACH(c, &clients, entry) {
274: if (c->session != NULL)
1.116 nicm 275: return (0);
276: }
277:
278: /*
279: * No attached clients therefore want to exit - flush any waiting
280: * clients but don't actually exit until they've gone.
281: */
282: cmd_wait_for_flush();
1.122 nicm 283: if (!TAILQ_EMPTY(&clients))
284: return (0);
1.116 nicm 285:
1.66 nicm 286: return (1);
1.1 nicm 287: }
288:
1.66 nicm 289: /* Shutdown the server by killing all clients and windows. */
1.1 nicm 290: void
1.66 nicm 291: server_send_shutdown(void)
1.1 nicm 292: {
1.122 nicm 293: struct client *c, *c1;
294: struct session *s, *s1;
1.116 nicm 295:
296: cmd_wait_for_flush();
1.1 nicm 297:
1.122 nicm 298: TAILQ_FOREACH_SAFE(c, &clients, entry, c1) {
299: if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
300: server_client_lost(c);
301: else
302: server_write_client(c, MSG_SHUTDOWN, NULL, 0);
303: c->session = NULL;
1.42 nicm 304: }
305:
1.122 nicm 306: RB_FOREACH_SAFE(s, sessions, &sessions, s1)
1.97 nicm 307: session_destroy(s);
1.66 nicm 308: }
309:
310: /* Update socket execute permissions based on whether sessions are attached. */
1.75 nicm 311: void
1.66 nicm 312: server_update_socket(void)
1.42 nicm 313: {
1.66 nicm 314: struct session *s;
315: static int last = -1;
1.93 nicm 316: int n, mode;
317: struct stat sb;
1.1 nicm 318:
1.66 nicm 319: n = 0;
1.97 nicm 320: RB_FOREACH(s, sessions, &sessions) {
321: if (!(s->flags & SESSION_UNATTACHED)) {
1.66 nicm 322: n++;
323: break;
324: }
325: }
326:
327: if (n != last) {
328: last = n;
1.93 nicm 329:
330: if (stat(socket_path, &sb) != 0)
331: return;
332: mode = sb.st_mode;
333: if (n != 0) {
334: if (mode & S_IRUSR)
335: mode |= S_IXUSR;
336: if (mode & S_IRGRP)
337: mode |= S_IXGRP;
338: if (mode & S_IROTH)
339: mode |= S_IXOTH;
340: } else
341: mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
342: chmod(socket_path, mode);
1.66 nicm 343: }
344: }
345:
346: /* Callback for server socket. */
347: void
348: server_accept_callback(int fd, short events, unused void *data)
349: {
350: struct sockaddr_storage sa;
351: socklen_t slen = sizeof sa;
352: int newfd;
353:
1.104 nicm 354: server_add_accept(0);
1.66 nicm 355: if (!(events & EV_READ))
356: return;
357:
358: newfd = accept(fd, (struct sockaddr *) &sa, &slen);
359: if (newfd == -1) {
360: if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
361: return;
1.104 nicm 362: if (errno == ENFILE || errno == EMFILE) {
363: /* Delete and don't try again for 1 second. */
364: server_add_accept(1);
365: return;
366: }
1.66 nicm 367: fatal("accept failed");
368: }
369: if (server_shutdown) {
370: close(newfd);
371: return;
1.42 nicm 372: }
1.66 nicm 373: server_client_create(newfd);
374: }
375:
1.104 nicm 376: /*
377: * Add accept event. If timeout is nonzero, add as a timeout instead of a read
378: * event - used to backoff when running out of file descriptors.
379: */
380: void
381: server_add_accept(int timeout)
382: {
383: struct timeval tv = { timeout, 0 };
384:
385: if (event_initialized(&server_ev_accept))
386: event_del(&server_ev_accept);
387:
388: if (timeout == 0) {
389: event_set(&server_ev_accept,
390: server_fd, EV_READ, server_accept_callback, NULL);
391: event_add(&server_ev_accept, NULL);
392: } else {
393: event_set(&server_ev_accept,
394: server_fd, EV_TIMEOUT, server_accept_callback, NULL);
395: event_add(&server_ev_accept, &tv);
396: }
397: }
398:
1.66 nicm 399: /* Signal handler. */
400: void
401: server_signal_callback(int sig, unused short events, unused void *data)
402: {
1.119 nicm 403: int fd;
1.120 nicm 404:
1.66 nicm 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);
1.119 nicm 415: fd = server_create_socket();
416: if (fd != -1) {
417: close(server_fd);
418: server_fd = fd;
419: server_update_socket();
420: }
1.104 nicm 421: server_add_accept(0);
1.66 nicm 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);
1.79 nicm 444: else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66 nicm 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: {
1.121 nicm 453: struct window *w, *w1;
1.66 nicm 454: struct window_pane *wp;
455: struct job *job;
456:
1.121 nicm 457: RB_FOREACH_SAFE(w, windows, &windows, w1) {
1.66 nicm 458: TAILQ_FOREACH(wp, &w->panes, entry) {
459: if (wp->pid == pid) {
1.118 nicm 460: wp->status = status;
1.77 nicm 461: server_destroy_pane(wp);
462: break;
1.53 nicm 463: }
464: }
1.80 nicm 465: }
1.1 nicm 466:
1.101 nicm 467: LIST_FOREACH(job, &all_jobs, lentry) {
1.66 nicm 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;
1.31 nicm 481:
1.66 nicm 482: if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
483: return;
1.31 nicm 484:
1.121 nicm 485: RB_FOREACH(w, windows, &windows) {
1.66 nicm 486: TAILQ_FOREACH(wp, &w->panes, entry) {
487: if (wp->pid == pid) {
488: if (killpg(pid, SIGCONT) != 0)
489: kill(pid, SIGCONT);
490: }
1.1 nicm 491: }
492: }
493: }