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