Annotation of src/usr.bin/tmux/server.c, Revision 1.13
1.13 ! nicm 1: /* $OpenBSD: server.c,v 1.12 2009/07/20 14:37:51 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);
88: c->fd = fd;
89: c->in = buffer_create(BUFSIZ);
90: c->out = buffer_create(BUFSIZ);
91:
92: ARRAY_INIT(&c->prompt_hdata);
93:
94: c->tty.fd = -1;
95: c->title = NULL;
96:
97: c->session = NULL;
98: c->tty.sx = 80;
99: c->tty.sy = 25;
100: screen_init(&c->status, c->tty.sx, 1, 0);
101:
102: c->message_string = NULL;
103:
104: c->prompt_string = NULL;
105: c->prompt_buffer = NULL;
106: c->prompt_index = 0;
107:
108: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
109: if (ARRAY_ITEM(&clients, i) == NULL) {
110: ARRAY_SET(&clients, i, c);
1.13 ! nicm 111: return;
1.1 nicm 112: }
113: }
114: ARRAY_ADD(&clients, 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;
1.12 nicm 582: struct grid_cell gc;
1.1 nicm 583: u_int colour, xx, yy, i;
584: int style;
585:
586: xx = c->tty.sx;
587: yy = c->tty.sy - 1;
588: if (xx == 0 || yy == 0)
589: return;
1.6 nicm 590: colour = options_get_number(&global_w_options, "clock-mode-colour");
591: style = options_get_number(&global_w_options, "clock-mode-style");
1.1 nicm 592:
1.12 nicm 593: memcpy(&gc, &grid_default_cell, sizeof gc);
594: gc.fg = colour;
595: gc.attr |= GRID_ATTR_BRIGHT;
596:
1.1 nicm 597: screen_init(&screen, xx, yy, 0);
598:
599: screen_write_start(&ctx, NULL, &screen);
600: clock_draw(&ctx, colour, style);
1.12 nicm 601:
602: if (password_failures != 0) {
603: screen_write_cursormove(&ctx, 0, 0);
604: screen_write_puts(
605: &ctx, &gc, "%u failed attempts", password_failures);
606: }
607:
1.1 nicm 608: screen_write_stop(&ctx);
609:
610: for (i = 0; i < screen_size_y(&screen); i++)
611: tty_draw_line(&c->tty, &screen, i, 0, 0);
1.9 nicm 612: screen_redraw_screen(c, 1);
1.1 nicm 613:
614: screen_free(&screen);
615: }
616:
617: /* Check for timers on client. */
618: void
619: server_check_timers(struct client *c)
620: {
621: struct session *s;
622: struct timeval tv;
623: u_int interval;
624:
625: if (c == NULL || c->session == NULL)
626: return;
627: s = c->session;
628:
629: if (gettimeofday(&tv, NULL) != 0)
630: fatal("gettimeofday");
631:
632: if (c->message_string != NULL && timercmp(&tv, &c->message_timer, >))
633: status_message_clear(c);
634:
635: if (c->message_string != NULL || c->prompt_string != NULL) {
636: /*
637: * Don't need timed redraw for messages/prompts so bail now.
638: * The status timer isn't reset when they are redrawn anyway.
639: */
640: return;
641: }
642: if (!options_get_number(&s->options, "status"))
643: return;
644:
645: /* Check timer; resolution is only a second so don't be too clever. */
646: interval = options_get_number(&s->options, "status-interval");
647: if (interval == 0)
648: return;
649: if (tv.tv_sec < c->status_timer.tv_sec ||
650: ((u_int) tv.tv_sec) - c->status_timer.tv_sec >= interval)
651: c->flags |= CLIENT_STATUS;
652: }
653:
654: /* Fill client pollfds. */
655: void
656: server_fill_clients(struct pollfd **pfd)
657: {
658: struct client *c;
659: struct window *w;
660: struct window_pane *wp;
661: u_int i;
662:
663: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
664: c = ARRAY_ITEM(&clients, i);
665:
666: server_check_timers(c);
667: server_check_redraw(c);
668:
669: if (c == NULL)
670: (*pfd)->fd = -1;
671: else {
672: (*pfd)->fd = c->fd;
673: (*pfd)->events = POLLIN;
674: if (BUFFER_USED(c->out) > 0)
675: (*pfd)->events |= POLLOUT;
676: }
677: (*pfd)++;
678:
679: if (c == NULL || c->flags & CLIENT_SUSPENDED ||
680: c->tty.fd == -1 || c->session == NULL)
681: (*pfd)->fd = -1;
682: else {
683: (*pfd)->fd = c->tty.fd;
684: (*pfd)->events = POLLIN;
685: if (BUFFER_USED(c->tty.out) > 0)
686: (*pfd)->events |= POLLOUT;
687: }
688: (*pfd)++;
689: }
690:
691: /*
692: * Clear any window redraw flags (will have been redrawn as part of
693: * client).
694: */
695: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
696: w = ARRAY_ITEM(&windows, i);
697: if (w == NULL)
698: continue;
699:
700: w->flags &= ~WINDOW_REDRAW;
701: TAILQ_FOREACH(wp, &w->panes, entry)
702: wp->flags &= ~PANE_REDRAW;
703: }
704: }
705:
706: /* Handle client pollfds. */
707: void
708: server_handle_clients(struct pollfd **pfd)
709: {
710: struct client *c;
711: u_int i;
712:
713: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
714: c = ARRAY_ITEM(&clients, i);
715:
716: if (c != NULL) {
717: if (buffer_poll(*pfd, c->in, c->out) != 0) {
718: server_lost_client(c);
719: (*pfd) += 2;
720: continue;
721: } else
722: server_msg_dispatch(c);
723: }
724: (*pfd)++;
725:
726: if (c != NULL && !(c->flags & CLIENT_SUSPENDED) &&
727: c->tty.fd != -1 && c->session != NULL) {
728: if (buffer_poll(*pfd, c->tty.in, c->tty.out) != 0)
729: server_lost_client(c);
730: else
731: server_handle_client(c);
732: }
733: (*pfd)++;
734: }
735: }
736:
737: /* accept(2) and create new client. */
1.13 ! nicm 738: void
1.1 nicm 739: server_accept_client(int srv_fd)
740: {
741: struct sockaddr_storage sa;
742: socklen_t slen = sizeof sa;
743: int fd;
744:
745: fd = accept(srv_fd, (struct sockaddr *) &sa, &slen);
746: if (fd == -1) {
747: if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
1.13 ! nicm 748: return;
1.1 nicm 749: fatal("accept failed");
750: }
751: if (sigterm) {
752: close(fd);
1.13 ! nicm 753: return;
1.1 nicm 754: }
1.13 ! nicm 755: server_create_client(fd);
1.1 nicm 756: }
757:
758: /* Input data from client. */
759: void
760: server_handle_client(struct client *c)
761: {
762: struct window_pane *wp;
763: struct screen *s;
764: struct timeval tv;
765: struct key_binding *bd;
766: int key, prefix, status, xtimeout;
767: int mode;
768: u_char mouse[3];
769:
770: xtimeout = options_get_number(&c->session->options, "repeat-time");
771: if (xtimeout != 0 && c->flags & CLIENT_REPEAT) {
772: if (gettimeofday(&tv, NULL) != 0)
773: fatal("gettimeofday");
774: if (timercmp(&tv, &c->repeat_timer, >))
775: c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
776: }
777:
778: /* Process keys. */
779: prefix = options_get_number(&c->session->options, "prefix");
780: while (tty_keys_next(&c->tty, &key, mouse) == 0) {
781: server_activity = time(NULL);
782:
783: if (c->session == NULL)
784: return;
785: wp = c->session->curw->window->active; /* could die */
786:
787: status_message_clear(c);
788: if (c->prompt_string != NULL) {
789: status_prompt_key(c, key);
790: continue;
791: }
792: if (server_locked)
793: continue;
794:
795: /* Check for mouse keys. */
796: if (key == KEYC_MOUSE) {
797: window_pane_mouse(wp, c, mouse[0], mouse[1], mouse[2]);
798: continue;
799: }
800:
801: /* No previous prefix key. */
802: if (!(c->flags & CLIENT_PREFIX)) {
803: if (key == prefix)
804: c->flags |= CLIENT_PREFIX;
805: else
806: window_pane_key(wp, c, key);
807: continue;
808: }
809:
810: /* Prefix key already pressed. Reset prefix and lookup key. */
811: c->flags &= ~CLIENT_PREFIX;
812: if ((bd = key_bindings_lookup(key)) == NULL) {
813: /* If repeating, treat this as a key, else ignore. */
814: if (c->flags & CLIENT_REPEAT) {
815: c->flags &= ~CLIENT_REPEAT;
816: if (key == prefix)
817: c->flags |= CLIENT_PREFIX;
818: else
819: window_pane_key(wp, c, key);
820: }
821: continue;
822: }
823:
824: /* If already repeating, but this key can't repeat, skip it. */
825: if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
826: c->flags &= ~CLIENT_REPEAT;
827: if (key == prefix)
828: c->flags |= CLIENT_PREFIX;
829: else
830: window_pane_key(wp, c, key);
831: continue;
832: }
833:
834: /* If this key can repeat, reset the repeat flags and timer. */
835: if (xtimeout != 0 && bd->can_repeat) {
836: c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
837:
838: tv.tv_sec = xtimeout / 1000;
839: tv.tv_usec = (xtimeout % 1000) * 1000L;
840: if (gettimeofday(&c->repeat_timer, NULL) != 0)
841: fatal("gettimeofday");
842: timeradd(&c->repeat_timer, &tv, &c->repeat_timer);
843: }
844:
845: /* Dispatch the command. */
846: key_bindings_dispatch(bd, c);
847: }
848: if (c->session == NULL)
849: return;
850: wp = c->session->curw->window->active; /* could die - do each loop */
851: s = wp->screen;
852:
853: /* Ensure cursor position and mode settings. */
854: status = options_get_number(&c->session->options, "status");
1.11 nicm 855: if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
856: tty_cursor(&c->tty, 0, 0, 0, 0);
857: else
1.1 nicm 858: tty_cursor(&c->tty, s->cx, s->cy, wp->xoff, wp->yoff);
859:
860: mode = s->mode;
861: if (server_locked)
862: mode &= ~TTY_NOCURSOR;
863: tty_update_mode(&c->tty, mode);
864: }
865:
866: /* Lost a client. */
867: void
868: server_lost_client(struct client *c)
869: {
870: u_int i;
871:
872: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
873: if (ARRAY_ITEM(&clients, i) == c)
874: ARRAY_SET(&clients, i, NULL);
875: }
876:
877: tty_free(&c->tty, c->flags & CLIENT_SUSPENDED);
878:
879: screen_free(&c->status);
880:
881: if (c->title != NULL)
882: xfree(c->title);
883:
884: if (c->message_string != NULL)
885: xfree(c->message_string);
886:
887: if (c->prompt_string != NULL)
888: xfree(c->prompt_string);
889: if (c->prompt_buffer != NULL)
890: xfree(c->prompt_buffer);
891: for (i = 0; i < ARRAY_LENGTH(&c->prompt_hdata); i++)
892: xfree(ARRAY_ITEM(&c->prompt_hdata, i));
893: ARRAY_FREE(&c->prompt_hdata);
894:
895: if (c->cwd != NULL)
896: xfree(c->cwd);
897:
898: close(c->fd);
899: buffer_destroy(c->in);
900: buffer_destroy(c->out);
901: xfree(c);
902:
903: recalculate_sizes();
904: }
905:
906: /* Handle window data. */
907: void
908: server_handle_window(struct window *w, struct window_pane *wp)
909: {
910: struct session *s;
911: u_int i;
912: int update;
913:
914: window_pane_parse(wp);
915:
916: if ((w->flags & (WINDOW_BELL|WINDOW_ACTIVITY|WINDOW_CONTENT)) == 0)
917: return;
918:
919: update = 0;
920: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
921: s = ARRAY_ITEM(&sessions, i);
922: if (s == NULL || !session_has(s, w))
923: continue;
924:
1.10 nicm 925: update += server_check_window_bell(s, w);
1.1 nicm 926: update += server_check_window_activity(s, w);
927: update += server_check_window_content(s, w, wp);
928: }
929: if (update)
930: server_status_window(w);
931:
932: w->flags &= ~(WINDOW_BELL|WINDOW_ACTIVITY|WINDOW_CONTENT);
933: }
934:
935: int
1.10 nicm 936: server_check_window_bell(struct session *s, struct window *w)
1.1 nicm 937: {
938: struct client *c;
939: u_int i;
1.10 nicm 940: int action, visual;
1.1 nicm 941:
942: if (!(w->flags & WINDOW_BELL))
943: return (0);
944: if (session_alert_has_window(s, w, WINDOW_BELL))
945: return (0);
946: session_alert_add(s, w, WINDOW_BELL);
947:
948: action = options_get_number(&s->options, "bell-action");
949: switch (action) {
950: case BELL_ANY:
951: if (s->flags & SESSION_UNATTACHED)
952: break;
1.10 nicm 953: visual = options_get_number(&s->options, "visual-bell");
1.1 nicm 954: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
955: c = ARRAY_ITEM(&clients, i);
1.10 nicm 956: if (c == NULL || c->session != s)
957: continue;
958: if (!visual) {
1.1 nicm 959: tty_putcode(&c->tty, TTYC_BEL);
1.10 nicm 960: continue;
961: }
962: if (c->session->curw->window == w) {
963: status_message_set(c, "Bell in current window");
964: continue;
965: }
966: status_message_set(c, "Bell in window %u",
967: winlink_find_by_window(&s->windows, w)->idx);
1.1 nicm 968: }
969: break;
970: case BELL_CURRENT:
1.10 nicm 971: if (s->flags & SESSION_UNATTACHED)
1.1 nicm 972: break;
1.10 nicm 973: visual = options_get_number(&s->options, "visual-bell");
1.1 nicm 974: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
975: c = ARRAY_ITEM(&clients, i);
1.10 nicm 976: if (c == NULL || c->session != s)
977: continue;
978: if (c->session->curw->window != w)
979: continue;
980: if (!visual) {
1.1 nicm 981: tty_putcode(&c->tty, TTYC_BEL);
1.10 nicm 982: continue;
983: }
984: status_message_set(c, "Bell in current window");
1.1 nicm 985: }
986: break;
987: }
988: return (1);
989: }
990:
991: int
992: server_check_window_activity(struct session *s, struct window *w)
993: {
1.10 nicm 994: struct client *c;
995: u_int i;
996:
1.1 nicm 997: if (!(w->flags & WINDOW_ACTIVITY))
998: return (0);
1.10 nicm 999:
1.1 nicm 1000: if (!options_get_number(&w->options, "monitor-activity"))
1001: return (0);
1.10 nicm 1002:
1.1 nicm 1003: if (session_alert_has_window(s, w, WINDOW_ACTIVITY))
1004: return (0);
1.10 nicm 1005: if (s->curw->window == w)
1006: return (0);
1007:
1.1 nicm 1008: session_alert_add(s, w, WINDOW_ACTIVITY);
1.10 nicm 1009: if (s->flags & SESSION_UNATTACHED)
1010: return (0);
1011: if (options_get_number(&s->options, "visual-activity")) {
1012: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1013: c = ARRAY_ITEM(&clients, i);
1014: if (c == NULL || c->session != s)
1015: continue;
1016: status_message_set(c, "Activity in window %u",
1017: winlink_find_by_window(&s->windows, w)->idx);
1018: }
1019: }
1020:
1.1 nicm 1021: return (1);
1022: }
1023:
1024: int
1025: server_check_window_content(
1026: struct session *s, struct window *w, struct window_pane *wp)
1027: {
1.10 nicm 1028: struct client *c;
1029: u_int i;
1030: char *found, *ptr;
1031:
1032: if (!(w->flags & WINDOW_ACTIVITY)) /* activity for new content */
1033: return (0);
1.1 nicm 1034:
1.10 nicm 1035: ptr = options_get_string(&w->options, "monitor-content");
1036: if (ptr == NULL || *ptr == '\0')
1.1 nicm 1037: return (0);
1.10 nicm 1038:
1039: if (session_alert_has_window(s, w, WINDOW_CONTENT))
1.1 nicm 1040: return (0);
1.10 nicm 1041: if (s->curw->window == w)
1.1 nicm 1042: return (0);
1.10 nicm 1043:
1.3 nicm 1044: if ((found = window_pane_search(wp, ptr, NULL)) == NULL)
1.1 nicm 1045: return (0);
1.10 nicm 1046: xfree(found);
1047:
1.1 nicm 1048: session_alert_add(s, w, WINDOW_CONTENT);
1.10 nicm 1049: if (s->flags & SESSION_UNATTACHED)
1050: return (0);
1051: if (options_get_number(&s->options, "visual-content")) {
1052: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1053: c = ARRAY_ITEM(&clients, i);
1054: if (c == NULL || c->session != s)
1055: continue;
1056: status_message_set(c, "Content in window %u",
1057: winlink_find_by_window(&s->windows, w)->idx);
1058: }
1059: }
1060:
1.1 nicm 1061: return (1);
1062: }
1063:
1.2 nicm 1064: /* Check if window still exists. */
1.1 nicm 1065: void
1066: server_check_window(struct window *w)
1067: {
1068: struct window_pane *wp, *wq;
1069: struct client *c;
1070: struct session *s;
1071: struct winlink *wl;
1072: u_int i, j;
1073: int destroyed, flag;
1074:
1075: flag = options_get_number(&w->options, "remain-on-exit");
1076:
1077: destroyed = 1;
1078:
1079: wp = TAILQ_FIRST(&w->panes);
1080: while (wp != NULL) {
1081: wq = TAILQ_NEXT(wp, entry);
1.2 nicm 1082: /*
1083: * If the pane has died and the remain-on-exit flag is not set,
1084: * remove the pane; otherwise, if the flag is set, don't allow
1085: * the window to be destroyed (or it'll close when the last
1086: * pane dies).
1087: */
1088: if (wp->fd == -1 && !flag) {
1.11 nicm 1089: layout_close_pane(wp);
1.1 nicm 1090: window_remove_pane(w, wp);
1091: server_redraw_window(w);
1.2 nicm 1092: } else
1093: destroyed = 0;
1.1 nicm 1094: wp = wq;
1095: }
1096:
1097: if (!destroyed)
1098: return;
1099:
1100: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1101: s = ARRAY_ITEM(&sessions, i);
1102: if (s == NULL)
1103: continue;
1104: if (!session_has(s, w))
1105: continue;
1106:
1107: restart:
1108: /* Detach window and either redraw or kill clients. */
1109: RB_FOREACH(wl, winlinks, &s->windows) {
1110: if (wl->window != w)
1111: continue;
1112: destroyed = session_detach(s, wl);
1113: for (j = 0; j < ARRAY_LENGTH(&clients); j++) {
1114: c = ARRAY_ITEM(&clients, j);
1115: if (c == NULL || c->session != s)
1116: continue;
1117: if (!destroyed) {
1118: server_redraw_client(c);
1119: continue;
1120: }
1121: c->session = NULL;
1122: server_write_client(c, MSG_EXIT, NULL, 0);
1123: }
1124: /* If the session was destroyed, bail now. */
1125: if (destroyed)
1126: break;
1127: goto restart;
1128: }
1129: }
1130:
1131: recalculate_sizes();
1132: }
1133:
1134: /* Call any once-per-second timers. */
1135: void
1136: server_second_timers(void)
1137: {
1138: struct window *w;
1139: struct window_pane *wp;
1140: u_int i;
1141: int xtimeout;
1142: struct tm now, then;
1143: static time_t last_t = 0;
1144: time_t t;
1145:
1146: t = time(NULL);
1.6 nicm 1147: xtimeout = options_get_number(&global_s_options, "lock-after-time");
1.1 nicm 1148: if (xtimeout > 0 && t > server_activity + xtimeout)
1149: server_lock();
1150:
1151: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
1152: w = ARRAY_ITEM(&windows, i);
1153: if (w == NULL)
1154: continue;
1155:
1156: TAILQ_FOREACH(wp, &w->panes, entry) {
1157: if (wp->mode != NULL && wp->mode->timer != NULL)
1158: wp->mode->timer(wp);
1159: }
1160: }
1161:
1162: /* Check for a minute having passed. */
1163: gmtime_r(&t, &now);
1164: gmtime_r(&last_t, &then);
1165: if (now.tm_min == then.tm_min)
1166: return;
1167: last_t = t;
1168:
1169: /* If locked, redraw all clients. */
1170: if (server_locked) {
1171: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1172: if (ARRAY_ITEM(&clients, i) != NULL)
1173: server_redraw_client(ARRAY_ITEM(&clients, i));
1174: }
1175: }
1176: }
1177:
1178: /* Update socket execute permissions based on whether sessions are attached. */
1179: int
1180: server_update_socket(void)
1181: {
1182: struct session *s;
1183: u_int i;
1184: static int last = -1;
1185: int n;
1186:
1187: n = 0;
1188: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1189: s = ARRAY_ITEM(&sessions, i);
1190: if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
1191: n++;
1192: break;
1193: }
1194: }
1195:
1196: if (n != last) {
1197: last = n;
1198: if (n != 0)
1199: chmod(socket_path, S_IRWXU);
1200: else
1201: chmod(socket_path, S_IRUSR|S_IWUSR);
1202: }
1203:
1204: return (n);
1205: }