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