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