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