xref: /illumos-gate/usr/src/cmd/isns/isnsd/config.c (revision fcf3ce44)
1*fcf3ce44SJohn Forte /*
2*fcf3ce44SJohn Forte  * CDDL HEADER START
3*fcf3ce44SJohn Forte  *
4*fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5*fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6*fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7*fcf3ce44SJohn Forte  *
8*fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10*fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11*fcf3ce44SJohn Forte  * and limitations under the License.
12*fcf3ce44SJohn Forte  *
13*fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14*fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16*fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17*fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18*fcf3ce44SJohn Forte  *
19*fcf3ce44SJohn Forte  * CDDL HEADER END
20*fcf3ce44SJohn Forte  */
21*fcf3ce44SJohn Forte 
22*fcf3ce44SJohn Forte /*
23*fcf3ce44SJohn Forte  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*fcf3ce44SJohn Forte  * Use is subject to license terms.
25*fcf3ce44SJohn Forte  */
26*fcf3ce44SJohn Forte 
27*fcf3ce44SJohn Forte #include <stdlib.h>
28*fcf3ce44SJohn Forte #include <string.h>
29*fcf3ce44SJohn Forte #include <libscf.h>
30*fcf3ce44SJohn Forte #include <pthread.h>
31*fcf3ce44SJohn Forte 
32*fcf3ce44SJohn Forte #include "isns_server.h"
33*fcf3ce44SJohn Forte #include "isns_log.h"
34*fcf3ce44SJohn Forte #include "isns_cfg.h"
35*fcf3ce44SJohn Forte 
36*fcf3ce44SJohn Forte /*
37*fcf3ce44SJohn Forte  * external variables
38*fcf3ce44SJohn Forte  */
39*fcf3ce44SJohn Forte extern uint64_t esi_threshold;
40*fcf3ce44SJohn Forte extern uint8_t mgmt_scn;
41*fcf3ce44SJohn Forte extern ctrl_node_t *control_nodes;
42*fcf3ce44SJohn Forte extern pthread_mutex_t ctrl_node_mtx;
43*fcf3ce44SJohn Forte extern char data_store[MAXPATHLEN];
44*fcf3ce44SJohn Forte 
45*fcf3ce44SJohn Forte #define	DEFAULT_ESI_THRESHOLD	3
46*fcf3ce44SJohn Forte #define	MAX_ESI_THRESHOLD	10
47*fcf3ce44SJohn Forte 
48*fcf3ce44SJohn Forte /*
49*fcf3ce44SJohn Forte  * load_config loads config data through SMF.
50*fcf3ce44SJohn Forte  * arg DATA_STORE_UPDATE indicates whether the data store location
51*fcf3ce44SJohn Forte  * can be updated or not.
52*fcf3ce44SJohn Forte  */
53*fcf3ce44SJohn Forte int
load_config(boolean_t DATA_STORE_UPDATE)54*fcf3ce44SJohn Forte load_config(boolean_t DATA_STORE_UPDATE)
55*fcf3ce44SJohn Forte {
56*fcf3ce44SJohn Forte 
57*fcf3ce44SJohn Forte 	int retval = -1;
58*fcf3ce44SJohn Forte 
59*fcf3ce44SJohn Forte 	scf_handle_t	*handle = NULL;
60*fcf3ce44SJohn Forte 	scf_scope_t	*sc = NULL;
61*fcf3ce44SJohn Forte 	scf_service_t	*svc = NULL;
62*fcf3ce44SJohn Forte 	scf_propertygroup_t	*pg = NULL;
63*fcf3ce44SJohn Forte 	scf_property_t	*prop = NULL;
64*fcf3ce44SJohn Forte 	scf_value_t	*value = NULL;
65*fcf3ce44SJohn Forte 	scf_iter_t	*value_iter = NULL;
66*fcf3ce44SJohn Forte 
67*fcf3ce44SJohn Forte 	ctrl_node_t *ctrl_node_p;
68*fcf3ce44SJohn Forte 	char scf_name[MAXNAMELEN];
69*fcf3ce44SJohn Forte 	char *name;
70*fcf3ce44SJohn Forte 
71*fcf3ce44SJohn Forte 	/* connect to the current SMF global repository */
72*fcf3ce44SJohn Forte 	handle = scf_handle_create(SCF_VERSION);
73*fcf3ce44SJohn Forte 
74*fcf3ce44SJohn Forte 	/* allocate scf resources */
75*fcf3ce44SJohn Forte 	sc = scf_scope_create(handle);
76*fcf3ce44SJohn Forte 	svc = scf_service_create(handle);
77*fcf3ce44SJohn Forte 	pg = scf_pg_create(handle);
78*fcf3ce44SJohn Forte 	prop = scf_property_create(handle);
79*fcf3ce44SJohn Forte 	value = scf_value_create(handle);
80*fcf3ce44SJohn Forte 	value_iter = scf_iter_create(handle);
81*fcf3ce44SJohn Forte 
82*fcf3ce44SJohn Forte 	/* if failed to allocate resources, exit */
83*fcf3ce44SJohn Forte 	if (handle == NULL || sc == NULL || svc == NULL || pg == NULL ||
84*fcf3ce44SJohn Forte 	    prop == NULL || value == NULL || value_iter == NULL) {
85*fcf3ce44SJohn Forte 		isnslog(LOG_DEBUG, "load_config",
86*fcf3ce44SJohn Forte 		    "scf handles allocation failed.");
87*fcf3ce44SJohn Forte 		goto out;
88*fcf3ce44SJohn Forte 	}
89*fcf3ce44SJohn Forte 
90*fcf3ce44SJohn Forte 	/* bind scf handle to the running svc.configd daemon */
91*fcf3ce44SJohn Forte 	if (scf_handle_bind(handle) == -1) {
92*fcf3ce44SJohn Forte 		isnslog(LOG_DEBUG, "load_config", "scf binding failed.");
93*fcf3ce44SJohn Forte 		goto out;
94*fcf3ce44SJohn Forte 	}
95*fcf3ce44SJohn Forte 
96*fcf3ce44SJohn Forte 	/* get the scope of the localhost in the current repository */
97*fcf3ce44SJohn Forte 	if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, sc) == -1) {
98*fcf3ce44SJohn Forte 		isnslog(LOG_DEBUG, "load_config", "Getting scf scope failed.");
99*fcf3ce44SJohn Forte 		goto out;
100*fcf3ce44SJohn Forte 	}
101*fcf3ce44SJohn Forte 
102*fcf3ce44SJohn Forte 	/* get the service "network/isns_server" within the scope */
103*fcf3ce44SJohn Forte 	if (scf_scope_get_service(sc, ISNS_SERVER_SVC_NAME, svc) == -1) {
104*fcf3ce44SJohn Forte 		isnslog(LOG_DEBUG, "load_config", "Getting %s service failed.",
105*fcf3ce44SJohn Forte 		    ISNS_SERVER_SVC_NAME);
106*fcf3ce44SJohn Forte 		goto out;
107*fcf3ce44SJohn Forte 	}
108*fcf3ce44SJohn Forte 
109*fcf3ce44SJohn Forte 	/* get the property group "config" within the given service */
110*fcf3ce44SJohn Forte 	if (scf_service_get_pg(svc, ISNS_SERVER_CONFIG, pg) == -1) {
111*fcf3ce44SJohn Forte 		isnslog(LOG_DEBUG, "load_config",
112*fcf3ce44SJohn Forte 		    "Getting property group %s failed.",
113*fcf3ce44SJohn Forte 		    ISNS_SERVER_CONFIG);
114*fcf3ce44SJohn Forte 		goto out;
115*fcf3ce44SJohn Forte 	}
116*fcf3ce44SJohn Forte 
117*fcf3ce44SJohn Forte 	/*
118*fcf3ce44SJohn Forte 	 * Now get the properties.
119*fcf3ce44SJohn Forte 	 */
120*fcf3ce44SJohn Forte 	if (scf_pg_get_property(pg, CONFIG_ESI_THRESHOLD, prop) == -1) {
121*fcf3ce44SJohn Forte 		isnslog(LOG_DEBUG, "load_config", "Getting property %s failed",
122*fcf3ce44SJohn Forte 		    CONFIG_ESI_THRESHOLD);
123*fcf3ce44SJohn Forte 		goto out;
124*fcf3ce44SJohn Forte 	}
125*fcf3ce44SJohn Forte 
126*fcf3ce44SJohn Forte 	if (scf_property_get_value(prop, value) == -1) {
127*fcf3ce44SJohn Forte 		isnslog(LOG_DEBUG, "load_config",
128*fcf3ce44SJohn Forte 		    "Getting property value for %s failed.",
129*fcf3ce44SJohn Forte 		    CONFIG_ESI_THRESHOLD);
130*fcf3ce44SJohn Forte 		goto out;
131*fcf3ce44SJohn Forte 	}
132*fcf3ce44SJohn Forte 
133*fcf3ce44SJohn Forte 	if (scf_value_get_count(value, &esi_threshold) == -1) {
134*fcf3ce44SJohn Forte 		isnslog(LOG_DEBUG, "load_config",
135*fcf3ce44SJohn Forte 		    "Getting property integer value for %s failed.",
136*fcf3ce44SJohn Forte 		    CONFIG_ESI_THRESHOLD);
137*fcf3ce44SJohn Forte 			goto out;
138*fcf3ce44SJohn Forte 	}
139*fcf3ce44SJohn Forte 
140*fcf3ce44SJohn Forte 	/* the range of ESI Threshold is [1, 10] */
141*fcf3ce44SJohn Forte 	if (esi_threshold < 1) {
142*fcf3ce44SJohn Forte 		esi_threshold = DEFAULT_ESI_THRESHOLD; /* 3 */
143*fcf3ce44SJohn Forte 	} else if (esi_threshold > MAX_ESI_THRESHOLD) {
144*fcf3ce44SJohn Forte 		esi_threshold = MAX_ESI_THRESHOLD; /* 10 */
145*fcf3ce44SJohn Forte 	}
146*fcf3ce44SJohn Forte 
147*fcf3ce44SJohn Forte 	isnslog(LOG_DEBUG, "load_config",
148*fcf3ce44SJohn Forte 	    "%s set to %d", CONFIG_ESI_THRESHOLD, esi_threshold);
149*fcf3ce44SJohn Forte 
150*fcf3ce44SJohn Forte 	if (scf_pg_get_property(pg, CONFIG_MGMT_SCN, prop) == -1) {
151*fcf3ce44SJohn Forte 		isnslog(LOG_DEBUG, "load_config",
152*fcf3ce44SJohn Forte 		    "Getting scf property %s failed.", CONFIG_MGMT_SCN);
153*fcf3ce44SJohn Forte 			goto out;
154*fcf3ce44SJohn Forte 	}
155*fcf3ce44SJohn Forte 
156*fcf3ce44SJohn Forte 	if (scf_property_get_value(prop, value) == -1) {
157*fcf3ce44SJohn Forte 		isnslog(LOG_DEBUG, "load_config",
158*fcf3ce44SJohn Forte 		    "Getting property value for %s failed.",
159*fcf3ce44SJohn Forte 		    CONFIG_MGMT_SCN);
160*fcf3ce44SJohn Forte 		goto out;
161*fcf3ce44SJohn Forte 	}
162*fcf3ce44SJohn Forte 
163*fcf3ce44SJohn Forte 	if (scf_value_get_boolean(value, &mgmt_scn) == -1) {
164*fcf3ce44SJohn Forte 		isnslog(LOG_DEBUG, "load_config",
165*fcf3ce44SJohn Forte 		    "Getting boolean value for property %s failed",
166*fcf3ce44SJohn Forte 		    CONFIG_MGMT_SCN);
167*fcf3ce44SJohn Forte 		goto out;
168*fcf3ce44SJohn Forte 	}
169*fcf3ce44SJohn Forte 	isnslog(LOG_DEBUG, "load_config",
170*fcf3ce44SJohn Forte 	    "%s set to %s", CONFIG_MGMT_SCN,
171*fcf3ce44SJohn Forte 	    mgmt_scn ? "true" : "false");
172*fcf3ce44SJohn Forte 
173*fcf3ce44SJohn Forte 	if (DATA_STORE_UPDATE) {
174*fcf3ce44SJohn Forte 	    if (scf_pg_get_property(pg, CONFIG_DATA_STORE, prop) == -1) {
175*fcf3ce44SJohn Forte 		isnslog(LOG_DEBUG, "load_config", "Getting property %s failed",
176*fcf3ce44SJohn Forte 		    CONFIG_DATA_STORE);
177*fcf3ce44SJohn Forte 		goto out;
178*fcf3ce44SJohn Forte 	    }
179*fcf3ce44SJohn Forte 
180*fcf3ce44SJohn Forte 	    if (scf_property_get_value(prop, value) == -1) {
181*fcf3ce44SJohn Forte 		isnslog(LOG_DEBUG, "load_config",
182*fcf3ce44SJohn Forte 		    "Getting property value for %s failed",
183*fcf3ce44SJohn Forte 		    CONFIG_DATA_STORE);
184*fcf3ce44SJohn Forte 		goto out;
185*fcf3ce44SJohn Forte 	    }
186*fcf3ce44SJohn Forte 
187*fcf3ce44SJohn Forte 	    data_store[0] = 0;
188*fcf3ce44SJohn Forte 	    if (scf_value_get_astring(value, data_store, MAXPATHLEN) == -1) {
189*fcf3ce44SJohn Forte 		isnslog(LOG_DEBUG, "load_config",
190*fcf3ce44SJohn Forte 		    "Getting property string value for %s failed",
191*fcf3ce44SJohn Forte 		    CONFIG_DATA_STORE);
192*fcf3ce44SJohn Forte 		goto out;
193*fcf3ce44SJohn Forte 	    }
194*fcf3ce44SJohn Forte 	    isnslog(LOG_DEBUG, "load_config",
195*fcf3ce44SJohn Forte 		"%s set to %s", CONFIG_DATA_STORE, data_store);
196*fcf3ce44SJohn Forte 	}
197*fcf3ce44SJohn Forte 
198*fcf3ce44SJohn Forte 	if (scf_pg_get_property(pg, CONFIG_CONTROL_NODES, prop) == -1) {
199*fcf3ce44SJohn Forte 		isnslog(LOG_DEBUG, "load_config", "Getting property %s failed",
200*fcf3ce44SJohn Forte 		    CONFIG_CONTROL_NODES);
201*fcf3ce44SJohn Forte 		goto out;
202*fcf3ce44SJohn Forte 	}
203*fcf3ce44SJohn Forte 
204*fcf3ce44SJohn Forte 	if (scf_iter_property_values(value_iter, prop) == -1) {
205*fcf3ce44SJohn Forte 		isnslog(LOG_DEBUG, "load_config",
206*fcf3ce44SJohn Forte 		    "Getting iteration property %s failed",
207*fcf3ce44SJohn Forte 		    CONFIG_CONTROL_NODES);
208*fcf3ce44SJohn Forte 		goto out;
209*fcf3ce44SJohn Forte 	}
210*fcf3ce44SJohn Forte 
211*fcf3ce44SJohn Forte 	/* remove any old control node first. */
212*fcf3ce44SJohn Forte 	(void) pthread_mutex_lock(&ctrl_node_mtx);
213*fcf3ce44SJohn Forte 	while (control_nodes != NULL) {
214*fcf3ce44SJohn Forte 	    ctrl_node_p = control_nodes->next;
215*fcf3ce44SJohn Forte 	    free(control_nodes->name);
216*fcf3ce44SJohn Forte 	    free(control_nodes);
217*fcf3ce44SJohn Forte 	    control_nodes = ctrl_node_p;
218*fcf3ce44SJohn Forte 	}
219*fcf3ce44SJohn Forte 
220*fcf3ce44SJohn Forte 	while (scf_iter_next_value(value_iter, value) != 0) {
221*fcf3ce44SJohn Forte 		if (scf_value_get_ustring(value, scf_name, MAXNAMELEN) == -1) {
222*fcf3ce44SJohn Forte 			isnslog(LOG_DEBUG, "load_config",
223*fcf3ce44SJohn Forte 			    "Getting property string value for %s failed",
224*fcf3ce44SJohn Forte 			    CONFIG_CONTROL_NODES);
225*fcf3ce44SJohn Forte 			(void) pthread_mutex_unlock(&ctrl_node_mtx);
226*fcf3ce44SJohn Forte 			goto out;
227*fcf3ce44SJohn Forte 		}
228*fcf3ce44SJohn Forte 		ctrl_node_p = (ctrl_node_t *)malloc(sizeof (ctrl_node_t));
229*fcf3ce44SJohn Forte 		if (ctrl_node_p == NULL) {
230*fcf3ce44SJohn Forte 		    isnslog(LOG_DEBUG, "load_config", "malloc() failed.");
231*fcf3ce44SJohn Forte 		    (void) pthread_mutex_unlock(&ctrl_node_mtx);
232*fcf3ce44SJohn Forte 		    goto out;
233*fcf3ce44SJohn Forte 		}
234*fcf3ce44SJohn Forte 		if (strlen(scf_name) != 0) {
235*fcf3ce44SJohn Forte 		    name = (char *)malloc(strlen(scf_name) + 1);
236*fcf3ce44SJohn Forte 		    if (name == NULL) {
237*fcf3ce44SJohn Forte 			free(ctrl_node_p);
238*fcf3ce44SJohn Forte 			isnslog(LOG_DEBUG, "load_config", "malloc() failed.");
239*fcf3ce44SJohn Forte 			(void) pthread_mutex_unlock(&ctrl_node_mtx);
240*fcf3ce44SJohn Forte 			goto out;
241*fcf3ce44SJohn Forte 		    } else {
242*fcf3ce44SJohn Forte 			(void) strcpy(name, scf_name);
243*fcf3ce44SJohn Forte 			ctrl_node_p->name = (uchar_t *)name;
244*fcf3ce44SJohn Forte 			ctrl_node_p->next = control_nodes;
245*fcf3ce44SJohn Forte 			control_nodes = ctrl_node_p;
246*fcf3ce44SJohn Forte 		    }
247*fcf3ce44SJohn Forte 		    isnslog(LOG_DEBUG, "load_config",
248*fcf3ce44SJohn Forte 			"%s set to %s", CONFIG_CONTROL_NODES, scf_name);
249*fcf3ce44SJohn Forte 		} else {
250*fcf3ce44SJohn Forte 		    free(ctrl_node_p);
251*fcf3ce44SJohn Forte 		}
252*fcf3ce44SJohn Forte 	}
253*fcf3ce44SJohn Forte 	(void) pthread_mutex_unlock(&ctrl_node_mtx);
254*fcf3ce44SJohn Forte 
255*fcf3ce44SJohn Forte 	isnslog(LOG_DEBUG, "load_config", "loading server settings ok.");
256*fcf3ce44SJohn Forte 
257*fcf3ce44SJohn Forte 	retval = 0; /* ok */
258*fcf3ce44SJohn Forte 
259*fcf3ce44SJohn Forte out:
260*fcf3ce44SJohn Forte 	/* destroy scf pointers */
261*fcf3ce44SJohn Forte 	if (value != NULL) {
262*fcf3ce44SJohn Forte 		scf_value_destroy(value);
263*fcf3ce44SJohn Forte 	}
264*fcf3ce44SJohn Forte 	if (value_iter != NULL) {
265*fcf3ce44SJohn Forte 		scf_iter_destroy(value_iter);
266*fcf3ce44SJohn Forte 	}
267*fcf3ce44SJohn Forte 	if (prop != NULL) {
268*fcf3ce44SJohn Forte 		scf_property_destroy(prop);
269*fcf3ce44SJohn Forte 	}
270*fcf3ce44SJohn Forte 	if (pg != NULL) {
271*fcf3ce44SJohn Forte 		scf_pg_destroy(pg);
272*fcf3ce44SJohn Forte 	}
273*fcf3ce44SJohn Forte 	if (svc != NULL) {
274*fcf3ce44SJohn Forte 		scf_service_destroy(svc);
275*fcf3ce44SJohn Forte 	}
276*fcf3ce44SJohn Forte 	if (sc != NULL) {
277*fcf3ce44SJohn Forte 		scf_scope_destroy(sc);
278*fcf3ce44SJohn Forte 	}
279*fcf3ce44SJohn Forte 	if (handle != NULL) {
280*fcf3ce44SJohn Forte 		scf_handle_destroy(handle);
281*fcf3ce44SJohn Forte 	}
282*fcf3ce44SJohn Forte 
283*fcf3ce44SJohn Forte 	return (retval);
284*fcf3ce44SJohn Forte }
285*fcf3ce44SJohn Forte 
286*fcf3ce44SJohn Forte /*
287*fcf3ce44SJohn Forte  * is_control_node checks the given name to see if it is a control node.
288*fcf3ce44SJohn Forte  */
289*fcf3ce44SJohn Forte int
is_control_node(uchar_t * name)290*fcf3ce44SJohn Forte is_control_node(
291*fcf3ce44SJohn Forte 	uchar_t *name
292*fcf3ce44SJohn Forte )
293*fcf3ce44SJohn Forte {
294*fcf3ce44SJohn Forte 	ctrl_node_t *p;
295*fcf3ce44SJohn Forte 
296*fcf3ce44SJohn Forte 	(void) pthread_mutex_lock(&ctrl_node_mtx);
297*fcf3ce44SJohn Forte 	p = control_nodes;
298*fcf3ce44SJohn Forte 	while (p != NULL) {
299*fcf3ce44SJohn Forte 		if (strcmp((char *)p->name, (char *)name) == 0) {
300*fcf3ce44SJohn Forte 		    (void) pthread_mutex_unlock(&ctrl_node_mtx);
301*fcf3ce44SJohn Forte 		    return (1);
302*fcf3ce44SJohn Forte 		}
303*fcf3ce44SJohn Forte 		p = p->next;
304*fcf3ce44SJohn Forte 	}
305*fcf3ce44SJohn Forte 	(void) pthread_mutex_unlock(&ctrl_node_mtx);
306*fcf3ce44SJohn Forte 
307*fcf3ce44SJohn Forte 	return (0);
308*fcf3ce44SJohn Forte }
309