17836SJohn.Forte@Sun.COM /*
27836SJohn.Forte@Sun.COM * CDDL HEADER START
37836SJohn.Forte@Sun.COM *
47836SJohn.Forte@Sun.COM * The contents of this file are subject to the terms of the
57836SJohn.Forte@Sun.COM * Common Development and Distribution License (the "License").
67836SJohn.Forte@Sun.COM * You may not use this file except in compliance with the License.
77836SJohn.Forte@Sun.COM *
87836SJohn.Forte@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97836SJohn.Forte@Sun.COM * or http://www.opensolaris.org/os/licensing.
107836SJohn.Forte@Sun.COM * See the License for the specific language governing permissions
117836SJohn.Forte@Sun.COM * and limitations under the License.
127836SJohn.Forte@Sun.COM *
137836SJohn.Forte@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
147836SJohn.Forte@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157836SJohn.Forte@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
167836SJohn.Forte@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
177836SJohn.Forte@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
187836SJohn.Forte@Sun.COM *
197836SJohn.Forte@Sun.COM * CDDL HEADER END
207836SJohn.Forte@Sun.COM */
217836SJohn.Forte@Sun.COM
227836SJohn.Forte@Sun.COM /*
23*8971Swl202157@icefox * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
247836SJohn.Forte@Sun.COM * Use is subject to license terms.
257836SJohn.Forte@Sun.COM */
267836SJohn.Forte@Sun.COM
277836SJohn.Forte@Sun.COM #include <sys/types.h>
287836SJohn.Forte@Sun.COM #include <unistd.h>
297836SJohn.Forte@Sun.COM #include <stdio.h>
307836SJohn.Forte@Sun.COM #include <stdlib.h>
317836SJohn.Forte@Sun.COM #include <sys/stat.h>
327836SJohn.Forte@Sun.COM #include <fcntl.h>
337836SJohn.Forte@Sun.COM #include <pthread.h>
347836SJohn.Forte@Sun.COM #include <errno.h>
357836SJohn.Forte@Sun.COM #include <libscf.h>
367836SJohn.Forte@Sun.COM #ifdef DEBUG
377836SJohn.Forte@Sun.COM #include <time.h>
387836SJohn.Forte@Sun.COM #endif
397836SJohn.Forte@Sun.COM #include <signal.h>
407836SJohn.Forte@Sun.COM #include <semaphore.h>
417836SJohn.Forte@Sun.COM #include <sys/wait.h>
427836SJohn.Forte@Sun.COM
437836SJohn.Forte@Sun.COM #include "isns_server.h"
447836SJohn.Forte@Sun.COM #include "isns_dseng.h"
457836SJohn.Forte@Sun.COM #include "isns_msgq.h"
467836SJohn.Forte@Sun.COM #include "isns_log.h"
477836SJohn.Forte@Sun.COM #include "isns_cfg.h"
487836SJohn.Forte@Sun.COM #include "isns_utils.h"
497836SJohn.Forte@Sun.COM #include "isns_cache.h"
507836SJohn.Forte@Sun.COM #include "isns_obj.h"
517836SJohn.Forte@Sun.COM #include "isns_dd.h"
527836SJohn.Forte@Sun.COM #include "isns_scn.h"
537836SJohn.Forte@Sun.COM #include "isns_sched.h"
547836SJohn.Forte@Sun.COM #include "isns_esi.h"
557836SJohn.Forte@Sun.COM #include "isns_mgmt.h"
567836SJohn.Forte@Sun.COM
577836SJohn.Forte@Sun.COM /*
587836SJohn.Forte@Sun.COM * iSNS Server administrative settings.
597836SJohn.Forte@Sun.COM */
607836SJohn.Forte@Sun.COM uint8_t daemonlize = 0;
617836SJohn.Forte@Sun.COM int dbg_level = 7;
627836SJohn.Forte@Sun.COM uint64_t esi_threshold;
637836SJohn.Forte@Sun.COM uint8_t mgmt_scn;
647836SJohn.Forte@Sun.COM ctrl_node_t *control_nodes = NULL;
657836SJohn.Forte@Sun.COM pthread_mutex_t ctrl_node_mtx = PTHREAD_MUTEX_INITIALIZER;
667836SJohn.Forte@Sun.COM char data_store[MAXPATHLEN];
677836SJohn.Forte@Sun.COM
687836SJohn.Forte@Sun.COM
697836SJohn.Forte@Sun.COM /* semaphore for handling exit */
707836SJohn.Forte@Sun.COM static sem_t isns_child_sem;
717836SJohn.Forte@Sun.COM static int isns_child_smf_exit_code;
727836SJohn.Forte@Sun.COM static pid_t isns_child_pid;
737836SJohn.Forte@Sun.COM
747836SJohn.Forte@Sun.COM #if !defined(SMF_EXIT_ERR_OTHER)
757836SJohn.Forte@Sun.COM #define SMF_EXIT_ERR_OTHER -1
767836SJohn.Forte@Sun.COM #endif
777836SJohn.Forte@Sun.COM
787836SJohn.Forte@Sun.COM /*
797836SJohn.Forte@Sun.COM * Globals for singal handling. time_to_exit is set by sig_handle()
807836SJohn.Forte@Sun.COM * when set the main thread(daemon) and othere threads should exit.
817836SJohn.Forte@Sun.COM *
827836SJohn.Forte@Sun.COM * semaphone is used to make sure all threads that are created
837836SJohn.Forte@Sun.COM * by isns_port_watcher and esi.
847836SJohn.Forte@Sun.COM */
857836SJohn.Forte@Sun.COM boolean_t time_to_exit = B_FALSE;
867836SJohn.Forte@Sun.COM static uint32_t thr_ref_count;
877836SJohn.Forte@Sun.COM static pthread_mutex_t thr_count_mtx = PTHREAD_MUTEX_INITIALIZER;
887836SJohn.Forte@Sun.COM #define MAX_RETRY_COUNT 10 /* for checking remaining threads before exit. */
897836SJohn.Forte@Sun.COM
907836SJohn.Forte@Sun.COM /*
91*8971Swl202157@icefox * Door creation flag.
92*8971Swl202157@icefox */
93*8971Swl202157@icefox boolean_t door_created = B_FALSE;
94*8971Swl202157@icefox
95*8971Swl202157@icefox /*
967836SJohn.Forte@Sun.COM * global system message queue
977836SJohn.Forte@Sun.COM */
987836SJohn.Forte@Sun.COM msg_queue_t *sys_q = NULL;
997836SJohn.Forte@Sun.COM msg_queue_t *scn_q = NULL;
1007836SJohn.Forte@Sun.COM
1017836SJohn.Forte@Sun.COM #ifdef DEBUG
1027836SJohn.Forte@Sun.COM extern void *cli_test(void *argv);
1037836SJohn.Forte@Sun.COM extern dump_db(void);
1047836SJohn.Forte@Sun.COM #endif
1057836SJohn.Forte@Sun.COM
1067836SJohn.Forte@Sun.COM extern void sigalrm(int);
1077836SJohn.Forte@Sun.COM
1087836SJohn.Forte@Sun.COM /*
1097836SJohn.Forte@Sun.COM * sigusr2_handler -- SIGUSR2 Handler
1107836SJohn.Forte@Sun.COM * sigusr2 is exepected only when child is running okay.
1117836SJohn.Forte@Sun.COM */
1127836SJohn.Forte@Sun.COM /* ARGSUSED */
1137836SJohn.Forte@Sun.COM static void
sigusr2_handler(int sig)1147836SJohn.Forte@Sun.COM sigusr2_handler(
1157836SJohn.Forte@Sun.COM int sig
1167836SJohn.Forte@Sun.COM )
1177836SJohn.Forte@Sun.COM {
1187836SJohn.Forte@Sun.COM /* post okay status. */
1197836SJohn.Forte@Sun.COM isnslog(LOG_DEBUG, "sigusr2_handler",
1207836SJohn.Forte@Sun.COM "SIGUSR@ is received. Parent is existing...");
1217836SJohn.Forte@Sun.COM isns_child_smf_exit_code = SMF_EXIT_OK;
1227836SJohn.Forte@Sun.COM
1237836SJohn.Forte@Sun.COM (void) sem_post(&isns_child_sem);
1247836SJohn.Forte@Sun.COM }
1257836SJohn.Forte@Sun.COM
1267836SJohn.Forte@Sun.COM /*
1277836SJohn.Forte@Sun.COM * sigchld_handler -- SIGCHLD Handler
1287836SJohn.Forte@Sun.COM * sigchld is exepected only when there is an error.
1297836SJohn.Forte@Sun.COM */
1307836SJohn.Forte@Sun.COM /* ARGSUSED */
1317836SJohn.Forte@Sun.COM static void
sigchld_handler(int sig)1327836SJohn.Forte@Sun.COM sigchld_handler(
1337836SJohn.Forte@Sun.COM int sig
1347836SJohn.Forte@Sun.COM )
1357836SJohn.Forte@Sun.COM {
1367836SJohn.Forte@Sun.COM int status;
1377836SJohn.Forte@Sun.COM pid_t ret_pid;
1387836SJohn.Forte@Sun.COM
1397836SJohn.Forte@Sun.COM /* This is the default code. */
1407836SJohn.Forte@Sun.COM isns_child_smf_exit_code = SMF_EXIT_ERR_OTHER;
1417836SJohn.Forte@Sun.COM
1427836SJohn.Forte@Sun.COM ret_pid = waitpid(isns_child_pid, &status, WNOHANG);
1437836SJohn.Forte@Sun.COM
1447836SJohn.Forte@Sun.COM if (ret_pid == isns_child_pid) {
1457836SJohn.Forte@Sun.COM if (WIFEXITED(status)) {
1467836SJohn.Forte@Sun.COM isns_child_smf_exit_code = WEXITSTATUS(status);
1477836SJohn.Forte@Sun.COM }
1487836SJohn.Forte@Sun.COM }
1497836SJohn.Forte@Sun.COM (void) sem_post(&isns_child_sem);
1507836SJohn.Forte@Sun.COM }
1517836SJohn.Forte@Sun.COM
1527836SJohn.Forte@Sun.COM /* ARGSUSED */
1537836SJohn.Forte@Sun.COM static void
sighup_handler(int sig)1547836SJohn.Forte@Sun.COM sighup_handler(
1557836SJohn.Forte@Sun.COM int sig
1567836SJohn.Forte@Sun.COM )
1577836SJohn.Forte@Sun.COM {
1587836SJohn.Forte@Sun.COM
1597836SJohn.Forte@Sun.COM isnslog(LOG_DEBUG, "sighup_handle",
1607836SJohn.Forte@Sun.COM "SIGHUP is received. Reloading config...");
1617836SJohn.Forte@Sun.COM (void) queue_msg_set(sys_q, CONFIG_RELOAD, NULL);
1627836SJohn.Forte@Sun.COM }
1637836SJohn.Forte@Sun.COM
1647836SJohn.Forte@Sun.COM /* ARGSUSED */
1657836SJohn.Forte@Sun.COM static void
sigexit_handler(int sig)1667836SJohn.Forte@Sun.COM sigexit_handler(
1677836SJohn.Forte@Sun.COM int sig
1687836SJohn.Forte@Sun.COM )
1697836SJohn.Forte@Sun.COM {
1707836SJohn.Forte@Sun.COM isnslog(LOG_DEBUG, "sigexit_handler",
1717836SJohn.Forte@Sun.COM "Signal: %d received and sending server exit.", sig);
1727836SJohn.Forte@Sun.COM shutdown_server();
1737836SJohn.Forte@Sun.COM }
1747836SJohn.Forte@Sun.COM
1757836SJohn.Forte@Sun.COM void
inc_thr_count()1767836SJohn.Forte@Sun.COM inc_thr_count(
1777836SJohn.Forte@Sun.COM )
1787836SJohn.Forte@Sun.COM {
1797836SJohn.Forte@Sun.COM (void) pthread_mutex_lock(&thr_count_mtx);
1807836SJohn.Forte@Sun.COM
1817836SJohn.Forte@Sun.COM isnslog(LOG_DEBUG, "inc_thr_count",
1827836SJohn.Forte@Sun.COM "increase thread reference count(%d).", thr_ref_count);
1837836SJohn.Forte@Sun.COM
1847836SJohn.Forte@Sun.COM thr_ref_count++;
1857836SJohn.Forte@Sun.COM
1867836SJohn.Forte@Sun.COM (void) pthread_mutex_unlock(&thr_count_mtx);
1877836SJohn.Forte@Sun.COM }
1887836SJohn.Forte@Sun.COM
1897836SJohn.Forte@Sun.COM void
dec_thr_count()1907836SJohn.Forte@Sun.COM dec_thr_count(
1917836SJohn.Forte@Sun.COM )
1927836SJohn.Forte@Sun.COM {
1937836SJohn.Forte@Sun.COM (void) pthread_mutex_lock(&thr_count_mtx);
1947836SJohn.Forte@Sun.COM
1957836SJohn.Forte@Sun.COM isnslog(LOG_DEBUG, "dec_thr_count",
1967836SJohn.Forte@Sun.COM "decrease thread reference count(%d).", thr_ref_count);
1977836SJohn.Forte@Sun.COM
1987836SJohn.Forte@Sun.COM thr_ref_count--;
1997836SJohn.Forte@Sun.COM
2007836SJohn.Forte@Sun.COM (void) pthread_mutex_unlock(&thr_count_mtx);
2017836SJohn.Forte@Sun.COM }
2027836SJohn.Forte@Sun.COM
2037836SJohn.Forte@Sun.COM uint32_t
get_thr_count()2047836SJohn.Forte@Sun.COM get_thr_count(
2057836SJohn.Forte@Sun.COM )
2067836SJohn.Forte@Sun.COM {
2077836SJohn.Forte@Sun.COM uint32_t ref;
2087836SJohn.Forte@Sun.COM
2097836SJohn.Forte@Sun.COM (void) pthread_mutex_lock(&thr_count_mtx);
2107836SJohn.Forte@Sun.COM
2117836SJohn.Forte@Sun.COM ref = thr_ref_count;
2127836SJohn.Forte@Sun.COM
2137836SJohn.Forte@Sun.COM (void) pthread_mutex_unlock(&thr_count_mtx);
2147836SJohn.Forte@Sun.COM
2157836SJohn.Forte@Sun.COM isnslog(LOG_DEBUG, "get_thr_count",
2167836SJohn.Forte@Sun.COM "checking thread reference count %d.", ref);
2177836SJohn.Forte@Sun.COM
2187836SJohn.Forte@Sun.COM return (ref);
2197836SJohn.Forte@Sun.COM }
2207836SJohn.Forte@Sun.COM
2217836SJohn.Forte@Sun.COM void
shutdown_server()2227836SJohn.Forte@Sun.COM shutdown_server(
2237836SJohn.Forte@Sun.COM )
2247836SJohn.Forte@Sun.COM {
2257836SJohn.Forte@Sun.COM isnslog(LOG_DEBUG, "shutdown", "raise exit flag.");
2267836SJohn.Forte@Sun.COM time_to_exit = B_TRUE;
2277836SJohn.Forte@Sun.COM (void) queue_msg_set(sys_q, SERVER_EXIT, NULL);
2287836SJohn.Forte@Sun.COM }
2297836SJohn.Forte@Sun.COM
2307836SJohn.Forte@Sun.COM int
main(int argc,char * argv[])2317836SJohn.Forte@Sun.COM main(
2327836SJohn.Forte@Sun.COM /* LINTED E_FUNC_ARG_UNUSED */
2337836SJohn.Forte@Sun.COM int argc,
2347836SJohn.Forte@Sun.COM /* LINTED E_FUNC_ARG_UNUSED */
2357836SJohn.Forte@Sun.COM char *argv[]
2367836SJohn.Forte@Sun.COM )
2377836SJohn.Forte@Sun.COM {
2387836SJohn.Forte@Sun.COM int opt_i = 0;
2397836SJohn.Forte@Sun.COM pthread_t port_tid, esi_tid, scn_tid;
2407836SJohn.Forte@Sun.COM uint32_t thr_cnt;
2417836SJohn.Forte@Sun.COM int i;
2427836SJohn.Forte@Sun.COM
2437836SJohn.Forte@Sun.COM #ifdef DEBUG
2447836SJohn.Forte@Sun.COM time_t t;
2457836SJohn.Forte@Sun.COM clock_t c;
2467836SJohn.Forte@Sun.COM #endif
2477836SJohn.Forte@Sun.COM
2487836SJohn.Forte@Sun.COM #ifdef DEBUG
2497836SJohn.Forte@Sun.COM if (getopt(argc, argv, "i") == 'i') {
2507836SJohn.Forte@Sun.COM opt_i = 1; /* interactive mode */
2517836SJohn.Forte@Sun.COM }
2527836SJohn.Forte@Sun.COM #endif
2537836SJohn.Forte@Sun.COM
2547836SJohn.Forte@Sun.COM /* set locale */
2557836SJohn.Forte@Sun.COM openlog(ISNS_DAEMON_SYSLOG_PP, LOG_PID | LOG_CONS, LOG_DAEMON);
2567836SJohn.Forte@Sun.COM
2577836SJohn.Forte@Sun.COM /* load administative settings. pick up data location. */
2587836SJohn.Forte@Sun.COM if (load_config(B_TRUE) != 0) {
2597836SJohn.Forte@Sun.COM isnslog(LOG_ERR, "main", "administrative settings load error.");
2607836SJohn.Forte@Sun.COM exit(SMF_EXIT_ERR_OTHER);
2617836SJohn.Forte@Sun.COM }
2627836SJohn.Forte@Sun.COM
2637836SJohn.Forte@Sun.COM /* A signal handler is set for SIGCHLD. */
2647836SJohn.Forte@Sun.COM (void) signal(SIGCHLD, sigchld_handler);
2657836SJohn.Forte@Sun.COM (void) signal(SIGUSR2, sigusr2_handler);
2667836SJohn.Forte@Sun.COM (void) sigset(SIGALRM, sigalrm);
2677836SJohn.Forte@Sun.COM
2687836SJohn.Forte@Sun.COM #ifdef DEBUG
2697836SJohn.Forte@Sun.COM printf("start daemon\n");
2707836SJohn.Forte@Sun.COM #endif
2717836SJohn.Forte@Sun.COM if (opt_i == 0 || daemonlize) {
2727836SJohn.Forte@Sun.COM isnslog(LOG_DEBUG, "main", "now forking... pid %d", getpid());
2737836SJohn.Forte@Sun.COM daemonlize = 1;
2747836SJohn.Forte@Sun.COM /* daemonlize */
2757836SJohn.Forte@Sun.COM isns_child_pid = fork();
2767836SJohn.Forte@Sun.COM if (isns_child_pid < 0) {
2777836SJohn.Forte@Sun.COM /*
2787836SJohn.Forte@Sun.COM * cannot fork(), terminate the server.
2797836SJohn.Forte@Sun.COM */
2807836SJohn.Forte@Sun.COM exit(SMF_EXIT_ERR_CONFIG);
2817836SJohn.Forte@Sun.COM }
2827836SJohn.Forte@Sun.COM if (isns_child_pid > 0) {
2837836SJohn.Forte@Sun.COM /*
2847836SJohn.Forte@Sun.COM * terminate parent.
2857836SJohn.Forte@Sun.COM */
2867836SJohn.Forte@Sun.COM (void) sem_wait(&isns_child_sem);
2877836SJohn.Forte@Sun.COM (void) sem_destroy(&isns_child_sem);
2887836SJohn.Forte@Sun.COM isnslog(LOG_DEBUG, "main", "exiting with %d",
2897836SJohn.Forte@Sun.COM isns_child_smf_exit_code);
2907836SJohn.Forte@Sun.COM exit(isns_child_smf_exit_code);
2917836SJohn.Forte@Sun.COM }
2927836SJohn.Forte@Sun.COM
2937836SJohn.Forte@Sun.COM /*
2947836SJohn.Forte@Sun.COM * redirect stdout, and stderr to /dev/null.
2957836SJohn.Forte@Sun.COM */
2967836SJohn.Forte@Sun.COM i = open("/dev/null", O_RDWR);
2977836SJohn.Forte@Sun.COM (void) dup2(i, 1);
2987836SJohn.Forte@Sun.COM (void) dup2(i, 2);
2997836SJohn.Forte@Sun.COM } /* end of daemonlize */
3007836SJohn.Forte@Sun.COM
3017836SJohn.Forte@Sun.COM #ifdef DEBUG
3027836SJohn.Forte@Sun.COM printf("calling cache init\n");
3037836SJohn.Forte@Sun.COM #endif
3047836SJohn.Forte@Sun.COM /* initialize object hash table */
3057836SJohn.Forte@Sun.COM if (cache_init() != 0) {
3067836SJohn.Forte@Sun.COM isnslog(LOG_ERR, "main",
3077836SJohn.Forte@Sun.COM "object hash table initialization error.");
3087836SJohn.Forte@Sun.COM exit(SMF_EXIT_ERR_OTHER);
3097836SJohn.Forte@Sun.COM }
3107836SJohn.Forte@Sun.COM
3117836SJohn.Forte@Sun.COM /* initialize event list */
3127836SJohn.Forte@Sun.COM if (el_init(10, 60, 6) != 0) {
3137836SJohn.Forte@Sun.COM isnslog(LOG_ERR, "main",
3147836SJohn.Forte@Sun.COM "ESI event list initialization error.");
3157836SJohn.Forte@Sun.COM exit(SMF_EXIT_ERR_OTHER);
3167836SJohn.Forte@Sun.COM }
3177836SJohn.Forte@Sun.COM
3187836SJohn.Forte@Sun.COM /* initialize iSNS database */
3197836SJohn.Forte@Sun.COM if (init_data() != 0) {
3207836SJohn.Forte@Sun.COM isnslog(LOG_ERR, "main",
3217836SJohn.Forte@Sun.COM "internal database initialization error");
3227836SJohn.Forte@Sun.COM exit(SMF_EXIT_ERR_OTHER);
3237836SJohn.Forte@Sun.COM }
3247836SJohn.Forte@Sun.COM
3257836SJohn.Forte@Sun.COM #ifdef DEBUG
3267836SJohn.Forte@Sun.COM printf("calling load_data\n");
3277836SJohn.Forte@Sun.COM t = time(NULL);
3287836SJohn.Forte@Sun.COM c = clock();
3297836SJohn.Forte@Sun.COM #endif
3307836SJohn.Forte@Sun.COM
3317836SJohn.Forte@Sun.COM if (load_data() != 0) {
3327836SJohn.Forte@Sun.COM isnslog(LOG_ERR, "main", "loading data store failed");
3337836SJohn.Forte@Sun.COM exit(SMF_EXIT_ERR_OTHER);
3347836SJohn.Forte@Sun.COM }
3357836SJohn.Forte@Sun.COM
3367836SJohn.Forte@Sun.COM #ifdef DEBUG
3377836SJohn.Forte@Sun.COM t = time(NULL) - t;
3387836SJohn.Forte@Sun.COM c = clock() - c;
3397836SJohn.Forte@Sun.COM printf("time %d clock %.4lf -loading data\n",
3407836SJohn.Forte@Sun.COM t, c / (double)CLOCKS_PER_SEC);
3417836SJohn.Forte@Sun.COM #endif
3427836SJohn.Forte@Sun.COM
3437836SJohn.Forte@Sun.COM #ifdef DEBUG
3447836SJohn.Forte@Sun.COM printf("sys queue creating...\n");
3457836SJohn.Forte@Sun.COM #endif
3467836SJohn.Forte@Sun.COM /* create a message queue for system control */
3477836SJohn.Forte@Sun.COM sys_q = queue_calloc();
3487836SJohn.Forte@Sun.COM if (!sys_q) {
3497836SJohn.Forte@Sun.COM exit(SMF_EXIT_ERR_OTHER);
3507836SJohn.Forte@Sun.COM }
3517836SJohn.Forte@Sun.COM
3527836SJohn.Forte@Sun.COM /* create a message queue for scn thread */
3537836SJohn.Forte@Sun.COM scn_q = queue_calloc();
3547836SJohn.Forte@Sun.COM if (!scn_q) {
3557836SJohn.Forte@Sun.COM exit(SMF_EXIT_ERR_OTHER);
3567836SJohn.Forte@Sun.COM }
3577836SJohn.Forte@Sun.COM
3587836SJohn.Forte@Sun.COM /* create scn thread */
3597836SJohn.Forte@Sun.COM /* Check for Default DD/DD-set existence and */
3607836SJohn.Forte@Sun.COM /* create them if they are not there. */
3617836SJohn.Forte@Sun.COM if (verify_ddd() != 0) {
3627836SJohn.Forte@Sun.COM exit(SMF_EXIT_ERR_OTHER);
3637836SJohn.Forte@Sun.COM }
3647836SJohn.Forte@Sun.COM
3657836SJohn.Forte@Sun.COM /* setup and verify the portal(s) for scn(s) */
3667836SJohn.Forte@Sun.COM /* after scn registry is loaded from data store. */
3677836SJohn.Forte@Sun.COM if (verify_scn_portal() != 0) {
3687836SJohn.Forte@Sun.COM exit(SMF_EXIT_ERR_OTHER);
3697836SJohn.Forte@Sun.COM }
3707836SJohn.Forte@Sun.COM
3717836SJohn.Forte@Sun.COM /* setup and verify the portal(s) for esi(s) */
3727836SJohn.Forte@Sun.COM /* after esi list is loaded from data store. */
3737836SJohn.Forte@Sun.COM if (verify_esi_portal() != 0) {
3747836SJohn.Forte@Sun.COM exit(SMF_EXIT_ERR_OTHER);
3757836SJohn.Forte@Sun.COM }
3767836SJohn.Forte@Sun.COM
3777836SJohn.Forte@Sun.COM #ifdef DEBUG
3787836SJohn.Forte@Sun.COM printf("scn queue creating...\n");
3797836SJohn.Forte@Sun.COM #endif
3807836SJohn.Forte@Sun.COM
3817836SJohn.Forte@Sun.COM (void) sigset(SIGHUP, sighup_handler);
3827836SJohn.Forte@Sun.COM (void) sigset(SIGINT, sigexit_handler);
3837836SJohn.Forte@Sun.COM (void) sigset(SIGTERM, sigexit_handler);
3847836SJohn.Forte@Sun.COM (void) sigset(SIGQUIT, sigexit_handler);
3857836SJohn.Forte@Sun.COM
3867836SJohn.Forte@Sun.COM /* create scn thread */
3877836SJohn.Forte@Sun.COM if (pthread_create(&scn_tid, NULL, scn_proc, NULL) != 0) {
3887836SJohn.Forte@Sun.COM isnslog(LOG_ERR, "main", "SCN thread creating error.");
3897836SJohn.Forte@Sun.COM exit(SMF_EXIT_ERR_OTHER);
3907836SJohn.Forte@Sun.COM }
3917836SJohn.Forte@Sun.COM
3927836SJohn.Forte@Sun.COM /* setup a door for management interface */
3937836SJohn.Forte@Sun.COM if (setup_mgmt_door(sys_q) != 0) {
3947836SJohn.Forte@Sun.COM exit(SMF_EXIT_ERR_OTHER);
3957836SJohn.Forte@Sun.COM }
3967836SJohn.Forte@Sun.COM
3977836SJohn.Forte@Sun.COM /* create server port watcher */
3987836SJohn.Forte@Sun.COM if (pthread_create(&port_tid, NULL,
3997836SJohn.Forte@Sun.COM isns_port_watcher, (void *)sys_q) != 0) {
4007836SJohn.Forte@Sun.COM isnslog(LOG_ERR, "main", "iSNS port thread creating error.");
4017836SJohn.Forte@Sun.COM exit(SMF_EXIT_ERR_OTHER);
4027836SJohn.Forte@Sun.COM }
4037836SJohn.Forte@Sun.COM
4047836SJohn.Forte@Sun.COM /* create entity status inquiry thread */
4057836SJohn.Forte@Sun.COM if (pthread_create(&esi_tid, NULL,
4067836SJohn.Forte@Sun.COM esi_proc, NULL) != 0) {
4077836SJohn.Forte@Sun.COM isnslog(LOG_ERR, "main", "ESI thread creating error.");
4087836SJohn.Forte@Sun.COM exit(SMF_EXIT_ERR_OTHER);
4097836SJohn.Forte@Sun.COM }
4107836SJohn.Forte@Sun.COM
4117836SJohn.Forte@Sun.COM #ifdef DEBUG
4127836SJohn.Forte@Sun.COM if (!daemonlize) {
4137836SJohn.Forte@Sun.COM (void) pthread_create(&tid,
4147836SJohn.Forte@Sun.COM NULL,
4157836SJohn.Forte@Sun.COM cli_test,
4167836SJohn.Forte@Sun.COM (void *)sys_q);
4177836SJohn.Forte@Sun.COM }
4187836SJohn.Forte@Sun.COM #endif
4197836SJohn.Forte@Sun.COM if (opt_i == 0 || daemonlize) {
4207836SJohn.Forte@Sun.COM isnslog(LOG_DEBUG, "main", "issuing SIGUSR2.. parent pid %d",
4217836SJohn.Forte@Sun.COM getppid());
4227836SJohn.Forte@Sun.COM (void) kill(getppid(), SIGUSR2);
4237836SJohn.Forte@Sun.COM }
4247836SJohn.Forte@Sun.COM
4257836SJohn.Forte@Sun.COM /* pause */
4267836SJohn.Forte@Sun.COM for (;;) {
4277836SJohn.Forte@Sun.COM msg_text_t *msg = queue_msg_get(sys_q);
4287836SJohn.Forte@Sun.COM switch (msg->id) {
4297836SJohn.Forte@Sun.COM case DATA_ADD:
4307836SJohn.Forte@Sun.COM case DATA_UPDATE:
4317836SJohn.Forte@Sun.COM case DATA_DELETE:
4327836SJohn.Forte@Sun.COM case DATA_DELETE_ASSOC:
4337836SJohn.Forte@Sun.COM case DATA_COMMIT:
4347836SJohn.Forte@Sun.COM case DATA_RETREAT:
4357836SJohn.Forte@Sun.COM break;
4367836SJohn.Forte@Sun.COM case REG_EXP:
4377836SJohn.Forte@Sun.COM /* registration expiring */
4387836SJohn.Forte@Sun.COM reg_expiring(msg->data);
4397836SJohn.Forte@Sun.COM break;
4407836SJohn.Forte@Sun.COM case DEAD_PORTAL:
4417836SJohn.Forte@Sun.COM portal_dies((uint32_t)msg->data);
4427836SJohn.Forte@Sun.COM break;
4437836SJohn.Forte@Sun.COM case SERVER_EXIT:
4447836SJohn.Forte@Sun.COM /* graceful exit. */
4457836SJohn.Forte@Sun.COM (void) queue_msg_free(msg);
4467836SJohn.Forte@Sun.COM isnslog(LOG_DEBUG, "main",
4477836SJohn.Forte@Sun.COM "wake up ESI and stop it.");
4487836SJohn.Forte@Sun.COM (void) get_stopwatch(1);
4497836SJohn.Forte@Sun.COM isnslog(LOG_DEBUG, "main",
4507836SJohn.Forte@Sun.COM "sending SCN stop msg.");
4517836SJohn.Forte@Sun.COM (void) queue_msg_set(scn_q, SCN_STOP, NULL);
452*8971Swl202157@icefox if (door_created) {
453*8971Swl202157@icefox isnslog(LOG_DEBUG, "main",
454*8971Swl202157@icefox "closing the door.");
455*8971Swl202157@icefox (void) fdetach(ISNS_DOOR_NAME);
456*8971Swl202157@icefox }
4577836SJohn.Forte@Sun.COM (void) pthread_join(esi_tid, NULL);
4587836SJohn.Forte@Sun.COM isnslog(LOG_DEBUG, "main",
4597836SJohn.Forte@Sun.COM "esi thread %d exited.", esi_tid);
4607836SJohn.Forte@Sun.COM (void) pthread_join(port_tid, NULL);
4617836SJohn.Forte@Sun.COM isnslog(LOG_DEBUG, "main",
4627836SJohn.Forte@Sun.COM "port watcher thread %d exited.", port_tid);
4637836SJohn.Forte@Sun.COM (void) pthread_join(scn_tid, NULL);
4647836SJohn.Forte@Sun.COM isnslog(LOG_DEBUG, "main",
4657836SJohn.Forte@Sun.COM "scn thread %d exited.", scn_tid);
4667836SJohn.Forte@Sun.COM
4677836SJohn.Forte@Sun.COM /* now check any remaining threads. */
4687836SJohn.Forte@Sun.COM i = 0;
4697836SJohn.Forte@Sun.COM do {
4707836SJohn.Forte@Sun.COM thr_cnt = get_thr_count();
4717836SJohn.Forte@Sun.COM if (thr_cnt == 0) {
4727836SJohn.Forte@Sun.COM isnslog(LOG_DEBUG, "main",
4737836SJohn.Forte@Sun.COM "main thread %d is done.",
4747836SJohn.Forte@Sun.COM pthread_self());
4757836SJohn.Forte@Sun.COM exit(1);
4767836SJohn.Forte@Sun.COM } else {
4777836SJohn.Forte@Sun.COM (void) sleep(1);
4787836SJohn.Forte@Sun.COM i++;
4797836SJohn.Forte@Sun.COM }
4807836SJohn.Forte@Sun.COM } while (MAX_RETRY_COUNT > i);
4817836SJohn.Forte@Sun.COM isnslog(LOG_DEBUG, "main",
4827836SJohn.Forte@Sun.COM "main thread %d existing ...",
4837836SJohn.Forte@Sun.COM pthread_self());
4847836SJohn.Forte@Sun.COM exit(1);
4857836SJohn.Forte@Sun.COM break;
4867836SJohn.Forte@Sun.COM case CONFIG_RELOAD:
4877836SJohn.Forte@Sun.COM /* load config again. don't pick data store. */
4887836SJohn.Forte@Sun.COM (void) load_config(B_FALSE);
4897836SJohn.Forte@Sun.COM break;
4907836SJohn.Forte@Sun.COM case SYS_QUIT_OK:
4917836SJohn.Forte@Sun.COM (void) queue_msg_free(msg);
4927836SJohn.Forte@Sun.COM exit(0);
4937836SJohn.Forte@Sun.COM default:
4947836SJohn.Forte@Sun.COM break;
4957836SJohn.Forte@Sun.COM }
4967836SJohn.Forte@Sun.COM (void) queue_msg_free(msg);
4977836SJohn.Forte@Sun.COM }
4987836SJohn.Forte@Sun.COM
4997836SJohn.Forte@Sun.COM /* LINTED E_STMT_NOT_REACHED */
5007836SJohn.Forte@Sun.COM return (0);
5017836SJohn.Forte@Sun.COM }
502