Ignore the user keys range when checking if a key is Unicode.
[tmux-openbsd.git] / client.c
blob4f91d30ed7c413a365f1c5e5f6747406eab69c67
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/uio.h>
22 #include <sys/un.h>
23 #include <sys/wait.h>
25 #include <errno.h>
26 #include <event.h>
27 #include <fcntl.h>
28 #include <imsg.h>
29 #include <signal.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include "tmux.h"
36 static struct tmuxproc *client_proc;
37 static struct tmuxpeer *client_peer;
38 static uint64_t client_flags;
39 static int client_suspended;
40 static enum {
41 CLIENT_EXIT_NONE,
42 CLIENT_EXIT_DETACHED,
43 CLIENT_EXIT_DETACHED_HUP,
44 CLIENT_EXIT_LOST_TTY,
45 CLIENT_EXIT_TERMINATED,
46 CLIENT_EXIT_LOST_SERVER,
47 CLIENT_EXIT_EXITED,
48 CLIENT_EXIT_SERVER_EXITED,
49 CLIENT_EXIT_MESSAGE_PROVIDED
50 } client_exitreason = CLIENT_EXIT_NONE;
51 static int client_exitflag;
52 static int client_exitval;
53 static enum msgtype client_exittype;
54 static const char *client_exitsession;
55 static char *client_exitmessage;
56 static const char *client_execshell;
57 static const char *client_execcmd;
58 static int client_attached;
59 static struct client_files client_files = RB_INITIALIZER(&client_files);
61 static __dead void client_exec(const char *,const char *);
62 static int client_get_lock(char *);
63 static int client_connect(struct event_base *, const char *,
64 uint64_t);
65 static void client_send_identify(const char *, const char *,
66 char **, u_int, const char *, int);
67 static void client_signal(int);
68 static void client_dispatch(struct imsg *, void *);
69 static void client_dispatch_attached(struct imsg *);
70 static void client_dispatch_wait(struct imsg *);
71 static const char *client_exit_message(void);
74 * Get server create lock. If already held then server start is happening in
75 * another client, so block until the lock is released and return -2 to
76 * retry. Return -1 on failure to continue and start the server anyway.
78 static int
79 client_get_lock(char *lockfile)
81 int lockfd;
83 log_debug("lock file is %s", lockfile);
85 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
86 log_debug("open failed: %s", strerror(errno));
87 return (-1);
90 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
91 log_debug("flock failed: %s", strerror(errno));
92 if (errno != EAGAIN)
93 return (lockfd);
94 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
95 /* nothing */;
96 close(lockfd);
97 return (-2);
99 log_debug("flock succeeded");
101 return (lockfd);
104 /* Connect client to server. */
105 static int
106 client_connect(struct event_base *base, const char *path, uint64_t flags)
108 struct sockaddr_un sa;
109 size_t size;
110 int fd, lockfd = -1, locked = 0;
111 char *lockfile = NULL;
113 memset(&sa, 0, sizeof sa);
114 sa.sun_family = AF_UNIX;
115 size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
116 if (size >= sizeof sa.sun_path) {
117 errno = ENAMETOOLONG;
118 return (-1);
120 log_debug("socket is %s", path);
122 retry:
123 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
124 return (-1);
126 log_debug("trying connect");
127 if (connect(fd, (struct sockaddr *)&sa, sizeof sa) == -1) {
128 log_debug("connect failed: %s", strerror(errno));
129 if (errno != ECONNREFUSED && errno != ENOENT)
130 goto failed;
131 if (flags & CLIENT_NOSTARTSERVER)
132 goto failed;
133 if (~flags & CLIENT_STARTSERVER)
134 goto failed;
135 close(fd);
137 if (!locked) {
138 xasprintf(&lockfile, "%s.lock", path);
139 if ((lockfd = client_get_lock(lockfile)) < 0) {
140 log_debug("didn't get lock (%d)", lockfd);
142 free(lockfile);
143 lockfile = NULL;
145 if (lockfd == -2)
146 goto retry;
148 log_debug("got lock (%d)", lockfd);
151 * Always retry at least once, even if we got the lock,
152 * because another client could have taken the lock,
153 * started the server and released the lock between our
154 * connect() and flock().
156 locked = 1;
157 goto retry;
160 if (lockfd >= 0 && unlink(path) != 0 && errno != ENOENT) {
161 free(lockfile);
162 close(lockfd);
163 return (-1);
165 fd = server_start(client_proc, flags, base, lockfd, lockfile);
168 if (locked && lockfd >= 0) {
169 free(lockfile);
170 close(lockfd);
172 setblocking(fd, 0);
173 return (fd);
175 failed:
176 if (locked) {
177 free(lockfile);
178 close(lockfd);
180 close(fd);
181 return (-1);
184 /* Get exit string from reason number. */
185 const char *
186 client_exit_message(void)
188 static char msg[256];
190 switch (client_exitreason) {
191 case CLIENT_EXIT_NONE:
192 break;
193 case CLIENT_EXIT_DETACHED:
194 if (client_exitsession != NULL) {
195 xsnprintf(msg, sizeof msg, "detached "
196 "(from session %s)", client_exitsession);
197 return (msg);
199 return ("detached");
200 case CLIENT_EXIT_DETACHED_HUP:
201 if (client_exitsession != NULL) {
202 xsnprintf(msg, sizeof msg, "detached and SIGHUP "
203 "(from session %s)", client_exitsession);
204 return (msg);
206 return ("detached and SIGHUP");
207 case CLIENT_EXIT_LOST_TTY:
208 return ("lost tty");
209 case CLIENT_EXIT_TERMINATED:
210 return ("terminated");
211 case CLIENT_EXIT_LOST_SERVER:
212 return ("server exited unexpectedly");
213 case CLIENT_EXIT_EXITED:
214 return ("exited");
215 case CLIENT_EXIT_SERVER_EXITED:
216 return ("server exited");
217 case CLIENT_EXIT_MESSAGE_PROVIDED:
218 return (client_exitmessage);
220 return ("unknown reason");
223 /* Exit if all streams flushed. */
224 static void
225 client_exit(void)
227 if (!file_write_left(&client_files))
228 proc_exit(client_proc);
231 /* Client main loop. */
233 client_main(struct event_base *base, int argc, char **argv, uint64_t flags,
234 int feat)
236 struct cmd_parse_result *pr;
237 struct msg_command *data;
238 int fd, i;
239 const char *ttynam, *termname, *cwd;
240 pid_t ppid;
241 enum msgtype msg;
242 struct termios tio, saved_tio;
243 size_t size, linesize = 0;
244 ssize_t linelen;
245 char *line = NULL, **caps = NULL, *cause;
246 u_int ncaps = 0;
247 struct args_value *values;
249 /* Ignore SIGCHLD now or daemon() in the server will leave a zombie. */
250 signal(SIGCHLD, SIG_IGN);
252 /* Set up the initial command. */
253 if (shell_command != NULL) {
254 msg = MSG_SHELL;
255 flags |= CLIENT_STARTSERVER;
256 } else if (argc == 0) {
257 msg = MSG_COMMAND;
258 flags |= CLIENT_STARTSERVER;
259 } else {
260 msg = MSG_COMMAND;
263 * It's annoying parsing the command string twice (in client
264 * and later in server) but it is necessary to get the start
265 * server flag.
267 values = args_from_vector(argc, argv);
268 pr = cmd_parse_from_arguments(values, argc, NULL);
269 if (pr->status == CMD_PARSE_SUCCESS) {
270 if (cmd_list_any_have(pr->cmdlist, CMD_STARTSERVER))
271 flags |= CLIENT_STARTSERVER;
272 cmd_list_free(pr->cmdlist);
273 } else
274 free(pr->error);
275 args_free_values(values, argc);
276 free(values);
279 /* Create client process structure (starts logging). */
280 client_proc = proc_start("client");
281 proc_set_signals(client_proc, client_signal);
283 /* Save the flags. */
284 client_flags = flags;
285 log_debug("flags are %#llx", (unsigned long long)client_flags);
287 /* Initialize the client socket and start the server. */
288 fd = client_connect(base, socket_path, client_flags);
289 if (fd == -1) {
290 if (errno == ECONNREFUSED) {
291 fprintf(stderr, "no server running on %s\n",
292 socket_path);
293 } else {
294 fprintf(stderr, "error connecting to %s (%s)\n",
295 socket_path, strerror(errno));
297 return (1);
299 client_peer = proc_add_peer(client_proc, fd, client_dispatch, NULL);
301 /* Save these before pledge(). */
302 if ((cwd = find_cwd()) == NULL && (cwd = find_home()) == NULL)
303 cwd = "/";
304 if ((ttynam = ttyname(STDIN_FILENO)) == NULL)
305 ttynam = "";
306 if ((termname = getenv("TERM")) == NULL)
307 termname = "";
310 * Drop privileges for client. "proc exec" is needed for -c and for
311 * locking (which uses system(3)).
313 * "tty" is needed to restore termios(4) and also for some reason -CC
314 * does not work properly without it (input is not recognised).
316 * "sendfd" is dropped later in client_dispatch_wait().
318 if (pledge(
319 "stdio rpath wpath cpath unix sendfd proc exec tty",
320 NULL) != 0)
321 fatal("pledge failed");
323 /* Load terminfo entry if any. */
324 if (isatty(STDIN_FILENO) &&
325 *termname != '\0' &&
326 tty_term_read_list(termname, STDIN_FILENO, &caps, &ncaps,
327 &cause) != 0) {
328 fprintf(stderr, "%s\n", cause);
329 free(cause);
330 return (1);
333 /* Free stuff that is not used in the client. */
334 if (ptm_fd != -1)
335 close(ptm_fd);
336 options_free(global_options);
337 options_free(global_s_options);
338 options_free(global_w_options);
339 environ_free(global_environ);
341 /* Set up control mode. */
342 if (client_flags & CLIENT_CONTROLCONTROL) {
343 if (tcgetattr(STDIN_FILENO, &saved_tio) != 0) {
344 fprintf(stderr, "tcgetattr failed: %s\n",
345 strerror(errno));
346 return (1);
348 cfmakeraw(&tio);
349 tio.c_iflag = ICRNL|IXANY;
350 tio.c_oflag = OPOST|ONLCR;
351 tio.c_lflag = NOKERNINFO;
352 tio.c_cflag = CREAD|CS8|HUPCL;
353 tio.c_cc[VMIN] = 1;
354 tio.c_cc[VTIME] = 0;
355 cfsetispeed(&tio, cfgetispeed(&saved_tio));
356 cfsetospeed(&tio, cfgetospeed(&saved_tio));
357 tcsetattr(STDIN_FILENO, TCSANOW, &tio);
360 /* Send identify messages. */
361 client_send_identify(ttynam, termname, caps, ncaps, cwd, feat);
362 tty_term_free_list(caps, ncaps);
363 proc_flush_peer(client_peer);
365 /* Send first command. */
366 if (msg == MSG_COMMAND) {
367 /* How big is the command? */
368 size = 0;
369 for (i = 0; i < argc; i++)
370 size += strlen(argv[i]) + 1;
371 if (size > MAX_IMSGSIZE - (sizeof *data)) {
372 fprintf(stderr, "command too long\n");
373 return (1);
375 data = xmalloc((sizeof *data) + size);
377 /* Prepare command for server. */
378 data->argc = argc;
379 if (cmd_pack_argv(argc, argv, (char *)(data + 1), size) != 0) {
380 fprintf(stderr, "command too long\n");
381 free(data);
382 return (1);
384 size += sizeof *data;
386 /* Send the command. */
387 if (proc_send(client_peer, msg, -1, data, size) != 0) {
388 fprintf(stderr, "failed to send command\n");
389 free(data);
390 return (1);
392 free(data);
393 } else if (msg == MSG_SHELL)
394 proc_send(client_peer, msg, -1, NULL, 0);
396 /* Start main loop. */
397 proc_loop(client_proc, NULL);
399 /* Run command if user requested exec, instead of exiting. */
400 if (client_exittype == MSG_EXEC) {
401 if (client_flags & CLIENT_CONTROLCONTROL)
402 tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
403 client_exec(client_execshell, client_execcmd);
406 /* Restore streams to blocking. */
407 setblocking(STDIN_FILENO, 1);
408 setblocking(STDOUT_FILENO, 1);
409 setblocking(STDERR_FILENO, 1);
411 /* Print the exit message, if any, and exit. */
412 if (client_attached) {
413 if (client_exitreason != CLIENT_EXIT_NONE)
414 printf("[%s]\n", client_exit_message());
416 ppid = getppid();
417 if (client_exittype == MSG_DETACHKILL && ppid > 1)
418 kill(ppid, SIGHUP);
419 } else if (client_flags & CLIENT_CONTROL) {
420 if (client_exitreason != CLIENT_EXIT_NONE)
421 printf("%%exit %s\n", client_exit_message());
422 else
423 printf("%%exit\n");
424 fflush(stdout);
425 if (client_flags & CLIENT_CONTROL_WAITEXIT) {
426 setvbuf(stdin, NULL, _IOLBF, 0);
427 for (;;) {
428 linelen = getline(&line, &linesize, stdin);
429 if (linelen <= 1)
430 break;
432 free(line);
434 if (client_flags & CLIENT_CONTROLCONTROL) {
435 printf("\033\\");
436 fflush(stdout);
437 tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
439 } else if (client_exitreason != CLIENT_EXIT_NONE)
440 fprintf(stderr, "%s\n", client_exit_message());
441 return (client_exitval);
444 /* Send identify messages to server. */
445 static void
446 client_send_identify(const char *ttynam, const char *termname, char **caps,
447 u_int ncaps, const char *cwd, int feat)
449 char **ss;
450 size_t sslen;
451 int fd, flags = client_flags;
452 pid_t pid;
453 u_int i;
455 proc_send(client_peer, MSG_IDENTIFY_FLAGS, -1, &flags, sizeof flags);
456 proc_send(client_peer, MSG_IDENTIFY_LONGFLAGS, -1, &client_flags,
457 sizeof client_flags);
459 proc_send(client_peer, MSG_IDENTIFY_TERM, -1, termname,
460 strlen(termname) + 1);
461 proc_send(client_peer, MSG_IDENTIFY_FEATURES, -1, &feat, sizeof feat);
463 proc_send(client_peer, MSG_IDENTIFY_TTYNAME, -1, ttynam,
464 strlen(ttynam) + 1);
465 proc_send(client_peer, MSG_IDENTIFY_CWD, -1, cwd, strlen(cwd) + 1);
467 for (i = 0; i < ncaps; i++) {
468 proc_send(client_peer, MSG_IDENTIFY_TERMINFO, -1,
469 caps[i], strlen(caps[i]) + 1);
472 if ((fd = dup(STDIN_FILENO)) == -1)
473 fatal("dup failed");
474 proc_send(client_peer, MSG_IDENTIFY_STDIN, fd, NULL, 0);
475 if ((fd = dup(STDOUT_FILENO)) == -1)
476 fatal("dup failed");
477 proc_send(client_peer, MSG_IDENTIFY_STDOUT, fd, NULL, 0);
479 pid = getpid();
480 proc_send(client_peer, MSG_IDENTIFY_CLIENTPID, -1, &pid, sizeof pid);
482 for (ss = environ; *ss != NULL; ss++) {
483 sslen = strlen(*ss) + 1;
484 if (sslen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
485 continue;
486 proc_send(client_peer, MSG_IDENTIFY_ENVIRON, -1, *ss, sslen);
489 proc_send(client_peer, MSG_IDENTIFY_DONE, -1, NULL, 0);
492 /* Run command in shell; used for -c. */
493 static __dead void
494 client_exec(const char *shell, const char *shellcmd)
496 const char *name, *ptr;
497 char *argv0;
499 log_debug("shell %s, command %s", shell, shellcmd);
501 ptr = strrchr(shell, '/');
502 if (ptr != NULL && *(ptr + 1) != '\0')
503 name = ptr + 1;
504 else
505 name = shell;
506 if (client_flags & CLIENT_LOGIN)
507 xasprintf(&argv0, "-%s", name);
508 else
509 xasprintf(&argv0, "%s", name);
510 setenv("SHELL", shell, 1);
512 proc_clear_signals(client_proc, 1);
514 setblocking(STDIN_FILENO, 1);
515 setblocking(STDOUT_FILENO, 1);
516 setblocking(STDERR_FILENO, 1);
517 closefrom(STDERR_FILENO + 1);
519 execl(shell, argv0, "-c", shellcmd, (char *) NULL);
520 fatal("execl failed");
523 /* Callback to handle signals in the client. */
524 static void
525 client_signal(int sig)
527 struct sigaction sigact;
528 int status;
530 log_debug("%s: %s", __func__, strsignal(sig));
531 if (sig == SIGCHLD)
532 waitpid(WAIT_ANY, &status, WNOHANG);
533 else if (!client_attached) {
534 if (sig == SIGTERM || sig == SIGHUP)
535 proc_exit(client_proc);
536 } else {
537 switch (sig) {
538 case SIGHUP:
539 client_exitreason = CLIENT_EXIT_LOST_TTY;
540 client_exitval = 1;
541 proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
542 break;
543 case SIGTERM:
544 if (!client_suspended)
545 client_exitreason = CLIENT_EXIT_TERMINATED;
546 client_exitval = 1;
547 proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
548 break;
549 case SIGWINCH:
550 proc_send(client_peer, MSG_RESIZE, -1, NULL, 0);
551 break;
552 case SIGCONT:
553 memset(&sigact, 0, sizeof sigact);
554 sigemptyset(&sigact.sa_mask);
555 sigact.sa_flags = SA_RESTART;
556 sigact.sa_handler = SIG_IGN;
557 if (sigaction(SIGTSTP, &sigact, NULL) != 0)
558 fatal("sigaction failed");
559 proc_send(client_peer, MSG_WAKEUP, -1, NULL, 0);
560 client_suspended = 0;
561 break;
566 /* Callback for file write error or close. */
567 static void
568 client_file_check_cb(__unused struct client *c, __unused const char *path,
569 __unused int error, __unused int closed, __unused struct evbuffer *buffer,
570 __unused void *data)
572 if (client_exitflag)
573 client_exit();
576 /* Callback for client read events. */
577 static void
578 client_dispatch(struct imsg *imsg, __unused void *arg)
580 if (imsg == NULL) {
581 if (!client_exitflag) {
582 client_exitreason = CLIENT_EXIT_LOST_SERVER;
583 client_exitval = 1;
585 proc_exit(client_proc);
586 return;
589 if (client_attached)
590 client_dispatch_attached(imsg);
591 else
592 client_dispatch_wait(imsg);
595 /* Process an exit message. */
596 static void
597 client_dispatch_exit_message(char *data, size_t datalen)
599 int retval;
601 if (datalen < sizeof retval && datalen != 0)
602 fatalx("bad MSG_EXIT size");
604 if (datalen >= sizeof retval) {
605 memcpy(&retval, data, sizeof retval);
606 client_exitval = retval;
609 if (datalen > sizeof retval) {
610 datalen -= sizeof retval;
611 data += sizeof retval;
613 client_exitmessage = xmalloc(datalen);
614 memcpy(client_exitmessage, data, datalen);
615 client_exitmessage[datalen - 1] = '\0';
617 client_exitreason = CLIENT_EXIT_MESSAGE_PROVIDED;
621 /* Dispatch imsgs when in wait state (before MSG_READY). */
622 static void
623 client_dispatch_wait(struct imsg *imsg)
625 char *data;
626 ssize_t datalen;
627 static int pledge_applied;
630 * "sendfd" is no longer required once all of the identify messages
631 * have been sent. We know the server won't send us anything until that
632 * point (because we don't ask it to), so we can drop "sendfd" once we
633 * get the first message from the server.
635 if (!pledge_applied) {
636 if (pledge(
637 "stdio rpath wpath cpath unix proc exec tty",
638 NULL) != 0)
639 fatal("pledge failed");
640 pledge_applied = 1;
643 data = imsg->data;
644 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
646 switch (imsg->hdr.type) {
647 case MSG_EXIT:
648 case MSG_SHUTDOWN:
649 client_dispatch_exit_message(data, datalen);
650 client_exitflag = 1;
651 client_exit();
652 break;
653 case MSG_READY:
654 if (datalen != 0)
655 fatalx("bad MSG_READY size");
657 client_attached = 1;
658 proc_send(client_peer, MSG_RESIZE, -1, NULL, 0);
659 break;
660 case MSG_VERSION:
661 if (datalen != 0)
662 fatalx("bad MSG_VERSION size");
664 fprintf(stderr, "protocol version mismatch "
665 "(client %d, server %u)\n", PROTOCOL_VERSION,
666 imsg->hdr.peerid & 0xff);
667 client_exitval = 1;
668 proc_exit(client_proc);
669 break;
670 case MSG_FLAGS:
671 if (datalen != sizeof client_flags)
672 fatalx("bad MSG_FLAGS string");
674 memcpy(&client_flags, data, sizeof client_flags);
675 log_debug("new flags are %#llx",
676 (unsigned long long)client_flags);
677 break;
678 case MSG_SHELL:
679 if (datalen == 0 || data[datalen - 1] != '\0')
680 fatalx("bad MSG_SHELL string");
682 client_exec(data, shell_command);
683 /* NOTREACHED */
684 case MSG_DETACH:
685 case MSG_DETACHKILL:
686 proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
687 break;
688 case MSG_EXITED:
689 proc_exit(client_proc);
690 break;
691 case MSG_READ_OPEN:
692 file_read_open(&client_files, client_peer, imsg, 1,
693 !(client_flags & CLIENT_CONTROL), client_file_check_cb,
694 NULL);
695 break;
696 case MSG_READ_CANCEL:
697 file_read_cancel(&client_files, imsg);
698 break;
699 case MSG_WRITE_OPEN:
700 file_write_open(&client_files, client_peer, imsg, 1,
701 !(client_flags & CLIENT_CONTROL), client_file_check_cb,
702 NULL);
703 break;
704 case MSG_WRITE:
705 file_write_data(&client_files, imsg);
706 break;
707 case MSG_WRITE_CLOSE:
708 file_write_close(&client_files, imsg);
709 break;
710 case MSG_OLDSTDERR:
711 case MSG_OLDSTDIN:
712 case MSG_OLDSTDOUT:
713 fprintf(stderr, "server version is too old for client\n");
714 proc_exit(client_proc);
715 break;
719 /* Dispatch imsgs in attached state (after MSG_READY). */
720 static void
721 client_dispatch_attached(struct imsg *imsg)
723 struct sigaction sigact;
724 char *data;
725 ssize_t datalen;
727 data = imsg->data;
728 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
730 switch (imsg->hdr.type) {
731 case MSG_FLAGS:
732 if (datalen != sizeof client_flags)
733 fatalx("bad MSG_FLAGS string");
735 memcpy(&client_flags, data, sizeof client_flags);
736 log_debug("new flags are %#llx",
737 (unsigned long long)client_flags);
738 break;
739 case MSG_DETACH:
740 case MSG_DETACHKILL:
741 if (datalen == 0 || data[datalen - 1] != '\0')
742 fatalx("bad MSG_DETACH string");
744 client_exitsession = xstrdup(data);
745 client_exittype = imsg->hdr.type;
746 if (imsg->hdr.type == MSG_DETACHKILL)
747 client_exitreason = CLIENT_EXIT_DETACHED_HUP;
748 else
749 client_exitreason = CLIENT_EXIT_DETACHED;
750 proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
751 break;
752 case MSG_EXEC:
753 if (datalen == 0 || data[datalen - 1] != '\0' ||
754 strlen(data) + 1 == (size_t)datalen)
755 fatalx("bad MSG_EXEC string");
756 client_execcmd = xstrdup(data);
757 client_execshell = xstrdup(data + strlen(data) + 1);
759 client_exittype = imsg->hdr.type;
760 proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
761 break;
762 case MSG_EXIT:
763 client_dispatch_exit_message(data, datalen);
764 if (client_exitreason == CLIENT_EXIT_NONE)
765 client_exitreason = CLIENT_EXIT_EXITED;
766 proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
767 break;
768 case MSG_EXITED:
769 if (datalen != 0)
770 fatalx("bad MSG_EXITED size");
772 proc_exit(client_proc);
773 break;
774 case MSG_SHUTDOWN:
775 if (datalen != 0)
776 fatalx("bad MSG_SHUTDOWN size");
778 proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
779 client_exitreason = CLIENT_EXIT_SERVER_EXITED;
780 client_exitval = 1;
781 break;
782 case MSG_SUSPEND:
783 if (datalen != 0)
784 fatalx("bad MSG_SUSPEND size");
786 memset(&sigact, 0, sizeof sigact);
787 sigemptyset(&sigact.sa_mask);
788 sigact.sa_flags = SA_RESTART;
789 sigact.sa_handler = SIG_DFL;
790 if (sigaction(SIGTSTP, &sigact, NULL) != 0)
791 fatal("sigaction failed");
792 client_suspended = 1;
793 kill(getpid(), SIGTSTP);
794 break;
795 case MSG_LOCK:
796 if (datalen == 0 || data[datalen - 1] != '\0')
797 fatalx("bad MSG_LOCK string");
799 system(data);
800 proc_send(client_peer, MSG_UNLOCK, -1, NULL, 0);
801 break;