8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / dcs / sparc / sun4u / dcs_ses.c
blobe70d76f996523888e1b7bb025a746af5520350fd
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
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.
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.
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.
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>
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 */
68 #include "dcs.h"
69 #include "rdr_messages.h"
70 #include "rdr_param_types.h"
73 #define DCS_DEFAULT_LOCALE "C"
76 /* session allocation/deallocation functions */
77 static int ses_alloc(void);
78 static int ses_free(void);
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 */
86 /* session accounting functions */
87 #ifdef DCS_MULTI_THREAD
88 static void ses_thr_exit(void);
89 #endif /* DCS_MULTI_THREAD */
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.
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 */
107 * Information about the current number of active sessions.
108 * If multiple threads are used, synchronization objects
109 * are required.
111 static ulong_t sessions = 0;
113 #ifdef DCS_MULTI_THREAD
114 static mutex_t sessions_lock = DEFAULTMUTEX;
115 static cond_t sessions_cv = DEFAULTCV;
116 #endif /* DCS_MULTI_THREAD */
120 * ses_start:
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().
127 ses_start(int fd)
129 #ifdef DCS_MULTI_THREAD
131 int thr_err;
134 mutex_lock(&sessions_lock);
135 sessions++;
136 mutex_unlock(&sessions_lock);
138 thr_err = thr_create(NULL, 0, ses_handler, (void *)fd,
139 THR_DETACHED | THR_NEW_LWP, NULL);
141 return ((thr_err) ? -1 : 0);
143 #else /* DCS_MULTI_THREAD */
145 int pid;
148 pid = fork();
150 if (pid == -1) {
151 (void) rdr_close(fd);
152 return (-1);
156 * Parent:
158 if (pid) {
159 /* close the child's fd */
160 (void) close(fd);
162 sessions++;
164 return (0);
168 * Child:
170 ses_handler((void *)fd);
173 * Prevent return to parent's loop
175 exit(0);
177 /* NOTREACHED */
179 #endif /* DCS_MULTI_THREAD */
184 * ses_close:
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.
190 ses_close(int err_code)
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";
199 /* get the current session information */
200 if ((sp = curr_ses()) == NULL) {
201 ses_close(DCS_ERROR);
202 return (-1);
205 /* check if already sent session end */
206 if (sp->state == DCS_SES_END) {
207 return (0);
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;
216 /* no operation specific data */
217 (void) memset(&req_data, 0, sizeof (req_data));
219 PRINT_MSG_DBG(DCS_SEND, &req_hdr);
221 /* send the message */
222 snd_status = rdr_snd_msg(sp->fd, &req_hdr, &req_data, DCS_SND_TIMEOUT);
224 if (snd_status == RDR_ABORTED) {
225 abort_handler();
228 if (snd_status != RDR_OK) {
229 dcs_log_msg(LOG_ERR, DCS_OP_REPLY_ERR, op_name);
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.
239 sp->state = DCS_SES_END;
240 return (0);
245 * ses_abort:
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.
254 ses_abort(long ses_id)
256 DCS_DBG(DBG_SES, "killing session %d", ses_id);
258 #ifdef DCS_MULTI_THREAD
260 if (thr_kill(ses_id, SIGINT) != 0) {
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.
267 if (errno == ESRCH) {
268 return (0);
270 return (-1);
273 #else /* DCS_MULTI_THREAD */
275 if (kill(ses_id, SIGINT) == -1) {
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.
282 if (errno == ESRCH) {
283 return (0);
285 return (-1);
288 #endif /* DCS_MULTI_THREAD */
290 return (0);
295 * ses_abort_enable:
297 * Enter a mode where the current session can be aborted. This mode
298 * will persist until ses_abort_disable() is called.
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.
308 void
309 ses_abort_enable(void)
311 sigset_t unblock_set;
314 /* unblock SIGINT */
315 sigemptyset(&unblock_set);
316 sigaddset(&unblock_set, SIGINT);
317 (void) sigprocmask(SIG_UNBLOCK, &unblock_set, NULL);
322 * ses_abort_disable:
324 * Exit the mode where the current session can be aborted. This
325 * will leave the mode entered by ses_abort_enable().
327 void
328 ses_abort_disable(void)
330 sigset_t block_set;
333 /* block SIGINT */
334 sigemptyset(&block_set);
335 sigaddset(&block_set, SIGINT);
336 (void) sigprocmask(SIG_BLOCK, &block_set, NULL);
341 * ses_setlocale:
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.
350 ses_setlocale(char *locale)
352 char *new_locale;
354 /* sanity check */
355 if (locale == NULL) {
356 locale = DCS_DEFAULT_LOCALE;
359 #ifdef DCS_MULTI_THREAD
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.
366 new_locale = setlocale(LC_ALL, DCS_DEFAULT_LOCALE);
368 #else /* DCS_MULTI_THREAD */
370 new_locale = setlocale(LC_ALL, locale);
372 #endif /* DCS_MULTI_THREAD */
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);
379 DCS_DBG(DBG_SES, "using '%s' locale", new_locale);
381 return (0);
386 * ses_init_signals:
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.
395 /* ARGSUSED */
396 void
397 ses_init_signals(sigset_t *mask)
399 #ifndef DCS_MULTI_THREAD
401 struct sigaction act;
404 /* unblock SIGCHLD */
405 (void) sigdelset(mask, SIGCHLD);
408 * Establish a handler for SIGCHLD
410 (void) memset(&act, 0, sizeof (act));
411 act.sa_sigaction = exit_handler;
412 act.sa_flags = SA_SIGINFO;
414 (void) sigaction(SIGCHLD, &act, NULL);
416 #endif /* !DCS_MULTI_THREAD */
421 * ses_sleep:
423 * Sleep for a specified amount of time, but don't prevent the
424 * session from being aborted.
426 void
427 ses_sleep(int sec)
429 ses_abort_enable();
430 sleep(sec);
431 ses_abort_disable();
436 * ses_wait:
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.
444 static void
445 ses_wait(void)
447 #ifdef DCS_MULTI_THREAD
449 mutex_lock(&sessions_lock);
451 while (sessions >= max_sessions) {
452 cond_wait(&sessions_cv, &sessions_lock);
455 mutex_unlock(&sessions_lock);
457 #else /* DCS_MULTI_THREAD */
459 if (sessions >= max_sessions) {
460 (void) wait(NULL);
463 #endif /* DCS_MULTI_THREAD */
468 * ses_poll:
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.
477 ses_poll(struct pollfd fds[], nfds_t nfds, int timeout)
479 int err;
482 ses_wait();
484 err = poll(fds, nfds, timeout);
486 return (err);
491 * curr_ses:
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.
497 session_t *
498 curr_ses(void)
500 #ifdef DCS_MULTI_THREAD
502 return (pthread_getspecific(ses_key));
504 #else /* DCS_MULTI_THREAD */
506 return (ses);
508 #endif /* DCS_MULTI_THREAD */
513 * curr_ses_id:
515 * Return the session identifier. This is either the thread_t identifier
516 * of the thread, or the pid of the process.
518 long
519 curr_ses_id(void)
521 #ifdef DCS_MULTI_THREAD
523 return (thr_self());
525 #else /* DCS_MULTI_THREAD */
527 return (getpid());
529 #endif /* DCS_MULTI_THREAD */
534 * ses_handler:
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.
541 static void *
542 ses_handler(void *arg)
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;
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"
562 if (ses_alloc() == -1) {
563 (void) rdr_close((int)arg);
564 return ((void *)-1);
567 if ((sp = curr_ses()) == NULL) {
568 ses_close(DCS_ERROR);
569 return (NULL);
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();
579 /* initially, block all signals and cancels */
580 (void) sigfillset(&block_set);
581 (void) sigprocmask(SIG_BLOCK, &block_set, NULL);
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);
588 DCS_DBG(DBG_SES, "session handler starting...");
591 * Process all requests in the session until the
592 * session is terminated
594 for (;;) {
596 DCS_DBG(DBG_STATE, "session state: %s",
597 dcs_state_str[sp->state]);
599 if (sp->state == DCS_SES_END) {
600 break;
603 (void) memset(&op_hdr, 0, sizeof (op_hdr));
604 (void) memset(&op_data, 0, sizeof (op_data));
606 rcv_status = rdr_rcv_msg(sp->fd, &op_hdr, &op_data,
607 DCS_RCV_TIMEOUT);
609 if (rcv_status != RDR_OK) {
611 switch (rcv_status) {
613 case RDR_TIMEOUT:
614 DCS_DBG(DBG_SES, "receive timed out");
615 break;
617 case RDR_DISCONNECT:
618 dcs_log_msg(LOG_NOTICE, DCS_DISCONNECT);
619 break;
621 case RDR_ABORTED:
622 dcs_log_msg(LOG_INFO, DCS_SES_ABORTED);
623 break;
625 case RDR_MSG_INVAL:
627 * Only log invalid messages if a session has
628 * already been established. Logging invalid
629 * session request messages could flood syslog.
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");
638 break;
640 default:
641 dcs_log_msg(LOG_ERR, DCS_RECEIVE_ERR);
642 break;
646 * We encountered an unrecoverable error,
647 * so exit this session handler.
649 break;
651 } else {
652 /* handle the message */
653 dcs_dispatch_message(&op_hdr, &op_data);
654 rdr_cleanup_params(op_hdr.message_opcode, &op_data);
658 DCS_DBG(DBG_SES, "connection closed");
660 /* clean up */
661 (void) rdr_close(sp->fd);
662 (void) ses_free();
664 #ifdef DCS_MULTI_THREAD
665 ses_thr_exit();
666 #endif /* DCS_MULTI_THREAD */
668 return (0);
673 * abort_handler:
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.
681 void
682 abort_handler(void)
684 session_t *sp;
685 rdr_msg_hdr_t op_hdr;
686 cfga_params_t op_data;
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 */
700 DCS_DBG(DBG_MSG, "abort_handler()");
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;
708 /* no operation specific data */
709 (void) memset(&op_data, 0, sizeof (op_data));
711 PRINT_MSG_DBG(DCS_SEND, &op_hdr);
713 (void) rdr_snd_msg(sp->fd, &op_hdr, &op_data, DCS_SND_TIMEOUT);
715 DCS_DBG(DBG_INFO, "abort_handler: connection closed");
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();
722 dcs_log_msg(LOG_INFO, DCS_SES_ABORTED);
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 */
733 #ifndef DCS_MULTI_THREAD
736 * exit_handler:
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.
743 /* ARGSUSED */
744 static void
745 exit_handler(int sig, siginfo_t *info, void *context)
747 sessions--;
749 if (info != NULL) {
750 (void) waitpid(info->si_pid, NULL, 0);
754 #endif /* !DCS_MULTI_THREAD */
758 * ses_alloc:
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.
765 static int
766 ses_alloc(void)
768 session_t *sp;
770 #ifdef DCS_MULTI_THREAD
772 int thr_err;
774 thr_err = thr_keycreate_once(&ses_key, NULL);
775 if (thr_err)
776 return (-1);
778 #endif /* DCS_MULTI_THREAD */
780 DCS_DBG(DBG_SES, "allocating session memory");
782 sp = (session_t *)malloc(sizeof (session_t));
784 if (!sp) {
785 dcs_log_msg(LOG_ERR, DCS_INT_ERR, "malloc", strerror(errno));
786 return (-1);
789 #ifdef DCS_MULTI_THREAD
791 thr_err = thr_setspecific(ses_key, sp);
793 return ((thr_err) ? -1 : 0);
795 #else /* DCS_MULTI_THREAD */
797 /* make the data global */
798 ses = sp;
800 return (0);
802 #endif /* DCS_MULTI_THREAD */
807 * ses_free:
809 * Deallocate the memory associated with the global session structure.
811 static int
812 ses_free(void)
814 session_t *sp;
817 DCS_DBG(DBG_SES, "freeing session memory");
819 if ((sp = curr_ses()) == NULL) {
820 ses_close(DCS_ERROR);
821 return (-1);
824 if (sp) {
825 (void) free((void *)sp);
828 return (0);
832 #ifdef DCS_MULTI_THREAD
835 * ses_thr_exit:
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.
842 static void
843 ses_thr_exit()
845 mutex_lock(&sessions_lock);
847 sessions--;
849 cond_signal(&sessions_cv);
851 mutex_unlock(&sessions_lock);
854 #endif /* DCS_MULTI_THREAD */