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 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * This file is a module that provides an interface to managing 31 * concurrent sessions executed in either a separate thread or a 32 * separate process. Threads are used only if the compile time flag 33 * DCS_MULTI_THREAD is set. Otherwise, a new process is forked for 34 * each session. 35 * 36 * Multiple processes are used to enable full Internationalization 37 * support. This support requires that each session is able to set 38 * its own locale for use in reporting errors to the user. Currently, 39 * this is not possible using multiple threads because the locale 40 * can not be set for an individual thread. For this reason, multiple 41 * processes are supported until proper locale support is provided 42 * for multiple threads. 43 * 44 * When Solaris supports a different locale in each thread, all 45 * code used to enable using multiple processes should be removed. 46 * To simplify this process, all references to DCS_MULTI_THREAD can 47 * be found in this file. 48 */ 49 50 #include <stdlib.h> 51 #include <stdio.h> 52 #include <unistd.h> 53 #include <string.h> 54 #include <errno.h> 55 #include <signal.h> 56 #include <syslog.h> 57 #include <locale.h> 58 #include <sys/socket.h> 59 60 #ifdef DCS_MULTI_THREAD 61 #include <thread.h> 62 #include <pthread.h> 63 #else /* DCS_MULTI_THREAD */ 64 #include <sys/types.h> 65 #include <sys/wait.h> 66 #endif /* DCS_MULTI_THREAD */ 67 68 #include "dcs.h" 69 #include "rdr_messages.h" 70 #include "rdr_param_types.h" 71 72 73 #define DCS_DEFAULT_LOCALE "C" 74 75 76 /* session allocation/deallocation functions */ 77 static int ses_alloc(void); 78 static int ses_free(void); 79 80 /* handler functions */ 81 static void *ses_handler(void *arg); 82 #ifndef DCS_MULTI_THREAD 83 static void exit_handler(int sig, siginfo_t *info, void *context); 84 #endif /* !DCS_MULTI_THREAD */ 85 86 /* session accounting functions */ 87 #ifdef DCS_MULTI_THREAD 88 static void ses_thr_exit(void); 89 #endif /* DCS_MULTI_THREAD */ 90 91 92 /* 93 * Global structure that holds all relevant information 94 * about the current session. If multiple threads are 95 * used, the thread specific data mechanism is used. This 96 * requires a data key to access the thread's private 97 * session information. 98 */ 99 #ifdef DCS_MULTI_THREAD 100 thread_key_t ses_key = THR_ONCE_KEY; 101 #else /* DCS_MULTI_THREAD */ 102 session_t *ses; 103 #endif /* DCS_MULTI_THREAD */ 104 105 106 /* 107 * Information about the current number of active sessions. 108 * If multiple threads are used, synchronization objects 109 * are required. 110 */ 111 static ulong_t sessions = 0; 112 113 #ifdef DCS_MULTI_THREAD 114 static mutex_t sessions_lock = DEFAULTMUTEX; 115 static cond_t sessions_cv = DEFAULTCV; 116 #endif /* DCS_MULTI_THREAD */ 117 118 119 /* 120 * ses_start: 121 * 122 * Start the session handler. If multiple threads are used, create a new 123 * thread that runs the ses_handler() function. If multiple processes 124 * are used, fork a new process and call ses_handler(). 125 */ 126 int 127 ses_start(int fd) 128 { 129 #ifdef DCS_MULTI_THREAD 130 131 int thr_err; 132 133 134 mutex_lock(&sessions_lock); 135 sessions++; 136 mutex_unlock(&sessions_lock); 137 138 thr_err = thr_create(NULL, 0, ses_handler, (void *)fd, 139 THR_DETACHED | THR_NEW_LWP, NULL); 140 141 return ((thr_err) ? -1 : 0); 142 143 #else /* DCS_MULTI_THREAD */ 144 145 int pid; 146 147 148 pid = fork(); 149 150 if (pid == -1) { 151 (void) rdr_close(fd); 152 return (-1); 153 } 154 155 /* 156 * Parent: 157 */ 158 if (pid) { 159 /* close the child's fd */ 160 (void) close(fd); 161 162 sessions++; 163 164 return (0); 165 } 166 167 /* 168 * Child: 169 */ 170 ses_handler((void *)fd); 171 172 /* 173 * Prevent return to parent's loop 174 */ 175 exit(0); 176 177 /* NOTREACHED */ 178 179 #endif /* DCS_MULTI_THREAD */ 180 } 181 182 183 /* 184 * ses_close: 185 * 186 * Initiate the closure of a session by sending an RDR_SES_END message 187 * to the client. It does not attempt to close the network connection. 188 */ 189 int 190 ses_close(int err_code) 191 { 192 session_t *sp; 193 cfga_params_t req_data; 194 rdr_msg_hdr_t req_hdr; 195 int snd_status; 196 static char *op_name = "session close"; 197 198 199 /* get the current session information */ 200 if ((sp = curr_ses()) == NULL) { 201 ses_close(DCS_ERROR); 202 return (-1); 203 } 204 205 /* check if already sent session end */ 206 if (sp->state == DCS_SES_END) { 207 return (0); 208 } 209 210 /* prepare header information */ 211 init_msg(&req_hdr); 212 req_hdr.message_opcode = RDR_SES_END; 213 req_hdr.data_type = RDR_REQUEST; 214 req_hdr.status = err_code; 215 216 /* no operation specific data */ 217 (void) memset(&req_data, 0, sizeof (req_data)); 218 219 PRINT_MSG_DBG(DCS_SEND, &req_hdr); 220 221 /* send the message */ 222 snd_status = rdr_snd_msg(sp->fd, &req_hdr, &req_data, DCS_SND_TIMEOUT); 223 224 if (snd_status == RDR_ABORTED) { 225 abort_handler(); 226 } 227 228 if (snd_status != RDR_OK) { 229 dcs_log_msg(LOG_ERR, DCS_OP_REPLY_ERR, op_name); 230 } 231 232 /* 233 * Setting the session state to DCS_SES_END will 234 * cause the session handler to terminate the 235 * network connection. This should happen whether 236 * or not the session end message that was just 237 * sent was received successfully. 238 */ 239 sp->state = DCS_SES_END; 240 return (0); 241 } 242 243 244 /* 245 * ses_abort: 246 * 247 * Attempt to abort an active session. If multiple threads are used, 248 * the parameter represents a thread_t identifier. If multiple 249 * processes are used, the parameter represents a pid. In either 250 * case, use this identifier to send a SIGINT signal to the approprate 251 * session. 252 */ 253 int 254 ses_abort(long ses_id) 255 { 256 DCS_DBG(DBG_SES, "killing session %d", ses_id); 257 258 #ifdef DCS_MULTI_THREAD 259 260 if (thr_kill(ses_id, SIGINT) != 0) { 261 /* 262 * If the thread cannot be found, we will assume 263 * that the session was able to exit normally. In 264 * this case, there is no error since the desired 265 * result has already been achieved. 266 */ 267 if (errno == ESRCH) { 268 return (0); 269 } 270 return (-1); 271 } 272 273 #else /* DCS_MULTI_THREAD */ 274 275 if (kill(ses_id, SIGINT) == -1) { 276 /* 277 * If the process cannot be found, we will assume 278 * that the session was able to exit normally. In 279 * this case, there is no error since the desired 280 * result has already been achieved. 281 */ 282 if (errno == ESRCH) { 283 return (0); 284 } 285 return (-1); 286 } 287 288 #endif /* DCS_MULTI_THREAD */ 289 290 return (0); 291 } 292 293 294 /* 295 * ses_abort_enable: 296 * 297 * Enter a mode where the current session can be aborted. This mode 298 * will persist until ses_abort_disable() is called. 299 * 300 * A signal handler for SIGINT must be installed prior to calling this 301 * function. If this is not the case, and multiple threads are used, 302 * the default handler for SIGINT will cause the entire process to 303 * exit, rather than just the current session. If multiple processes 304 * are used, the default handler for SIGINT will not affect the main 305 * process, but it will prevent both sides from gracefully closing 306 * the session. 307 */ 308 void 309 ses_abort_enable(void) 310 { 311 sigset_t unblock_set; 312 313 314 /* unblock SIGINT */ 315 sigemptyset(&unblock_set); 316 sigaddset(&unblock_set, SIGINT); 317 (void) sigprocmask(SIG_UNBLOCK, &unblock_set, NULL); 318 } 319 320 321 /* 322 * ses_abort_disable: 323 * 324 * Exit the mode where the current session can be aborted. This 325 * will leave the mode entered by ses_abort_enable(). 326 */ 327 void 328 ses_abort_disable(void) 329 { 330 sigset_t block_set; 331 332 333 /* block SIGINT */ 334 sigemptyset(&block_set); 335 sigaddset(&block_set, SIGINT); 336 (void) sigprocmask(SIG_BLOCK, &block_set, NULL); 337 } 338 339 340 /* 341 * ses_setlocale: 342 * 343 * Set the locale for the current session. Currently, if multiple threads 344 * are used, the 'C' locale is specified for all cases. Once there is support 345 * for setting a thread specific locale, the requested locale will be used. 346 * If multiple processes are used, an attempt is made to set the locale of 347 * the process to the locale passed in as a parameter. 348 */ 349 int 350 ses_setlocale(char *locale) 351 { 352 char *new_locale; 353 354 /* sanity check */ 355 if (locale == NULL) { 356 locale = DCS_DEFAULT_LOCALE; 357 } 358 359 #ifdef DCS_MULTI_THREAD 360 361 /* 362 * Reserved for setting the locale on a per thread 363 * basis. Currently there is no Solaris support for 364 * this, so use the default locale. 365 */ 366 new_locale = setlocale(LC_ALL, DCS_DEFAULT_LOCALE); 367 368 #else /* DCS_MULTI_THREAD */ 369 370 new_locale = setlocale(LC_ALL, locale); 371 372 #endif /* DCS_MULTI_THREAD */ 373 374 if ((new_locale == NULL) || (strcmp(new_locale, locale) != 0)) { 375 /* silently fall back to C locale */ 376 new_locale = setlocale(LC_ALL, DCS_DEFAULT_LOCALE); 377 } 378 379 DCS_DBG(DBG_SES, "using '%s' locale", new_locale); 380 381 return (0); 382 } 383 384 385 /* 386 * ses_init_signals: 387 * 388 * Initialize the set of signals to be blocked. It is assumed that the 389 * mask parameter initially contains all signals. If multiple threads 390 * are used, this is the correct behavior and the mask is not altered. 391 * If multiple processes are used, session accounting is performed in 392 * a SIGCHLD handler and so SIGCHLD must not be blocked. The action of 393 * initializing this handler is also performed in this function. 394 */ 395 /* ARGSUSED */ 396 void 397 ses_init_signals(sigset_t *mask) 398 { 399 #ifndef DCS_MULTI_THREAD 400 401 struct sigaction act; 402 403 404 /* unblock SIGCHLD */ 405 (void) sigdelset(mask, SIGCHLD); 406 407 /* 408 * Establish a handler for SIGCHLD 409 */ 410 (void) memset(&act, 0, sizeof (act)); 411 act.sa_sigaction = exit_handler; 412 act.sa_flags = SA_SIGINFO; 413 414 (void) sigaction(SIGCHLD, &act, NULL); 415 416 #endif /* !DCS_MULTI_THREAD */ 417 } 418 419 420 /* 421 * ses_sleep: 422 * 423 * Sleep for a specified amount of time, but don't prevent the 424 * session from being aborted. 425 */ 426 void 427 ses_sleep(int sec) 428 { 429 ses_abort_enable(); 430 sleep(sec); 431 ses_abort_disable(); 432 } 433 434 435 /* 436 * ses_wait: 437 * 438 * Wait for the number of active sessions to drop below the maximum 439 * allowed number of active sessions. If multiple threads are used, 440 * the thread waits on a condition variable until a child thread 441 * signals that it is going to exit. If multiple processes are used, 442 * the process waits until at least one child process exits. 443 */ 444 static void 445 ses_wait(void) 446 { 447 #ifdef DCS_MULTI_THREAD 448 449 mutex_lock(&sessions_lock); 450 451 while (sessions >= max_sessions) { 452 cond_wait(&sessions_cv, &sessions_lock); 453 } 454 455 mutex_unlock(&sessions_lock); 456 457 #else /* DCS_MULTI_THREAD */ 458 459 if (sessions >= max_sessions) { 460 (void) wait(NULL); 461 } 462 463 #endif /* DCS_MULTI_THREAD */ 464 } 465 466 467 /* 468 * ses_poll: 469 * 470 * Poll on the file descriptors passed in as a parameter. Before polling, 471 * a check is performed to see if the number of active sessions is less 472 * than the maximum number of active sessions allowed. If the limit for 473 * active sessions is reached, the poll will be delayed until at least 474 * one session exits. 475 */ 476 int 477 ses_poll(struct pollfd fds[], nfds_t nfds, int timeout) 478 { 479 int err; 480 481 482 ses_wait(); 483 484 err = poll(fds, nfds, timeout); 485 486 return (err); 487 } 488 489 490 /* 491 * curr_ses: 492 * 493 * Return a pointer to the global session information. If multiple threads 494 * are being used, this will point to a thread specific instance of a 495 * session structure. 496 */ 497 session_t * 498 curr_ses(void) 499 { 500 #ifdef DCS_MULTI_THREAD 501 502 return (pthread_getspecific(ses_key)); 503 504 #else /* DCS_MULTI_THREAD */ 505 506 return (ses); 507 508 #endif /* DCS_MULTI_THREAD */ 509 } 510 511 512 /* 513 * curr_ses_id: 514 * 515 * Return the session identifier. This is either the thread_t identifier 516 * of the thread, or the pid of the process. 517 */ 518 long 519 curr_ses_id(void) 520 { 521 #ifdef DCS_MULTI_THREAD 522 523 return (thr_self()); 524 525 #else /* DCS_MULTI_THREAD */ 526 527 return (getpid()); 528 529 #endif /* DCS_MULTI_THREAD */ 530 } 531 532 533 /* 534 * ses_handler: 535 * 536 * Handle initialization and processing of a session. Initializes a session 537 * and enters a loop which waits for requests. When a request comes in, it 538 * is dispatched. When the session is terminated, the loop exits and the 539 * session is cleaned up. 540 */ 541 static void * 542 ses_handler(void *arg) 543 { 544 session_t *sp; 545 rdr_msg_hdr_t op_hdr; 546 cfga_params_t op_data; 547 int rcv_status; 548 sigset_t block_set; 549 struct sigaction act; 550 551 static char *dcs_state_str[] = { 552 "unknown state", 553 "DCS_CONNECTED", 554 "DCS_SES_REQ", 555 "DCS_SES_ESTBL", 556 "DCS_CONF_PENDING", 557 "DCS_CONF_DONE", 558 "DCS_SES_END" 559 }; 560 561 562 if (ses_alloc() == -1) { 563 (void) rdr_close((int)arg); 564 return ((void *)-1); 565 } 566 567 if ((sp = curr_ses()) == NULL) { 568 ses_close(DCS_ERROR); 569 return (NULL); 570 } 571 572 /* initialize session information */ 573 memset(sp, 0, sizeof (session_t)); 574 sp->state = DCS_CONNECTED; 575 sp->random_resp = lrand48(); 576 sp->fd = (int)arg; 577 sp->id = curr_ses_id(); 578 579 /* initially, block all signals and cancels */ 580 (void) sigfillset(&block_set); 581 (void) sigprocmask(SIG_BLOCK, &block_set, NULL); 582 583 /* set the abort handler for this session */ 584 (void) memset(&act, 0, sizeof (act)); 585 act.sa_handler = abort_handler; 586 (void) sigaction(SIGINT, &act, NULL); 587 588 DCS_DBG(DBG_SES, "session handler starting..."); 589 590 /* 591 * Process all requests in the session until the 592 * session is terminated 593 */ 594 for (;;) { 595 596 DCS_DBG(DBG_STATE, "session state: %s", 597 dcs_state_str[sp->state]); 598 599 if (sp->state == DCS_SES_END) { 600 break; 601 } 602 603 (void) memset(&op_hdr, 0, sizeof (op_hdr)); 604 (void) memset(&op_data, 0, sizeof (op_data)); 605 606 rcv_status = rdr_rcv_msg(sp->fd, &op_hdr, &op_data, 607 DCS_RCV_TIMEOUT); 608 609 if (rcv_status != RDR_OK) { 610 611 switch (rcv_status) { 612 613 case RDR_TIMEOUT: 614 DCS_DBG(DBG_SES, "receive timed out"); 615 break; 616 617 case RDR_DISCONNECT: 618 dcs_log_msg(LOG_NOTICE, DCS_DISCONNECT); 619 break; 620 621 case RDR_ABORTED: 622 dcs_log_msg(LOG_INFO, DCS_SES_ABORTED); 623 break; 624 625 case RDR_MSG_INVAL: 626 /* 627 * Only log invalid messages if a session has 628 * already been established. Logging invalid 629 * session request messages could flood syslog. 630 */ 631 if (sp->state != DCS_CONNECTED) { 632 dcs_log_msg(LOG_WARNING, DCS_MSG_INVAL); 633 } else { 634 DCS_DBG(DBG_SES, "received an invalid " 635 "message"); 636 } 637 638 break; 639 640 default: 641 dcs_log_msg(LOG_ERR, DCS_RECEIVE_ERR); 642 break; 643 } 644 645 /* 646 * We encountered an unrecoverable error, 647 * so exit this session handler. 648 */ 649 break; 650 651 } else { 652 /* handle the message */ 653 dcs_dispatch_message(&op_hdr, &op_data); 654 rdr_cleanup_params(op_hdr.message_opcode, &op_data); 655 } 656 } 657 658 DCS_DBG(DBG_SES, "connection closed"); 659 660 /* clean up */ 661 (void) rdr_close(sp->fd); 662 (void) ses_free(); 663 664 #ifdef DCS_MULTI_THREAD 665 ses_thr_exit(); 666 #endif /* DCS_MULTI_THREAD */ 667 668 return (0); 669 } 670 671 672 /* 673 * abort_handler: 674 * 675 * Handle a request to abort a session. This function should be installed 676 * as the signal handler for SIGINT. It sends a message to the client 677 * indicating that the session was aborted, and that the operation failed 678 * as a result. The session then terminates, and the thread or process 679 * handling the session exits. 680 */ 681 void 682 abort_handler(void) 683 { 684 session_t *sp; 685 rdr_msg_hdr_t op_hdr; 686 cfga_params_t op_data; 687 688 689 /* get the current session information */ 690 if ((sp = curr_ses()) == NULL) { 691 ses_close(DCS_ERROR); 692 #ifdef DCS_MULTI_THREAD 693 ses_thr_exit(); 694 thr_exit(0); 695 #else /* DCS_MULTI_THREAD */ 696 exit(0); 697 #endif /* DCS_MULTI_THREAD */ 698 } 699 700 DCS_DBG(DBG_MSG, "abort_handler()"); 701 702 /* prepare header information */ 703 init_msg(&op_hdr); 704 op_hdr.message_opcode = sp->curr_msg.hdr->message_opcode; 705 op_hdr.data_type = RDR_REPLY; 706 op_hdr.status = DCS_SES_ABORTED; 707 708 /* no operation specific data */ 709 (void) memset(&op_data, 0, sizeof (op_data)); 710 711 PRINT_MSG_DBG(DCS_SEND, &op_hdr); 712 713 (void) rdr_snd_msg(sp->fd, &op_hdr, &op_data, DCS_SND_TIMEOUT); 714 715 DCS_DBG(DBG_INFO, "abort_handler: connection closed"); 716 717 /* clean up */ 718 rdr_cleanup_params(op_hdr.message_opcode, sp->curr_msg.params); 719 (void) rdr_close(sp->fd); 720 (void) ses_free(); 721 722 dcs_log_msg(LOG_INFO, DCS_SES_ABORTED); 723 724 #ifdef DCS_MULTI_THREAD 725 ses_thr_exit(); 726 thr_exit(0); 727 #else /* DCS_MULTI_THREAD */ 728 exit(0); 729 #endif /* DCS_MULTI_THREAD */ 730 } 731 732 733 #ifndef DCS_MULTI_THREAD 734 735 /* 736 * exit_handler: 737 * 738 * If multiple processes are used, this function is used to record 739 * the fact that a child process has exited. In order to make sure 740 * that all zombie processes are released, a waitpid() is performed 741 * for the child that has exited. 742 */ 743 /* ARGSUSED */ 744 static void 745 exit_handler(int sig, siginfo_t *info, void *context) 746 { 747 sessions--; 748 749 if (info != NULL) { 750 (void) waitpid(info->si_pid, NULL, 0); 751 } 752 } 753 754 #endif /* !DCS_MULTI_THREAD */ 755 756 757 /* 758 * ses_alloc: 759 * 760 * Allocate the memory required for the global session structure. 761 * If multiple threads are used, create a thread specific data 762 * key. This will only occur the first time that this function 763 * gets called. 764 */ 765 static int 766 ses_alloc(void) 767 { 768 session_t *sp; 769 770 #ifdef DCS_MULTI_THREAD 771 772 int thr_err; 773 774 thr_err = thr_keycreate_once(&ses_key, NULL); 775 if (thr_err) 776 return (-1); 777 778 #endif /* DCS_MULTI_THREAD */ 779 780 DCS_DBG(DBG_SES, "allocating session memory"); 781 782 sp = (session_t *)malloc(sizeof (session_t)); 783 784 if (!sp) { 785 dcs_log_msg(LOG_ERR, DCS_INT_ERR, "malloc", strerror(errno)); 786 return (-1); 787 } 788 789 #ifdef DCS_MULTI_THREAD 790 791 thr_err = thr_setspecific(ses_key, sp); 792 793 return ((thr_err) ? -1 : 0); 794 795 #else /* DCS_MULTI_THREAD */ 796 797 /* make the data global */ 798 ses = sp; 799 800 return (0); 801 802 #endif /* DCS_MULTI_THREAD */ 803 } 804 805 806 /* 807 * ses_free: 808 * 809 * Deallocate the memory associated with the global session structure. 810 */ 811 static int 812 ses_free(void) 813 { 814 session_t *sp; 815 816 817 DCS_DBG(DBG_SES, "freeing session memory"); 818 819 if ((sp = curr_ses()) == NULL) { 820 ses_close(DCS_ERROR); 821 return (-1); 822 } 823 824 if (sp) { 825 (void) free((void *)sp); 826 } 827 828 return (0); 829 } 830 831 832 #ifdef DCS_MULTI_THREAD 833 834 /* 835 * ses_thr_exit: 836 * 837 * If multiple threads are used, this function is used to record the 838 * fact that a child thread has exited. In addition, the condition 839 * variable is signaled so that the main thread can wakeup and begin 840 * accepting connections again. 841 */ 842 static void 843 ses_thr_exit() 844 { 845 mutex_lock(&sessions_lock); 846 847 sessions--; 848 849 cond_signal(&sessions_cv); 850 851 mutex_unlock(&sessions_lock); 852 } 853 854 #endif /* DCS_MULTI_THREAD */ 855