Annotation of src/usr.bin/tmux/client.c, Revision 1.146
1.146 ! nicm 1: /* $OpenBSD: client.c,v 1.145 2020/05/26 08:41:47 nicm Exp $ */
1.1 nicm 2:
3: /*
1.113 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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>
20: #include <sys/socket.h>
1.144 nicm 21: #include <sys/uio.h>
1.1 nicm 22: #include <sys/un.h>
23: #include <sys/wait.h>
24:
25: #include <errno.h>
1.30 nicm 26: #include <event.h>
1.80 nicm 27: #include <fcntl.h>
1.99 nicm 28: #include <imsg.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.114 nicm 36: static struct tmuxproc *client_proc;
37: static struct tmuxpeer *client_peer;
38: static int client_flags;
39: static enum {
1.52 nicm 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.133 nicm 49: static int client_exitflag;
1.114 nicm 50: static int client_exitval;
51: static enum msgtype client_exittype;
52: static const char *client_exitsession;
1.115 nicm 53: static const char *client_execshell;
54: static const char *client_execcmd;
1.114 nicm 55: static int client_attached;
1.132 nicm 56: static struct client_files client_files = RB_INITIALIZER(&client_files);
1.114 nicm 57:
58: static __dead void client_exec(const char *,const char *);
59: static int client_get_lock(char *);
60: static int client_connect(struct event_base *, const char *, int);
1.142 nicm 61: static void client_send_identify(const char *, const char *, int);
1.114 nicm 62: static void client_signal(int);
63: static void client_dispatch(struct imsg *, void *);
64: static void client_dispatch_attached(struct imsg *);
1.121 nicm 65: static void client_dispatch_wait(struct imsg *);
1.114 nicm 66: static 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
1.110 nicm 70: * another client, so block until the lock is released and return -2 to
71: * retry. Return -1 on failure to continue and start the server anyway.
1.49 nicm 72: */
1.114 nicm 73: static int
1.49 nicm 74: client_get_lock(char *lockfile)
75: {
76: int lockfd;
77:
1.110 nicm 78: log_debug("lock file is %s", lockfile);
79:
1.109 nicm 80: if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
1.110 nicm 81: log_debug("open failed: %s", strerror(errno));
82: return (-1);
1.109 nicm 83: }
1.49 nicm 84:
1.82 nicm 85: if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
86: log_debug("flock failed: %s", strerror(errno));
87: if (errno != EAGAIN)
88: return (lockfd);
89: while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
1.49 nicm 90: /* nothing */;
91: close(lockfd);
1.110 nicm 92: return (-2);
1.49 nicm 93: }
1.82 nicm 94: log_debug("flock succeeded");
1.49 nicm 95:
96: return (lockfd);
97: }
98:
1.46 nicm 99: /* Connect client to server. */
1.114 nicm 100: static int
1.138 nicm 101: client_connect(struct event_base *base, const char *path, int flags)
1.1 nicm 102: {
1.26 nicm 103: struct sockaddr_un sa;
104: size_t size;
1.82 nicm 105: int fd, lockfd = -1, locked = 0;
106: char *lockfile = NULL;
1.1 nicm 107:
108: memset(&sa, 0, sizeof sa);
109: sa.sun_family = AF_UNIX;
110: size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
111: if (size >= sizeof sa.sun_path) {
112: errno = ENAMETOOLONG;
1.46 nicm 113: return (-1);
1.1 nicm 114: }
1.82 nicm 115: log_debug("socket is %s", path);
1.1 nicm 116:
1.49 nicm 117: retry:
1.10 nicm 118: if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1.109 nicm 119: return (-1);
1.1 nicm 120:
1.82 nicm 121: log_debug("trying connect");
1.109 nicm 122: if (connect(fd, (struct sockaddr *)&sa, sizeof sa) == -1) {
1.82 nicm 123: log_debug("connect failed: %s", strerror(errno));
1.49 nicm 124: if (errno != ECONNREFUSED && errno != ENOENT)
125: goto failed;
1.138 nicm 126: if (~flags & CLIENT_STARTSERVER)
1.46 nicm 127: goto failed;
1.49 nicm 128: close(fd);
129:
1.82 nicm 130: if (!locked) {
131: xasprintf(&lockfile, "%s.lock", path);
1.110 nicm 132: if ((lockfd = client_get_lock(lockfile)) < 0) {
133: log_debug("didn't get lock (%d)", lockfd);
134:
1.82 nicm 135: free(lockfile);
1.110 nicm 136: lockfile = NULL;
137:
138: if (lockfd == -2)
139: goto retry;
1.82 nicm 140: }
1.110 nicm 141: log_debug("got lock (%d)", lockfd);
1.82 nicm 142:
143: /*
144: * Always retry at least once, even if we got the lock,
145: * because another client could have taken the lock,
146: * started the server and released the lock between our
147: * connect() and flock().
148: */
149: locked = 1;
1.49 nicm 150: goto retry;
1.78 nicm 151: }
1.82 nicm 152:
1.110 nicm 153: if (lockfd >= 0 && unlink(path) != 0 && errno != ENOENT) {
1.78 nicm 154: free(lockfile);
155: close(lockfd);
1.49 nicm 156: return (-1);
1.78 nicm 157: }
1.138 nicm 158: fd = server_start(client_proc, flags, base, lockfd, lockfile);
1.82 nicm 159: }
1.95 nicm 160:
1.110 nicm 161: if (locked && lockfd >= 0) {
1.58 nicm 162: free(lockfile);
1.49 nicm 163: close(lockfd);
1.1 nicm 164: }
1.47 nicm 165: setblocking(fd, 0);
1.46 nicm 166: return (fd);
167:
168: failed:
1.95 nicm 169: if (locked) {
170: free(lockfile);
171: close(lockfd);
172: }
1.46 nicm 173: close(fd);
174: return (-1);
175: }
176:
1.52 nicm 177: /* Get exit string from reason number. */
178: const char *
179: client_exit_message(void)
180: {
1.73 nicm 181: static char msg[256];
182:
1.52 nicm 183: switch (client_exitreason) {
184: case CLIENT_EXIT_NONE:
185: break;
186: case CLIENT_EXIT_DETACHED:
1.73 nicm 187: if (client_exitsession != NULL) {
188: xsnprintf(msg, sizeof msg, "detached "
189: "(from session %s)", client_exitsession);
190: return (msg);
191: }
1.52 nicm 192: return ("detached");
193: case CLIENT_EXIT_DETACHED_HUP:
1.73 nicm 194: if (client_exitsession != NULL) {
195: xsnprintf(msg, sizeof msg, "detached and SIGHUP "
196: "(from session %s)", client_exitsession);
197: return (msg);
198: }
1.52 nicm 199: return ("detached and SIGHUP");
200: case CLIENT_EXIT_LOST_TTY:
201: return ("lost tty");
202: case CLIENT_EXIT_TERMINATED:
203: return ("terminated");
204: case CLIENT_EXIT_LOST_SERVER:
1.131 nicm 205: return ("server exited unexpectedly");
1.52 nicm 206: case CLIENT_EXIT_EXITED:
207: return ("exited");
208: case CLIENT_EXIT_SERVER_EXITED:
209: return ("server exited");
210: }
211: return ("unknown reason");
212: }
213:
1.133 nicm 214: /* Exit if all streams flushed. */
215: static void
216: client_exit(void)
217: {
218: struct client_file *cf;
219: size_t left;
220: int waiting = 0;
221:
222: RB_FOREACH (cf, client_files, &client_files) {
223: if (cf->event == NULL)
224: continue;
225: left = EVBUFFER_LENGTH(cf->event->output);
226: if (left != 0) {
227: waiting++;
228: log_debug("file %u %zu bytes left", cf->stream, left);
229: }
230: }
231: if (waiting == 0)
232: proc_exit(client_proc);
233: }
234:
1.46 nicm 235: /* Client main loop. */
236: int
1.142 nicm 237: client_main(struct event_base *base, int argc, char **argv, int flags, int feat)
1.46 nicm 238: {
1.129 nicm 239: struct cmd_parse_result *pr;
1.132 nicm 240: struct msg_command *data;
1.138 nicm 241: int fd, i;
1.98 nicm 242: const char *ttynam, *cwd;
1.48 nicm 243: pid_t ppid;
1.46 nicm 244: enum msgtype msg;
1.56 nicm 245: struct termios tio, saved_tio;
1.70 nicm 246: size_t size;
1.46 nicm 247:
1.99 nicm 248: /* Ignore SIGCHLD now or daemon() in the server will leave a zombie. */
249: signal(SIGCHLD, SIG_IGN);
250:
1.46 nicm 251: /* Set up the initial command. */
1.121 nicm 252: if (shell_command != NULL) {
1.46 nicm 253: msg = MSG_SHELL;
1.139 nicm 254: flags |= CLIENT_STARTSERVER;
1.46 nicm 255: } else if (argc == 0) {
256: msg = MSG_COMMAND;
1.138 nicm 257: flags |= CLIENT_STARTSERVER;
1.46 nicm 258: } else {
259: msg = MSG_COMMAND;
260:
261: /*
262: * It sucks parsing the command string twice (in client and
263: * later in server) but it is necessary to get the start server
264: * flag.
265: */
1.129 nicm 266: pr = cmd_parse_from_arguments(argc, argv, NULL);
267: if (pr->status == CMD_PARSE_SUCCESS) {
1.141 nicm 268: if (cmd_list_any_have(pr->cmdlist, CMD_STARTSERVER))
269: flags |= CLIENT_STARTSERVER;
1.129 nicm 270: cmd_list_free(pr->cmdlist);
271: } else
272: free(pr->error);
1.46 nicm 273: }
274:
1.138 nicm 275: /* Save the flags. */
276: client_flags = flags;
277:
1.109 nicm 278: /* Create client process structure (starts logging). */
1.122 nicm 279: client_proc = proc_start("client");
280: proc_set_signals(client_proc, client_signal);
1.109 nicm 281:
1.82 nicm 282: /* Initialize the client socket and start the server. */
1.138 nicm 283: fd = client_connect(base, socket_path, client_flags);
1.46 nicm 284: if (fd == -1) {
1.87 nicm 285: if (errno == ECONNREFUSED) {
286: fprintf(stderr, "no server running on %s\n",
287: socket_path);
288: } else {
289: fprintf(stderr, "error connecting to %s (%s)\n",
290: socket_path, strerror(errno));
291: }
1.46 nicm 292: return (1);
293: }
1.121 nicm 294: client_peer = proc_add_peer(client_proc, fd, client_dispatch, NULL);
1.99 nicm 295:
1.97 nicm 296: /* Save these before pledge(). */
1.128 nicm 297: if ((cwd = find_cwd()) == NULL && (cwd = find_home()) == NULL)
1.126 nicm 298: cwd = "/";
1.97 nicm 299: if ((ttynam = ttyname(STDIN_FILENO)) == NULL)
300: ttynam = "";
301:
302: /*
303: * Drop privileges for client. "proc exec" is needed for -c and for
304: * locking (which uses system(3)).
305: *
306: * "tty" is needed to restore termios(4) and also for some reason -CC
307: * does not work properly without it (input is not recognised).
308: *
309: * "sendfd" is dropped later in client_dispatch_wait().
310: */
1.132 nicm 311: if (pledge(
312: "stdio rpath wpath cpath unix sendfd proc exec tty",
313: NULL) != 0)
1.97 nicm 314: fatal("pledge failed");
315:
316: /* Free stuff that is not used in the client. */
1.119 nicm 317: if (ptm_fd != -1)
318: close(ptm_fd);
1.100 nicm 319: options_free(global_options);
320: options_free(global_s_options);
321: options_free(global_w_options);
1.101 nicm 322: environ_free(global_environ);
1.46 nicm 323:
1.132 nicm 324: /* Set up control mode. */
1.93 nicm 325: if (client_flags & CLIENT_CONTROLCONTROL) {
1.118 nicm 326: if (tcgetattr(STDIN_FILENO, &saved_tio) != 0) {
327: fprintf(stderr, "tcgetattr failed: %s\n",
328: strerror(errno));
329: return (1);
330: }
1.56 nicm 331: cfmakeraw(&tio);
332: tio.c_iflag = ICRNL|IXANY;
333: tio.c_oflag = OPOST|ONLCR;
334: tio.c_lflag = NOKERNINFO;
335: tio.c_cflag = CREAD|CS8|HUPCL;
336: tio.c_cc[VMIN] = 1;
337: tio.c_cc[VTIME] = 0;
338: cfsetispeed(&tio, cfgetispeed(&saved_tio));
339: cfsetospeed(&tio, cfgetospeed(&saved_tio));
340: tcsetattr(STDIN_FILENO, TCSANOW, &tio);
341: }
1.1 nicm 342:
1.71 nicm 343: /* Send identify messages. */
1.142 nicm 344: client_send_identify(ttynam, cwd, feat);
1.1 nicm 345:
1.46 nicm 346: /* Send first command. */
347: if (msg == MSG_COMMAND) {
1.70 nicm 348: /* How big is the command? */
349: size = 0;
350: for (i = 0; i < argc; i++)
351: size += strlen(argv[i]) + 1;
1.124 nicm 352: if (size > MAX_IMSGSIZE - (sizeof *data)) {
353: fprintf(stderr, "command too long\n");
354: return (1);
355: }
1.70 nicm 356: data = xmalloc((sizeof *data) + size);
1.46 nicm 357:
358: /* Prepare command for server. */
1.70 nicm 359: data->argc = argc;
1.83 nicm 360: if (cmd_pack_argv(argc, argv, (char *)(data + 1), size) != 0) {
1.55 nicm 361: fprintf(stderr, "command too long\n");
1.70 nicm 362: free(data);
1.46 nicm 363: return (1);
364: }
1.70 nicm 365: size += sizeof *data;
1.46 nicm 366:
1.70 nicm 367: /* Send the command. */
1.99 nicm 368: if (proc_send(client_peer, msg, -1, data, size) != 0) {
1.70 nicm 369: fprintf(stderr, "failed to send command\n");
370: free(data);
371: return (1);
372: }
373: free(data);
1.46 nicm 374: } else if (msg == MSG_SHELL)
1.99 nicm 375: proc_send(client_peer, msg, -1, NULL, 0);
1.1 nicm 376:
1.99 nicm 377: /* Start main loop. */
378: proc_loop(client_proc, NULL);
1.46 nicm 379:
1.115 nicm 380: /* Run command if user requested exec, instead of exiting. */
381: if (client_exittype == MSG_EXEC) {
382: if (client_flags & CLIENT_CONTROLCONTROL)
383: tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
384: client_exec(client_execshell, client_execcmd);
385: }
386:
1.146 ! nicm 387: /* Restore streams to blocking. */
! 388: setblocking(STDIN_FILENO, 1);
! 389: setblocking(STDOUT_FILENO, 1);
! 390: setblocking(STDERR_FILENO, 1);
! 391:
1.46 nicm 392: /* Print the exit message, if any, and exit. */
1.48 nicm 393: if (client_attached) {
1.93 nicm 394: if (client_exitreason != CLIENT_EXIT_NONE)
1.52 nicm 395: printf("[%s]\n", client_exit_message());
1.48 nicm 396:
397: ppid = getppid();
398: if (client_exittype == MSG_DETACHKILL && ppid > 1)
399: kill(ppid, SIGHUP);
1.146 ! nicm 400: } else if (client_flags & CLIENT_CONTROL) {
1.68 nicm 401: if (client_exitreason != CLIENT_EXIT_NONE)
402: printf("%%exit %s\n", client_exit_message());
403: else
404: printf("%%exit\n");
1.146 ! nicm 405: if (client_flags & CLIENT_CONTROLCONTROL) {
! 406: printf("\033\\");
! 407: tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
! 408: }
1.112 nicm 409: } else if (client_exitreason != CLIENT_EXIT_NONE)
1.109 nicm 410: fprintf(stderr, "%s\n", client_exit_message());
1.46 nicm 411: return (client_exitval);
1.1 nicm 412: }
413:
1.71 nicm 414: /* Send identify messages to server. */
1.114 nicm 415: static void
1.142 nicm 416: client_send_identify(const char *ttynam, const char *cwd, int feat)
1.26 nicm 417: {
1.90 nicm 418: const char *s;
1.71 nicm 419: char **ss;
1.91 nicm 420: size_t sslen;
1.93 nicm 421: int fd, flags = client_flags;
1.90 nicm 422: pid_t pid;
1.71 nicm 423:
1.99 nicm 424: proc_send(client_peer, MSG_IDENTIFY_FLAGS, -1, &flags, sizeof flags);
1.71 nicm 425:
426: if ((s = getenv("TERM")) == NULL)
427: s = "";
1.99 nicm 428: proc_send(client_peer, MSG_IDENTIFY_TERM, -1, s, strlen(s) + 1);
1.142 nicm 429: proc_send(client_peer, MSG_IDENTIFY_FEATURES, -1, &feat, sizeof feat);
1.71 nicm 430:
1.107 nicm 431: proc_send(client_peer, MSG_IDENTIFY_TTYNAME, -1, ttynam,
432: strlen(ttynam) + 1);
1.99 nicm 433: proc_send(client_peer, MSG_IDENTIFY_CWD, -1, cwd, strlen(cwd) + 1);
1.26 nicm 434:
1.50 nicm 435: if ((fd = dup(STDIN_FILENO)) == -1)
436: fatal("dup failed");
1.99 nicm 437: proc_send(client_peer, MSG_IDENTIFY_STDIN, fd, NULL, 0);
1.145 nicm 438: if ((fd = dup(STDOUT_FILENO)) == -1)
439: fatal("dup failed");
440: proc_send(client_peer, MSG_IDENTIFY_STDOUT, fd, NULL, 0);
1.90 nicm 441:
442: pid = getpid();
1.99 nicm 443: proc_send(client_peer, MSG_IDENTIFY_CLIENTPID, -1, &pid, sizeof pid);
1.26 nicm 444:
1.91 nicm 445: for (ss = environ; *ss != NULL; ss++) {
446: sslen = strlen(*ss) + 1;
1.107 nicm 447: if (sslen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
448: continue;
449: proc_send(client_peer, MSG_IDENTIFY_ENVIRON, -1, *ss, sslen);
1.91 nicm 450: }
1.75 nicm 451:
1.99 nicm 452: proc_send(client_peer, MSG_IDENTIFY_DONE, -1, NULL, 0);
1.30 nicm 453: }
454:
1.132 nicm 455: /* File write error callback. */
1.114 nicm 456: static void
1.132 nicm 457: client_write_error_callback(__unused struct bufferevent *bev,
458: __unused short what, void *arg)
1.54 nicm 459: {
1.132 nicm 460: struct client_file *cf = arg;
1.54 nicm 461:
1.132 nicm 462: log_debug("write error file %d", cf->stream);
463:
464: bufferevent_free(cf->event);
465: cf->event = NULL;
466:
467: close(cf->fd);
468: cf->fd = -1;
1.134 nicm 469:
470: if (client_exitflag)
471: client_exit();
1.132 nicm 472: }
473:
474: /* File write callback. */
475: static void
476: client_write_callback(__unused struct bufferevent *bev, void *arg)
477: {
478: struct client_file *cf = arg;
479:
480: if (cf->closed && EVBUFFER_LENGTH(cf->event->output) == 0) {
481: bufferevent_free(cf->event);
482: close(cf->fd);
483: RB_REMOVE(client_files, &client_files, cf);
484: file_free(cf);
485: }
1.133 nicm 486:
487: if (client_exitflag)
488: client_exit();
1.132 nicm 489: }
490:
491: /* Open write file. */
492: static void
493: client_write_open(void *data, size_t datalen)
494: {
495: struct msg_write_open *msg = data;
1.135 nicm 496: const char *path;
1.132 nicm 497: struct msg_write_ready reply;
498: struct client_file find, *cf;
499: const int flags = O_NONBLOCK|O_WRONLY|O_CREAT;
500: int error = 0;
501:
1.135 nicm 502: if (datalen < sizeof *msg)
1.132 nicm 503: fatalx("bad MSG_WRITE_OPEN size");
1.135 nicm 504: if (datalen == sizeof *msg)
505: path = "-";
506: else
507: path = (const char *)(msg + 1);
508: log_debug("open write file %d %s", msg->stream, path);
1.132 nicm 509:
510: find.stream = msg->stream;
511: if ((cf = RB_FIND(client_files, &client_files, &find)) == NULL) {
512: cf = file_create(NULL, msg->stream, NULL, NULL);
513: RB_INSERT(client_files, &client_files, cf);
514: } else {
515: error = EBADF;
516: goto reply;
517: }
518: if (cf->closed) {
519: error = EBADF;
520: goto reply;
521: }
522:
523: cf->fd = -1;
524: if (msg->fd == -1)
1.135 nicm 525: cf->fd = open(path, msg->flags|flags, 0644);
1.132 nicm 526: else {
527: if (msg->fd != STDOUT_FILENO && msg->fd != STDERR_FILENO)
528: errno = EBADF;
529: else {
530: cf->fd = dup(msg->fd);
1.143 nicm 531: if (~client_flags & CLIENT_CONTROL)
1.132 nicm 532: close(msg->fd); /* can only be used once */
533: }
534: }
535: if (cf->fd == -1) {
536: error = errno;
537: goto reply;
538: }
539:
540: cf->event = bufferevent_new(cf->fd, NULL, client_write_callback,
541: client_write_error_callback, cf);
542: bufferevent_enable(cf->event, EV_WRITE);
543: goto reply;
544:
545: reply:
546: reply.stream = msg->stream;
547: reply.error = error;
548: proc_send(client_peer, MSG_WRITE_READY, -1, &reply, sizeof reply);
549: }
550:
551: /* Write to client file. */
552: static void
553: client_write_data(void *data, size_t datalen)
554: {
555: struct msg_write_data *msg = data;
556: struct client_file find, *cf;
1.135 nicm 557: size_t size = datalen - sizeof *msg;
1.132 nicm 558:
1.135 nicm 559: if (datalen < sizeof *msg)
1.132 nicm 560: fatalx("bad MSG_WRITE size");
561: find.stream = msg->stream;
562: if ((cf = RB_FIND(client_files, &client_files, &find)) == NULL)
563: fatalx("unknown stream number");
1.135 nicm 564: log_debug("write %zu to file %d", size, cf->stream);
1.132 nicm 565:
566: if (cf->event != NULL)
1.135 nicm 567: bufferevent_write(cf->event, msg + 1, size);
1.132 nicm 568: }
569:
570: /* Close client file. */
571: static void
572: client_write_close(void *data, size_t datalen)
573: {
574: struct msg_write_close *msg = data;
575: struct client_file find, *cf;
1.54 nicm 576:
1.132 nicm 577: if (datalen != sizeof *msg)
578: fatalx("bad MSG_WRITE_CLOSE size");
579: find.stream = msg->stream;
580: if ((cf = RB_FIND(client_files, &client_files, &find)) == NULL)
581: fatalx("unknown stream number");
582: log_debug("close file %d", cf->stream);
583:
584: if (cf->event == NULL || EVBUFFER_LENGTH(cf->event->output) == 0) {
585: if (cf->event != NULL)
586: bufferevent_free(cf->event);
587: if (cf->fd != -1)
588: close(cf->fd);
589: RB_REMOVE(client_files, &client_files, cf);
590: file_free(cf);
591: }
1.54 nicm 592: }
593:
1.132 nicm 594: /* File read callback. */
1.114 nicm 595: static void
1.132 nicm 596: client_read_callback(__unused struct bufferevent *bev, void *arg)
1.57 nicm 597: {
1.132 nicm 598: struct client_file *cf = arg;
599: void *bdata;
600: size_t bsize;
1.135 nicm 601: struct msg_read_data *msg;
602: size_t msglen;
1.132 nicm 603:
1.135 nicm 604: msg = xmalloc(sizeof *msg);
1.132 nicm 605: for (;;) {
606: bdata = EVBUFFER_DATA(cf->event->input);
607: bsize = EVBUFFER_LENGTH(cf->event->input);
1.57 nicm 608:
1.132 nicm 609: if (bsize == 0)
1.57 nicm 610: break;
1.136 nicm 611: if (bsize > MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof *msg)
612: bsize = MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof *msg;
1.132 nicm 613: log_debug("read %zu from file %d", bsize, cf->stream);
614:
1.135 nicm 615: msglen = (sizeof *msg) + bsize;
616: msg = xrealloc(msg, msglen);
617: msg->stream = cf->stream;
618: memcpy(msg + 1, bdata, bsize);
619: proc_send(client_peer, MSG_READ, -1, msg, msglen);
1.132 nicm 620:
621: evbuffer_drain(cf->event->input, bsize);
622: }
1.135 nicm 623: free(msg);
1.132 nicm 624: }
625:
626: /* File read error callback. */
627: static void
628: client_read_error_callback(__unused struct bufferevent *bev,
629: __unused short what, void *arg)
630: {
631: struct client_file *cf = arg;
632: struct msg_read_done msg;
633:
634: log_debug("read error file %d", cf->stream);
635:
636: msg.stream = cf->stream;
637: msg.error = 0;
638: proc_send(client_peer, MSG_READ_DONE, -1, &msg, sizeof msg);
639:
640: bufferevent_free(cf->event);
641: close(cf->fd);
642: RB_REMOVE(client_files, &client_files, cf);
643: file_free(cf);
644: }
645:
646: /* Open read file. */
647: static void
648: client_read_open(void *data, size_t datalen)
649: {
650: struct msg_read_open *msg = data;
1.135 nicm 651: const char *path;
1.132 nicm 652: struct msg_read_done reply;
653: struct client_file find, *cf;
654: const int flags = O_NONBLOCK|O_RDONLY;
1.140 nicm 655: int error;
1.132 nicm 656:
1.135 nicm 657: if (datalen < sizeof *msg)
1.132 nicm 658: fatalx("bad MSG_READ_OPEN size");
1.135 nicm 659: if (datalen == sizeof *msg)
660: path = "-";
661: else
662: path = (const char *)(msg + 1);
663: log_debug("open read file %d %s", msg->stream, path);
1.132 nicm 664:
665: find.stream = msg->stream;
666: if ((cf = RB_FIND(client_files, &client_files, &find)) == NULL) {
667: cf = file_create(NULL, msg->stream, NULL, NULL);
668: RB_INSERT(client_files, &client_files, cf);
669: } else {
670: error = EBADF;
671: goto reply;
1.57 nicm 672: }
1.132 nicm 673: if (cf->closed) {
674: error = EBADF;
675: goto reply;
676: }
677:
678: cf->fd = -1;
679: if (msg->fd == -1)
1.135 nicm 680: cf->fd = open(path, flags);
1.132 nicm 681: else {
682: if (msg->fd != STDIN_FILENO)
683: errno = EBADF;
684: else {
685: cf->fd = dup(msg->fd);
1.143 nicm 686: if (~client_flags & CLIENT_CONTROL)
687: close(msg->fd); /* can only be used once */
1.132 nicm 688: }
689: }
690: if (cf->fd == -1) {
691: error = errno;
692: goto reply;
693: }
694:
695: cf->event = bufferevent_new(cf->fd, client_read_callback, NULL,
696: client_read_error_callback, cf);
697: bufferevent_enable(cf->event, EV_READ);
698: return;
699:
700: reply:
701: reply.stream = msg->stream;
702: reply.error = error;
703: proc_send(client_peer, MSG_READ_DONE, -1, &reply, sizeof reply);
1.57 nicm 704: }
705:
1.93 nicm 706: /* Run command in shell; used for -c. */
1.114 nicm 707: static __dead void
1.111 nicm 708: client_exec(const char *shell, const char *shellcmd)
1.93 nicm 709: {
710: const char *name, *ptr;
711: char *argv0;
712:
1.111 nicm 713: log_debug("shell %s, command %s", shell, shellcmd);
1.93 nicm 714:
715: ptr = strrchr(shell, '/');
716: if (ptr != NULL && *(ptr + 1) != '\0')
717: name = ptr + 1;
718: else
719: name = shell;
720: if (client_flags & CLIENT_LOGIN)
721: xasprintf(&argv0, "-%s", name);
722: else
723: xasprintf(&argv0, "%s", name);
724: setenv("SHELL", shell, 1);
725:
1.123 nicm 726: proc_clear_signals(client_proc, 1);
727:
1.93 nicm 728: setblocking(STDIN_FILENO, 1);
729: setblocking(STDOUT_FILENO, 1);
730: setblocking(STDERR_FILENO, 1);
731: closefrom(STDERR_FILENO + 1);
732:
1.111 nicm 733: execl(shell, argv0, "-c", shellcmd, (char *) NULL);
1.93 nicm 734: fatal("execl failed");
735: }
736:
1.99 nicm 737: /* Callback to handle signals in the client. */
1.114 nicm 738: static void
1.99 nicm 739: client_signal(int sig)
740: {
741: struct sigaction sigact;
742: int status;
743:
744: if (sig == SIGCHLD)
745: waitpid(WAIT_ANY, &status, WNOHANG);
746: else if (!client_attached) {
747: if (sig == SIGTERM)
748: proc_exit(client_proc);
749: } else {
750: switch (sig) {
751: case SIGHUP:
752: client_exitreason = CLIENT_EXIT_LOST_TTY;
753: client_exitval = 1;
754: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
755: break;
756: case SIGTERM:
757: client_exitreason = CLIENT_EXIT_TERMINATED;
758: client_exitval = 1;
759: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
760: break;
761: case SIGWINCH:
762: proc_send(client_peer, MSG_RESIZE, -1, NULL, 0);
763: break;
764: case SIGCONT:
765: memset(&sigact, 0, sizeof sigact);
766: sigemptyset(&sigact.sa_mask);
767: sigact.sa_flags = SA_RESTART;
768: sigact.sa_handler = SIG_IGN;
769: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
770: fatal("sigaction failed");
771: proc_send(client_peer, MSG_WAKEUP, -1, NULL, 0);
772: break;
773: }
774: }
775: }
776:
777: /* Callback for client read events. */
1.114 nicm 778: static void
1.121 nicm 779: client_dispatch(struct imsg *imsg, __unused void *arg)
1.99 nicm 780: {
781: if (imsg == NULL) {
782: client_exitreason = CLIENT_EXIT_LOST_SERVER;
783: client_exitval = 1;
1.109 nicm 784: proc_exit(client_proc);
785: return;
786: }
787:
788: if (client_attached)
1.99 nicm 789: client_dispatch_attached(imsg);
790: else
1.121 nicm 791: client_dispatch_wait(imsg);
1.99 nicm 792: }
793:
1.46 nicm 794: /* Dispatch imsgs when in wait state (before MSG_READY). */
1.114 nicm 795: static void
1.121 nicm 796: client_dispatch_wait(struct imsg *imsg)
1.46 nicm 797: {
1.132 nicm 798: char *data;
799: ssize_t datalen;
800: int retval;
801: static int pledge_applied;
1.97 nicm 802:
803: /*
804: * "sendfd" is no longer required once all of the identify messages
805: * have been sent. We know the server won't send us anything until that
806: * point (because we don't ask it to), so we can drop "sendfd" once we
807: * get the first message from the server.
808: */
809: if (!pledge_applied) {
1.132 nicm 810: if (pledge(
811: "stdio rpath wpath cpath unix proc exec tty",
812: NULL) != 0)
1.97 nicm 813: fatal("pledge failed");
814: pledge_applied = 1;
1.132 nicm 815: }
1.46 nicm 816:
1.99 nicm 817: data = imsg->data;
818: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.60 nicm 819:
1.99 nicm 820: switch (imsg->hdr.type) {
821: case MSG_EXIT:
822: case MSG_SHUTDOWN:
823: if (datalen != sizeof retval && datalen != 0)
824: fatalx("bad MSG_EXIT size");
825: if (datalen == sizeof retval) {
826: memcpy(&retval, data, sizeof retval);
827: client_exitval = retval;
828: }
1.133 nicm 829: client_exitflag = 1;
830: client_exit();
1.99 nicm 831: break;
832: case MSG_READY:
833: if (datalen != 0)
834: fatalx("bad MSG_READY size");
1.54 nicm 835:
1.99 nicm 836: client_attached = 1;
837: proc_send(client_peer, MSG_RESIZE, -1, NULL, 0);
838: break;
839: case MSG_VERSION:
840: if (datalen != 0)
841: fatalx("bad MSG_VERSION size");
842:
843: fprintf(stderr, "protocol version mismatch "
844: "(client %d, server %u)\n", PROTOCOL_VERSION,
1.103 nicm 845: imsg->hdr.peerid & 0xff);
1.99 nicm 846: client_exitval = 1;
847: proc_exit(client_proc);
848: break;
849: case MSG_SHELL:
850: if (datalen == 0 || data[datalen - 1] != '\0')
851: fatalx("bad MSG_SHELL string");
852:
1.121 nicm 853: client_exec(data, shell_command);
1.99 nicm 854: /* NOTREACHED */
855: case MSG_DETACH:
856: case MSG_DETACHKILL:
857: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
858: break;
859: case MSG_EXITED:
860: proc_exit(client_proc);
1.132 nicm 861: break;
862: case MSG_READ_OPEN:
863: client_read_open(data, datalen);
864: break;
865: case MSG_WRITE_OPEN:
866: client_write_open(data, datalen);
867: break;
868: case MSG_WRITE:
869: client_write_data(data, datalen);
870: break;
871: case MSG_WRITE_CLOSE:
872: client_write_close(data, datalen);
1.137 nicm 873: break;
874: case MSG_OLDSTDERR:
875: case MSG_OLDSTDIN:
876: case MSG_OLDSTDOUT:
877: fprintf(stderr, "server version is too old for client\n");
878: proc_exit(client_proc);
1.99 nicm 879: break;
1.46 nicm 880: }
881: }
882:
883: /* Dispatch imsgs in attached state (after MSG_READY). */
1.114 nicm 884: static void
1.99 nicm 885: client_dispatch_attached(struct imsg *imsg)
1.9 nicm 886: {
1.69 nicm 887: struct sigaction sigact;
888: char *data;
1.99 nicm 889: ssize_t datalen;
1.9 nicm 890:
1.99 nicm 891: data = imsg->data;
892: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.12 nicm 893:
1.99 nicm 894: switch (imsg->hdr.type) {
895: case MSG_DETACH:
896: case MSG_DETACHKILL:
897: if (datalen == 0 || data[datalen - 1] != '\0')
898: fatalx("bad MSG_DETACH string");
899:
900: client_exitsession = xstrdup(data);
901: client_exittype = imsg->hdr.type;
902: if (imsg->hdr.type == MSG_DETACHKILL)
903: client_exitreason = CLIENT_EXIT_DETACHED_HUP;
904: else
905: client_exitreason = CLIENT_EXIT_DETACHED;
1.115 nicm 906: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
907: break;
908: case MSG_EXEC:
909: if (datalen == 0 || data[datalen - 1] != '\0' ||
910: strlen(data) + 1 == (size_t)datalen)
911: fatalx("bad MSG_EXEC string");
912: client_execcmd = xstrdup(data);
913: client_execshell = xstrdup(data + strlen(data) + 1);
914:
915: client_exittype = imsg->hdr.type;
1.99 nicm 916: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
917: break;
918: case MSG_EXIT:
919: if (datalen != 0 && datalen != sizeof (int))
920: fatalx("bad MSG_EXIT size");
1.9 nicm 921:
1.99 nicm 922: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
923: client_exitreason = CLIENT_EXIT_EXITED;
924: break;
925: case MSG_EXITED:
926: if (datalen != 0)
927: fatalx("bad MSG_EXITED size");
1.9 nicm 928:
1.99 nicm 929: proc_exit(client_proc);
930: break;
931: case MSG_SHUTDOWN:
932: if (datalen != 0)
933: fatalx("bad MSG_SHUTDOWN size");
934:
935: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
936: client_exitreason = CLIENT_EXIT_SERVER_EXITED;
937: client_exitval = 1;
938: break;
939: case MSG_SUSPEND:
940: if (datalen != 0)
941: fatalx("bad MSG_SUSPEND size");
942:
943: memset(&sigact, 0, sizeof sigact);
944: sigemptyset(&sigact.sa_mask);
945: sigact.sa_flags = SA_RESTART;
946: sigact.sa_handler = SIG_DFL;
947: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
948: fatal("sigaction failed");
949: kill(getpid(), SIGTSTP);
950: break;
951: case MSG_LOCK:
952: if (datalen == 0 || data[datalen - 1] != '\0')
953: fatalx("bad MSG_LOCK string");
1.9 nicm 954:
1.99 nicm 955: system(data);
956: proc_send(client_peer, MSG_UNLOCK, -1, NULL, 0);
957: break;
1.9 nicm 958: }
1.1 nicm 959: }