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>
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
;
60 static int server_loop(void);
61 static void server_send_exit(void);
62 static void server_accept(int, short, void *);
63 static void server_signal(int);
64 static void server_child_signal(void);
65 static void server_child_exited(pid_t
, int);
66 static void server_child_stopped(pid_t
, int);
68 /* Set marked pane. */
70 server_set_marked(struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
72 cmd_find_clear_state(&marked_pane
, 0);
75 marked_pane
.w
= wl
->window
;
79 /* Clear marked pane. */
81 server_clear_marked(void)
83 cmd_find_clear_state(&marked_pane
, 0);
86 /* Is this the marked pane? */
88 server_is_marked(struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
90 if (s
== NULL
|| wl
== NULL
|| wp
== NULL
)
92 if (marked_pane
.s
!= s
|| marked_pane
.wl
!= wl
)
94 if (marked_pane
.wp
!= wp
)
96 return (server_check_marked());
99 /* Check if the marked pane is still valid. */
101 server_check_marked(void)
103 return (cmd_find_valid_state(&marked_pane
));
106 /* Create server socket. */
108 server_create_socket(int flags
, char **cause
)
110 struct sockaddr_un sa
;
115 memset(&sa
, 0, sizeof sa
);
116 sa
.sun_family
= AF_UNIX
;
117 size
= strlcpy(sa
.sun_path
, socket_path
, sizeof sa
.sun_path
);
118 if (size
>= sizeof sa
.sun_path
) {
119 errno
= ENAMETOOLONG
;
124 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1)
127 if (flags
& CLIENT_DEFAULTSOCKET
)
128 mask
= umask(S_IXUSR
|S_IXGRP
|S_IRWXO
);
130 mask
= umask(S_IXUSR
|S_IRWXG
|S_IRWXO
);
131 if (bind(fd
, (struct sockaddr
*)&sa
, sizeof sa
) == -1) {
139 if (listen(fd
, 128) == -1) {
151 xasprintf(cause
, "error creating %s (%s)", socket_path
,
157 /* Tidy up every hour. */
159 server_tidy_event(__unused
int fd
, __unused
short events
, __unused
void *data
)
161 struct timeval tv
= { .tv_sec
= 3600 };
162 uint64_t t
= get_timer();
166 log_debug("%s: took %llu milliseconds", __func__
,
167 (unsigned long long)(get_timer() - t
));
168 evtimer_add(&server_ev_tidy
, &tv
);
171 /* Fork new server. */
173 server_start(struct tmuxproc
*client
, int flags
, struct event_base
*base
,
174 int lockfd
, char *lockfile
)
177 sigset_t set
, oldset
;
178 struct client
*c
= NULL
;
180 struct timeval tv
= { .tv_sec
= 3600 };
183 sigprocmask(SIG_BLOCK
, &set
, &oldset
);
185 if (~flags
& CLIENT_NOFORK
) {
186 if (proc_fork_and_daemon(&fd
) != 0) {
187 sigprocmask(SIG_SETMASK
, &oldset
, NULL
);
191 proc_clear_signals(client
, 0);
192 server_client_flags
= flags
;
194 if (event_reinit(base
) != 0)
195 fatalx("event_reinit failed");
196 server_proc
= proc_start("server");
198 proc_set_signals(server_proc
, server_signal
);
199 sigprocmask(SIG_SETMASK
, &oldset
, NULL
);
201 if (log_get_level() > 1)
203 if (pledge("stdio rpath wpath cpath fattr unix getpw recvfd proc exec "
204 "tty ps", NULL
) != 0)
205 fatal("pledge failed");
208 utf8_build_combined();
210 RB_INIT(&all_window_panes
);
211 TAILQ_INIT(&clients
);
214 TAILQ_INIT(&message_log
);
215 gettimeofday(&start_time
, NULL
);
217 server_fd
= server_create_socket(flags
, &cause
);
219 server_update_socket();
220 if (~flags
& CLIENT_NOFORK
)
221 c
= server_client_create(fd
);
223 options_set_number(global_options
, "exit-empty", 0);
233 c
->exit_message
= cause
;
234 c
->flags
|= CLIENT_EXIT
;
236 fprintf(stderr
, "%s\n", cause
);
241 evtimer_set(&server_ev_tidy
, server_tidy_event
, NULL
);
242 evtimer_add(&server_ev_tidy
, &tv
);
246 server_add_accept(0);
247 proc_loop(server_proc
, server_loop
);
250 status_prompt_save_history();
255 /* Server loop callback. */
262 current_time
= time (NULL
);
265 items
= cmdq_next(NULL
);
266 TAILQ_FOREACH(c
, &clients
, entry
) {
267 if (c
->flags
& CLIENT_IDENTIFIED
)
268 items
+= cmdq_next(c
);
270 } while (items
!= 0);
272 server_client_loop();
274 if (!options_get_number(global_options
, "exit-empty") && !server_exit
)
277 if (!options_get_number(global_options
, "exit-unattached")) {
278 if (!RB_EMPTY(&sessions
))
282 TAILQ_FOREACH(c
, &clients
, entry
) {
283 if (c
->session
!= NULL
)
288 * No attached clients therefore want to exit - flush any waiting
289 * clients but don't actually exit until they've gone.
291 cmd_wait_for_flush();
292 if (!TAILQ_EMPTY(&clients
))
295 if (job_still_running())
301 /* Exit the server by killing all clients and windows. */
303 server_send_exit(void)
305 struct client
*c
, *c1
;
306 struct session
*s
, *s1
;
308 cmd_wait_for_flush();
310 TAILQ_FOREACH_SAFE(c
, &clients
, entry
, c1
) {
311 if (c
->flags
& CLIENT_SUSPENDED
)
312 server_client_lost(c
);
314 c
->flags
|= CLIENT_EXIT
;
315 c
->exit_type
= CLIENT_EXIT_SHUTDOWN
;
320 RB_FOREACH_SAFE(s
, sessions
, &sessions
, s1
)
321 session_destroy(s
, 1, __func__
);
324 /* Update socket execute permissions based on whether sessions are attached. */
326 server_update_socket(void)
329 static int last
= -1;
334 RB_FOREACH(s
, sessions
, &sessions
) {
335 if (s
->attached
!= 0) {
344 if (stat(socket_path
, &sb
) != 0)
346 mode
= sb
.st_mode
& ACCESSPERMS
;
355 mode
&= ~(S_IXUSR
|S_IXGRP
|S_IXOTH
);
356 chmod(socket_path
, mode
);
360 /* Callback for server socket. */
362 server_accept(int fd
, short events
, __unused
void *data
)
364 struct sockaddr_storage sa
;
365 socklen_t slen
= sizeof sa
;
369 server_add_accept(0);
370 if (!(events
& EV_READ
))
373 newfd
= accept(fd
, (struct sockaddr
*) &sa
, &slen
);
375 if (errno
== EAGAIN
|| errno
== EINTR
|| errno
== ECONNABORTED
)
377 if (errno
== ENFILE
|| errno
== EMFILE
) {
378 /* Delete and don't try again for 1 second. */
379 server_add_accept(1);
382 fatal("accept failed");
389 c
= server_client_create(newfd
);
390 if (!server_acl_join(c
)) {
391 c
->exit_message
= xstrdup("access not allowed");
392 c
->flags
|= CLIENT_EXIT
;
397 * Add accept event. If timeout is nonzero, add as a timeout instead of a read
398 * event - used to backoff when running out of file descriptors.
401 server_add_accept(int timeout
)
403 struct timeval tv
= { timeout
, 0 };
408 if (event_initialized(&server_ev_accept
))
409 event_del(&server_ev_accept
);
412 event_set(&server_ev_accept
, server_fd
, EV_READ
, server_accept
,
414 event_add(&server_ev_accept
, NULL
);
416 event_set(&server_ev_accept
, server_fd
, EV_TIMEOUT
,
417 server_accept
, NULL
);
418 event_add(&server_ev_accept
, &tv
);
422 /* Signal handler. */
424 server_signal(int sig
)
428 log_debug("%s: %s", __func__
, strsignal(sig
));
436 server_child_signal();
439 event_del(&server_ev_accept
);
440 fd
= server_create_socket(server_client_flags
, NULL
);
444 server_update_socket();
446 server_add_accept(0);
449 proc_toggle_log(server_proc
);
454 /* Handle SIGCHLD. */
456 server_child_signal(void)
462 switch (pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
|WUNTRACED
)) {
466 fatal("waitpid failed");
470 if (WIFSTOPPED(status
))
471 server_child_stopped(pid
, status
);
472 else if (WIFEXITED(status
) || WIFSIGNALED(status
))
473 server_child_exited(pid
, status
);
477 /* Handle exited children. */
479 server_child_exited(pid_t pid
, int status
)
481 struct window
*w
, *w1
;
482 struct window_pane
*wp
;
484 RB_FOREACH_SAFE(w
, windows
, &windows
, w1
) {
485 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
486 if (wp
->pid
== pid
) {
488 wp
->flags
|= PANE_STATUSREADY
;
490 log_debug("%%%u exited", wp
->id
);
491 wp
->flags
|= PANE_EXITED
;
493 if (window_pane_destroy_ready(wp
))
494 server_destroy_pane(wp
, 1);
499 job_check_died(pid
, status
);
502 /* Handle stopped children. */
504 server_child_stopped(pid_t pid
, int status
)
507 struct window_pane
*wp
;
509 if (WSTOPSIG(status
) == SIGTTIN
|| WSTOPSIG(status
) == SIGTTOU
)
512 RB_FOREACH(w
, windows
, &windows
) {
513 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
514 if (wp
->pid
== pid
) {
515 if (killpg(pid
, SIGCONT
) != 0)
520 job_check_died(pid
, status
);
523 /* Add to message log. */
525 server_add_message(const char *fmt
, ...)
527 struct message_entry
*msg
, *msg1
;
533 xvasprintf(&s
, fmt
, ap
);
536 log_debug("message: %s", s
);
538 msg
= xcalloc(1, sizeof *msg
);
539 gettimeofday(&msg
->msg_time
, NULL
);
540 msg
->msg_num
= message_next
++;
542 TAILQ_INSERT_TAIL(&message_log
, msg
, entry
);
544 limit
= options_get_number(global_options
, "message-limit");
545 TAILQ_FOREACH_SAFE(msg
, &message_log
, entry
, msg1
) {
546 if (msg
->msg_num
+ limit
>= message_next
)
549 TAILQ_REMOVE(&message_log
, msg
, entry
);