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