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