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>
41 * Main server functions.
45 struct clients clients
;
46 struct clients dead_clients
;
50 struct event server_ev_accept
;
51 struct event server_ev_second
;
53 struct paste_stack global_buffers
;
55 int server_create_socket(void);
56 void server_loop(void);
57 int server_should_shutdown(void);
58 void server_send_shutdown(void);
59 void server_clean_dead(void);
60 void server_accept_callback(int, short, void *);
61 void server_signal_callback(int, short, void *);
62 void server_child_signal(void);
63 void server_child_exited(pid_t
, int);
64 void server_child_stopped(pid_t
, int);
65 void server_second_callback(int, short, void *);
66 void server_lock_server(void);
67 void server_lock_sessions(void);
69 /* Create server socket. */
71 server_create_socket(void)
73 struct sockaddr_un sa
;
78 memset(&sa
, 0, sizeof sa
);
79 sa
.sun_family
= AF_UNIX
;
80 size
= strlcpy(sa
.sun_path
, socket_path
, sizeof sa
.sun_path
);
81 if (size
>= sizeof sa
.sun_path
) {
83 fatal("socket failed");
87 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1)
88 fatal("socket failed");
90 mask
= umask(S_IXUSR
|S_IXGRP
|S_IRWXO
);
91 if (bind(fd
, (struct sockaddr
*) &sa
, SUN_LEN(&sa
)) == -1)
95 if (listen(fd
, 16) == -1)
96 fatal("listen failed");
99 server_update_socket();
104 /* Fork new server. */
106 server_start(int lockfd
, char *lockfile
)
112 /* The first client is special and gets a socketpair; create it. */
113 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, pair
) != 0)
114 fatal("socketpair failed");
118 fatal("fork failed");
128 * Must daemonise before loading configuration as the PID changes so
129 * $TMUX would be wrong for sessions created in the config file.
131 if (daemon(1, 0) != 0)
132 fatal("daemon failed");
134 /* event_init() was called in our parent, need to reinit. */
135 if (event_reinit(ev_base
) != 0)
136 fatal("event_reinit failed");
140 log_debug("server started, pid %ld", (long) getpid());
142 ARRAY_INIT(&windows
);
143 RB_INIT(&all_window_panes
);
144 ARRAY_INIT(&clients
);
145 ARRAY_INIT(&dead_clients
);
147 RB_INIT(&dead_sessions
);
148 TAILQ_INIT(&session_groups
);
149 ARRAY_INIT(&global_buffers
);
150 mode_key_init_trees();
154 start_time
= time(NULL
);
155 log_debug("socket path %s", socket_path
);
156 #ifdef HAVE_SETPROCTITLE
157 setproctitle("server (%s)", socket_path
);
160 server_fd
= server_create_socket();
161 server_client_create(pair
[1]);
167 cfg_cmd_q
= cmdq_new(NULL
);
168 cfg_cmd_q
->emptyfn
= cfg_default_done
;
171 ARRAY_INIT(&cfg_causes
);
172 cfg_client
= ARRAY_FIRST(&clients
);
173 if (cfg_client
!= NULL
)
174 cfg_client
->references
++;
176 if (access(TMUX_CONF
, R_OK
) == 0) {
177 if (load_cfg(TMUX_CONF
, cfg_cmd_q
, &cause
) == -1) {
178 xasprintf(&cause
, "%s: %s", TMUX_CONF
, cause
);
179 ARRAY_ADD(&cfg_causes
, cause
);
181 } else if (errno
!= ENOENT
) {
182 xasprintf(&cause
, "%s: %s", TMUX_CONF
, strerror(errno
));
183 ARRAY_ADD(&cfg_causes
, cause
);
185 if (cfg_file
!= NULL
) {
186 if (load_cfg(cfg_file
, cfg_cmd_q
, &cause
) == -1) {
187 xasprintf(&cause
, "%s: %s", cfg_file
, cause
);
188 ARRAY_ADD(&cfg_causes
, cause
);
191 cmdq_continue(cfg_cmd_q
);
193 server_add_accept(0);
195 memset(&tv
, 0, sizeof tv
);
197 evtimer_set(&server_ev_second
, server_second_callback
, NULL
);
198 evtimer_add(&server_ev_second
, &tv
);
200 set_signals(server_signal_callback
);
205 /* Main server loop. */
209 while (!server_should_shutdown()) {
210 event_loop(EVLOOP_ONCE
);
212 server_window_loop();
213 server_client_loop();
215 key_bindings_clean();
220 /* Check if the server should be shutting down (no more clients or sessions). */
222 server_should_shutdown(void)
226 if (!options_get_number(&global_options
, "exit-unattached")) {
227 if (!RB_EMPTY(&sessions
))
230 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
231 if (ARRAY_ITEM(&clients
, i
) != NULL
)
237 /* Shutdown the server by killing all clients and windows. */
239 server_send_shutdown(void)
242 struct session
*s
, *next_s
;
245 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
246 c
= ARRAY_ITEM(&clients
, i
);
248 if (c
->flags
& (CLIENT_BAD
|CLIENT_SUSPENDED
))
249 server_client_lost(c
);
251 server_write_client(c
, MSG_SHUTDOWN
, NULL
, 0);
256 s
= RB_MIN(sessions
, &sessions
);
258 next_s
= RB_NEXT(sessions
, &sessions
, s
);
264 /* Free dead, unreferenced clients and sessions. */
266 server_clean_dead(void)
268 struct session
*s
, *next_s
;
272 s
= RB_MIN(sessions
, &dead_sessions
);
274 next_s
= RB_NEXT(sessions
, &dead_sessions
, s
);
275 if (s
->references
== 0) {
276 RB_REMOVE(sessions
, &dead_sessions
, s
);
283 for (i
= 0; i
< ARRAY_LENGTH(&dead_clients
); i
++) {
284 c
= ARRAY_ITEM(&dead_clients
, i
);
285 if (c
== NULL
|| c
->references
!= 0)
287 ARRAY_SET(&dead_clients
, i
, NULL
);
292 /* Update socket execute permissions based on whether sessions are attached. */
294 server_update_socket(void)
297 static int last
= -1;
302 RB_FOREACH(s
, sessions
, &sessions
) {
303 if (!(s
->flags
& SESSION_UNATTACHED
)) {
312 if (stat(socket_path
, &sb
) != 0)
323 mode
&= ~(S_IXUSR
|S_IXGRP
|S_IXOTH
);
324 chmod(socket_path
, mode
);
328 /* Callback for server socket. */
330 server_accept_callback(int fd
, short events
, unused
void *data
)
332 struct sockaddr_storage sa
;
333 socklen_t slen
= sizeof sa
;
336 server_add_accept(0);
337 if (!(events
& EV_READ
))
340 newfd
= accept(fd
, (struct sockaddr
*) &sa
, &slen
);
342 if (errno
== EAGAIN
|| errno
== EINTR
|| errno
== ECONNABORTED
)
344 if (errno
== ENFILE
|| errno
== EMFILE
) {
345 /* Delete and don't try again for 1 second. */
346 server_add_accept(1);
349 fatal("accept failed");
351 if (server_shutdown
) {
355 server_client_create(newfd
);
359 * Add accept event. If timeout is nonzero, add as a timeout instead of a read
360 * event - used to backoff when running out of file descriptors.
363 server_add_accept(int timeout
)
365 struct timeval tv
= { timeout
, 0 };
367 if (event_initialized(&server_ev_accept
))
368 event_del(&server_ev_accept
);
371 event_set(&server_ev_accept
,
372 server_fd
, EV_READ
, server_accept_callback
, NULL
);
373 event_add(&server_ev_accept
, NULL
);
375 event_set(&server_ev_accept
,
376 server_fd
, EV_TIMEOUT
, server_accept_callback
, NULL
);
377 event_add(&server_ev_accept
, &tv
);
381 /* Signal handler. */
383 server_signal_callback(int sig
, unused
short events
, unused
void *data
)
388 server_send_shutdown();
391 server_child_signal();
394 event_del(&server_ev_accept
);
396 server_fd
= server_create_socket();
397 server_add_accept(0);
402 /* Handle SIGCHLD. */
404 server_child_signal(void)
410 switch (pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
|WUNTRACED
)) {
414 fatal("waitpid failed");
418 if (WIFSTOPPED(status
))
419 server_child_stopped(pid
, status
);
420 else if (WIFEXITED(status
) || WIFSIGNALED(status
))
421 server_child_exited(pid
, status
);
425 /* Handle exited children. */
427 server_child_exited(pid_t pid
, int status
)
430 struct window_pane
*wp
;
434 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
435 if ((w
= ARRAY_ITEM(&windows
, i
)) == NULL
)
437 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
438 if (wp
->pid
== pid
) {
439 server_destroy_pane(wp
);
445 LIST_FOREACH(job
, &all_jobs
, lentry
) {
446 if (pid
== job
->pid
) {
447 job_died(job
, status
); /* might free job */
453 /* Handle stopped children. */
455 server_child_stopped(pid_t pid
, int status
)
458 struct window_pane
*wp
;
461 if (WSTOPSIG(status
) == SIGTTIN
|| WSTOPSIG(status
) == SIGTTOU
)
464 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
465 if ((w
= ARRAY_ITEM(&windows
, i
)) == NULL
)
467 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
468 if (wp
->pid
== pid
) {
469 if (killpg(pid
, SIGCONT
) != 0)
476 /* Handle once-per-second timer events. */
478 server_second_callback(unused
int fd
, unused
short events
, unused
void *arg
)
481 struct window_pane
*wp
;
485 if (options_get_number(&global_s_options
, "lock-server"))
486 server_lock_server();
488 server_lock_sessions();
490 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
491 w
= ARRAY_ITEM(&windows
, i
);
495 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
496 if (wp
->mode
!= NULL
&& wp
->mode
->timer
!= NULL
)
501 server_client_status_timer();
503 evtimer_del(&server_ev_second
);
504 memset(&tv
, 0, sizeof tv
);
506 evtimer_add(&server_ev_second
, &tv
);
509 /* Lock the server if ALL sessions have hit the time limit. */
511 server_lock_server(void)
518 RB_FOREACH(s
, sessions
, &sessions
) {
519 if (s
->flags
& SESSION_UNATTACHED
)
521 timeout
= options_get_number(&s
->options
, "lock-after-time");
522 if (timeout
<= 0 || t
<= s
->activity_time
.tv_sec
+ timeout
)
523 return; /* not timed out */
530 /* Lock any sessions which have timed out. */
532 server_lock_sessions(void)
539 RB_FOREACH(s
, sessions
, &sessions
) {
540 if (s
->flags
& SESSION_UNATTACHED
)
542 timeout
= options_get_number(&s
->options
, "lock-after-time");
543 if (timeout
> 0 && t
> s
->activity_time
.tv_sec
+ timeout
) {
544 server_lock_session(s
);