Annotation of src/usr.bin/tmux/server.c, Revision 1.48
1.48 ! nicm 1: /* $OpenBSD: server.c,v 1.47 2009/10/10 10:02:48 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;
1.48 ! nicm 828: struct options *oo;
1.1 nicm 829: struct timeval tv;
830: struct key_binding *bd;
1.40 nicm 831: struct keylist *keylist;
832: int key, status, xtimeout, mode, isprefix;
833: u_int i;
1.1 nicm 834: u_char mouse[3];
835:
836: xtimeout = options_get_number(&c->session->options, "repeat-time");
837: if (xtimeout != 0 && c->flags & CLIENT_REPEAT) {
838: if (gettimeofday(&tv, NULL) != 0)
1.39 nicm 839: fatal("gettimeofday failed");
1.1 nicm 840: if (timercmp(&tv, &c->repeat_timer, >))
841: c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
842: }
843:
844: /* Process keys. */
1.40 nicm 845: keylist = options_get_data(&c->session->options, "prefix");
1.1 nicm 846: while (tty_keys_next(&c->tty, &key, mouse) == 0) {
847: if (c->session == NULL)
848: return;
1.46 nicm 849:
850: c->session->activity = time(NULL);
1.32 nicm 851: w = c->session->curw->window;
852: wp = w->active; /* could die */
1.48 ! nicm 853: oo = &c->session->options;
1.1 nicm 854:
1.32 nicm 855: /* Special case: number keys jump to pane in identify mode. */
856: if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
857: wp = window_pane_at_index(w, key - '0');
858: if (wp != NULL && window_pane_visible(wp))
859: window_set_active_pane(w, wp);
860: server_clear_identify(c);
861: continue;
862: }
863:
1.1 nicm 864: status_message_clear(c);
1.26 nicm 865: server_clear_identify(c);
1.1 nicm 866: if (c->prompt_string != NULL) {
867: status_prompt_key(c, key);
868: continue;
869: }
870:
871: /* Check for mouse keys. */
872: if (key == KEYC_MOUSE) {
1.48 ! nicm 873: if (options_get_number(oo, "mouse-select-pane")) {
! 874: window_set_active_at(w, mouse[1], mouse[2]);
! 875: wp = w->active;
! 876: }
1.1 nicm 877: window_pane_mouse(wp, c, mouse[0], mouse[1], mouse[2]);
878: continue;
879: }
880:
1.40 nicm 881: /* Is this a prefix key? */
882: isprefix = 0;
883: for (i = 0; i < ARRAY_LENGTH(keylist); i++) {
884: if (key == ARRAY_ITEM(keylist, i)) {
885: isprefix = 1;
886: break;
887: }
888: }
889:
1.1 nicm 890: /* No previous prefix key. */
891: if (!(c->flags & CLIENT_PREFIX)) {
1.40 nicm 892: if (isprefix)
1.1 nicm 893: c->flags |= CLIENT_PREFIX;
1.14 nicm 894: else {
895: /* Try as a non-prefix key binding. */
896: if ((bd = key_bindings_lookup(key)) == NULL)
897: window_pane_key(wp, c, key);
898: else
899: key_bindings_dispatch(bd, c);
900: }
1.1 nicm 901: continue;
902: }
903:
904: /* Prefix key already pressed. Reset prefix and lookup key. */
905: c->flags &= ~CLIENT_PREFIX;
1.14 nicm 906: if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
1.1 nicm 907: /* If repeating, treat this as a key, else ignore. */
908: if (c->flags & CLIENT_REPEAT) {
909: c->flags &= ~CLIENT_REPEAT;
1.40 nicm 910: if (isprefix)
1.1 nicm 911: c->flags |= CLIENT_PREFIX;
912: else
913: window_pane_key(wp, c, key);
914: }
915: continue;
916: }
917:
918: /* If already repeating, but this key can't repeat, skip it. */
919: if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
920: c->flags &= ~CLIENT_REPEAT;
1.40 nicm 921: if (isprefix)
1.1 nicm 922: c->flags |= CLIENT_PREFIX;
923: else
924: window_pane_key(wp, c, key);
925: continue;
926: }
927:
928: /* If this key can repeat, reset the repeat flags and timer. */
929: if (xtimeout != 0 && bd->can_repeat) {
930: c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
931:
932: tv.tv_sec = xtimeout / 1000;
933: tv.tv_usec = (xtimeout % 1000) * 1000L;
934: if (gettimeofday(&c->repeat_timer, NULL) != 0)
1.39 nicm 935: fatal("gettimeofday failed");
1.1 nicm 936: timeradd(&c->repeat_timer, &tv, &c->repeat_timer);
937: }
938:
939: /* Dispatch the command. */
940: key_bindings_dispatch(bd, c);
941: }
942: if (c->session == NULL)
943: return;
1.48 ! nicm 944: w = c->session->curw->window;
! 945: wp = w->active;
! 946: oo = &c->session->options;
1.1 nicm 947: s = wp->screen;
948:
1.21 nicm 949: /*
950: * Update cursor position and mode settings. The scroll region and
951: * attributes are cleared across poll(2) as this is the most likely
952: * time a user may interrupt tmux, for example with ~^Z in ssh(1). This
953: * is a compromise between excessive resets and likelihood of an
954: * interrupt.
955: *
956: * tty_region/tty_reset/tty_update_mode already take care of not
957: * resetting things that are already in their default state.
958: */
1.48 ! nicm 959: status = options_get_number(oo, "status");
1.17 nicm 960: tty_region(&c->tty, 0, c->tty.sy - 1, 0);
1.11 nicm 961: if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
962: tty_cursor(&c->tty, 0, 0, 0, 0);
963: else
1.1 nicm 964: tty_cursor(&c->tty, s->cx, s->cy, wp->xoff, wp->yoff);
965:
966: mode = s->mode;
1.48 ! nicm 967: if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
! 968: options_get_number(oo, "mouse-select-pane"))
! 969: mode |= MODE_MOUSE;
1.1 nicm 970: tty_update_mode(&c->tty, mode);
1.21 nicm 971: tty_reset(&c->tty);
1.1 nicm 972: }
973:
974: /* Lost a client. */
975: void
976: server_lost_client(struct client *c)
977: {
978: u_int i;
979:
980: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
981: if (ARRAY_ITEM(&clients, i) == c)
982: ARRAY_SET(&clients, i, NULL);
983: }
1.20 nicm 984: log_debug("lost client %d", c->ibuf.fd);
1.1 nicm 985:
1.25 nicm 986: /*
987: * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
988: * and tty_free might close an unrelated fd.
989: */
990: if (c->flags & CLIENT_TERMINAL)
991: tty_free(&c->tty);
1.1 nicm 992:
993: screen_free(&c->status);
994:
995: if (c->title != NULL)
996: xfree(c->title);
997:
998: if (c->message_string != NULL)
999: xfree(c->message_string);
1000:
1001: if (c->prompt_string != NULL)
1002: xfree(c->prompt_string);
1003: if (c->prompt_buffer != NULL)
1004: xfree(c->prompt_buffer);
1005: for (i = 0; i < ARRAY_LENGTH(&c->prompt_hdata); i++)
1006: xfree(ARRAY_ITEM(&c->prompt_hdata, i));
1007: ARRAY_FREE(&c->prompt_hdata);
1008:
1009: if (c->cwd != NULL)
1010: xfree(c->cwd);
1011:
1.18 nicm 1012: close(c->ibuf.fd);
1013: imsg_clear(&c->ibuf);
1.31 nicm 1014:
1015: for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
1016: if (ARRAY_ITEM(&dead_clients, i) == NULL) {
1017: ARRAY_SET(&dead_clients, i, c);
1018: break;
1019: }
1020: }
1021: if (i == ARRAY_LENGTH(&dead_clients))
1022: ARRAY_ADD(&dead_clients, c);
1023: c->flags |= CLIENT_DEAD;
1.1 nicm 1024:
1025: recalculate_sizes();
1.31 nicm 1026: }
1027:
1028: /* Free dead, unreferenced clients and sessions. */
1029: void
1030: server_clean_dead(void)
1031: {
1032: struct session *s;
1033: struct client *c;
1034: u_int i;
1035:
1036: for (i = 0; i < ARRAY_LENGTH(&dead_sessions); i++) {
1037: s = ARRAY_ITEM(&dead_sessions, i);
1038: if (s == NULL || s->references != 0)
1039: continue;
1040: ARRAY_SET(&dead_sessions, i, NULL);
1041: xfree(s);
1042: }
1043:
1044: for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
1045: c = ARRAY_ITEM(&dead_clients, i);
1046: if (c == NULL || c->references != 0)
1047: continue;
1048: ARRAY_SET(&dead_clients, i, NULL);
1049: xfree(c);
1050: }
1.1 nicm 1051: }
1052:
1053: /* Handle window data. */
1054: void
1055: server_handle_window(struct window *w, struct window_pane *wp)
1056: {
1057: struct session *s;
1058: u_int i;
1059: int update;
1060:
1061: window_pane_parse(wp);
1062:
1063: if ((w->flags & (WINDOW_BELL|WINDOW_ACTIVITY|WINDOW_CONTENT)) == 0)
1064: return;
1065:
1066: update = 0;
1067: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1068: s = ARRAY_ITEM(&sessions, i);
1069: if (s == NULL || !session_has(s, w))
1070: continue;
1071:
1.10 nicm 1072: update += server_check_window_bell(s, w);
1.1 nicm 1073: update += server_check_window_activity(s, w);
1074: update += server_check_window_content(s, w, wp);
1075: }
1076: if (update)
1077: server_status_window(w);
1078:
1079: w->flags &= ~(WINDOW_BELL|WINDOW_ACTIVITY|WINDOW_CONTENT);
1080: }
1081:
1082: int
1.10 nicm 1083: server_check_window_bell(struct session *s, struct window *w)
1.1 nicm 1084: {
1085: struct client *c;
1086: u_int i;
1.10 nicm 1087: int action, visual;
1.1 nicm 1088:
1089: if (!(w->flags & WINDOW_BELL))
1090: return (0);
1091: if (session_alert_has_window(s, w, WINDOW_BELL))
1092: return (0);
1093: session_alert_add(s, w, WINDOW_BELL);
1094:
1095: action = options_get_number(&s->options, "bell-action");
1096: switch (action) {
1097: case BELL_ANY:
1098: if (s->flags & SESSION_UNATTACHED)
1099: break;
1.10 nicm 1100: visual = options_get_number(&s->options, "visual-bell");
1.1 nicm 1101: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1102: c = ARRAY_ITEM(&clients, i);
1.10 nicm 1103: if (c == NULL || c->session != s)
1104: continue;
1105: if (!visual) {
1.1 nicm 1106: tty_putcode(&c->tty, TTYC_BEL);
1.10 nicm 1107: continue;
1108: }
1109: if (c->session->curw->window == w) {
1110: status_message_set(c, "Bell in current window");
1111: continue;
1112: }
1113: status_message_set(c, "Bell in window %u",
1114: winlink_find_by_window(&s->windows, w)->idx);
1.1 nicm 1115: }
1116: break;
1117: case BELL_CURRENT:
1.10 nicm 1118: if (s->flags & SESSION_UNATTACHED)
1.1 nicm 1119: break;
1.10 nicm 1120: visual = options_get_number(&s->options, "visual-bell");
1.1 nicm 1121: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1122: c = ARRAY_ITEM(&clients, i);
1.10 nicm 1123: if (c == NULL || c->session != s)
1124: continue;
1125: if (c->session->curw->window != w)
1126: continue;
1127: if (!visual) {
1.1 nicm 1128: tty_putcode(&c->tty, TTYC_BEL);
1.10 nicm 1129: continue;
1130: }
1131: status_message_set(c, "Bell in current window");
1.1 nicm 1132: }
1133: break;
1134: }
1135: return (1);
1136: }
1137:
1138: int
1139: server_check_window_activity(struct session *s, struct window *w)
1140: {
1.10 nicm 1141: struct client *c;
1142: u_int i;
1143:
1.1 nicm 1144: if (!(w->flags & WINDOW_ACTIVITY))
1145: return (0);
1.10 nicm 1146:
1.1 nicm 1147: if (!options_get_number(&w->options, "monitor-activity"))
1148: return (0);
1.10 nicm 1149:
1.1 nicm 1150: if (session_alert_has_window(s, w, WINDOW_ACTIVITY))
1151: return (0);
1.10 nicm 1152: if (s->curw->window == w)
1153: return (0);
1154:
1.1 nicm 1155: session_alert_add(s, w, WINDOW_ACTIVITY);
1.10 nicm 1156: if (s->flags & SESSION_UNATTACHED)
1157: return (0);
1158: if (options_get_number(&s->options, "visual-activity")) {
1159: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1160: c = ARRAY_ITEM(&clients, i);
1161: if (c == NULL || c->session != s)
1162: continue;
1163: status_message_set(c, "Activity in window %u",
1164: winlink_find_by_window(&s->windows, w)->idx);
1165: }
1166: }
1167:
1.1 nicm 1168: return (1);
1169: }
1170:
1171: int
1172: server_check_window_content(
1173: struct session *s, struct window *w, struct window_pane *wp)
1174: {
1.10 nicm 1175: struct client *c;
1176: u_int i;
1177: char *found, *ptr;
1178:
1179: if (!(w->flags & WINDOW_ACTIVITY)) /* activity for new content */
1180: return (0);
1.1 nicm 1181:
1.10 nicm 1182: ptr = options_get_string(&w->options, "monitor-content");
1183: if (ptr == NULL || *ptr == '\0')
1.1 nicm 1184: return (0);
1.10 nicm 1185:
1186: if (session_alert_has_window(s, w, WINDOW_CONTENT))
1.1 nicm 1187: return (0);
1.10 nicm 1188: if (s->curw->window == w)
1.1 nicm 1189: return (0);
1.10 nicm 1190:
1.3 nicm 1191: if ((found = window_pane_search(wp, ptr, NULL)) == NULL)
1.1 nicm 1192: return (0);
1.10 nicm 1193: xfree(found);
1194:
1.1 nicm 1195: session_alert_add(s, w, WINDOW_CONTENT);
1.10 nicm 1196: if (s->flags & SESSION_UNATTACHED)
1197: return (0);
1198: if (options_get_number(&s->options, "visual-content")) {
1199: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1200: c = ARRAY_ITEM(&clients, i);
1201: if (c == NULL || c->session != s)
1202: continue;
1203: status_message_set(c, "Content in window %u",
1204: winlink_find_by_window(&s->windows, w)->idx);
1205: }
1206: }
1207:
1.1 nicm 1208: return (1);
1209: }
1210:
1.2 nicm 1211: /* Check if window still exists. */
1.1 nicm 1212: void
1213: server_check_window(struct window *w)
1214: {
1215: struct window_pane *wp, *wq;
1.22 nicm 1216: struct options *oo = &w->options;
1.1 nicm 1217: struct session *s;
1218: struct winlink *wl;
1.35 nicm 1219: u_int i;
1.22 nicm 1220: int destroyed;
1.1 nicm 1221:
1222: destroyed = 1;
1223:
1224: wp = TAILQ_FIRST(&w->panes);
1225: while (wp != NULL) {
1226: wq = TAILQ_NEXT(wp, entry);
1.2 nicm 1227: /*
1228: * If the pane has died and the remain-on-exit flag is not set,
1229: * remove the pane; otherwise, if the flag is set, don't allow
1230: * the window to be destroyed (or it'll close when the last
1231: * pane dies).
1232: */
1.23 nicm 1233: if (wp->fd == -1 && !options_get_number(oo, "remain-on-exit")) {
1.11 nicm 1234: layout_close_pane(wp);
1.1 nicm 1235: window_remove_pane(w, wp);
1236: server_redraw_window(w);
1.2 nicm 1237: } else
1238: destroyed = 0;
1.1 nicm 1239: wp = wq;
1240: }
1241:
1242: if (!destroyed)
1243: return;
1244:
1245: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1246: s = ARRAY_ITEM(&sessions, i);
1247: if (s == NULL)
1248: continue;
1249: if (!session_has(s, w))
1250: continue;
1251:
1252: restart:
1253: /* Detach window and either redraw or kill clients. */
1254: RB_FOREACH(wl, winlinks, &s->windows) {
1255: if (wl->window != w)
1256: continue;
1.34 nicm 1257: if (session_detach(s, wl)) {
1.47 nicm 1258: server_destroy_session_group(s);
1.34 nicm 1259: break;
1.1 nicm 1260: }
1.34 nicm 1261: server_redraw_session(s);
1.47 nicm 1262: server_status_session_group(s);
1.1 nicm 1263: goto restart;
1264: }
1265: }
1266:
1267: recalculate_sizes();
1268: }
1269:
1.46 nicm 1270: /* Lock the server if ALL sessions have hit the time limit. */
1271: void
1272: server_lock_server(void)
1273: {
1274: struct session *s;
1275: u_int i;
1276: int timeout;
1277: time_t t;
1278:
1279: t = time(NULL);
1280: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1281: if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
1282: continue;
1283:
1284: timeout = options_get_number(&s->options, "lock-after-time");
1285: if (timeout <= 0 || t <= s->activity + timeout)
1286: return; /* not timed out */
1287: }
1288:
1289: server_lock();
1290: recalculate_sizes();
1291: }
1292:
1293: /* Lock any sessions which have timed out. */
1294: void
1295: server_lock_sessions(void)
1296: {
1297: struct session *s;
1298: u_int i;
1299: int timeout;
1300: time_t t;
1301:
1302: t = time(NULL);
1303: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1304: if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
1305: continue;
1306:
1307: timeout = options_get_number(&s->options, "lock-after-time");
1308: if (timeout > 0 && t > s->activity + timeout) {
1309: server_lock_session(s);
1310: recalculate_sizes();
1311: }
1312: }
1313: }
1314:
1.1 nicm 1315: /* Call any once-per-second timers. */
1316: void
1317: server_second_timers(void)
1318: {
1319: struct window *w;
1320: struct window_pane *wp;
1321: u_int i;
1322:
1.46 nicm 1323: if (options_get_number(&global_s_options, "lock-server"))
1324: server_lock_server();
1325: else
1326: server_lock_sessions();
1.1 nicm 1327:
1328: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
1329: w = ARRAY_ITEM(&windows, i);
1330: if (w == NULL)
1331: continue;
1332:
1333: TAILQ_FOREACH(wp, &w->panes, entry) {
1334: if (wp->mode != NULL && wp->mode->timer != NULL)
1335: wp->mode->timer(wp);
1336: }
1337: }
1338: }
1339:
1340: /* Update socket execute permissions based on whether sessions are attached. */
1341: int
1342: server_update_socket(void)
1343: {
1344: struct session *s;
1345: u_int i;
1346: static int last = -1;
1347: int n;
1348:
1349: n = 0;
1350: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1351: s = ARRAY_ITEM(&sessions, i);
1352: if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
1353: n++;
1354: break;
1355: }
1356: }
1357:
1358: if (n != last) {
1359: last = n;
1360: if (n != 0)
1361: chmod(socket_path, S_IRWXU);
1362: else
1363: chmod(socket_path, S_IRUSR|S_IWUSR);
1364: }
1365:
1366: return (n);
1367: }