Add window-status-separator option, from Thomas Adam.
[tmux-openbsd.git] / server.c
blobbc0e4ab98be8b6d73de62cf61bc006316cbee7bd
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
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>
26 #include <errno.h>
27 #include <event.h>
28 #include <fcntl.h>
29 #include <paths.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <syslog.h>
35 #include <termios.h>
36 #include <time.h>
37 #include <unistd.h>
39 #include "tmux.h"
42 * Main server functions.
45 /* Client list. */
46 struct clients clients;
47 struct clients dead_clients;
49 int server_fd;
50 int server_shutdown;
51 struct event server_ev_accept;
52 struct event server_ev_second;
54 struct paste_stack global_buffers;
56 int server_create_socket(void);
57 void server_loop(void);
58 int server_should_shutdown(void);
59 void server_send_shutdown(void);
60 void server_clean_dead(void);
61 void server_accept_callback(int, short, void *);
62 void server_signal_callback(int, short, void *);
63 void server_child_signal(void);
64 void server_child_exited(pid_t, int);
65 void server_child_stopped(pid_t, int);
66 void server_second_callback(int, short, void *);
67 void server_lock_server(void);
68 void server_lock_sessions(void);
70 /* Create server socket. */
71 int
72 server_create_socket(void)
74 struct sockaddr_un sa;
75 size_t size;
76 mode_t mask;
77 int fd;
79 memset(&sa, 0, sizeof sa);
80 sa.sun_family = AF_UNIX;
81 size = strlcpy(sa.sun_path, socket_path, sizeof sa.sun_path);
82 if (size >= sizeof sa.sun_path) {
83 errno = ENAMETOOLONG;
84 fatal("socket failed");
86 unlink(sa.sun_path);
88 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
89 fatal("socket failed");
91 mask = umask(S_IXUSR|S_IXGRP|S_IRWXO);
92 if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1)
93 fatal("bind failed");
94 umask(mask);
96 if (listen(fd, 16) == -1)
97 fatal("listen failed");
98 setblocking(fd, 0);
100 server_update_socket();
102 return (fd);
105 /* Fork new server. */
107 server_start(int lockfd, char *lockfile)
109 struct window_pane *wp;
110 int pair[2];
111 char *cause;
112 struct timeval tv;
113 u_int i;
115 /* The first client is special and gets a socketpair; create it. */
116 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
117 fatal("socketpair failed");
119 switch (fork()) {
120 case -1:
121 fatal("fork failed");
122 case 0:
123 break;
124 default:
125 close(pair[1]);
126 return (pair[0]);
128 close(pair[0]);
131 * Must daemonise before loading configuration as the PID changes so
132 * $TMUX would be wrong for sessions created in the config file.
134 if (daemon(1, 0) != 0)
135 fatal("daemon failed");
137 /* event_init() was called in our parent, need to reinit. */
138 if (event_reinit(ev_base) != 0)
139 fatal("event_reinit failed");
140 clear_signals(0);
142 logfile("server");
143 log_debug("server started, pid %ld", (long) getpid());
145 ARRAY_INIT(&windows);
146 RB_INIT(&all_window_panes);
147 ARRAY_INIT(&clients);
148 ARRAY_INIT(&dead_clients);
149 RB_INIT(&sessions);
150 RB_INIT(&dead_sessions);
151 TAILQ_INIT(&session_groups);
152 ARRAY_INIT(&global_buffers);
153 mode_key_init_trees();
154 key_bindings_init();
155 utf8_build();
157 start_time = time(NULL);
158 log_debug("socket path %s", socket_path);
159 setproctitle("server (%s)", socket_path);
161 server_fd = server_create_socket();
162 server_client_create(pair[1]);
164 unlink(lockfile);
165 xfree(lockfile);
166 close(lockfd);
168 if (access(SYSTEM_CFG, R_OK) == 0)
169 load_cfg(SYSTEM_CFG, NULL, &cfg_causes);
170 else if (errno != ENOENT) {
171 cfg_add_cause(
172 &cfg_causes, "%s: %s", strerror(errno), SYSTEM_CFG);
174 if (cfg_file != NULL)
175 load_cfg(cfg_file, NULL, &cfg_causes);
178 * If there is a session already, put the current window and pane into
179 * more mode.
181 if (!RB_EMPTY(&sessions) && !ARRAY_EMPTY(&cfg_causes)) {
182 wp = RB_MIN(sessions, &sessions)->curw->window->active;
183 window_pane_set_mode(wp, &window_copy_mode);
184 window_copy_init_for_output(wp);
185 for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
186 cause = ARRAY_ITEM(&cfg_causes, i);
187 window_copy_add(wp, "%s", cause);
188 xfree(cause);
190 ARRAY_FREE(&cfg_causes);
192 cfg_finished = 1;
194 server_add_accept(0);
196 memset(&tv, 0, sizeof tv);
197 tv.tv_sec = 1;
198 evtimer_set(&server_ev_second, server_second_callback, NULL);
199 evtimer_add(&server_ev_second, &tv);
201 set_signals(server_signal_callback);
202 server_loop();
203 exit(0);
206 /* Main server loop. */
207 void
208 server_loop(void)
210 while (!server_should_shutdown()) {
211 event_loop(EVLOOP_ONCE);
213 server_window_loop();
214 server_client_loop();
216 key_bindings_clean();
217 server_clean_dead();
221 /* Check if the server should be shutting down (no more clients or sessions). */
223 server_should_shutdown(void)
225 u_int i;
227 if (!options_get_number(&global_options, "exit-unattached")) {
228 if (!RB_EMPTY(&sessions))
229 return (0);
231 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
232 if (ARRAY_ITEM(&clients, i) != NULL)
233 return (0);
235 return (1);
238 /* Shutdown the server by killing all clients and windows. */
239 void
240 server_send_shutdown(void)
242 struct client *c;
243 struct session *s, *next_s;
244 u_int i;
246 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
247 c = ARRAY_ITEM(&clients, i);
248 if (c != NULL) {
249 if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
250 server_client_lost(c);
251 else
252 server_write_client(c, MSG_SHUTDOWN, NULL, 0);
253 c->session = NULL;
257 s = RB_MIN(sessions, &sessions);
258 while (s != NULL) {
259 next_s = RB_NEXT(sessions, &sessions, s);
260 session_destroy(s);
261 s = next_s;
265 /* Free dead, unreferenced clients and sessions. */
266 void
267 server_clean_dead(void)
269 struct session *s, *next_s;
270 struct client *c;
271 u_int i;
273 s = RB_MIN(sessions, &dead_sessions);
274 while (s != NULL) {
275 next_s = RB_NEXT(sessions, &dead_sessions, s);
276 if (s->references == 0) {
277 RB_REMOVE(sessions, &dead_sessions, s);
278 xfree(s->name);
279 xfree(s);
281 s = next_s;
284 for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
285 c = ARRAY_ITEM(&dead_clients, i);
286 if (c == NULL || c->references != 0)
287 continue;
288 ARRAY_SET(&dead_clients, i, NULL);
289 xfree(c);
293 /* Update socket execute permissions based on whether sessions are attached. */
294 void
295 server_update_socket(void)
297 struct session *s;
298 static int last = -1;
299 int n, mode;
300 struct stat sb;
302 n = 0;
303 RB_FOREACH(s, sessions, &sessions) {
304 if (!(s->flags & SESSION_UNATTACHED)) {
305 n++;
306 break;
310 if (n != last) {
311 last = n;
313 if (stat(socket_path, &sb) != 0)
314 return;
315 mode = sb.st_mode;
316 if (n != 0) {
317 if (mode & S_IRUSR)
318 mode |= S_IXUSR;
319 if (mode & S_IRGRP)
320 mode |= S_IXGRP;
321 if (mode & S_IROTH)
322 mode |= S_IXOTH;
323 } else
324 mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
325 chmod(socket_path, mode);
329 /* Callback for server socket. */
330 /* ARGSUSED */
331 void
332 server_accept_callback(int fd, short events, unused void *data)
334 struct sockaddr_storage sa;
335 socklen_t slen = sizeof sa;
336 int newfd;
338 server_add_accept(0);
339 if (!(events & EV_READ))
340 return;
342 newfd = accept(fd, (struct sockaddr *) &sa, &slen);
343 if (newfd == -1) {
344 if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
345 return;
346 if (errno == ENFILE || errno == EMFILE) {
347 /* Delete and don't try again for 1 second. */
348 server_add_accept(1);
349 return;
351 fatal("accept failed");
353 if (server_shutdown) {
354 close(newfd);
355 return;
357 server_client_create(newfd);
361 * Add accept event. If timeout is nonzero, add as a timeout instead of a read
362 * event - used to backoff when running out of file descriptors.
364 void
365 server_add_accept(int timeout)
367 struct timeval tv = { timeout, 0 };
369 if (event_initialized(&server_ev_accept))
370 event_del(&server_ev_accept);
372 if (timeout == 0) {
373 event_set(&server_ev_accept,
374 server_fd, EV_READ, server_accept_callback, NULL);
375 event_add(&server_ev_accept, NULL);
376 } else {
377 event_set(&server_ev_accept,
378 server_fd, EV_TIMEOUT, server_accept_callback, NULL);
379 event_add(&server_ev_accept, &tv);
383 /* Signal handler. */
384 /* ARGSUSED */
385 void
386 server_signal_callback(int sig, unused short events, unused void *data)
388 switch (sig) {
389 case SIGTERM:
390 server_shutdown = 1;
391 server_send_shutdown();
392 break;
393 case SIGCHLD:
394 server_child_signal();
395 break;
396 case SIGUSR1:
397 event_del(&server_ev_accept);
398 close(server_fd);
399 server_fd = server_create_socket();
400 server_add_accept(0);
401 break;
405 /* Handle SIGCHLD. */
406 void
407 server_child_signal(void)
409 int status;
410 pid_t pid;
412 for (;;) {
413 switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
414 case -1:
415 if (errno == ECHILD)
416 return;
417 fatal("waitpid failed");
418 case 0:
419 return;
421 if (WIFSTOPPED(status))
422 server_child_stopped(pid, status);
423 else if (WIFEXITED(status) || WIFSIGNALED(status))
424 server_child_exited(pid, status);
428 /* Handle exited children. */
429 void
430 server_child_exited(pid_t pid, int status)
432 struct window *w;
433 struct window_pane *wp;
434 struct job *job;
435 u_int i;
437 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
438 if ((w = ARRAY_ITEM(&windows, i)) == NULL)
439 continue;
440 TAILQ_FOREACH(wp, &w->panes, entry) {
441 if (wp->pid == pid) {
442 server_destroy_pane(wp);
443 break;
448 LIST_FOREACH(job, &all_jobs, lentry) {
449 if (pid == job->pid) {
450 job_died(job, status); /* might free job */
451 break;
456 /* Handle stopped children. */
457 void
458 server_child_stopped(pid_t pid, int status)
460 struct window *w;
461 struct window_pane *wp;
462 u_int i;
464 if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
465 return;
467 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
468 if ((w = ARRAY_ITEM(&windows, i)) == NULL)
469 continue;
470 TAILQ_FOREACH(wp, &w->panes, entry) {
471 if (wp->pid == pid) {
472 if (killpg(pid, SIGCONT) != 0)
473 kill(pid, SIGCONT);
479 /* Handle once-per-second timer events. */
480 /* ARGSUSED */
481 void
482 server_second_callback(unused int fd, unused short events, unused void *arg)
484 struct window *w;
485 struct window_pane *wp;
486 struct timeval tv;
487 u_int i;
489 if (options_get_number(&global_s_options, "lock-server"))
490 server_lock_server();
491 else
492 server_lock_sessions();
494 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
495 w = ARRAY_ITEM(&windows, i);
496 if (w == NULL)
497 continue;
499 TAILQ_FOREACH(wp, &w->panes, entry) {
500 if (wp->mode != NULL && wp->mode->timer != NULL)
501 wp->mode->timer(wp);
505 server_client_status_timer();
507 evtimer_del(&server_ev_second);
508 memset(&tv, 0, sizeof tv);
509 tv.tv_sec = 1;
510 evtimer_add(&server_ev_second, &tv);
513 /* Lock the server if ALL sessions have hit the time limit. */
514 void
515 server_lock_server(void)
517 struct session *s;
518 int timeout;
519 time_t t;
521 t = time(NULL);
522 RB_FOREACH(s, sessions, &sessions) {
523 if (s->flags & SESSION_UNATTACHED)
524 continue;
525 timeout = options_get_number(&s->options, "lock-after-time");
526 if (timeout <= 0 || t <= s->activity_time.tv_sec + timeout)
527 return; /* not timed out */
530 server_lock();
531 recalculate_sizes();
534 /* Lock any sessions which have timed out. */
535 void
536 server_lock_sessions(void)
538 struct session *s;
539 int timeout;
540 time_t t;
542 t = time(NULL);
543 RB_FOREACH(s, sessions, &sessions) {
544 if (s->flags & SESSION_UNATTACHED)
545 continue;
546 timeout = options_get_number(&s->options, "lock-after-time");
547 if (timeout > 0 && t > s->activity_time.tv_sec + timeout) {
548 server_lock_session(s);
549 recalculate_sizes();