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