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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright (c) 2000-2001 by Sun Microsystems, Inc. 24 * All rights reserved. 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 #else /* DCS_MULTI_THREAD */ 63 #include <sys/types.h> 64 #include <sys/wait.h> 65 #endif /* DCS_MULTI_THREAD */ 66 67 #include "dcs.h" 68 #include "rdr_messages.h" 69 #include "rdr_param_types.h" 70 71 72 #define DCS_DEFAULT_LOCALE "C" 73 74 75 /* session allocation/deallocation functions */ 76 static int ses_alloc(void); 77 static int ses_free(void); 78 79 /* handler functions */ 80 static void *ses_handler(void *arg); 81 #ifndef DCS_MULTI_THREAD 82 static void exit_handler(int sig, siginfo_t *info, void *context); 83 #endif /* !DCS_MULTI_THREAD */ 84 85 /* session accounting functions */ 86 #ifdef DCS_MULTI_THREAD 87 static void ses_thr_exit(void); 88 #endif /* DCS_MULTI_THREAD */ 89 90 91 /* 92 * Global structure that holds all relevant information 93 * about the current session. If multiple threads are 94 * used, the thread specific data mechanism is used. This 95 * requires a data key to access the thread's private 96 * session information. 97 */ 98 #ifdef DCS_MULTI_THREAD 99 thread_key_t ses_key; 100 static int thr_key_created = 0; 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 session_t *sp; 503 int thr_err; 504 505 506 thr_err = thr_getspecific(ses_key, (void **)&sp); 507 508 return ((thr_err) ? NULL : sp); 509 510 #else /* DCS_MULTI_THREAD */ 511 512 return (ses); 513 514 #endif /* DCS_MULTI_THREAD */ 515 } 516 517 518 /* 519 * curr_ses_id: 520 * 521 * Return the session identifier. This is either the thread_t identifier 522 * of the thread, or the pid of the process. 523 */ 524 long 525 curr_ses_id(void) 526 { 527 #ifdef DCS_MULTI_THREAD 528 529 return (thr_self()); 530 531 #else /* DCS_MULTI_THREAD */ 532 533 return (getpid()); 534 535 #endif /* DCS_MULTI_THREAD */ 536 } 537 538 539 /* 540 * ses_handler: 541 * 542 * Handle initialization and processing of a session. Initializes a session 543 * and enters a loop which waits for requests. When a request comes in, it 544 * is dispatched. When the session is terminated, the loop exits and the 545 * session is cleaned up. 546 */ 547 static void * 548 ses_handler(void *arg) 549 { 550 session_t *sp; 551 rdr_msg_hdr_t op_hdr; 552 cfga_params_t op_data; 553 int rcv_status; 554 sigset_t block_set; 555 struct sigaction act; 556 557 static char *dcs_state_str[] = { 558 "unknown state", 559 "DCS_CONNECTED", 560 "DCS_SES_REQ", 561 "DCS_SES_ESTBL", 562 "DCS_CONF_PENDING", 563 "DCS_CONF_DONE", 564 "DCS_SES_END" 565 }; 566 567 568 if (ses_alloc() == -1) { 569 (void) rdr_close((int)arg); 570 return ((void *)-1); 571 } 572 573 if ((sp = curr_ses()) == NULL) { 574 ses_close(DCS_ERROR); 575 return (NULL); 576 } 577 578 /* initialize session information */ 579 memset(sp, 0, sizeof (session_t)); 580 sp->state = DCS_CONNECTED; 581 sp->random_resp = lrand48(); 582 sp->fd = (int)arg; 583 sp->id = curr_ses_id(); 584 585 /* initially, block all signals and cancels */ 586 (void) sigfillset(&block_set); 587 (void) sigprocmask(SIG_BLOCK, &block_set, NULL); 588 589 /* set the abort handler for this session */ 590 (void) memset(&act, 0, sizeof (act)); 591 act.sa_handler = abort_handler; 592 (void) sigaction(SIGINT, &act, NULL); 593 594 DCS_DBG(DBG_SES, "session handler starting..."); 595 596 /* 597 * Process all requests in the session until the 598 * session is terminated 599 */ 600 for (;;) { 601 602 DCS_DBG(DBG_STATE, "session state: %s", 603 dcs_state_str[sp->state]); 604 605 if (sp->state == DCS_SES_END) { 606 break; 607 } 608 609 (void) memset(&op_hdr, 0, sizeof (op_hdr)); 610 (void) memset(&op_data, 0, sizeof (op_data)); 611 612 rcv_status = rdr_rcv_msg(sp->fd, &op_hdr, &op_data, 613 DCS_RCV_TIMEOUT); 614 615 if (rcv_status != RDR_OK) { 616 617 switch (rcv_status) { 618 619 case RDR_TIMEOUT: 620 DCS_DBG(DBG_SES, "receive timed out"); 621 break; 622 623 case RDR_DISCONNECT: 624 dcs_log_msg(LOG_NOTICE, DCS_DISCONNECT); 625 break; 626 627 case RDR_ABORTED: 628 dcs_log_msg(LOG_INFO, DCS_SES_ABORTED); 629 break; 630 631 case RDR_MSG_INVAL: 632 /* 633 * Only log invalid messages if a session has 634 * already been established. Logging invalid 635 * session request messages could flood syslog. 636 */ 637 if (sp->state != DCS_CONNECTED) { 638 dcs_log_msg(LOG_WARNING, DCS_MSG_INVAL); 639 } else { 640 DCS_DBG(DBG_SES, "received an invalid " 641 "message"); 642 } 643 644 break; 645 646 default: 647 dcs_log_msg(LOG_ERR, DCS_RECEIVE_ERR); 648 break; 649 } 650 651 /* 652 * We encountered an unrecoverable error, 653 * so exit this session handler. 654 */ 655 break; 656 657 } else { 658 /* handle the message */ 659 dcs_dispatch_message(&op_hdr, &op_data); 660 rdr_cleanup_params(op_hdr.message_opcode, &op_data); 661 } 662 } 663 664 DCS_DBG(DBG_SES, "connection closed"); 665 666 /* clean up */ 667 (void) rdr_close(sp->fd); 668 (void) ses_free(); 669 670 #ifdef DCS_MULTI_THREAD 671 ses_thr_exit(); 672 #endif /* DCS_MULTI_THREAD */ 673 674 return (0); 675 } 676 677 678 /* 679 * abort_handler: 680 * 681 * Handle a request to abort a session. This function should be installed 682 * as the signal handler for SIGINT. It sends a message to the client 683 * indicating that the session was aborted, and that the operation failed 684 * as a result. The session then terminates, and the thread or process 685 * handling the session exits. 686 */ 687 void 688 abort_handler(void) 689 { 690 session_t *sp; 691 rdr_msg_hdr_t op_hdr; 692 cfga_params_t op_data; 693 694 695 /* get the current session information */ 696 if ((sp = curr_ses()) == NULL) { 697 ses_close(DCS_ERROR); 698 #ifdef DCS_MULTI_THREAD 699 ses_thr_exit(); 700 thr_exit(0); 701 #else /* DCS_MULTI_THREAD */ 702 exit(0); 703 #endif /* DCS_MULTI_THREAD */ 704 } 705 706 DCS_DBG(DBG_MSG, "abort_handler()"); 707 708 /* prepare header information */ 709 init_msg(&op_hdr); 710 op_hdr.message_opcode = sp->curr_msg.hdr->message_opcode; 711 op_hdr.data_type = RDR_REPLY; 712 op_hdr.status = DCS_SES_ABORTED; 713 714 /* no operation specific data */ 715 (void) memset(&op_data, 0, sizeof (op_data)); 716 717 PRINT_MSG_DBG(DCS_SEND, &op_hdr); 718 719 (void) rdr_snd_msg(sp->fd, &op_hdr, &op_data, DCS_SND_TIMEOUT); 720 721 DCS_DBG(DBG_INFO, "abort_handler: connection closed"); 722 723 /* clean up */ 724 rdr_cleanup_params(op_hdr.message_opcode, sp->curr_msg.params); 725 (void) rdr_close(sp->fd); 726 (void) ses_free(); 727 728 dcs_log_msg(LOG_INFO, DCS_SES_ABORTED); 729 730 #ifdef DCS_MULTI_THREAD 731 ses_thr_exit(); 732 thr_exit(0); 733 #else /* DCS_MULTI_THREAD */ 734 exit(0); 735 #endif /* DCS_MULTI_THREAD */ 736 } 737 738 739 #ifndef DCS_MULTI_THREAD 740 741 /* 742 * exit_handler: 743 * 744 * If multiple processes are used, this function is used to record 745 * the fact that a child process has exited. In order to make sure 746 * that all zombie processes are released, a waitpid() is performed 747 * for the child that has exited. 748 */ 749 /* ARGSUSED */ 750 static void 751 exit_handler(int sig, siginfo_t *info, void *context) 752 { 753 sessions--; 754 755 if (info != NULL) { 756 (void) waitpid(info->si_pid, NULL, 0); 757 } 758 } 759 760 #endif /* !DCS_MULTI_THREAD */ 761 762 763 /* 764 * ses_alloc: 765 * 766 * Allocate the memory required for the global session structure. 767 * If multiple threads are used, create a thread specific data 768 * key. This will only occur the first time that this function 769 * gets called. 770 */ 771 static int 772 ses_alloc(void) 773 { 774 session_t *sp; 775 776 #ifdef DCS_MULTI_THREAD 777 778 int thr_err; 779 780 781 if (!thr_key_created) { 782 thr_err = thr_keycreate(&ses_key, NULL); 783 784 if (thr_err) { 785 return (-1); 786 } 787 788 thr_key_created = 1; 789 } 790 791 #endif /* DCS_MULTI_THREAD */ 792 793 DCS_DBG(DBG_SES, "allocating session memory"); 794 795 sp = (session_t *)malloc(sizeof (session_t)); 796 797 if (!sp) { 798 dcs_log_msg(LOG_ERR, DCS_INT_ERR, "malloc", strerror(errno)); 799 return (-1); 800 } 801 802 #ifdef DCS_MULTI_THREAD 803 804 thr_err = thr_setspecific(ses_key, sp); 805 806 return ((thr_err) ? -1 : 0); 807 808 #else /* DCS_MULTI_THREAD */ 809 810 /* make the data global */ 811 ses = sp; 812 813 return (0); 814 815 #endif /* DCS_MULTI_THREAD */ 816 } 817 818 819 /* 820 * ses_free: 821 * 822 * Deallocate the memory associated with the global session structure. 823 */ 824 static int 825 ses_free(void) 826 { 827 session_t *sp; 828 829 830 DCS_DBG(DBG_SES, "freeing session memory"); 831 832 if ((sp = curr_ses()) == NULL) { 833 ses_close(DCS_ERROR); 834 return (-1); 835 } 836 837 if (sp) { 838 (void) free((void *)sp); 839 } 840 841 return (0); 842 } 843 844 845 #ifdef DCS_MULTI_THREAD 846 847 /* 848 * ses_thr_exit: 849 * 850 * If multiple threads are used, this function is used to record the 851 * fact that a child thread has exited. In addition, the condition 852 * variable is signaled so that the main thread can wakeup and begin 853 * accepting connections again. 854 */ 855 static void 856 ses_thr_exit() 857 { 858 mutex_lock(&sessions_lock); 859 860 sessions--; 861 862 cond_signal(&sessions_cv); 863 864 mutex_unlock(&sessions_lock); 865 } 866 867 #endif /* DCS_MULTI_THREAD */ 868