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