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