Annotation of src/usr.bin/tmux/client.c, Revision 1.89
1.89 ! nicm 1: /* $OpenBSD: client.c,v 1.88 2015/04/24 20:58:44 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.71 nicm 351: const char *s;
352: char **ss;
353: int fd;
354:
355: client_write_one(MSG_IDENTIFY_FLAGS, -1, &flags, sizeof flags);
356:
357: if ((s = getenv("TERM")) == NULL)
358: s = "";
359: client_write_one(MSG_IDENTIFY_TERM, -1, s, strlen(s) + 1);
360:
361: if ((s = ttyname(STDIN_FILENO)) == NULL)
362: s = "";
363: client_write_one(MSG_IDENTIFY_TTYNAME, -1, s, strlen(s) + 1);
364:
365: if ((fd = open(".", O_RDONLY)) == -1)
366: fd = open("/", O_RDONLY);
367: client_write_one(MSG_IDENTIFY_CWD, fd, NULL, 0);
1.26 nicm 368:
1.50 nicm 369: if ((fd = dup(STDIN_FILENO)) == -1)
370: fatal("dup failed");
1.71 nicm 371: client_write_one(MSG_IDENTIFY_STDIN, fd, NULL, 0);
1.26 nicm 372:
1.71 nicm 373: for (ss = environ; *ss != NULL; ss++)
374: client_write_one(MSG_IDENTIFY_ENVIRON, -1, *ss, strlen(*ss) + 1);
1.75 nicm 375:
1.71 nicm 376: client_write_one(MSG_IDENTIFY_DONE, -1, NULL, 0);
1.11 nicm 377:
1.71 nicm 378: client_update_event();
1.11 nicm 379: }
380:
1.70 nicm 381: /* Helper to send one message. */
382: int
383: client_write_one(enum msgtype type, int fd, const void *buf, size_t len)
384: {
385: int retval;
386:
387: retval = imsg_compose(&client_ibuf, type, PROTOCOL_VERSION, -1, fd,
1.83 nicm 388: (void *)buf, len);
1.70 nicm 389: if (retval != 1)
390: return (-1);
391: return (0);
392: }
393:
1.46 nicm 394: /* Write a message to the server without a file descriptor. */
1.70 nicm 395: int
396: client_write_server(enum msgtype type, const void *buf, size_t len)
1.25 nicm 397: {
1.70 nicm 398: int retval;
399:
400: retval = client_write_one(type, -1, buf, len);
401: if (retval == 0)
402: client_update_event();
403: return (retval);
1.25 nicm 404: }
405:
1.46 nicm 406: /* Update client event based on whether it needs to read or read and write. */
1.31 nicm 407: void
408: client_update_event(void)
409: {
410: short events;
411:
412: event_del(&client_event);
413: events = EV_READ;
414: if (client_ibuf.w.queued > 0)
415: events |= EV_WRITE;
1.46 nicm 416: event_set(
417: &client_event, client_ibuf.fd, events, client_callback, shell_cmd);
1.31 nicm 418: event_add(&client_event, NULL);
419: }
420:
1.46 nicm 421: /* Callback to handle signals in the client. */
1.30 nicm 422: void
1.31 nicm 423: client_signal(int sig, unused short events, unused void *data)
1.30 nicm 424: {
1.46 nicm 425: struct sigaction sigact;
426: int status;
1.30 nicm 427:
1.84 nicm 428: if (sig == SIGCHLD)
429: waitpid(WAIT_ANY, &status, WNOHANG);
430: else if (!client_attached) {
431: if (sig == SIGTERM)
1.46 nicm 432: event_loopexit(NULL);
433: } else {
434: switch (sig) {
435: case SIGHUP:
1.52 nicm 436: client_exitreason = CLIENT_EXIT_LOST_TTY;
1.46 nicm 437: client_exitval = 1;
438: client_write_server(MSG_EXITING, NULL, 0);
439: break;
440: case SIGTERM:
1.52 nicm 441: client_exitreason = CLIENT_EXIT_TERMINATED;
1.46 nicm 442: client_exitval = 1;
443: client_write_server(MSG_EXITING, NULL, 0);
444: break;
445: case SIGWINCH:
446: client_write_server(MSG_RESIZE, NULL, 0);
447: break;
448: case SIGCONT:
449: memset(&sigact, 0, sizeof sigact);
450: sigemptyset(&sigact.sa_mask);
451: sigact.sa_flags = SA_RESTART;
452: sigact.sa_handler = SIG_IGN;
453: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
454: fatal("sigaction failed");
455: client_write_server(MSG_WAKEUP, NULL, 0);
456: break;
457: }
1.30 nicm 458: }
459:
1.31 nicm 460: client_update_event();
1.30 nicm 461: }
462:
1.46 nicm 463: /* Callback for client imsg read events. */
1.30 nicm 464: void
1.46 nicm 465: client_callback(unused int fd, short events, void *data)
1.30 nicm 466: {
1.33 nicm 467: ssize_t n;
1.46 nicm 468: int retval;
1.30 nicm 469:
470: if (events & EV_READ) {
471: if ((n = imsg_read(&client_ibuf)) == -1 || n == 0)
472: goto lost_server;
1.46 nicm 473: if (client_attached)
474: retval = client_dispatch_attached();
475: else
476: retval = client_dispatch_wait(data);
477: if (retval != 0) {
1.30 nicm 478: event_loopexit(NULL);
479: return;
1.35 nicm 480: }
1.30 nicm 481: }
1.35 nicm 482:
1.30 nicm 483: if (events & EV_WRITE) {
1.81 krw 484: if (msgbuf_write(&client_ibuf.w) <= 0 && errno != EAGAIN)
1.30 nicm 485: goto lost_server;
486: }
487:
1.31 nicm 488: client_update_event();
1.30 nicm 489: return;
490:
491: lost_server:
1.52 nicm 492: client_exitreason = CLIENT_EXIT_LOST_SERVER;
1.32 nicm 493: client_exitval = 1;
1.30 nicm 494: event_loopexit(NULL);
495: }
496:
1.54 nicm 497: /* Callback for client stdin read events. */
498: void
499: client_stdin_callback(unused int fd, unused short events, unused void *data1)
500: {
501: struct msg_stdin_data data;
502:
503: data.size = read(STDIN_FILENO, data.data, sizeof data.data);
504: if (data.size < 0 && (errno == EINTR || errno == EAGAIN))
505: return;
506:
507: client_write_server(MSG_STDIN, &data, sizeof data);
508: if (data.size <= 0)
509: event_del(&client_stdin);
510: client_update_event();
511: }
512:
1.57 nicm 513: /* Force write to file descriptor. */
514: void
515: client_write(int fd, const char *data, size_t size)
516: {
517: ssize_t used;
518:
519: while (size != 0) {
520: used = write(fd, data, size);
521: if (used == -1) {
522: if (errno == EINTR || errno == EAGAIN)
523: continue;
524: break;
525: }
526: data += used;
527: size -= used;
528: }
529: }
530:
1.46 nicm 531: /* Dispatch imsgs when in wait state (before MSG_READY). */
532: int
1.69 nicm 533: client_dispatch_wait(void *data0)
1.46 nicm 534: {
1.69 nicm 535: struct imsg imsg;
536: char *data;
537: ssize_t n, datalen;
538: struct msg_stdout_data stdoutdata;
539: struct msg_stderr_data stderrdata;
540: int retval;
1.46 nicm 541:
542: for (;;) {
543: if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
544: fatalx("imsg_get failed");
545: if (n == 0)
546: return (0);
1.69 nicm 547:
548: data = imsg.data;
1.46 nicm 549: datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
550:
1.86 nicm 551: log_debug("got %u from server", imsg.hdr.type);
1.46 nicm 552: switch (imsg.hdr.type) {
553: case MSG_EXIT:
554: case MSG_SHUTDOWN:
1.69 nicm 555: if (datalen != sizeof retval && datalen != 0)
556: fatalx("bad MSG_EXIT size");
557: if (datalen == sizeof retval) {
558: memcpy(&retval, data, sizeof retval);
559: client_exitval = retval;
1.46 nicm 560: }
561: imsg_free(&imsg);
562: return (-1);
563: case MSG_READY:
564: if (datalen != 0)
565: fatalx("bad MSG_READY size");
566:
1.54 nicm 567: event_del(&client_stdin);
1.46 nicm 568: client_attached = 1;
1.65 nicm 569: client_write_server(MSG_RESIZE, NULL, 0);
1.60 nicm 570: break;
571: case MSG_STDIN:
572: if (datalen != 0)
573: fatalx("bad MSG_STDIN size");
574:
575: event_add(&client_stdin, NULL);
1.46 nicm 576: break;
1.54 nicm 577: case MSG_STDOUT:
578: if (datalen != sizeof stdoutdata)
1.69 nicm 579: fatalx("bad MSG_STDOUT size");
580: memcpy(&stdoutdata, data, sizeof stdoutdata);
1.54 nicm 581:
1.70 nicm 582: client_write(STDOUT_FILENO, stdoutdata.data,
583: stdoutdata.size);
1.54 nicm 584: break;
585: case MSG_STDERR:
586: if (datalen != sizeof stderrdata)
1.69 nicm 587: fatalx("bad MSG_STDERR size");
588: memcpy(&stderrdata, data, sizeof stderrdata);
1.54 nicm 589:
1.70 nicm 590: client_write(STDERR_FILENO, stderrdata.data,
591: stderrdata.size);
1.54 nicm 592: break;
1.46 nicm 593: case MSG_VERSION:
594: if (datalen != 0)
595: fatalx("bad MSG_VERSION size");
596:
1.54 nicm 597: fprintf(stderr, "protocol version mismatch "
1.86 nicm 598: "(client %d, server %u)\n", PROTOCOL_VERSION,
1.54 nicm 599: imsg.hdr.peerid);
1.46 nicm 600: client_exitval = 1;
601:
602: imsg_free(&imsg);
603: return (-1);
604: case MSG_SHELL:
1.72 nicm 605: if (datalen == 0 || data[datalen - 1] != '\0')
1.69 nicm 606: fatalx("bad MSG_SHELL string");
1.46 nicm 607:
608: clear_signals(0);
1.69 nicm 609: shell_exec(data, data0);
1.46 nicm 610: /* NOTREACHED */
1.56 nicm 611: case MSG_DETACH:
1.73 nicm 612: case MSG_DETACHKILL:
1.56 nicm 613: client_write_server(MSG_EXITING, NULL, 0);
614: break;
615: case MSG_EXITED:
616: imsg_free(&imsg);
617: return (-1);
1.46 nicm 618: }
619:
620: imsg_free(&imsg);
621: }
622: }
623:
624: /* Dispatch imsgs in attached state (after MSG_READY). */
1.9 nicm 625: int
1.46 nicm 626: client_dispatch_attached(void)
1.9 nicm 627: {
1.69 nicm 628: struct imsg imsg;
629: struct sigaction sigact;
630: char *data;
631: ssize_t n, datalen;
1.9 nicm 632:
633: for (;;) {
1.25 nicm 634: if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
1.12 nicm 635: fatalx("imsg_get failed");
636: if (n == 0)
1.9 nicm 637: return (0);
1.69 nicm 638:
639: data = imsg.data;
1.12 nicm 640: datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1.9 nicm 641:
1.86 nicm 642: log_debug("got %u from server", imsg.hdr.type);
1.12 nicm 643: switch (imsg.hdr.type) {
1.73 nicm 644: case MSG_DETACH:
1.48 nicm 645: case MSG_DETACHKILL:
1.73 nicm 646: if (datalen == 0 || data[datalen - 1] != '\0')
647: fatalx("bad MSG_DETACH string");
1.9 nicm 648:
1.73 nicm 649: client_exitsession = xstrdup(data);
1.48 nicm 650: client_exittype = imsg.hdr.type;
651: if (imsg.hdr.type == MSG_DETACHKILL)
1.52 nicm 652: client_exitreason = CLIENT_EXIT_DETACHED_HUP;
1.48 nicm 653: else
1.52 nicm 654: client_exitreason = CLIENT_EXIT_DETACHED;
1.25 nicm 655: client_write_server(MSG_EXITING, NULL, 0);
1.9 nicm 656: break;
657: case MSG_EXIT:
1.69 nicm 658: if (datalen != 0 && datalen != sizeof (int))
1.9 nicm 659: fatalx("bad MSG_EXIT size");
1.12 nicm 660:
1.25 nicm 661: client_write_server(MSG_EXITING, NULL, 0);
1.52 nicm 662: client_exitreason = CLIENT_EXIT_EXITED;
1.9 nicm 663: break;
664: case MSG_EXITED:
1.12 nicm 665: if (datalen != 0)
1.9 nicm 666: fatalx("bad MSG_EXITED size");
667:
1.12 nicm 668: imsg_free(&imsg);
1.9 nicm 669: return (-1);
670: case MSG_SHUTDOWN:
1.12 nicm 671: if (datalen != 0)
1.9 nicm 672: fatalx("bad MSG_SHUTDOWN size");
673:
1.25 nicm 674: client_write_server(MSG_EXITING, NULL, 0);
1.52 nicm 675: client_exitreason = CLIENT_EXIT_SERVER_EXITED;
1.32 nicm 676: client_exitval = 1;
1.9 nicm 677: break;
678: case MSG_SUSPEND:
1.12 nicm 679: if (datalen != 0)
1.9 nicm 680: fatalx("bad MSG_SUSPEND size");
681:
1.30 nicm 682: memset(&sigact, 0, sizeof sigact);
683: sigemptyset(&sigact.sa_mask);
684: sigact.sa_flags = SA_RESTART;
685: sigact.sa_handler = SIG_DFL;
686: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
687: fatal("sigaction failed");
688: kill(getpid(), SIGTSTP);
1.21 nicm 689: break;
690: case MSG_LOCK:
1.72 nicm 691: if (datalen == 0 || data[datalen - 1] != '\0')
1.69 nicm 692: fatalx("bad MSG_LOCK string");
1.35 nicm 693:
1.69 nicm 694: system(data);
1.25 nicm 695: client_write_server(MSG_UNLOCK, NULL, 0);
1.9 nicm 696: break;
697: }
1.12 nicm 698:
699: imsg_free(&imsg);
1.9 nicm 700: }
1.1 nicm 701: }