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