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