Annotation of src/usr.bin/tmux/server.c, Revision 1.27
1.27 ! nicm 1: /* $OpenBSD: server.c,v 1.26 2009/08/31 20:46:19 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>
1.5 nicm 28: #include <paths.h>
1.1 nicm 29: #include <signal.h>
30: #include <stdio.h>
31: #include <stdlib.h>
32: #include <string.h>
33: #include <syslog.h>
34: #include <termios.h>
35: #include <time.h>
36: #include <unistd.h>
37:
38: #include "tmux.h"
39:
40: /*
41: * Main server functions.
42: */
43:
44: /* Client list. */
45: struct clients clients;
46:
1.13 nicm 47: void server_create_client(int);
1.1 nicm 48: int server_create_socket(void);
49: int server_main(int);
50: void server_shutdown(void);
51: void server_child_signal(void);
52: void server_fill_windows(struct pollfd **);
53: void server_handle_windows(struct pollfd **);
54: void server_fill_clients(struct pollfd **);
55: void server_handle_clients(struct pollfd **);
1.13 nicm 56: void server_accept_client(int);
1.1 nicm 57: void server_handle_client(struct client *);
58: void server_handle_window(struct window *, struct window_pane *);
1.10 nicm 59: int server_check_window_bell(struct session *, struct window *);
1.1 nicm 60: int server_check_window_activity(struct session *,
61: struct window *);
62: int server_check_window_content(struct session *, struct window *,
63: struct window_pane *);
64: void server_lost_client(struct client *);
65: void server_check_window(struct window *);
66: void server_check_redraw(struct client *);
67: void server_redraw_locked(struct client *);
68: void server_check_timers(struct client *);
69: void server_second_timers(void);
70: int server_update_socket(void);
71:
72: /* Create a new client. */
1.13 nicm 73: void
1.1 nicm 74: server_create_client(int fd)
75: {
76: struct client *c;
77: int mode;
78: u_int i;
79:
80: if ((mode = fcntl(fd, F_GETFL)) == -1)
81: fatal("fcntl failed");
82: if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
83: fatal("fcntl failed");
84: if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
85: fatal("fcntl failed");
86:
87: c = xcalloc(1, sizeof *c);
1.18 nicm 88: imsg_init(&c->ibuf, fd);
1.1 nicm 89:
90: ARRAY_INIT(&c->prompt_hdata);
91:
92: c->tty.fd = -1;
93: c->title = NULL;
94:
95: c->session = NULL;
96: c->tty.sx = 80;
97: c->tty.sy = 25;
98: screen_init(&c->status, c->tty.sx, 1, 0);
99:
100: c->message_string = NULL;
101:
102: c->prompt_string = NULL;
103: c->prompt_buffer = NULL;
104: c->prompt_index = 0;
105:
106: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
107: if (ARRAY_ITEM(&clients, i) == NULL) {
108: ARRAY_SET(&clients, i, c);
1.13 nicm 109: return;
1.1 nicm 110: }
111: }
112: ARRAY_ADD(&clients, c);
1.20 nicm 113: log_debug("new client %d", fd);
1.1 nicm 114: }
115:
116: /* Find client index. */
117: int
118: server_client_index(struct client *c)
119: {
120: u_int i;
121:
122: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
123: if (c == ARRAY_ITEM(&clients, i))
124: return (i);
125: }
126: return (-1);
127: }
128:
129: /* Fork new server. */
130: int
131: server_start(char *path)
132: {
1.16 nicm 133: struct client *c;
134: int pair[2], srv_fd;
135: char *cause;
136: char rpathbuf[MAXPATHLEN];
1.1 nicm 137:
138: /* The first client is special and gets a socketpair; create it. */
139: if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
140: fatal("socketpair failed");
141:
142: switch (fork()) {
143: case -1:
144: fatal("fork failed");
145: case 0:
146: break;
147: default:
148: close(pair[1]);
149: return (pair[0]);
150: }
151: close(pair[0]);
152:
153: /*
154: * Must daemonise before loading configuration as the PID changes so
155: * $TMUX would be wrong for sessions created in the config file.
156: */
1.16 nicm 157: if (daemon(1, 0) != 0)
1.1 nicm 158: fatal("daemon failed");
159:
1.16 nicm 160: logfile("server");
161: log_debug("server started, pid %ld", (long) getpid());
162:
1.1 nicm 163: ARRAY_INIT(&windows);
164: ARRAY_INIT(&clients);
165: ARRAY_INIT(&sessions);
1.15 nicm 166: mode_key_init_trees();
1.1 nicm 167: key_bindings_init();
168: utf8_build();
169:
170: server_locked = 0;
171: server_password = NULL;
172: server_activity = time(NULL);
173:
174: start_time = time(NULL);
175: socket_path = path;
176:
1.16 nicm 177: if (realpath(socket_path, rpathbuf) == NULL)
178: strlcpy(rpathbuf, socket_path, sizeof rpathbuf);
179: log_debug("socket path %s", socket_path);
180: setproctitle("server (%s)", rpathbuf);
181:
182: srv_fd = server_create_socket();
183: server_create_client(pair[1]);
184:
1.7 nicm 185: if (access(SYSTEM_CFG, R_OK) != 0) {
186: if (errno != ENOENT) {
1.16 nicm 187: xasprintf(
188: &cause, "%s: %s", strerror(errno), SYSTEM_CFG);
189: goto error;
190: }
1.24 nicm 191: } else if (load_cfg(SYSTEM_CFG, NULL, &cause) != 0)
1.16 nicm 192: goto error;
1.24 nicm 193: if (cfg_file != NULL && load_cfg(cfg_file, NULL, &cause) != 0)
1.16 nicm 194: goto error;
1.5 nicm 195:
1.16 nicm 196: exit(server_main(srv_fd));
1.1 nicm 197:
1.16 nicm 198: error:
199: /* Write the error and shutdown the server. */
200: c = ARRAY_FIRST(&clients);
1.1 nicm 201:
1.16 nicm 202: server_write_error(c, cause);
203: xfree(cause);
1.1 nicm 204:
1.27 ! nicm 205: sigterm = 1;
1.16 nicm 206: server_shutdown();
1.1 nicm 207:
208: exit(server_main(srv_fd));
209: }
210:
211: /* Create server socket. */
212: int
213: server_create_socket(void)
214: {
215: struct sockaddr_un sa;
216: size_t size;
217: mode_t mask;
218: int fd, mode;
219:
220: memset(&sa, 0, sizeof sa);
221: sa.sun_family = AF_UNIX;
222: size = strlcpy(sa.sun_path, socket_path, sizeof sa.sun_path);
223: if (size >= sizeof sa.sun_path) {
224: errno = ENAMETOOLONG;
225: fatal("socket failed");
226: }
227: unlink(sa.sun_path);
228:
229: if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
230: fatal("socket failed");
231:
232: mask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
233: if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1)
234: fatal("bind failed");
235: umask(mask);
236:
237: if (listen(fd, 16) == -1)
238: fatal("listen failed");
239:
240: if ((mode = fcntl(fd, F_GETFL)) == -1)
241: fatal("fcntl failed");
242: if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
243: fatal("fcntl failed");
244: if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
245: fatal("fcntl failed");
246:
247: return (fd);
248: }
249:
250: /* Main server loop. */
251: int
252: server_main(int srv_fd)
253: {
254: struct window *w;
255: struct pollfd *pfds, *pfd;
256: int nfds, xtimeout;
257: u_int i, n;
258: time_t now, last;
259:
260: siginit();
1.20 nicm 261: log_debug("server socket is %d", srv_fd);
1.1 nicm 262:
263: last = time(NULL);
264:
265: pfds = NULL;
266: for (;;) {
267: /* If sigterm, kill all windows and clients. */
268: if (sigterm)
269: server_shutdown();
270:
271: /* Handle child exit. */
272: if (sigchld) {
273: server_child_signal();
274: sigchld = 0;
275: }
276:
277: /* Recreate socket on SIGUSR1. */
278: if (sigusr1) {
279: close(srv_fd);
280: srv_fd = server_create_socket();
281: sigusr1 = 0;
282: }
283:
284: /* Initialise pollfd array. */
285: nfds = 1;
286: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
287: w = ARRAY_ITEM(&windows, i);
288: if (w != NULL)
289: nfds += window_count_panes(w);
290: }
291: nfds += ARRAY_LENGTH(&clients) * 2;
292: pfds = xrealloc(pfds, nfds, sizeof *pfds);
293: memset(pfds, 0, nfds * sizeof *pfds);
294: pfd = pfds;
295:
296: /* Fill server socket. */
297: pfd->fd = srv_fd;
298: pfd->events = POLLIN;
299: pfd++;
300:
301: /* Fill window and client sockets. */
302: server_fill_windows(&pfd);
303: server_fill_clients(&pfd);
304:
305: /* Update socket permissions. */
306: xtimeout = INFTIM;
1.27 ! nicm 307: if (server_update_socket() != 0)
1.1 nicm 308: xtimeout = POLL_TIMEOUT;
309:
310: /* Do the poll. */
1.4 nicm 311: if (poll(pfds, nfds, xtimeout) == -1) {
1.1 nicm 312: if (errno == EAGAIN || errno == EINTR)
313: continue;
314: fatal("poll failed");
315: }
316: pfd = pfds;
317:
318: /* Handle server socket. */
319: if (pfd->revents & (POLLERR|POLLNVAL|POLLHUP))
320: fatalx("lost server socket");
321: if (pfd->revents & POLLIN) {
322: server_accept_client(srv_fd);
323: continue;
324: }
325: pfd++;
326:
327: /* Call second-based timers. */
328: now = time(NULL);
329: if (now != last) {
330: last = now;
331: server_second_timers();
332: }
333:
334: /* Set window names. */
335: set_window_names();
336:
337: /*
338: * Handle window and client sockets. Clients can create
339: * windows, so windows must come first to avoid messing up by
340: * increasing the array size.
341: */
342: server_handle_windows(&pfd);
343: server_handle_clients(&pfd);
344:
1.8 nicm 345: /* Collect any unset key bindings. */
346: key_bindings_clean();
347:
1.1 nicm 348: /*
349: * If we have no sessions and clients left, let's get out
350: * of here...
351: */
352: n = 0;
353: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
354: if (ARRAY_ITEM(&sessions, i) != NULL)
355: n++;
356: }
357: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
358: if (ARRAY_ITEM(&clients, i) != NULL)
359: n++;
360: }
361: if (n == 0)
362: break;
363: }
364: if (pfds != NULL)
365: xfree(pfds);
366:
367: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
368: if (ARRAY_ITEM(&sessions, i) != NULL)
369: session_destroy(ARRAY_ITEM(&sessions, i));
370: }
371: ARRAY_FREE(&sessions);
372:
373: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
374: if (ARRAY_ITEM(&clients, i) != NULL)
375: server_lost_client(ARRAY_ITEM(&clients, i));
376: }
377: ARRAY_FREE(&clients);
378:
1.15 nicm 379: mode_key_free_trees();
1.1 nicm 380: key_bindings_free();
381:
382: close(srv_fd);
383:
384: unlink(socket_path);
385: xfree(socket_path);
386:
1.6 nicm 387: options_free(&global_s_options);
388: options_free(&global_w_options);
1.1 nicm 389: if (server_password != NULL)
390: xfree(server_password);
391:
392: return (0);
393: }
394:
395: /* Kill all clients. */
396: void
397: server_shutdown(void)
398: {
399: struct session *s;
400: struct client *c;
401: u_int i, j;
402:
403: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
404: s = ARRAY_ITEM(&sessions, i);
405: for (j = 0; j < ARRAY_LENGTH(&clients); j++) {
406: c = ARRAY_ITEM(&clients, j);
407: if (c != NULL && c->session == s) {
408: s = NULL;
409: break;
410: }
411: }
412: if (s != NULL)
413: session_destroy(s);
414: }
415:
416: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
417: c = ARRAY_ITEM(&clients, i);
1.16 nicm 418: if (c != NULL) {
419: if (c->flags & CLIENT_BAD)
420: server_lost_client(c);
421: else
422: server_write_client(c, MSG_SHUTDOWN, NULL, 0);
423: }
1.1 nicm 424: }
425: }
426:
427: /* Handle SIGCHLD. */
428: void
429: server_child_signal(void)
430: {
431: struct window *w;
432: struct window_pane *wp;
433: int status;
434: pid_t pid;
435: u_int i;
436:
437: for (;;) {
438: switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
439: case -1:
440: if (errno == ECHILD)
441: return;
442: fatal("waitpid");
443: case 0:
444: return;
445: }
446: if (!WIFSTOPPED(status))
447: continue;
448: if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
449: continue;
450:
451: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
452: w = ARRAY_ITEM(&windows, i);
453: if (w == NULL)
454: continue;
455: TAILQ_FOREACH(wp, &w->panes, entry) {
456: if (wp->pid == pid) {
457: if (killpg(pid, SIGCONT) != 0)
458: kill(pid, SIGCONT);
459: }
460: }
461: }
462: }
463: }
464:
465: /* Fill window pollfds. */
466: void
467: server_fill_windows(struct pollfd **pfd)
468: {
469: struct window *w;
470: struct window_pane *wp;
471: u_int i;
472:
473: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
474: w = ARRAY_ITEM(&windows, i);
475: if (w == NULL)
476: continue;
477:
478: TAILQ_FOREACH(wp, &w->panes, entry) {
479: (*pfd)->fd = wp->fd;
480: if (wp->fd != -1) {
481: (*pfd)->events = POLLIN;
482: if (BUFFER_USED(wp->out) > 0)
483: (*pfd)->events |= POLLOUT;
484: }
485: (*pfd)++;
486: }
487: }
488: }
489:
490: /* Handle window pollfds. */
491: void
492: server_handle_windows(struct pollfd **pfd)
493: {
494: struct window *w;
495: struct window_pane *wp;
496: u_int i;
497:
498: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
499: w = ARRAY_ITEM(&windows, i);
500: if (w == NULL)
501: continue;
502:
503: TAILQ_FOREACH(wp, &w->panes, entry) {
504: if (wp->fd != -1) {
505: if (buffer_poll(*pfd, wp->in, wp->out) != 0) {
506: close(wp->fd);
507: wp->fd = -1;
508: } else
509: server_handle_window(w, wp);
510: }
511: (*pfd)++;
512: }
513:
514: server_check_window(w);
515: }
516: }
517:
518: /* Check for general redraw on client. */
519: void
520: server_check_redraw(struct client *c)
521: {
522: struct session *s;
523: struct window_pane *wp;
524: char title[512];
525: int flags, redraw;
526:
527: if (c == NULL || c->session == NULL)
528: return;
529: s = c->session;
530:
531: flags = c->tty.flags & TTY_FREEZE;
532: c->tty.flags &= ~TTY_FREEZE;
533:
534: if (options_get_number(&s->options, "set-titles")) {
535: xsnprintf(title, sizeof title, "%s:%u:%s - \"%s\"",
536: s->name, s->curw->idx, s->curw->window->name,
537: s->curw->window->active->screen->title);
538: if (c->title == NULL || strcmp(title, c->title) != 0) {
539: if (c->title != NULL)
540: xfree(c->title);
541: c->title = xstrdup(title);
542: tty_set_title(&c->tty, c->title);
543: }
544: }
545:
546: if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
547: if (c->message_string != NULL)
548: redraw = status_message_redraw(c);
549: else if (c->prompt_string != NULL)
550: redraw = status_prompt_redraw(c);
551: else
552: redraw = status_redraw(c);
553: if (!redraw)
554: c->flags &= ~CLIENT_STATUS;
555: }
556:
557: if (c->flags & CLIENT_REDRAW) {
558: if (server_locked)
559: server_redraw_locked(c);
560: else
1.9 nicm 561: screen_redraw_screen(c, 0);
1.1 nicm 562: c->flags &= ~CLIENT_STATUS;
563: } else {
564: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
565: if (wp->flags & PANE_REDRAW)
566: screen_redraw_pane(c, wp);
567: }
568: }
569:
570: if (c->flags & CLIENT_STATUS)
1.9 nicm 571: screen_redraw_screen(c, 1);
1.1 nicm 572:
573: c->tty.flags |= flags;
574:
575: c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS);
576: }
577:
578: /* Redraw client when locked. */
579: void
580: server_redraw_locked(struct client *c)
581: {
582: struct screen_write_ctx ctx;
583: struct screen screen;
1.12 nicm 584: struct grid_cell gc;
1.1 nicm 585: u_int colour, xx, yy, i;
586: int style;
587:
588: xx = c->tty.sx;
589: yy = c->tty.sy - 1;
590: if (xx == 0 || yy == 0)
591: return;
1.6 nicm 592: colour = options_get_number(&global_w_options, "clock-mode-colour");
593: style = options_get_number(&global_w_options, "clock-mode-style");
1.1 nicm 594:
1.12 nicm 595: memcpy(&gc, &grid_default_cell, sizeof gc);
596: gc.fg = colour;
597: gc.attr |= GRID_ATTR_BRIGHT;
598:
1.1 nicm 599: screen_init(&screen, xx, yy, 0);
600:
601: screen_write_start(&ctx, NULL, &screen);
602: clock_draw(&ctx, colour, style);
1.12 nicm 603:
604: if (password_failures != 0) {
605: screen_write_cursormove(&ctx, 0, 0);
606: screen_write_puts(
607: &ctx, &gc, "%u failed attempts", password_failures);
608: }
609:
1.1 nicm 610: screen_write_stop(&ctx);
611:
612: for (i = 0; i < screen_size_y(&screen); i++)
613: tty_draw_line(&c->tty, &screen, i, 0, 0);
1.9 nicm 614: screen_redraw_screen(c, 1);
1.1 nicm 615:
616: screen_free(&screen);
617: }
618:
619: /* Check for timers on client. */
620: void
621: server_check_timers(struct client *c)
622: {
623: struct session *s;
624: struct timeval tv;
625: u_int interval;
626:
627: if (c == NULL || c->session == NULL)
628: return;
629: s = c->session;
630:
631: if (gettimeofday(&tv, NULL) != 0)
632: fatal("gettimeofday");
633:
1.26 nicm 634: if (c->flags & CLIENT_IDENTIFY && timercmp(&tv, &c->identify_timer, >))
635: server_clear_identify(c);
636:
1.1 nicm 637: if (c->message_string != NULL && timercmp(&tv, &c->message_timer, >))
638: status_message_clear(c);
639:
640: if (c->message_string != NULL || c->prompt_string != NULL) {
641: /*
642: * Don't need timed redraw for messages/prompts so bail now.
643: * The status timer isn't reset when they are redrawn anyway.
644: */
645: return;
646: }
647: if (!options_get_number(&s->options, "status"))
648: return;
649:
650: /* Check timer; resolution is only a second so don't be too clever. */
651: interval = options_get_number(&s->options, "status-interval");
652: if (interval == 0)
653: return;
654: if (tv.tv_sec < c->status_timer.tv_sec ||
655: ((u_int) tv.tv_sec) - c->status_timer.tv_sec >= interval)
656: c->flags |= CLIENT_STATUS;
657: }
658:
659: /* Fill client pollfds. */
660: void
661: server_fill_clients(struct pollfd **pfd)
662: {
663: struct client *c;
664: struct window *w;
665: struct window_pane *wp;
666: u_int i;
667:
668: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
669: c = ARRAY_ITEM(&clients, i);
670:
671: server_check_timers(c);
672: server_check_redraw(c);
673:
674: if (c == NULL)
675: (*pfd)->fd = -1;
676: else {
1.18 nicm 677: (*pfd)->fd = c->ibuf.fd;
1.16 nicm 678: if (!(c->flags & CLIENT_BAD))
1.18 nicm 679: (*pfd)->events |= POLLIN;
680: if (c->ibuf.w.queued > 0)
1.1 nicm 681: (*pfd)->events |= POLLOUT;
682: }
683: (*pfd)++;
684:
685: if (c == NULL || c->flags & CLIENT_SUSPENDED ||
686: c->tty.fd == -1 || c->session == NULL)
687: (*pfd)->fd = -1;
688: else {
689: (*pfd)->fd = c->tty.fd;
690: (*pfd)->events = POLLIN;
691: if (BUFFER_USED(c->tty.out) > 0)
692: (*pfd)->events |= POLLOUT;
693: }
694: (*pfd)++;
695: }
696:
697: /*
698: * Clear any window redraw flags (will have been redrawn as part of
699: * client).
700: */
701: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
702: w = ARRAY_ITEM(&windows, i);
703: if (w == NULL)
704: continue;
705:
706: w->flags &= ~WINDOW_REDRAW;
707: TAILQ_FOREACH(wp, &w->panes, entry)
708: wp->flags &= ~PANE_REDRAW;
709: }
710: }
711:
712: /* Handle client pollfds. */
713: void
714: server_handle_clients(struct pollfd **pfd)
715: {
716: struct client *c;
717: u_int i;
718:
719: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
720: c = ARRAY_ITEM(&clients, i);
721:
722: if (c != NULL) {
1.18 nicm 723: if ((*pfd)->revents & (POLLERR|POLLNVAL|POLLHUP)) {
1.1 nicm 724: server_lost_client(c);
725: (*pfd) += 2;
726: continue;
1.18 nicm 727: }
728:
729: if ((*pfd)->revents & POLLOUT) {
730: if (msgbuf_write(&c->ibuf.w) < 0) {
731: server_lost_client(c);
732: (*pfd) += 2;
733: continue;
734: }
735: }
736:
737: if (c->flags & CLIENT_BAD) {
738: if (c->ibuf.w.queued == 0)
1.16 nicm 739: server_lost_client(c);
740: (*pfd) += 2;
1.18 nicm 741: continue;
742: } else if ((*pfd)->revents & POLLIN) {
743: if (server_msg_dispatch(c) != 0) {
744: server_lost_client(c);
745: (*pfd) += 2;
746: continue;
747: }
748: }
1.1 nicm 749: }
750: (*pfd)++;
751:
752: if (c != NULL && !(c->flags & CLIENT_SUSPENDED) &&
753: c->tty.fd != -1 && c->session != NULL) {
754: if (buffer_poll(*pfd, c->tty.in, c->tty.out) != 0)
755: server_lost_client(c);
756: else
757: server_handle_client(c);
758: }
759: (*pfd)++;
760: }
761: }
762:
763: /* accept(2) and create new client. */
1.13 nicm 764: void
1.1 nicm 765: server_accept_client(int srv_fd)
766: {
767: struct sockaddr_storage sa;
768: socklen_t slen = sizeof sa;
769: int fd;
770:
771: fd = accept(srv_fd, (struct sockaddr *) &sa, &slen);
772: if (fd == -1) {
773: if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
1.13 nicm 774: return;
1.1 nicm 775: fatal("accept failed");
776: }
777: if (sigterm) {
778: close(fd);
1.13 nicm 779: return;
1.1 nicm 780: }
1.13 nicm 781: server_create_client(fd);
1.1 nicm 782: }
783:
784: /* Input data from client. */
785: void
786: server_handle_client(struct client *c)
787: {
788: struct window_pane *wp;
789: struct screen *s;
790: struct timeval tv;
791: struct key_binding *bd;
792: int key, prefix, status, xtimeout;
793: int mode;
794: u_char mouse[3];
795:
796: xtimeout = options_get_number(&c->session->options, "repeat-time");
797: if (xtimeout != 0 && c->flags & CLIENT_REPEAT) {
798: if (gettimeofday(&tv, NULL) != 0)
799: fatal("gettimeofday");
800: if (timercmp(&tv, &c->repeat_timer, >))
801: c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
802: }
803:
804: /* Process keys. */
805: prefix = options_get_number(&c->session->options, "prefix");
806: while (tty_keys_next(&c->tty, &key, mouse) == 0) {
807: server_activity = time(NULL);
808:
809: if (c->session == NULL)
810: return;
811: wp = c->session->curw->window->active; /* could die */
812:
813: status_message_clear(c);
1.26 nicm 814: server_clear_identify(c);
1.1 nicm 815: if (c->prompt_string != NULL) {
816: status_prompt_key(c, key);
817: continue;
818: }
819: if (server_locked)
820: continue;
821:
822: /* Check for mouse keys. */
823: if (key == KEYC_MOUSE) {
824: window_pane_mouse(wp, c, mouse[0], mouse[1], mouse[2]);
825: continue;
826: }
827:
828: /* No previous prefix key. */
829: if (!(c->flags & CLIENT_PREFIX)) {
830: if (key == prefix)
831: c->flags |= CLIENT_PREFIX;
1.14 nicm 832: else {
833: /* Try as a non-prefix key binding. */
834: if ((bd = key_bindings_lookup(key)) == NULL)
835: window_pane_key(wp, c, key);
836: else
837: key_bindings_dispatch(bd, c);
838: }
1.1 nicm 839: continue;
840: }
841:
842: /* Prefix key already pressed. Reset prefix and lookup key. */
843: c->flags &= ~CLIENT_PREFIX;
1.14 nicm 844: if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
1.1 nicm 845: /* If repeating, treat this as a key, else ignore. */
846: if (c->flags & CLIENT_REPEAT) {
847: c->flags &= ~CLIENT_REPEAT;
848: if (key == prefix)
849: c->flags |= CLIENT_PREFIX;
850: else
851: window_pane_key(wp, c, key);
852: }
853: continue;
854: }
855:
856: /* If already repeating, but this key can't repeat, skip it. */
857: if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
858: c->flags &= ~CLIENT_REPEAT;
859: if (key == prefix)
860: c->flags |= CLIENT_PREFIX;
861: else
862: window_pane_key(wp, c, key);
863: continue;
864: }
865:
866: /* If this key can repeat, reset the repeat flags and timer. */
867: if (xtimeout != 0 && bd->can_repeat) {
868: c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
869:
870: tv.tv_sec = xtimeout / 1000;
871: tv.tv_usec = (xtimeout % 1000) * 1000L;
872: if (gettimeofday(&c->repeat_timer, NULL) != 0)
873: fatal("gettimeofday");
874: timeradd(&c->repeat_timer, &tv, &c->repeat_timer);
875: }
876:
877: /* Dispatch the command. */
878: key_bindings_dispatch(bd, c);
879: }
880: if (c->session == NULL)
881: return;
882: wp = c->session->curw->window->active; /* could die - do each loop */
883: s = wp->screen;
884:
1.21 nicm 885: /*
886: * Update cursor position and mode settings. The scroll region and
887: * attributes are cleared across poll(2) as this is the most likely
888: * time a user may interrupt tmux, for example with ~^Z in ssh(1). This
889: * is a compromise between excessive resets and likelihood of an
890: * interrupt.
891: *
892: * tty_region/tty_reset/tty_update_mode already take care of not
893: * resetting things that are already in their default state.
894: */
1.1 nicm 895: status = options_get_number(&c->session->options, "status");
1.17 nicm 896: tty_region(&c->tty, 0, c->tty.sy - 1, 0);
1.11 nicm 897: if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
898: tty_cursor(&c->tty, 0, 0, 0, 0);
899: else
1.1 nicm 900: tty_cursor(&c->tty, s->cx, s->cy, wp->xoff, wp->yoff);
901:
902: mode = s->mode;
903: if (server_locked)
904: mode &= ~TTY_NOCURSOR;
905: tty_update_mode(&c->tty, mode);
1.21 nicm 906: tty_reset(&c->tty);
1.1 nicm 907: }
908:
909: /* Lost a client. */
910: void
911: server_lost_client(struct client *c)
912: {
913: u_int i;
914:
915: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
916: if (ARRAY_ITEM(&clients, i) == c)
917: ARRAY_SET(&clients, i, NULL);
918: }
1.20 nicm 919: log_debug("lost client %d", c->ibuf.fd);
1.1 nicm 920:
1.25 nicm 921: /*
922: * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
923: * and tty_free might close an unrelated fd.
924: */
925: if (c->flags & CLIENT_TERMINAL)
926: tty_free(&c->tty);
1.1 nicm 927:
928: screen_free(&c->status);
929:
930: if (c->title != NULL)
931: xfree(c->title);
932:
933: if (c->message_string != NULL)
934: xfree(c->message_string);
935:
936: if (c->prompt_string != NULL)
937: xfree(c->prompt_string);
938: if (c->prompt_buffer != NULL)
939: xfree(c->prompt_buffer);
940: for (i = 0; i < ARRAY_LENGTH(&c->prompt_hdata); i++)
941: xfree(ARRAY_ITEM(&c->prompt_hdata, i));
942: ARRAY_FREE(&c->prompt_hdata);
943:
944: if (c->cwd != NULL)
945: xfree(c->cwd);
946:
1.18 nicm 947: close(c->ibuf.fd);
948: imsg_clear(&c->ibuf);
1.1 nicm 949: xfree(c);
950:
951: recalculate_sizes();
952: }
953:
954: /* Handle window data. */
955: void
956: server_handle_window(struct window *w, struct window_pane *wp)
957: {
958: struct session *s;
959: u_int i;
960: int update;
961:
962: window_pane_parse(wp);
963:
964: if ((w->flags & (WINDOW_BELL|WINDOW_ACTIVITY|WINDOW_CONTENT)) == 0)
965: return;
966:
967: update = 0;
968: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
969: s = ARRAY_ITEM(&sessions, i);
970: if (s == NULL || !session_has(s, w))
971: continue;
972:
1.10 nicm 973: update += server_check_window_bell(s, w);
1.1 nicm 974: update += server_check_window_activity(s, w);
975: update += server_check_window_content(s, w, wp);
976: }
977: if (update)
978: server_status_window(w);
979:
980: w->flags &= ~(WINDOW_BELL|WINDOW_ACTIVITY|WINDOW_CONTENT);
981: }
982:
983: int
1.10 nicm 984: server_check_window_bell(struct session *s, struct window *w)
1.1 nicm 985: {
986: struct client *c;
987: u_int i;
1.10 nicm 988: int action, visual;
1.1 nicm 989:
990: if (!(w->flags & WINDOW_BELL))
991: return (0);
992: if (session_alert_has_window(s, w, WINDOW_BELL))
993: return (0);
994: session_alert_add(s, w, WINDOW_BELL);
995:
996: action = options_get_number(&s->options, "bell-action");
997: switch (action) {
998: case BELL_ANY:
999: if (s->flags & SESSION_UNATTACHED)
1000: break;
1.10 nicm 1001: visual = options_get_number(&s->options, "visual-bell");
1.1 nicm 1002: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1003: c = ARRAY_ITEM(&clients, i);
1.10 nicm 1004: if (c == NULL || c->session != s)
1005: continue;
1006: if (!visual) {
1.1 nicm 1007: tty_putcode(&c->tty, TTYC_BEL);
1.10 nicm 1008: continue;
1009: }
1010: if (c->session->curw->window == w) {
1011: status_message_set(c, "Bell in current window");
1012: continue;
1013: }
1014: status_message_set(c, "Bell in window %u",
1015: winlink_find_by_window(&s->windows, w)->idx);
1.1 nicm 1016: }
1017: break;
1018: case BELL_CURRENT:
1.10 nicm 1019: if (s->flags & SESSION_UNATTACHED)
1.1 nicm 1020: break;
1.10 nicm 1021: visual = options_get_number(&s->options, "visual-bell");
1.1 nicm 1022: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1023: c = ARRAY_ITEM(&clients, i);
1.10 nicm 1024: if (c == NULL || c->session != s)
1025: continue;
1026: if (c->session->curw->window != w)
1027: continue;
1028: if (!visual) {
1.1 nicm 1029: tty_putcode(&c->tty, TTYC_BEL);
1.10 nicm 1030: continue;
1031: }
1032: status_message_set(c, "Bell in current window");
1.1 nicm 1033: }
1034: break;
1035: }
1036: return (1);
1037: }
1038:
1039: int
1040: server_check_window_activity(struct session *s, struct window *w)
1041: {
1.10 nicm 1042: struct client *c;
1043: u_int i;
1044:
1.1 nicm 1045: if (!(w->flags & WINDOW_ACTIVITY))
1046: return (0);
1.10 nicm 1047:
1.1 nicm 1048: if (!options_get_number(&w->options, "monitor-activity"))
1049: return (0);
1.10 nicm 1050:
1.1 nicm 1051: if (session_alert_has_window(s, w, WINDOW_ACTIVITY))
1052: return (0);
1.10 nicm 1053: if (s->curw->window == w)
1054: return (0);
1055:
1.1 nicm 1056: session_alert_add(s, w, WINDOW_ACTIVITY);
1.10 nicm 1057: if (s->flags & SESSION_UNATTACHED)
1058: return (0);
1059: if (options_get_number(&s->options, "visual-activity")) {
1060: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1061: c = ARRAY_ITEM(&clients, i);
1062: if (c == NULL || c->session != s)
1063: continue;
1064: status_message_set(c, "Activity in window %u",
1065: winlink_find_by_window(&s->windows, w)->idx);
1066: }
1067: }
1068:
1.1 nicm 1069: return (1);
1070: }
1071:
1072: int
1073: server_check_window_content(
1074: struct session *s, struct window *w, struct window_pane *wp)
1075: {
1.10 nicm 1076: struct client *c;
1077: u_int i;
1078: char *found, *ptr;
1079:
1080: if (!(w->flags & WINDOW_ACTIVITY)) /* activity for new content */
1081: return (0);
1.1 nicm 1082:
1.10 nicm 1083: ptr = options_get_string(&w->options, "monitor-content");
1084: if (ptr == NULL || *ptr == '\0')
1.1 nicm 1085: return (0);
1.10 nicm 1086:
1087: if (session_alert_has_window(s, w, WINDOW_CONTENT))
1.1 nicm 1088: return (0);
1.10 nicm 1089: if (s->curw->window == w)
1.1 nicm 1090: return (0);
1.10 nicm 1091:
1.3 nicm 1092: if ((found = window_pane_search(wp, ptr, NULL)) == NULL)
1.1 nicm 1093: return (0);
1.10 nicm 1094: xfree(found);
1095:
1.1 nicm 1096: session_alert_add(s, w, WINDOW_CONTENT);
1.10 nicm 1097: if (s->flags & SESSION_UNATTACHED)
1098: return (0);
1099: if (options_get_number(&s->options, "visual-content")) {
1100: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1101: c = ARRAY_ITEM(&clients, i);
1102: if (c == NULL || c->session != s)
1103: continue;
1104: status_message_set(c, "Content in window %u",
1105: winlink_find_by_window(&s->windows, w)->idx);
1106: }
1107: }
1108:
1.1 nicm 1109: return (1);
1110: }
1111:
1.2 nicm 1112: /* Check if window still exists. */
1.1 nicm 1113: void
1114: server_check_window(struct window *w)
1115: {
1116: struct window_pane *wp, *wq;
1.22 nicm 1117: struct options *oo = &w->options;
1.1 nicm 1118: struct client *c;
1119: struct session *s;
1120: struct winlink *wl;
1121: u_int i, j;
1.22 nicm 1122: int destroyed;
1.1 nicm 1123:
1124: destroyed = 1;
1125:
1126: wp = TAILQ_FIRST(&w->panes);
1127: while (wp != NULL) {
1128: wq = TAILQ_NEXT(wp, entry);
1.2 nicm 1129: /*
1130: * If the pane has died and the remain-on-exit flag is not set,
1131: * remove the pane; otherwise, if the flag is set, don't allow
1132: * the window to be destroyed (or it'll close when the last
1133: * pane dies).
1134: */
1.23 nicm 1135: if (wp->fd == -1 && !options_get_number(oo, "remain-on-exit")) {
1.11 nicm 1136: layout_close_pane(wp);
1.1 nicm 1137: window_remove_pane(w, wp);
1138: server_redraw_window(w);
1.2 nicm 1139: } else
1140: destroyed = 0;
1.1 nicm 1141: wp = wq;
1142: }
1143:
1144: if (!destroyed)
1145: return;
1146:
1147: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1148: s = ARRAY_ITEM(&sessions, i);
1149: if (s == NULL)
1150: continue;
1151: if (!session_has(s, w))
1152: continue;
1153:
1154: restart:
1155: /* Detach window and either redraw or kill clients. */
1156: RB_FOREACH(wl, winlinks, &s->windows) {
1157: if (wl->window != w)
1158: continue;
1159: destroyed = session_detach(s, wl);
1160: for (j = 0; j < ARRAY_LENGTH(&clients); j++) {
1161: c = ARRAY_ITEM(&clients, j);
1162: if (c == NULL || c->session != s)
1163: continue;
1164: if (!destroyed) {
1165: server_redraw_client(c);
1166: continue;
1167: }
1168: c->session = NULL;
1169: server_write_client(c, MSG_EXIT, NULL, 0);
1170: }
1171: /* If the session was destroyed, bail now. */
1172: if (destroyed)
1173: break;
1174: goto restart;
1175: }
1176: }
1177:
1178: recalculate_sizes();
1179: }
1180:
1181: /* Call any once-per-second timers. */
1182: void
1183: server_second_timers(void)
1184: {
1185: struct window *w;
1186: struct window_pane *wp;
1187: u_int i;
1188: int xtimeout;
1189: struct tm now, then;
1190: static time_t last_t = 0;
1191: time_t t;
1192:
1193: t = time(NULL);
1.6 nicm 1194: xtimeout = options_get_number(&global_s_options, "lock-after-time");
1.1 nicm 1195: if (xtimeout > 0 && t > server_activity + xtimeout)
1196: server_lock();
1197:
1198: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
1199: w = ARRAY_ITEM(&windows, i);
1200: if (w == NULL)
1201: continue;
1202:
1203: TAILQ_FOREACH(wp, &w->panes, entry) {
1204: if (wp->mode != NULL && wp->mode->timer != NULL)
1205: wp->mode->timer(wp);
1206: }
1207: }
1208:
1209: /* Check for a minute having passed. */
1210: gmtime_r(&t, &now);
1211: gmtime_r(&last_t, &then);
1212: if (now.tm_min == then.tm_min)
1213: return;
1214: last_t = t;
1215:
1216: /* If locked, redraw all clients. */
1217: if (server_locked) {
1218: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1219: if (ARRAY_ITEM(&clients, i) != NULL)
1220: server_redraw_client(ARRAY_ITEM(&clients, i));
1221: }
1222: }
1223: }
1224:
1225: /* Update socket execute permissions based on whether sessions are attached. */
1226: int
1227: server_update_socket(void)
1228: {
1229: struct session *s;
1230: u_int i;
1231: static int last = -1;
1232: int n;
1233:
1234: n = 0;
1235: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1236: s = ARRAY_ITEM(&sessions, i);
1237: if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
1238: n++;
1239: break;
1240: }
1241: }
1242:
1243: if (n != last) {
1244: last = n;
1245: if (n != 0)
1246: chmod(socket_path, S_IRWXU);
1247: else
1248: chmod(socket_path, S_IRUSR|S_IWUSR);
1249: }
1250:
1251: return (n);
1252: }