xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.lib/inetd/config.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Routines used by inetd to read inetd's configuration from the repository,
31*0Sstevel@tonic-gate  * to validate it and setup inetd's data structures appropriately based on
32*0Sstevel@tonic-gate  * in.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate #include <string.h>
37*0Sstevel@tonic-gate #include <errno.h>
38*0Sstevel@tonic-gate #include <unistd.h>
39*0Sstevel@tonic-gate #include <netdb.h>
40*0Sstevel@tonic-gate #include <netinet/in.h>
41*0Sstevel@tonic-gate #include <libintl.h>
42*0Sstevel@tonic-gate #include <nss_dbdefs.h>
43*0Sstevel@tonic-gate #include <signal.h>
44*0Sstevel@tonic-gate #include <wait.h>
45*0Sstevel@tonic-gate #include "inetd_impl.h"
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate /* method timeout used if one isn't explicitly specified */
49*0Sstevel@tonic-gate #define	DEFAULT_METHOD_TIMEOUT	10
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /* supported method properties and their attributes */
53*0Sstevel@tonic-gate static inetd_prop_t method_props[] = {
54*0Sstevel@tonic-gate {PR_EXEC_NAME, "", SCF_TYPE_ASTRING, B_FALSE, IVE_UNSET, NULL},
55*0Sstevel@tonic-gate {PR_ARG0_NAME, "", SCF_TYPE_ASTRING, B_TRUE, IVE_UNSET, NULL},
56*0Sstevel@tonic-gate {NULL, "", SCF_TYPE_COUNT, B_TRUE, IVE_UNSET, NULL}
57*0Sstevel@tonic-gate };
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate /* enumeration of method properties; used to index into method_props[] */
60*0Sstevel@tonic-gate typedef enum {
61*0Sstevel@tonic-gate 	MP_EXEC,
62*0Sstevel@tonic-gate 	MP_ARG0,
63*0Sstevel@tonic-gate 	MP_TIMEOUT,
64*0Sstevel@tonic-gate 	NUM_METHOD_PROPS
65*0Sstevel@tonic-gate } method_prop_t;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /* handle used for repository access in read_prop() */
69*0Sstevel@tonic-gate static scf_handle_t	*rep_handle = NULL;
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate /* pool used to create proto_info_t lists (generic proto info structure) */
72*0Sstevel@tonic-gate static uu_list_pool_t	*proto_info_pool = NULL;
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate static void destroy_method_props(inetd_prop_t *);
75*0Sstevel@tonic-gate static int proto_info_compare(const void *, const void *, void *);
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate int
78*0Sstevel@tonic-gate config_init(void)
79*0Sstevel@tonic-gate {
80*0Sstevel@tonic-gate 	if ((rep_handle = scf_handle_create(SCF_VERSION)) == NULL) {
81*0Sstevel@tonic-gate 		error_msg("%s: %s",
82*0Sstevel@tonic-gate 		    gettext("Failed to create repository handle"),
83*0Sstevel@tonic-gate 		    scf_strerror(scf_error()));
84*0Sstevel@tonic-gate 		return (-1);
85*0Sstevel@tonic-gate 	} else if (make_handle_bound(rep_handle) == -1) {
86*0Sstevel@tonic-gate 		/* let config_fini clean-up */
87*0Sstevel@tonic-gate 		return (-1);
88*0Sstevel@tonic-gate 	}
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	/*
91*0Sstevel@tonic-gate 	 * Work around the (const *) nature of SCF property #defines in
92*0Sstevel@tonic-gate 	 * libscf.h that prevent us from directly initializing the name
93*0Sstevel@tonic-gate 	 * element of members of the method properties table.
94*0Sstevel@tonic-gate 	 */
95*0Sstevel@tonic-gate 	if ((method_props[MP_TIMEOUT].ip_name = strdup(SCF_PROPERTY_TIMEOUT))
96*0Sstevel@tonic-gate 	    == NULL) {
97*0Sstevel@tonic-gate 		error_msg(strerror(errno));
98*0Sstevel@tonic-gate 		return (-1);
99*0Sstevel@tonic-gate 	}
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	if ((proto_info_pool = uu_list_pool_create("proto_info_pool",
102*0Sstevel@tonic-gate 	    sizeof (proto_info_t), offsetof(proto_info_t, link),
103*0Sstevel@tonic-gate 	    proto_info_compare, UU_LIST_POOL_DEBUG)) == NULL) {
104*0Sstevel@tonic-gate 		error_msg(gettext("Failed to create uu list pool: %s"),
105*0Sstevel@tonic-gate 		    uu_strerror(uu_error()));
106*0Sstevel@tonic-gate 		return (-1);
107*0Sstevel@tonic-gate 	}
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	return (0);
110*0Sstevel@tonic-gate }
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate void
113*0Sstevel@tonic-gate config_fini(void)
114*0Sstevel@tonic-gate {
115*0Sstevel@tonic-gate 	if (rep_handle == NULL)
116*0Sstevel@tonic-gate 		return;
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	if (proto_info_pool != NULL) {
119*0Sstevel@tonic-gate 		uu_list_pool_destroy(proto_info_pool);
120*0Sstevel@tonic-gate 		proto_info_pool = NULL;
121*0Sstevel@tonic-gate 	}
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	(void) scf_handle_unbind(rep_handle);
124*0Sstevel@tonic-gate 	scf_handle_destroy(rep_handle);
125*0Sstevel@tonic-gate 	rep_handle = NULL;
126*0Sstevel@tonic-gate }
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate static void
129*0Sstevel@tonic-gate destroy_method_info(method_info_t *mi)
130*0Sstevel@tonic-gate {
131*0Sstevel@tonic-gate 	if (mi == NULL)
132*0Sstevel@tonic-gate 		return;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	if (mi->wordexp_arg0_backup != NULL) {
135*0Sstevel@tonic-gate 		/*
136*0Sstevel@tonic-gate 		 * Return the wordexp structure back to its original
137*0Sstevel@tonic-gate 		 * state so it can be consumed by wordfree.
138*0Sstevel@tonic-gate 		 */
139*0Sstevel@tonic-gate 		free(mi->exec_args_we.we_wordv[0]);
140*0Sstevel@tonic-gate 		mi->exec_args_we.we_wordv[0] =
141*0Sstevel@tonic-gate 		    (char *)mi->wordexp_arg0_backup;
142*0Sstevel@tonic-gate 	}
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	free(mi->exec_path);
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	wordfree(&mi->exec_args_we);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	free(mi);
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate /*
152*0Sstevel@tonic-gate  * Transforms the properties read from the repository for a method into a
153*0Sstevel@tonic-gate  * method_info_t and returns a pointer to it. If expansion of the exec
154*0Sstevel@tonic-gate  * property fails, due to an invalid string or memory allocation failure,
155*0Sstevel@tonic-gate  * NULL is returned and exec_invalid is set appropriately to indicate whether
156*0Sstevel@tonic-gate  * it was a memory allocation failure or an invalid exec string.
157*0Sstevel@tonic-gate  */
158*0Sstevel@tonic-gate static method_info_t *
159*0Sstevel@tonic-gate create_method_info(const inetd_prop_t *mprops, boolean_t *exec_invalid)
160*0Sstevel@tonic-gate {
161*0Sstevel@tonic-gate 	method_info_t	*ret;
162*0Sstevel@tonic-gate 	int		i;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	debug_msg("Entering create_method_info");
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	if ((ret = calloc(1, sizeof (method_info_t))) == NULL)
167*0Sstevel@tonic-gate 		goto alloc_fail;
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	/* Expand the exec string. */
170*0Sstevel@tonic-gate 	if ((i = wordexp(get_prop_value(mprops, PR_EXEC_NAME),
171*0Sstevel@tonic-gate 	    &ret->exec_args_we, WRDE_NOCMD|WRDE_UNDEF)) != 0) {
172*0Sstevel@tonic-gate 		if (i == WRDE_NOSPACE)
173*0Sstevel@tonic-gate 			goto alloc_fail;
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 		*exec_invalid = B_TRUE;
176*0Sstevel@tonic-gate 		free(ret);
177*0Sstevel@tonic-gate 		return (NULL);
178*0Sstevel@tonic-gate 	}
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	if ((ret->exec_path = strdup(ret->exec_args_we.we_wordv[0])) == NULL)
181*0Sstevel@tonic-gate 		goto alloc_fail;
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	if (mprops[MP_ARG0].ip_error == IVE_VALID) {	/* arg0 is set */
184*0Sstevel@tonic-gate 		/*
185*0Sstevel@tonic-gate 		 * Keep a copy of arg0 of the wordexp structure so that
186*0Sstevel@tonic-gate 		 * wordfree() gets passed what wordexp() originally returned,
187*0Sstevel@tonic-gate 		 * as documented as required in the man page.
188*0Sstevel@tonic-gate 		 */
189*0Sstevel@tonic-gate 		ret->wordexp_arg0_backup = ret->exec_args_we.we_wordv[0];
190*0Sstevel@tonic-gate 		if ((ret->exec_args_we.we_wordv[0] =
191*0Sstevel@tonic-gate 		    strdup(get_prop_value(mprops, PR_ARG0_NAME))) == NULL)
192*0Sstevel@tonic-gate 			goto alloc_fail;
193*0Sstevel@tonic-gate 	}
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	if (mprops[MP_TIMEOUT].ip_error == IVE_VALID) {
196*0Sstevel@tonic-gate 		ret->timeout = *(int64_t *)get_prop_value(mprops,
197*0Sstevel@tonic-gate 		    (char *)SCF_PROPERTY_TIMEOUT);
198*0Sstevel@tonic-gate 	} else {
199*0Sstevel@tonic-gate 		ret->timeout = DEFAULT_METHOD_TIMEOUT;
200*0Sstevel@tonic-gate 	}
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	/* exec_invalid not set on success */
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	return (ret);
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate alloc_fail:
207*0Sstevel@tonic-gate 	error_msg(strerror(errno));
208*0Sstevel@tonic-gate 	destroy_method_info(ret);
209*0Sstevel@tonic-gate 	*exec_invalid = B_FALSE;
210*0Sstevel@tonic-gate 	return (NULL);
211*0Sstevel@tonic-gate }
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate /*
214*0Sstevel@tonic-gate  * Returns B_TRUE if the contents of the 2 method_info_t structures are
215*0Sstevel@tonic-gate  * equivalent, else B_FALSE.
216*0Sstevel@tonic-gate  */
217*0Sstevel@tonic-gate boolean_t
218*0Sstevel@tonic-gate method_info_equal(const method_info_t *mi, const method_info_t *mi2)
219*0Sstevel@tonic-gate {
220*0Sstevel@tonic-gate 	int		i;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	debug_msg("Entering method_info_equal");
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	if ((mi == NULL) && (mi2 == NULL)) {
225*0Sstevel@tonic-gate 		return (B_TRUE);
226*0Sstevel@tonic-gate 	} else if (((mi == NULL) || (mi2 == NULL)) ||
227*0Sstevel@tonic-gate 	    (mi->exec_args_we.we_wordc != mi2->exec_args_we.we_wordc) ||
228*0Sstevel@tonic-gate 	    (strcmp(mi->exec_path, mi2->exec_path) != 0)) {
229*0Sstevel@tonic-gate 		return (B_FALSE);
230*0Sstevel@tonic-gate 	}
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	for (i = 0; i < mi->exec_args_we.we_wordc; i++) {
233*0Sstevel@tonic-gate 		if (strcmp(mi->exec_args_we.we_wordv[i],
234*0Sstevel@tonic-gate 		    mi2->exec_args_we.we_wordv[i]) != 0) {
235*0Sstevel@tonic-gate 			return (B_FALSE);
236*0Sstevel@tonic-gate 		}
237*0Sstevel@tonic-gate 	}
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	return (B_TRUE);
240*0Sstevel@tonic-gate }
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate /*
243*0Sstevel@tonic-gate  * Checks if the contents of the 2 socket_info_t structures are equivalent.
244*0Sstevel@tonic-gate  * If 'isrpc' is false, the address components of the two structures are
245*0Sstevel@tonic-gate  * compared for equality as part of this. If the two structures are
246*0Sstevel@tonic-gate  * equivalent B_TRUE is returned, else B_FALSE.
247*0Sstevel@tonic-gate  */
248*0Sstevel@tonic-gate boolean_t
249*0Sstevel@tonic-gate socket_info_equal(const socket_info_t *si, const socket_info_t *si2,
250*0Sstevel@tonic-gate     boolean_t isrpc)
251*0Sstevel@tonic-gate {
252*0Sstevel@tonic-gate 	return ((isrpc || (memcmp(&si->local_addr, &si2->local_addr,
253*0Sstevel@tonic-gate 	    sizeof (si->local_addr)) == 0)) &&
254*0Sstevel@tonic-gate 	    (si->type == si2->type));
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate }
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate /*
259*0Sstevel@tonic-gate  * proto_info_t comparison function. Returns 0 on match, else -1, as required
260*0Sstevel@tonic-gate  * by uu_list_find().
261*0Sstevel@tonic-gate  */
262*0Sstevel@tonic-gate static int
263*0Sstevel@tonic-gate proto_info_compare(const void *lv, const void *rv, void *istlx)
264*0Sstevel@tonic-gate {
265*0Sstevel@tonic-gate 	proto_info_t	*pi = (proto_info_t *)lv;
266*0Sstevel@tonic-gate 	proto_info_t	*pi2 = (proto_info_t *)rv;
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 	/* check their RPC configuration matches */
269*0Sstevel@tonic-gate 	if (pi->ri != NULL) {
270*0Sstevel@tonic-gate 		if ((pi2->ri == NULL) || !rpc_info_equal(pi->ri, pi2->ri))
271*0Sstevel@tonic-gate 			return (-1);
272*0Sstevel@tonic-gate 	} else if (pi2->ri != NULL) {
273*0Sstevel@tonic-gate 		return (-1);
274*0Sstevel@tonic-gate 	}
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	if (pi->v6only != pi2->v6only)
277*0Sstevel@tonic-gate 		return (-1);
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	if (*(boolean_t *)istlx) {
280*0Sstevel@tonic-gate 		if (tlx_info_equal((tlx_info_t *)lv, (tlx_info_t *)rv,
281*0Sstevel@tonic-gate 		    pi->ri != NULL))
282*0Sstevel@tonic-gate 			return (0);
283*0Sstevel@tonic-gate 	} else {
284*0Sstevel@tonic-gate 		if (socket_info_equal((socket_info_t *)lv,
285*0Sstevel@tonic-gate 		    (socket_info_t *)rv, pi->ri != NULL))
286*0Sstevel@tonic-gate 			return (0);
287*0Sstevel@tonic-gate 	}
288*0Sstevel@tonic-gate 	return (-1);
289*0Sstevel@tonic-gate }
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate /*
292*0Sstevel@tonic-gate  * Returns B_TRUE if the bind configuration of the two instance_cfg_t
293*0Sstevel@tonic-gate  * structures are equivalent, else B_FALSE.
294*0Sstevel@tonic-gate  */
295*0Sstevel@tonic-gate boolean_t
296*0Sstevel@tonic-gate bind_config_equal(const basic_cfg_t *c1, const basic_cfg_t *c2)
297*0Sstevel@tonic-gate {
298*0Sstevel@tonic-gate 	proto_info_t	*pi;
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	debug_msg("Entering bind_config_equal");
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	if ((c1->iswait != c2->iswait) ||
303*0Sstevel@tonic-gate 	    (c1->istlx != c2->istlx))
304*0Sstevel@tonic-gate 		return (B_FALSE);
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 	if (uu_list_numnodes(c1->proto_list) !=
307*0Sstevel@tonic-gate 	    uu_list_numnodes(c2->proto_list))
308*0Sstevel@tonic-gate 		return (B_FALSE);
309*0Sstevel@tonic-gate 	/*
310*0Sstevel@tonic-gate 	 * For each element in the first configuration's socket/tlx list,
311*0Sstevel@tonic-gate 	 * check there's a matching one in the other list.
312*0Sstevel@tonic-gate 	 */
313*0Sstevel@tonic-gate 	for (pi = uu_list_first(c1->proto_list); pi != NULL;
314*0Sstevel@tonic-gate 	    pi = uu_list_next(c1->proto_list, pi)) {
315*0Sstevel@tonic-gate 		uu_list_index_t idx;
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 		if (uu_list_find(c2->proto_list, pi, (void *)&c1->istlx,
318*0Sstevel@tonic-gate 		    &idx) == NULL)
319*0Sstevel@tonic-gate 			return (B_FALSE);
320*0Sstevel@tonic-gate 	}
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 	return (B_TRUE);
323*0Sstevel@tonic-gate }
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate /*
326*0Sstevel@tonic-gate  * Write the default values contained in 'bprops', read by
327*0Sstevel@tonic-gate  * read_instance_props(), into 'cfg'.
328*0Sstevel@tonic-gate  * Returns -1 if memory allocation fails, else 0.
329*0Sstevel@tonic-gate  */
330*0Sstevel@tonic-gate static int
331*0Sstevel@tonic-gate populate_defaults(inetd_prop_t *bprops, basic_cfg_t *cfg)
332*0Sstevel@tonic-gate {
333*0Sstevel@tonic-gate 	debug_msg("Entering populate_defaults");
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate 	/*
336*0Sstevel@tonic-gate 	 * All time related values below are stored as 32 bits values because
337*0Sstevel@tonic-gate 	 * the consumers of the data rely on this, and so we cast them all
338*0Sstevel@tonic-gate 	 * to int's here.
339*0Sstevel@tonic-gate 	 */
340*0Sstevel@tonic-gate 	cfg->do_tcp_wrappers =
341*0Sstevel@tonic-gate 	    *(boolean_t *)get_prop_value(bprops, PR_DO_TCP_WRAPPERS_NAME);
342*0Sstevel@tonic-gate 	cfg->do_tcp_trace =
343*0Sstevel@tonic-gate 	    *(boolean_t *)get_prop_value(bprops, PR_DO_TCP_TRACE_NAME);
344*0Sstevel@tonic-gate 	cfg->inherit_env =
345*0Sstevel@tonic-gate 	    *(boolean_t *)get_prop_value(bprops, PR_INHERIT_ENV_NAME);
346*0Sstevel@tonic-gate 	cfg->wait_fail_cnt =
347*0Sstevel@tonic-gate 	    *(int64_t *)get_prop_value(bprops, PR_MAX_FAIL_RATE_CNT_NAME);
348*0Sstevel@tonic-gate 	cfg->wait_fail_interval = (int)*(int64_t *)get_prop_value(bprops,
349*0Sstevel@tonic-gate 	    PR_MAX_FAIL_RATE_INTVL_NAME);
350*0Sstevel@tonic-gate 	cfg->max_copies =
351*0Sstevel@tonic-gate 	    *(int64_t *)get_prop_value(bprops, PR_MAX_COPIES_NAME);
352*0Sstevel@tonic-gate 	cfg->conn_rate_offline =
353*0Sstevel@tonic-gate 	    (int)*(int64_t *)get_prop_value(bprops, PR_CON_RATE_OFFLINE_NAME);
354*0Sstevel@tonic-gate 	cfg->conn_rate_max =
355*0Sstevel@tonic-gate 	    *(int64_t *)get_prop_value(bprops, PR_CON_RATE_MAX_NAME);
356*0Sstevel@tonic-gate 	cfg->bind_fail_interval =
357*0Sstevel@tonic-gate 	    (int)*(int64_t *)get_prop_value(bprops, PR_BIND_FAIL_INTVL_NAME);
358*0Sstevel@tonic-gate 	cfg->bind_fail_max =
359*0Sstevel@tonic-gate 	    *(int64_t *)get_prop_value(bprops, PR_BIND_FAIL_MAX_NAME);
360*0Sstevel@tonic-gate 	if ((cfg->bind_addr =
361*0Sstevel@tonic-gate 	    strdup(get_prop_value(bprops, PR_BIND_ADDR_NAME))) == NULL) {
362*0Sstevel@tonic-gate 		error_msg(strerror(errno));
363*0Sstevel@tonic-gate 		return (-1);
364*0Sstevel@tonic-gate 	}
365*0Sstevel@tonic-gate 	return (0);
366*0Sstevel@tonic-gate }
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate void
369*0Sstevel@tonic-gate destroy_method_infos(method_info_t **mis)
370*0Sstevel@tonic-gate {
371*0Sstevel@tonic-gate 	int i;
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 	for (i = 0; i < NUM_METHODS; i++) {
374*0Sstevel@tonic-gate 		destroy_method_info(mis[i]);
375*0Sstevel@tonic-gate 		mis[i] = NULL;
376*0Sstevel@tonic-gate 	}
377*0Sstevel@tonic-gate }
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate /*
380*0Sstevel@tonic-gate  * For each method, if it was specifed convert its entry in 'mprops',
381*0Sstevel@tonic-gate  * into an entry in 'mis'. Returns -1 if memory allocation fails or one of the
382*0Sstevel@tonic-gate  * exec strings was invalid, else 0.
383*0Sstevel@tonic-gate  */
384*0Sstevel@tonic-gate static int
385*0Sstevel@tonic-gate create_method_infos(const char *fmri, inetd_prop_t **mprops,
386*0Sstevel@tonic-gate     method_info_t **mis)
387*0Sstevel@tonic-gate {
388*0Sstevel@tonic-gate 	int i;
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 	debug_msg("Entering create_method_infos, inst: %s", fmri);
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 	for (i = 0; i < NUM_METHODS; i++) {
393*0Sstevel@tonic-gate 		/*
394*0Sstevel@tonic-gate 		 * Only create a method info structure if the method properties
395*0Sstevel@tonic-gate 		 * contain an exec string, which we take to mean the method
396*0Sstevel@tonic-gate 		 * is specified.
397*0Sstevel@tonic-gate 		 */
398*0Sstevel@tonic-gate 		if (mprops[i][MP_EXEC].ip_error == IVE_VALID) {
399*0Sstevel@tonic-gate 			boolean_t exec_invalid;
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 			if ((mis[i] = create_method_info(mprops[i],
402*0Sstevel@tonic-gate 			    &exec_invalid)) == NULL) {
403*0Sstevel@tonic-gate 				if (exec_invalid) {
404*0Sstevel@tonic-gate 					error_msg(gettext("Property %s for "
405*0Sstevel@tonic-gate 					    "method %s of instance %s is "
406*0Sstevel@tonic-gate 					    "invalid"), PR_EXEC_NAME,
407*0Sstevel@tonic-gate 					    methods[i].name, fmri);
408*0Sstevel@tonic-gate 				}
409*0Sstevel@tonic-gate 				return (-1);
410*0Sstevel@tonic-gate 			}
411*0Sstevel@tonic-gate 		}
412*0Sstevel@tonic-gate 	}
413*0Sstevel@tonic-gate 	return (0);
414*0Sstevel@tonic-gate }
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate /*
417*0Sstevel@tonic-gate  * Try and read each of the method properties for the method 'method' of
418*0Sstevel@tonic-gate  * instance 'inst', and return a table containing all method properties. If an
419*0Sstevel@tonic-gate  * error occurs, NULL is returned, with 'err' set to indicate the cause.
420*0Sstevel@tonic-gate  * Otherwise, a pointer to an inetd_prop_t table is returned containing all
421*0Sstevel@tonic-gate  * the method properties, and each of the properties is flagged according to
422*0Sstevel@tonic-gate  * whether it was present or not, and if it was present its value is set in
423*0Sstevel@tonic-gate  * the property's entry in the table.
424*0Sstevel@tonic-gate  */
425*0Sstevel@tonic-gate static inetd_prop_t *
426*0Sstevel@tonic-gate read_method_props(const char *inst, instance_method_t method, scf_error_t *err)
427*0Sstevel@tonic-gate {
428*0Sstevel@tonic-gate 	inetd_prop_t	*ret;
429*0Sstevel@tonic-gate 	int		i;
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate 	debug_msg("Entering read_method_props");
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 	if ((ret = calloc(1, sizeof (method_props))) == NULL) {
434*0Sstevel@tonic-gate 		*err = SCF_ERROR_NO_MEMORY;
435*0Sstevel@tonic-gate 		return (NULL);
436*0Sstevel@tonic-gate 	}
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate 	(void) memcpy(ret, method_props, sizeof (method_props));
439*0Sstevel@tonic-gate 	for (i = 0; i < NUM_METHOD_PROPS; i++) {
440*0Sstevel@tonic-gate 		*err = read_prop(rep_handle, &ret[i], i, inst,
441*0Sstevel@tonic-gate 		    methods[method].name);
442*0Sstevel@tonic-gate 		if ((*err != 0) && (*err != SCF_ERROR_NOT_FOUND)) {
443*0Sstevel@tonic-gate 			destroy_method_props(ret);
444*0Sstevel@tonic-gate 			return (NULL);
445*0Sstevel@tonic-gate 		}
446*0Sstevel@tonic-gate 	}
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	return (ret);
449*0Sstevel@tonic-gate }
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate static void
452*0Sstevel@tonic-gate destroy_method_props(inetd_prop_t *mprop)
453*0Sstevel@tonic-gate {
454*0Sstevel@tonic-gate 	int i;
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 	if (mprop == NULL)
457*0Sstevel@tonic-gate 		return;
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate 	for (i = 0; i < NUM_METHOD_PROPS; i++) {
460*0Sstevel@tonic-gate 		if (mprop[i].ip_type == SCF_TYPE_ASTRING)
461*0Sstevel@tonic-gate 			free(mprop[i].ip_value.iv_astring);
462*0Sstevel@tonic-gate 	}
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 	free(mprop);
465*0Sstevel@tonic-gate }
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate /*
468*0Sstevel@tonic-gate  * Destroy the basic and method properties returned by read_inst_props().
469*0Sstevel@tonic-gate  */
470*0Sstevel@tonic-gate static void
471*0Sstevel@tonic-gate destroy_inst_props(inetd_prop_t *bprops, inetd_prop_t **mprops)
472*0Sstevel@tonic-gate {
473*0Sstevel@tonic-gate 	int	i;
474*0Sstevel@tonic-gate 
475*0Sstevel@tonic-gate 	free_instance_props(bprops);
476*0Sstevel@tonic-gate 	for (i = 0; i < NUM_METHODS; i++)
477*0Sstevel@tonic-gate 		destroy_method_props(mprops[i]);
478*0Sstevel@tonic-gate }
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate /*
481*0Sstevel@tonic-gate  * Read all the basic and method properties for instance 'inst', as inetd_prop_t
482*0Sstevel@tonic-gate  * tables, into the spaces referenced by 'bprops' and 'mprops' respectively.
483*0Sstevel@tonic-gate  * Each of the properties in the tables are flagged to indicate if the
484*0Sstevel@tonic-gate  * property was present or not, and if it was the value is stored within it.
485*0Sstevel@tonic-gate  * If an error occurs at any time -1 is returned and 'err' is set to
486*0Sstevel@tonic-gate  * indicate the reason, else 0 is returned.
487*0Sstevel@tonic-gate  */
488*0Sstevel@tonic-gate static int
489*0Sstevel@tonic-gate read_inst_props(const char *fmri, inetd_prop_t **bprops,
490*0Sstevel@tonic-gate     inetd_prop_t **mprops, scf_error_t *err)
491*0Sstevel@tonic-gate {
492*0Sstevel@tonic-gate 	size_t		nprops;
493*0Sstevel@tonic-gate 	int		i;
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate 	debug_msg("Entering read_inst_props");
496*0Sstevel@tonic-gate 
497*0Sstevel@tonic-gate 	if ((*bprops = read_instance_props(rep_handle, (char *)fmri, &nprops,
498*0Sstevel@tonic-gate 	    err)) == NULL)
499*0Sstevel@tonic-gate 		return (-1);
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	for (i = 0; i < NUM_METHODS; i++) {
502*0Sstevel@tonic-gate 		if ((mprops[i] =
503*0Sstevel@tonic-gate 		    read_method_props(fmri, (instance_method_t)i, err)) ==
504*0Sstevel@tonic-gate 		    NULL) {
505*0Sstevel@tonic-gate 			for (i--; i >= 0; i--)
506*0Sstevel@tonic-gate 				destroy_method_props(mprops[i]);
507*0Sstevel@tonic-gate 			free_instance_props(*bprops);
508*0Sstevel@tonic-gate 			return (-1);
509*0Sstevel@tonic-gate 		}
510*0Sstevel@tonic-gate 	}
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate 	return (0);
513*0Sstevel@tonic-gate }
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate /*
516*0Sstevel@tonic-gate  * Returns B_TRUE if all required properties were read from the repository
517*0Sstevel@tonic-gate  * (whether taken from the defaults or directly from the instance), they
518*0Sstevel@tonic-gate  * all had valid values, all the required methods were present, and they
519*0Sstevel@tonic-gate  * each had the required properties with valid values. Else, returns B_FALSE.
520*0Sstevel@tonic-gate  * If the function returns B_TRUE, the storage referenced by 'cfg' is set
521*0Sstevel@tonic-gate  * to point at an allocated instance_cfg_t initialized based on the basic
522*0Sstevel@tonic-gate  * properties (not method or defaults).
523*0Sstevel@tonic-gate  */
524*0Sstevel@tonic-gate static boolean_t
525*0Sstevel@tonic-gate valid_inst_props(const char *fmri, inetd_prop_t *bprops, inetd_prop_t **mprops,
526*0Sstevel@tonic-gate     basic_cfg_t **cfg)
527*0Sstevel@tonic-gate {
528*0Sstevel@tonic-gate 	boolean_t	valid;
529*0Sstevel@tonic-gate 	size_t		num_bprops;
530*0Sstevel@tonic-gate 	int		i;
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 	debug_msg("Entering valid_inst_props: inst: %s, bprops: %x, mprops: %x",
533*0Sstevel@tonic-gate 	    fmri, bprops, *mprops);
534*0Sstevel@tonic-gate 
535*0Sstevel@tonic-gate 	valid = valid_props(bprops, fmri, cfg, proto_info_pool, conn_ind_pool);
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 	/*
538*0Sstevel@tonic-gate 	 * Double check we've got all necessary properties (valid_props()
539*0Sstevel@tonic-gate 	 * doesn't enforce the presence of defaults), and output error messages
540*0Sstevel@tonic-gate 	 * for each invalid/ missing property.
541*0Sstevel@tonic-gate 	 */
542*0Sstevel@tonic-gate 	(void) get_prop_table(&num_bprops);
543*0Sstevel@tonic-gate 	for (i = 0; i < num_bprops; i++) {
544*0Sstevel@tonic-gate 		switch (bprops[i].ip_error) {
545*0Sstevel@tonic-gate 		case IVE_UNSET:
546*0Sstevel@tonic-gate 			if (!bprops[i].ip_default)
547*0Sstevel@tonic-gate 				continue;
548*0Sstevel@tonic-gate 			if ((i == PT_ARG0_INDEX) || (i == PT_EXEC_INDEX))
549*0Sstevel@tonic-gate 				continue;
550*0Sstevel@tonic-gate 			/* FALLTHROUGH */
551*0Sstevel@tonic-gate 		case IVE_INVALID:
552*0Sstevel@tonic-gate 			error_msg(gettext("Property '%s' of instance "
553*0Sstevel@tonic-gate 			    "%s is missing, inconsistent or invalid"),
554*0Sstevel@tonic-gate 			    bprops[i].ip_name, fmri);
555*0Sstevel@tonic-gate 			valid = B_FALSE;
556*0Sstevel@tonic-gate 		}
557*0Sstevel@tonic-gate 	}
558*0Sstevel@tonic-gate 
559*0Sstevel@tonic-gate 	for (i = 0; i < NUM_METHODS; i++) {
560*0Sstevel@tonic-gate 		int	j;
561*0Sstevel@tonic-gate 
562*0Sstevel@tonic-gate 		/* check if any properties are set */
563*0Sstevel@tonic-gate 		for (j = 0; j < NUM_METHOD_PROPS; j++) {
564*0Sstevel@tonic-gate 			if (mprops[i][j].ip_error != IVE_UNSET)
565*0Sstevel@tonic-gate 				break;
566*0Sstevel@tonic-gate 		}
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 		if (j == NUM_METHOD_PROPS) {
569*0Sstevel@tonic-gate 			/* an unspecified method */
570*0Sstevel@tonic-gate 			if ((instance_method_t)i == IM_START) {
571*0Sstevel@tonic-gate 				error_msg(gettext(
572*0Sstevel@tonic-gate 				    "Unspecified %s method for instance %s"),
573*0Sstevel@tonic-gate 				    START_METHOD_NAME, fmri);
574*0Sstevel@tonic-gate 				valid = B_FALSE;
575*0Sstevel@tonic-gate 			}
576*0Sstevel@tonic-gate 		} else if (mprops[i][MP_EXEC].ip_error == IVE_UNSET) {
577*0Sstevel@tonic-gate 			error_msg(gettext("Missing %s property from method %s "
578*0Sstevel@tonic-gate 			    "of instance %s"), PR_EXEC_NAME,
579*0Sstevel@tonic-gate 			    methods[(instance_method_t)i].name, fmri);
580*0Sstevel@tonic-gate 			valid = B_FALSE;
581*0Sstevel@tonic-gate 		}
582*0Sstevel@tonic-gate 	}
583*0Sstevel@tonic-gate 
584*0Sstevel@tonic-gate 	if (!valid)
585*0Sstevel@tonic-gate 		destroy_basic_cfg(*cfg);
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate 	return (valid);
588*0Sstevel@tonic-gate }
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate void
591*0Sstevel@tonic-gate destroy_instance_cfg(instance_cfg_t *cfg)
592*0Sstevel@tonic-gate {
593*0Sstevel@tonic-gate 	if (cfg != NULL) {
594*0Sstevel@tonic-gate 		destroy_basic_cfg(cfg->basic);
595*0Sstevel@tonic-gate 		destroy_method_infos(cfg->methods);
596*0Sstevel@tonic-gate 		free(cfg);
597*0Sstevel@tonic-gate 	}
598*0Sstevel@tonic-gate }
599*0Sstevel@tonic-gate 
600*0Sstevel@tonic-gate /*
601*0Sstevel@tonic-gate  * Returns an allocated instance_cfg_t representation of an instance's
602*0Sstevel@tonic-gate  * configuration read from the repository. If the configuration is invalid, a
603*0Sstevel@tonic-gate  * repository error occurred, or a memory allocation occurred returns NULL,
604*0Sstevel@tonic-gate  * else returns a pointer to the allocated instance_cfg_t.
605*0Sstevel@tonic-gate  */
606*0Sstevel@tonic-gate instance_cfg_t *
607*0Sstevel@tonic-gate read_instance_cfg(const char *fmri)
608*0Sstevel@tonic-gate {
609*0Sstevel@tonic-gate 	uint_t		retries;
610*0Sstevel@tonic-gate 	inetd_prop_t	*bprops;
611*0Sstevel@tonic-gate 	inetd_prop_t	*mprops[NUM_METHODS];
612*0Sstevel@tonic-gate 	instance_cfg_t	*ret = NULL;
613*0Sstevel@tonic-gate 	scf_error_t	err;
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 	debug_msg("Entering read_instance_cfg");
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 	if ((ret = calloc(1, sizeof (instance_cfg_t))) == NULL)
618*0Sstevel@tonic-gate 		return (NULL);
619*0Sstevel@tonic-gate 
620*0Sstevel@tonic-gate 	for (retries = 0; retries <= REP_OP_RETRIES; retries++) {
621*0Sstevel@tonic-gate 		if (make_handle_bound(rep_handle) == -1) {
622*0Sstevel@tonic-gate 			err = scf_error();
623*0Sstevel@tonic-gate 			goto read_error;
624*0Sstevel@tonic-gate 		}
625*0Sstevel@tonic-gate 
626*0Sstevel@tonic-gate 		if (read_inst_props(fmri, &bprops, mprops, &err) == 0)
627*0Sstevel@tonic-gate 			break;
628*0Sstevel@tonic-gate 		if (err != SCF_ERROR_CONNECTION_BROKEN)
629*0Sstevel@tonic-gate 			goto read_error;
630*0Sstevel@tonic-gate 		(void) scf_handle_unbind(rep_handle);
631*0Sstevel@tonic-gate 	}
632*0Sstevel@tonic-gate 	if (retries > REP_OP_RETRIES)
633*0Sstevel@tonic-gate 		goto read_error;
634*0Sstevel@tonic-gate 
635*0Sstevel@tonic-gate 	/*
636*0Sstevel@tonic-gate 	 * Switch off validation of the start method's exec string, since
637*0Sstevel@tonic-gate 	 * during boot the filesystem it resides on may not have been
638*0Sstevel@tonic-gate 	 * mounted yet, which would result in a false validation failure.
639*0Sstevel@tonic-gate 	 * We'll catch any real errors when the start method is first run
640*0Sstevel@tonic-gate 	 * in passes_basic_exec_checks().
641*0Sstevel@tonic-gate 	 */
642*0Sstevel@tonic-gate 	bprops[PT_EXEC_INDEX].ip_error = IVE_UNSET;
643*0Sstevel@tonic-gate 
644*0Sstevel@tonic-gate 	if ((!valid_inst_props(fmri, bprops, mprops, &ret->basic)) ||
645*0Sstevel@tonic-gate 	    (populate_defaults(bprops, ret->basic) != 0) ||
646*0Sstevel@tonic-gate 	    (create_method_infos(fmri, mprops, ret->methods) != 0)) {
647*0Sstevel@tonic-gate 		destroy_instance_cfg(ret);
648*0Sstevel@tonic-gate 		ret = NULL;
649*0Sstevel@tonic-gate 	}
650*0Sstevel@tonic-gate 
651*0Sstevel@tonic-gate 	destroy_inst_props(bprops, mprops);
652*0Sstevel@tonic-gate 	return (ret);
653*0Sstevel@tonic-gate 
654*0Sstevel@tonic-gate read_error:
655*0Sstevel@tonic-gate 	error_msg(gettext(
656*0Sstevel@tonic-gate 	    "Failed to read the configuration of instance %s: %s"), fmri,
657*0Sstevel@tonic-gate 	    scf_strerror(err));
658*0Sstevel@tonic-gate 	free(ret);
659*0Sstevel@tonic-gate 	return (NULL);
660*0Sstevel@tonic-gate }
661*0Sstevel@tonic-gate 
662*0Sstevel@tonic-gate /*
663*0Sstevel@tonic-gate  * Returns a pointer to an allocated method context for the specified method
664*0Sstevel@tonic-gate  * of the specified instance if it could retrieve it. Else, if there were
665*0Sstevel@tonic-gate  * errors retrieving it, NULL is returned and the pointer referenced by
666*0Sstevel@tonic-gate  * 'errstr' is set to point at an appropriate error string.
667*0Sstevel@tonic-gate  */
668*0Sstevel@tonic-gate struct method_context *
669*0Sstevel@tonic-gate read_method_context(const char *inst_fmri, const char *method, const char *path,
670*0Sstevel@tonic-gate     const char **errstr)
671*0Sstevel@tonic-gate {
672*0Sstevel@tonic-gate 	scf_instance_t			*scf_inst = NULL;
673*0Sstevel@tonic-gate 	struct method_context		*ret;
674*0Sstevel@tonic-gate 	uint_t				retries;
675*0Sstevel@tonic-gate 	const char			*tmpstr;
676*0Sstevel@tonic-gate 
677*0Sstevel@tonic-gate 	debug_msg("Entering read_method_context: inst: %s, method: %s, "
678*0Sstevel@tonic-gate 	    "path: %s", inst_fmri, method, path);
679*0Sstevel@tonic-gate 
680*0Sstevel@tonic-gate 	for (retries = 0; retries <= REP_OP_RETRIES; retries++) {
681*0Sstevel@tonic-gate 		if (make_handle_bound(rep_handle) == -1)
682*0Sstevel@tonic-gate 			goto inst_failure;
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate 		if (((scf_inst = scf_instance_create(rep_handle)) != NULL) &&
685*0Sstevel@tonic-gate 		    (scf_handle_decode_fmri(rep_handle, inst_fmri, NULL, NULL,
686*0Sstevel@tonic-gate 		    scf_inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) == 0))
687*0Sstevel@tonic-gate 			break;
688*0Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN) {
689*0Sstevel@tonic-gate 			scf_instance_destroy(scf_inst);
690*0Sstevel@tonic-gate 			goto inst_failure;
691*0Sstevel@tonic-gate 		}
692*0Sstevel@tonic-gate 
693*0Sstevel@tonic-gate 		(void) scf_instance_destroy(scf_inst);
694*0Sstevel@tonic-gate 		scf_inst = NULL;
695*0Sstevel@tonic-gate 
696*0Sstevel@tonic-gate 		(void) scf_handle_unbind(rep_handle);
697*0Sstevel@tonic-gate 	}
698*0Sstevel@tonic-gate 	if (retries > REP_OP_RETRIES)
699*0Sstevel@tonic-gate 		goto inst_failure;
700*0Sstevel@tonic-gate 
701*0Sstevel@tonic-gate 	if ((tmpstr = restarter_get_method_context(
702*0Sstevel@tonic-gate 	    RESTARTER_METHOD_CONTEXT_VERSION, scf_inst, NULL, method, path,
703*0Sstevel@tonic-gate 	    &ret)) != NULL) {
704*0Sstevel@tonic-gate 		ret = NULL;
705*0Sstevel@tonic-gate 		*errstr = tmpstr;
706*0Sstevel@tonic-gate 	}
707*0Sstevel@tonic-gate 
708*0Sstevel@tonic-gate 	scf_instance_destroy(scf_inst);
709*0Sstevel@tonic-gate 	return (ret);
710*0Sstevel@tonic-gate 
711*0Sstevel@tonic-gate inst_failure:
712*0Sstevel@tonic-gate 	/*
713*0Sstevel@tonic-gate 	 * We can rely on this string not becoming invalid
714*0Sstevel@tonic-gate 	 * since we don't call bind_textdomain_codeset() or
715*0Sstevel@tonic-gate 	 * setlocale(3C) after initialization.
716*0Sstevel@tonic-gate 	 */
717*0Sstevel@tonic-gate 	*errstr = gettext("failed to get instance from repository");
718*0Sstevel@tonic-gate 	return (NULL);
719*0Sstevel@tonic-gate }
720*0Sstevel@tonic-gate 
721*0Sstevel@tonic-gate /*
722*0Sstevel@tonic-gate  * Reads the value of the enabled property from the named property group
723*0Sstevel@tonic-gate  * of the given instance.
724*0Sstevel@tonic-gate  * If an error occurs, the SCF error code is returned. The possible errors are:
725*0Sstevel@tonic-gate  * - SCF_ERROR_INVALID_ARGUMENT: The enabled property is not a boolean.
726*0Sstevel@tonic-gate  * - SCF_ERROR_NONE: No value exists for the enabled property.
727*0Sstevel@tonic-gate  * - SCF_ERROR_CONNECTION_BROKEN: Repository connection broken.
728*0Sstevel@tonic-gate  * - SCF_ERROR_NOT_FOUND: The property wasn't found.
729*0Sstevel@tonic-gate  * - SCF_ERROR_NO_MEMORY: allocation failure.
730*0Sstevel@tonic-gate  * Else 0 is returned and 'enabled' set appropriately.
731*0Sstevel@tonic-gate  */
732*0Sstevel@tonic-gate static scf_error_t
733*0Sstevel@tonic-gate read_enable_prop(const char *fmri, boolean_t *enabled, const char *pg)
734*0Sstevel@tonic-gate {
735*0Sstevel@tonic-gate 	scf_simple_prop_t	*sp;
736*0Sstevel@tonic-gate 	uint8_t			*u8p;
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 	if ((sp = scf_simple_prop_get(rep_handle, fmri, pg,
739*0Sstevel@tonic-gate 	    SCF_PROPERTY_ENABLED)) == NULL)
740*0Sstevel@tonic-gate 		return (scf_error());
741*0Sstevel@tonic-gate 
742*0Sstevel@tonic-gate 	if ((u8p = scf_simple_prop_next_boolean(sp)) == NULL) {
743*0Sstevel@tonic-gate 		scf_simple_prop_free(sp);
744*0Sstevel@tonic-gate 		return (scf_error());
745*0Sstevel@tonic-gate 	}
746*0Sstevel@tonic-gate 
747*0Sstevel@tonic-gate 	*enabled = (*u8p != 0);
748*0Sstevel@tonic-gate 	scf_simple_prop_free(sp);
749*0Sstevel@tonic-gate 	return (0);
750*0Sstevel@tonic-gate }
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate /*
753*0Sstevel@tonic-gate  * Reads the enabled value for the given instance FMRI. The read value
754*0Sstevel@tonic-gate  * is based on a merge of the 'standard' enabled property, and the temporary
755*0Sstevel@tonic-gate  * override one; the merge involves using the latter properties value if
756*0Sstevel@tonic-gate  * present, else resporting to the formers. If an error occurs -1 is returned,
757*0Sstevel@tonic-gate  * else 0 is returned and 'enabled' set approriately.
758*0Sstevel@tonic-gate  */
759*0Sstevel@tonic-gate int
760*0Sstevel@tonic-gate read_enable_merged(const char *fmri, boolean_t *enabled)
761*0Sstevel@tonic-gate {
762*0Sstevel@tonic-gate 	uint_t		retries;
763*0Sstevel@tonic-gate 
764*0Sstevel@tonic-gate 	debug_msg("Entering read_enabled_prop: inst: %s", fmri);
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate 	for (retries = 0; retries <= REP_OP_RETRIES; retries++) {
767*0Sstevel@tonic-gate 		if (make_handle_bound(rep_handle) == -1)
768*0Sstevel@tonic-gate 			goto gen_fail;
769*0Sstevel@tonic-gate 
770*0Sstevel@tonic-gate 		switch (read_enable_prop(fmri, enabled, SCF_PG_GENERAL_OVR)) {
771*0Sstevel@tonic-gate 		case 0:
772*0Sstevel@tonic-gate 			debug_msg("read %d from override", *enabled);
773*0Sstevel@tonic-gate 			return (0);
774*0Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
775*0Sstevel@tonic-gate 			break;
776*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
777*0Sstevel@tonic-gate 		case SCF_ERROR_NONE:
778*0Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
779*0Sstevel@tonic-gate 			switch (read_enable_prop(fmri, enabled,
780*0Sstevel@tonic-gate 			    SCF_PG_GENERAL)) {
781*0Sstevel@tonic-gate 			case 0:
782*0Sstevel@tonic-gate 				debug_msg("read %d from non_override",
783*0Sstevel@tonic-gate 				    *enabled);
784*0Sstevel@tonic-gate 				return (0);
785*0Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
786*0Sstevel@tonic-gate 				break;
787*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
788*0Sstevel@tonic-gate 			case SCF_ERROR_NONE:
789*0Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
790*0Sstevel@tonic-gate 				error_msg(gettext("Missing %s property/value "
791*0Sstevel@tonic-gate 				    "for instance %s"), SCF_PROPERTY_ENABLED,
792*0Sstevel@tonic-gate 				    fmri);
793*0Sstevel@tonic-gate 				return (-1);
794*0Sstevel@tonic-gate 			default:
795*0Sstevel@tonic-gate 				goto gen_fail;
796*0Sstevel@tonic-gate 			}
797*0Sstevel@tonic-gate 			break;
798*0Sstevel@tonic-gate 		default:
799*0Sstevel@tonic-gate 			goto gen_fail;
800*0Sstevel@tonic-gate 		}
801*0Sstevel@tonic-gate 
802*0Sstevel@tonic-gate 		(void) scf_handle_unbind(rep_handle);
803*0Sstevel@tonic-gate 		continue;
804*0Sstevel@tonic-gate 	}
805*0Sstevel@tonic-gate 
806*0Sstevel@tonic-gate gen_fail:
807*0Sstevel@tonic-gate 	error_msg(gettext("Failed to read the %s property of instance %s: %s"),
808*0Sstevel@tonic-gate 	    SCF_PROPERTY_ENABLED, fmri, scf_strerror(scf_error()));
809*0Sstevel@tonic-gate 	return (-1);
810*0Sstevel@tonic-gate }
811