Annotation of src/usr.bin/tmux/client.c, Revision 1.27
1.27 ! nicm 1: /* $OpenBSD: client.c,v 1.26 2009/10/21 21:11:55 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/ioctl.h>
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>
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;
37: const char *client_exitmsg;
1.1 nicm 38:
1.26 nicm 39: void client_send_identify(int);
1.25 nicm 40: void client_send_environ(void);
41: void client_write_server(enum msgtype, void *, size_t);
42: int client_dispatch(void);
43: void client_suspend(void);
44:
45: struct imsgbuf *
46: client_init(char *path, int cmdflags, int flags)
1.1 nicm 47: {
1.26 nicm 48: struct sockaddr_un sa;
49: struct stat sb;
50: size_t size;
51: int fd, mode;
52: char rpathbuf[MAXPATHLEN];
1.2 nicm 53:
54: if (realpath(path, rpathbuf) == NULL)
55: strlcpy(rpathbuf, path, sizeof rpathbuf);
56: setproctitle("client (%s)", rpathbuf);
1.1 nicm 57:
58: if (lstat(path, &sb) != 0) {
1.4 nicm 59: if (cmdflags & CMD_STARTSERVER && errno == ENOENT) {
1.10 nicm 60: if ((fd = server_start(path)) == -1)
1.1 nicm 61: goto start_failed;
62: goto server_started;
63: }
64: goto not_found;
65: }
66: if (!S_ISSOCK(sb.st_mode)) {
67: errno = ENOTSOCK;
68: goto not_found;
69: }
70:
71: memset(&sa, 0, sizeof sa);
72: sa.sun_family = AF_UNIX;
73: size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
74: if (size >= sizeof sa.sun_path) {
75: errno = ENAMETOOLONG;
76: goto not_found;
77: }
78:
1.10 nicm 79: if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1.18 nicm 80: fatal("socket failed");
1.1 nicm 81:
1.10 nicm 82: if (connect(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
1.1 nicm 83: if (errno == ECONNREFUSED) {
1.4 nicm 84: if (unlink(path) != 0 || !(cmdflags & CMD_STARTSERVER))
1.1 nicm 85: goto not_found;
1.10 nicm 86: if ((fd = server_start(path)) == -1)
1.1 nicm 87: goto start_failed;
88: goto server_started;
89: }
90: goto not_found;
91: }
92:
93: server_started:
1.10 nicm 94: if ((mode = fcntl(fd, F_GETFL)) == -1)
1.1 nicm 95: fatal("fcntl failed");
1.10 nicm 96: if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
1.22 nicm 97: fatal("fcntl failed");
98: if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
1.1 nicm 99: fatal("fcntl failed");
1.25 nicm 100: imsg_init(&client_ibuf, fd);
1.1 nicm 101:
1.11 nicm 102: if (cmdflags & CMD_SENDENVIRON)
1.25 nicm 103: client_send_environ();
1.26 nicm 104: if (isatty(STDIN_FILENO))
105: client_send_identify(flags);
1.1 nicm 106:
1.25 nicm 107: return (&client_ibuf);
1.1 nicm 108:
109: start_failed:
110: log_warnx("server failed to start");
1.25 nicm 111: return (NULL);
1.1 nicm 112:
113: not_found:
114: log_warn("server not found");
1.25 nicm 115: return (NULL);
1.1 nicm 116: }
117:
1.11 nicm 118: void
1.26 nicm 119: client_send_identify(int flags)
120: {
121: struct msg_identify_data data;
122: struct winsize ws;
123: char *term;
124: int fd;
125:
126: if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
127: fatal("ioctl(TIOCGWINSZ)");
128: data.flags = flags;
129:
130: if (getcwd(data.cwd, sizeof data.cwd) == NULL)
131: *data.cwd = '\0';
132:
133: term = getenv("TERM");
134: if (term == NULL ||
135: strlcpy(data.term, term, sizeof data.term) >= sizeof data.term)
136: *data.term = '\0';
137:
138: if ((fd = dup(STDIN_FILENO)) == -1)
139: fatal("dup failed");
140: imsg_compose(&client_ibuf,
141: MSG_IDENTIFY, PROTOCOL_VERSION, -1, fd, &data, sizeof data);
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:
150: for (var = environ; *var != NULL; var++) {
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: {
160: imsg_compose(&client_ibuf, type, PROTOCOL_VERSION, -1, -1, buf, len);
161: }
162:
163: __dead void
164: client_main(void)
1.1 nicm 165: {
166: struct pollfd pfd;
1.17 nicm 167: int n, nfds;
1.1 nicm 168:
169: siginit();
170:
171: logfile("client");
172:
1.17 nicm 173: /*
174: * imsg_read in the first client poll loop (before the terminal has
175: * been initialiased) may have read messages into the buffer after the
176: * MSG_READY switched to here. Process anything outstanding now so poll
177: * doesn't hang waiting for messages that have already arrived.
178: */
1.25 nicm 179: if (client_dispatch() != 0)
1.17 nicm 180: goto out;
181:
1.8 nicm 182: for (;;) {
1.25 nicm 183: if (sigterm) {
184: client_exitmsg = "terminated";
185: client_write_server(MSG_EXITING, NULL, 0);
186: }
1.1 nicm 187: if (sigchld) {
1.27 ! nicm 188: sigchld = 0;
1.1 nicm 189: waitpid(WAIT_ANY, NULL, WNOHANG);
1.27 ! nicm 190: continue;
1.1 nicm 191: }
1.20 nicm 192: if (sigwinch) {
1.27 ! nicm 193: sigwinch = 0;
1.25 nicm 194: client_write_server(MSG_RESIZE, NULL, 0);
1.27 ! nicm 195: continue;
1.20 nicm 196: }
1.1 nicm 197: if (sigcont) {
1.27 ! nicm 198: sigcont = 0;
1.1 nicm 199: siginit();
1.25 nicm 200: client_write_server(MSG_WAKEUP, NULL, 0);
1.27 ! nicm 201: continue;
1.1 nicm 202: }
203:
1.25 nicm 204: pfd.fd = client_ibuf.fd;
1.1 nicm 205: pfd.events = POLLIN;
1.25 nicm 206: if (client_ibuf.w.queued > 0)
1.1 nicm 207: pfd.events |= POLLOUT;
208:
1.12 nicm 209: if ((nfds = poll(&pfd, 1, INFTIM)) == -1) {
1.1 nicm 210: if (errno == EAGAIN || errno == EINTR)
211: continue;
212: fatal("poll failed");
213: }
1.12 nicm 214: if (nfds == 0)
215: continue;
216:
217: if (pfd.revents & (POLLERR|POLLHUP|POLLNVAL))
218: fatalx("socket error");
1.1 nicm 219:
1.12 nicm 220: if (pfd.revents & POLLIN) {
1.25 nicm 221: if ((n = imsg_read(&client_ibuf)) == -1 || n == 0) {
222: client_exitmsg = "lost server";
1.17 nicm 223: break;
224: }
1.25 nicm 225: if (client_dispatch() != 0)
1.12 nicm 226: break;
1.5 nicm 227: }
1.9 nicm 228:
1.12 nicm 229: if (pfd.revents & POLLOUT) {
1.25 nicm 230: if (msgbuf_write(&client_ibuf.w) < 0) {
231: client_exitmsg = "lost server";
1.12 nicm 232: break;
233: }
234: }
1.1 nicm 235: }
1.17 nicm 236:
237: out:
1.25 nicm 238: /* Print the exit message, if any, and exit. */
239: if (client_exitmsg != NULL) {
1.24 nicm 240: if (!login_shell)
1.25 nicm 241: printf("[%s]\n", client_exitmsg);
242: exit(1);
1.1 nicm 243: }
1.25 nicm 244: exit(0);
1.9 nicm 245: }
246:
247: int
1.25 nicm 248: client_dispatch(void)
1.9 nicm 249: {
1.12 nicm 250: struct imsg imsg;
1.21 nicm 251: struct msg_lock_data lockdata;
1.12 nicm 252: ssize_t n, datalen;
1.9 nicm 253:
254: for (;;) {
1.25 nicm 255: if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
1.12 nicm 256: fatalx("imsg_get failed");
257: if (n == 0)
1.9 nicm 258: return (0);
1.12 nicm 259: datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1.9 nicm 260:
1.12 nicm 261: switch (imsg.hdr.type) {
1.9 nicm 262: case MSG_DETACH:
1.12 nicm 263: if (datalen != 0)
1.9 nicm 264: fatalx("bad MSG_DETACH size");
265:
1.25 nicm 266: client_write_server(MSG_EXITING, NULL, 0);
267: client_exitmsg = "detached";
1.9 nicm 268: break;
269: case MSG_EXIT:
1.12 nicm 270: if (datalen != 0)
1.9 nicm 271: fatalx("bad MSG_EXIT size");
1.12 nicm 272:
1.25 nicm 273: client_write_server(MSG_EXITING, NULL, 0);
274: client_exitmsg = "exited";
1.9 nicm 275: break;
276: case MSG_EXITED:
1.12 nicm 277: if (datalen != 0)
1.9 nicm 278: fatalx("bad MSG_EXITED size");
279:
1.12 nicm 280: imsg_free(&imsg);
1.9 nicm 281: return (-1);
282: case MSG_SHUTDOWN:
1.12 nicm 283: if (datalen != 0)
1.9 nicm 284: fatalx("bad MSG_SHUTDOWN size");
285:
1.25 nicm 286: client_write_server(MSG_EXITING, NULL, 0);
287: client_exitmsg = "server exited";
1.9 nicm 288: break;
289: case MSG_SUSPEND:
1.12 nicm 290: if (datalen != 0)
1.9 nicm 291: fatalx("bad MSG_SUSPEND size");
292:
293: client_suspend();
1.21 nicm 294: break;
295: case MSG_LOCK:
296: if (datalen != sizeof lockdata)
297: fatalx("bad MSG_LOCK size");
298: memcpy(&lockdata, imsg.data, sizeof lockdata);
299:
300: lockdata.cmd[(sizeof lockdata.cmd) - 1] = '\0';
301: system(lockdata.cmd);
1.25 nicm 302: client_write_server(MSG_UNLOCK, NULL, 0);
1.9 nicm 303: break;
304: default:
305: fatalx("unexpected message");
306: }
1.12 nicm 307:
308: imsg_free(&imsg);
1.9 nicm 309: }
1.25 nicm 310: }
311:
312: void
313: client_suspend(void)
314: {
315: struct sigaction act;
316:
317: memset(&act, 0, sizeof act);
318: sigemptyset(&act.sa_mask);
319: act.sa_flags = SA_RESTART;
320:
321: act.sa_handler = SIG_DFL;
322: if (sigaction(SIGTSTP, &act, NULL) != 0)
323: fatal("sigaction failed");
324:
325: act.sa_handler = sighandler;
326: if (sigaction(SIGCONT, &act, NULL) != 0)
327: fatal("sigaction failed");
328:
329: kill(getpid(), SIGTSTP);
1.1 nicm 330: }