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