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 int server_create_socket(void);
55 void server_loop(void);
56 int server_should_shutdown(void);
57 void server_send_shutdown(void);
58 void server_clean_dead(void);
59 void server_accept_callback(int, short, void *);
60 void server_signal_callback(int, short, void *);
61 void server_child_signal(void);
62 void server_child_exited(pid_t
, int);
63 void server_child_stopped(pid_t
, int);
64 void server_second_callback(int, short, void *);
65 void server_lock_server(void);
66 void server_lock_sessions(void);
68 /* Create server socket. */
70 server_create_socket(void)
72 struct sockaddr_un sa
;
77 memset(&sa
, 0, sizeof sa
);
78 sa
.sun_family
= AF_UNIX
;
79 size
= strlcpy(sa
.sun_path
, socket_path
, sizeof sa
.sun_path
);
80 if (size
>= sizeof sa
.sun_path
) {
82 fatal("socket failed");
86 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1)
87 fatal("socket failed");
89 mask
= umask(S_IXUSR
|S_IXGRP
|S_IRWXO
);
90 if (bind(fd
, (struct sockaddr
*) &sa
, SUN_LEN(&sa
)) == -1)
94 if (listen(fd
, 16) == -1)
95 fatal("listen failed");
97 if ((mode
= fcntl(fd
, F_GETFL
)) == -1)
98 fatal("fcntl failed");
99 if (fcntl(fd
, F_SETFL
, mode
|O_NONBLOCK
) == -1)
100 fatal("fcntl failed");
101 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
) == -1)
102 fatal("fcntl failed");
104 server_update_socket();
109 /* Fork new server. */
111 server_start(char *path
)
113 struct window_pane
*wp
;
115 char rpathbuf
[MAXPATHLEN
], *cause
;
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
);
152 ARRAY_INIT(&sessions
);
153 ARRAY_INIT(&dead_sessions
);
154 TAILQ_INIT(&session_groups
);
155 mode_key_init_trees();
159 start_time
= time(NULL
);
162 if (realpath(socket_path
, rpathbuf
) == NULL
)
163 strlcpy(rpathbuf
, socket_path
, sizeof rpathbuf
);
164 log_debug("socket path %s", socket_path
);
165 setproctitle("server (%s)", rpathbuf
);
167 server_fd
= server_create_socket();
168 server_client_create(pair
[1]);
170 if (access(SYSTEM_CFG
, R_OK
) == 0)
171 load_cfg(SYSTEM_CFG
, NULL
, &cfg_causes
);
172 else if (errno
!= ENOENT
) {
174 &cfg_causes
, "%s: %s", strerror(errno
), SYSTEM_CFG
);
176 if (cfg_file
!= NULL
)
177 load_cfg(cfg_file
, NULL
, &cfg_causes
);
180 * If there is a session already, put the current window and pane into
183 if (!ARRAY_EMPTY(&sessions
) && !ARRAY_EMPTY(&cfg_causes
)) {
184 wp
= ARRAY_FIRST(&sessions
)->curw
->window
->active
;
185 window_pane_set_mode(wp
, &window_copy_mode
);
186 window_copy_init_for_output(wp
);
187 for (i
= 0; i
< ARRAY_LENGTH(&cfg_causes
); i
++) {
188 cause
= ARRAY_ITEM(&cfg_causes
, i
);
189 window_copy_add(wp
, "%s", cause
);
192 ARRAY_FREE(&cfg_causes
);
196 event_set(&server_ev_accept
,
197 server_fd
, EV_READ
|EV_PERSIST
, server_accept_callback
, NULL
);
198 event_add(&server_ev_accept
, NULL
);
200 memset(&tv
, 0, sizeof tv
);
202 evtimer_set(&server_ev_second
, server_second_callback
, NULL
);
203 evtimer_add(&server_ev_second
, &tv
);
205 set_signals(server_signal_callback
);
210 /* Main server loop. */
214 while (!server_should_shutdown()) {
215 event_loop(EVLOOP_ONCE
);
217 server_window_loop();
218 server_client_loop();
220 key_bindings_clean();
225 /* Check if the server should be shutting down (no more clients or windows). */
227 server_should_shutdown(void)
231 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
232 if (ARRAY_ITEM(&sessions
, i
) != NULL
)
235 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
236 if (ARRAY_ITEM(&clients
, i
) != NULL
)
242 /* Shutdown the server by killing all clients and windows. */
244 server_send_shutdown(void)
250 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
251 c
= ARRAY_ITEM(&clients
, i
);
253 if (c
->flags
& (CLIENT_BAD
|CLIENT_SUSPENDED
))
254 server_client_lost(c
);
256 server_write_client(c
, MSG_SHUTDOWN
, NULL
, 0);
261 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
262 if ((s
= ARRAY_ITEM(&sessions
, i
)) != NULL
)
267 /* Free dead, unreferenced clients and sessions. */
269 server_clean_dead(void)
275 for (i
= 0; i
< ARRAY_LENGTH(&dead_sessions
); i
++) {
276 s
= ARRAY_ITEM(&dead_sessions
, i
);
277 if (s
== NULL
|| s
->references
!= 0)
279 ARRAY_SET(&dead_sessions
, i
, NULL
);
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)
298 static int last
= -1;
302 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
303 s
= ARRAY_ITEM(&sessions
, i
);
304 if (s
!= NULL
&& !(s
->flags
& SESSION_UNATTACHED
)) {
313 chmod(socket_path
, S_IRWXU
|S_IRWXG
);
315 chmod(socket_path
, S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
319 /* Callback for server socket. */
322 server_accept_callback(int fd
, short events
, unused
void *data
)
324 struct sockaddr_storage sa
;
325 socklen_t slen
= sizeof sa
;
328 if (!(events
& EV_READ
))
331 newfd
= accept(fd
, (struct sockaddr
*) &sa
, &slen
);
333 if (errno
== EAGAIN
|| errno
== EINTR
|| errno
== ECONNABORTED
)
335 fatal("accept failed");
337 if (server_shutdown
) {
341 server_client_create(newfd
);
344 /* Signal handler. */
347 server_signal_callback(int sig
, unused
short events
, unused
void *data
)
352 server_send_shutdown();
355 server_child_signal();
358 event_del(&server_ev_accept
);
360 server_fd
= server_create_socket();
361 event_set(&server_ev_accept
, server_fd
,
362 EV_READ
|EV_PERSIST
, server_accept_callback
, NULL
);
363 event_add(&server_ev_accept
, NULL
);
368 /* Handle SIGCHLD. */
370 server_child_signal(void)
376 switch (pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
|WUNTRACED
)) {
380 fatal("waitpid failed");
384 if (WIFSTOPPED(status
))
385 server_child_stopped(pid
, status
);
386 else if (WIFEXITED(status
) || WIFSIGNALED(status
))
387 server_child_exited(pid
, status
);
391 /* Handle exited children. */
393 server_child_exited(pid_t pid
, int status
)
396 struct window_pane
*wp
;
400 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
401 if ((w
= ARRAY_ITEM(&windows
, i
)) == NULL
)
403 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
404 if (wp
->pid
== pid
) {
405 server_destroy_pane(wp
);
411 SLIST_FOREACH(job
, &all_jobs
, lentry
) {
412 if (pid
== job
->pid
) {
413 job_died(job
, status
); /* might free job */
419 /* Handle stopped children. */
421 server_child_stopped(pid_t pid
, int status
)
424 struct window_pane
*wp
;
427 if (WSTOPSIG(status
) == SIGTTIN
|| WSTOPSIG(status
) == SIGTTOU
)
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 if (killpg(pid
, SIGCONT
) != 0)
442 /* Handle once-per-second timer events. */
445 server_second_callback(unused
int fd
, unused
short events
, unused
void *arg
)
448 struct window_pane
*wp
;
452 if (options_get_number(&global_s_options
, "lock-server"))
453 server_lock_server();
455 server_lock_sessions();
457 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
458 w
= ARRAY_ITEM(&windows
, i
);
462 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
463 if (wp
->mode
!= NULL
&& wp
->mode
->timer
!= NULL
)
468 server_client_status_timer();
470 evtimer_del(&server_ev_second
);
471 memset(&tv
, 0, sizeof tv
);
473 evtimer_add(&server_ev_second
, &tv
);
476 /* Lock the server if ALL sessions have hit the time limit. */
478 server_lock_server(void)
486 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
487 if ((s
= ARRAY_ITEM(&sessions
, i
)) == NULL
)
490 if (s
->flags
& SESSION_UNATTACHED
) {
491 if (gettimeofday(&s
->activity_time
, NULL
) != 0)
492 fatal("gettimeofday failed");
496 timeout
= options_get_number(&s
->options
, "lock-after-time");
497 if (timeout
<= 0 || t
<= s
->activity_time
.tv_sec
+ timeout
)
498 return; /* not timed out */
505 /* Lock any sessions which have timed out. */
507 server_lock_sessions(void)
515 for (i
= 0; i
< ARRAY_LENGTH(&sessions
); i
++) {
516 if ((s
= ARRAY_ITEM(&sessions
, i
)) == NULL
)
519 if (s
->flags
& SESSION_UNATTACHED
) {
520 if (gettimeofday(&s
->activity_time
, NULL
) != 0)
521 fatal("gettimeofday failed");
525 timeout
= options_get_number(&s
->options
, "lock-after-time");
526 if (timeout
> 0 && t
> s
->activity_time
.tv_sec
+ timeout
) {
527 server_lock_session(s
);