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