Annotation of src/usr.bin/tmux/client.c, Revision 1.54
1.54 ! nicm 1: /* $OpenBSD: client.c,v 1.53 2012/04/23 23:25:02 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
1.51 nicm 20: #include <sys/file.h>
1.1 nicm 21: #include <sys/socket.h>
22: #include <sys/stat.h>
23: #include <sys/un.h>
24: #include <sys/wait.h>
25:
26: #include <errno.h>
1.30 nicm 27: #include <event.h>
1.1 nicm 28: #include <fcntl.h>
29: #include <pwd.h>
30: #include <stdlib.h>
31: #include <string.h>
32: #include <unistd.h>
33:
34: #include "tmux.h"
35:
1.25 nicm 36: struct imsgbuf client_ibuf;
1.30 nicm 37: struct event client_event;
1.54 ! nicm 38: struct event client_stdin;
1.52 nicm 39: enum {
40: CLIENT_EXIT_NONE,
41: CLIENT_EXIT_DETACHED,
42: CLIENT_EXIT_DETACHED_HUP,
43: CLIENT_EXIT_LOST_TTY,
44: CLIENT_EXIT_TERMINATED,
45: CLIENT_EXIT_LOST_SERVER,
46: CLIENT_EXIT_EXITED,
47: CLIENT_EXIT_SERVER_EXITED,
48: } client_exitreason = CLIENT_EXIT_NONE;
1.32 nicm 49: int client_exitval;
1.48 nicm 50: enum msgtype client_exittype;
1.46 nicm 51: int client_attached;
1.1 nicm 52:
1.49 nicm 53: int client_get_lock(char *);
1.46 nicm 54: int client_connect(char *, int);
1.30 nicm 55: void client_send_identify(int);
56: void client_send_environ(void);
57: void client_write_server(enum msgtype, void *, size_t);
1.31 nicm 58: void client_update_event(void);
1.30 nicm 59: void client_signal(int, short, void *);
1.54 ! nicm 60: void client_stdin_callback(int, short, void *);
1.30 nicm 61: void client_callback(int, short, void *);
1.46 nicm 62: int client_dispatch_attached(void);
63: int client_dispatch_wait(void *);
1.53 nicm 64: const char *client_exit_message(void);
1.25 nicm 65:
1.49 nicm 66: /*
67: * Get server create lock. If already held then server start is happening in
68: * another client, so block until the lock is released and return -1 to
69: * retry. Ignore other errors - just continue and start the server without the
70: * lock.
71: */
72: int
73: client_get_lock(char *lockfile)
74: {
75: int lockfd;
76:
77: if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1)
78: fatal("open failed");
79:
80: if (flock(lockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) {
81: while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
82: /* nothing */;
83: close(lockfd);
84: return (-1);
85: }
86:
87: return (lockfd);
88: }
89:
1.46 nicm 90: /* Connect client to server. */
91: int
92: client_connect(char *path, int start_server)
1.1 nicm 93: {
1.26 nicm 94: struct sockaddr_un sa;
95: size_t size;
1.49 nicm 96: int fd, lockfd;
97: char *lockfile;
1.1 nicm 98:
99: memset(&sa, 0, sizeof sa);
100: sa.sun_family = AF_UNIX;
101: size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
102: if (size >= sizeof sa.sun_path) {
103: errno = ENAMETOOLONG;
1.46 nicm 104: return (-1);
1.1 nicm 105: }
106:
1.49 nicm 107: retry:
1.10 nicm 108: if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1.18 nicm 109: fatal("socket failed");
1.1 nicm 110:
1.10 nicm 111: if (connect(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
1.49 nicm 112: if (errno != ECONNREFUSED && errno != ENOENT)
113: goto failed;
1.46 nicm 114: if (!start_server)
115: goto failed;
1.49 nicm 116: close(fd);
117:
118: xasprintf(&lockfile, "%s.lock", path);
119: if ((lockfd = client_get_lock(lockfile)) == -1)
120: goto retry;
121: if (unlink(path) != 0 && errno != ENOENT)
122: return (-1);
123: fd = server_start(lockfd, lockfile);
124: xfree(lockfile);
125: close(lockfd);
1.1 nicm 126: }
127:
1.47 nicm 128: setblocking(fd, 0);
1.46 nicm 129: return (fd);
130:
131: failed:
132: close(fd);
133: return (-1);
134: }
135:
1.52 nicm 136: /* Get exit string from reason number. */
137: const char *
138: client_exit_message(void)
139: {
140: switch (client_exitreason) {
141: case CLIENT_EXIT_NONE:
142: break;
143: case CLIENT_EXIT_DETACHED:
144: return ("detached");
145: case CLIENT_EXIT_DETACHED_HUP:
146: return ("detached and SIGHUP");
147: case CLIENT_EXIT_LOST_TTY:
148: return ("lost tty");
149: case CLIENT_EXIT_TERMINATED:
150: return ("terminated");
151: case CLIENT_EXIT_LOST_SERVER:
152: return ("lost server");
153: case CLIENT_EXIT_EXITED:
154: return ("exited");
155: case CLIENT_EXIT_SERVER_EXITED:
156: return ("server exited");
157: }
158: return ("unknown reason");
159: }
160:
1.46 nicm 161: /* Client main loop. */
162: int
163: client_main(int argc, char **argv, int flags)
164: {
165: struct cmd *cmd;
166: struct cmd_list *cmdlist;
167: struct msg_command_data cmddata;
168: int cmdflags, fd;
1.48 nicm 169: pid_t ppid;
1.46 nicm 170: enum msgtype msg;
171: char *cause;
172:
173: /* Set up the initial command. */
174: cmdflags = 0;
175: if (shell_cmd != NULL) {
176: msg = MSG_SHELL;
177: cmdflags = CMD_STARTSERVER;
178: } else if (argc == 0) {
179: msg = MSG_COMMAND;
180: cmdflags = CMD_STARTSERVER|CMD_SENDENVIRON|CMD_CANTNEST;
181: } else {
182: msg = MSG_COMMAND;
183:
184: /*
185: * It sucks parsing the command string twice (in client and
186: * later in server) but it is necessary to get the start server
187: * flag.
188: */
189: if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
190: log_warnx("%s", cause);
191: return (1);
192: }
193: cmdflags &= ~CMD_STARTSERVER;
194: TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
195: if (cmd->entry->flags & CMD_STARTSERVER)
196: cmdflags |= CMD_STARTSERVER;
197: if (cmd->entry->flags & CMD_SENDENVIRON)
198: cmdflags |= CMD_SENDENVIRON;
199: if (cmd->entry->flags & CMD_CANTNEST)
200: cmdflags |= CMD_CANTNEST;
201: }
202: cmd_list_free(cmdlist);
203: }
204:
205: /*
206: * Check if this could be a nested session, if the command can't nest:
207: * if the socket path matches $TMUX, this is probably the same server.
208: */
209: if (shell_cmd == NULL && environ_path != NULL &&
1.52 nicm 210: (cmdflags & CMD_CANTNEST) &&
211: strcmp(socket_path, environ_path) == 0) {
1.46 nicm 212: log_warnx("sessions should be nested with care. "
213: "unset $TMUX to force.");
214: return (1);
215: }
216:
217: /* Initialise the client socket and start the server. */
218: fd = client_connect(socket_path, cmdflags & CMD_STARTSERVER);
219: if (fd == -1) {
220: log_warn("failed to connect to server");
221: return (1);
222: }
223:
224: /* Set process title, log and signals now this is the client. */
225: setproctitle("client (%s)", socket_path);
226: logfile("client");
227:
228: /* Create imsg. */
1.25 nicm 229: imsg_init(&client_ibuf, fd);
1.46 nicm 230: event_set(&client_event, fd, EV_READ, client_callback, shell_cmd);
231:
1.54 ! nicm 232: /* Create stdin handler. */
! 233: setblocking(STDIN_FILENO, 0);
! 234: event_set(&client_stdin, STDIN_FILENO, EV_READ|EV_PERSIST,
! 235: client_stdin_callback, NULL);
! 236:
1.46 nicm 237: /* Establish signal handlers. */
238: set_signals(client_signal);
1.1 nicm 239:
1.46 nicm 240: /* Send initial environment. */
1.11 nicm 241: if (cmdflags & CMD_SENDENVIRON)
1.25 nicm 242: client_send_environ();
1.42 nicm 243: client_send_identify(flags);
1.1 nicm 244:
1.46 nicm 245: /* Send first command. */
246: if (msg == MSG_COMMAND) {
247: /* Fill in command line arguments. */
248: cmddata.pid = environ_pid;
249: cmddata.idx = environ_idx;
250:
251: /* Prepare command for server. */
252: cmddata.argc = argc;
253: if (cmd_pack_argv(
254: argc, argv, cmddata.argv, sizeof cmddata.argv) != 0) {
255: log_warnx("command too long");
256: return (1);
257: }
258:
259: client_write_server(msg, &cmddata, sizeof cmddata);
260: } else if (msg == MSG_SHELL)
261: client_write_server(msg, NULL, 0);
1.1 nicm 262:
1.46 nicm 263: /* Set the event and dispatch. */
264: client_update_event();
1.54 ! nicm 265: event_add (&client_stdin, NULL);
1.46 nicm 266: event_dispatch();
267:
268: /* Print the exit message, if any, and exit. */
1.48 nicm 269: if (client_attached) {
1.52 nicm 270: if (client_exitreason != CLIENT_EXIT_NONE && !login_shell)
271: printf("[%s]\n", client_exit_message());
1.48 nicm 272:
273: ppid = getppid();
274: if (client_exittype == MSG_DETACHKILL && ppid > 1)
275: kill(ppid, SIGHUP);
276: }
1.54 ! nicm 277: setblocking(STDIN_FILENO, 1);
1.46 nicm 278: return (client_exitval);
1.1 nicm 279: }
280:
1.46 nicm 281: /* Send identify message to server with the file descriptors. */
1.11 nicm 282: void
1.26 nicm 283: client_send_identify(int flags)
284: {
285: struct msg_identify_data data;
286: char *term;
287: int fd;
288:
289: data.flags = flags;
290:
291: if (getcwd(data.cwd, sizeof data.cwd) == NULL)
292: *data.cwd = '\0';
1.35 nicm 293:
1.26 nicm 294: term = getenv("TERM");
295: if (term == NULL ||
296: strlcpy(data.term, term, sizeof data.term) >= sizeof data.term)
297: *data.term = '\0';
298:
1.50 nicm 299: if ((fd = dup(STDIN_FILENO)) == -1)
300: fatal("dup failed");
301: imsg_compose(&client_ibuf,
302: MSG_IDENTIFY, PROTOCOL_VERSION, -1, fd, &data, sizeof data);
1.54 ! nicm 303: client_update_event();
1.26 nicm 304: }
305:
1.46 nicm 306: /* Forward entire environment to server. */
1.26 nicm 307: void
1.25 nicm 308: client_send_environ(void)
1.11 nicm 309: {
1.26 nicm 310: struct msg_environ_data data;
1.11 nicm 311: char **var;
312:
1.35 nicm 313: for (var = environ; *var != NULL; var++) {
1.11 nicm 314: if (strlcpy(data.var, *var, sizeof data.var) >= sizeof data.var)
315: continue;
1.25 nicm 316: client_write_server(MSG_ENVIRON, &data, sizeof data);
1.11 nicm 317: }
318: }
319:
1.46 nicm 320: /* Write a message to the server without a file descriptor. */
1.25 nicm 321: void
322: client_write_server(enum msgtype type, void *buf, size_t len)
323: {
1.35 nicm 324: imsg_compose(&client_ibuf, type, PROTOCOL_VERSION, -1, -1, buf, len);
1.54 ! nicm 325: client_update_event();
1.25 nicm 326: }
327:
1.46 nicm 328: /* Update client event based on whether it needs to read or read and write. */
1.31 nicm 329: void
330: client_update_event(void)
331: {
332: short events;
333:
334: event_del(&client_event);
335: events = EV_READ;
336: if (client_ibuf.w.queued > 0)
337: events |= EV_WRITE;
1.46 nicm 338: event_set(
339: &client_event, client_ibuf.fd, events, client_callback, shell_cmd);
1.31 nicm 340: event_add(&client_event, NULL);
341: }
342:
1.46 nicm 343: /* Callback to handle signals in the client. */
1.34 nicm 344: /* ARGSUSED */
1.30 nicm 345: void
1.31 nicm 346: client_signal(int sig, unused short events, unused void *data)
1.30 nicm 347: {
1.46 nicm 348: struct sigaction sigact;
349: int status;
1.30 nicm 350:
1.46 nicm 351: if (!client_attached) {
352: switch (sig) {
353: case SIGCHLD:
354: waitpid(WAIT_ANY, &status, WNOHANG);
355: break;
356: case SIGTERM:
357: event_loopexit(NULL);
358: break;
359: }
360: } else {
361: switch (sig) {
362: case SIGHUP:
1.52 nicm 363: client_exitreason = CLIENT_EXIT_LOST_TTY;
1.46 nicm 364: client_exitval = 1;
365: client_write_server(MSG_EXITING, NULL, 0);
366: break;
367: case SIGTERM:
1.52 nicm 368: client_exitreason = CLIENT_EXIT_TERMINATED;
1.46 nicm 369: client_exitval = 1;
370: client_write_server(MSG_EXITING, NULL, 0);
371: break;
372: case SIGWINCH:
373: client_write_server(MSG_RESIZE, NULL, 0);
374: break;
375: case SIGCONT:
376: memset(&sigact, 0, sizeof sigact);
377: sigemptyset(&sigact.sa_mask);
378: sigact.sa_flags = SA_RESTART;
379: sigact.sa_handler = SIG_IGN;
380: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
381: fatal("sigaction failed");
382: client_write_server(MSG_WAKEUP, NULL, 0);
383: break;
384: }
1.30 nicm 385: }
386:
1.31 nicm 387: client_update_event();
1.30 nicm 388: }
389:
1.46 nicm 390: /* Callback for client imsg read events. */
1.34 nicm 391: /* ARGSUSED */
1.30 nicm 392: void
1.46 nicm 393: client_callback(unused int fd, short events, void *data)
1.30 nicm 394: {
1.33 nicm 395: ssize_t n;
1.46 nicm 396: int retval;
1.30 nicm 397:
398: if (events & EV_READ) {
399: if ((n = imsg_read(&client_ibuf)) == -1 || n == 0)
400: goto lost_server;
1.46 nicm 401: if (client_attached)
402: retval = client_dispatch_attached();
403: else
404: retval = client_dispatch_wait(data);
405: if (retval != 0) {
1.30 nicm 406: event_loopexit(NULL);
407: return;
1.35 nicm 408: }
1.30 nicm 409: }
1.35 nicm 410:
1.30 nicm 411: if (events & EV_WRITE) {
412: if (msgbuf_write(&client_ibuf.w) < 0)
413: goto lost_server;
414: }
415:
1.31 nicm 416: client_update_event();
1.30 nicm 417: return;
418:
419: lost_server:
1.52 nicm 420: client_exitreason = CLIENT_EXIT_LOST_SERVER;
1.32 nicm 421: client_exitval = 1;
1.30 nicm 422: event_loopexit(NULL);
423: }
424:
1.54 ! nicm 425: /* Callback for client stdin read events. */
! 426: /* ARGSUSED */
! 427: void
! 428: client_stdin_callback(unused int fd, unused short events, unused void *data1)
! 429: {
! 430: struct msg_stdin_data data;
! 431:
! 432: data.size = read(STDIN_FILENO, data.data, sizeof data.data);
! 433: if (data.size < 0 && (errno == EINTR || errno == EAGAIN))
! 434: return;
! 435:
! 436: client_write_server(MSG_STDIN, &data, sizeof data);
! 437: if (data.size <= 0)
! 438: event_del(&client_stdin);
! 439: client_update_event();
! 440: }
! 441:
1.46 nicm 442: /* Dispatch imsgs when in wait state (before MSG_READY). */
443: int
444: client_dispatch_wait(void *data)
445: {
446: struct imsg imsg;
447: ssize_t n, datalen;
448: struct msg_shell_data shelldata;
449: struct msg_exit_data exitdata;
1.54 ! nicm 450: struct msg_stdout_data stdoutdata;
! 451: struct msg_stderr_data stderrdata;
1.46 nicm 452: const char *shellcmd = data;
453:
454: for (;;) {
455: if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
456: fatalx("imsg_get failed");
457: if (n == 0)
458: return (0);
459: datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
460:
1.54 ! nicm 461: log_debug("got %d from server", imsg.hdr.type);
1.46 nicm 462: switch (imsg.hdr.type) {
463: case MSG_EXIT:
464: case MSG_SHUTDOWN:
465: if (datalen != sizeof exitdata) {
466: if (datalen != 0)
467: fatalx("bad MSG_EXIT size");
468: } else {
469: memcpy(&exitdata, imsg.data, sizeof exitdata);
470: client_exitval = exitdata.retcode;
471: }
472: imsg_free(&imsg);
473: return (-1);
474: case MSG_READY:
475: if (datalen != 0)
476: fatalx("bad MSG_READY size");
477:
1.54 ! nicm 478: event_del(&client_stdin);
1.46 nicm 479: client_attached = 1;
480: break;
1.54 ! nicm 481: case MSG_STDOUT:
! 482: if (datalen != sizeof stdoutdata)
! 483: fatalx("bad MSG_STDOUT");
! 484: memcpy(&stdoutdata, imsg.data, sizeof stdoutdata);
! 485:
! 486: fwrite(stdoutdata.data, stdoutdata.size, 1, stdout);
! 487: break;
! 488: case MSG_STDERR:
! 489: if (datalen != sizeof stderrdata)
! 490: fatalx("bad MSG_STDERR");
! 491: memcpy(&stderrdata, imsg.data, sizeof stderrdata);
! 492:
! 493: fwrite(stderrdata.data, stderrdata.size, 1, stderr);
! 494: break;
1.46 nicm 495: case MSG_VERSION:
496: if (datalen != 0)
497: fatalx("bad MSG_VERSION size");
498:
1.54 ! nicm 499: fprintf(stderr, "protocol version mismatch "
! 500: "(client %u, server %u)\n", PROTOCOL_VERSION,
! 501: imsg.hdr.peerid);
1.46 nicm 502: client_exitval = 1;
503:
504: imsg_free(&imsg);
505: return (-1);
506: case MSG_SHELL:
507: if (datalen != sizeof shelldata)
508: fatalx("bad MSG_SHELL size");
509: memcpy(&shelldata, imsg.data, sizeof shelldata);
510: shelldata.shell[(sizeof shelldata.shell) - 1] = '\0';
511:
512: clear_signals(0);
513:
514: shell_exec(shelldata.shell, shellcmd);
515: /* NOTREACHED */
516: default:
517: fatalx("unexpected message");
518: }
519:
520: imsg_free(&imsg);
521: }
522: }
523:
524: /* Dispatch imsgs in attached state (after MSG_READY). */
525: /* ARGSUSED */
1.9 nicm 526: int
1.46 nicm 527: client_dispatch_attached(void)
1.9 nicm 528: {
1.30 nicm 529: struct imsg imsg;
530: struct msg_lock_data lockdata;
531: struct sigaction sigact;
532: ssize_t n, datalen;
1.9 nicm 533:
534: for (;;) {
1.25 nicm 535: if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
1.12 nicm 536: fatalx("imsg_get failed");
537: if (n == 0)
1.9 nicm 538: return (0);
1.12 nicm 539: datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1.9 nicm 540:
1.30 nicm 541: log_debug("client got %d", imsg.hdr.type);
1.12 nicm 542: switch (imsg.hdr.type) {
1.48 nicm 543: case MSG_DETACHKILL:
1.9 nicm 544: case MSG_DETACH:
1.12 nicm 545: if (datalen != 0)
1.9 nicm 546: fatalx("bad MSG_DETACH size");
547:
1.48 nicm 548: client_exittype = imsg.hdr.type;
549: if (imsg.hdr.type == MSG_DETACHKILL)
1.52 nicm 550: client_exitreason = CLIENT_EXIT_DETACHED_HUP;
1.48 nicm 551: else
1.52 nicm 552: client_exitreason = CLIENT_EXIT_DETACHED;
1.25 nicm 553: client_write_server(MSG_EXITING, NULL, 0);
1.9 nicm 554: break;
555: case MSG_EXIT:
1.43 nicm 556: if (datalen != 0 &&
557: datalen != sizeof (struct msg_exit_data))
1.9 nicm 558: fatalx("bad MSG_EXIT size");
1.12 nicm 559:
1.25 nicm 560: client_write_server(MSG_EXITING, NULL, 0);
1.52 nicm 561: client_exitreason = CLIENT_EXIT_EXITED;
1.9 nicm 562: break;
563: case MSG_EXITED:
1.12 nicm 564: if (datalen != 0)
1.9 nicm 565: fatalx("bad MSG_EXITED size");
566:
1.12 nicm 567: imsg_free(&imsg);
1.9 nicm 568: return (-1);
569: case MSG_SHUTDOWN:
1.12 nicm 570: if (datalen != 0)
1.9 nicm 571: fatalx("bad MSG_SHUTDOWN size");
572:
1.25 nicm 573: client_write_server(MSG_EXITING, NULL, 0);
1.52 nicm 574: client_exitreason = CLIENT_EXIT_SERVER_EXITED;
1.32 nicm 575: client_exitval = 1;
1.9 nicm 576: break;
577: case MSG_SUSPEND:
1.12 nicm 578: if (datalen != 0)
1.9 nicm 579: fatalx("bad MSG_SUSPEND size");
580:
1.30 nicm 581: memset(&sigact, 0, sizeof sigact);
582: sigemptyset(&sigact.sa_mask);
583: sigact.sa_flags = SA_RESTART;
584: sigact.sa_handler = SIG_DFL;
585: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
586: fatal("sigaction failed");
587: kill(getpid(), SIGTSTP);
1.21 nicm 588: break;
589: case MSG_LOCK:
590: if (datalen != sizeof lockdata)
591: fatalx("bad MSG_LOCK size");
592: memcpy(&lockdata, imsg.data, sizeof lockdata);
1.35 nicm 593:
1.21 nicm 594: lockdata.cmd[(sizeof lockdata.cmd) - 1] = '\0';
595: system(lockdata.cmd);
1.25 nicm 596: client_write_server(MSG_UNLOCK, NULL, 0);
1.9 nicm 597: break;
598: default:
599: fatalx("unexpected message");
600: }
1.12 nicm 601:
602: imsg_free(&imsg);
1.9 nicm 603: }
1.1 nicm 604: }