Annotation of src/usr.bin/tmux/client.c, Revision 1.3
1.3 ! nicm 1: /* $OpenBSD: client.c,v 1.2 2009/06/05 07:15:58 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
39: client_init(char *path, struct client_ctx *cctx, int start_server, int flags)
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) {
56: if (start_server && errno == ENOENT) {
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) {
82: if (unlink(path) != 0 || !start_server)
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: char *error;
138: int xtimeout; /* Yay for ncurses namespace! */
139:
140: siginit();
141:
142: logfile("client");
143:
144: error = NULL;
145: while (!sigterm) {
146: if (sigchld) {
147: waitpid(WAIT_ANY, NULL, WNOHANG);
148: sigchld = 0;
149: }
150: if (sigwinch)
151: client_handle_winch(cctx);
152: if (sigcont) {
153: siginit();
154: client_write_server(cctx, MSG_WAKEUP, NULL, 0);
155: sigcont = 0;
156: }
157:
158: switch (client_msg_dispatch(cctx, &error)) {
159: case -1:
160: goto out;
161: case 0:
162: /* May be more in buffer, don't let poll block. */
163: xtimeout = 0;
164: break;
165: default:
166: /* Out of data, poll may block. */
167: xtimeout = INFTIM;
168: break;
169: }
170:
171: pfd.fd = cctx->srv_fd;
172: pfd.events = POLLIN;
173: if (BUFFER_USED(cctx->srv_out) > 0)
174: pfd.events |= POLLOUT;
175:
176: if (poll(&pfd, 1, xtimeout) == -1) {
177: if (errno == EAGAIN || errno == EINTR)
178: continue;
179: fatal("poll failed");
180: }
181:
182: if (buffer_poll(&pfd, cctx->srv_in, cctx->srv_out) != 0)
183: goto server_dead;
184: }
185:
186: out:
187: if (sigterm) {
188: printf("[terminated]\n");
189: return (1);
190: }
191:
192: if (cctx->flags & CCTX_SHUTDOWN) {
193: printf("[server exited]\n");
194: return (0);
195: }
196:
197: if (cctx->flags & CCTX_EXIT) {
198: printf("[exited]\n");
199: return (0);
200: }
201:
202: if (cctx->flags & CCTX_DETACH) {
203: printf("[detached]\n");
204: return (0);
205: }
206:
207: printf("[error: %s]\n", error);
208: return (1);
209:
210: server_dead:
211: printf("[lost server]\n");
212: return (0);
213: }
214:
215: void
216: client_handle_winch(struct client_ctx *cctx)
217: {
218: struct msg_resize_data data;
219: struct winsize ws;
220:
221: if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
222: fatal("ioctl failed");
223:
224: data.sx = ws.ws_col;
225: data.sy = ws.ws_row;
226: client_write_server(cctx, MSG_RESIZE, &data, sizeof data);
227:
228: sigwinch = 0;
229: }