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