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>
42 * Main server functions.
46 struct clients clients
;
47 struct clients dead_clients
;
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. */
72 server_create_socket(void)
74 struct sockaddr_un sa
;
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
) {
84 fatal("socket failed");
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)
96 if (listen(fd
, 16) == -1)
97 fatal("listen failed");
99 if ((mode
= fcntl(fd
, F_GETFL
)) == -1)
100 fatal("fcntl failed");
101 if (fcntl(fd
, F_SETFL
, mode
|O_NONBLOCK
) == -1)
102 fatal("fcntl failed");
104 server_update_socket();
109 /* Fork new server. */
113 struct window_pane
*wp
;
119 /* The first client is special and gets a socketpair; create it. */
120 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, pair
) != 0)
121 fatal("socketpair failed");
125 fatal("fork failed");
135 * Must daemonise before loading configuration as the PID changes so
136 * $TMUX would be wrong for sessions created in the config file.
138 if (daemon(1, 0) != 0)
139 fatal("daemon failed");
141 /* event_init() was called in our parent, need to reinit. */
142 if (event_reinit(ev_base
) != 0)
143 fatal("event_reinit failed");
147 log_debug("server started, pid %ld", (long) getpid());
149 ARRAY_INIT(&windows
);
150 ARRAY_INIT(&clients
);
151 ARRAY_INIT(&dead_clients
);
153 RB_INIT(&dead_sessions
);
154 TAILQ_INIT(&session_groups
);
155 ARRAY_INIT(&global_buffers
);
156 mode_key_init_trees();
160 start_time
= time(NULL
);
161 log_debug("socket path %s", socket_path
);
162 setproctitle("server (%s)", socket_path
);
164 server_fd
= server_create_socket();
165 server_client_create(pair
[1]);
167 if (access(SYSTEM_CFG
, R_OK
) == 0)
168 load_cfg(SYSTEM_CFG
, NULL
, &cfg_causes
);
169 else if (errno
!= ENOENT
) {
171 &cfg_causes
, "%s: %s", strerror(errno
), SYSTEM_CFG
);
173 if (cfg_file
!= NULL
)
174 load_cfg(cfg_file
, NULL
, &cfg_causes
);
177 * If there is a session already, put the current window and pane into
180 if (!RB_EMPTY(&sessions
) && !ARRAY_EMPTY(&cfg_causes
)) {
181 wp
= RB_MIN(sessions
, &sessions
)->curw
->window
->active
;
182 window_pane_set_mode(wp
, &window_copy_mode
);
183 window_copy_init_for_output(wp
);
184 for (i
= 0; i
< ARRAY_LENGTH(&cfg_causes
); i
++) {
185 cause
= ARRAY_ITEM(&cfg_causes
, i
);
186 window_copy_add(wp
, "%s", cause
);
189 ARRAY_FREE(&cfg_causes
);
193 event_set(&server_ev_accept
,
194 server_fd
, EV_READ
|EV_PERSIST
, server_accept_callback
, NULL
);
195 event_add(&server_ev_accept
, NULL
);
197 memset(&tv
, 0, sizeof tv
);
199 evtimer_set(&server_ev_second
, server_second_callback
, NULL
);
200 evtimer_add(&server_ev_second
, &tv
);
202 set_signals(server_signal_callback
);
207 /* Main server loop. */
211 while (!server_should_shutdown()) {
212 event_loop(EVLOOP_ONCE
);
214 server_window_loop();
215 server_client_loop();
217 key_bindings_clean();
222 /* Check if the server should be shutting down (no more clients or sessions). */
224 server_should_shutdown(void)
228 if (!options_get_number(&global_options
, "exit-unattached")) {
229 if (!RB_EMPTY(&sessions
))
232 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
233 if (ARRAY_ITEM(&clients
, i
) != NULL
)
239 /* Shutdown the server by killing all clients and windows. */
241 server_send_shutdown(void)
244 struct session
*s
, *next_s
;
247 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
248 c
= ARRAY_ITEM(&clients
, i
);
250 if (c
->flags
& (CLIENT_BAD
|CLIENT_SUSPENDED
))
251 server_client_lost(c
);
253 server_write_client(c
, MSG_SHUTDOWN
, NULL
, 0);
258 s
= RB_MIN(sessions
, &sessions
);
260 next_s
= RB_NEXT(sessions
, &sessions
, s
);
266 /* Free dead, unreferenced clients and sessions. */
268 server_clean_dead(void)
270 struct session
*s
, *next_s
;
274 s
= RB_MIN(sessions
, &dead_sessions
);
276 next_s
= RB_NEXT(sessions
, &dead_sessions
, s
);
277 if (s
->references
== 0) {
278 RB_REMOVE(sessions
, &dead_sessions
, s
);
285 for (i
= 0; i
< ARRAY_LENGTH(&dead_clients
); i
++) {
286 c
= ARRAY_ITEM(&dead_clients
, i
);
287 if (c
== NULL
|| c
->references
!= 0)
289 ARRAY_SET(&dead_clients
, i
, NULL
);
294 /* Update socket execute permissions based on whether sessions are attached. */
296 server_update_socket(void)
299 static int last
= -1;
304 RB_FOREACH(s
, sessions
, &sessions
) {
305 if (!(s
->flags
& SESSION_UNATTACHED
)) {
314 if (stat(socket_path
, &sb
) != 0)
325 mode
&= ~(S_IXUSR
|S_IXGRP
|S_IXOTH
);
326 chmod(socket_path
, mode
);
330 /* Callback for server socket. */
333 server_accept_callback(int fd
, short events
, unused
void *data
)
335 struct sockaddr_storage sa
;
336 socklen_t slen
= sizeof sa
;
339 if (!(events
& EV_READ
))
342 newfd
= accept(fd
, (struct sockaddr
*) &sa
, &slen
);
344 if (errno
== EAGAIN
|| errno
== EINTR
|| errno
== ECONNABORTED
)
346 fatal("accept failed");
348 if (server_shutdown
) {
352 server_client_create(newfd
);
355 /* Signal handler. */
358 server_signal_callback(int sig
, unused
short events
, unused
void *data
)
363 server_send_shutdown();
366 server_child_signal();
369 event_del(&server_ev_accept
);
371 server_fd
= server_create_socket();
372 event_set(&server_ev_accept
, server_fd
,
373 EV_READ
|EV_PERSIST
, server_accept_callback
, NULL
);
374 event_add(&server_ev_accept
, NULL
);
379 /* Handle SIGCHLD. */
381 server_child_signal(void)
387 switch (pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
|WUNTRACED
)) {
391 fatal("waitpid failed");
395 if (WIFSTOPPED(status
))
396 server_child_stopped(pid
, status
);
397 else if (WIFEXITED(status
) || WIFSIGNALED(status
))
398 server_child_exited(pid
, status
);
402 /* Handle exited children. */
404 server_child_exited(pid_t pid
, int status
)
407 struct window_pane
*wp
;
411 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
412 if ((w
= ARRAY_ITEM(&windows
, i
)) == NULL
)
414 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
415 if (wp
->pid
== pid
) {
416 server_destroy_pane(wp
);
422 SLIST_FOREACH(job
, &all_jobs
, lentry
) {
423 if (pid
== job
->pid
) {
424 job_died(job
, status
); /* might free job */
430 /* Handle stopped children. */
432 server_child_stopped(pid_t pid
, int status
)
435 struct window_pane
*wp
;
438 if (WSTOPSIG(status
) == SIGTTIN
|| WSTOPSIG(status
) == SIGTTOU
)
441 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
442 if ((w
= ARRAY_ITEM(&windows
, i
)) == NULL
)
444 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
445 if (wp
->pid
== pid
) {
446 if (killpg(pid
, SIGCONT
) != 0)
453 /* Handle once-per-second timer events. */
456 server_second_callback(unused
int fd
, unused
short events
, unused
void *arg
)
459 struct window_pane
*wp
;
463 if (options_get_number(&global_s_options
, "lock-server"))
464 server_lock_server();
466 server_lock_sessions();
468 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
469 w
= ARRAY_ITEM(&windows
, i
);
473 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
474 if (wp
->mode
!= NULL
&& wp
->mode
->timer
!= NULL
)
479 server_client_status_timer();
481 evtimer_del(&server_ev_second
);
482 memset(&tv
, 0, sizeof tv
);
484 evtimer_add(&server_ev_second
, &tv
);
487 /* Lock the server if ALL sessions have hit the time limit. */
489 server_lock_server(void)
496 RB_FOREACH(s
, sessions
, &sessions
) {
497 if (s
->flags
& SESSION_UNATTACHED
)
499 timeout
= options_get_number(&s
->options
, "lock-after-time");
500 if (timeout
<= 0 || t
<= s
->activity_time
.tv_sec
+ timeout
)
501 return; /* not timed out */
508 /* Lock any sessions which have timed out. */
510 server_lock_sessions(void)
517 RB_FOREACH(s
, sessions
, &sessions
) {
518 if (s
->flags
& SESSION_UNATTACHED
)
520 timeout
= options_get_number(&s
->options
, "lock-after-time");
521 if (timeout
> 0 && t
> s
->activity_time
.tv_sec
+ timeout
) {
522 server_lock_session(s
);