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