Annotation of src/usr.bin/tmux/server.c, Revision 1.143
1.143 ! nicm 1: /* $OpenBSD: server.c,v 1.142 2015/10/22 10:48:30 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;
1.141 nicm 47: int server_exit;
1.66 nicm 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);
1.141 nicm 58: int server_should_exit(void);
59: void server_send_exit(void);
1.126 nicm 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.140 guenther 148: if (bind(fd, (struct sockaddr *) &sa, sizeof(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]);
1.143 ! nicm 180:
! 181: if (pledge("stdio rpath wpath cpath fattr unix recvfd proc exec tty "
! 182: "ps", NULL) != 0)
! 183: fatal("pledge failed");
1.1 nicm 184:
185: /*
186: * Must daemonise before loading configuration as the PID changes so
187: * $TMUX would be wrong for sessions created in the config file.
188: */
1.16 nicm 189: if (daemon(1, 0) != 0)
1.1 nicm 190: fatal("daemon failed");
191:
1.88 nicm 192: /* event_init() was called in our parent, need to reinit. */
1.125 nicm 193: clear_signals(0);
1.138 nicm 194: if (event_reinit(base) != 0)
1.88 nicm 195: fatal("event_reinit failed");
196:
1.16 nicm 197: logfile("server");
198: log_debug("server started, pid %ld", (long) getpid());
199:
1.121 nicm 200: RB_INIT(&windows);
1.102 nicm 201: RB_INIT(&all_window_panes);
1.122 nicm 202: TAILQ_INIT(&clients);
1.97 nicm 203: RB_INIT(&sessions);
1.47 nicm 204: TAILQ_INIT(&session_groups);
1.15 nicm 205: mode_key_init_trees();
1.1 nicm 206: key_bindings_init();
207: utf8_build();
208:
209: start_time = time(NULL);
1.16 nicm 210: log_debug("socket path %s", socket_path);
1.96 nicm 211: setproctitle("server (%s)", socket_path);
1.16 nicm 212:
1.66 nicm 213: server_fd = server_create_socket();
1.119 nicm 214: if (server_fd == -1)
215: fatal("couldn't create socket");
216: server_update_socket();
1.60 nicm 217: server_client_create(pair[1]);
1.103 nicm 218:
219: unlink(lockfile);
1.105 nicm 220: free(lockfile);
1.103 nicm 221: close(lockfd);
1.16 nicm 222:
1.139 nicm 223: start_cfg();
224:
1.129 nicm 225: status_prompt_load_history();
1.66 nicm 226:
1.104 nicm 227: server_add_accept(0);
1.66 nicm 228:
1.88 nicm 229: set_signals(server_signal_callback);
1.66 nicm 230: server_loop();
1.129 nicm 231: status_prompt_save_history();
1.66 nicm 232: exit(0);
1.1 nicm 233: }
234:
235: /* Main server loop. */
1.66 nicm 236: void
237: server_loop(void)
1.1 nicm 238: {
1.141 nicm 239: while (!server_should_exit()) {
1.137 nicm 240: log_debug("event dispatch enter");
1.66 nicm 241: event_loop(EVLOOP_ONCE);
1.137 nicm 242: log_debug("event dispatch exit");
1.1 nicm 243:
1.60 nicm 244: server_client_loop();
1.80 nicm 245: }
1.66 nicm 246: }
1.31 nicm 247:
1.112 nicm 248: /* Check if the server should exit (no more clients or sessions). */
1.66 nicm 249: int
1.141 nicm 250: server_should_exit(void)
1.66 nicm 251: {
1.116 nicm 252: struct client *c;
1.1 nicm 253:
1.94 nicm 254: if (!options_get_number(&global_options, "exit-unattached")) {
1.97 nicm 255: if (!RB_EMPTY(&sessions))
256: return (0);
1.1 nicm 257: }
1.116 nicm 258:
1.122 nicm 259: TAILQ_FOREACH(c, &clients, entry) {
260: if (c->session != NULL)
1.116 nicm 261: return (0);
262: }
263:
264: /*
265: * No attached clients therefore want to exit - flush any waiting
266: * clients but don't actually exit until they've gone.
267: */
268: cmd_wait_for_flush();
1.122 nicm 269: if (!TAILQ_EMPTY(&clients))
270: return (0);
1.116 nicm 271:
1.66 nicm 272: return (1);
1.1 nicm 273: }
274:
1.141 nicm 275: /* Exit the server by killing all clients and windows. */
1.1 nicm 276: void
1.141 nicm 277: server_send_exit(void)
1.1 nicm 278: {
1.122 nicm 279: struct client *c, *c1;
280: struct session *s, *s1;
1.116 nicm 281:
282: cmd_wait_for_flush();
1.1 nicm 283:
1.122 nicm 284: TAILQ_FOREACH_SAFE(c, &clients, entry, c1) {
285: if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
286: server_client_lost(c);
287: else
1.142 nicm 288: server_write_client(c, MSG_SHUTDOWN, NULL, 0);
1.122 nicm 289: c->session = NULL;
1.42 nicm 290: }
291:
1.122 nicm 292: RB_FOREACH_SAFE(s, sessions, &sessions, s1)
1.97 nicm 293: session_destroy(s);
1.66 nicm 294: }
295:
296: /* Update socket execute permissions based on whether sessions are attached. */
1.75 nicm 297: void
1.66 nicm 298: server_update_socket(void)
1.42 nicm 299: {
1.66 nicm 300: struct session *s;
301: static int last = -1;
1.93 nicm 302: int n, mode;
303: struct stat sb;
1.1 nicm 304:
1.66 nicm 305: n = 0;
1.97 nicm 306: RB_FOREACH(s, sessions, &sessions) {
307: if (!(s->flags & SESSION_UNATTACHED)) {
1.66 nicm 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. */
333: void
334: server_accept_callback(int fd, short events, unused void *data)
335: {
336: struct sockaddr_storage sa;
337: socklen_t slen = sizeof sa;
338: int newfd;
339:
1.104 nicm 340: server_add_accept(0);
1.66 nicm 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;
1.104 nicm 348: if (errno == ENFILE || errno == EMFILE) {
349: /* Delete and don't try again for 1 second. */
350: server_add_accept(1);
351: return;
352: }
1.66 nicm 353: fatal("accept failed");
354: }
1.141 nicm 355: if (server_exit) {
1.66 nicm 356: close(newfd);
357: return;
1.42 nicm 358: }
1.66 nicm 359: server_client_create(newfd);
360: }
361:
1.104 nicm 362: /*
363: * Add accept event. If timeout is nonzero, add as a timeout instead of a read
364: * event - used to backoff when running out of file descriptors.
365: */
366: void
367: server_add_accept(int timeout)
368: {
369: struct timeval tv = { timeout, 0 };
370:
371: if (event_initialized(&server_ev_accept))
372: event_del(&server_ev_accept);
373:
374: if (timeout == 0) {
375: event_set(&server_ev_accept,
376: server_fd, EV_READ, server_accept_callback, NULL);
377: event_add(&server_ev_accept, NULL);
378: } else {
379: event_set(&server_ev_accept,
380: server_fd, EV_TIMEOUT, server_accept_callback, NULL);
381: event_add(&server_ev_accept, &tv);
382: }
383: }
384:
1.66 nicm 385: /* Signal handler. */
386: void
387: server_signal_callback(int sig, unused short events, unused void *data)
388: {
1.119 nicm 389: int fd;
1.120 nicm 390:
1.66 nicm 391: switch (sig) {
392: case SIGTERM:
1.141 nicm 393: server_exit = 1;
394: server_send_exit();
1.66 nicm 395: break;
396: case SIGCHLD:
397: server_child_signal();
398: break;
399: case SIGUSR1:
400: event_del(&server_ev_accept);
1.119 nicm 401: fd = server_create_socket();
402: if (fd != -1) {
403: close(server_fd);
404: server_fd = fd;
405: server_update_socket();
406: }
1.104 nicm 407: server_add_accept(0);
1.66 nicm 408: break;
1.1 nicm 409: }
410: }
411:
412: /* Handle SIGCHLD. */
413: void
414: server_child_signal(void)
415: {
1.66 nicm 416: int status;
417: pid_t pid;
1.1 nicm 418:
419: for (;;) {
420: switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
421: case -1:
422: if (errno == ECHILD)
423: return;
1.39 nicm 424: fatal("waitpid failed");
1.1 nicm 425: case 0:
426: return;
427: }
1.66 nicm 428: if (WIFSTOPPED(status))
429: server_child_stopped(pid, status);
1.79 nicm 430: else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66 nicm 431: server_child_exited(pid, status);
432: }
433: }
434:
435: /* Handle exited children. */
436: void
437: server_child_exited(pid_t pid, int status)
438: {
1.121 nicm 439: struct window *w, *w1;
1.66 nicm 440: struct window_pane *wp;
441: struct job *job;
442:
1.121 nicm 443: RB_FOREACH_SAFE(w, windows, &windows, w1) {
1.66 nicm 444: TAILQ_FOREACH(wp, &w->panes, entry) {
445: if (wp->pid == pid) {
1.118 nicm 446: wp->status = status;
1.77 nicm 447: server_destroy_pane(wp);
448: break;
1.53 nicm 449: }
450: }
1.80 nicm 451: }
1.1 nicm 452:
1.101 nicm 453: LIST_FOREACH(job, &all_jobs, lentry) {
1.66 nicm 454: if (pid == job->pid) {
1.67 nicm 455: job_died(job, status); /* might free job */
456: break;
1.1 nicm 457: }
1.53 nicm 458: }
459: }
460:
1.66 nicm 461: /* Handle stopped children. */
1.31 nicm 462: void
1.66 nicm 463: server_child_stopped(pid_t pid, int status)
1.31 nicm 464: {
1.66 nicm 465: struct window *w;
466: struct window_pane *wp;
1.31 nicm 467:
1.66 nicm 468: if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
469: return;
1.31 nicm 470:
1.121 nicm 471: RB_FOREACH(w, windows, &windows) {
1.66 nicm 472: TAILQ_FOREACH(wp, &w->panes, entry) {
473: if (wp->pid == pid) {
474: if (killpg(pid, SIGCONT) != 0)
475: kill(pid, SIGCONT);
476: }
1.1 nicm 477: }
478: }
479: }