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