Annotation of src/usr.bin/tmux/client.c, Revision 1.7
1.7 ! nicm 1: /* $OpenBSD: client.c,v 1.6 2009/07/23 21:19:11 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;
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.1 nicm 56: if ((cctx->srv_fd = server_start(path)) == -1)
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:
75: if ((cctx->srv_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
76: fatal("socket");
77:
78: if (connect(
79: cctx->srv_fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
80: if (errno == ECONNREFUSED) {
1.4 nicm 81: if (unlink(path) != 0 || !(cmdflags & CMD_STARTSERVER))
1.1 nicm 82: goto not_found;
83: if ((cctx->srv_fd = server_start(path)) == -1)
84: goto start_failed;
85: goto server_started;
86: }
87: goto not_found;
88: }
89:
90: server_started:
91: if ((mode = fcntl(cctx->srv_fd, F_GETFL)) == -1)
92: fatal("fcntl failed");
93: if (fcntl(cctx->srv_fd, F_SETFL, mode|O_NONBLOCK) == -1)
94: fatal("fcntl failed");
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: int xtimeout; /* Yay for ncurses namespace! */
141:
142: siginit();
143:
144: logfile("client");
145:
146: while (!sigterm) {
147: if (sigchld) {
148: waitpid(WAIT_ANY, NULL, WNOHANG);
149: sigchld = 0;
150: }
151: if (sigwinch)
152: client_handle_winch(cctx);
153: if (sigcont) {
154: siginit();
155: client_write_server(cctx, MSG_WAKEUP, NULL, 0);
156: sigcont = 0;
157: }
158:
1.5 nicm 159: switch (client_msg_dispatch(cctx)) {
1.1 nicm 160: case -1:
161: goto out;
162: case 0:
163: /* May be more in buffer, don't let poll block. */
164: xtimeout = 0;
165: break;
166: default:
167: /* Out of data, poll may block. */
168: xtimeout = INFTIM;
169: break;
170: }
171:
172: pfd.fd = cctx->srv_fd;
173: pfd.events = POLLIN;
174: if (BUFFER_USED(cctx->srv_out) > 0)
175: pfd.events |= POLLOUT;
176:
177: if (poll(&pfd, 1, xtimeout) == -1) {
178: if (errno == EAGAIN || errno == EINTR)
179: continue;
180: fatal("poll failed");
181: }
182:
1.5 nicm 183: if (buffer_poll(&pfd, cctx->srv_in, cctx->srv_out) != 0) {
184: cctx->exittype = CCTX_DIED;
185: break;
186: }
1.1 nicm 187: }
188:
189: out:
190: if (sigterm) {
191: printf("[terminated]\n");
192: return (1);
193: }
1.5 nicm 194: switch (cctx->exittype) {
195: case CCTX_DIED:
196: printf("[lost server]\n");
197: return (0);
198: case CCTX_SHUTDOWN:
1.1 nicm 199: printf("[server exited]\n");
200: return (0);
1.5 nicm 201: case CCTX_EXIT:
1.1 nicm 202: printf("[exited]\n");
203: return (0);
1.5 nicm 204: case CCTX_DETACH:
1.1 nicm 205: printf("[detached]\n");
206: return (0);
1.5 nicm 207: default:
208: printf("[error: %s]\n", cctx->errstr);
209: return (1);
1.1 nicm 210: }
211: }
212:
213: void
214: client_handle_winch(struct client_ctx *cctx)
215: {
216: struct msg_resize_data data;
217: struct winsize ws;
218:
219: if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
220: fatal("ioctl failed");
221:
222: data.sx = ws.ws_col;
223: data.sy = ws.ws_row;
224: client_write_server(cctx, MSG_RESIZE, &data, sizeof data);
225:
226: sigwinch = 0;
227: }