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