Annotation of src/usr.bin/tmux/client.c, Revision 1.10
1.10 ! nicm 1: /* $OpenBSD: client.c,v 1.9 2009/07/30 16:32:12 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:
36: void client_handle_winch(struct client_ctx *);
37:
38: int
1.4 nicm 39: client_init(char *path, struct client_ctx *cctx, int cmdflags, int flags)
1.1 nicm 40: {
41: struct sockaddr_un sa;
42: struct stat sb;
43: struct msg_identify_data data;
44: struct winsize ws;
45: size_t size;
1.10 ! nicm 46: int fd, mode;
1.7 nicm 47: char *name, *term;
1.2 nicm 48: char rpathbuf[MAXPATHLEN];
49:
50: if (realpath(path, rpathbuf) == NULL)
51: strlcpy(rpathbuf, path, sizeof rpathbuf);
52: setproctitle("client (%s)", rpathbuf);
1.1 nicm 53:
54: if (lstat(path, &sb) != 0) {
1.4 nicm 55: if (cmdflags & CMD_STARTSERVER && errno == ENOENT) {
1.10 ! nicm 56: if ((fd = server_start(path)) == -1)
1.1 nicm 57: goto start_failed;
58: goto server_started;
59: }
60: goto not_found;
61: }
62: if (!S_ISSOCK(sb.st_mode)) {
63: errno = ENOTSOCK;
64: goto not_found;
65: }
66:
67: memset(&sa, 0, sizeof sa);
68: sa.sun_family = AF_UNIX;
69: size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
70: if (size >= sizeof sa.sun_path) {
71: errno = ENAMETOOLONG;
72: goto not_found;
73: }
74:
1.10 ! nicm 75: if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1.1 nicm 76: fatal("socket");
77:
1.10 ! nicm 78: if (connect(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
1.1 nicm 79: if (errno == ECONNREFUSED) {
1.4 nicm 80: if (unlink(path) != 0 || !(cmdflags & CMD_STARTSERVER))
1.1 nicm 81: goto not_found;
1.10 ! nicm 82: if ((fd = server_start(path)) == -1)
1.1 nicm 83: goto start_failed;
84: goto server_started;
85: }
86: goto not_found;
87: }
88:
89: server_started:
1.10 ! nicm 90: if ((mode = fcntl(fd, F_GETFL)) == -1)
1.1 nicm 91: fatal("fcntl failed");
1.10 ! nicm 92: if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
1.1 nicm 93: fatal("fcntl failed");
1.10 ! nicm 94: cctx->srv_fd = fd;
1.1 nicm 95: cctx->srv_in = buffer_create(BUFSIZ);
96: cctx->srv_out = buffer_create(BUFSIZ);
97:
98: if (isatty(STDIN_FILENO)) {
99: if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
100: fatal("ioctl(TIOCGWINSZ)");
101: data.version = PROTOCOL_VERSION;
102: data.flags = flags;
103: data.sx = ws.ws_col;
104: data.sy = ws.ws_row;
1.7 nicm 105:
1.1 nicm 106: if (getcwd(data.cwd, sizeof data.cwd) == NULL)
107: *data.cwd = '\0';
108:
1.7 nicm 109: *data.term = '\0';
110: if ((term = getenv("TERM")) != NULL) {
111: if (strlcpy(data.term,
112: term, sizeof data.term) >= sizeof data.term)
113: *data.term = '\0';
114: }
115:
116: *data.tty = '\0';
1.1 nicm 117: if ((name = ttyname(STDIN_FILENO)) == NULL)
118: fatal("ttyname failed");
119: if (strlcpy(data.tty, name, sizeof data.tty) >= sizeof data.tty)
120: fatalx("ttyname failed");
121:
1.7 nicm 122: client_write_server(cctx, MSG_IDENTIFY, &data, sizeof data);
1.1 nicm 123: }
124:
125: return (0);
126:
127: start_failed:
128: log_warnx("server failed to start");
129: return (1);
130:
131: not_found:
132: log_warn("server not found");
133: return (1);
134: }
135:
136: int
137: client_main(struct client_ctx *cctx)
138: {
139: struct pollfd pfd;
140:
141: siginit();
142:
143: logfile("client");
144:
1.8 nicm 145: for (;;) {
146: if (sigterm)
147: client_write_server(cctx, MSG_EXITING, NULL, 0);
1.1 nicm 148: if (sigchld) {
149: waitpid(WAIT_ANY, NULL, WNOHANG);
150: sigchld = 0;
151: }
152: if (sigwinch)
153: client_handle_winch(cctx);
154: if (sigcont) {
155: siginit();
156: client_write_server(cctx, MSG_WAKEUP, NULL, 0);
157: sigcont = 0;
158: }
159:
160: pfd.fd = cctx->srv_fd;
161: pfd.events = POLLIN;
162: if (BUFFER_USED(cctx->srv_out) > 0)
163: pfd.events |= POLLOUT;
164:
1.9 nicm 165: if (poll(&pfd, 1, INFTIM) == -1) {
1.1 nicm 166: if (errno == EAGAIN || errno == EINTR)
167: continue;
168: fatal("poll failed");
169: }
170:
1.5 nicm 171: if (buffer_poll(&pfd, cctx->srv_in, cctx->srv_out) != 0) {
172: cctx->exittype = CCTX_DIED;
173: break;
174: }
1.9 nicm 175:
176: if (client_msg_dispatch(cctx) != 0)
177: break;
1.1 nicm 178: }
179:
180: if (sigterm) {
181: printf("[terminated]\n");
182: return (1);
183: }
1.5 nicm 184: switch (cctx->exittype) {
185: case CCTX_DIED:
186: printf("[lost server]\n");
187: return (0);
188: case CCTX_SHUTDOWN:
1.1 nicm 189: printf("[server exited]\n");
190: return (0);
1.5 nicm 191: case CCTX_EXIT:
1.1 nicm 192: printf("[exited]\n");
193: return (0);
1.5 nicm 194: case CCTX_DETACH:
1.1 nicm 195: printf("[detached]\n");
196: return (0);
1.5 nicm 197: default:
198: printf("[error: %s]\n", cctx->errstr);
199: return (1);
1.1 nicm 200: }
201: }
202:
203: void
204: client_handle_winch(struct client_ctx *cctx)
205: {
206: struct msg_resize_data data;
207: struct winsize ws;
208:
209: if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
210: fatal("ioctl failed");
211:
212: data.sx = ws.ws_col;
213: data.sy = ws.ws_row;
214: client_write_server(cctx, MSG_RESIZE, &data, sizeof data);
215:
216: sigwinch = 0;
1.9 nicm 217: }
218:
219: int
220: client_msg_dispatch(struct client_ctx *cctx)
221: {
222: struct hdr hdr;
223: struct msg_print_data printdata;
224:
225: for (;;) {
226: if (BUFFER_USED(cctx->srv_in) < sizeof hdr)
227: return (0);
228: memcpy(&hdr, BUFFER_OUT(cctx->srv_in), sizeof hdr);
229: if (BUFFER_USED(cctx->srv_in) < (sizeof hdr) + hdr.size)
230: return (0);
231: buffer_remove(cctx->srv_in, sizeof hdr);
232:
233: switch (hdr.type) {
234: case MSG_DETACH:
235: if (hdr.size != 0)
236: fatalx("bad MSG_DETACH size");
237:
238: client_write_server(cctx, MSG_EXITING, NULL, 0);
239: cctx->exittype = CCTX_DETACH;
240: break;
241: case MSG_ERROR:
242: if (hdr.size != sizeof printdata)
243: fatalx("bad MSG_PRINT size");
244: buffer_read(cctx->srv_in, &printdata, sizeof printdata);
245: printdata.msg[(sizeof printdata.msg) - 1] = '\0';
246:
247: cctx->errstr = xstrdup(printdata.msg);
248: return (-1);
249: case MSG_EXIT:
250: if (hdr.size != 0)
251: fatalx("bad MSG_EXIT size");
252:
253: client_write_server(cctx, MSG_EXITING, NULL, 0);
254: cctx->exittype = CCTX_EXIT;
255: break;
256: case MSG_EXITED:
257: if (hdr.size != 0)
258: fatalx("bad MSG_EXITED size");
259:
260: return (-1);
261: case MSG_SHUTDOWN:
262: if (hdr.size != 0)
263: fatalx("bad MSG_SHUTDOWN size");
264:
265: client_write_server(cctx, MSG_EXITING, NULL, 0);
266: cctx->exittype = CCTX_SHUTDOWN;
267: break;
268: case MSG_SUSPEND:
269: if (hdr.size != 0)
270: fatalx("bad MSG_SUSPEND size");
271:
272: client_suspend();
273: break;
274: default:
275: fatalx("unexpected message");
276: }
277: }
1.1 nicm 278: }