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