1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * VNTSD main 29 * 30 * VNTSD takes the following options: 31 * -i <device instance> 32 * VCC device instance to use, e.g. virtual-console-concentrator@0. 33 * Required option. 34 * -p <ip address> 35 * IP address VNTSD listens to. 36 * -d 37 * Do not daemonize. This is only available in a DEBUG build. 38 * -t timeout for inactivity 0 = indefinite 39 * -A enable Authorization checking. Mutually exclusive with -p. 40 */ 41 42 #include <stdio.h> 43 #include <stdio_ext.h> 44 #include <sys/types.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 #include <sys/socket.h> 49 #include <arpa/inet.h> 50 #include <time.h> 51 #include <netinet/in.h> 52 #include <thread.h> 53 #include <signal.h> 54 #include <fcntl.h> 55 #include <ctype.h> 56 #include <libintl.h> 57 #include <locale.h> 58 #include <syslog.h> 59 #include <sys/socket.h> 60 #include <netdb.h> 61 #include "vntsd.h" 62 #include "chars.h" 63 64 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 65 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't. */ 66 #endif 67 68 /* global variables */ 69 70 #ifdef DEBUG 71 int vntsddbg = 0x8; 72 #endif 73 74 #define MINUTE 60 75 76 #define VNTSD_INVALID_LISTEN_ADDR ((in_addr_t)-1) 77 78 #define LOCALHOST_IPv4 "127.0.0.1" 79 #define LOCALHOST_IPv6 "::1" 80 81 static vntsd_t *vntsdp; 82 83 84 static void vntsd_exit(void); 85 /* Signal handler for SIGINT, SIGKILL and SIGHUP */ 86 static void 87 exit_sig_handler(int sig) 88 { 89 90 D1(stderr, "t@%d exit_sig_handler%d \n", thr_self(), sig); 91 92 if (thr_self() != vntsdp->tid) { 93 /* not main thread, pass to main thread */ 94 (void) thr_kill(vntsdp->tid, sig); 95 } else { 96 exit(0); 97 } 98 } 99 100 /* 101 * Before a thread reads in client's input, it attaches to vntsd timer so that 102 * it can be waken up if a client does not access the connection for 103 * VNTSD_INPUT_TIMEOUT(10) minutes. 104 */ 105 106 /* attach a thread to timer */ 107 int 108 vntsd_attach_timer(vntsd_timeout_t *tmop) 109 { 110 int rv; 111 112 if (vntsdp->timeout == 0) { 113 return (VNTSD_SUCCESS); 114 } 115 116 (void) mutex_lock(&vntsdp->tmo_lock); 117 rv = vntsd_que_append(&vntsdp->tmoq, (void *)tmop); 118 (void) mutex_unlock(&vntsdp->tmo_lock); 119 return (rv); 120 } 121 122 /* detach a thread from timer */ 123 int 124 vntsd_detach_timer(vntsd_timeout_t *tmop) 125 { 126 int rv; 127 128 if (vntsdp->timeout == 0) { 129 return (VNTSD_SUCCESS); 130 } 131 132 (void) mutex_lock(&vntsdp->tmo_lock); 133 rv = vntsd_que_rm(&vntsdp->tmoq, (void *)tmop); 134 (void) mutex_unlock(&vntsdp->tmo_lock); 135 136 return (rv); 137 } 138 139 /* check threadd's timeout */ 140 static boolean_t 141 chk_timeout(vntsd_timeout_t *tmop) 142 { 143 tmop->minutes++; 144 145 if (tmop->minutes == vntsdp->timeout) { 146 /* wake up the thread */ 147 tmop->clientp->status |= VNTSD_CLIENT_TIMEOUT; 148 (void) thr_kill(tmop->tid, SIGALRM); 149 } 150 151 /* return false to walk the queue */ 152 return (B_FALSE); 153 } 154 155 /* reset timer */ 156 static boolean_t 157 reset_timeout(vntsd_timeout_t *tmop, thread_t tid) 158 { 159 if (tmop->tid == tid) { 160 tmop->minutes = 0; 161 } 162 /* return false to walk the queue */ 163 return (B_FALSE); 164 } 165 166 void 167 vntsd_reset_timer(thread_t tid) 168 { 169 if (vntsdp->timeout == 0) { 170 return; 171 } 172 173 (void) mutex_lock(&vntsdp->tmo_lock); 174 (void) vntsd_que_find(vntsdp->tmoq, (compare_func_t)reset_timeout, 175 (void*)tid); 176 (void) mutex_unlock(&vntsdp->tmo_lock); 177 } 178 179 /* 180 * When alarm goes off, wake up timeout threads. Alarm is set off every 181 * minutes. 182 */ 183 static void 184 vntsd_alarm_sig_handler(int sig) 185 { 186 static thread_t main_thread = 0; 187 188 D1(stderr, "t@%d alarm signal %d\n", thr_self(), sig); 189 if (vntsdp->timeout == 0) { 190 DERR(stderr, "t@%d alarm signal should not recv %d\n", 191 thr_self(), sig); 192 return; 193 } 194 195 196 if (main_thread == 0) { 197 /* initialize thread id */ 198 main_thread = thr_self(); 199 } else if (main_thread != thr_self()) { 200 /* get signal because thread is timeout */ 201 return; 202 } 203 204 /* in main thread */ 205 (void) mutex_lock(&vntsdp->tmo_lock); 206 207 /* wake up timeout threads */ 208 (void) vntsd_que_walk(vntsdp->tmoq, (el_func_t)chk_timeout); 209 (void) mutex_unlock(&vntsdp->tmo_lock); 210 211 /* reset alarm */ 212 (void) alarm(MINUTE); 213 } 214 215 /* got a SIGUSER1 siginal */ 216 static void 217 vntsd_sig_handler(int sig) 218 { 219 char err_msg[VNTSD_LINE_LEN]; 220 221 (void) snprintf(err_msg, sizeof (err_msg), "sig_handler() sig=%d", 222 sig); 223 224 if (sig != SIGUSR1) { 225 vntsd_log(VNTSD_STATUS_SIG, err_msg); 226 } 227 } 228 229 /* vntsd exits */ 230 static void 231 vntsd_exit(void) 232 { 233 D1(stderr, "t@%d vntsd_exit\n", thr_self()); 234 235 (void) mutex_lock(&vntsdp->lock); 236 237 if (vntsdp->timeout > 0) { 238 /* cancel the timer */ 239 (void) alarm(0); 240 } 241 /* delete all groups */ 242 vntsd_free_que(&vntsdp->grouppq, (clean_func_t)vntsd_clean_group); 243 244 /* close control port */ 245 (void) close(vntsdp->ctrl_fd); 246 247 assert(vntsdp->tmoq == NULL); 248 (void) mutex_unlock(&vntsdp->lock); 249 250 /* clean up vntsdp */ 251 (void) mutex_destroy(&vntsdp->tmo_lock); 252 (void) mutex_destroy(&vntsdp->lock); 253 free(vntsdp); 254 closelog(); 255 } 256 257 /* 258 * vntsd_help() 259 * print out valid command line options 260 */ 261 static void 262 vntsd_help(void) 263 { 264 (void) fprintf(stderr, gettext("Usage: vntsd -i <VCC device instance> " 265 "[-p <listen address>] [-t <timeout in minutes>] [-A]\n")); 266 } 267 268 /* 269 * get_listen_ip_addr() 270 * check for a valid control domain ip address in format of xxx.xxx.xxx.xxx. 271 * if ip address is valid and is assigned to this host, return ip address 272 * or else return VNTSD_INVALID_LISTEN_ADDR. 273 */ 274 static in_addr_t 275 get_listen_ip_addr(char *listen_addr) 276 { 277 char host_name[MAXPATHLEN]; 278 in_addr_t addr; 279 struct addrinfo hints; 280 struct addrinfo *res, *infop; 281 int err; 282 struct sockaddr_in *sa; 283 284 if (gethostname(host_name, MAXPATHLEN) != 0) { 285 syslog(LOG_ERR, "Can not get host name!"); 286 return (VNTSD_INVALID_LISTEN_ADDR); 287 } 288 289 if ((int)(addr = inet_addr(listen_addr)) == -1) 290 /* bad IP address format */ 291 return (VNTSD_INVALID_LISTEN_ADDR); 292 293 bzero(&hints, sizeof (hints)); 294 hints.ai_family = PF_INET; 295 hints.ai_socktype = SOCK_STREAM; 296 297 err = getaddrinfo(host_name, NULL, &hints, &res); 298 if (err != 0) { 299 syslog(LOG_ERR, "getaddrinfo failed: %s", gai_strerror(err)); 300 return (VNTSD_INVALID_LISTEN_ADDR); 301 } 302 303 infop = res; 304 while (infop != NULL) { 305 /* LINTED E_BAD_PTR_CAST_ALIGN */ 306 sa = (struct sockaddr_in *)infop->ai_addr; 307 if (sa->sin_addr.s_addr == addr) { 308 /* ip address found */ 309 freeaddrinfo(res); 310 return (addr); 311 } 312 infop = infop->ai_next; 313 } 314 315 /* ip address not found */ 316 freeaddrinfo(res); 317 return (VNTSD_INVALID_LISTEN_ADDR); 318 } 319 320 #ifdef DEBUG 321 #define DEBUG_OPTIONS "d" 322 #else 323 #define DEBUG_OPTIONS "" 324 #endif 325 326 int 327 main(int argc, char ** argv) 328 { 329 char *path; 330 struct pollfd poll_drv[1]; 331 struct sigaction act; 332 struct rlimit rlim; 333 char *listen_addr = NULL; 334 pid_t pid; 335 int i; 336 int option; 337 int sz; 338 int fd; 339 int n; 340 341 /* internationalization */ 342 (void) setlocale(LC_MESSAGES, ""); 343 (void) textdomain(TEXT_DOMAIN); 344 vntsd_init_esctable_msgs(); 345 346 /* initialization */ 347 bzero(&act, sizeof (act)); 348 349 /* 350 * ensure that we can obtain sufficient file descriptors for all 351 * the accept() calls when a machine contains many domains. 352 */ 353 (void) getrlimit(RLIMIT_NOFILE, &rlim); 354 if (rlim.rlim_cur < rlim.rlim_max) 355 rlim.rlim_cur = rlim.rlim_max; 356 if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) 357 vntsd_log(VNTSD_STATUS_CONTINUE, "Unable to increase file " 358 "descriptor limit."); 359 (void) enable_extended_FILE_stdio(-1, -1); 360 361 vntsdp = calloc(1, sizeof (vntsd_t)); 362 if (vntsdp == NULL) { 363 vntsd_log(VNTSD_ERR_NO_MEM, "main:vntsdp"); 364 exit(1); 365 } 366 367 vntsdp->ctrl_fd = -1; 368 vntsdp->devinst = NULL; 369 370 (void) mutex_init(&vntsdp->lock, USYNC_THREAD|LOCK_ERRORCHECK, NULL); 371 (void) mutex_init(&vntsdp->tmo_lock, USYNC_THREAD|LOCK_ERRORCHECK, 372 NULL); 373 374 /* get CLI options */ 375 while ((option = getopt(argc, argv, "i:t:p:A"DEBUG_OPTIONS)) != EOF) { 376 switch (option) { 377 #ifdef DEBUG 378 case 'd': 379 vntsdp->options |= VNTSD_OPT_DAEMON_OFF; 380 break; 381 #endif 382 case 'i': 383 vntsdp->devinst = optarg; 384 break; 385 case 'p': 386 listen_addr = optarg; 387 break; 388 389 case 't': 390 n = sscanf(optarg, "%d", &(vntsdp->timeout)); 391 if (n != 1) { 392 vntsdp->timeout = -1; 393 } 394 break; 395 396 case 'A': 397 /* 398 * This option enables authorization checking of the 399 * user for the console(s) being accessed. As the 400 * authorization checking can be done only for a local 401 * client process, it requires that vntsd listen only 402 * on the loopback address. It means while this option 403 * is enabled, vntsd cannot listen on either INADDR_ANY 404 * or a specific ip address and thus the telnet client 405 * must also run on the local machine in order to 406 * connect to vntsd. The '-p' option if specified while 407 * this option is enabled, will be ignored and the auth 408 * checking takes precedence forcing vntsd to listen on 409 * the loopback interface. 410 */ 411 vntsdp->options |= VNTSD_OPT_AUTH_CHECK; 412 break; 413 414 default: 415 vntsd_help(); 416 exit(1); 417 } 418 } 419 420 if ((vntsdp->devinst == NULL) || (vntsdp->timeout == -1)) { 421 vntsd_help(); 422 exit(1); 423 } 424 425 if (listen_addr == NULL || strcmp(listen_addr, "localhost") == 0 || 426 strcmp(listen_addr, LOCALHOST_IPv4) == 0 || 427 strcmp(listen_addr, LOCALHOST_IPv6) == 0) { 428 /* by default listen on loopback interface */ 429 vntsdp->ip_addr.s_addr = htonl(INADDR_LOOPBACK); 430 } else if ((vntsdp->options & VNTSD_OPT_AUTH_CHECK) != 0) { 431 vntsd_log(VNTSD_STATUS_AUTH_ENABLED, 432 "Listen address ignored as authorization checking " 433 "is enabled"); 434 vntsdp->ip_addr.s_addr = htonl(INADDR_LOOPBACK); 435 } else if (strcmp(listen_addr, "any") == 0) { 436 vntsdp->ip_addr.s_addr = htonl(INADDR_ANY); 437 } else { 438 vntsdp->ip_addr.s_addr = get_listen_ip_addr(listen_addr); 439 if (vntsdp->ip_addr.s_addr == VNTSD_INVALID_LISTEN_ADDR) { 440 syslog(LOG_ERR, 441 "Invalid listen address '%s'\n", 442 listen_addr); 443 exit(2); 444 } 445 } 446 447 D3(stderr, "options = %llx, instance = %s, listen = %s\n", 448 vntsdp->options, vntsdp->devinst, 449 listen_addr ? listen_addr : "<null>"); 450 451 /* open VCC driver control port */ 452 sz = strlen(VCC_DEVICE_CTL_PATH) + strlen(vntsdp->devinst) + 1; 453 path = calloc(sz, 1); 454 if (path == NULL) { 455 vntsd_log(VNTSD_ERR_NO_MEM, "main(): alloc dev path"); 456 exit(1); 457 } 458 (void) snprintf(path, sz-1, VCC_DEVICE_CTL_PATH, vntsdp->devinst, 459 sizeof (vntsdp->devinst)); 460 vntsdp->ctrl_fd = open(path, O_RDWR); 461 462 if (vntsdp->ctrl_fd == -1) { 463 /* print error if device is not present */ 464 syslog(LOG_ERR, 465 "Error opening VCC device control port: %s", 466 path); 467 /* tell SMF no retry */ 468 exit(2); 469 } 470 471 free(path); 472 473 if ((vntsdp->options & VNTSD_OPT_DAEMON_OFF) == 0) { 474 /* daemonize it */ 475 pid = fork(); 476 if (pid < 0) { 477 perror("fork"); 478 exit(1); 479 } 480 if (pid > 0) { 481 /* parent */ 482 exit(0); 483 } 484 485 /* 486 * child process (daemon) 487 * 488 * Close all file descriptors other than 2 and the ctrl fd. 489 */ 490 (void) close(0); 491 (void) close(1); 492 for (i = 3; i < vntsdp->ctrl_fd; i++) { 493 (void) close(i); 494 } 495 closefrom(vntsdp->ctrl_fd + 1); 496 497 /* obtain a new process group */ 498 (void) setsid(); 499 fd = open("/dev/null", O_RDWR); 500 if (fd < 0) { 501 syslog(LOG_ERR, "Can not open /dev/null"); 502 exit(1); 503 } 504 /* handle standard I/O */ 505 if (dup2(fd, 0) < 0) { 506 syslog(LOG_ERR, "Failed dup2()"); 507 exit(1); 508 } 509 510 if (dup2(fd, 1) < 0) { 511 syslog(LOG_ERR, "Failed dup2()"); 512 exit(1); 513 } 514 515 /* ignore terminal signals */ 516 (void) signal(SIGTSTP, SIG_IGN); 517 (void) signal(SIGTTOU, SIG_IGN); 518 (void) signal(SIGTTIN, SIG_IGN); 519 } 520 521 522 /* set up signal handlers */ 523 524 /* exit signals */ 525 act.sa_handler = exit_sig_handler; 526 527 (void) sigemptyset(&act.sa_mask); 528 (void) sigaction(SIGINT, &act, NULL); 529 (void) sigaction(SIGTERM, &act, NULL); 530 (void) sigaction(SIGHUP, &act, NULL); 531 532 /* vntsd internal signals */ 533 act.sa_handler = vntsd_sig_handler; 534 (void) sigemptyset(&act.sa_mask); 535 (void) sigaction(SIGUSR1, &act, NULL); 536 537 538 act.sa_handler = vntsd_alarm_sig_handler; 539 (void) sigemptyset(&act.sa_mask); 540 (void) sigaction(SIGALRM, &act, NULL); 541 542 543 /* setup exit */ 544 (void) atexit(vntsd_exit); 545 546 547 548 /* initialization */ 549 openlog("vntsd", LOG_CONS, LOG_DAEMON); 550 551 552 /* set alarm */ 553 if (vntsdp->timeout > 0) { 554 (void) alarm(MINUTE); 555 } 556 557 vntsdp->tid = thr_self(); 558 559 /* get exiting consoles from vcc */ 560 vntsd_get_config(vntsdp); 561 562 for (; ; ) { 563 /* poll vcc for configuration change */ 564 bzero(poll_drv, sizeof (poll_drv)); 565 566 poll_drv[0].fd = vntsdp->ctrl_fd; 567 poll_drv[0].events = POLLIN; 568 569 if (poll(poll_drv, 1, -1) == -1) { 570 if (errno == EINTR) { 571 /* wake up because a consle was deleted */ 572 vntsd_delete_cons(vntsdp); 573 continue; 574 } 575 vntsd_log(VNTSD_ERR_VCC_POLL, 576 "vcc control poll err! aborting.."); 577 exit(1); 578 } 579 580 D1(stderr, "t@%d driver event %x\n", thr_self(), 581 poll_drv[0].revents); 582 583 vntsd_daemon_wakeup(vntsdp); 584 /* 585 * Main thread may miss a console-delete signal when it is 586 * not polling vcc. check if any console is deleted. 587 */ 588 vntsd_delete_cons(vntsdp); 589 590 } 591 592 /*NOTREACHED*/ 593 return (0); 594 } 595 596 /* export ip_addr */ 597 struct in_addr 598 vntsd_ip_addr(void) 599 { 600 return (vntsdp->ip_addr); 601 } 602 603 /* 604 * ioctl to vcc control port 605 * Supported ioctls interface are: 606 * ioctl code parameters return data 607 * VCC_NUM_CONSOLE none uint_t no consoles 608 * VCC_CONS_TBL none array of vcc_cons_t 609 * VCC_INQUIRY none vcc_response_t response 610 * VCC_CONS_INFO uint_t portno vcc_cons_t 611 * VCC_CONS_STATUS uint_t portno 612 * VCC_FORCE_CLOSE uint_t portno 613 */ 614 int 615 vntsd_vcc_ioctl(int ioctl_code, uint_t portno, void *buf) 616 { 617 D1(stderr, "t@%d vcc_ioctl@%d code=%x\n", thr_self(), portno, 618 ioctl_code); 619 620 if ((ioctl_code == (VCC_CONS_INFO)) || 621 (ioctl_code == (VCC_FORCE_CLOSE))) { 622 /* construct vcc in buf */ 623 *((uint_t *)buf) = portno; 624 } 625 626 if (ioctl(vntsdp->ctrl_fd, ioctl_code, (caddr_t)buf)) { 627 /* ioctl request error */ 628 return (VNTSD_STATUS_VCC_IO_ERR); 629 } 630 631 return (VNTSD_SUCCESS); 632 } 633 634 /* 635 * check if a vcc i/o error is caused by removal of a console. If so 636 * wake up main thread to cleanup the console. 637 */ 638 int 639 vntsd_vcc_err(vntsd_cons_t *consp) 640 { 641 vntsd_group_t *groupp; 642 643 assert(consp); 644 groupp = consp->group; 645 assert(groupp); 646 647 if (consp->status & VNTSD_CONS_DELETED) { 648 /* console was deleted */ 649 return (VNTSD_STATUS_VCC_IO_ERR); 650 } 651 652 if (vntsd_vcc_cons_alive(consp)) { 653 /* console is ok */ 654 return (VNTSD_STATUS_CONTINUE); 655 } 656 657 /* console needs to be deleted */ 658 (void) mutex_lock(&consp->lock); 659 consp->status |= VNTSD_CONS_DELETED; 660 661 /* 662 * main thread will close all clients after receiving console 663 * delete signal. 664 */ 665 (void) mutex_unlock(&consp->lock); 666 667 /* mark the group */ 668 (void) mutex_lock(&groupp->lock); 669 groupp->status |= VNTSD_GROUP_CLEAN_CONS; 670 (void) mutex_unlock(&groupp->lock); 671 672 /* signal main thread to deleted console */ 673 (void) thr_kill(vntsdp->tid, SIGUSR1); 674 675 return (VNTSD_STATUS_VCC_IO_ERR); 676 } 677