Annotation of src/usr.bin/tmux/client.c, Revision 1.137
1.137 ! nicm 1: /* $OpenBSD: client.c,v 1.136 2019/12/16 16:09:28 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);
60: static void client_send_identify(const char *, const char *);
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.108 nicm 100: client_connect(struct event_base *base, const char *path, int start_server)
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.46 nicm 125: if (!start_server)
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.122 nicm 157: fd = server_start(client_proc, 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.121 nicm 236: client_main(struct event_base *base, int argc, char **argv, int flags)
1.46 nicm 237: {
1.129 nicm 238: struct cmd_parse_result *pr;
1.46 nicm 239: struct cmd *cmd;
1.132 nicm 240: struct msg_command *data;
1.98 nicm 241: int cmdflags, fd, i;
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.93 nicm 251: /* Save the flags. */
252: client_flags = flags;
253:
1.46 nicm 254: /* Set up the initial command. */
255: cmdflags = 0;
1.121 nicm 256: if (shell_command != NULL) {
1.46 nicm 257: msg = MSG_SHELL;
258: cmdflags = CMD_STARTSERVER;
259: } else if (argc == 0) {
260: msg = MSG_COMMAND;
1.89 nicm 261: cmdflags = CMD_STARTSERVER;
1.46 nicm 262: } else {
263: msg = MSG_COMMAND;
264:
265: /*
266: * It sucks parsing the command string twice (in client and
267: * later in server) but it is necessary to get the start server
268: * flag.
269: */
1.129 nicm 270: pr = cmd_parse_from_arguments(argc, argv, NULL);
271: if (pr->status == CMD_PARSE_SUCCESS) {
272: TAILQ_FOREACH(cmd, &pr->cmdlist->list, qentry) {
1.120 nicm 273: if (cmd->entry->flags & CMD_STARTSERVER)
274: cmdflags |= CMD_STARTSERVER;
275: }
1.129 nicm 276: cmd_list_free(pr->cmdlist);
277: } else
278: free(pr->error);
1.46 nicm 279: }
280:
1.109 nicm 281: /* Create client process structure (starts logging). */
1.122 nicm 282: client_proc = proc_start("client");
283: proc_set_signals(client_proc, client_signal);
1.109 nicm 284:
1.82 nicm 285: /* Initialize the client socket and start the server. */
1.92 nicm 286: fd = client_connect(base, socket_path, cmdflags & CMD_STARTSERVER);
1.46 nicm 287: if (fd == -1) {
1.87 nicm 288: if (errno == ECONNREFUSED) {
289: fprintf(stderr, "no server running on %s\n",
290: socket_path);
291: } else {
292: fprintf(stderr, "error connecting to %s (%s)\n",
293: socket_path, strerror(errno));
294: }
1.46 nicm 295: return (1);
296: }
1.121 nicm 297: client_peer = proc_add_peer(client_proc, fd, client_dispatch, NULL);
1.99 nicm 298:
1.97 nicm 299: /* Save these before pledge(). */
1.128 nicm 300: if ((cwd = find_cwd()) == NULL && (cwd = find_home()) == NULL)
1.126 nicm 301: cwd = "/";
1.97 nicm 302: if ((ttynam = ttyname(STDIN_FILENO)) == NULL)
303: ttynam = "";
304:
305: /*
306: * Drop privileges for client. "proc exec" is needed for -c and for
307: * locking (which uses system(3)).
308: *
309: * "tty" is needed to restore termios(4) and also for some reason -CC
310: * does not work properly without it (input is not recognised).
311: *
312: * "sendfd" is dropped later in client_dispatch_wait().
313: */
1.132 nicm 314: if (pledge(
315: "stdio rpath wpath cpath unix sendfd proc exec tty",
316: NULL) != 0)
1.97 nicm 317: fatal("pledge failed");
318:
319: /* Free stuff that is not used in the client. */
1.119 nicm 320: if (ptm_fd != -1)
321: close(ptm_fd);
1.100 nicm 322: options_free(global_options);
323: options_free(global_s_options);
324: options_free(global_w_options);
1.101 nicm 325: environ_free(global_environ);
1.46 nicm 326:
1.132 nicm 327: /* Set up control mode. */
1.93 nicm 328: if (client_flags & CLIENT_CONTROLCONTROL) {
1.118 nicm 329: if (tcgetattr(STDIN_FILENO, &saved_tio) != 0) {
330: fprintf(stderr, "tcgetattr failed: %s\n",
331: strerror(errno));
332: return (1);
333: }
1.56 nicm 334: cfmakeraw(&tio);
335: tio.c_iflag = ICRNL|IXANY;
336: tio.c_oflag = OPOST|ONLCR;
337: tio.c_lflag = NOKERNINFO;
338: tio.c_cflag = CREAD|CS8|HUPCL;
339: tio.c_cc[VMIN] = 1;
340: tio.c_cc[VTIME] = 0;
341: cfsetispeed(&tio, cfgetispeed(&saved_tio));
342: cfsetospeed(&tio, cfgetospeed(&saved_tio));
343: tcsetattr(STDIN_FILENO, TCSANOW, &tio);
344: }
1.1 nicm 345:
1.71 nicm 346: /* Send identify messages. */
1.98 nicm 347: client_send_identify(ttynam, cwd);
1.1 nicm 348:
1.46 nicm 349: /* Send first command. */
350: if (msg == MSG_COMMAND) {
1.70 nicm 351: /* How big is the command? */
352: size = 0;
353: for (i = 0; i < argc; i++)
354: size += strlen(argv[i]) + 1;
1.124 nicm 355: if (size > MAX_IMSGSIZE - (sizeof *data)) {
356: fprintf(stderr, "command too long\n");
357: return (1);
358: }
1.70 nicm 359: data = xmalloc((sizeof *data) + size);
1.46 nicm 360:
361: /* Prepare command for server. */
1.70 nicm 362: data->argc = argc;
1.83 nicm 363: if (cmd_pack_argv(argc, argv, (char *)(data + 1), size) != 0) {
1.55 nicm 364: fprintf(stderr, "command too long\n");
1.70 nicm 365: free(data);
1.46 nicm 366: return (1);
367: }
1.70 nicm 368: size += sizeof *data;
1.46 nicm 369:
1.70 nicm 370: /* Send the command. */
1.99 nicm 371: if (proc_send(client_peer, msg, -1, data, size) != 0) {
1.70 nicm 372: fprintf(stderr, "failed to send command\n");
373: free(data);
374: return (1);
375: }
376: free(data);
1.46 nicm 377: } else if (msg == MSG_SHELL)
1.99 nicm 378: proc_send(client_peer, msg, -1, NULL, 0);
1.1 nicm 379:
1.99 nicm 380: /* Start main loop. */
381: proc_loop(client_proc, NULL);
1.46 nicm 382:
1.115 nicm 383: /* Run command if user requested exec, instead of exiting. */
384: if (client_exittype == MSG_EXEC) {
385: if (client_flags & CLIENT_CONTROLCONTROL)
386: tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
387: client_exec(client_execshell, client_execcmd);
388: }
389:
1.46 nicm 390: /* Print the exit message, if any, and exit. */
1.48 nicm 391: if (client_attached) {
1.93 nicm 392: if (client_exitreason != CLIENT_EXIT_NONE)
1.52 nicm 393: printf("[%s]\n", client_exit_message());
1.48 nicm 394:
395: ppid = getppid();
396: if (client_exittype == MSG_DETACHKILL && ppid > 1)
397: kill(ppid, SIGHUP);
1.93 nicm 398: } else if (client_flags & CLIENT_CONTROLCONTROL) {
1.68 nicm 399: if (client_exitreason != CLIENT_EXIT_NONE)
400: printf("%%exit %s\n", client_exit_message());
401: else
402: printf("%%exit\n");
403: printf("\033\\");
1.56 nicm 404: tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
1.112 nicm 405: } else if (client_exitreason != CLIENT_EXIT_NONE)
1.109 nicm 406: fprintf(stderr, "%s\n", client_exit_message());
1.54 nicm 407: setblocking(STDIN_FILENO, 1);
1.46 nicm 408: return (client_exitval);
1.1 nicm 409: }
410:
1.71 nicm 411: /* Send identify messages to server. */
1.114 nicm 412: static void
1.98 nicm 413: client_send_identify(const char *ttynam, const char *cwd)
1.26 nicm 414: {
1.90 nicm 415: const char *s;
1.71 nicm 416: char **ss;
1.91 nicm 417: size_t sslen;
1.93 nicm 418: int fd, flags = client_flags;
1.90 nicm 419: pid_t pid;
1.71 nicm 420:
1.99 nicm 421: proc_send(client_peer, MSG_IDENTIFY_FLAGS, -1, &flags, sizeof flags);
1.71 nicm 422:
423: if ((s = getenv("TERM")) == NULL)
424: s = "";
1.99 nicm 425: proc_send(client_peer, MSG_IDENTIFY_TERM, -1, s, strlen(s) + 1);
1.71 nicm 426:
1.107 nicm 427: proc_send(client_peer, MSG_IDENTIFY_TTYNAME, -1, ttynam,
428: strlen(ttynam) + 1);
1.99 nicm 429: proc_send(client_peer, MSG_IDENTIFY_CWD, -1, cwd, strlen(cwd) + 1);
1.26 nicm 430:
1.50 nicm 431: if ((fd = dup(STDIN_FILENO)) == -1)
432: fatal("dup failed");
1.99 nicm 433: proc_send(client_peer, MSG_IDENTIFY_STDIN, fd, NULL, 0);
1.90 nicm 434:
435: pid = getpid();
1.99 nicm 436: proc_send(client_peer, MSG_IDENTIFY_CLIENTPID, -1, &pid, sizeof pid);
1.26 nicm 437:
1.91 nicm 438: for (ss = environ; *ss != NULL; ss++) {
439: sslen = strlen(*ss) + 1;
1.107 nicm 440: if (sslen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
441: continue;
442: proc_send(client_peer, MSG_IDENTIFY_ENVIRON, -1, *ss, sslen);
1.91 nicm 443: }
1.75 nicm 444:
1.99 nicm 445: proc_send(client_peer, MSG_IDENTIFY_DONE, -1, NULL, 0);
1.30 nicm 446: }
447:
1.132 nicm 448: /* File write error callback. */
1.114 nicm 449: static void
1.132 nicm 450: client_write_error_callback(__unused struct bufferevent *bev,
451: __unused short what, void *arg)
1.54 nicm 452: {
1.132 nicm 453: struct client_file *cf = arg;
1.54 nicm 454:
1.132 nicm 455: log_debug("write error file %d", cf->stream);
456:
457: bufferevent_free(cf->event);
458: cf->event = NULL;
459:
460: close(cf->fd);
461: cf->fd = -1;
1.134 nicm 462:
463: if (client_exitflag)
464: client_exit();
1.132 nicm 465: }
466:
467: /* File write callback. */
468: static void
469: client_write_callback(__unused struct bufferevent *bev, void *arg)
470: {
471: struct client_file *cf = arg;
472:
473: if (cf->closed && EVBUFFER_LENGTH(cf->event->output) == 0) {
474: bufferevent_free(cf->event);
475: close(cf->fd);
476: RB_REMOVE(client_files, &client_files, cf);
477: file_free(cf);
478: }
1.133 nicm 479:
480: if (client_exitflag)
481: client_exit();
1.132 nicm 482: }
483:
484: /* Open write file. */
485: static void
486: client_write_open(void *data, size_t datalen)
487: {
488: struct msg_write_open *msg = data;
1.135 nicm 489: const char *path;
1.132 nicm 490: struct msg_write_ready reply;
491: struct client_file find, *cf;
492: const int flags = O_NONBLOCK|O_WRONLY|O_CREAT;
493: int error = 0;
494:
1.135 nicm 495: if (datalen < sizeof *msg)
1.132 nicm 496: fatalx("bad MSG_WRITE_OPEN size");
1.135 nicm 497: if (datalen == sizeof *msg)
498: path = "-";
499: else
500: path = (const char *)(msg + 1);
501: log_debug("open write file %d %s", msg->stream, path);
1.132 nicm 502:
503: find.stream = msg->stream;
504: if ((cf = RB_FIND(client_files, &client_files, &find)) == NULL) {
505: cf = file_create(NULL, msg->stream, NULL, NULL);
506: RB_INSERT(client_files, &client_files, cf);
507: } else {
508: error = EBADF;
509: goto reply;
510: }
511: if (cf->closed) {
512: error = EBADF;
513: goto reply;
514: }
515:
516: cf->fd = -1;
517: if (msg->fd == -1)
1.135 nicm 518: cf->fd = open(path, msg->flags|flags, 0644);
1.132 nicm 519: else {
520: if (msg->fd != STDOUT_FILENO && msg->fd != STDERR_FILENO)
521: errno = EBADF;
522: else {
523: cf->fd = dup(msg->fd);
524: if (client_flags & CLIENT_CONTROL)
525: close(msg->fd); /* can only be used once */
526: }
527: }
528: if (cf->fd == -1) {
529: error = errno;
530: goto reply;
531: }
532:
533: cf->event = bufferevent_new(cf->fd, NULL, client_write_callback,
534: client_write_error_callback, cf);
535: bufferevent_enable(cf->event, EV_WRITE);
536: goto reply;
537:
538: reply:
539: reply.stream = msg->stream;
540: reply.error = error;
541: proc_send(client_peer, MSG_WRITE_READY, -1, &reply, sizeof reply);
542: }
543:
544: /* Write to client file. */
545: static void
546: client_write_data(void *data, size_t datalen)
547: {
548: struct msg_write_data *msg = data;
549: struct client_file find, *cf;
1.135 nicm 550: size_t size = datalen - sizeof *msg;
1.132 nicm 551:
1.135 nicm 552: if (datalen < sizeof *msg)
1.132 nicm 553: fatalx("bad MSG_WRITE size");
554: find.stream = msg->stream;
555: if ((cf = RB_FIND(client_files, &client_files, &find)) == NULL)
556: fatalx("unknown stream number");
1.135 nicm 557: log_debug("write %zu to file %d", size, cf->stream);
1.132 nicm 558:
559: if (cf->event != NULL)
1.135 nicm 560: bufferevent_write(cf->event, msg + 1, size);
1.132 nicm 561: }
562:
563: /* Close client file. */
564: static void
565: client_write_close(void *data, size_t datalen)
566: {
567: struct msg_write_close *msg = data;
568: struct client_file find, *cf;
1.54 nicm 569:
1.132 nicm 570: if (datalen != sizeof *msg)
571: fatalx("bad MSG_WRITE_CLOSE size");
572: find.stream = msg->stream;
573: if ((cf = RB_FIND(client_files, &client_files, &find)) == NULL)
574: fatalx("unknown stream number");
575: log_debug("close file %d", cf->stream);
576:
577: if (cf->event == NULL || EVBUFFER_LENGTH(cf->event->output) == 0) {
578: if (cf->event != NULL)
579: bufferevent_free(cf->event);
580: if (cf->fd != -1)
581: close(cf->fd);
582: RB_REMOVE(client_files, &client_files, cf);
583: file_free(cf);
584: }
1.54 nicm 585: }
586:
1.132 nicm 587: /* File read callback. */
1.114 nicm 588: static void
1.132 nicm 589: client_read_callback(__unused struct bufferevent *bev, void *arg)
1.57 nicm 590: {
1.132 nicm 591: struct client_file *cf = arg;
592: void *bdata;
593: size_t bsize;
1.135 nicm 594: struct msg_read_data *msg;
595: size_t msglen;
1.132 nicm 596:
1.135 nicm 597: msg = xmalloc(sizeof *msg);
1.132 nicm 598: for (;;) {
599: bdata = EVBUFFER_DATA(cf->event->input);
600: bsize = EVBUFFER_LENGTH(cf->event->input);
1.57 nicm 601:
1.132 nicm 602: if (bsize == 0)
1.57 nicm 603: break;
1.136 nicm 604: if (bsize > MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof *msg)
605: bsize = MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof *msg;
1.132 nicm 606: log_debug("read %zu from file %d", bsize, cf->stream);
607:
1.135 nicm 608: msglen = (sizeof *msg) + bsize;
609: msg = xrealloc(msg, msglen);
610: msg->stream = cf->stream;
611: memcpy(msg + 1, bdata, bsize);
612: proc_send(client_peer, MSG_READ, -1, msg, msglen);
1.132 nicm 613:
614: evbuffer_drain(cf->event->input, bsize);
615: }
1.135 nicm 616: free(msg);
1.132 nicm 617: }
618:
619: /* File read error callback. */
620: static void
621: client_read_error_callback(__unused struct bufferevent *bev,
622: __unused short what, void *arg)
623: {
624: struct client_file *cf = arg;
625: struct msg_read_done msg;
626:
627: log_debug("read error file %d", cf->stream);
628:
629: msg.stream = cf->stream;
630: msg.error = 0;
631: proc_send(client_peer, MSG_READ_DONE, -1, &msg, sizeof msg);
632:
633: bufferevent_free(cf->event);
634: close(cf->fd);
635: RB_REMOVE(client_files, &client_files, cf);
636: file_free(cf);
637: }
638:
639: /* Open read file. */
640: static void
641: client_read_open(void *data, size_t datalen)
642: {
643: struct msg_read_open *msg = data;
1.135 nicm 644: const char *path;
1.132 nicm 645: struct msg_read_done reply;
646: struct client_file find, *cf;
647: const int flags = O_NONBLOCK|O_RDONLY;
648: int error = 0;
649:
1.135 nicm 650: if (datalen < sizeof *msg)
1.132 nicm 651: fatalx("bad MSG_READ_OPEN size");
1.135 nicm 652: if (datalen == sizeof *msg)
653: path = "-";
654: else
655: path = (const char *)(msg + 1);
656: log_debug("open read file %d %s", msg->stream, path);
1.132 nicm 657:
658: find.stream = msg->stream;
659: if ((cf = RB_FIND(client_files, &client_files, &find)) == NULL) {
660: cf = file_create(NULL, msg->stream, NULL, NULL);
661: RB_INSERT(client_files, &client_files, cf);
662: } else {
663: error = EBADF;
664: goto reply;
1.57 nicm 665: }
1.132 nicm 666: if (cf->closed) {
667: error = EBADF;
668: goto reply;
669: }
670:
671: cf->fd = -1;
672: if (msg->fd == -1)
1.135 nicm 673: cf->fd = open(path, flags);
1.132 nicm 674: else {
675: if (msg->fd != STDIN_FILENO)
676: errno = EBADF;
677: else {
678: cf->fd = dup(msg->fd);
679: close(msg->fd); /* can only be used once */
680: }
681: }
682: if (cf->fd == -1) {
683: error = errno;
684: goto reply;
685: }
686:
687: cf->event = bufferevent_new(cf->fd, client_read_callback, NULL,
688: client_read_error_callback, cf);
689: bufferevent_enable(cf->event, EV_READ);
690: return;
691:
692: reply:
693: reply.stream = msg->stream;
694: reply.error = error;
695: proc_send(client_peer, MSG_READ_DONE, -1, &reply, sizeof reply);
1.57 nicm 696: }
697:
1.93 nicm 698: /* Run command in shell; used for -c. */
1.114 nicm 699: static __dead void
1.111 nicm 700: client_exec(const char *shell, const char *shellcmd)
1.93 nicm 701: {
702: const char *name, *ptr;
703: char *argv0;
704:
1.111 nicm 705: log_debug("shell %s, command %s", shell, shellcmd);
1.93 nicm 706:
707: ptr = strrchr(shell, '/');
708: if (ptr != NULL && *(ptr + 1) != '\0')
709: name = ptr + 1;
710: else
711: name = shell;
712: if (client_flags & CLIENT_LOGIN)
713: xasprintf(&argv0, "-%s", name);
714: else
715: xasprintf(&argv0, "%s", name);
716: setenv("SHELL", shell, 1);
717:
1.123 nicm 718: proc_clear_signals(client_proc, 1);
719:
1.93 nicm 720: setblocking(STDIN_FILENO, 1);
721: setblocking(STDOUT_FILENO, 1);
722: setblocking(STDERR_FILENO, 1);
723: closefrom(STDERR_FILENO + 1);
724:
1.111 nicm 725: execl(shell, argv0, "-c", shellcmd, (char *) NULL);
1.93 nicm 726: fatal("execl failed");
727: }
728:
1.99 nicm 729: /* Callback to handle signals in the client. */
1.114 nicm 730: static void
1.99 nicm 731: client_signal(int sig)
732: {
733: struct sigaction sigact;
734: int status;
735:
736: if (sig == SIGCHLD)
737: waitpid(WAIT_ANY, &status, WNOHANG);
738: else if (!client_attached) {
739: if (sig == SIGTERM)
740: proc_exit(client_proc);
741: } else {
742: switch (sig) {
743: case SIGHUP:
744: client_exitreason = CLIENT_EXIT_LOST_TTY;
745: client_exitval = 1;
746: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
747: break;
748: case SIGTERM:
749: client_exitreason = CLIENT_EXIT_TERMINATED;
750: client_exitval = 1;
751: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
752: break;
753: case SIGWINCH:
754: proc_send(client_peer, MSG_RESIZE, -1, NULL, 0);
755: break;
756: case SIGCONT:
757: memset(&sigact, 0, sizeof sigact);
758: sigemptyset(&sigact.sa_mask);
759: sigact.sa_flags = SA_RESTART;
760: sigact.sa_handler = SIG_IGN;
761: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
762: fatal("sigaction failed");
763: proc_send(client_peer, MSG_WAKEUP, -1, NULL, 0);
764: break;
765: }
766: }
767: }
768:
769: /* Callback for client read events. */
1.114 nicm 770: static void
1.121 nicm 771: client_dispatch(struct imsg *imsg, __unused void *arg)
1.99 nicm 772: {
773: if (imsg == NULL) {
774: client_exitreason = CLIENT_EXIT_LOST_SERVER;
775: client_exitval = 1;
1.109 nicm 776: proc_exit(client_proc);
777: return;
778: }
779:
780: if (client_attached)
1.99 nicm 781: client_dispatch_attached(imsg);
782: else
1.121 nicm 783: client_dispatch_wait(imsg);
1.99 nicm 784: }
785:
1.46 nicm 786: /* Dispatch imsgs when in wait state (before MSG_READY). */
1.114 nicm 787: static void
1.121 nicm 788: client_dispatch_wait(struct imsg *imsg)
1.46 nicm 789: {
1.132 nicm 790: char *data;
791: ssize_t datalen;
792: int retval;
793: static int pledge_applied;
1.97 nicm 794:
795: /*
796: * "sendfd" is no longer required once all of the identify messages
797: * have been sent. We know the server won't send us anything until that
798: * point (because we don't ask it to), so we can drop "sendfd" once we
799: * get the first message from the server.
800: */
801: if (!pledge_applied) {
1.132 nicm 802: if (pledge(
803: "stdio rpath wpath cpath unix proc exec tty",
804: NULL) != 0)
1.97 nicm 805: fatal("pledge failed");
806: pledge_applied = 1;
1.132 nicm 807: }
1.46 nicm 808:
1.99 nicm 809: data = imsg->data;
810: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.60 nicm 811:
1.99 nicm 812: switch (imsg->hdr.type) {
813: case MSG_EXIT:
814: case MSG_SHUTDOWN:
815: if (datalen != sizeof retval && datalen != 0)
816: fatalx("bad MSG_EXIT size");
817: if (datalen == sizeof retval) {
818: memcpy(&retval, data, sizeof retval);
819: client_exitval = retval;
820: }
1.133 nicm 821: client_exitflag = 1;
822: client_exit();
1.99 nicm 823: break;
824: case MSG_READY:
825: if (datalen != 0)
826: fatalx("bad MSG_READY size");
1.54 nicm 827:
1.99 nicm 828: client_attached = 1;
829: proc_send(client_peer, MSG_RESIZE, -1, NULL, 0);
830: break;
831: case MSG_VERSION:
832: if (datalen != 0)
833: fatalx("bad MSG_VERSION size");
834:
835: fprintf(stderr, "protocol version mismatch "
836: "(client %d, server %u)\n", PROTOCOL_VERSION,
1.103 nicm 837: imsg->hdr.peerid & 0xff);
1.99 nicm 838: client_exitval = 1;
839: proc_exit(client_proc);
840: break;
841: case MSG_SHELL:
842: if (datalen == 0 || data[datalen - 1] != '\0')
843: fatalx("bad MSG_SHELL string");
844:
1.121 nicm 845: client_exec(data, shell_command);
1.99 nicm 846: /* NOTREACHED */
847: case MSG_DETACH:
848: case MSG_DETACHKILL:
849: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
850: break;
851: case MSG_EXITED:
852: proc_exit(client_proc);
1.132 nicm 853: break;
854: case MSG_READ_OPEN:
855: client_read_open(data, datalen);
856: break;
857: case MSG_WRITE_OPEN:
858: client_write_open(data, datalen);
859: break;
860: case MSG_WRITE:
861: client_write_data(data, datalen);
862: break;
863: case MSG_WRITE_CLOSE:
864: client_write_close(data, datalen);
1.137 ! nicm 865: break;
! 866: case MSG_OLDSTDERR:
! 867: case MSG_OLDSTDIN:
! 868: case MSG_OLDSTDOUT:
! 869: fprintf(stderr, "server version is too old for client\n");
! 870: proc_exit(client_proc);
1.99 nicm 871: break;
1.46 nicm 872: }
873: }
874:
875: /* Dispatch imsgs in attached state (after MSG_READY). */
1.114 nicm 876: static void
1.99 nicm 877: client_dispatch_attached(struct imsg *imsg)
1.9 nicm 878: {
1.69 nicm 879: struct sigaction sigact;
880: char *data;
1.99 nicm 881: ssize_t datalen;
1.9 nicm 882:
1.99 nicm 883: data = imsg->data;
884: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.12 nicm 885:
1.99 nicm 886: switch (imsg->hdr.type) {
887: case MSG_DETACH:
888: case MSG_DETACHKILL:
889: if (datalen == 0 || data[datalen - 1] != '\0')
890: fatalx("bad MSG_DETACH string");
891:
892: client_exitsession = xstrdup(data);
893: client_exittype = imsg->hdr.type;
894: if (imsg->hdr.type == MSG_DETACHKILL)
895: client_exitreason = CLIENT_EXIT_DETACHED_HUP;
896: else
897: client_exitreason = CLIENT_EXIT_DETACHED;
1.115 nicm 898: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
899: break;
900: case MSG_EXEC:
901: if (datalen == 0 || data[datalen - 1] != '\0' ||
902: strlen(data) + 1 == (size_t)datalen)
903: fatalx("bad MSG_EXEC string");
904: client_execcmd = xstrdup(data);
905: client_execshell = xstrdup(data + strlen(data) + 1);
906:
907: client_exittype = imsg->hdr.type;
1.99 nicm 908: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
909: break;
910: case MSG_EXIT:
911: if (datalen != 0 && datalen != sizeof (int))
912: fatalx("bad MSG_EXIT size");
1.9 nicm 913:
1.99 nicm 914: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
915: client_exitreason = CLIENT_EXIT_EXITED;
916: break;
917: case MSG_EXITED:
918: if (datalen != 0)
919: fatalx("bad MSG_EXITED size");
1.9 nicm 920:
1.99 nicm 921: proc_exit(client_proc);
922: break;
923: case MSG_SHUTDOWN:
924: if (datalen != 0)
925: fatalx("bad MSG_SHUTDOWN size");
926:
927: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
928: client_exitreason = CLIENT_EXIT_SERVER_EXITED;
929: client_exitval = 1;
930: break;
931: case MSG_SUSPEND:
932: if (datalen != 0)
933: fatalx("bad MSG_SUSPEND size");
934:
935: memset(&sigact, 0, sizeof sigact);
936: sigemptyset(&sigact.sa_mask);
937: sigact.sa_flags = SA_RESTART;
938: sigact.sa_handler = SIG_DFL;
939: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
940: fatal("sigaction failed");
941: kill(getpid(), SIGTSTP);
942: break;
943: case MSG_LOCK:
944: if (datalen == 0 || data[datalen - 1] != '\0')
945: fatalx("bad MSG_LOCK string");
1.9 nicm 946:
1.99 nicm 947: system(data);
948: proc_send(client_peer, MSG_UNLOCK, -1, NULL, 0);
949: break;
1.9 nicm 950: }
1.1 nicm 951: }