Annotation of src/usr.bin/tmux/server.c, Revision 1.188
1.188 ! nicm 1: /* $OpenBSD: server.c,v 1.187 2019/06/20 06:51:36 nicm Exp $ */
1.1 nicm 2:
3: /*
1.157 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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:
1.155 nicm 44: struct clients clients;
1.1 nicm 45:
1.155 nicm 46: struct tmuxproc *server_proc;
1.186 nicm 47: static int server_fd = -1;
1.188 ! nicm 48: static int server_client_flags;
1.160 nicm 49: static int server_exit;
50: static struct event server_ev_accept;
1.155 nicm 51:
52: struct cmd_find_state marked_pane;
1.126 nicm 53:
1.160 nicm 54: static int server_loop(void);
55: static void server_send_exit(void);
56: static void server_accept(int, short, void *);
57: static void server_signal(int);
58: static void server_child_signal(void);
59: static void server_child_exited(pid_t, int);
60: static void server_child_stopped(pid_t, int);
1.126 nicm 61:
62: /* Set marked pane. */
63: void
64: server_set_marked(struct session *s, struct winlink *wl, struct window_pane *wp)
65: {
1.169 nicm 66: cmd_find_clear_state(&marked_pane, 0);
1.155 nicm 67: marked_pane.s = s;
68: marked_pane.wl = wl;
69: marked_pane.w = wl->window;
70: marked_pane.wp = wp;
1.126 nicm 71: }
72:
73: /* Clear marked pane. */
74: void
75: server_clear_marked(void)
76: {
1.169 nicm 77: cmd_find_clear_state(&marked_pane, 0);
1.126 nicm 78: }
79:
80: /* Is this the marked pane? */
81: int
82: server_is_marked(struct session *s, struct winlink *wl, struct window_pane *wp)
83: {
84: if (s == NULL || wl == NULL || wp == NULL)
85: return (0);
1.155 nicm 86: if (marked_pane.s != s || marked_pane.wl != wl)
1.126 nicm 87: return (0);
1.155 nicm 88: if (marked_pane.wp != wp)
1.126 nicm 89: return (0);
90: return (server_check_marked());
91: }
92:
93: /* Check if the marked pane is still valid. */
94: int
95: server_check_marked(void)
96: {
1.155 nicm 97: return (cmd_find_valid_state(&marked_pane));
1.126 nicm 98: }
1.45 nicm 99:
1.60 nicm 100: /* Create server socket. */
1.160 nicm 101: static int
1.188 ! nicm 102: server_create_socket(int flags, char **cause)
1.1 nicm 103: {
1.60 nicm 104: struct sockaddr_un sa;
105: size_t size;
106: mode_t mask;
1.178 nicm 107: int fd, saved_errno;
1.60 nicm 108:
109: memset(&sa, 0, sizeof sa);
110: sa.sun_family = AF_UNIX;
111: size = strlcpy(sa.sun_path, socket_path, sizeof sa.sun_path);
112: if (size >= sizeof sa.sun_path) {
113: errno = ENAMETOOLONG;
1.178 nicm 114: goto fail;
1.60 nicm 115: }
116: unlink(sa.sun_path);
117:
118: if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1.178 nicm 119: goto fail;
1.60 nicm 120:
1.188 ! nicm 121: if (flags & CLIENT_DEFAULTSOCKET)
! 122: mask = umask(S_IXUSR|S_IXGRP|S_IRWXO);
! 123: else
! 124: mask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
1.178 nicm 125: if (bind(fd, (struct sockaddr *)&sa, sizeof sa) == -1) {
126: saved_errno = errno;
1.170 nicm 127: close(fd);
1.178 nicm 128: errno = saved_errno;
129: goto fail;
1.170 nicm 130: }
1.60 nicm 131: umask(mask);
132:
1.170 nicm 133: if (listen(fd, 128) == -1) {
1.178 nicm 134: saved_errno = errno;
1.170 nicm 135: close(fd);
1.178 nicm 136: errno = saved_errno;
137: goto fail;
1.170 nicm 138: }
1.100 nicm 139: setblocking(fd, 0);
1.1 nicm 140:
1.60 nicm 141: return (fd);
1.178 nicm 142:
143: fail:
144: if (cause != NULL) {
145: xasprintf(cause, "error creating %s (%s)", socket_path,
146: strerror(errno));
147: }
148: return (-1);
149: }
150:
1.1 nicm 151: /* Fork new server. */
152: int
1.188 ! nicm 153: server_start(struct tmuxproc *client, int flags, struct event_base *base,
! 154: int lockfd, char *lockfile)
1.1 nicm 155: {
1.167 nicm 156: int pair[2];
1.175 nicm 157: sigset_t set, oldset;
1.178 nicm 158: struct client *c;
159: char *cause = NULL;
1.1 nicm 160:
161: if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
162: fatal("socketpair failed");
1.188 ! nicm 163: server_client_flags = flags;
1.1 nicm 164:
1.175 nicm 165: sigfillset(&set);
166: sigprocmask(SIG_BLOCK, &set, &oldset);
1.174 nicm 167: switch (fork()) {
168: case -1:
169: fatal("fork failed");
170: case 0:
171: break;
172: default:
1.175 nicm 173: sigprocmask(SIG_SETMASK, &oldset, NULL);
1.1 nicm 174: close(pair[1]);
175: return (pair[0]);
176: }
177: close(pair[0]);
1.174 nicm 178: if (daemon(1, 0) != 0)
179: fatal("daemon failed");
1.176 nicm 180: proc_clear_signals(client, 0);
1.174 nicm 181: if (event_reinit(base) != 0)
182: fatalx("event_reinit failed");
183: server_proc = proc_start("server");
184: proc_set_signals(server_proc, server_signal);
1.175 nicm 185: sigprocmask(SIG_SETMASK, &oldset, NULL);
1.143 nicm 186:
1.171 nicm 187: if (log_get_level() > 1)
1.146 nicm 188: tty_create_log();
1.151 nicm 189: if (pledge("stdio rpath wpath cpath fattr unix getpw recvfd proc exec "
190: "tty ps", NULL) != 0)
1.143 nicm 191: fatal("pledge failed");
1.1 nicm 192:
1.121 nicm 193: RB_INIT(&windows);
1.102 nicm 194: RB_INIT(&all_window_panes);
1.122 nicm 195: TAILQ_INIT(&clients);
1.97 nicm 196: RB_INIT(&sessions);
1.1 nicm 197: key_bindings_init();
198:
1.153 nicm 199: gettimeofday(&start_time, NULL);
1.16 nicm 200:
1.188 ! nicm 201: server_fd = server_create_socket(flags, &cause);
1.178 nicm 202: if (server_fd != -1)
203: server_update_socket();
204: c = server_client_create(pair[1]);
1.103 nicm 205:
1.154 nicm 206: if (lockfd >= 0) {
207: unlink(lockfile);
208: free(lockfile);
209: close(lockfd);
210: }
1.16 nicm 211:
1.178 nicm 212: if (cause != NULL) {
1.185 nicm 213: cmdq_append(c, cmdq_get_error(cause));
214: free(cause);
1.178 nicm 215: c->flags |= CLIENT_EXIT;
1.187 nicm 216: }
1.178 nicm 217:
1.104 nicm 218: server_add_accept(0);
1.144 nicm 219: proc_loop(server_proc, server_loop);
1.167 nicm 220:
1.183 nicm 221: job_kill_all();
222: status_prompt_save_history();
1.167 nicm 223:
1.66 nicm 224: exit(0);
1.1 nicm 225: }
226:
1.144 nicm 227: /* Server loop callback. */
1.160 nicm 228: static int
1.66 nicm 229: server_loop(void)
1.1 nicm 230: {
1.144 nicm 231: struct client *c;
1.162 nicm 232: u_int items;
233:
234: do {
235: items = cmdq_next(NULL);
1.164 nicm 236: TAILQ_FOREACH(c, &clients, entry) {
237: if (c->flags & CLIENT_IDENTIFIED)
238: items += cmdq_next(c);
239: }
1.162 nicm 240: } while (items != 0);
1.31 nicm 241:
1.144 nicm 242: server_client_loop();
1.179 nicm 243:
244: if (!options_get_number(global_options, "exit-empty") && !server_exit)
245: return (0);
1.1 nicm 246:
1.145 nicm 247: if (!options_get_number(global_options, "exit-unattached")) {
1.97 nicm 248: if (!RB_EMPTY(&sessions))
249: return (0);
1.1 nicm 250: }
1.116 nicm 251:
1.122 nicm 252: TAILQ_FOREACH(c, &clients, entry) {
253: if (c->session != NULL)
1.116 nicm 254: return (0);
255: }
256:
257: /*
258: * No attached clients therefore want to exit - flush any waiting
259: * clients but don't actually exit until they've gone.
260: */
261: cmd_wait_for_flush();
1.122 nicm 262: if (!TAILQ_EMPTY(&clients))
263: return (0);
1.116 nicm 264:
1.183 nicm 265: if (job_still_running())
266: return (0);
1.180 nicm 267:
1.66 nicm 268: return (1);
1.1 nicm 269: }
270:
1.141 nicm 271: /* Exit the server by killing all clients and windows. */
1.160 nicm 272: static void
1.141 nicm 273: server_send_exit(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) {
1.144 nicm 281: if (c->flags & CLIENT_SUSPENDED)
1.122 nicm 282: server_client_lost(c);
1.180 nicm 283: else {
284: if (c->flags & CLIENT_ATTACHED)
285: notify_client("client-detached", c);
1.144 nicm 286: proc_send(c->peer, MSG_SHUTDOWN, -1, NULL, 0);
1.180 nicm 287: }
1.122 nicm 288: c->session = NULL;
1.42 nicm 289: }
290:
1.122 nicm 291: RB_FOREACH_SAFE(s, sessions, &sessions, s1)
1.184 nicm 292: session_destroy(s, 1, __func__);
1.66 nicm 293: }
294:
295: /* Update socket execute permissions based on whether sessions are attached. */
1.75 nicm 296: void
1.66 nicm 297: server_update_socket(void)
1.42 nicm 298: {
1.66 nicm 299: struct session *s;
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.97 nicm 305: RB_FOREACH(s, sessions, &sessions) {
1.182 nicm 306: if (s->attached != 0) {
1.66 nicm 307: n++;
308: break;
309: }
310: }
311:
312: if (n != last) {
313: last = n;
1.93 nicm 314:
315: if (stat(socket_path, &sb) != 0)
316: return;
1.159 semarie 317: mode = sb.st_mode & ACCESSPERMS;
1.93 nicm 318: if (n != 0) {
319: if (mode & S_IRUSR)
320: mode |= S_IXUSR;
321: if (mode & S_IRGRP)
322: mode |= S_IXGRP;
323: if (mode & S_IROTH)
324: mode |= S_IXOTH;
325: } else
326: mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
327: chmod(socket_path, mode);
1.66 nicm 328: }
329: }
330:
331: /* Callback for server socket. */
1.160 nicm 332: static void
1.150 nicm 333: server_accept(int fd, short events, __unused void *data)
1.66 nicm 334: {
335: struct sockaddr_storage sa;
336: socklen_t slen = sizeof sa;
337: int newfd;
338:
1.104 nicm 339: server_add_accept(0);
1.66 nicm 340: if (!(events & EV_READ))
341: return;
342:
343: newfd = accept(fd, (struct sockaddr *) &sa, &slen);
344: if (newfd == -1) {
345: if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
346: return;
1.104 nicm 347: if (errno == ENFILE || errno == EMFILE) {
348: /* Delete and don't try again for 1 second. */
349: server_add_accept(1);
350: return;
351: }
1.66 nicm 352: fatal("accept failed");
353: }
1.141 nicm 354: if (server_exit) {
1.66 nicm 355: close(newfd);
356: return;
1.42 nicm 357: }
1.66 nicm 358: server_client_create(newfd);
359: }
360:
1.104 nicm 361: /*
362: * Add accept event. If timeout is nonzero, add as a timeout instead of a read
363: * event - used to backoff when running out of file descriptors.
364: */
365: void
366: server_add_accept(int timeout)
367: {
368: struct timeval tv = { timeout, 0 };
1.186 nicm 369:
370: if (server_fd == -1)
371: return;
1.104 nicm 372:
373: if (event_initialized(&server_ev_accept))
374: event_del(&server_ev_accept);
375:
376: if (timeout == 0) {
1.144 nicm 377: event_set(&server_ev_accept, server_fd, EV_READ, server_accept,
378: NULL);
1.104 nicm 379: event_add(&server_ev_accept, NULL);
380: } else {
1.144 nicm 381: event_set(&server_ev_accept, server_fd, EV_TIMEOUT,
382: server_accept, NULL);
1.104 nicm 383: event_add(&server_ev_accept, &tv);
384: }
385: }
386:
1.66 nicm 387: /* Signal handler. */
1.160 nicm 388: static void
1.144 nicm 389: server_signal(int sig)
1.66 nicm 390: {
1.119 nicm 391: int fd;
1.120 nicm 392:
1.173 nicm 393: log_debug("%s: %s", __func__, strsignal(sig));
1.66 nicm 394: switch (sig) {
395: case SIGTERM:
1.141 nicm 396: server_exit = 1;
397: server_send_exit();
1.66 nicm 398: break;
399: case SIGCHLD:
400: server_child_signal();
401: break;
402: case SIGUSR1:
403: event_del(&server_ev_accept);
1.188 ! nicm 404: fd = server_create_socket(server_client_flags, NULL);
1.119 nicm 405: if (fd != -1) {
406: close(server_fd);
407: server_fd = fd;
408: server_update_socket();
409: }
1.104 nicm 410: server_add_accept(0);
1.171 nicm 411: break;
412: case SIGUSR2:
413: proc_toggle_log(server_proc);
1.66 nicm 414: break;
1.1 nicm 415: }
416: }
417:
418: /* Handle SIGCHLD. */
1.160 nicm 419: static void
1.1 nicm 420: server_child_signal(void)
421: {
1.66 nicm 422: int status;
423: pid_t pid;
1.1 nicm 424:
425: for (;;) {
426: switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
427: case -1:
428: if (errno == ECHILD)
429: return;
1.39 nicm 430: fatal("waitpid failed");
1.1 nicm 431: case 0:
432: return;
433: }
1.66 nicm 434: if (WIFSTOPPED(status))
435: server_child_stopped(pid, status);
1.79 nicm 436: else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66 nicm 437: server_child_exited(pid, status);
438: }
439: }
440:
441: /* Handle exited children. */
1.160 nicm 442: static void
1.66 nicm 443: server_child_exited(pid_t pid, int status)
444: {
1.121 nicm 445: struct window *w, *w1;
1.66 nicm 446: struct window_pane *wp;
447:
1.121 nicm 448: RB_FOREACH_SAFE(w, windows, &windows, w1) {
1.66 nicm 449: TAILQ_FOREACH(wp, &w->panes, entry) {
450: if (wp->pid == pid) {
1.118 nicm 451: wp->status = status;
1.177 nicm 452: wp->flags |= PANE_STATUSREADY;
1.172 nicm 453:
454: log_debug("%%%u exited", wp->id);
455: wp->flags |= PANE_EXITED;
456:
457: if (window_pane_destroy_ready(wp))
458: server_destroy_pane(wp, 1);
1.77 nicm 459: break;
1.53 nicm 460: }
461: }
1.80 nicm 462: }
1.183 nicm 463: job_check_died(pid, status);
1.53 nicm 464: }
465:
1.66 nicm 466: /* Handle stopped children. */
1.160 nicm 467: static void
1.66 nicm 468: server_child_stopped(pid_t pid, int status)
1.31 nicm 469: {
1.66 nicm 470: struct window *w;
471: struct window_pane *wp;
1.31 nicm 472:
1.66 nicm 473: if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
474: return;
1.31 nicm 475:
1.121 nicm 476: RB_FOREACH(w, windows, &windows) {
1.66 nicm 477: TAILQ_FOREACH(wp, &w->panes, entry) {
478: if (wp->pid == pid) {
479: if (killpg(pid, SIGCONT) != 0)
480: kill(pid, SIGCONT);
481: }
1.1 nicm 482: }
483: }
484: }