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
;
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. */
68 server_set_marked(struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
70 cmd_find_clear_state(&marked_pane
, 0);
73 marked_pane
.w
= wl
->window
;
77 /* Clear marked pane. */
79 server_clear_marked(void)
81 cmd_find_clear_state(&marked_pane
, 0);
84 /* Is this the marked pane? */
86 server_is_marked(struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
88 if (s
== NULL
|| wl
== NULL
|| wp
== NULL
)
90 if (marked_pane
.s
!= s
|| marked_pane
.wl
!= wl
)
92 if (marked_pane
.wp
!= wp
)
94 return (server_check_marked());
97 /* Check if the marked pane is still valid. */
99 server_check_marked(void)
101 return (cmd_find_valid_state(&marked_pane
));
104 /* Create server socket. */
106 server_create_socket(int flags
, char **cause
)
108 struct sockaddr_un sa
;
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
;
122 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1)
125 if (flags
& CLIENT_DEFAULTSOCKET
)
126 mask
= umask(S_IXUSR
|S_IXGRP
|S_IRWXO
);
128 mask
= umask(S_IXUSR
|S_IRWXG
|S_IRWXO
);
129 if (bind(fd
, (struct sockaddr
*)&sa
, sizeof sa
) == -1) {
137 if (listen(fd
, 128) == -1) {
149 xasprintf(cause
, "error creating %s (%s)", socket_path
,
155 /* Tidy up every hour. */
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();
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
)
175 sigset_t set
, oldset
;
176 struct client
*c
= NULL
;
178 struct timeval tv
= { .tv_sec
= 3600 };
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
);
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)
201 if (pledge("stdio rpath wpath cpath fattr unix getpw recvfd proc exec "
202 "tty ps", NULL
) != 0)
203 fatal("pledge failed");
207 RB_INIT(&all_window_panes
);
208 TAILQ_INIT(&clients
);
211 TAILQ_INIT(&message_log
);
213 gettimeofday(&start_time
, NULL
);
215 server_fd
= server_create_socket(flags
, &cause
);
217 server_update_socket();
218 if (~flags
& CLIENT_NOFORK
)
219 c
= server_client_create(fd
);
221 options_set_number(global_options
, "exit-empty", 0);
231 cmdq_append(c
, cmdq_get_error(cause
));
232 c
->flags
|= CLIENT_EXIT
;
237 evtimer_set(&server_ev_tidy
, server_tidy_event
, NULL
);
238 evtimer_add(&server_ev_tidy
, &tv
);
240 server_add_accept(0);
241 proc_loop(server_proc
, server_loop
);
244 status_prompt_save_history();
249 /* Server loop callback. */
257 items
= cmdq_next(NULL
);
258 TAILQ_FOREACH(c
, &clients
, entry
) {
259 if (c
->flags
& CLIENT_IDENTIFIED
)
260 items
+= cmdq_next(c
);
262 } while (items
!= 0);
264 server_client_loop();
266 if (!options_get_number(global_options
, "exit-empty") && !server_exit
)
269 if (!options_get_number(global_options
, "exit-unattached")) {
270 if (!RB_EMPTY(&sessions
))
274 TAILQ_FOREACH(c
, &clients
, entry
) {
275 if (c
->session
!= NULL
)
280 * No attached clients therefore want to exit - flush any waiting
281 * clients but don't actually exit until they've gone.
283 cmd_wait_for_flush();
284 if (!TAILQ_EMPTY(&clients
))
287 if (job_still_running())
293 /* Exit the server by killing all clients and windows. */
295 server_send_exit(void)
297 struct client
*c
, *c1
;
298 struct session
*s
, *s1
;
300 cmd_wait_for_flush();
302 TAILQ_FOREACH_SAFE(c
, &clients
, entry
, c1
) {
303 if (c
->flags
& CLIENT_SUSPENDED
)
304 server_client_lost(c
);
306 c
->flags
|= CLIENT_EXIT
;
307 c
->exit_type
= CLIENT_EXIT_SHUTDOWN
;
312 RB_FOREACH_SAFE(s
, sessions
, &sessions
, s1
)
313 session_destroy(s
, 1, __func__
);
316 /* Update socket execute permissions based on whether sessions are attached. */
318 server_update_socket(void)
321 static int last
= -1;
326 RB_FOREACH(s
, sessions
, &sessions
) {
327 if (s
->attached
!= 0) {
336 if (stat(socket_path
, &sb
) != 0)
338 mode
= sb
.st_mode
& ACCESSPERMS
;
347 mode
&= ~(S_IXUSR
|S_IXGRP
|S_IXOTH
);
348 chmod(socket_path
, mode
);
352 /* Callback for server socket. */
354 server_accept(int fd
, short events
, __unused
void *data
)
356 struct sockaddr_storage sa
;
357 socklen_t slen
= sizeof sa
;
360 server_add_accept(0);
361 if (!(events
& EV_READ
))
364 newfd
= accept(fd
, (struct sockaddr
*) &sa
, &slen
);
366 if (errno
== EAGAIN
|| errno
== EINTR
|| errno
== ECONNABORTED
)
368 if (errno
== ENFILE
|| errno
== EMFILE
) {
369 /* Delete and don't try again for 1 second. */
370 server_add_accept(1);
373 fatal("accept failed");
379 server_client_create(newfd
);
383 * Add accept event. If timeout is nonzero, add as a timeout instead of a read
384 * event - used to backoff when running out of file descriptors.
387 server_add_accept(int timeout
)
389 struct timeval tv
= { timeout
, 0 };
394 if (event_initialized(&server_ev_accept
))
395 event_del(&server_ev_accept
);
398 event_set(&server_ev_accept
, server_fd
, EV_READ
, server_accept
,
400 event_add(&server_ev_accept
, NULL
);
402 event_set(&server_ev_accept
, server_fd
, EV_TIMEOUT
,
403 server_accept
, NULL
);
404 event_add(&server_ev_accept
, &tv
);
408 /* Signal handler. */
410 server_signal(int sig
)
414 log_debug("%s: %s", __func__
, strsignal(sig
));
422 server_child_signal();
425 event_del(&server_ev_accept
);
426 fd
= server_create_socket(server_client_flags
, NULL
);
430 server_update_socket();
432 server_add_accept(0);
435 proc_toggle_log(server_proc
);
440 /* Handle SIGCHLD. */
442 server_child_signal(void)
448 switch (pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
|WUNTRACED
)) {
452 fatal("waitpid failed");
456 if (WIFSTOPPED(status
))
457 server_child_stopped(pid
, status
);
458 else if (WIFEXITED(status
) || WIFSIGNALED(status
))
459 server_child_exited(pid
, status
);
463 /* Handle exited children. */
465 server_child_exited(pid_t pid
, int status
)
467 struct window
*w
, *w1
;
468 struct window_pane
*wp
;
470 RB_FOREACH_SAFE(w
, windows
, &windows
, w1
) {
471 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
472 if (wp
->pid
== pid
) {
474 wp
->flags
|= PANE_STATUSREADY
;
476 log_debug("%%%u exited", wp
->id
);
477 wp
->flags
|= PANE_EXITED
;
479 if (window_pane_destroy_ready(wp
))
480 server_destroy_pane(wp
, 1);
485 job_check_died(pid
, status
);
488 /* Handle stopped children. */
490 server_child_stopped(pid_t pid
, int status
)
493 struct window_pane
*wp
;
495 if (WSTOPSIG(status
) == SIGTTIN
|| WSTOPSIG(status
) == SIGTTOU
)
498 RB_FOREACH(w
, windows
, &windows
) {
499 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
500 if (wp
->pid
== pid
) {
501 if (killpg(pid
, SIGCONT
) != 0)
506 job_check_died(pid
, status
);
509 /* Add to message log. */
511 server_add_message(const char *fmt
, ...)
513 struct message_entry
*msg
, *msg1
;
519 xvasprintf(&s
, fmt
, ap
);
522 log_debug("message: %s", s
);
524 msg
= xcalloc(1, sizeof *msg
);
525 gettimeofday(&msg
->msg_time
, NULL
);
526 msg
->msg_num
= message_next
++;
528 TAILQ_INSERT_TAIL(&message_log
, msg
, entry
);
530 limit
= options_get_number(global_options
, "message-limit");
531 TAILQ_FOREACH_SAFE(msg
, &message_log
, entry
, msg1
) {
532 if (msg
->msg_num
+ limit
>= message_next
)
535 TAILQ_REMOVE(&message_log
, msg
, entry
);