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");
100 server_update_socket();
105 /* Fork new server. */
107 server_start(int lockfd
, char *lockfile
)
113 /* The first client is special and gets a socketpair; create it. */
114 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, pair
) != 0)
115 fatal("socketpair failed");
119 fatal("fork failed");
129 * Must daemonise before loading configuration as the PID changes so
130 * $TMUX would be wrong for sessions created in the config file.
132 if (daemon(1, 0) != 0)
133 fatal("daemon failed");
135 /* event_init() was called in our parent, need to reinit. */
136 if (event_reinit(ev_base
) != 0)
137 fatal("event_reinit failed");
141 log_debug("server started, pid %ld", (long) getpid());
143 ARRAY_INIT(&windows
);
144 RB_INIT(&all_window_panes
);
145 ARRAY_INIT(&clients
);
146 ARRAY_INIT(&dead_clients
);
148 RB_INIT(&dead_sessions
);
149 TAILQ_INIT(&session_groups
);
150 ARRAY_INIT(&global_buffers
);
151 mode_key_init_trees();
155 start_time
= time(NULL
);
156 log_debug("socket path %s", socket_path
);
157 setproctitle("server (%s)", socket_path
);
159 server_fd
= server_create_socket();
160 server_client_create(pair
[1]);
166 cfg_cmd_q
= cmdq_new(NULL
);
167 cfg_cmd_q
->emptyfn
= cfg_default_done
;
170 ARRAY_INIT(&cfg_causes
);
172 if (access(SYSTEM_CFG
, R_OK
) == 0) {
173 if (load_cfg(SYSTEM_CFG
, cfg_cmd_q
, &cause
) == -1) {
174 xasprintf(&cause
, "%s: %s", SYSTEM_CFG
, cause
);
175 ARRAY_ADD(&cfg_causes
, cause
);
177 } else if (errno
!= ENOENT
) {
178 xasprintf(&cause
, "%s: %s", SYSTEM_CFG
, strerror(errno
));
179 ARRAY_ADD(&cfg_causes
, cause
);
181 if (cfg_file
!= NULL
) {
182 if (load_cfg(cfg_file
, cfg_cmd_q
, &cause
) == -1) {
183 xasprintf(&cause
, "%s: %s", cfg_file
, cause
);
184 ARRAY_ADD(&cfg_causes
, cause
);
187 cmdq_continue(cfg_cmd_q
);
189 server_add_accept(0);
191 memset(&tv
, 0, sizeof tv
);
193 evtimer_set(&server_ev_second
, server_second_callback
, NULL
);
194 evtimer_add(&server_ev_second
, &tv
);
196 set_signals(server_signal_callback
);
201 /* Main server loop. */
205 while (!server_should_shutdown()) {
206 event_loop(EVLOOP_ONCE
);
208 server_window_loop();
209 server_client_loop();
211 key_bindings_clean();
216 /* Check if the server should be shutting down (no more clients or sessions). */
218 server_should_shutdown(void)
222 if (!options_get_number(&global_options
, "exit-unattached")) {
223 if (!RB_EMPTY(&sessions
))
226 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
227 if (ARRAY_ITEM(&clients
, i
) != NULL
)
233 /* Shutdown the server by killing all clients and windows. */
235 server_send_shutdown(void)
238 struct session
*s
, *next_s
;
241 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
242 c
= ARRAY_ITEM(&clients
, i
);
244 if (c
->flags
& (CLIENT_BAD
|CLIENT_SUSPENDED
))
245 server_client_lost(c
);
247 server_write_client(c
, MSG_SHUTDOWN
, NULL
, 0);
252 s
= RB_MIN(sessions
, &sessions
);
254 next_s
= RB_NEXT(sessions
, &sessions
, s
);
260 /* Free dead, unreferenced clients and sessions. */
262 server_clean_dead(void)
264 struct session
*s
, *next_s
;
268 s
= RB_MIN(sessions
, &dead_sessions
);
270 next_s
= RB_NEXT(sessions
, &dead_sessions
, s
);
271 if (s
->references
== 0) {
272 RB_REMOVE(sessions
, &dead_sessions
, s
);
279 for (i
= 0; i
< ARRAY_LENGTH(&dead_clients
); i
++) {
280 c
= ARRAY_ITEM(&dead_clients
, i
);
281 if (c
== NULL
|| c
->references
!= 0)
283 ARRAY_SET(&dead_clients
, i
, NULL
);
288 /* Update socket execute permissions based on whether sessions are attached. */
290 server_update_socket(void)
293 static int last
= -1;
298 RB_FOREACH(s
, sessions
, &sessions
) {
299 if (!(s
->flags
& SESSION_UNATTACHED
)) {
308 if (stat(socket_path
, &sb
) != 0)
319 mode
&= ~(S_IXUSR
|S_IXGRP
|S_IXOTH
);
320 chmod(socket_path
, mode
);
324 /* Callback for server socket. */
326 server_accept_callback(int fd
, short events
, unused
void *data
)
328 struct sockaddr_storage sa
;
329 socklen_t slen
= sizeof sa
;
332 server_add_accept(0);
333 if (!(events
& EV_READ
))
336 newfd
= accept(fd
, (struct sockaddr
*) &sa
, &slen
);
338 if (errno
== EAGAIN
|| errno
== EINTR
|| errno
== ECONNABORTED
)
340 if (errno
== ENFILE
|| errno
== EMFILE
) {
341 /* Delete and don't try again for 1 second. */
342 server_add_accept(1);
345 fatal("accept failed");
347 if (server_shutdown
) {
351 server_client_create(newfd
);
355 * Add accept event. If timeout is nonzero, add as a timeout instead of a read
356 * event - used to backoff when running out of file descriptors.
359 server_add_accept(int timeout
)
361 struct timeval tv
= { timeout
, 0 };
363 if (event_initialized(&server_ev_accept
))
364 event_del(&server_ev_accept
);
367 event_set(&server_ev_accept
,
368 server_fd
, EV_READ
, server_accept_callback
, NULL
);
369 event_add(&server_ev_accept
, NULL
);
371 event_set(&server_ev_accept
,
372 server_fd
, EV_TIMEOUT
, server_accept_callback
, NULL
);
373 event_add(&server_ev_accept
, &tv
);
377 /* Signal handler. */
379 server_signal_callback(int sig
, unused
short events
, unused
void *data
)
384 server_send_shutdown();
387 server_child_signal();
390 event_del(&server_ev_accept
);
392 server_fd
= server_create_socket();
393 server_add_accept(0);
398 /* Handle SIGCHLD. */
400 server_child_signal(void)
406 switch (pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
|WUNTRACED
)) {
410 fatal("waitpid failed");
414 if (WIFSTOPPED(status
))
415 server_child_stopped(pid
, status
);
416 else if (WIFEXITED(status
) || WIFSIGNALED(status
))
417 server_child_exited(pid
, status
);
421 /* Handle exited children. */
423 server_child_exited(pid_t pid
, int status
)
426 struct window_pane
*wp
;
430 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
431 if ((w
= ARRAY_ITEM(&windows
, i
)) == NULL
)
433 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
434 if (wp
->pid
== pid
) {
435 server_destroy_pane(wp
);
441 LIST_FOREACH(job
, &all_jobs
, lentry
) {
442 if (pid
== job
->pid
) {
443 job_died(job
, status
); /* might free job */
449 /* Handle stopped children. */
451 server_child_stopped(pid_t pid
, int status
)
454 struct window_pane
*wp
;
457 if (WSTOPSIG(status
) == SIGTTIN
|| WSTOPSIG(status
) == SIGTTOU
)
460 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
461 if ((w
= ARRAY_ITEM(&windows
, i
)) == NULL
)
463 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
464 if (wp
->pid
== pid
) {
465 if (killpg(pid
, SIGCONT
) != 0)
472 /* Handle once-per-second timer events. */
474 server_second_callback(unused
int fd
, unused
short events
, unused
void *arg
)
477 struct window_pane
*wp
;
481 if (options_get_number(&global_s_options
, "lock-server"))
482 server_lock_server();
484 server_lock_sessions();
486 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
487 w
= ARRAY_ITEM(&windows
, i
);
491 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
492 if (wp
->mode
!= NULL
&& wp
->mode
->timer
!= NULL
)
497 server_client_status_timer();
499 evtimer_del(&server_ev_second
);
500 memset(&tv
, 0, sizeof tv
);
502 evtimer_add(&server_ev_second
, &tv
);
505 /* Lock the server if ALL sessions have hit the time limit. */
507 server_lock_server(void)
514 RB_FOREACH(s
, sessions
, &sessions
) {
515 if (s
->flags
& SESSION_UNATTACHED
)
517 timeout
= options_get_number(&s
->options
, "lock-after-time");
518 if (timeout
<= 0 || t
<= s
->activity_time
.tv_sec
+ timeout
)
519 return; /* not timed out */
526 /* Lock any sessions which have timed out. */
528 server_lock_sessions(void)
535 RB_FOREACH(s
, sessions
, &sessions
) {
536 if (s
->flags
& SESSION_UNATTACHED
)
538 timeout
= options_get_number(&s
->options
, "lock-after-time");
539 if (timeout
> 0 && t
> s
->activity_time
.tv_sec
+ timeout
) {
540 server_lock_session(s
);