Annotation of src/usr.bin/tmux/client.c, Revision 1.133
1.133 ! nicm 1: /* $OpenBSD: client.c,v 1.132 2019/12/12 11:39:56 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;
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;
486: struct msg_write_ready reply;
487: struct client_file find, *cf;
488: const int flags = O_NONBLOCK|O_WRONLY|O_CREAT;
489: int error = 0;
490:
491: if (datalen != sizeof *msg)
492: fatalx("bad MSG_WRITE_OPEN size");
493: log_debug("open write file %d %s", msg->stream, msg->path);
494:
495: find.stream = msg->stream;
496: if ((cf = RB_FIND(client_files, &client_files, &find)) == NULL) {
497: cf = file_create(NULL, msg->stream, NULL, NULL);
498: RB_INSERT(client_files, &client_files, cf);
499: } else {
500: error = EBADF;
501: goto reply;
502: }
503: if (cf->closed) {
504: error = EBADF;
505: goto reply;
506: }
507:
508: cf->fd = -1;
509: if (msg->fd == -1)
510: cf->fd = open(msg->path, msg->flags|flags, 0644);
511: else {
512: if (msg->fd != STDOUT_FILENO && msg->fd != STDERR_FILENO)
513: errno = EBADF;
514: else {
515: cf->fd = dup(msg->fd);
516: if (client_flags & CLIENT_CONTROL)
517: close(msg->fd); /* can only be used once */
518: }
519: }
520: if (cf->fd == -1) {
521: error = errno;
522: goto reply;
523: }
524:
525: cf->event = bufferevent_new(cf->fd, NULL, client_write_callback,
526: client_write_error_callback, cf);
527: bufferevent_enable(cf->event, EV_WRITE);
528: goto reply;
529:
530: reply:
531: reply.stream = msg->stream;
532: reply.error = error;
533: proc_send(client_peer, MSG_WRITE_READY, -1, &reply, sizeof reply);
534: }
535:
536: /* Write to client file. */
537: static void
538: client_write_data(void *data, size_t datalen)
539: {
540: struct msg_write_data *msg = data;
541: struct client_file find, *cf;
542:
543: if (datalen != sizeof *msg)
544: fatalx("bad MSG_WRITE size");
545: find.stream = msg->stream;
546: if ((cf = RB_FIND(client_files, &client_files, &find)) == NULL)
547: fatalx("unknown stream number");
548: log_debug("write %zu to file %d", msg->size, cf->stream);
549:
550: if (cf->event != NULL)
551: bufferevent_write(cf->event, msg->data, msg->size);
552: }
553:
554: /* Close client file. */
555: static void
556: client_write_close(void *data, size_t datalen)
557: {
558: struct msg_write_close *msg = data;
559: struct client_file find, *cf;
1.54 nicm 560:
1.132 nicm 561: if (datalen != sizeof *msg)
562: fatalx("bad MSG_WRITE_CLOSE size");
563: find.stream = msg->stream;
564: if ((cf = RB_FIND(client_files, &client_files, &find)) == NULL)
565: fatalx("unknown stream number");
566: log_debug("close file %d", cf->stream);
567:
568: if (cf->event == NULL || EVBUFFER_LENGTH(cf->event->output) == 0) {
569: if (cf->event != NULL)
570: bufferevent_free(cf->event);
571: if (cf->fd != -1)
572: close(cf->fd);
573: RB_REMOVE(client_files, &client_files, cf);
574: file_free(cf);
575: }
1.54 nicm 576: }
577:
1.132 nicm 578: /* File read callback. */
1.114 nicm 579: static void
1.132 nicm 580: client_read_callback(__unused struct bufferevent *bev, void *arg)
1.57 nicm 581: {
1.132 nicm 582: struct client_file *cf = arg;
583: void *bdata;
584: size_t bsize;
585: struct msg_read_data msg;
586:
587: for (;;) {
588: bdata = EVBUFFER_DATA(cf->event->input);
589: bsize = EVBUFFER_LENGTH(cf->event->input);
1.57 nicm 590:
1.132 nicm 591: if (bsize == 0)
1.57 nicm 592: break;
1.132 nicm 593: if (bsize > sizeof msg.data)
594: bsize = sizeof msg.data;
595: log_debug("read %zu from file %d", bsize, cf->stream);
596:
597: memcpy(msg.data, bdata, bsize);
598: msg.size = bsize;
599:
600: msg.stream = cf->stream;
601: proc_send(client_peer, MSG_READ, -1, &msg, sizeof msg);
602:
603: evbuffer_drain(cf->event->input, bsize);
604: }
605: }
606:
607: /* File read error callback. */
608: static void
609: client_read_error_callback(__unused struct bufferevent *bev,
610: __unused short what, void *arg)
611: {
612: struct client_file *cf = arg;
613: struct msg_read_done msg;
614:
615: log_debug("read error file %d", cf->stream);
616:
617: msg.stream = cf->stream;
618: msg.error = 0;
619: proc_send(client_peer, MSG_READ_DONE, -1, &msg, sizeof msg);
620:
621: bufferevent_free(cf->event);
622: close(cf->fd);
623: RB_REMOVE(client_files, &client_files, cf);
624: file_free(cf);
625: }
626:
627: /* Open read file. */
628: static void
629: client_read_open(void *data, size_t datalen)
630: {
631: struct msg_read_open *msg = data;
632: struct msg_read_done reply;
633: struct client_file find, *cf;
634: const int flags = O_NONBLOCK|O_RDONLY;
635: int error = 0;
636:
637: if (datalen != sizeof *msg)
638: fatalx("bad MSG_READ_OPEN size");
639: log_debug("open read file %d %s", msg->stream, msg->path);
640:
641: find.stream = msg->stream;
642: if ((cf = RB_FIND(client_files, &client_files, &find)) == NULL) {
643: cf = file_create(NULL, msg->stream, NULL, NULL);
644: RB_INSERT(client_files, &client_files, cf);
645: } else {
646: error = EBADF;
647: goto reply;
1.57 nicm 648: }
1.132 nicm 649: if (cf->closed) {
650: error = EBADF;
651: goto reply;
652: }
653:
654: cf->fd = -1;
655: if (msg->fd == -1)
656: cf->fd = open(msg->path, flags);
657: else {
658: if (msg->fd != STDIN_FILENO)
659: errno = EBADF;
660: else {
661: cf->fd = dup(msg->fd);
662: close(msg->fd); /* can only be used once */
663: }
664: }
665: if (cf->fd == -1) {
666: error = errno;
667: goto reply;
668: }
669:
670: cf->event = bufferevent_new(cf->fd, client_read_callback, NULL,
671: client_read_error_callback, cf);
672: bufferevent_enable(cf->event, EV_READ);
673: return;
674:
675: reply:
676: reply.stream = msg->stream;
677: reply.error = error;
678: proc_send(client_peer, MSG_READ_DONE, -1, &reply, sizeof reply);
1.57 nicm 679: }
680:
1.93 nicm 681: /* Run command in shell; used for -c. */
1.114 nicm 682: static __dead void
1.111 nicm 683: client_exec(const char *shell, const char *shellcmd)
1.93 nicm 684: {
685: const char *name, *ptr;
686: char *argv0;
687:
1.111 nicm 688: log_debug("shell %s, command %s", shell, shellcmd);
1.93 nicm 689:
690: ptr = strrchr(shell, '/');
691: if (ptr != NULL && *(ptr + 1) != '\0')
692: name = ptr + 1;
693: else
694: name = shell;
695: if (client_flags & CLIENT_LOGIN)
696: xasprintf(&argv0, "-%s", name);
697: else
698: xasprintf(&argv0, "%s", name);
699: setenv("SHELL", shell, 1);
700:
1.123 nicm 701: proc_clear_signals(client_proc, 1);
702:
1.93 nicm 703: setblocking(STDIN_FILENO, 1);
704: setblocking(STDOUT_FILENO, 1);
705: setblocking(STDERR_FILENO, 1);
706: closefrom(STDERR_FILENO + 1);
707:
1.111 nicm 708: execl(shell, argv0, "-c", shellcmd, (char *) NULL);
1.93 nicm 709: fatal("execl failed");
710: }
711:
1.99 nicm 712: /* Callback to handle signals in the client. */
1.114 nicm 713: static void
1.99 nicm 714: client_signal(int sig)
715: {
716: struct sigaction sigact;
717: int status;
718:
719: if (sig == SIGCHLD)
720: waitpid(WAIT_ANY, &status, WNOHANG);
721: else if (!client_attached) {
722: if (sig == SIGTERM)
723: proc_exit(client_proc);
724: } else {
725: switch (sig) {
726: case SIGHUP:
727: client_exitreason = CLIENT_EXIT_LOST_TTY;
728: client_exitval = 1;
729: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
730: break;
731: case SIGTERM:
732: client_exitreason = CLIENT_EXIT_TERMINATED;
733: client_exitval = 1;
734: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
735: break;
736: case SIGWINCH:
737: proc_send(client_peer, MSG_RESIZE, -1, NULL, 0);
738: break;
739: case SIGCONT:
740: memset(&sigact, 0, sizeof sigact);
741: sigemptyset(&sigact.sa_mask);
742: sigact.sa_flags = SA_RESTART;
743: sigact.sa_handler = SIG_IGN;
744: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
745: fatal("sigaction failed");
746: proc_send(client_peer, MSG_WAKEUP, -1, NULL, 0);
747: break;
748: }
749: }
750: }
751:
752: /* Callback for client read events. */
1.114 nicm 753: static void
1.121 nicm 754: client_dispatch(struct imsg *imsg, __unused void *arg)
1.99 nicm 755: {
756: if (imsg == NULL) {
757: client_exitreason = CLIENT_EXIT_LOST_SERVER;
758: client_exitval = 1;
1.109 nicm 759: proc_exit(client_proc);
760: return;
761: }
762:
763: if (client_attached)
1.99 nicm 764: client_dispatch_attached(imsg);
765: else
1.121 nicm 766: client_dispatch_wait(imsg);
1.99 nicm 767: }
768:
1.46 nicm 769: /* Dispatch imsgs when in wait state (before MSG_READY). */
1.114 nicm 770: static void
1.121 nicm 771: client_dispatch_wait(struct imsg *imsg)
1.46 nicm 772: {
1.132 nicm 773: char *data;
774: ssize_t datalen;
775: int retval;
776: static int pledge_applied;
1.97 nicm 777:
778: /*
779: * "sendfd" is no longer required once all of the identify messages
780: * have been sent. We know the server won't send us anything until that
781: * point (because we don't ask it to), so we can drop "sendfd" once we
782: * get the first message from the server.
783: */
784: if (!pledge_applied) {
1.132 nicm 785: if (pledge(
786: "stdio rpath wpath cpath unix proc exec tty",
787: NULL) != 0)
1.97 nicm 788: fatal("pledge failed");
789: pledge_applied = 1;
1.132 nicm 790: }
1.46 nicm 791:
1.99 nicm 792: data = imsg->data;
793: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.60 nicm 794:
1.99 nicm 795: switch (imsg->hdr.type) {
796: case MSG_EXIT:
797: case MSG_SHUTDOWN:
798: if (datalen != sizeof retval && datalen != 0)
799: fatalx("bad MSG_EXIT size");
800: if (datalen == sizeof retval) {
801: memcpy(&retval, data, sizeof retval);
802: client_exitval = retval;
803: }
1.133 ! nicm 804: client_exitflag = 1;
! 805: client_exit();
1.99 nicm 806: break;
807: case MSG_READY:
808: if (datalen != 0)
809: fatalx("bad MSG_READY size");
1.54 nicm 810:
1.99 nicm 811: client_attached = 1;
812: proc_send(client_peer, MSG_RESIZE, -1, NULL, 0);
813: break;
814: case MSG_VERSION:
815: if (datalen != 0)
816: fatalx("bad MSG_VERSION size");
817:
818: fprintf(stderr, "protocol version mismatch "
819: "(client %d, server %u)\n", PROTOCOL_VERSION,
1.103 nicm 820: imsg->hdr.peerid & 0xff);
1.99 nicm 821: client_exitval = 1;
822: proc_exit(client_proc);
823: break;
824: case MSG_SHELL:
825: if (datalen == 0 || data[datalen - 1] != '\0')
826: fatalx("bad MSG_SHELL string");
827:
1.121 nicm 828: client_exec(data, shell_command);
1.99 nicm 829: /* NOTREACHED */
830: case MSG_DETACH:
831: case MSG_DETACHKILL:
832: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
833: break;
834: case MSG_EXITED:
835: proc_exit(client_proc);
1.132 nicm 836: break;
837: case MSG_READ_OPEN:
838: client_read_open(data, datalen);
839: break;
840: case MSG_WRITE_OPEN:
841: client_write_open(data, datalen);
842: break;
843: case MSG_WRITE:
844: client_write_data(data, datalen);
845: break;
846: case MSG_WRITE_CLOSE:
847: client_write_close(data, datalen);
1.99 nicm 848: break;
1.46 nicm 849: }
850: }
851:
852: /* Dispatch imsgs in attached state (after MSG_READY). */
1.114 nicm 853: static void
1.99 nicm 854: client_dispatch_attached(struct imsg *imsg)
1.9 nicm 855: {
1.69 nicm 856: struct sigaction sigact;
857: char *data;
1.99 nicm 858: ssize_t datalen;
1.9 nicm 859:
1.99 nicm 860: data = imsg->data;
861: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.12 nicm 862:
1.99 nicm 863: switch (imsg->hdr.type) {
864: case MSG_DETACH:
865: case MSG_DETACHKILL:
866: if (datalen == 0 || data[datalen - 1] != '\0')
867: fatalx("bad MSG_DETACH string");
868:
869: client_exitsession = xstrdup(data);
870: client_exittype = imsg->hdr.type;
871: if (imsg->hdr.type == MSG_DETACHKILL)
872: client_exitreason = CLIENT_EXIT_DETACHED_HUP;
873: else
874: client_exitreason = CLIENT_EXIT_DETACHED;
1.115 nicm 875: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
876: break;
877: case MSG_EXEC:
878: if (datalen == 0 || data[datalen - 1] != '\0' ||
879: strlen(data) + 1 == (size_t)datalen)
880: fatalx("bad MSG_EXEC string");
881: client_execcmd = xstrdup(data);
882: client_execshell = xstrdup(data + strlen(data) + 1);
883:
884: client_exittype = imsg->hdr.type;
1.99 nicm 885: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
886: break;
887: case MSG_EXIT:
888: if (datalen != 0 && datalen != sizeof (int))
889: fatalx("bad MSG_EXIT size");
1.9 nicm 890:
1.99 nicm 891: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
892: client_exitreason = CLIENT_EXIT_EXITED;
893: break;
894: case MSG_EXITED:
895: if (datalen != 0)
896: fatalx("bad MSG_EXITED size");
1.9 nicm 897:
1.99 nicm 898: proc_exit(client_proc);
899: break;
900: case MSG_SHUTDOWN:
901: if (datalen != 0)
902: fatalx("bad MSG_SHUTDOWN size");
903:
904: proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
905: client_exitreason = CLIENT_EXIT_SERVER_EXITED;
906: client_exitval = 1;
907: break;
908: case MSG_SUSPEND:
909: if (datalen != 0)
910: fatalx("bad MSG_SUSPEND size");
911:
912: memset(&sigact, 0, sizeof sigact);
913: sigemptyset(&sigact.sa_mask);
914: sigact.sa_flags = SA_RESTART;
915: sigact.sa_handler = SIG_DFL;
916: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
917: fatal("sigaction failed");
918: kill(getpid(), SIGTSTP);
919: break;
920: case MSG_LOCK:
921: if (datalen == 0 || data[datalen - 1] != '\0')
922: fatalx("bad MSG_LOCK string");
1.9 nicm 923:
1.99 nicm 924: system(data);
925: proc_send(client_peer, MSG_UNLOCK, -1, NULL, 0);
926: break;
1.9 nicm 927: }
1.1 nicm 928: }