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