Annotation of src/usr.bin/tmux/client.c, Revision 1.161
1.161 ! nicm 1: /* $OpenBSD: client.c,v 1.160 2023/07/10 09:35:46 nicm Exp $ */
1.1 nicm 2:
3: /*
1.113 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/socket.h>
1.144 nicm 21: #include <sys/uio.h>
1.1 nicm 22: #include <sys/un.h>
23: #include <sys/wait.h>
24:
25: #include <errno.h>
1.30 nicm 26: #include <event.h>
1.80 nicm 27: #include <fcntl.h>
1.99 nicm 28: #include <imsg.h>
1.85 nicm 29: #include <signal.h>
1.1 nicm 30: #include <stdlib.h>
31: #include <string.h>
32: #include <unistd.h>
33:
34: #include "tmux.h"
35:
1.114 nicm 36: static struct tmuxproc *client_proc;
37: static struct tmuxpeer *client_peer;
1.148 nicm 38: static uint64_t client_flags;
1.150 nicm 39: static int client_suspended;
1.114 nicm 40: static enum {
1.52 nicm 41: CLIENT_EXIT_NONE,
42: CLIENT_EXIT_DETACHED,
43: CLIENT_EXIT_DETACHED_HUP,
44: CLIENT_EXIT_LOST_TTY,
45: CLIENT_EXIT_TERMINATED,
46: CLIENT_EXIT_LOST_SERVER,
47: CLIENT_EXIT_EXITED,
48: CLIENT_EXIT_SERVER_EXITED,
1.147 nicm 49: CLIENT_EXIT_MESSAGE_PROVIDED
1.52 nicm 50: } client_exitreason = CLIENT_EXIT_NONE;
1.133 nicm 51: static int client_exitflag;
1.114 nicm 52: static int client_exitval;
53: static enum msgtype client_exittype;
54: static const char *client_exitsession;
1.147 nicm 55: static char *client_exitmessage;
1.115 nicm 56: static const char *client_execshell;
57: static const char *client_execcmd;
1.114 nicm 58: static int client_attached;
1.132 nicm 59: static struct client_files client_files = RB_INITIALIZER(&client_files);
1.114 nicm 60:
61: static __dead void client_exec(const char *,const char *);
62: static int client_get_lock(char *);
1.149 nicm 63: static int client_connect(struct event_base *, const char *,
64: uint64_t);
1.155 nicm 65: static void client_send_identify(const char *, const char *,
66: char **, u_int, const char *, int);
1.114 nicm 67: static void client_signal(int);
68: static void client_dispatch(struct imsg *, void *);
69: static void client_dispatch_attached(struct imsg *);
1.121 nicm 70: static void client_dispatch_wait(struct imsg *);
1.114 nicm 71: static const char *client_exit_message(void);
1.25 nicm 72:
1.49 nicm 73: /*
74: * Get server create lock. If already held then server start is happening in
1.110 nicm 75: * another client, so block until the lock is released and return -2 to
76: * retry. Return -1 on failure to continue and start the server anyway.
1.49 nicm 77: */
1.114 nicm 78: static int
1.49 nicm 79: client_get_lock(char *lockfile)
80: {
81: int lockfd;
82:
1.110 nicm 83: log_debug("lock file is %s", lockfile);
84:
1.109 nicm 85: if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
1.110 nicm 86: log_debug("open failed: %s", strerror(errno));
87: return (-1);
1.109 nicm 88: }
1.49 nicm 89:
1.82 nicm 90: if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
91: log_debug("flock failed: %s", strerror(errno));
92: if (errno != EAGAIN)
93: return (lockfd);
94: while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
1.49 nicm 95: /* nothing */;
96: close(lockfd);
1.110 nicm 97: return (-2);
1.49 nicm 98: }
1.82 nicm 99: log_debug("flock succeeded");
1.49 nicm 100:
101: return (lockfd);
102: }
103:
1.46 nicm 104: /* Connect client to server. */
1.114 nicm 105: static int
1.149 nicm 106: client_connect(struct event_base *base, const char *path, uint64_t flags)
1.1 nicm 107: {
1.26 nicm 108: struct sockaddr_un sa;
109: size_t size;
1.82 nicm 110: int fd, lockfd = -1, locked = 0;
111: char *lockfile = NULL;
1.1 nicm 112:
113: memset(&sa, 0, sizeof sa);
114: sa.sun_family = AF_UNIX;
115: size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
116: if (size >= sizeof sa.sun_path) {
117: errno = ENAMETOOLONG;
1.46 nicm 118: return (-1);
1.1 nicm 119: }
1.82 nicm 120: log_debug("socket is %s", path);
1.1 nicm 121:
1.49 nicm 122: retry:
1.10 nicm 123: if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1.109 nicm 124: return (-1);
1.1 nicm 125:
1.82 nicm 126: log_debug("trying connect");
1.109 nicm 127: if (connect(fd, (struct sockaddr *)&sa, sizeof sa) == -1) {
1.82 nicm 128: log_debug("connect failed: %s", strerror(errno));
1.49 nicm 129: if (errno != ECONNREFUSED && errno != ENOENT)
1.151 nicm 130: goto failed;
131: if (flags & CLIENT_NOSTARTSERVER)
1.49 nicm 132: goto failed;
1.138 nicm 133: if (~flags & CLIENT_STARTSERVER)
1.46 nicm 134: goto failed;
1.49 nicm 135: close(fd);
136:
1.82 nicm 137: if (!locked) {
138: xasprintf(&lockfile, "%s.lock", path);
1.110 nicm 139: if ((lockfd = client_get_lock(lockfile)) < 0) {
140: log_debug("didn't get lock (%d)", lockfd);
141:
1.82 nicm 142: free(lockfile);
1.110 nicm 143: lockfile = NULL;
144:
145: if (lockfd == -2)
146: goto retry;
1.82 nicm 147: }
1.110 nicm 148: log_debug("got lock (%d)", lockfd);
1.82 nicm 149:
150: /*
151: * Always retry at least once, even if we got the lock,
152: * because another client could have taken the lock,
153: * started the server and released the lock between our
154: * connect() and flock().
155: */
156: locked = 1;
1.49 nicm 157: goto retry;
1.78 nicm 158: }
1.82 nicm 159:
1.110 nicm 160: if (lockfd >= 0 && unlink(path) != 0 && errno != ENOENT) {
1.78 nicm 161: free(lockfile);
162: close(lockfd);
1.49 nicm 163: return (-1);
1.78 nicm 164: }
1.138 nicm 165: fd = server_start(client_proc, flags, base, lockfd, lockfile);
1.82 nicm 166: }
1.95 nicm 167:
1.110 nicm 168: if (locked && lockfd >= 0) {
1.58 nicm 169: free(lockfile);
1.49 nicm 170: close(lockfd);
1.1 nicm 171: }
1.47 nicm 172: setblocking(fd, 0);
1.46 nicm 173: return (fd);
174:
175: failed:
1.95 nicm 176: if (locked) {
177: free(lockfile);
178: close(lockfd);
179: }
1.46 nicm 180: close(fd);
181: return (-1);
182: }
183:
1.52 nicm 184: /* Get exit string from reason number. */
185: const char *
186: client_exit_message(void)
187: {
1.73 nicm 188: static char msg[256];
189:
1.52 nicm 190: switch (client_exitreason) {
191: case CLIENT_EXIT_NONE:
192: break;
193: case CLIENT_EXIT_DETACHED:
1.73 nicm 194: if (client_exitsession != NULL) {
195: xsnprintf(msg, sizeof msg, "detached "
196: "(from session %s)", client_exitsession);
197: return (msg);
198: }
1.52 nicm 199: return ("detached");
200: case CLIENT_EXIT_DETACHED_HUP:
1.73 nicm 201: if (client_exitsession != NULL) {
202: xsnprintf(msg, sizeof msg, "detached and SIGHUP "
203: "(from session %s)", client_exitsession);
204: return (msg);
205: }
1.52 nicm 206: return ("detached and SIGHUP");
207: case CLIENT_EXIT_LOST_TTY:
208: return ("lost tty");
209: case CLIENT_EXIT_TERMINATED:
210: return ("terminated");
211: case CLIENT_EXIT_LOST_SERVER:
1.131 nicm 212: return ("server exited unexpectedly");
1.52 nicm 213: case CLIENT_EXIT_EXITED:
214: return ("exited");
215: case CLIENT_EXIT_SERVER_EXITED:
216: return ("server exited");
1.147 nicm 217: case CLIENT_EXIT_MESSAGE_PROVIDED:
218: return (client_exitmessage);
1.52 nicm 219: }
220: return ("unknown reason");
221: }
222:
1.133 nicm 223: /* Exit if all streams flushed. */
224: static void
225: client_exit(void)
226: {
1.153 nicm 227: if (!file_write_left(&client_files))
1.133 nicm 228: proc_exit(client_proc);
229: }
230:
1.46 nicm 231: /* Client main loop. */
232: int
1.149 nicm 233: client_main(struct event_base *base, int argc, char **argv, uint64_t flags,
234: int feat)
1.46 nicm 235: {
1.129 nicm 236: struct cmd_parse_result *pr;
1.132 nicm 237: struct msg_command *data;
1.138 nicm 238: int fd, i;
1.155 nicm 239: const char *ttynam, *termname, *cwd;
1.48 nicm 240: pid_t ppid;
1.46 nicm 241: enum msgtype msg;
1.56 nicm 242: struct termios tio, saved_tio;
1.148 nicm 243: size_t size, linesize = 0;
244: ssize_t linelen;
1.155 nicm 245: char *line = NULL, **caps = NULL, *cause;
246: u_int ncaps = 0;
1.156 nicm 247: struct args_value *values;
1.99 nicm 248:
1.46 nicm 249: /* Set up the initial command. */
1.121 nicm 250: if (shell_command != NULL) {
1.46 nicm 251: msg = MSG_SHELL;
1.139 nicm 252: flags |= CLIENT_STARTSERVER;
1.46 nicm 253: } else if (argc == 0) {
254: msg = MSG_COMMAND;
1.138 nicm 255: flags |= CLIENT_STARTSERVER;
1.46 nicm 256: } else {
257: msg = MSG_COMMAND;
258:
259: /*
1.156 nicm 260: * It's annoying parsing the command string twice (in client
261: * and later in server) but it is necessary to get the start
262: * server flag.
1.46 nicm 263: */
1.156 nicm 264: values = args_from_vector(argc, argv);
265: pr = cmd_parse_from_arguments(values, argc, NULL);
1.129 nicm 266: if (pr->status == CMD_PARSE_SUCCESS) {
1.141 nicm 267: if (cmd_list_any_have(pr->cmdlist, CMD_STARTSERVER))
268: flags |= CLIENT_STARTSERVER;
1.129 nicm 269: cmd_list_free(pr->cmdlist);
270: } else
271: free(pr->error);
1.156 nicm 272: args_free_values(values, argc);
273: free(values);
1.46 nicm 274: }
275:
1.109 nicm 276: /* Create client process structure (starts logging). */
1.122 nicm 277: client_proc = proc_start("client");
278: proc_set_signals(client_proc, client_signal);
1.109 nicm 279:
1.148 nicm 280: /* Save the flags. */
281: client_flags = flags;
1.149 nicm 282: log_debug("flags are %#llx", (unsigned long long)client_flags);
1.148 nicm 283:
1.82 nicm 284: /* Initialize the client socket and start the server. */
1.138 nicm 285: fd = client_connect(base, socket_path, client_flags);
1.46 nicm 286: if (fd == -1) {
1.87 nicm 287: if (errno == ECONNREFUSED) {
288: fprintf(stderr, "no server running on %s\n",
289: socket_path);
290: } else {
291: fprintf(stderr, "error connecting to %s (%s)\n",
292: socket_path, strerror(errno));
293: }
1.46 nicm 294: return (1);
295: }
1.121 nicm 296: client_peer = proc_add_peer(client_proc, fd, client_dispatch, NULL);
1.99 nicm 297:
1.97 nicm 298: /* Save these before pledge(). */
1.128 nicm 299: if ((cwd = find_cwd()) == NULL && (cwd = find_home()) == NULL)
1.126 nicm 300: cwd = "/";
1.97 nicm 301: if ((ttynam = ttyname(STDIN_FILENO)) == NULL)
302: ttynam = "";
1.155 nicm 303: if ((termname = getenv("TERM")) == NULL)
304: termname = "";
1.97 nicm 305:
306: /*
307: * Drop privileges for client. "proc exec" is needed for -c and for
308: * locking (which uses system(3)).
309: *
310: * "tty" is needed to restore termios(4) and also for some reason -CC
311: * does not work properly without it (input is not recognised).
312: *
313: * "sendfd" is dropped later in client_dispatch_wait().
314: */
1.132 nicm 315: if (pledge(
316: "stdio rpath wpath cpath unix sendfd proc exec tty",
317: NULL) != 0)
1.97 nicm 318: fatal("pledge failed");
319:
1.155 nicm 320: /* Load terminfo entry if any. */
321: if (isatty(STDIN_FILENO) &&
322: *termname != '\0' &&
323: tty_term_read_list(termname, STDIN_FILENO, &caps, &ncaps,
324: &cause) != 0) {
325: fprintf(stderr, "%s\n", cause);
326: free(cause);
327: return (1);
328: }
329:
1.97 nicm 330: /* Free stuff that is not used in the client. */
1.119 nicm 331: if (ptm_fd != -1)
332: close(ptm_fd);
1.100 nicm 333: options_free(global_options);
334: options_free(global_s_options);
335: options_free(global_w_options);
1.101 nicm 336: environ_free(global_environ);
1.46 nicm 337:
1.132 nicm 338: /* Set up control mode. */
1.93 nicm 339: if (client_flags & CLIENT_CONTROLCONTROL) {
1.118 nicm 340: if (tcgetattr(STDIN_FILENO, &saved_tio) != 0) {
341: fprintf(stderr, "tcgetattr failed: %s\n",
342: strerror(errno));
343: return (1);
344: }
1.56 nicm 345: cfmakeraw(&tio);
346: tio.c_iflag = ICRNL|IXANY;
347: tio.c_oflag = OPOST|ONLCR;
348: tio.c_lflag = NOKERNINFO;
349: tio.c_cflag = CREAD|CS8|HUPCL;
350: tio.c_cc[VMIN] = 1;
351: tio.c_cc[VTIME] = 0;
352: cfsetispeed(&tio, cfgetispeed(&saved_tio));
353: cfsetospeed(&tio, cfgetospeed(&saved_tio));
354: tcsetattr(STDIN_FILENO, TCSANOW, &tio);
355: }
1.1 nicm 356:
1.71 nicm 357: /* Send identify messages. */
1.155 nicm 358: client_send_identify(ttynam, termname, caps, ncaps, cwd, feat);
359: tty_term_free_list(caps, ncaps);
1.158 nicm 360: proc_flush_peer(client_peer);
1.1 nicm 361:
1.46 nicm 362: /* Send first command. */
363: if (msg == MSG_COMMAND) {
1.70 nicm 364: /* How big is the command? */
365: size = 0;
366: for (i = 0; i < argc; i++)
367: size += strlen(argv[i]) + 1;
1.124 nicm 368: if (size > MAX_IMSGSIZE - (sizeof *data)) {
369: fprintf(stderr, "command too long\n");
370: return (1);
371: }
1.70 nicm 372: data = xmalloc((sizeof *data) + size);
1.46 nicm 373:
374: /* Prepare command for server. */
1.70 nicm 375: data->argc = argc;
1.83 nicm 376: if (cmd_pack_argv(argc, argv, (char *)(data + 1), size) != 0) {
1.55 nicm 377: fprintf(stderr, "command too long\n");
1.70 nicm 378: free(data);
1.46 nicm 379: return (1);
380: }
1.70 nicm 381: size += sizeof *data;
1.46 nicm 382:
1.70 nicm 383: /* Send the command. */
1.99 nicm 384: if (proc_send(client_peer, msg, -1, data, size) != 0) {
1.70 nicm 385: fprintf(stderr, "failed to send command\n");
386: free(data);
387: return (1);
388: }
389: free(data);
1.46 nicm 390: } else if (msg == MSG_SHELL)
1.99 nicm 391: proc_send(client_peer, msg, -1, NULL, 0);
1.1 nicm 392:
1.99 nicm 393: /* Start main loop. */
394: proc_loop(client_proc, NULL);
1.46 nicm 395:
1.115 nicm 396: /* Run command if user requested exec, instead of exiting. */
397: if (client_exittype == MSG_EXEC) {
398: if (client_flags & CLIENT_CONTROLCONTROL)
399: tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
400: client_exec(client_execshell, client_execcmd);
401: }
402:
1.146 nicm 403: /* Restore streams to blocking. */
404: setblocking(STDIN_FILENO, 1);
405: setblocking(STDOUT_FILENO, 1);
406: setblocking(STDERR_FILENO, 1);
407:
1.46 nicm 408: /* Print the exit message, if any, and exit. */
1.48 nicm 409: if (client_attached) {
1.93 nicm 410: if (client_exitreason != CLIENT_EXIT_NONE)
1.52 nicm 411: printf("[%s]\n", client_exit_message());
1.48 nicm 412:
413: ppid = getppid();
414: if (client_exittype == MSG_DETACHKILL && ppid > 1)
415: kill(ppid, SIGHUP);
1.146 nicm 416: } else if (client_flags & CLIENT_CONTROL) {
1.68 nicm 417: if (client_exitreason != CLIENT_EXIT_NONE)
418: printf("%%exit %s\n", client_exit_message());
419: else
420: printf("%%exit\n");
1.148 nicm 421: fflush(stdout);
422: if (client_flags & CLIENT_CONTROL_WAITEXIT) {
423: setvbuf(stdin, NULL, _IOLBF, 0);
424: for (;;) {
425: linelen = getline(&line, &linesize, stdin);
426: if (linelen <= 1)
427: break;
428: }
429: free(line);
430: }
1.146 nicm 431: if (client_flags & CLIENT_CONTROLCONTROL) {
432: printf("\033\\");
1.148 nicm 433: fflush(stdout);
1.146 nicm 434: tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
435: }
1.112 nicm 436: } else if (client_exitreason != CLIENT_EXIT_NONE)
1.109 nicm 437: fprintf(stderr, "%s\n", client_exit_message());
1.46 nicm 438: return (client_exitval);
1.1 nicm 439: }
440:
1.71 nicm 441: /* Send identify messages to server. */
1.114 nicm 442: static void
1.155 nicm 443: client_send_identify(const char *ttynam, const char *termname, char **caps,
444: u_int ncaps, const char *cwd, int feat)
1.26 nicm 445: {
1.155 nicm 446: char **ss;
447: size_t sslen;
448: int fd, flags = client_flags;
449: pid_t pid;
450: u_int i;
1.71 nicm 451:
1.99 nicm 452: proc_send(client_peer, MSG_IDENTIFY_FLAGS, -1, &flags, sizeof flags);
1.149 nicm 453: proc_send(client_peer, MSG_IDENTIFY_LONGFLAGS, -1, &client_flags,
454: sizeof client_flags);
1.71 nicm 455:
1.155 nicm 456: proc_send(client_peer, MSG_IDENTIFY_TERM, -1, termname,
457: strlen(termname) + 1);
1.142 nicm 458: proc_send(client_peer, MSG_IDENTIFY_FEATURES, -1, &feat, sizeof feat);
1.71 nicm 459:
1.107 nicm 460: proc_send(client_peer, MSG_IDENTIFY_TTYNAME, -1, ttynam,
461: strlen(ttynam) + 1);
1.99 nicm 462: proc_send(client_peer, MSG_IDENTIFY_CWD, -1, cwd, strlen(cwd) + 1);
1.155 nicm 463:
464: for (i = 0; i < ncaps; i++) {
465: proc_send(client_peer, MSG_IDENTIFY_TERMINFO, -1,
466: caps[i], strlen(caps[i]) + 1);
467: }
1.26 nicm 468:
1.50 nicm 469: if ((fd = dup(STDIN_FILENO)) == -1)
470: fatal("dup failed");
1.99 nicm 471: proc_send(client_peer, MSG_IDENTIFY_STDIN, fd, NULL, 0);
1.145 nicm 472: if ((fd = dup(STDOUT_FILENO)) == -1)
473: fatal("dup failed");
474: proc_send(client_peer, MSG_IDENTIFY_STDOUT, fd, NULL, 0);
1.90 nicm 475:
476: pid = getpid();
1.99 nicm 477: proc_send(client_peer, MSG_IDENTIFY_CLIENTPID, -1, &pid, sizeof pid);
1.26 nicm 478:
1.91 nicm 479: for (ss = environ; *ss != NULL; ss++) {
480: sslen = strlen(*ss) + 1;
1.107 nicm 481: if (sslen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
482: continue;
483: proc_send(client_peer, MSG_IDENTIFY_ENVIRON, -1, *ss, sslen);
1.91 nicm 484: }
1.75 nicm 485:
1.99 nicm 486: proc_send(client_peer, MSG_IDENTIFY_DONE, -1, NULL, 0);
1.30 nicm 487: }
488:
1.93 nicm 489: /* Run command in shell; used for -c. */
1.114 nicm 490: static __dead void
1.111 nicm 491: client_exec(const char *shell, const char *shellcmd)
1.93 nicm 492: {
493: const char *name, *ptr;
494: char *argv0;
495:
1.111 nicm 496: log_debug("shell %s, command %s", shell, shellcmd);
1.93 nicm 497:
498: ptr = strrchr(shell, '/');
499: if (ptr != NULL && *(ptr + 1) != '\0')
500: name = ptr + 1;
501: else
502: name = shell;
503: if (client_flags & CLIENT_LOGIN)
504: xasprintf(&argv0, "-%s", name);
505: else
506: xasprintf(&argv0, "%s", name);
507: setenv("SHELL", shell, 1);
508:
1.123 nicm 509: proc_clear_signals(client_proc, 1);
510:
1.93 nicm 511: setblocking(STDIN_FILENO, 1);
512: setblocking(STDOUT_FILENO, 1);
513: setblocking(STDERR_FILENO, 1);
514: closefrom(STDERR_FILENO + 1);
515:
1.111 nicm 516: execl(shell, argv0, "-c", shellcmd, (char *) NULL);
1.93 nicm 517: fatal("execl failed");
518: }
519:
1.99 nicm 520: /* Callback to handle signals in the client. */
1.114 nicm 521: static void
1.99 nicm 522: client_signal(int sig)
523: {
524: struct sigaction sigact;
525: int status;
1.160 nicm 526: pid_t pid;
1.99 nicm 527:
1.150 nicm 528: log_debug("%s: %s", __func__, strsignal(sig));
1.160 nicm 529: if (sig == SIGCHLD) {
530: for (;;) {
531: pid = waitpid(WAIT_ANY, &status, WNOHANG);
532: if (pid == 0)
533: break;
534: if (pid == -1) {
535: if (errno == ECHILD)
536: break;
537: log_debug("waitpid failed: %s",
538: strerror(errno));
539: }
540: }
541: } else if (!client_attached) {
1.157 nicm 542: if (sig == SIGTERM || sig == SIGHUP)
1.99 nicm 543: proc_exit(client_proc);
544: } else {
545: switch (sig) {
546: case SIGHUP:
547: client_exitreason = CLIENT_EXIT_LOST_TTY;
548: client_exitval = 1;
549: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
550: break;
551: case SIGTERM:
1.150 nicm 552: if (!client_suspended)
553: client_exitreason = CLIENT_EXIT_TERMINATED;
1.99 nicm 554: client_exitval = 1;
555: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
556: break;
557: case SIGWINCH:
558: proc_send(client_peer, MSG_RESIZE, -1, NULL, 0);
559: break;
560: case SIGCONT:
561: memset(&sigact, 0, sizeof sigact);
562: sigemptyset(&sigact.sa_mask);
563: sigact.sa_flags = SA_RESTART;
564: sigact.sa_handler = SIG_IGN;
565: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
566: fatal("sigaction failed");
567: proc_send(client_peer, MSG_WAKEUP, -1, NULL, 0);
1.150 nicm 568: client_suspended = 0;
1.99 nicm 569: break;
570: }
571: }
572: }
573:
1.152 nicm 574: /* Callback for file write error or close. */
575: static void
576: client_file_check_cb(__unused struct client *c, __unused const char *path,
577: __unused int error, __unused int closed, __unused struct evbuffer *buffer,
578: __unused void *data)
579: {
580: if (client_exitflag)
581: client_exit();
582: }
583:
1.99 nicm 584: /* Callback for client read events. */
1.114 nicm 585: static void
1.121 nicm 586: client_dispatch(struct imsg *imsg, __unused void *arg)
1.99 nicm 587: {
588: if (imsg == NULL) {
1.154 nicm 589: if (!client_exitflag) {
590: client_exitreason = CLIENT_EXIT_LOST_SERVER;
591: client_exitval = 1;
592: }
1.109 nicm 593: proc_exit(client_proc);
594: return;
595: }
596:
597: if (client_attached)
1.99 nicm 598: client_dispatch_attached(imsg);
599: else
1.121 nicm 600: client_dispatch_wait(imsg);
1.99 nicm 601: }
602:
1.147 nicm 603: /* Process an exit message. */
604: static void
605: client_dispatch_exit_message(char *data, size_t datalen)
606: {
607: int retval;
608:
609: if (datalen < sizeof retval && datalen != 0)
610: fatalx("bad MSG_EXIT size");
611:
612: if (datalen >= sizeof retval) {
613: memcpy(&retval, data, sizeof retval);
614: client_exitval = retval;
615: }
616:
617: if (datalen > sizeof retval) {
618: datalen -= sizeof retval;
619: data += sizeof retval;
620:
621: client_exitmessage = xmalloc(datalen);
622: memcpy(client_exitmessage, data, datalen);
623: client_exitmessage[datalen - 1] = '\0';
624:
625: client_exitreason = CLIENT_EXIT_MESSAGE_PROVIDED;
626: }
627: }
628:
1.46 nicm 629: /* Dispatch imsgs when in wait state (before MSG_READY). */
1.114 nicm 630: static void
1.121 nicm 631: client_dispatch_wait(struct imsg *imsg)
1.46 nicm 632: {
1.132 nicm 633: char *data;
634: ssize_t datalen;
635: static int pledge_applied;
1.97 nicm 636:
637: /*
638: * "sendfd" is no longer required once all of the identify messages
639: * have been sent. We know the server won't send us anything until that
640: * point (because we don't ask it to), so we can drop "sendfd" once we
641: * get the first message from the server.
642: */
643: if (!pledge_applied) {
1.132 nicm 644: if (pledge(
645: "stdio rpath wpath cpath unix proc exec tty",
646: NULL) != 0)
1.97 nicm 647: fatal("pledge failed");
648: pledge_applied = 1;
1.132 nicm 649: }
1.46 nicm 650:
1.99 nicm 651: data = imsg->data;
652: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.60 nicm 653:
1.99 nicm 654: switch (imsg->hdr.type) {
655: case MSG_EXIT:
656: case MSG_SHUTDOWN:
1.147 nicm 657: client_dispatch_exit_message(data, datalen);
1.133 nicm 658: client_exitflag = 1;
659: client_exit();
1.99 nicm 660: break;
661: case MSG_READY:
662: if (datalen != 0)
663: fatalx("bad MSG_READY size");
1.54 nicm 664:
1.99 nicm 665: client_attached = 1;
666: proc_send(client_peer, MSG_RESIZE, -1, NULL, 0);
667: break;
668: case MSG_VERSION:
669: if (datalen != 0)
670: fatalx("bad MSG_VERSION size");
671:
672: fprintf(stderr, "protocol version mismatch "
673: "(client %d, server %u)\n", PROTOCOL_VERSION,
1.103 nicm 674: imsg->hdr.peerid & 0xff);
1.99 nicm 675: client_exitval = 1;
676: proc_exit(client_proc);
677: break;
1.148 nicm 678: case MSG_FLAGS:
679: if (datalen != sizeof client_flags)
680: fatalx("bad MSG_FLAGS string");
681:
682: memcpy(&client_flags, data, sizeof client_flags);
1.149 nicm 683: log_debug("new flags are %#llx",
684: (unsigned long long)client_flags);
1.148 nicm 685: break;
1.99 nicm 686: case MSG_SHELL:
687: if (datalen == 0 || data[datalen - 1] != '\0')
688: fatalx("bad MSG_SHELL string");
689:
1.121 nicm 690: client_exec(data, shell_command);
1.99 nicm 691: /* NOTREACHED */
692: case MSG_DETACH:
693: case MSG_DETACHKILL:
694: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
695: break;
696: case MSG_EXITED:
697: proc_exit(client_proc);
1.132 nicm 698: break;
699: case MSG_READ_OPEN:
1.152 nicm 700: file_read_open(&client_files, client_peer, imsg, 1,
701: !(client_flags & CLIENT_CONTROL), client_file_check_cb,
702: NULL);
1.159 nicm 703: break;
704: case MSG_READ_CANCEL:
705: file_read_cancel(&client_files, imsg);
1.132 nicm 706: break;
707: case MSG_WRITE_OPEN:
1.152 nicm 708: file_write_open(&client_files, client_peer, imsg, 1,
709: !(client_flags & CLIENT_CONTROL), client_file_check_cb,
710: NULL);
1.132 nicm 711: break;
712: case MSG_WRITE:
1.152 nicm 713: file_write_data(&client_files, imsg);
1.132 nicm 714: break;
715: case MSG_WRITE_CLOSE:
1.152 nicm 716: file_write_close(&client_files, imsg);
1.137 nicm 717: break;
718: case MSG_OLDSTDERR:
719: case MSG_OLDSTDIN:
720: case MSG_OLDSTDOUT:
721: fprintf(stderr, "server version is too old for client\n");
722: proc_exit(client_proc);
1.99 nicm 723: break;
1.46 nicm 724: }
725: }
726:
727: /* Dispatch imsgs in attached state (after MSG_READY). */
1.114 nicm 728: static void
1.99 nicm 729: client_dispatch_attached(struct imsg *imsg)
1.9 nicm 730: {
1.69 nicm 731: struct sigaction sigact;
732: char *data;
1.99 nicm 733: ssize_t datalen;
1.9 nicm 734:
1.99 nicm 735: data = imsg->data;
736: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.12 nicm 737:
1.99 nicm 738: switch (imsg->hdr.type) {
1.148 nicm 739: case MSG_FLAGS:
740: if (datalen != sizeof client_flags)
741: fatalx("bad MSG_FLAGS string");
742:
743: memcpy(&client_flags, data, sizeof client_flags);
1.149 nicm 744: log_debug("new flags are %#llx",
745: (unsigned long long)client_flags);
1.148 nicm 746: break;
1.99 nicm 747: case MSG_DETACH:
748: case MSG_DETACHKILL:
749: if (datalen == 0 || data[datalen - 1] != '\0')
750: fatalx("bad MSG_DETACH string");
751:
752: client_exitsession = xstrdup(data);
753: client_exittype = imsg->hdr.type;
754: if (imsg->hdr.type == MSG_DETACHKILL)
755: client_exitreason = CLIENT_EXIT_DETACHED_HUP;
756: else
757: client_exitreason = CLIENT_EXIT_DETACHED;
1.115 nicm 758: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
759: break;
760: case MSG_EXEC:
761: if (datalen == 0 || data[datalen - 1] != '\0' ||
762: strlen(data) + 1 == (size_t)datalen)
763: fatalx("bad MSG_EXEC string");
764: client_execcmd = xstrdup(data);
765: client_execshell = xstrdup(data + strlen(data) + 1);
766:
767: client_exittype = imsg->hdr.type;
1.99 nicm 768: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
769: break;
770: case MSG_EXIT:
1.147 nicm 771: client_dispatch_exit_message(data, datalen);
772: if (client_exitreason == CLIENT_EXIT_NONE)
773: client_exitreason = CLIENT_EXIT_EXITED;
1.99 nicm 774: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
775: break;
776: case MSG_EXITED:
777: if (datalen != 0)
778: fatalx("bad MSG_EXITED size");
1.9 nicm 779:
1.99 nicm 780: proc_exit(client_proc);
781: break;
782: case MSG_SHUTDOWN:
783: if (datalen != 0)
784: fatalx("bad MSG_SHUTDOWN size");
785:
786: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
787: client_exitreason = CLIENT_EXIT_SERVER_EXITED;
788: client_exitval = 1;
789: break;
790: case MSG_SUSPEND:
791: if (datalen != 0)
792: fatalx("bad MSG_SUSPEND size");
793:
794: memset(&sigact, 0, sizeof sigact);
795: sigemptyset(&sigact.sa_mask);
796: sigact.sa_flags = SA_RESTART;
797: sigact.sa_handler = SIG_DFL;
798: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
799: fatal("sigaction failed");
1.150 nicm 800: client_suspended = 1;
1.99 nicm 801: kill(getpid(), SIGTSTP);
802: break;
803: case MSG_LOCK:
804: if (datalen == 0 || data[datalen - 1] != '\0')
805: fatalx("bad MSG_LOCK string");
1.9 nicm 806:
1.99 nicm 807: system(data);
808: proc_send(client_peer, MSG_UNLOCK, -1, NULL, 0);
809: break;
1.9 nicm 810: }
1.1 nicm 811: }