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_sigterm
;
53 struct event server_ev_sigusr1
;
54 struct event server_ev_sigchld
;
55 struct event server_ev_second
;
57 int server_create_socket(void);
58 void server_loop(void);
59 int server_should_shutdown(void);
60 void server_send_shutdown(void);
61 void server_clean_dead(void);
62 void server_accept_callback(int, short, void *);
63 void server_signal_callback(int, short, void *);
64 void server_child_signal(void);
65 void server_child_exited(pid_t
, int);
66 void server_child_stopped(pid_t
, int);
67 void server_second_callback(int, short, void *);
68 void server_lock_server(void);
69 void server_lock_sessions(void);
71 /* Create server socket. */
73 server_create_socket(void)
75 struct sockaddr_un sa
;
80 memset(&sa
, 0, sizeof sa
);
81 sa
.sun_family
= AF_UNIX
;
82 size
= strlcpy(sa
.sun_path
, socket_path
, sizeof sa
.sun_path
);
83 if (size
>= sizeof sa
.sun_path
) {
85 fatal("socket failed");
89 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1)
90 fatal("socket failed");
92 mask
= umask(S_IXUSR
|S_IRWXG
|S_IRWXO
);
93 if (bind(fd
, (struct sockaddr
*) &sa
, SUN_LEN(&sa
)) == -1)
97 if (listen(fd
, 16) == -1)
98 fatal("listen failed");
100 if ((mode
= fcntl(fd
, F_GETFL
)) == -1)
101 fatal("fcntl failed");
102 if (fcntl(fd
, F_SETFL
, mode
|O_NONBLOCK
) == -1)
103 fatal("fcntl failed");
104 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
) == -1)
105 fatal("fcntl failed");
107 server_update_socket();
112 /* Fork new server. */
114 server_start(char *path
)
118 char *cause
, rpathbuf
[MAXPATHLEN
];
121 /* The first client is special and gets a socketpair; create it. */
122 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, pair
) != 0)
123 fatal("socketpair failed");
127 fatal("fork failed");
137 * Must daemonise before loading configuration as the PID changes so
138 * $TMUX would be wrong for sessions created in the config file.
140 if (daemon(1, 0) != 0)
141 fatal("daemon failed");
144 log_debug("server started, pid %ld", (long) getpid());
146 ARRAY_INIT(&windows
);
147 ARRAY_INIT(&clients
);
148 ARRAY_INIT(&dead_clients
);
149 ARRAY_INIT(&sessions
);
150 ARRAY_INIT(&dead_sessions
);
151 TAILQ_INIT(&session_groups
);
152 mode_key_init_trees();
156 start_time
= time(NULL
);
159 if (realpath(socket_path
, rpathbuf
) == NULL
)
160 strlcpy(rpathbuf
, socket_path
, sizeof rpathbuf
);
161 log_debug("socket path %s", socket_path
);
162 setproctitle("server (%s)", rpathbuf
);
166 server_fd
= server_create_socket();
167 server_client_create(pair
[1]);
169 if (access(SYSTEM_CFG
, R_OK
) == 0) {
170 if (load_cfg(SYSTEM_CFG
, NULL
, &cause
) != 0)
172 } else if (errno
!= ENOENT
) {
173 xasprintf(&cause
, "%s: %s", strerror(errno
), SYSTEM_CFG
);
176 if (cfg_file
!= NULL
&& load_cfg(cfg_file
, NULL
, &cause
) != 0)
179 event_set(&server_ev_accept
,
180 server_fd
, EV_READ
|EV_PERSIST
, server_accept_callback
, NULL
);
181 event_add(&server_ev_accept
, NULL
);
183 memset(&tv
, 0, sizeof tv
);
185 evtimer_set(&server_ev_second
, server_second_callback
, NULL
);
186 evtimer_add(&server_ev_second
, &tv
);
193 /* Write the error and shutdown the server. */
194 c
= ARRAY_FIRST(&clients
);
196 server_write_error(c
, cause
);
197 server_write_client(c
, MSG_EXIT
, NULL
, 0);
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 windows). */
224 server_should_shutdown(void)
228 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
229 if (ARRAY_ITEM(&sessions
, i
) != NULL
)
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)
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 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
259 if ((s
= ARRAY_ITEM(&sessions
, i
)) != NULL
)
264 /* Free dead, unreferenced clients and sessions. */
266 server_clean_dead(void)
272 for (i
= 0; i
< ARRAY_LENGTH(&dead_sessions
); i
++) {
273 s
= ARRAY_ITEM(&dead_sessions
, i
);
274 if (s
== NULL
|| s
->references
!= 0)
276 ARRAY_SET(&dead_sessions
, i
, NULL
);
280 for (i
= 0; i
< ARRAY_LENGTH(&dead_clients
); i
++) {
281 c
= ARRAY_ITEM(&dead_clients
, i
);
282 if (c
== NULL
|| c
->references
!= 0)
284 ARRAY_SET(&dead_clients
, i
, NULL
);
289 /* Update socket execute permissions based on whether sessions are attached. */
291 server_update_socket(void)
295 static int last
= -1;
299 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
300 s
= ARRAY_ITEM(&sessions
, i
);
301 if (s
!= NULL
&& !(s
->flags
& SESSION_UNATTACHED
)) {
310 chmod(socket_path
, S_IRWXU
);
312 chmod(socket_path
, S_IRUSR
|S_IWUSR
);
316 /* Callback for server socket. */
319 server_accept_callback(int fd
, short events
, unused
void *data
)
321 struct sockaddr_storage sa
;
322 socklen_t slen
= sizeof sa
;
325 if (!(events
& EV_READ
))
328 newfd
= accept(fd
, (struct sockaddr
*) &sa
, &slen
);
330 if (errno
== EAGAIN
|| errno
== EINTR
|| errno
== ECONNABORTED
)
332 fatal("accept failed");
334 if (server_shutdown
) {
338 server_client_create(newfd
);
341 /* Set up server signal handling. */
343 server_signal_set(void)
345 struct sigaction sigact
;
347 memset(&sigact
, 0, sizeof sigact
);
348 sigemptyset(&sigact
.sa_mask
);
349 sigact
.sa_flags
= SA_RESTART
;
350 sigact
.sa_handler
= SIG_IGN
;
351 if (sigaction(SIGINT
, &sigact
, NULL
) != 0)
352 fatal("sigaction failed");
353 if (sigaction(SIGPIPE
, &sigact
, NULL
) != 0)
354 fatal("sigaction failed");
355 if (sigaction(SIGUSR2
, &sigact
, NULL
) != 0)
356 fatal("sigaction failed");
357 if (sigaction(SIGTSTP
, &sigact
, NULL
) != 0)
358 fatal("sigaction failed");
360 signal_set(&server_ev_sigchld
, SIGCHLD
, server_signal_callback
, NULL
);
361 signal_add(&server_ev_sigchld
, NULL
);
362 signal_set(&server_ev_sigterm
, SIGTERM
, server_signal_callback
, NULL
);
363 signal_add(&server_ev_sigterm
, NULL
);
364 signal_set(&server_ev_sigusr1
, SIGUSR1
, server_signal_callback
, NULL
);
365 signal_add(&server_ev_sigusr1
, NULL
);
368 /* Destroy server signal events. */
370 server_signal_clear(void)
372 struct sigaction sigact
;
374 memset(&sigact
, 0, sizeof sigact
);
375 sigemptyset(&sigact
.sa_mask
);
376 sigact
.sa_flags
= SA_RESTART
;
377 sigact
.sa_handler
= SIG_DFL
;
378 if (sigaction(SIGINT
, &sigact
, NULL
) != 0)
379 fatal("sigaction failed");
380 if (sigaction(SIGPIPE
, &sigact
, NULL
) != 0)
381 fatal("sigaction failed");
382 if (sigaction(SIGUSR2
, &sigact
, NULL
) != 0)
383 fatal("sigaction failed");
384 if (sigaction(SIGTSTP
, &sigact
, NULL
) != 0)
385 fatal("sigaction failed");
387 signal_del(&server_ev_sigchld
);
388 signal_del(&server_ev_sigterm
);
389 signal_del(&server_ev_sigusr1
);
392 /* Signal handler. */
395 server_signal_callback(int sig
, unused
short events
, unused
void *data
)
400 server_send_shutdown();
403 server_child_signal();
406 event_del(&server_ev_accept
);
408 server_fd
= server_create_socket();
409 event_set(&server_ev_accept
, server_fd
,
410 EV_READ
|EV_PERSIST
, server_accept_callback
, NULL
);
411 event_add(&server_ev_accept
, NULL
);
416 /* Handle SIGCHLD. */
418 server_child_signal(void)
424 switch (pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
|WUNTRACED
)) {
428 fatal("waitpid failed");
432 if (WIFSTOPPED(status
))
433 server_child_stopped(pid
, status
);
434 else if (WIFEXITED(status
) || WIFSIGNALED(status
))
435 server_child_exited(pid
, status
);
439 /* Handle exited children. */
441 server_child_exited(pid_t pid
, int status
)
444 struct window_pane
*wp
;
448 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
449 if ((w
= ARRAY_ITEM(&windows
, i
)) == NULL
)
451 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
452 if (wp
->pid
== pid
) {
453 server_destroy_pane(wp
);
459 SLIST_FOREACH(job
, &all_jobs
, lentry
) {
460 if (pid
== job
->pid
) {
461 job_died(job
, status
); /* might free job */
467 /* Handle stopped children. */
469 server_child_stopped(pid_t pid
, int status
)
472 struct window_pane
*wp
;
475 if (WSTOPSIG(status
) == SIGTTIN
|| WSTOPSIG(status
) == SIGTTOU
)
478 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
479 if ((w
= ARRAY_ITEM(&windows
, i
)) == NULL
)
481 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
482 if (wp
->pid
== pid
) {
483 if (killpg(pid
, SIGCONT
) != 0)
490 /* Handle once-per-second timer events. */
493 server_second_callback(unused
int fd
, unused
short events
, unused
void *arg
)
496 struct window_pane
*wp
;
500 if (options_get_number(&global_s_options
, "lock-server"))
501 server_lock_server();
503 server_lock_sessions();
505 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
506 w
= ARRAY_ITEM(&windows
, i
);
510 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
511 if (wp
->mode
!= NULL
&& wp
->mode
->timer
!= NULL
)
516 server_client_status_timer();
518 evtimer_del(&server_ev_second
);
519 memset(&tv
, 0, sizeof tv
);
521 evtimer_add(&server_ev_second
, &tv
);
524 /* Lock the server if ALL sessions have hit the time limit. */
526 server_lock_server(void)
534 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
535 if ((s
= ARRAY_ITEM(&sessions
, i
)) == NULL
)
538 if (s
->flags
& SESSION_UNATTACHED
) {
539 if (gettimeofday(&s
->activity_time
, NULL
) != 0)
540 fatal("gettimeofday failed");
544 timeout
= options_get_number(&s
->options
, "lock-after-time");
545 if (timeout
<= 0 || t
<= s
->activity_time
.tv_sec
+ timeout
)
546 return; /* not timed out */
553 /* Lock any sessions which have timed out. */
555 server_lock_sessions(void)
563 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
564 if ((s
= ARRAY_ITEM(&sessions
, i
)) == NULL
)
567 if (s
->flags
& SESSION_UNATTACHED
) {
568 if (gettimeofday(&s
->activity_time
, NULL
) != 0)
569 fatal("gettimeofday failed");
573 timeout
= options_get_number(&s
->options
, "lock-after-time");
574 if (timeout
> 0 && t
> s
->activity_time
.tv_sec
+ timeout
) {
575 server_lock_session(s
);