Check cursor options when a pane is created, not just when they are changed.
[tmux.git] / server.c
blob3a2580a92481cf494527769483874ad28b65b835
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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 <termios.h>
35 #include <time.h>
36 #include <unistd.h>
38 #include "tmux.h"
41 * Main server functions.
44 struct clients clients;
46 struct tmuxproc *server_proc;
47 static int server_fd = -1;
48 static uint64_t server_client_flags;
49 static int server_exit;
50 static struct event server_ev_accept;
51 static struct event server_ev_tidy;
53 struct cmd_find_state marked_pane;
55 static u_int message_next;
56 struct message_list message_log;
58 static int server_loop(void);
59 static void server_send_exit(void);
60 static void server_accept(int, short, void *);
61 static void server_signal(int);
62 static void server_child_signal(void);
63 static void server_child_exited(pid_t, int);
64 static void server_child_stopped(pid_t, int);
66 /* Set marked pane. */
67 void
68 server_set_marked(struct session *s, struct winlink *wl, struct window_pane *wp)
70 cmd_find_clear_state(&marked_pane, 0);
71 marked_pane.s = s;
72 marked_pane.wl = wl;
73 marked_pane.w = wl->window;
74 marked_pane.wp = wp;
77 /* Clear marked pane. */
78 void
79 server_clear_marked(void)
81 cmd_find_clear_state(&marked_pane, 0);
84 /* Is this the marked pane? */
85 int
86 server_is_marked(struct session *s, struct winlink *wl, struct window_pane *wp)
88 if (s == NULL || wl == NULL || wp == NULL)
89 return (0);
90 if (marked_pane.s != s || marked_pane.wl != wl)
91 return (0);
92 if (marked_pane.wp != wp)
93 return (0);
94 return (server_check_marked());
97 /* Check if the marked pane is still valid. */
98 int
99 server_check_marked(void)
101 return (cmd_find_valid_state(&marked_pane));
104 /* Create server socket. */
105 static int
106 server_create_socket(int flags, char **cause)
108 struct sockaddr_un sa;
109 size_t size;
110 mode_t mask;
111 int fd, saved_errno;
113 memset(&sa, 0, sizeof sa);
114 sa.sun_family = AF_UNIX;
115 size = strlcpy(sa.sun_path, socket_path, sizeof sa.sun_path);
116 if (size >= sizeof sa.sun_path) {
117 errno = ENAMETOOLONG;
118 goto fail;
120 unlink(sa.sun_path);
122 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
123 goto fail;
125 if (flags & CLIENT_DEFAULTSOCKET)
126 mask = umask(S_IXUSR|S_IXGRP|S_IRWXO);
127 else
128 mask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
129 if (bind(fd, (struct sockaddr *)&sa, sizeof sa) == -1) {
130 saved_errno = errno;
131 close(fd);
132 errno = saved_errno;
133 goto fail;
135 umask(mask);
137 if (listen(fd, 128) == -1) {
138 saved_errno = errno;
139 close(fd);
140 errno = saved_errno;
141 goto fail;
143 setblocking(fd, 0);
145 return (fd);
147 fail:
148 if (cause != NULL) {
149 xasprintf(cause, "error creating %s (%s)", socket_path,
150 strerror(errno));
152 return (-1);
155 /* Tidy up every hour. */
156 static void
157 server_tidy_event(__unused int fd, __unused short events, __unused void *data)
159 struct timeval tv = { .tv_sec = 3600 };
160 uint64_t t = get_timer();
162 format_tidy_jobs();
164 log_debug("%s: took %llu milliseconds", __func__,
165 (unsigned long long)(get_timer() - t));
166 evtimer_add(&server_ev_tidy, &tv);
169 /* Fork new server. */
171 server_start(struct tmuxproc *client, int flags, struct event_base *base,
172 int lockfd, char *lockfile)
174 int fd;
175 sigset_t set, oldset;
176 struct client *c = NULL;
177 char *cause = NULL;
178 struct timeval tv = { .tv_sec = 3600 };
180 sigfillset(&set);
181 sigprocmask(SIG_BLOCK, &set, &oldset);
183 if (~flags & CLIENT_NOFORK) {
184 if (proc_fork_and_daemon(&fd) != 0) {
185 sigprocmask(SIG_SETMASK, &oldset, NULL);
186 return (fd);
189 proc_clear_signals(client, 0);
190 server_client_flags = flags;
192 if (event_reinit(base) != 0)
193 fatalx("event_reinit failed");
194 server_proc = proc_start("server");
196 proc_set_signals(server_proc, server_signal);
197 sigprocmask(SIG_SETMASK, &oldset, NULL);
199 if (log_get_level() > 1)
200 tty_create_log();
201 if (pledge("stdio rpath wpath cpath fattr unix getpw recvfd proc exec "
202 "tty ps", NULL) != 0)
203 fatal("pledge failed");
205 input_key_build();
206 RB_INIT(&windows);
207 RB_INIT(&all_window_panes);
208 TAILQ_INIT(&clients);
209 RB_INIT(&sessions);
210 key_bindings_init();
211 TAILQ_INIT(&message_log);
213 gettimeofday(&start_time, NULL);
215 server_fd = server_create_socket(flags, &cause);
216 if (server_fd != -1)
217 server_update_socket();
218 if (~flags & CLIENT_NOFORK)
219 c = server_client_create(fd);
220 else
221 options_set_number(global_options, "exit-empty", 0);
223 if (lockfd >= 0) {
224 unlink(lockfile);
225 free(lockfile);
226 close(lockfd);
229 if (cause != NULL) {
230 if (c != NULL) {
231 c->exit_message = cause;
232 c->flags |= CLIENT_EXIT;
233 } else {
234 fprintf(stderr, "%s\n", cause);
235 exit(1);
239 evtimer_set(&server_ev_tidy, server_tidy_event, NULL);
240 evtimer_add(&server_ev_tidy, &tv);
242 server_acl_init();
244 server_add_accept(0);
245 proc_loop(server_proc, server_loop);
247 job_kill_all();
248 status_prompt_save_history();
250 exit(0);
253 /* Server loop callback. */
254 static int
255 server_loop(void)
257 struct client *c;
258 u_int items;
260 do {
261 items = cmdq_next(NULL);
262 TAILQ_FOREACH(c, &clients, entry) {
263 if (c->flags & CLIENT_IDENTIFIED)
264 items += cmdq_next(c);
266 } while (items != 0);
268 server_client_loop();
270 if (!options_get_number(global_options, "exit-empty") && !server_exit)
271 return (0);
273 if (!options_get_number(global_options, "exit-unattached")) {
274 if (!RB_EMPTY(&sessions))
275 return (0);
278 TAILQ_FOREACH(c, &clients, entry) {
279 if (c->session != NULL)
280 return (0);
284 * No attached clients therefore want to exit - flush any waiting
285 * clients but don't actually exit until they've gone.
287 cmd_wait_for_flush();
288 if (!TAILQ_EMPTY(&clients))
289 return (0);
291 if (job_still_running())
292 return (0);
294 return (1);
297 /* Exit the server by killing all clients and windows. */
298 static void
299 server_send_exit(void)
301 struct client *c, *c1;
302 struct session *s, *s1;
304 cmd_wait_for_flush();
306 TAILQ_FOREACH_SAFE(c, &clients, entry, c1) {
307 if (c->flags & CLIENT_SUSPENDED)
308 server_client_lost(c);
309 else {
310 c->flags |= CLIENT_EXIT;
311 c->exit_type = CLIENT_EXIT_SHUTDOWN;
313 c->session = NULL;
316 RB_FOREACH_SAFE(s, sessions, &sessions, s1)
317 session_destroy(s, 1, __func__);
320 /* Update socket execute permissions based on whether sessions are attached. */
321 void
322 server_update_socket(void)
324 struct session *s;
325 static int last = -1;
326 int n, mode;
327 struct stat sb;
329 n = 0;
330 RB_FOREACH(s, sessions, &sessions) {
331 if (s->attached != 0) {
332 n++;
333 break;
337 if (n != last) {
338 last = n;
340 if (stat(socket_path, &sb) != 0)
341 return;
342 mode = sb.st_mode & ACCESSPERMS;
343 if (n != 0) {
344 if (mode & S_IRUSR)
345 mode |= S_IXUSR;
346 if (mode & S_IRGRP)
347 mode |= S_IXGRP;
348 if (mode & S_IROTH)
349 mode |= S_IXOTH;
350 } else
351 mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
352 chmod(socket_path, mode);
356 /* Callback for server socket. */
357 static void
358 server_accept(int fd, short events, __unused void *data)
360 struct sockaddr_storage sa;
361 socklen_t slen = sizeof sa;
362 int newfd;
363 struct client *c;
365 server_add_accept(0);
366 if (!(events & EV_READ))
367 return;
369 newfd = accept(fd, (struct sockaddr *) &sa, &slen);
370 if (newfd == -1) {
371 if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
372 return;
373 if (errno == ENFILE || errno == EMFILE) {
374 /* Delete and don't try again for 1 second. */
375 server_add_accept(1);
376 return;
378 fatal("accept failed");
381 if (server_exit) {
382 close(newfd);
383 return;
385 c = server_client_create(newfd);
386 if (!server_acl_join(c)) {
387 c->exit_message = xstrdup("access not allowed");
388 c->flags |= CLIENT_EXIT;
393 * Add accept event. If timeout is nonzero, add as a timeout instead of a read
394 * event - used to backoff when running out of file descriptors.
396 void
397 server_add_accept(int timeout)
399 struct timeval tv = { timeout, 0 };
401 if (server_fd == -1)
402 return;
404 if (event_initialized(&server_ev_accept))
405 event_del(&server_ev_accept);
407 if (timeout == 0) {
408 event_set(&server_ev_accept, server_fd, EV_READ, server_accept,
409 NULL);
410 event_add(&server_ev_accept, NULL);
411 } else {
412 event_set(&server_ev_accept, server_fd, EV_TIMEOUT,
413 server_accept, NULL);
414 event_add(&server_ev_accept, &tv);
418 /* Signal handler. */
419 static void
420 server_signal(int sig)
422 int fd;
424 log_debug("%s: %s", __func__, strsignal(sig));
425 switch (sig) {
426 case SIGINT:
427 case SIGTERM:
428 server_exit = 1;
429 server_send_exit();
430 break;
431 case SIGCHLD:
432 server_child_signal();
433 break;
434 case SIGUSR1:
435 event_del(&server_ev_accept);
436 fd = server_create_socket(server_client_flags, NULL);
437 if (fd != -1) {
438 close(server_fd);
439 server_fd = fd;
440 server_update_socket();
442 server_add_accept(0);
443 break;
444 case SIGUSR2:
445 proc_toggle_log(server_proc);
446 break;
450 /* Handle SIGCHLD. */
451 static void
452 server_child_signal(void)
454 int status;
455 pid_t pid;
457 for (;;) {
458 switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
459 case -1:
460 if (errno == ECHILD)
461 return;
462 fatal("waitpid failed");
463 case 0:
464 return;
466 if (WIFSTOPPED(status))
467 server_child_stopped(pid, status);
468 else if (WIFEXITED(status) || WIFSIGNALED(status))
469 server_child_exited(pid, status);
473 /* Handle exited children. */
474 static void
475 server_child_exited(pid_t pid, int status)
477 struct window *w, *w1;
478 struct window_pane *wp;
480 RB_FOREACH_SAFE(w, windows, &windows, w1) {
481 TAILQ_FOREACH(wp, &w->panes, entry) {
482 if (wp->pid == pid) {
483 wp->status = status;
484 wp->flags |= PANE_STATUSREADY;
486 log_debug("%%%u exited", wp->id);
487 wp->flags |= PANE_EXITED;
489 if (window_pane_destroy_ready(wp))
490 server_destroy_pane(wp, 1);
491 break;
495 job_check_died(pid, status);
498 /* Handle stopped children. */
499 static void
500 server_child_stopped(pid_t pid, int status)
502 struct window *w;
503 struct window_pane *wp;
505 if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
506 return;
508 RB_FOREACH(w, windows, &windows) {
509 TAILQ_FOREACH(wp, &w->panes, entry) {
510 if (wp->pid == pid) {
511 if (killpg(pid, SIGCONT) != 0)
512 kill(pid, SIGCONT);
516 job_check_died(pid, status);
519 /* Add to message log. */
520 void
521 server_add_message(const char *fmt, ...)
523 struct message_entry *msg, *msg1;
524 char *s;
525 va_list ap;
526 u_int limit;
528 va_start(ap, fmt);
529 xvasprintf(&s, fmt, ap);
530 va_end(ap);
532 log_debug("message: %s", s);
534 msg = xcalloc(1, sizeof *msg);
535 gettimeofday(&msg->msg_time, NULL);
536 msg->msg_num = message_next++;
537 msg->msg = s;
538 TAILQ_INSERT_TAIL(&message_log, msg, entry);
540 limit = options_get_number(global_options, "message-limit");
541 TAILQ_FOREACH_SAFE(msg, &message_log, entry, msg1) {
542 if (msg->msg_num + limit >= message_next)
543 break;
544 free(msg->msg);
545 TAILQ_REMOVE(&message_log, msg, entry);
546 free(msg);