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