xref: /illumos-gate/usr/src/cmd/isns/isnsd/main.c (revision 3f1da666)
1fcf3ce44SJohn Forte /*
2fcf3ce44SJohn Forte  * CDDL HEADER START
3fcf3ce44SJohn Forte  *
4fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7fcf3ce44SJohn Forte  *
8fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11fcf3ce44SJohn Forte  * and limitations under the License.
12fcf3ce44SJohn Forte  *
13fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18fcf3ce44SJohn Forte  *
19fcf3ce44SJohn Forte  * CDDL HEADER END
20fcf3ce44SJohn Forte  */
21fcf3ce44SJohn Forte 
22fcf3ce44SJohn Forte /*
23*3f1da666Swl  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24fcf3ce44SJohn Forte  * Use is subject to license terms.
25fcf3ce44SJohn Forte  */
26fcf3ce44SJohn Forte 
27fcf3ce44SJohn Forte #include <sys/types.h>
28fcf3ce44SJohn Forte #include <unistd.h>
29fcf3ce44SJohn Forte #include <stdio.h>
30fcf3ce44SJohn Forte #include <stdlib.h>
31fcf3ce44SJohn Forte #include <sys/stat.h>
32fcf3ce44SJohn Forte #include <fcntl.h>
33fcf3ce44SJohn Forte #include <pthread.h>
34fcf3ce44SJohn Forte #include <errno.h>
35fcf3ce44SJohn Forte #include <libscf.h>
36fcf3ce44SJohn Forte #ifdef DEBUG
37fcf3ce44SJohn Forte #include <time.h>
38fcf3ce44SJohn Forte #endif
39fcf3ce44SJohn Forte #include <signal.h>
40fcf3ce44SJohn Forte #include <semaphore.h>
41fcf3ce44SJohn Forte #include <sys/wait.h>
42fcf3ce44SJohn Forte 
43fcf3ce44SJohn Forte #include "isns_server.h"
44fcf3ce44SJohn Forte #include "isns_dseng.h"
45fcf3ce44SJohn Forte #include "isns_msgq.h"
46fcf3ce44SJohn Forte #include "isns_log.h"
47fcf3ce44SJohn Forte #include "isns_cfg.h"
48fcf3ce44SJohn Forte #include "isns_utils.h"
49fcf3ce44SJohn Forte #include "isns_cache.h"
50fcf3ce44SJohn Forte #include "isns_obj.h"
51fcf3ce44SJohn Forte #include "isns_dd.h"
52fcf3ce44SJohn Forte #include "isns_scn.h"
53fcf3ce44SJohn Forte #include "isns_sched.h"
54fcf3ce44SJohn Forte #include "isns_esi.h"
55fcf3ce44SJohn Forte #include "isns_mgmt.h"
56fcf3ce44SJohn Forte 
57fcf3ce44SJohn Forte /*
58fcf3ce44SJohn Forte  * iSNS Server administrative settings.
59fcf3ce44SJohn Forte  */
60fcf3ce44SJohn Forte uint8_t daemonlize = 0;
61fcf3ce44SJohn Forte int dbg_level = 7;
62fcf3ce44SJohn Forte uint64_t esi_threshold;
63fcf3ce44SJohn Forte uint8_t mgmt_scn;
64fcf3ce44SJohn Forte ctrl_node_t *control_nodes = NULL;
65fcf3ce44SJohn Forte pthread_mutex_t ctrl_node_mtx = PTHREAD_MUTEX_INITIALIZER;
66fcf3ce44SJohn Forte char data_store[MAXPATHLEN];
67fcf3ce44SJohn Forte 
68fcf3ce44SJohn Forte 
69fcf3ce44SJohn Forte /* semaphore for handling exit */
70fcf3ce44SJohn Forte static sem_t	isns_child_sem;
71fcf3ce44SJohn Forte static int	isns_child_smf_exit_code;
72fcf3ce44SJohn Forte static pid_t	isns_child_pid;
73fcf3ce44SJohn Forte 
74fcf3ce44SJohn Forte #if !defined(SMF_EXIT_ERR_OTHER)
75fcf3ce44SJohn Forte #define	SMF_EXIT_ERR_OTHER	-1
76fcf3ce44SJohn Forte #endif
77fcf3ce44SJohn Forte 
78fcf3ce44SJohn Forte /*
79fcf3ce44SJohn Forte  * Globals for singal handling.  time_to_exit is set by sig_handle()
80fcf3ce44SJohn Forte  * when set the main thread(daemon) and othere threads should exit.
81fcf3ce44SJohn Forte  *
82fcf3ce44SJohn Forte  * semaphone is used to make sure all threads that are created
83fcf3ce44SJohn Forte  * by isns_port_watcher and esi.
84fcf3ce44SJohn Forte  */
85fcf3ce44SJohn Forte boolean_t time_to_exit = B_FALSE;
86fcf3ce44SJohn Forte static uint32_t thr_ref_count;
87fcf3ce44SJohn Forte static pthread_mutex_t thr_count_mtx = PTHREAD_MUTEX_INITIALIZER;
88fcf3ce44SJohn Forte #define	MAX_RETRY_COUNT	10 /* for checking remaining threads before exit. */
89fcf3ce44SJohn Forte 
90*3f1da666Swl /*
91*3f1da666Swl  * Door creation flag.
92*3f1da666Swl  */
93*3f1da666Swl boolean_t door_created = B_FALSE;
94*3f1da666Swl 
95fcf3ce44SJohn Forte /*
96fcf3ce44SJohn Forte  * global system message queue
97fcf3ce44SJohn Forte  */
98fcf3ce44SJohn Forte msg_queue_t *sys_q = NULL;
99fcf3ce44SJohn Forte msg_queue_t *scn_q = NULL;
100fcf3ce44SJohn Forte 
101fcf3ce44SJohn Forte #ifdef DEBUG
102fcf3ce44SJohn Forte extern void *cli_test(void *argv);
103fcf3ce44SJohn Forte extern dump_db(void);
104fcf3ce44SJohn Forte #endif
105fcf3ce44SJohn Forte 
106fcf3ce44SJohn Forte extern void sigalrm(int);
107fcf3ce44SJohn Forte 
108fcf3ce44SJohn Forte /*
109fcf3ce44SJohn Forte  * sigusr2_handler -- SIGUSR2 Handler
110fcf3ce44SJohn Forte  * sigusr2 is exepected only when child is running okay.
111fcf3ce44SJohn Forte  */
112fcf3ce44SJohn Forte /* ARGSUSED */
113fcf3ce44SJohn Forte static void
sigusr2_handler(int sig)114fcf3ce44SJohn Forte sigusr2_handler(
115fcf3ce44SJohn Forte 	int	sig
116fcf3ce44SJohn Forte )
117fcf3ce44SJohn Forte {
118fcf3ce44SJohn Forte 	/* post okay status. */
119fcf3ce44SJohn Forte 	isnslog(LOG_DEBUG, "sigusr2_handler",
120fcf3ce44SJohn Forte 	    "SIGUSR@ is received.  Parent is existing...");
121fcf3ce44SJohn Forte 	isns_child_smf_exit_code = SMF_EXIT_OK;
122fcf3ce44SJohn Forte 
123fcf3ce44SJohn Forte 	(void) sem_post(&isns_child_sem);
124fcf3ce44SJohn Forte }
125fcf3ce44SJohn Forte 
126fcf3ce44SJohn Forte /*
127fcf3ce44SJohn Forte  * sigchld_handler -- SIGCHLD Handler
128fcf3ce44SJohn Forte  * sigchld is exepected only when there is an error.
129fcf3ce44SJohn Forte  */
130fcf3ce44SJohn Forte /* ARGSUSED */
131fcf3ce44SJohn Forte static void
sigchld_handler(int sig)132fcf3ce44SJohn Forte sigchld_handler(
133fcf3ce44SJohn Forte 	int	sig
134fcf3ce44SJohn Forte )
135fcf3ce44SJohn Forte {
136fcf3ce44SJohn Forte 	int	status;
137fcf3ce44SJohn Forte 	pid_t	ret_pid;
138fcf3ce44SJohn Forte 
139fcf3ce44SJohn Forte 	/* This is the default code. */
140fcf3ce44SJohn Forte 	isns_child_smf_exit_code = SMF_EXIT_ERR_OTHER;
141fcf3ce44SJohn Forte 
142fcf3ce44SJohn Forte 	ret_pid = waitpid(isns_child_pid, &status, WNOHANG);
143fcf3ce44SJohn Forte 
144fcf3ce44SJohn Forte 	if (ret_pid == isns_child_pid) {
145fcf3ce44SJohn Forte 		if (WIFEXITED(status)) {
146fcf3ce44SJohn Forte 			isns_child_smf_exit_code = WEXITSTATUS(status);
147fcf3ce44SJohn Forte 		}
148fcf3ce44SJohn Forte 	}
149fcf3ce44SJohn Forte 	(void) sem_post(&isns_child_sem);
150fcf3ce44SJohn Forte }
151fcf3ce44SJohn Forte 
152fcf3ce44SJohn Forte /* ARGSUSED */
153fcf3ce44SJohn Forte static void
sighup_handler(int sig)154fcf3ce44SJohn Forte sighup_handler(
155fcf3ce44SJohn Forte 	int	sig
156fcf3ce44SJohn Forte )
157fcf3ce44SJohn Forte {
158fcf3ce44SJohn Forte 
159fcf3ce44SJohn Forte 	isnslog(LOG_DEBUG, "sighup_handle",
160fcf3ce44SJohn Forte 	    "SIGHUP is received.  Reloading config...");
161fcf3ce44SJohn Forte 	(void) queue_msg_set(sys_q, CONFIG_RELOAD, NULL);
162fcf3ce44SJohn Forte }
163fcf3ce44SJohn Forte 
164fcf3ce44SJohn Forte /* ARGSUSED */
165fcf3ce44SJohn Forte static void
sigexit_handler(int sig)166fcf3ce44SJohn Forte sigexit_handler(
167fcf3ce44SJohn Forte 	int	sig
168fcf3ce44SJohn Forte )
169fcf3ce44SJohn Forte {
170fcf3ce44SJohn Forte 	isnslog(LOG_DEBUG, "sigexit_handler",
171fcf3ce44SJohn Forte 	    "Signal: %d received and sending server exit.", sig);
172fcf3ce44SJohn Forte 	shutdown_server();
173fcf3ce44SJohn Forte }
174fcf3ce44SJohn Forte 
175fcf3ce44SJohn Forte void
inc_thr_count()176fcf3ce44SJohn Forte inc_thr_count(
177fcf3ce44SJohn Forte )
178fcf3ce44SJohn Forte {
179fcf3ce44SJohn Forte 	(void) pthread_mutex_lock(&thr_count_mtx);
180fcf3ce44SJohn Forte 
181fcf3ce44SJohn Forte 	isnslog(LOG_DEBUG, "inc_thr_count",
182fcf3ce44SJohn Forte 	    "increase thread reference count(%d).", thr_ref_count);
183fcf3ce44SJohn Forte 
184fcf3ce44SJohn Forte 	thr_ref_count++;
185fcf3ce44SJohn Forte 
186fcf3ce44SJohn Forte 	(void) pthread_mutex_unlock(&thr_count_mtx);
187fcf3ce44SJohn Forte }
188fcf3ce44SJohn Forte 
189fcf3ce44SJohn Forte void
dec_thr_count()190fcf3ce44SJohn Forte dec_thr_count(
191fcf3ce44SJohn Forte )
192fcf3ce44SJohn Forte {
193fcf3ce44SJohn Forte 	(void) pthread_mutex_lock(&thr_count_mtx);
194fcf3ce44SJohn Forte 
195fcf3ce44SJohn Forte 	isnslog(LOG_DEBUG, "dec_thr_count",
196fcf3ce44SJohn Forte 	    "decrease thread reference count(%d).", thr_ref_count);
197fcf3ce44SJohn Forte 
198fcf3ce44SJohn Forte 	thr_ref_count--;
199fcf3ce44SJohn Forte 
200fcf3ce44SJohn Forte 	(void) pthread_mutex_unlock(&thr_count_mtx);
201fcf3ce44SJohn Forte }
202fcf3ce44SJohn Forte 
203fcf3ce44SJohn Forte uint32_t
get_thr_count()204fcf3ce44SJohn Forte get_thr_count(
205fcf3ce44SJohn Forte )
206fcf3ce44SJohn Forte {
207fcf3ce44SJohn Forte 	uint32_t ref;
208fcf3ce44SJohn Forte 
209fcf3ce44SJohn Forte 	(void) pthread_mutex_lock(&thr_count_mtx);
210fcf3ce44SJohn Forte 
211fcf3ce44SJohn Forte 	ref = thr_ref_count;
212fcf3ce44SJohn Forte 
213fcf3ce44SJohn Forte 	(void) pthread_mutex_unlock(&thr_count_mtx);
214fcf3ce44SJohn Forte 
215fcf3ce44SJohn Forte 	isnslog(LOG_DEBUG, "get_thr_count",
216fcf3ce44SJohn Forte 	    "checking thread reference count %d.", ref);
217fcf3ce44SJohn Forte 
218fcf3ce44SJohn Forte 	return (ref);
219fcf3ce44SJohn Forte }
220fcf3ce44SJohn Forte 
221fcf3ce44SJohn Forte void
shutdown_server()222fcf3ce44SJohn Forte shutdown_server(
223fcf3ce44SJohn Forte )
224fcf3ce44SJohn Forte {
225fcf3ce44SJohn Forte 	isnslog(LOG_DEBUG, "shutdown", "raise exit flag.");
226fcf3ce44SJohn Forte 	time_to_exit = B_TRUE;
227fcf3ce44SJohn Forte 	(void) queue_msg_set(sys_q, SERVER_EXIT, NULL);
228fcf3ce44SJohn Forte }
229fcf3ce44SJohn Forte 
230fcf3ce44SJohn Forte int
main(int argc,char * argv[])231fcf3ce44SJohn Forte main(
232fcf3ce44SJohn Forte 	/* LINTED E_FUNC_ARG_UNUSED */
233fcf3ce44SJohn Forte 	int	argc,
234fcf3ce44SJohn Forte 	/* LINTED E_FUNC_ARG_UNUSED */
235fcf3ce44SJohn Forte 	char	*argv[]
236fcf3ce44SJohn Forte )
237fcf3ce44SJohn Forte {
238fcf3ce44SJohn Forte 	int opt_i = 0;
239fcf3ce44SJohn Forte 	pthread_t port_tid, esi_tid, scn_tid;
240fcf3ce44SJohn Forte 	uint32_t thr_cnt;
241fcf3ce44SJohn Forte 	int i;
242fcf3ce44SJohn Forte 
243fcf3ce44SJohn Forte #ifdef DEBUG
244fcf3ce44SJohn Forte 	time_t t;
245fcf3ce44SJohn Forte 	clock_t c;
246fcf3ce44SJohn Forte #endif
247fcf3ce44SJohn Forte 
248fcf3ce44SJohn Forte #ifdef DEBUG
249fcf3ce44SJohn Forte 	if (getopt(argc, argv, "i") == 'i') {
250fcf3ce44SJohn Forte 		opt_i = 1; /* interactive mode */
251fcf3ce44SJohn Forte 	}
252fcf3ce44SJohn Forte #endif
253fcf3ce44SJohn Forte 
254fcf3ce44SJohn Forte 	/* set locale */
255fcf3ce44SJohn Forte 	openlog(ISNS_DAEMON_SYSLOG_PP, LOG_PID | LOG_CONS, LOG_DAEMON);
256fcf3ce44SJohn Forte 
257fcf3ce44SJohn Forte 	/* load administative settings. pick up data location. */
258fcf3ce44SJohn Forte 	if (load_config(B_TRUE) != 0) {
259fcf3ce44SJohn Forte 		isnslog(LOG_ERR, "main", "administrative settings load error.");
260fcf3ce44SJohn Forte 		exit(SMF_EXIT_ERR_OTHER);
261fcf3ce44SJohn Forte 	}
262fcf3ce44SJohn Forte 
263fcf3ce44SJohn Forte 	/* A signal handler is set for SIGCHLD. */
264fcf3ce44SJohn Forte 	(void) signal(SIGCHLD, sigchld_handler);
265fcf3ce44SJohn Forte 	(void) signal(SIGUSR2, sigusr2_handler);
266fcf3ce44SJohn Forte 	(void) sigset(SIGALRM, sigalrm);
267fcf3ce44SJohn Forte 
268fcf3ce44SJohn Forte #ifdef DEBUG
269fcf3ce44SJohn Forte 	printf("start daemon\n");
270fcf3ce44SJohn Forte #endif
271fcf3ce44SJohn Forte 	if (opt_i == 0 || daemonlize) {
272fcf3ce44SJohn Forte 		isnslog(LOG_DEBUG, "main", "now forking... pid %d", getpid());
273fcf3ce44SJohn Forte 		daemonlize = 1;
274fcf3ce44SJohn Forte 		/* daemonlize */
275fcf3ce44SJohn Forte 		isns_child_pid = fork();
276fcf3ce44SJohn Forte 		if (isns_child_pid < 0) {
277fcf3ce44SJohn Forte 			/*
278fcf3ce44SJohn Forte 			 * cannot fork(), terminate the server.
279fcf3ce44SJohn Forte 			 */
280fcf3ce44SJohn Forte 			exit(SMF_EXIT_ERR_CONFIG);
281fcf3ce44SJohn Forte 		}
282fcf3ce44SJohn Forte 		if (isns_child_pid > 0) {
283fcf3ce44SJohn Forte 			/*
284fcf3ce44SJohn Forte 			 * terminate parent.
285fcf3ce44SJohn Forte 			 */
286fcf3ce44SJohn Forte 			(void) sem_wait(&isns_child_sem);
287fcf3ce44SJohn Forte 			(void) sem_destroy(&isns_child_sem);
288fcf3ce44SJohn Forte 			isnslog(LOG_DEBUG, "main", "exiting with %d",
289fcf3ce44SJohn Forte 				isns_child_smf_exit_code);
290fcf3ce44SJohn Forte 			exit(isns_child_smf_exit_code);
291fcf3ce44SJohn Forte 		}
292fcf3ce44SJohn Forte 
293fcf3ce44SJohn Forte 		/*
294fcf3ce44SJohn Forte 		 * redirect stdout, and stderr to /dev/null.
295fcf3ce44SJohn Forte 		 */
296fcf3ce44SJohn Forte 		i = open("/dev/null", O_RDWR);
297fcf3ce44SJohn Forte 		(void) dup2(i, 1);
298fcf3ce44SJohn Forte 		(void) dup2(i, 2);
299fcf3ce44SJohn Forte 	} /* end of daemonlize */
300fcf3ce44SJohn Forte 
301fcf3ce44SJohn Forte #ifdef DEBUG
302fcf3ce44SJohn Forte 	printf("calling cache init\n");
303fcf3ce44SJohn Forte #endif
304fcf3ce44SJohn Forte 	/* initialize object hash table */
305fcf3ce44SJohn Forte 	if (cache_init() != 0) {
306fcf3ce44SJohn Forte 		isnslog(LOG_ERR, "main",
307fcf3ce44SJohn Forte 		    "object hash table initialization error.");
308fcf3ce44SJohn Forte 		exit(SMF_EXIT_ERR_OTHER);
309fcf3ce44SJohn Forte 	}
310fcf3ce44SJohn Forte 
311fcf3ce44SJohn Forte 	/* initialize event list */
312fcf3ce44SJohn Forte 	if (el_init(10, 60, 6) != 0) {
313fcf3ce44SJohn Forte 		isnslog(LOG_ERR, "main",
314fcf3ce44SJohn Forte 		"ESI event list initialization error.");
315fcf3ce44SJohn Forte 		exit(SMF_EXIT_ERR_OTHER);
316fcf3ce44SJohn Forte 	}
317fcf3ce44SJohn Forte 
318fcf3ce44SJohn Forte 	/* initialize iSNS database */
319fcf3ce44SJohn Forte 	if (init_data() != 0) {
320fcf3ce44SJohn Forte 		isnslog(LOG_ERR, "main",
321fcf3ce44SJohn Forte 		    "internal database initialization error");
322fcf3ce44SJohn Forte 		exit(SMF_EXIT_ERR_OTHER);
323fcf3ce44SJohn Forte 	}
324fcf3ce44SJohn Forte 
325fcf3ce44SJohn Forte #ifdef DEBUG
326fcf3ce44SJohn Forte 	printf("calling load_data\n");
327fcf3ce44SJohn Forte 	t = time(NULL);
328fcf3ce44SJohn Forte 	c = clock();
329fcf3ce44SJohn Forte #endif
330fcf3ce44SJohn Forte 
331fcf3ce44SJohn Forte 	if (load_data() != 0) {
332fcf3ce44SJohn Forte 		isnslog(LOG_ERR, "main", "loading data store failed");
333fcf3ce44SJohn Forte 		exit(SMF_EXIT_ERR_OTHER);
334fcf3ce44SJohn Forte 	}
335fcf3ce44SJohn Forte 
336fcf3ce44SJohn Forte #ifdef DEBUG
337fcf3ce44SJohn Forte 	t = time(NULL) - t;
338fcf3ce44SJohn Forte 	c = clock() - c;
339fcf3ce44SJohn Forte 	printf("time %d clock %.4lf -loading data\n",
340fcf3ce44SJohn Forte 	    t, c / (double)CLOCKS_PER_SEC);
341fcf3ce44SJohn Forte #endif
342fcf3ce44SJohn Forte 
343fcf3ce44SJohn Forte #ifdef DEBUG
344fcf3ce44SJohn Forte 	printf("sys queue creating...\n");
345fcf3ce44SJohn Forte #endif
346fcf3ce44SJohn Forte 	/* create a message queue for system control */
347fcf3ce44SJohn Forte 	sys_q = queue_calloc();
348fcf3ce44SJohn Forte 	if (!sys_q) {
349fcf3ce44SJohn Forte 		exit(SMF_EXIT_ERR_OTHER);
350fcf3ce44SJohn Forte 	}
351fcf3ce44SJohn Forte 
352fcf3ce44SJohn Forte 	/* create a message queue for scn thread */
353fcf3ce44SJohn Forte 	scn_q = queue_calloc();
354fcf3ce44SJohn Forte 	if (!scn_q) {
355fcf3ce44SJohn Forte 		exit(SMF_EXIT_ERR_OTHER);
356fcf3ce44SJohn Forte 	}
357fcf3ce44SJohn Forte 
358fcf3ce44SJohn Forte 	/* create scn thread */
359fcf3ce44SJohn Forte 	/* Check for Default DD/DD-set existence and */
360fcf3ce44SJohn Forte 	/* create them if they are not there. */
361fcf3ce44SJohn Forte 	if (verify_ddd() != 0) {
362fcf3ce44SJohn Forte 		exit(SMF_EXIT_ERR_OTHER);
363fcf3ce44SJohn Forte 	}
364fcf3ce44SJohn Forte 
365fcf3ce44SJohn Forte 	/* setup and verify the portal(s) for scn(s) */
366fcf3ce44SJohn Forte 	/* after scn registry is loaded from data store. */
367fcf3ce44SJohn Forte 	if (verify_scn_portal() != 0) {
368fcf3ce44SJohn Forte 		exit(SMF_EXIT_ERR_OTHER);
369fcf3ce44SJohn Forte 	}
370fcf3ce44SJohn Forte 
371fcf3ce44SJohn Forte 	/* setup and verify the portal(s) for esi(s) */
372fcf3ce44SJohn Forte 	/* after esi list is loaded from data store. */
373fcf3ce44SJohn Forte 	if (verify_esi_portal() != 0) {
374fcf3ce44SJohn Forte 		exit(SMF_EXIT_ERR_OTHER);
375fcf3ce44SJohn Forte 	}
376fcf3ce44SJohn Forte 
377fcf3ce44SJohn Forte #ifdef DEBUG
378fcf3ce44SJohn Forte 	printf("scn queue creating...\n");
379fcf3ce44SJohn Forte #endif
380fcf3ce44SJohn Forte 
381fcf3ce44SJohn Forte 	(void) sigset(SIGHUP, sighup_handler);
382fcf3ce44SJohn Forte 	(void) sigset(SIGINT, sigexit_handler);
383fcf3ce44SJohn Forte 	(void) sigset(SIGTERM, sigexit_handler);
384fcf3ce44SJohn Forte 	(void) sigset(SIGQUIT, sigexit_handler);
385fcf3ce44SJohn Forte 
386fcf3ce44SJohn Forte 	/* create scn thread */
387fcf3ce44SJohn Forte 	if (pthread_create(&scn_tid, NULL, scn_proc, NULL) != 0) {
388fcf3ce44SJohn Forte 		isnslog(LOG_ERR, "main", "SCN thread creating error.");
389fcf3ce44SJohn Forte 		exit(SMF_EXIT_ERR_OTHER);
390fcf3ce44SJohn Forte 	}
391fcf3ce44SJohn Forte 
392fcf3ce44SJohn Forte 	/* setup a door for management interface */
393fcf3ce44SJohn Forte 	if (setup_mgmt_door(sys_q) != 0) {
394fcf3ce44SJohn Forte 		exit(SMF_EXIT_ERR_OTHER);
395fcf3ce44SJohn Forte 	}
396fcf3ce44SJohn Forte 
397fcf3ce44SJohn Forte 	/* create server port watcher */
398fcf3ce44SJohn Forte 	if (pthread_create(&port_tid, NULL,
399fcf3ce44SJohn Forte 	    isns_port_watcher, (void *)sys_q) != 0) {
400fcf3ce44SJohn Forte 		isnslog(LOG_ERR, "main", "iSNS port thread creating error.");
401fcf3ce44SJohn Forte 		exit(SMF_EXIT_ERR_OTHER);
402fcf3ce44SJohn Forte 	}
403fcf3ce44SJohn Forte 
404fcf3ce44SJohn Forte 	/* create entity status inquiry thread */
405fcf3ce44SJohn Forte 	if (pthread_create(&esi_tid, NULL,
406fcf3ce44SJohn Forte 	    esi_proc, NULL) != 0) {
407fcf3ce44SJohn Forte 		isnslog(LOG_ERR, "main", "ESI thread creating error.");
408fcf3ce44SJohn Forte 		exit(SMF_EXIT_ERR_OTHER);
409fcf3ce44SJohn Forte 	}
410fcf3ce44SJohn Forte 
411fcf3ce44SJohn Forte #ifdef DEBUG
412fcf3ce44SJohn Forte 	if (!daemonlize) {
413fcf3ce44SJohn Forte 		(void) pthread_create(&tid,
414fcf3ce44SJohn Forte 		    NULL,
415fcf3ce44SJohn Forte 		    cli_test,
416fcf3ce44SJohn Forte 		    (void *)sys_q);
417fcf3ce44SJohn Forte 	}
418fcf3ce44SJohn Forte #endif
419fcf3ce44SJohn Forte 	if (opt_i == 0 || daemonlize) {
420fcf3ce44SJohn Forte 		isnslog(LOG_DEBUG, "main", "issuing SIGUSR2.. parent pid %d",
421fcf3ce44SJohn Forte 		    getppid());
422fcf3ce44SJohn Forte 		(void) kill(getppid(), SIGUSR2);
423fcf3ce44SJohn Forte 	}
424fcf3ce44SJohn Forte 
425fcf3ce44SJohn Forte 	/* pause */
426fcf3ce44SJohn Forte 	for (;;) {
427fcf3ce44SJohn Forte 		msg_text_t *msg = queue_msg_get(sys_q);
428fcf3ce44SJohn Forte 		switch (msg->id) {
429fcf3ce44SJohn Forte 			case DATA_ADD:
430fcf3ce44SJohn Forte 			case DATA_UPDATE:
431fcf3ce44SJohn Forte 			case DATA_DELETE:
432fcf3ce44SJohn Forte 			case DATA_DELETE_ASSOC:
433fcf3ce44SJohn Forte 			case DATA_COMMIT:
434fcf3ce44SJohn Forte 			case DATA_RETREAT:
435fcf3ce44SJohn Forte 				break;
436fcf3ce44SJohn Forte 			case REG_EXP:
437fcf3ce44SJohn Forte 				/* registration expiring */
438fcf3ce44SJohn Forte 				reg_expiring(msg->data);
439fcf3ce44SJohn Forte 				break;
440fcf3ce44SJohn Forte 			case DEAD_PORTAL:
441fcf3ce44SJohn Forte 				portal_dies((uint32_t)msg->data);
442fcf3ce44SJohn Forte 				break;
443fcf3ce44SJohn Forte 			case SERVER_EXIT:
444fcf3ce44SJohn Forte 				/* graceful exit. */
445fcf3ce44SJohn Forte 				(void) queue_msg_free(msg);
446fcf3ce44SJohn Forte 				isnslog(LOG_DEBUG, "main",
447fcf3ce44SJohn Forte 				    "wake up ESI and stop it.");
448fcf3ce44SJohn Forte 				(void) get_stopwatch(1);
449fcf3ce44SJohn Forte 				isnslog(LOG_DEBUG, "main",
450fcf3ce44SJohn Forte 				    "sending SCN stop msg.");
451fcf3ce44SJohn Forte 				(void) queue_msg_set(scn_q, SCN_STOP, NULL);
452*3f1da666Swl 				if (door_created) {
453*3f1da666Swl 					isnslog(LOG_DEBUG, "main",
454*3f1da666Swl 					    "closing the door.");
455*3f1da666Swl 					(void) fdetach(ISNS_DOOR_NAME);
456*3f1da666Swl 				}
457fcf3ce44SJohn Forte 				(void) pthread_join(esi_tid, NULL);
458fcf3ce44SJohn Forte 				isnslog(LOG_DEBUG, "main",
459fcf3ce44SJohn Forte 				    "esi thread %d exited.", esi_tid);
460fcf3ce44SJohn Forte 				(void) pthread_join(port_tid, NULL);
461fcf3ce44SJohn Forte 				isnslog(LOG_DEBUG, "main",
462fcf3ce44SJohn Forte 				    "port watcher thread %d exited.", port_tid);
463fcf3ce44SJohn Forte 				(void) pthread_join(scn_tid, NULL);
464fcf3ce44SJohn Forte 				isnslog(LOG_DEBUG, "main",
465fcf3ce44SJohn Forte 				    "scn thread %d exited.", scn_tid);
466fcf3ce44SJohn Forte 
467fcf3ce44SJohn Forte 				/* now check any remaining threads. */
468fcf3ce44SJohn Forte 				i = 0;
469fcf3ce44SJohn Forte 				do {
470fcf3ce44SJohn Forte 					thr_cnt = get_thr_count();
471fcf3ce44SJohn Forte 					if (thr_cnt == 0) {
472fcf3ce44SJohn Forte 						isnslog(LOG_DEBUG, "main",
473fcf3ce44SJohn Forte 						    "main thread %d is done.",
474fcf3ce44SJohn Forte 						    pthread_self());
475fcf3ce44SJohn Forte 						exit(1);
476fcf3ce44SJohn Forte 					} else {
477fcf3ce44SJohn Forte 						(void) sleep(1);
478fcf3ce44SJohn Forte 						i++;
479fcf3ce44SJohn Forte 					}
480fcf3ce44SJohn Forte 				} while (MAX_RETRY_COUNT > i);
481fcf3ce44SJohn Forte 				isnslog(LOG_DEBUG, "main",
482fcf3ce44SJohn Forte 				    "main thread %d existing ...",
483fcf3ce44SJohn Forte 				    pthread_self());
484fcf3ce44SJohn Forte 				exit(1);
485fcf3ce44SJohn Forte 				break;
486fcf3ce44SJohn Forte 			case CONFIG_RELOAD:
487fcf3ce44SJohn Forte 				/* load config again. don't pick data store. */
488fcf3ce44SJohn Forte 				(void) load_config(B_FALSE);
489fcf3ce44SJohn Forte 				break;
490fcf3ce44SJohn Forte 			case SYS_QUIT_OK:
491fcf3ce44SJohn Forte 				(void) queue_msg_free(msg);
492fcf3ce44SJohn Forte 				exit(0);
493fcf3ce44SJohn Forte 			default:
494fcf3ce44SJohn Forte 				break;
495fcf3ce44SJohn Forte 		}
496fcf3ce44SJohn Forte 		(void) queue_msg_free(msg);
497fcf3ce44SJohn Forte 	}
498fcf3ce44SJohn Forte 
499fcf3ce44SJohn Forte 	/* LINTED E_STMT_NOT_REACHED */
500fcf3ce44SJohn Forte 	return (0);
501fcf3ce44SJohn Forte }
502