Annotation of src/usr.bin/tmux/client.c, Revision 1.31
1.31 ! nicm 1: /* $OpenBSD: client.c,v 1.30 2009/11/04 20:50: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>
1.30 nicm 27: #include <event.h>
1.1 nicm 28: #include <fcntl.h>
29: #include <pwd.h>
30: #include <stdlib.h>
31: #include <string.h>
32: #include <syslog.h>
33: #include <unistd.h>
34:
35: #include "tmux.h"
36:
1.25 nicm 37: struct imsgbuf client_ibuf;
1.30 nicm 38: struct event client_event;
1.25 nicm 39: const char *client_exitmsg;
1.1 nicm 40:
1.30 nicm 41: void client_send_identify(int);
42: void client_send_environ(void);
43: void client_write_server(enum msgtype, void *, size_t);
1.31 ! nicm 44: void client_update_event(void);
1.30 nicm 45: void client_signal(int, short, void *);
46: void client_callback(int, short, void *);
47: int client_dispatch(void);
1.25 nicm 48:
49: struct imsgbuf *
50: client_init(char *path, int cmdflags, int flags)
1.1 nicm 51: {
1.26 nicm 52: struct sockaddr_un sa;
53: size_t size;
54: int fd, mode;
55: char rpathbuf[MAXPATHLEN];
1.2 nicm 56:
57: if (realpath(path, rpathbuf) == NULL)
58: strlcpy(rpathbuf, path, sizeof rpathbuf);
59: setproctitle("client (%s)", rpathbuf);
1.1 nicm 60:
61: memset(&sa, 0, sizeof sa);
62: sa.sun_family = AF_UNIX;
63: size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
64: if (size >= sizeof sa.sun_path) {
65: errno = ENAMETOOLONG;
66: goto not_found;
67: }
68:
1.10 nicm 69: if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1.18 nicm 70: fatal("socket failed");
1.1 nicm 71:
1.10 nicm 72: if (connect(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
1.28 nicm 73: if (!(cmdflags & CMD_STARTSERVER))
74: goto not_found;
75: switch (errno) {
76: case ECONNREFUSED:
77: if (unlink(path) != 0)
1.1 nicm 78: goto not_found;
1.28 nicm 79: /* FALLTHROUGH */
80: case ENOENT:
1.10 nicm 81: if ((fd = server_start(path)) == -1)
1.1 nicm 82: goto start_failed;
83: goto server_started;
84: }
85: goto not_found;
86: }
87:
88: server_started:
1.10 nicm 89: if ((mode = fcntl(fd, F_GETFL)) == -1)
1.1 nicm 90: fatal("fcntl failed");
1.10 nicm 91: if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
1.22 nicm 92: fatal("fcntl failed");
93: if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
1.1 nicm 94: fatal("fcntl failed");
1.25 nicm 95: imsg_init(&client_ibuf, fd);
1.1 nicm 96:
1.11 nicm 97: if (cmdflags & CMD_SENDENVIRON)
1.25 nicm 98: client_send_environ();
1.26 nicm 99: if (isatty(STDIN_FILENO))
100: client_send_identify(flags);
1.1 nicm 101:
1.25 nicm 102: return (&client_ibuf);
1.1 nicm 103:
104: start_failed:
105: log_warnx("server failed to start");
1.25 nicm 106: return (NULL);
1.1 nicm 107:
108: not_found:
109: log_warn("server not found");
1.25 nicm 110: return (NULL);
1.1 nicm 111: }
112:
1.11 nicm 113: void
1.26 nicm 114: client_send_identify(int flags)
115: {
116: struct msg_identify_data data;
117: struct winsize ws;
118: char *term;
119: int fd;
120:
121: if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
122: fatal("ioctl(TIOCGWINSZ)");
123: data.flags = flags;
124:
125: if (getcwd(data.cwd, sizeof data.cwd) == NULL)
126: *data.cwd = '\0';
127:
128: term = getenv("TERM");
129: if (term == NULL ||
130: strlcpy(data.term, term, sizeof data.term) >= sizeof data.term)
131: *data.term = '\0';
132:
133: if ((fd = dup(STDIN_FILENO)) == -1)
134: fatal("dup failed");
135: imsg_compose(&client_ibuf,
136: MSG_IDENTIFY, PROTOCOL_VERSION, -1, fd, &data, sizeof data);
137: }
138:
139: void
1.25 nicm 140: client_send_environ(void)
1.11 nicm 141: {
1.26 nicm 142: struct msg_environ_data data;
1.11 nicm 143: char **var;
144:
145: for (var = environ; *var != NULL; var++) {
146: if (strlcpy(data.var, *var, sizeof data.var) >= sizeof data.var)
147: continue;
1.25 nicm 148: client_write_server(MSG_ENVIRON, &data, sizeof data);
1.11 nicm 149: }
150: }
151:
1.25 nicm 152: void
153: client_write_server(enum msgtype type, void *buf, size_t len)
154: {
155: imsg_compose(&client_ibuf, type, PROTOCOL_VERSION, -1, -1, buf, len);
156: }
157:
1.31 ! nicm 158: void
! 159: client_update_event(void)
! 160: {
! 161: short events;
! 162:
! 163: event_del(&client_event);
! 164: events = EV_READ;
! 165: if (client_ibuf.w.queued > 0)
! 166: events |= EV_WRITE;
! 167: event_set(&client_event, client_ibuf.fd, events, client_callback, NULL);
! 168: event_add(&client_event, NULL);
! 169: }
! 170:
1.25 nicm 171: __dead void
172: client_main(void)
1.1 nicm 173: {
1.30 nicm 174: struct event ev_sigcont, ev_sigterm, ev_sigwinch;
175: struct sigaction sigact;
1.1 nicm 176:
1.30 nicm 177: logfile("client");
178:
179: /* Note: event_init() has already been called. */
180:
181: /* Set up signals. */
182: memset(&sigact, 0, sizeof sigact);
183: sigemptyset(&sigact.sa_mask);
184: sigact.sa_flags = SA_RESTART;
185: sigact.sa_handler = SIG_IGN;
186: if (sigaction(SIGINT, &sigact, NULL) != 0)
187: fatal("sigaction failed");
188: if (sigaction(SIGPIPE, &sigact, NULL) != 0)
189: fatal("sigaction failed");
190: if (sigaction(SIGUSR1, &sigact, NULL) != 0)
191: fatal("sigaction failed");
192: if (sigaction(SIGUSR2, &sigact, NULL) != 0)
193: fatal("sigaction failed");
194: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
195: fatal("sigaction failed");
1.1 nicm 196:
1.30 nicm 197: signal_set(&ev_sigcont, SIGCONT, client_signal, NULL);
198: signal_add(&ev_sigcont, NULL);
199: signal_set(&ev_sigterm, SIGTERM, client_signal, NULL);
200: signal_add(&ev_sigterm, NULL);
201: signal_set(&ev_sigwinch, SIGWINCH, client_signal, NULL);
202: signal_add(&ev_sigwinch, NULL);
1.1 nicm 203:
1.17 nicm 204: /*
205: * imsg_read in the first client poll loop (before the terminal has
1.30 nicm 206: * been initialised) may have read messages into the buffer after the
207: * MSG_READY switched to here. Process anything outstanding now to
208: * avoid hanging waiting for messages that have already arrived.
1.17 nicm 209: */
1.25 nicm 210: if (client_dispatch() != 0)
1.17 nicm 211: goto out;
212:
1.31 ! nicm 213: /* Set the event and dispatch. */
! 214: client_update_event();
1.30 nicm 215: event_dispatch();
1.17 nicm 216:
217: out:
1.25 nicm 218: /* Print the exit message, if any, and exit. */
219: if (client_exitmsg != NULL) {
1.24 nicm 220: if (!login_shell)
1.25 nicm 221: printf("[%s]\n", client_exitmsg);
222: exit(1);
1.1 nicm 223: }
1.25 nicm 224: exit(0);
1.9 nicm 225: }
226:
1.30 nicm 227: void
1.31 ! nicm 228: client_signal(int sig, unused short events, unused void *data)
1.30 nicm 229: {
230: struct sigaction sigact;
231:
232: switch (sig) {
233: case SIGTERM:
234: client_exitmsg = "terminated";
235: client_write_server(MSG_EXITING, NULL, 0);
236: break;
237: case SIGWINCH:
238: client_write_server(MSG_RESIZE, NULL, 0);
239: break;
240: case SIGCONT:
241: memset(&sigact, 0, sizeof sigact);
242: sigemptyset(&sigact.sa_mask);
243: sigact.sa_flags = SA_RESTART;
244: sigact.sa_handler = SIG_IGN;
245: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
246: fatal("sigaction failed");
247: client_write_server(MSG_WAKEUP, NULL, 0);
248: break;
249: }
250:
1.31 ! nicm 251: client_update_event();
1.30 nicm 252: }
253:
254: void
255: client_callback(unused int fd, short events, unused void *data)
256: {
257: int n;
258:
259: if (events & EV_READ) {
260: if ((n = imsg_read(&client_ibuf)) == -1 || n == 0)
261: goto lost_server;
262: if (client_dispatch() != 0) {
263: event_loopexit(NULL);
264: return;
265: }
266: }
267:
268: if (events & EV_WRITE) {
269: if (msgbuf_write(&client_ibuf.w) < 0)
270: goto lost_server;
271: }
272:
1.31 ! nicm 273: client_update_event();
1.30 nicm 274: return;
275:
276: lost_server:
277: client_exitmsg = "lost server";
278: event_loopexit(NULL);
279: }
280:
1.9 nicm 281: int
1.25 nicm 282: client_dispatch(void)
1.9 nicm 283: {
1.30 nicm 284: struct imsg imsg;
285: struct msg_lock_data lockdata;
286: struct sigaction sigact;
287: ssize_t n, datalen;
1.9 nicm 288:
289: for (;;) {
1.25 nicm 290: if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
1.12 nicm 291: fatalx("imsg_get failed");
292: if (n == 0)
1.9 nicm 293: return (0);
1.12 nicm 294: datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1.9 nicm 295:
1.30 nicm 296: log_debug("client got %d", imsg.hdr.type);
1.12 nicm 297: switch (imsg.hdr.type) {
1.9 nicm 298: case MSG_DETACH:
1.12 nicm 299: if (datalen != 0)
1.9 nicm 300: fatalx("bad MSG_DETACH size");
301:
1.25 nicm 302: client_write_server(MSG_EXITING, NULL, 0);
303: client_exitmsg = "detached";
1.9 nicm 304: break;
305: case MSG_EXIT:
1.12 nicm 306: if (datalen != 0)
1.9 nicm 307: fatalx("bad MSG_EXIT size");
1.12 nicm 308:
1.25 nicm 309: client_write_server(MSG_EXITING, NULL, 0);
310: client_exitmsg = "exited";
1.9 nicm 311: break;
312: case MSG_EXITED:
1.12 nicm 313: if (datalen != 0)
1.9 nicm 314: fatalx("bad MSG_EXITED size");
315:
1.12 nicm 316: imsg_free(&imsg);
1.9 nicm 317: return (-1);
318: case MSG_SHUTDOWN:
1.12 nicm 319: if (datalen != 0)
1.9 nicm 320: fatalx("bad MSG_SHUTDOWN size");
321:
1.25 nicm 322: client_write_server(MSG_EXITING, NULL, 0);
323: client_exitmsg = "server exited";
1.9 nicm 324: break;
325: case MSG_SUSPEND:
1.12 nicm 326: if (datalen != 0)
1.9 nicm 327: fatalx("bad MSG_SUSPEND size");
328:
1.30 nicm 329: memset(&sigact, 0, sizeof sigact);
330: sigemptyset(&sigact.sa_mask);
331: sigact.sa_flags = SA_RESTART;
332: sigact.sa_handler = SIG_DFL;
333: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
334: fatal("sigaction failed");
335: kill(getpid(), SIGTSTP);
1.21 nicm 336: break;
337: case MSG_LOCK:
338: if (datalen != sizeof lockdata)
339: fatalx("bad MSG_LOCK size");
340: memcpy(&lockdata, imsg.data, sizeof lockdata);
341:
342: lockdata.cmd[(sizeof lockdata.cmd) - 1] = '\0';
343: system(lockdata.cmd);
1.25 nicm 344: client_write_server(MSG_UNLOCK, NULL, 0);
1.9 nicm 345: break;
346: default:
347: fatalx("unexpected message");
348: }
1.12 nicm 349:
350: imsg_free(&imsg);
1.9 nicm 351: }
1.1 nicm 352: }