Annotation of src/usr.bin/tmux/client.c, Revision 1.6
1.6 ! nicm 1: /* $OpenBSD: client.c,v 1.5 2009/07/23 20:24:27 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;
46: int mode;
47: struct buffer *b;
48: char *name;
1.2 nicm 49: char rpathbuf[MAXPATHLEN];
50:
51: if (realpath(path, rpathbuf) == NULL)
52: strlcpy(rpathbuf, path, sizeof rpathbuf);
53: setproctitle("client (%s)", rpathbuf);
1.1 nicm 54:
55: if (lstat(path, &sb) != 0) {
1.4 nicm 56: if (cmdflags & CMD_STARTSERVER && errno == ENOENT) {
1.1 nicm 57: if ((cctx->srv_fd = server_start(path)) == -1)
58: goto start_failed;
59: goto server_started;
60: }
61: goto not_found;
62: }
63: if (!S_ISSOCK(sb.st_mode)) {
64: errno = ENOTSOCK;
65: goto not_found;
66: }
67:
68: memset(&sa, 0, sizeof sa);
69: sa.sun_family = AF_UNIX;
70: size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
71: if (size >= sizeof sa.sun_path) {
72: errno = ENAMETOOLONG;
73: goto not_found;
74: }
75:
76: if ((cctx->srv_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
77: fatal("socket");
78:
79: if (connect(
80: cctx->srv_fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
81: if (errno == ECONNREFUSED) {
1.4 nicm 82: if (unlink(path) != 0 || !(cmdflags & CMD_STARTSERVER))
1.1 nicm 83: goto not_found;
84: if ((cctx->srv_fd = server_start(path)) == -1)
85: goto start_failed;
86: goto server_started;
87: }
88: goto not_found;
89: }
90:
91: server_started:
92: if ((mode = fcntl(cctx->srv_fd, F_GETFL)) == -1)
93: fatal("fcntl failed");
94: if (fcntl(cctx->srv_fd, F_SETFL, mode|O_NONBLOCK) == -1)
95: fatal("fcntl failed");
96: cctx->srv_in = buffer_create(BUFSIZ);
97: cctx->srv_out = buffer_create(BUFSIZ);
98:
99: if (isatty(STDIN_FILENO)) {
100: if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
101: fatal("ioctl(TIOCGWINSZ)");
102: data.version = PROTOCOL_VERSION;
103: data.flags = flags;
104: data.sx = ws.ws_col;
105: data.sy = ws.ws_row;
106: *data.tty = '\0';
107: if (getcwd(data.cwd, sizeof data.cwd) == NULL)
108: *data.cwd = '\0';
109:
110: if ((name = ttyname(STDIN_FILENO)) == NULL)
111: fatal("ttyname failed");
112: if (strlcpy(data.tty, name, sizeof data.tty) >= sizeof data.tty)
113: fatalx("ttyname failed");
114:
115: b = buffer_create(BUFSIZ);
116: cmd_send_string(b, getenv("TERM"));
117: client_write_server2(cctx, MSG_IDENTIFY,
118: &data, sizeof data, BUFFER_OUT(b), BUFFER_USED(b));
119: buffer_destroy(b);
120: }
121:
122: return (0);
123:
124: start_failed:
125: log_warnx("server failed to start");
126: return (1);
127:
128: not_found:
129: log_warn("server not found");
130: return (1);
131: }
132:
133: int
134: client_main(struct client_ctx *cctx)
135: {
136: struct pollfd pfd;
137: int xtimeout; /* Yay for ncurses namespace! */
138:
139: siginit();
140:
141: logfile("client");
142:
143: while (!sigterm) {
144: if (sigchld) {
145: waitpid(WAIT_ANY, NULL, WNOHANG);
146: sigchld = 0;
147: }
148: if (sigwinch)
149: client_handle_winch(cctx);
150: if (sigcont) {
151: siginit();
152: client_write_server(cctx, MSG_WAKEUP, NULL, 0);
153: sigcont = 0;
154: }
155:
1.5 nicm 156: switch (client_msg_dispatch(cctx)) {
1.1 nicm 157: case -1:
158: goto out;
159: case 0:
160: /* May be more in buffer, don't let poll block. */
161: xtimeout = 0;
162: break;
163: default:
164: /* Out of data, poll may block. */
165: xtimeout = INFTIM;
166: break;
167: }
168:
169: pfd.fd = cctx->srv_fd;
170: pfd.events = POLLIN;
171: if (BUFFER_USED(cctx->srv_out) > 0)
172: pfd.events |= POLLOUT;
173:
174: if (poll(&pfd, 1, xtimeout) == -1) {
175: if (errno == EAGAIN || errno == EINTR)
176: continue;
177: fatal("poll failed");
178: }
179:
1.5 nicm 180: if (buffer_poll(&pfd, cctx->srv_in, cctx->srv_out) != 0) {
181: cctx->exittype = CCTX_DIED;
182: break;
183: }
1.1 nicm 184: }
185:
186: out:
187: if (sigterm) {
188: printf("[terminated]\n");
189: return (1);
190: }
1.5 nicm 191: switch (cctx->exittype) {
192: case CCTX_DIED:
193: printf("[lost server]\n");
194: return (0);
195: case CCTX_SHUTDOWN:
1.1 nicm 196: printf("[server exited]\n");
197: return (0);
1.5 nicm 198: case CCTX_EXIT:
1.1 nicm 199: printf("[exited]\n");
200: return (0);
1.5 nicm 201: case CCTX_DETACH:
1.1 nicm 202: printf("[detached]\n");
203: return (0);
1.5 nicm 204: default:
205: printf("[error: %s]\n", cctx->errstr);
206: return (1);
1.1 nicm 207: }
208: }
209:
210: void
211: client_handle_winch(struct client_ctx *cctx)
212: {
213: struct msg_resize_data data;
214: struct winsize ws;
215:
216: if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
217: fatal("ioctl failed");
218:
219: data.sx = ws.ws_col;
220: data.sy = ws.ws_row;
221: client_write_server(cctx, MSG_RESIZE, &data, sizeof data);
222:
223: sigwinch = 0;
224: }