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