Annotation of src/usr.bin/tmux/server.c, Revision 1.11
1.11 ! nicm 1: /* $OpenBSD: server.c,v 1.10 2009/07/18 14:59:25 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");
1.11 ! nicm 843: if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
! 844: tty_cursor(&c->tty, 0, 0, 0, 0);
! 845: else
1.1 nicm 846: tty_cursor(&c->tty, s->cx, s->cy, wp->xoff, wp->yoff);
847:
848: mode = s->mode;
849: if (server_locked)
850: mode &= ~TTY_NOCURSOR;
851: tty_update_mode(&c->tty, mode);
852: }
853:
854: /* Lost a client. */
855: void
856: server_lost_client(struct client *c)
857: {
858: u_int i;
859:
860: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
861: if (ARRAY_ITEM(&clients, i) == c)
862: ARRAY_SET(&clients, i, NULL);
863: }
864:
865: tty_free(&c->tty, c->flags & CLIENT_SUSPENDED);
866:
867: screen_free(&c->status);
868:
869: if (c->title != NULL)
870: xfree(c->title);
871:
872: if (c->message_string != NULL)
873: xfree(c->message_string);
874:
875: if (c->prompt_string != NULL)
876: xfree(c->prompt_string);
877: if (c->prompt_buffer != NULL)
878: xfree(c->prompt_buffer);
879: for (i = 0; i < ARRAY_LENGTH(&c->prompt_hdata); i++)
880: xfree(ARRAY_ITEM(&c->prompt_hdata, i));
881: ARRAY_FREE(&c->prompt_hdata);
882:
883: if (c->cwd != NULL)
884: xfree(c->cwd);
885:
886: close(c->fd);
887: buffer_destroy(c->in);
888: buffer_destroy(c->out);
889: xfree(c);
890:
891: recalculate_sizes();
892: }
893:
894: /* Handle window data. */
895: void
896: server_handle_window(struct window *w, struct window_pane *wp)
897: {
898: struct session *s;
899: u_int i;
900: int update;
901:
902: window_pane_parse(wp);
903:
904: if ((w->flags & (WINDOW_BELL|WINDOW_ACTIVITY|WINDOW_CONTENT)) == 0)
905: return;
906:
907: update = 0;
908: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
909: s = ARRAY_ITEM(&sessions, i);
910: if (s == NULL || !session_has(s, w))
911: continue;
912:
1.10 nicm 913: update += server_check_window_bell(s, w);
1.1 nicm 914: update += server_check_window_activity(s, w);
915: update += server_check_window_content(s, w, wp);
916: }
917: if (update)
918: server_status_window(w);
919:
920: w->flags &= ~(WINDOW_BELL|WINDOW_ACTIVITY|WINDOW_CONTENT);
921: }
922:
923: int
1.10 nicm 924: server_check_window_bell(struct session *s, struct window *w)
1.1 nicm 925: {
926: struct client *c;
927: u_int i;
1.10 nicm 928: int action, visual;
1.1 nicm 929:
930: if (!(w->flags & WINDOW_BELL))
931: return (0);
932: if (session_alert_has_window(s, w, WINDOW_BELL))
933: return (0);
934: session_alert_add(s, w, WINDOW_BELL);
935:
936: action = options_get_number(&s->options, "bell-action");
937: switch (action) {
938: case BELL_ANY:
939: if (s->flags & SESSION_UNATTACHED)
940: break;
1.10 nicm 941: visual = options_get_number(&s->options, "visual-bell");
1.1 nicm 942: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
943: c = ARRAY_ITEM(&clients, i);
1.10 nicm 944: if (c == NULL || c->session != s)
945: continue;
946: if (!visual) {
1.1 nicm 947: tty_putcode(&c->tty, TTYC_BEL);
1.10 nicm 948: continue;
949: }
950: if (c->session->curw->window == w) {
951: status_message_set(c, "Bell in current window");
952: continue;
953: }
954: status_message_set(c, "Bell in window %u",
955: winlink_find_by_window(&s->windows, w)->idx);
1.1 nicm 956: }
957: break;
958: case BELL_CURRENT:
1.10 nicm 959: if (s->flags & SESSION_UNATTACHED)
1.1 nicm 960: break;
1.10 nicm 961: visual = options_get_number(&s->options, "visual-bell");
1.1 nicm 962: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
963: c = ARRAY_ITEM(&clients, i);
1.10 nicm 964: if (c == NULL || c->session != s)
965: continue;
966: if (c->session->curw->window != w)
967: continue;
968: if (!visual) {
1.1 nicm 969: tty_putcode(&c->tty, TTYC_BEL);
1.10 nicm 970: continue;
971: }
972: status_message_set(c, "Bell in current window");
1.1 nicm 973: }
974: break;
975: }
976: return (1);
977: }
978:
979: int
980: server_check_window_activity(struct session *s, struct window *w)
981: {
1.10 nicm 982: struct client *c;
983: u_int i;
984:
1.1 nicm 985: if (!(w->flags & WINDOW_ACTIVITY))
986: return (0);
1.10 nicm 987:
1.1 nicm 988: if (!options_get_number(&w->options, "monitor-activity"))
989: return (0);
1.10 nicm 990:
1.1 nicm 991: if (session_alert_has_window(s, w, WINDOW_ACTIVITY))
992: return (0);
1.10 nicm 993: if (s->curw->window == w)
994: return (0);
995:
1.1 nicm 996: session_alert_add(s, w, WINDOW_ACTIVITY);
1.10 nicm 997: if (s->flags & SESSION_UNATTACHED)
998: return (0);
999: if (options_get_number(&s->options, "visual-activity")) {
1000: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1001: c = ARRAY_ITEM(&clients, i);
1002: if (c == NULL || c->session != s)
1003: continue;
1004: status_message_set(c, "Activity in window %u",
1005: winlink_find_by_window(&s->windows, w)->idx);
1006: }
1007: }
1008:
1.1 nicm 1009: return (1);
1010: }
1011:
1012: int
1013: server_check_window_content(
1014: struct session *s, struct window *w, struct window_pane *wp)
1015: {
1.10 nicm 1016: struct client *c;
1017: u_int i;
1018: char *found, *ptr;
1019:
1020: if (!(w->flags & WINDOW_ACTIVITY)) /* activity for new content */
1021: return (0);
1.1 nicm 1022:
1.10 nicm 1023: ptr = options_get_string(&w->options, "monitor-content");
1024: if (ptr == NULL || *ptr == '\0')
1.1 nicm 1025: return (0);
1.10 nicm 1026:
1027: if (session_alert_has_window(s, w, WINDOW_CONTENT))
1.1 nicm 1028: return (0);
1.10 nicm 1029: if (s->curw->window == w)
1.1 nicm 1030: return (0);
1.10 nicm 1031:
1.3 nicm 1032: if ((found = window_pane_search(wp, ptr, NULL)) == NULL)
1.1 nicm 1033: return (0);
1.10 nicm 1034: xfree(found);
1035:
1.1 nicm 1036: session_alert_add(s, w, WINDOW_CONTENT);
1.10 nicm 1037: if (s->flags & SESSION_UNATTACHED)
1038: return (0);
1039: if (options_get_number(&s->options, "visual-content")) {
1040: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1041: c = ARRAY_ITEM(&clients, i);
1042: if (c == NULL || c->session != s)
1043: continue;
1044: status_message_set(c, "Content in window %u",
1045: winlink_find_by_window(&s->windows, w)->idx);
1046: }
1047: }
1048:
1.1 nicm 1049: return (1);
1050: }
1051:
1.2 nicm 1052: /* Check if window still exists. */
1.1 nicm 1053: void
1054: server_check_window(struct window *w)
1055: {
1056: struct window_pane *wp, *wq;
1057: struct client *c;
1058: struct session *s;
1059: struct winlink *wl;
1060: u_int i, j;
1061: int destroyed, flag;
1062:
1063: flag = options_get_number(&w->options, "remain-on-exit");
1064:
1065: destroyed = 1;
1066:
1067: wp = TAILQ_FIRST(&w->panes);
1068: while (wp != NULL) {
1069: wq = TAILQ_NEXT(wp, entry);
1.2 nicm 1070: /*
1071: * If the pane has died and the remain-on-exit flag is not set,
1072: * remove the pane; otherwise, if the flag is set, don't allow
1073: * the window to be destroyed (or it'll close when the last
1074: * pane dies).
1075: */
1076: if (wp->fd == -1 && !flag) {
1.11 ! nicm 1077: layout_close_pane(wp);
1.1 nicm 1078: window_remove_pane(w, wp);
1079: server_redraw_window(w);
1.2 nicm 1080: } else
1081: destroyed = 0;
1.1 nicm 1082: wp = wq;
1083: }
1084:
1085: if (!destroyed)
1086: return;
1087:
1088: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1089: s = ARRAY_ITEM(&sessions, i);
1090: if (s == NULL)
1091: continue;
1092: if (!session_has(s, w))
1093: continue;
1094:
1095: restart:
1096: /* Detach window and either redraw or kill clients. */
1097: RB_FOREACH(wl, winlinks, &s->windows) {
1098: if (wl->window != w)
1099: continue;
1100: destroyed = session_detach(s, wl);
1101: for (j = 0; j < ARRAY_LENGTH(&clients); j++) {
1102: c = ARRAY_ITEM(&clients, j);
1103: if (c == NULL || c->session != s)
1104: continue;
1105: if (!destroyed) {
1106: server_redraw_client(c);
1107: continue;
1108: }
1109: c->session = NULL;
1110: server_write_client(c, MSG_EXIT, NULL, 0);
1111: }
1112: /* If the session was destroyed, bail now. */
1113: if (destroyed)
1114: break;
1115: goto restart;
1116: }
1117: }
1118:
1119: recalculate_sizes();
1120: }
1121:
1122: /* Call any once-per-second timers. */
1123: void
1124: server_second_timers(void)
1125: {
1126: struct window *w;
1127: struct window_pane *wp;
1128: u_int i;
1129: int xtimeout;
1130: struct tm now, then;
1131: static time_t last_t = 0;
1132: time_t t;
1133:
1134: t = time(NULL);
1.6 nicm 1135: xtimeout = options_get_number(&global_s_options, "lock-after-time");
1.1 nicm 1136: if (xtimeout > 0 && t > server_activity + xtimeout)
1137: server_lock();
1138:
1139: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
1140: w = ARRAY_ITEM(&windows, i);
1141: if (w == NULL)
1142: continue;
1143:
1144: TAILQ_FOREACH(wp, &w->panes, entry) {
1145: if (wp->mode != NULL && wp->mode->timer != NULL)
1146: wp->mode->timer(wp);
1147: }
1148: }
1149:
1150: /* Check for a minute having passed. */
1151: gmtime_r(&t, &now);
1152: gmtime_r(&last_t, &then);
1153: if (now.tm_min == then.tm_min)
1154: return;
1155: last_t = t;
1156:
1157: /* If locked, redraw all clients. */
1158: if (server_locked) {
1159: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1160: if (ARRAY_ITEM(&clients, i) != NULL)
1161: server_redraw_client(ARRAY_ITEM(&clients, i));
1162: }
1163: }
1164: }
1165:
1166: /* Update socket execute permissions based on whether sessions are attached. */
1167: int
1168: server_update_socket(void)
1169: {
1170: struct session *s;
1171: u_int i;
1172: static int last = -1;
1173: int n;
1174:
1175: n = 0;
1176: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1177: s = ARRAY_ITEM(&sessions, i);
1178: if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
1179: n++;
1180: break;
1181: }
1182: }
1183:
1184: if (n != last) {
1185: last = n;
1186: if (n != 0)
1187: chmod(socket_path, S_IRWXU);
1188: else
1189: chmod(socket_path, S_IRUSR|S_IWUSR);
1190: }
1191:
1192: return (n);
1193: }