Annotation of src/usr.bin/tmux/client.c, Revision 1.62
1.62 ! nicm 1: /* $OpenBSD: client.c,v 1.61 2013/03/22 10:31:22 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:
81: if (flock(lockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) {
82: while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
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;
182: cmdflags = CMD_STARTSERVER|CMD_SENDENVIRON|CMD_CANTNEST;
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_SENDENVIRON)
201: cmdflags |= CMD_SENDENVIRON;
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.56 nicm 239: if (flags & IDENTIFY_TERMIOS) {
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.11 nicm 261: if (cmdflags & CMD_SENDENVIRON)
1.25 nicm 262: client_send_environ();
1.42 nicm 263: client_send_identify(flags);
1.1 nicm 264:
1.46 nicm 265: /* Send first command. */
266: if (msg == MSG_COMMAND) {
267: /* Fill in command line arguments. */
268: cmddata.pid = environ_pid;
269: cmddata.idx = environ_idx;
270:
271: /* Prepare command for server. */
272: cmddata.argc = argc;
273: if (cmd_pack_argv(
274: argc, argv, cmddata.argv, sizeof cmddata.argv) != 0) {
1.55 nicm 275: fprintf(stderr, "command too long\n");
1.46 nicm 276: return (1);
277: }
278:
279: client_write_server(msg, &cmddata, sizeof cmddata);
280: } else if (msg == MSG_SHELL)
281: client_write_server(msg, NULL, 0);
1.1 nicm 282:
1.46 nicm 283: /* Set the event and dispatch. */
284: client_update_event();
285: event_dispatch();
286:
287: /* Print the exit message, if any, and exit. */
1.48 nicm 288: if (client_attached) {
1.52 nicm 289: if (client_exitreason != CLIENT_EXIT_NONE && !login_shell)
290: printf("[%s]\n", client_exit_message());
1.48 nicm 291:
292: ppid = getppid();
293: if (client_exittype == MSG_DETACHKILL && ppid > 1)
294: kill(ppid, SIGHUP);
1.56 nicm 295: } else if (flags & IDENTIFY_TERMIOS)
296: tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
1.54 nicm 297: setblocking(STDIN_FILENO, 1);
1.46 nicm 298: return (client_exitval);
1.1 nicm 299: }
300:
1.46 nicm 301: /* Send identify message to server with the file descriptors. */
1.11 nicm 302: void
1.26 nicm 303: client_send_identify(int flags)
304: {
305: struct msg_identify_data data;
306: char *term;
307: int fd;
308:
309: data.flags = flags;
310:
311: if (getcwd(data.cwd, sizeof data.cwd) == NULL)
312: *data.cwd = '\0';
1.35 nicm 313:
1.26 nicm 314: term = getenv("TERM");
315: if (term == NULL ||
316: strlcpy(data.term, term, sizeof data.term) >= sizeof data.term)
317: *data.term = '\0';
318:
1.50 nicm 319: if ((fd = dup(STDIN_FILENO)) == -1)
320: fatal("dup failed");
321: imsg_compose(&client_ibuf,
322: MSG_IDENTIFY, PROTOCOL_VERSION, -1, fd, &data, sizeof data);
1.54 nicm 323: client_update_event();
1.26 nicm 324: }
325:
1.46 nicm 326: /* Forward entire environment to server. */
1.26 nicm 327: void
1.25 nicm 328: client_send_environ(void)
1.11 nicm 329: {
1.26 nicm 330: struct msg_environ_data data;
1.11 nicm 331: char **var;
332:
1.35 nicm 333: for (var = environ; *var != NULL; var++) {
1.11 nicm 334: if (strlcpy(data.var, *var, sizeof data.var) >= sizeof data.var)
335: continue;
1.25 nicm 336: client_write_server(MSG_ENVIRON, &data, sizeof data);
1.11 nicm 337: }
338: }
339:
1.46 nicm 340: /* Write a message to the server without a file descriptor. */
1.25 nicm 341: void
342: client_write_server(enum msgtype type, void *buf, size_t len)
343: {
1.35 nicm 344: imsg_compose(&client_ibuf, type, PROTOCOL_VERSION, -1, -1, buf, len);
1.54 nicm 345: client_update_event();
1.25 nicm 346: }
347:
1.46 nicm 348: /* Update client event based on whether it needs to read or read and write. */
1.31 nicm 349: void
350: client_update_event(void)
351: {
352: short events;
353:
354: event_del(&client_event);
355: events = EV_READ;
356: if (client_ibuf.w.queued > 0)
357: events |= EV_WRITE;
1.46 nicm 358: event_set(
359: &client_event, client_ibuf.fd, events, client_callback, shell_cmd);
1.31 nicm 360: event_add(&client_event, NULL);
361: }
362:
1.46 nicm 363: /* Callback to handle signals in the client. */
1.30 nicm 364: void
1.31 nicm 365: client_signal(int sig, unused short events, unused void *data)
1.30 nicm 366: {
1.46 nicm 367: struct sigaction sigact;
368: int status;
1.30 nicm 369:
1.46 nicm 370: if (!client_attached) {
371: switch (sig) {
372: case SIGCHLD:
373: waitpid(WAIT_ANY, &status, WNOHANG);
374: break;
375: case SIGTERM:
376: event_loopexit(NULL);
377: break;
378: }
379: } else {
380: switch (sig) {
381: case SIGHUP:
1.52 nicm 382: client_exitreason = CLIENT_EXIT_LOST_TTY;
1.46 nicm 383: client_exitval = 1;
384: client_write_server(MSG_EXITING, NULL, 0);
385: break;
386: case SIGTERM:
1.52 nicm 387: client_exitreason = CLIENT_EXIT_TERMINATED;
1.46 nicm 388: client_exitval = 1;
389: client_write_server(MSG_EXITING, NULL, 0);
390: break;
391: case SIGWINCH:
392: client_write_server(MSG_RESIZE, NULL, 0);
393: break;
394: case SIGCONT:
395: memset(&sigact, 0, sizeof sigact);
396: sigemptyset(&sigact.sa_mask);
397: sigact.sa_flags = SA_RESTART;
398: sigact.sa_handler = SIG_IGN;
399: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
400: fatal("sigaction failed");
401: client_write_server(MSG_WAKEUP, NULL, 0);
402: break;
403: }
1.30 nicm 404: }
405:
1.31 nicm 406: client_update_event();
1.30 nicm 407: }
408:
1.46 nicm 409: /* Callback for client imsg read events. */
1.30 nicm 410: void
1.46 nicm 411: client_callback(unused int fd, short events, void *data)
1.30 nicm 412: {
1.33 nicm 413: ssize_t n;
1.46 nicm 414: int retval;
1.30 nicm 415:
416: if (events & EV_READ) {
417: if ((n = imsg_read(&client_ibuf)) == -1 || n == 0)
418: goto lost_server;
1.46 nicm 419: if (client_attached)
420: retval = client_dispatch_attached();
421: else
422: retval = client_dispatch_wait(data);
423: if (retval != 0) {
1.30 nicm 424: event_loopexit(NULL);
425: return;
1.35 nicm 426: }
1.30 nicm 427: }
1.35 nicm 428:
1.30 nicm 429: if (events & EV_WRITE) {
430: if (msgbuf_write(&client_ibuf.w) < 0)
431: goto lost_server;
432: }
433:
1.31 nicm 434: client_update_event();
1.30 nicm 435: return;
436:
437: lost_server:
1.52 nicm 438: client_exitreason = CLIENT_EXIT_LOST_SERVER;
1.32 nicm 439: client_exitval = 1;
1.30 nicm 440: event_loopexit(NULL);
441: }
442:
1.54 nicm 443: /* Callback for client stdin read events. */
444: void
445: client_stdin_callback(unused int fd, unused short events, unused void *data1)
446: {
447: struct msg_stdin_data data;
448:
449: data.size = read(STDIN_FILENO, data.data, sizeof data.data);
450: if (data.size < 0 && (errno == EINTR || errno == EAGAIN))
451: return;
452:
453: client_write_server(MSG_STDIN, &data, sizeof data);
454: if (data.size <= 0)
455: event_del(&client_stdin);
456: client_update_event();
457: }
458:
1.57 nicm 459: /* Force write to file descriptor. */
460: void
461: client_write(int fd, const char *data, size_t size)
462: {
463: ssize_t used;
464:
465: while (size != 0) {
466: used = write(fd, data, size);
467: if (used == -1) {
468: if (errno == EINTR || errno == EAGAIN)
469: continue;
470: break;
471: }
472: data += used;
473: size -= used;
474: }
475: }
476:
1.46 nicm 477: /* Dispatch imsgs when in wait state (before MSG_READY). */
478: int
479: client_dispatch_wait(void *data)
480: {
481: struct imsg imsg;
482: ssize_t n, datalen;
483: struct msg_shell_data shelldata;
484: struct msg_exit_data exitdata;
1.54 nicm 485: struct msg_stdout_data stdoutdata;
486: struct msg_stderr_data stderrdata;
1.46 nicm 487: const char *shellcmd = data;
488:
489: for (;;) {
490: if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
491: fatalx("imsg_get failed");
492: if (n == 0)
493: return (0);
494: datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
495:
1.54 nicm 496: log_debug("got %d from server", imsg.hdr.type);
1.46 nicm 497: switch (imsg.hdr.type) {
498: case MSG_EXIT:
499: case MSG_SHUTDOWN:
500: if (datalen != sizeof exitdata) {
501: if (datalen != 0)
502: fatalx("bad MSG_EXIT size");
503: } else {
504: memcpy(&exitdata, imsg.data, sizeof exitdata);
505: client_exitval = exitdata.retcode;
506: }
507: imsg_free(&imsg);
508: return (-1);
509: case MSG_READY:
510: if (datalen != 0)
511: fatalx("bad MSG_READY size");
512:
1.54 nicm 513: event_del(&client_stdin);
1.46 nicm 514: client_attached = 1;
1.60 nicm 515: break;
516: case MSG_STDIN:
517: if (datalen != 0)
518: fatalx("bad MSG_STDIN size");
519:
520: event_add(&client_stdin, NULL);
1.46 nicm 521: break;
1.54 nicm 522: case MSG_STDOUT:
523: if (datalen != sizeof stdoutdata)
524: fatalx("bad MSG_STDOUT");
525: memcpy(&stdoutdata, imsg.data, sizeof stdoutdata);
526:
1.57 nicm 527: client_write(STDOUT_FILENO, stdoutdata.data, stdoutdata.size);
1.54 nicm 528: break;
529: case MSG_STDERR:
530: if (datalen != sizeof stderrdata)
531: fatalx("bad MSG_STDERR");
532: memcpy(&stderrdata, imsg.data, sizeof stderrdata);
533:
1.57 nicm 534: client_write(STDERR_FILENO, stderrdata.data, stderrdata.size);
1.54 nicm 535: break;
1.46 nicm 536: case MSG_VERSION:
537: if (datalen != 0)
538: fatalx("bad MSG_VERSION size");
539:
1.54 nicm 540: fprintf(stderr, "protocol version mismatch "
541: "(client %u, server %u)\n", PROTOCOL_VERSION,
542: imsg.hdr.peerid);
1.46 nicm 543: client_exitval = 1;
544:
545: imsg_free(&imsg);
546: return (-1);
547: case MSG_SHELL:
548: if (datalen != sizeof shelldata)
549: fatalx("bad MSG_SHELL size");
550: memcpy(&shelldata, imsg.data, sizeof shelldata);
551: shelldata.shell[(sizeof shelldata.shell) - 1] = '\0';
552:
553: clear_signals(0);
554:
555: shell_exec(shelldata.shell, shellcmd);
556: /* NOTREACHED */
1.56 nicm 557: case MSG_DETACH:
558: client_write_server(MSG_EXITING, NULL, 0);
559: break;
560: case MSG_EXITED:
561: imsg_free(&imsg);
562: return (-1);
1.46 nicm 563: default:
564: fatalx("unexpected message");
565: }
566:
567: imsg_free(&imsg);
568: }
569: }
570:
571: /* Dispatch imsgs in attached state (after MSG_READY). */
1.9 nicm 572: int
1.46 nicm 573: client_dispatch_attached(void)
1.9 nicm 574: {
1.30 nicm 575: struct imsg imsg;
576: struct msg_lock_data lockdata;
577: struct sigaction sigact;
578: ssize_t n, datalen;
1.9 nicm 579:
580: for (;;) {
1.25 nicm 581: if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
1.12 nicm 582: fatalx("imsg_get failed");
583: if (n == 0)
1.9 nicm 584: return (0);
1.12 nicm 585: datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1.9 nicm 586:
1.55 nicm 587: log_debug("got %d from server", imsg.hdr.type);
1.12 nicm 588: switch (imsg.hdr.type) {
1.48 nicm 589: case MSG_DETACHKILL:
1.9 nicm 590: case MSG_DETACH:
1.12 nicm 591: if (datalen != 0)
1.9 nicm 592: fatalx("bad MSG_DETACH size");
593:
1.48 nicm 594: client_exittype = imsg.hdr.type;
595: if (imsg.hdr.type == MSG_DETACHKILL)
1.52 nicm 596: client_exitreason = CLIENT_EXIT_DETACHED_HUP;
1.48 nicm 597: else
1.52 nicm 598: client_exitreason = CLIENT_EXIT_DETACHED;
1.25 nicm 599: client_write_server(MSG_EXITING, NULL, 0);
1.9 nicm 600: break;
601: case MSG_EXIT:
1.43 nicm 602: if (datalen != 0 &&
603: datalen != sizeof (struct msg_exit_data))
1.9 nicm 604: fatalx("bad MSG_EXIT size");
1.12 nicm 605:
1.25 nicm 606: client_write_server(MSG_EXITING, NULL, 0);
1.52 nicm 607: client_exitreason = CLIENT_EXIT_EXITED;
1.9 nicm 608: break;
609: case MSG_EXITED:
1.12 nicm 610: if (datalen != 0)
1.9 nicm 611: fatalx("bad MSG_EXITED size");
612:
1.12 nicm 613: imsg_free(&imsg);
1.9 nicm 614: return (-1);
615: case MSG_SHUTDOWN:
1.12 nicm 616: if (datalen != 0)
1.9 nicm 617: fatalx("bad MSG_SHUTDOWN size");
618:
1.25 nicm 619: client_write_server(MSG_EXITING, NULL, 0);
1.52 nicm 620: client_exitreason = CLIENT_EXIT_SERVER_EXITED;
1.32 nicm 621: client_exitval = 1;
1.9 nicm 622: break;
623: case MSG_SUSPEND:
1.12 nicm 624: if (datalen != 0)
1.9 nicm 625: fatalx("bad MSG_SUSPEND size");
626:
1.30 nicm 627: memset(&sigact, 0, sizeof sigact);
628: sigemptyset(&sigact.sa_mask);
629: sigact.sa_flags = SA_RESTART;
630: sigact.sa_handler = SIG_DFL;
631: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
632: fatal("sigaction failed");
633: kill(getpid(), SIGTSTP);
1.21 nicm 634: break;
635: case MSG_LOCK:
636: if (datalen != sizeof lockdata)
637: fatalx("bad MSG_LOCK size");
638: memcpy(&lockdata, imsg.data, sizeof lockdata);
1.35 nicm 639:
1.21 nicm 640: lockdata.cmd[(sizeof lockdata.cmd) - 1] = '\0';
641: system(lockdata.cmd);
1.25 nicm 642: client_write_server(MSG_UNLOCK, NULL, 0);
1.9 nicm 643: break;
644: default:
645: fatalx("unexpected message");
646: }
1.12 nicm 647:
648: imsg_free(&imsg);
1.9 nicm 649: }
1.1 nicm 650: }