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