1 /* $OpenBSD: server.c,v 1.114 2014/05/14 06:21:19 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> 5 * 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. 9 * 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. 17 */ 18 19 #include <sys/types.h> 20 #include <sys/ioctl.h> 21 #include <sys/socket.h> 22 #include <sys/stat.h> 23 #include <sys/un.h> 24 #include <sys/wait.h> 25 26 #include <errno.h> 27 #include <event.h> 28 #include <fcntl.h> 29 #include <paths.h> 30 #include <signal.h> 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include <syslog.h> 35 #include <termios.h> 36 #include <time.h> 37 #include <unistd.h> 38 39 #include "tmux.h" 40 41 /* 42 * Main server functions. 43 */ 44 45 /* Client list. */ 46 struct clients clients; 47 struct clients dead_clients; 48 49 int server_fd; 50 int server_shutdown; 51 struct event server_ev_accept; 52 struct event server_ev_second; 53 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); 67 68 /* Create server socket. */ 69 int 70 server_create_socket(void) 71 { 72 struct sockaddr_un sa; 73 size_t size; 74 mode_t mask; 75 int fd; 76 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) { 81 errno = ENAMETOOLONG; 82 fatal("socket failed"); 83 } 84 unlink(sa.sun_path); 85 86 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) 87 fatal("socket failed"); 88 89 mask = umask(S_IXUSR|S_IXGRP|S_IRWXO); 90 if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) 91 fatal("bind failed"); 92 umask(mask); 93 94 if (listen(fd, 16) == -1) 95 fatal("listen failed"); 96 setblocking(fd, 0); 97 98 server_update_socket(); 99 100 return (fd); 101 } 102 103 /* Fork new server. */ 104 int 105 server_start(int lockfd, char *lockfile) 106 { 107 int pair[2]; 108 struct timeval tv; 109 char *cause; 110 111 /* The first client is special and gets a socketpair; create it. */ 112 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0) 113 fatal("socketpair failed"); 114 115 switch (fork()) { 116 case -1: 117 fatal("fork failed"); 118 case 0: 119 break; 120 default: 121 close(pair[1]); 122 return (pair[0]); 123 } 124 close(pair[0]); 125 126 /* 127 * Must daemonise before loading configuration as the PID changes so 128 * $TMUX would be wrong for sessions created in the config file. 129 */ 130 if (daemon(1, 0) != 0) 131 fatal("daemon failed"); 132 133 /* event_init() was called in our parent, need to reinit. */ 134 if (event_reinit(ev_base) != 0) 135 fatal("event_reinit failed"); 136 clear_signals(0); 137 138 logfile("server"); 139 log_debug("server started, pid %ld", (long) getpid()); 140 141 ARRAY_INIT(&windows); 142 RB_INIT(&all_window_panes); 143 ARRAY_INIT(&clients); 144 ARRAY_INIT(&dead_clients); 145 RB_INIT(&sessions); 146 RB_INIT(&dead_sessions); 147 TAILQ_INIT(&session_groups); 148 mode_key_init_trees(); 149 key_bindings_init(); 150 utf8_build(); 151 152 start_time = time(NULL); 153 log_debug("socket path %s", socket_path); 154 setproctitle("server (%s)", socket_path); 155 156 server_fd = server_create_socket(); 157 server_client_create(pair[1]); 158 159 unlink(lockfile); 160 free(lockfile); 161 close(lockfd); 162 163 cfg_cmd_q = cmdq_new(NULL); 164 cfg_cmd_q->emptyfn = cfg_default_done; 165 cfg_finished = 0; 166 cfg_references = 1; 167 ARRAY_INIT(&cfg_causes); 168 cfg_client = ARRAY_FIRST(&clients); 169 if (cfg_client != NULL) 170 cfg_client->references++; 171 172 if (access(TMUX_CONF, R_OK) == 0) { 173 if (load_cfg(TMUX_CONF, cfg_cmd_q, &cause) == -1) { 174 xasprintf(&cause, "%s: %s", TMUX_CONF, cause); 175 ARRAY_ADD(&cfg_causes, cause); 176 } 177 } else if (errno != ENOENT) { 178 xasprintf(&cause, "%s: %s", TMUX_CONF, strerror(errno)); 179 ARRAY_ADD(&cfg_causes, cause); 180 } 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); 185 } 186 } 187 cmdq_continue(cfg_cmd_q); 188 189 server_add_accept(0); 190 191 memset(&tv, 0, sizeof tv); 192 tv.tv_sec = 1; 193 evtimer_set(&server_ev_second, server_second_callback, NULL); 194 evtimer_add(&server_ev_second, &tv); 195 196 set_signals(server_signal_callback); 197 server_loop(); 198 exit(0); 199 } 200 201 /* Main server loop. */ 202 void 203 server_loop(void) 204 { 205 while (!server_should_shutdown()) { 206 event_loop(EVLOOP_ONCE); 207 208 server_window_loop(); 209 server_client_loop(); 210 211 server_clean_dead(); 212 } 213 } 214 215 /* Check if the server should exit (no more clients or sessions). */ 216 int 217 server_should_shutdown(void) 218 { 219 u_int i; 220 221 if (!options_get_number(&global_options, "exit-unattached")) { 222 if (!RB_EMPTY(&sessions)) 223 return (0); 224 } 225 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 226 if (ARRAY_ITEM(&clients, i) != NULL) 227 return (0); 228 } 229 return (1); 230 } 231 232 /* Shutdown the server by killing all clients and windows. */ 233 void 234 server_send_shutdown(void) 235 { 236 struct client *c; 237 struct session *s, *next_s; 238 u_int i; 239 240 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 241 c = ARRAY_ITEM(&clients, i); 242 if (c != NULL) { 243 if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED)) 244 server_client_lost(c); 245 else 246 server_write_client(c, MSG_SHUTDOWN, NULL, 0); 247 c->session = NULL; 248 } 249 } 250 251 s = RB_MIN(sessions, &sessions); 252 while (s != NULL) { 253 next_s = RB_NEXT(sessions, &sessions, s); 254 session_destroy(s); 255 s = next_s; 256 } 257 } 258 259 /* Free dead, unreferenced clients and sessions. */ 260 void 261 server_clean_dead(void) 262 { 263 struct session *s, *next_s; 264 struct client *c; 265 u_int i; 266 267 s = RB_MIN(sessions, &dead_sessions); 268 while (s != NULL) { 269 next_s = RB_NEXT(sessions, &dead_sessions, s); 270 if (s->references == 0) { 271 RB_REMOVE(sessions, &dead_sessions, s); 272 free(s->name); 273 free(s); 274 } 275 s = next_s; 276 } 277 278 for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) { 279 c = ARRAY_ITEM(&dead_clients, i); 280 if (c == NULL || c->references != 0) 281 continue; 282 ARRAY_SET(&dead_clients, i, NULL); 283 free(c); 284 } 285 } 286 287 /* Update socket execute permissions based on whether sessions are attached. */ 288 void 289 server_update_socket(void) 290 { 291 struct session *s; 292 static int last = -1; 293 int n, mode; 294 struct stat sb; 295 296 n = 0; 297 RB_FOREACH(s, sessions, &sessions) { 298 if (!(s->flags & SESSION_UNATTACHED)) { 299 n++; 300 break; 301 } 302 } 303 304 if (n != last) { 305 last = n; 306 307 if (stat(socket_path, &sb) != 0) 308 return; 309 mode = sb.st_mode; 310 if (n != 0) { 311 if (mode & S_IRUSR) 312 mode |= S_IXUSR; 313 if (mode & S_IRGRP) 314 mode |= S_IXGRP; 315 if (mode & S_IROTH) 316 mode |= S_IXOTH; 317 } else 318 mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH); 319 chmod(socket_path, mode); 320 } 321 } 322 323 /* Callback for server socket. */ 324 void 325 server_accept_callback(int fd, short events, unused void *data) 326 { 327 struct sockaddr_storage sa; 328 socklen_t slen = sizeof sa; 329 int newfd; 330 331 server_add_accept(0); 332 if (!(events & EV_READ)) 333 return; 334 335 newfd = accept(fd, (struct sockaddr *) &sa, &slen); 336 if (newfd == -1) { 337 if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED) 338 return; 339 if (errno == ENFILE || errno == EMFILE) { 340 /* Delete and don't try again for 1 second. */ 341 server_add_accept(1); 342 return; 343 } 344 fatal("accept failed"); 345 } 346 if (server_shutdown) { 347 close(newfd); 348 return; 349 } 350 server_client_create(newfd); 351 } 352 353 /* 354 * Add accept event. If timeout is nonzero, add as a timeout instead of a read 355 * event - used to backoff when running out of file descriptors. 356 */ 357 void 358 server_add_accept(int timeout) 359 { 360 struct timeval tv = { timeout, 0 }; 361 362 if (event_initialized(&server_ev_accept)) 363 event_del(&server_ev_accept); 364 365 if (timeout == 0) { 366 event_set(&server_ev_accept, 367 server_fd, EV_READ, server_accept_callback, NULL); 368 event_add(&server_ev_accept, NULL); 369 } else { 370 event_set(&server_ev_accept, 371 server_fd, EV_TIMEOUT, server_accept_callback, NULL); 372 event_add(&server_ev_accept, &tv); 373 } 374 } 375 376 /* Signal handler. */ 377 void 378 server_signal_callback(int sig, unused short events, unused void *data) 379 { 380 switch (sig) { 381 case SIGTERM: 382 server_shutdown = 1; 383 server_send_shutdown(); 384 break; 385 case SIGCHLD: 386 server_child_signal(); 387 break; 388 case SIGUSR1: 389 event_del(&server_ev_accept); 390 close(server_fd); 391 server_fd = server_create_socket(); 392 server_add_accept(0); 393 break; 394 } 395 } 396 397 /* Handle SIGCHLD. */ 398 void 399 server_child_signal(void) 400 { 401 int status; 402 pid_t pid; 403 404 for (;;) { 405 switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) { 406 case -1: 407 if (errno == ECHILD) 408 return; 409 fatal("waitpid failed"); 410 case 0: 411 return; 412 } 413 if (WIFSTOPPED(status)) 414 server_child_stopped(pid, status); 415 else if (WIFEXITED(status) || WIFSIGNALED(status)) 416 server_child_exited(pid, status); 417 } 418 } 419 420 /* Handle exited children. */ 421 void 422 server_child_exited(pid_t pid, int status) 423 { 424 struct window *w; 425 struct window_pane *wp; 426 struct job *job; 427 u_int i; 428 429 for (i = 0; i < ARRAY_LENGTH(&windows); i++) { 430 if ((w = ARRAY_ITEM(&windows, i)) == NULL) 431 continue; 432 TAILQ_FOREACH(wp, &w->panes, entry) { 433 if (wp->pid == pid) { 434 server_destroy_pane(wp); 435 break; 436 } 437 } 438 } 439 440 LIST_FOREACH(job, &all_jobs, lentry) { 441 if (pid == job->pid) { 442 job_died(job, status); /* might free job */ 443 break; 444 } 445 } 446 } 447 448 /* Handle stopped children. */ 449 void 450 server_child_stopped(pid_t pid, int status) 451 { 452 struct window *w; 453 struct window_pane *wp; 454 u_int i; 455 456 if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU) 457 return; 458 459 for (i = 0; i < ARRAY_LENGTH(&windows); i++) { 460 if ((w = ARRAY_ITEM(&windows, i)) == NULL) 461 continue; 462 TAILQ_FOREACH(wp, &w->panes, entry) { 463 if (wp->pid == pid) { 464 if (killpg(pid, SIGCONT) != 0) 465 kill(pid, SIGCONT); 466 } 467 } 468 } 469 } 470 471 /* Handle once-per-second timer events. */ 472 void 473 server_second_callback(unused int fd, unused short events, unused void *arg) 474 { 475 struct window *w; 476 struct window_pane *wp; 477 struct timeval tv; 478 u_int i; 479 480 if (options_get_number(&global_s_options, "lock-server")) 481 server_lock_server(); 482 else 483 server_lock_sessions(); 484 485 for (i = 0; i < ARRAY_LENGTH(&windows); i++) { 486 w = ARRAY_ITEM(&windows, i); 487 if (w == NULL) 488 continue; 489 490 TAILQ_FOREACH(wp, &w->panes, entry) { 491 if (wp->mode != NULL && wp->mode->timer != NULL) 492 wp->mode->timer(wp); 493 } 494 } 495 496 server_client_status_timer(); 497 498 evtimer_del(&server_ev_second); 499 memset(&tv, 0, sizeof tv); 500 tv.tv_sec = 1; 501 evtimer_add(&server_ev_second, &tv); 502 } 503 504 /* Lock the server if ALL sessions have hit the time limit. */ 505 void 506 server_lock_server(void) 507 { 508 struct session *s; 509 int timeout; 510 time_t t; 511 512 t = time(NULL); 513 RB_FOREACH(s, sessions, &sessions) { 514 if (s->flags & SESSION_UNATTACHED) 515 continue; 516 timeout = options_get_number(&s->options, "lock-after-time"); 517 if (timeout <= 0 || t <= s->activity_time.tv_sec + timeout) 518 return; /* not timed out */ 519 } 520 521 server_lock(); 522 recalculate_sizes(); 523 } 524 525 /* Lock any sessions which have timed out. */ 526 void 527 server_lock_sessions(void) 528 { 529 struct session *s; 530 int timeout; 531 time_t t; 532 533 t = time(NULL); 534 RB_FOREACH(s, sessions, &sessions) { 535 if (s->flags & SESSION_UNATTACHED) 536 continue; 537 timeout = options_get_number(&s->options, "lock-after-time"); 538 if (timeout > 0 && t > s->activity_time.tv_sec + timeout) { 539 server_lock_session(s); 540 recalculate_sizes(); 541 } 542 } 543 } 544