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