Annotation of src/usr.bin/tmux/client.c, Revision 1.44
1.44 ! nicm 1: /* $OpenBSD: client.c,v 1.43 2010/08/22 16:09:49 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>
20: #include <sys/socket.h>
21: #include <sys/stat.h>
22: #include <sys/un.h>
23: #include <sys/wait.h>
24:
25: #include <errno.h>
1.30 nicm 26: #include <event.h>
1.1 nicm 27: #include <fcntl.h>
28: #include <pwd.h>
29: #include <stdlib.h>
30: #include <string.h>
31: #include <syslog.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.1 nicm 40:
1.30 nicm 41: void client_send_identify(int);
42: void client_send_environ(void);
43: void client_write_server(enum msgtype, void *, size_t);
1.31 nicm 44: void client_update_event(void);
1.30 nicm 45: void client_signal(int, short, void *);
46: void client_callback(int, short, void *);
47: int client_dispatch(void);
1.25 nicm 48:
49: struct imsgbuf *
50: client_init(char *path, int cmdflags, int flags)
1.1 nicm 51: {
1.26 nicm 52: struct sockaddr_un sa;
53: size_t size;
54: int fd, mode;
55: char rpathbuf[MAXPATHLEN];
1.2 nicm 56:
57: if (realpath(path, rpathbuf) == NULL)
58: strlcpy(rpathbuf, path, sizeof rpathbuf);
59: setproctitle("client (%s)", rpathbuf);
1.1 nicm 60:
61: memset(&sa, 0, sizeof sa);
62: sa.sun_family = AF_UNIX;
63: size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
64: if (size >= sizeof sa.sun_path) {
65: errno = ENAMETOOLONG;
66: goto not_found;
67: }
68:
1.10 nicm 69: if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1.18 nicm 70: fatal("socket failed");
1.1 nicm 71:
1.10 nicm 72: if (connect(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
1.28 nicm 73: if (!(cmdflags & CMD_STARTSERVER))
74: goto not_found;
75: switch (errno) {
76: case ECONNREFUSED:
77: if (unlink(path) != 0)
1.1 nicm 78: goto not_found;
1.28 nicm 79: /* FALLTHROUGH */
80: case ENOENT:
1.10 nicm 81: if ((fd = server_start(path)) == -1)
1.1 nicm 82: goto start_failed;
83: goto server_started;
84: }
85: goto not_found;
86: }
87:
88: server_started:
1.10 nicm 89: if ((mode = fcntl(fd, F_GETFL)) == -1)
1.1 nicm 90: fatal("fcntl failed");
1.10 nicm 91: if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
1.22 nicm 92: fatal("fcntl failed");
93: if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
1.1 nicm 94: fatal("fcntl failed");
1.25 nicm 95: imsg_init(&client_ibuf, fd);
1.44 ! nicm 96: event_set(&client_event, fd, EV_READ, client_callback, NULL);
1.1 nicm 97:
1.11 nicm 98: if (cmdflags & CMD_SENDENVIRON)
1.25 nicm 99: client_send_environ();
1.42 nicm 100: client_send_identify(flags);
1.1 nicm 101:
1.25 nicm 102: return (&client_ibuf);
1.1 nicm 103:
104: start_failed:
105: log_warnx("server failed to start");
1.25 nicm 106: return (NULL);
1.1 nicm 107:
108: not_found:
109: log_warn("server not found");
1.25 nicm 110: return (NULL);
1.1 nicm 111: }
112:
1.11 nicm 113: void
1.26 nicm 114: client_send_identify(int flags)
115: {
116: struct msg_identify_data data;
117: char *term;
118: int fd;
119:
120: data.flags = flags;
121:
122: if (getcwd(data.cwd, sizeof data.cwd) == NULL)
123: *data.cwd = '\0';
1.35 nicm 124:
1.26 nicm 125: term = getenv("TERM");
126: if (term == NULL ||
127: strlcpy(data.term, term, sizeof data.term) >= sizeof data.term)
128: *data.term = '\0';
129:
130: if ((fd = dup(STDIN_FILENO)) == -1)
131: fatal("dup failed");
132: imsg_compose(&client_ibuf,
133: MSG_IDENTIFY, PROTOCOL_VERSION, -1, fd, &data, sizeof data);
1.42 nicm 134:
135: if ((fd = dup(STDOUT_FILENO)) == -1)
136: fatal("dup failed");
137: imsg_compose(&client_ibuf, MSG_STDOUT, PROTOCOL_VERSION, -1, fd, NULL, 0);
138:
139: if ((fd = dup(STDERR_FILENO)) == -1)
140: fatal("dup failed");
141: imsg_compose(&client_ibuf, MSG_STDERR, PROTOCOL_VERSION, -1, fd, NULL, 0);
1.26 nicm 142: }
143:
144: void
1.25 nicm 145: client_send_environ(void)
1.11 nicm 146: {
1.26 nicm 147: struct msg_environ_data data;
1.11 nicm 148: char **var;
149:
1.35 nicm 150: for (var = environ; *var != NULL; var++) {
1.11 nicm 151: if (strlcpy(data.var, *var, sizeof data.var) >= sizeof data.var)
152: continue;
1.25 nicm 153: client_write_server(MSG_ENVIRON, &data, sizeof data);
1.11 nicm 154: }
155: }
156:
1.25 nicm 157: void
158: client_write_server(enum msgtype type, void *buf, size_t len)
159: {
1.35 nicm 160: imsg_compose(&client_ibuf, type, PROTOCOL_VERSION, -1, -1, buf, len);
1.25 nicm 161: }
162:
1.31 nicm 163: void
164: client_update_event(void)
165: {
166: short events;
167:
168: event_del(&client_event);
169: events = EV_READ;
170: if (client_ibuf.w.queued > 0)
171: events |= EV_WRITE;
172: event_set(&client_event, client_ibuf.fd, events, client_callback, NULL);
173: event_add(&client_event, NULL);
174: }
175:
1.25 nicm 176: __dead void
177: client_main(void)
1.1 nicm 178: {
1.30 nicm 179: logfile("client");
180:
181: /* Note: event_init() has already been called. */
182:
1.35 nicm 183: /* Set up signals. */
1.38 nicm 184: set_signals(client_signal);
1.41 nicm 185:
186: /*
187: * Send a resize message immediately in case the terminal size has
188: * changed between the identify message to the server and the MSG_READY
189: * telling us to move into the client code.
190: */
191: client_write_server(MSG_RESIZE, NULL, 0);
1.1 nicm 192:
1.17 nicm 193: /*
194: * imsg_read in the first client poll loop (before the terminal has
1.30 nicm 195: * been initialised) may have read messages into the buffer after the
196: * MSG_READY switched to here. Process anything outstanding now to
197: * avoid hanging waiting for messages that have already arrived.
1.17 nicm 198: */
1.25 nicm 199: if (client_dispatch() != 0)
1.17 nicm 200: goto out;
201:
1.31 nicm 202: /* Set the event and dispatch. */
1.32 nicm 203: client_update_event();
1.30 nicm 204: event_dispatch();
1.17 nicm 205:
206: out:
1.25 nicm 207: /* Print the exit message, if any, and exit. */
1.32 nicm 208: if (client_exitmsg != NULL && !login_shell)
209: printf("[%s]\n", client_exitmsg);
210: exit(client_exitval);
1.9 nicm 211: }
212:
1.34 nicm 213: /* ARGSUSED */
1.30 nicm 214: void
1.31 nicm 215: client_signal(int sig, unused short events, unused void *data)
1.30 nicm 216: {
217: struct sigaction sigact;
218:
219: switch (sig) {
1.39 jsing 220: case SIGHUP:
221: client_exitmsg = "lost tty";
222: client_exitval = 1;
223: client_write_server(MSG_EXITING, NULL, 0);
224: break;
1.30 nicm 225: case SIGTERM:
226: client_exitmsg = "terminated";
1.32 nicm 227: client_exitval = 1;
1.30 nicm 228: client_write_server(MSG_EXITING, NULL, 0);
229: break;
230: case SIGWINCH:
231: client_write_server(MSG_RESIZE, NULL, 0);
232: break;
233: case SIGCONT:
234: memset(&sigact, 0, sizeof sigact);
235: sigemptyset(&sigact.sa_mask);
236: sigact.sa_flags = SA_RESTART;
237: sigact.sa_handler = SIG_IGN;
238: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
239: fatal("sigaction failed");
240: client_write_server(MSG_WAKEUP, NULL, 0);
241: break;
242: }
243:
1.31 nicm 244: client_update_event();
1.30 nicm 245: }
246:
1.34 nicm 247: /* ARGSUSED */
1.30 nicm 248: void
249: client_callback(unused int fd, short events, unused void *data)
250: {
1.33 nicm 251: ssize_t n;
1.30 nicm 252:
253: if (events & EV_READ) {
254: if ((n = imsg_read(&client_ibuf)) == -1 || n == 0)
255: goto lost_server;
256: if (client_dispatch() != 0) {
257: event_loopexit(NULL);
258: return;
1.35 nicm 259: }
1.30 nicm 260: }
1.35 nicm 261:
1.30 nicm 262: if (events & EV_WRITE) {
263: if (msgbuf_write(&client_ibuf.w) < 0)
264: goto lost_server;
265: }
266:
1.31 nicm 267: client_update_event();
1.30 nicm 268: return;
269:
270: lost_server:
271: client_exitmsg = "lost server";
1.32 nicm 272: client_exitval = 1;
1.30 nicm 273: event_loopexit(NULL);
274: }
275:
1.9 nicm 276: int
1.25 nicm 277: client_dispatch(void)
1.9 nicm 278: {
1.30 nicm 279: struct imsg imsg;
280: struct msg_lock_data lockdata;
281: struct sigaction sigact;
282: ssize_t n, datalen;
1.9 nicm 283:
284: for (;;) {
1.25 nicm 285: if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
1.12 nicm 286: fatalx("imsg_get failed");
287: if (n == 0)
1.9 nicm 288: return (0);
1.12 nicm 289: datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1.9 nicm 290:
1.30 nicm 291: log_debug("client got %d", imsg.hdr.type);
1.12 nicm 292: switch (imsg.hdr.type) {
1.9 nicm 293: case MSG_DETACH:
1.12 nicm 294: if (datalen != 0)
1.9 nicm 295: fatalx("bad MSG_DETACH size");
296:
1.25 nicm 297: client_write_server(MSG_EXITING, NULL, 0);
298: client_exitmsg = "detached";
1.9 nicm 299: break;
300: case MSG_EXIT:
1.43 nicm 301: if (datalen != 0 &&
302: datalen != sizeof (struct msg_exit_data))
1.9 nicm 303: fatalx("bad MSG_EXIT size");
1.12 nicm 304:
1.25 nicm 305: client_write_server(MSG_EXITING, NULL, 0);
306: client_exitmsg = "exited";
1.9 nicm 307: break;
308: case MSG_EXITED:
1.12 nicm 309: if (datalen != 0)
1.9 nicm 310: fatalx("bad MSG_EXITED size");
311:
1.12 nicm 312: imsg_free(&imsg);
1.9 nicm 313: return (-1);
314: case MSG_SHUTDOWN:
1.12 nicm 315: if (datalen != 0)
1.9 nicm 316: fatalx("bad MSG_SHUTDOWN size");
317:
1.25 nicm 318: client_write_server(MSG_EXITING, NULL, 0);
319: client_exitmsg = "server exited";
1.32 nicm 320: client_exitval = 1;
1.9 nicm 321: break;
322: case MSG_SUSPEND:
1.12 nicm 323: if (datalen != 0)
1.9 nicm 324: fatalx("bad MSG_SUSPEND size");
325:
1.30 nicm 326: memset(&sigact, 0, sizeof sigact);
327: sigemptyset(&sigact.sa_mask);
328: sigact.sa_flags = SA_RESTART;
329: sigact.sa_handler = SIG_DFL;
330: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
331: fatal("sigaction failed");
332: kill(getpid(), SIGTSTP);
1.21 nicm 333: break;
334: case MSG_LOCK:
335: if (datalen != sizeof lockdata)
336: fatalx("bad MSG_LOCK size");
337: memcpy(&lockdata, imsg.data, sizeof lockdata);
1.35 nicm 338:
1.21 nicm 339: lockdata.cmd[(sizeof lockdata.cmd) - 1] = '\0';
340: system(lockdata.cmd);
1.25 nicm 341: client_write_server(MSG_UNLOCK, NULL, 0);
1.9 nicm 342: break;
343: default:
344: fatalx("unexpected message");
345: }
1.12 nicm 346:
347: imsg_free(&imsg);
1.9 nicm 348: }
1.1 nicm 349: }