xref: /onnv-gate/usr/src/cmd/svc/svcadm/synch.c (revision 471:fb6202c3da23)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*471Shg115875  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * synchronous svcadm logic
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <locale.h>
340Sstevel@tonic-gate #include <libintl.h>
350Sstevel@tonic-gate #include <libscf.h>
360Sstevel@tonic-gate #include <libscf_priv.h>
370Sstevel@tonic-gate #include <libuutil.h>
380Sstevel@tonic-gate #include <stddef.h>
390Sstevel@tonic-gate #include <stdio.h>
400Sstevel@tonic-gate #include <stdlib.h>
410Sstevel@tonic-gate #include <string.h>
420Sstevel@tonic-gate #include <unistd.h>
430Sstevel@tonic-gate #include <assert.h>
440Sstevel@tonic-gate #include <errno.h>
450Sstevel@tonic-gate #include <sys/stat.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * Definitions from svcadm.c.
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate extern scf_handle_t *h;
520Sstevel@tonic-gate extern ssize_t max_scf_fmri_sz;
530Sstevel@tonic-gate 
54*471Shg115875 extern void do_scfdie(int) __NORETURN;
550Sstevel@tonic-gate extern int inst_get_state(scf_instance_t *, char *, const char *,
560Sstevel@tonic-gate     scf_propertygroup_t **);
570Sstevel@tonic-gate extern ssize_t get_astring_prop(const scf_propertygroup_t *, const char *,
580Sstevel@tonic-gate     scf_property_t *, scf_value_t *, char *, size_t);
590Sstevel@tonic-gate extern int get_bool_prop(scf_propertygroup_t *, const char *, uint8_t *);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #define	scfdie()	do_scfdie(__LINE__)
620Sstevel@tonic-gate 
630Sstevel@tonic-gate int has_potential(scf_instance_t *, int);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * Determines if the specified instance is enabled, composing the
670Sstevel@tonic-gate  * general and general_ovr property groups.  For simplicity, we map
680Sstevel@tonic-gate  * most errors to "not enabled".
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate int
is_enabled(scf_instance_t * inst)710Sstevel@tonic-gate is_enabled(scf_instance_t *inst)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate 	scf_propertygroup_t *pg;
740Sstevel@tonic-gate 	uint8_t bp;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	if ((pg = scf_pg_create(h)) == NULL)
770Sstevel@tonic-gate 		scfdie();
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_GENERAL_OVR, pg) == 0 &&
800Sstevel@tonic-gate 	    get_bool_prop(pg, SCF_PROPERTY_ENABLED, &bp) == 0) {
810Sstevel@tonic-gate 		scf_pg_destroy(pg);
820Sstevel@tonic-gate 		return (bp);
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_GENERAL, pg) == 0 &&
860Sstevel@tonic-gate 	    get_bool_prop(pg, SCF_PROPERTY_ENABLED, &bp) == 0) {
870Sstevel@tonic-gate 		scf_pg_destroy(pg);
880Sstevel@tonic-gate 		return (bp);
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	scf_pg_destroy(pg);
920Sstevel@tonic-gate 	return (B_FALSE);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * Reads an astring property from a property group.  If the named
970Sstevel@tonic-gate  * property doesn't exist, returns NULL.  The result of a successful
980Sstevel@tonic-gate  * call should be freed.
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate static char *
read_astring_prop(scf_propertygroup_t * pg,scf_value_t * val,scf_property_t * prop,const char * name)1010Sstevel@tonic-gate read_astring_prop(scf_propertygroup_t *pg, scf_value_t *val,
1020Sstevel@tonic-gate     scf_property_t *prop, const char *name)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	char *value;
1050Sstevel@tonic-gate 	size_t value_sz;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	if (scf_pg_get_property(pg, name, prop) == -1) {
1080Sstevel@tonic-gate 		switch (scf_error()) {
1090Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
1100Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
1110Sstevel@tonic-gate 			return (NULL);
1120Sstevel@tonic-gate 		default:
1130Sstevel@tonic-gate 			scfdie();
1140Sstevel@tonic-gate 		}
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	if (scf_property_get_value(prop, val) != 0) {
1180Sstevel@tonic-gate 		switch (scf_error()) {
1190Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
1200Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
1210Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
1220Sstevel@tonic-gate 			return (NULL);
1230Sstevel@tonic-gate 		default:
1240Sstevel@tonic-gate 			scfdie();
1250Sstevel@tonic-gate 		}
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	value_sz = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
1290Sstevel@tonic-gate 	if ((value = malloc(value_sz)) == NULL)
1300Sstevel@tonic-gate 		scfdie();
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	if (scf_value_get_astring(val, value, value_sz) <= 0) {
1330Sstevel@tonic-gate 		free(value);
1340Sstevel@tonic-gate 		return (NULL);
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	return (value);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate  * Creates and returns an scf_iter for the values of the named
1420Sstevel@tonic-gate  * multi-value property.  Returns NULL on failure.
1430Sstevel@tonic-gate  */
1440Sstevel@tonic-gate static scf_iter_t *
prop_walk_init(scf_propertygroup_t * pg,const char * name)1450Sstevel@tonic-gate prop_walk_init(scf_propertygroup_t *pg, const char *name)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	scf_iter_t *iter;
1480Sstevel@tonic-gate 	scf_property_t *prop;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	if ((iter = scf_iter_create(h)) == NULL ||
1510Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL)
1520Sstevel@tonic-gate 		scfdie();
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	if (scf_pg_get_property(pg, name, prop) != 0) {
1550Sstevel@tonic-gate 		switch (scf_error()) {
1560Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
1570Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
1580Sstevel@tonic-gate 			goto error;
1590Sstevel@tonic-gate 		default:
1600Sstevel@tonic-gate 			scfdie();
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (scf_iter_property_values(iter, prop) != 0) {
1650Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_DELETED)
1660Sstevel@tonic-gate 			scfdie();
1670Sstevel@tonic-gate 		goto error;
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	scf_property_destroy(prop);
1710Sstevel@tonic-gate 	return (iter);
1720Sstevel@tonic-gate error:
1730Sstevel@tonic-gate 	scf_property_destroy(prop);
1740Sstevel@tonic-gate 	scf_iter_destroy(iter);
1750Sstevel@tonic-gate 	return (NULL);
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate /*
1790Sstevel@tonic-gate  * Reads the next value from the multi-value property using the
1800Sstevel@tonic-gate  * scf_iter obtained by prop_walk_init, and places it in the buffer
1810Sstevel@tonic-gate  * pointed to by fmri.  Returns -1 on failure, 0 when done, and non-0
1820Sstevel@tonic-gate  * when returning a value.
1830Sstevel@tonic-gate  */
1840Sstevel@tonic-gate static int
prop_walk_step(scf_iter_t * iter,char * fmri,size_t len)1850Sstevel@tonic-gate prop_walk_step(scf_iter_t *iter, char *fmri, size_t len)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate 	int r;
1880Sstevel@tonic-gate 	scf_value_t *val;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	if ((val = scf_value_create(h)) == NULL)
1910Sstevel@tonic-gate 		scfdie();
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	r = scf_iter_next_value(iter, val);
1940Sstevel@tonic-gate 	if (r == 0)
1950Sstevel@tonic-gate 		goto out;
1960Sstevel@tonic-gate 	if (r == -1) {
1970Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_DELETED)
1980Sstevel@tonic-gate 			scfdie();
1990Sstevel@tonic-gate 		goto out;
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 	if (scf_value_get_astring(val, fmri, len) <= 0) {
2020Sstevel@tonic-gate 		r = -1;
2030Sstevel@tonic-gate 		goto out;
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate out:
2070Sstevel@tonic-gate 	scf_value_destroy(val);
2080Sstevel@tonic-gate 	return (r);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate  * Determines if a file dependency is satisfied, taking into account
2130Sstevel@tonic-gate  * whether it is an exclusion dependency or not.  If we can't access
2140Sstevel@tonic-gate  * the file, we err on the side of caution and assume the dependency
2150Sstevel@tonic-gate  * isn't satisfied.
2160Sstevel@tonic-gate  */
2170Sstevel@tonic-gate static int
file_has_potential(char * fmri,int exclude)2180Sstevel@tonic-gate file_has_potential(char *fmri, int exclude)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate 	const char *path;
2210Sstevel@tonic-gate 	struct stat st;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	int good = exclude ? B_FALSE : B_TRUE;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	if (scf_parse_file_fmri(fmri, NULL, &path) != 0)
2260Sstevel@tonic-gate 		return (good);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (stat(path, &st) == 0)
2290Sstevel@tonic-gate 		return (good);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	if (errno == EACCES) {
2320Sstevel@tonic-gate 		uu_warn(gettext("Unable to access \"%s\".\n"), path);
2330Sstevel@tonic-gate 		return (B_FALSE);
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	return (!good);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate /*
2400Sstevel@tonic-gate  * Determines if a dependency on a service instance is satisfiable.
2410Sstevel@tonic-gate  * Returns 0 if not, 1 if it is, or 2 if it is an optional or exclude
2420Sstevel@tonic-gate  * dependency and the service only "weakly" satisfies (i.e. is disabled
2430Sstevel@tonic-gate  * or is in maintenance state).
2440Sstevel@tonic-gate  */
2450Sstevel@tonic-gate static int
inst_has_potential(scf_instance_t * inst,int enabled,int optional,int exclude)2460Sstevel@tonic-gate inst_has_potential(scf_instance_t *inst, int enabled, int optional, int exclude)
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate 	char state[MAX_SCF_STATE_STRING_SZ];
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	if (!enabled)
2510Sstevel@tonic-gate 		return ((optional || exclude) ? 2 : 0);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	/*
2540Sstevel@tonic-gate 	 * Normally we would return a positive value on failure;
2550Sstevel@tonic-gate 	 * relying on startd to place the service in maintenance.  But
2560Sstevel@tonic-gate 	 * if we can't read a service's state, we have to assume it is
2570Sstevel@tonic-gate 	 * out to lunch.
2580Sstevel@tonic-gate 	 */
2590Sstevel@tonic-gate 	if (inst_get_state(inst, state, NULL, NULL) != 0)
2600Sstevel@tonic-gate 		return (0);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/*
2630Sstevel@tonic-gate 	 * Optional dependencies which are offline always have a possibility of
2640Sstevel@tonic-gate 	 * coming online.
2650Sstevel@tonic-gate 	 */
2660Sstevel@tonic-gate 	if (optional && strcmp(state, SCF_STATE_STRING_OFFLINE) == 0)
2670Sstevel@tonic-gate 		return (2);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
2700Sstevel@tonic-gate 		/*
2710Sstevel@tonic-gate 		 * Enabled services in maintenance state satisfy
2720Sstevel@tonic-gate 		 * optional-all dependencies.
2730Sstevel@tonic-gate 		 */
2740Sstevel@tonic-gate 		return ((optional || exclude) ? 2 : 0);
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	/*
2780Sstevel@tonic-gate 	 * We're enabled and not in maintenance.
2790Sstevel@tonic-gate 	 */
2800Sstevel@tonic-gate 	if (exclude)
2810Sstevel@tonic-gate 		return (0);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0 ||
2840Sstevel@tonic-gate 	    strcmp(state, SCF_STATE_STRING_DEGRADED) == 0)
2850Sstevel@tonic-gate 		return (1);
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	return (has_potential(inst, B_FALSE));
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate  * Determines if a dependency on an fmri is satisfiable, handling the
2920Sstevel@tonic-gate  * separate cases for file, service, and instance fmris.  Returns false
2930Sstevel@tonic-gate  * if not, or true if it is.  Takes into account if the dependency is
2940Sstevel@tonic-gate  * an optional or exclusive one.
2950Sstevel@tonic-gate  */
2960Sstevel@tonic-gate static int
fmri_has_potential(char * fmri,int isfile,int optional,int exclude,int restarter)2970Sstevel@tonic-gate fmri_has_potential(char *fmri, int isfile, int optional, int exclude,
2980Sstevel@tonic-gate     int restarter)
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate 	scf_instance_t *inst;
3010Sstevel@tonic-gate 	scf_service_t *svc;
3020Sstevel@tonic-gate 	scf_iter_t *iter;
3030Sstevel@tonic-gate 	int good = exclude ? B_FALSE : B_TRUE;
3040Sstevel@tonic-gate 	int enabled;
3050Sstevel@tonic-gate 	int r, result;
3060Sstevel@tonic-gate 	int optbad;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	assert(!optional || !exclude);
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	if (isfile)
3110Sstevel@tonic-gate 		return (file_has_potential(fmri, exclude));
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	if ((inst = scf_instance_create(h)) == NULL ||
3140Sstevel@tonic-gate 	    (svc = scf_service_create(h)) == NULL ||
3150Sstevel@tonic-gate 	    (iter = scf_iter_create(h)) == NULL)
3160Sstevel@tonic-gate 		scfdie();
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL, NULL,
3190Sstevel@tonic-gate 	    SCF_DECODE_FMRI_EXACT) == 0) {
3200Sstevel@tonic-gate 		enabled = is_enabled(inst);
3210Sstevel@tonic-gate 		result =
3220Sstevel@tonic-gate 		    (inst_has_potential(inst, enabled, optional, exclude) != 0);
3230Sstevel@tonic-gate 		goto out;
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL,
3270Sstevel@tonic-gate 	    SCF_DECODE_FMRI_EXACT) != 0) {
3280Sstevel@tonic-gate 		/*
3290Sstevel@tonic-gate 		 * If we are checking a restarter dependency, a bad
3300Sstevel@tonic-gate 		 * or nonexistent service will never be noticed.
3310Sstevel@tonic-gate 		 */
3320Sstevel@tonic-gate 		result = restarter ? B_FALSE : good;
3330Sstevel@tonic-gate 		goto out;
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	if (scf_iter_service_instances(iter, svc) != 0) {
3370Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_DELETED)
3380Sstevel@tonic-gate 			scfdie();
3390Sstevel@tonic-gate 		result = good;
3400Sstevel@tonic-gate 		goto out;
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	optbad = 0;
3440Sstevel@tonic-gate 	for (;;) {
3450Sstevel@tonic-gate 		r = scf_iter_next_instance(iter, inst);
3460Sstevel@tonic-gate 		if (r == 0) {
3470Sstevel@tonic-gate 			result = exclude || (optional && !optbad);
3480Sstevel@tonic-gate 			goto out;
3490Sstevel@tonic-gate 		}
3500Sstevel@tonic-gate 		if (r == -1) {
3510Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_DELETED)
3520Sstevel@tonic-gate 				scfdie();
3530Sstevel@tonic-gate 			result = good;
3540Sstevel@tonic-gate 			goto out;
3550Sstevel@tonic-gate 		}
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 		enabled = is_enabled(inst);
3580Sstevel@tonic-gate 		r = inst_has_potential(inst, enabled, optional, exclude);
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 		/*
3610Sstevel@tonic-gate 		 * Exclusion dependencies over services map to
3620Sstevel@tonic-gate 		 * require-none for its instances.
3630Sstevel@tonic-gate 		 */
3640Sstevel@tonic-gate 		if (exclude)
3650Sstevel@tonic-gate 			r = (r == 0);
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 		if (r == 1) {
3680Sstevel@tonic-gate 			/*
3690Sstevel@tonic-gate 			 * Remember, if this is an exclusion dependency
3700Sstevel@tonic-gate 			 * (which means we are here because there
3710Sstevel@tonic-gate 			 * exists an instance which wasn't satisfiable
3720Sstevel@tonic-gate 			 * in that regard), good means bad.
3730Sstevel@tonic-gate 			 */
3740Sstevel@tonic-gate 			result = good;
3750Sstevel@tonic-gate 			goto out;
3760Sstevel@tonic-gate 		}
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 		if (optional && r == 0)
3790Sstevel@tonic-gate 			optbad = 1;
3800Sstevel@tonic-gate 	}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate out:
3830Sstevel@tonic-gate 	scf_instance_destroy(inst);
3840Sstevel@tonic-gate 	scf_service_destroy(svc);
3850Sstevel@tonic-gate 	scf_iter_destroy(iter);
3860Sstevel@tonic-gate 	return (result);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate static int
eval_require_any(scf_iter_t * iter,char * value,size_t value_sz,int isfile)3900Sstevel@tonic-gate eval_require_any(scf_iter_t *iter, char *value, size_t value_sz, int isfile)
3910Sstevel@tonic-gate {
3920Sstevel@tonic-gate 	int r, empty = B_TRUE;
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	for (;;) {
3950Sstevel@tonic-gate 		/*
3960Sstevel@tonic-gate 		 * For reasons unknown, an empty require_any dependency
3970Sstevel@tonic-gate 		 * group is considered by startd to be satisfied.
3980Sstevel@tonic-gate 		 * This insanity fortunately doesn't extend to
3990Sstevel@tonic-gate 		 * dependencies on services with no instances.
4000Sstevel@tonic-gate 		 */
4010Sstevel@tonic-gate 		if ((r = prop_walk_step(iter, value, value_sz)) <= 0)
4020Sstevel@tonic-gate 			return ((r == 0 && empty) ? B_TRUE : r);
4030Sstevel@tonic-gate 		if (fmri_has_potential(value, isfile, B_FALSE, B_FALSE,
4040Sstevel@tonic-gate 		    B_FALSE))
4050Sstevel@tonic-gate 			return (1);
4060Sstevel@tonic-gate 		empty = B_FALSE;
4070Sstevel@tonic-gate 	}
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate static int
eval_all(scf_iter_t * iter,char * value,size_t value_sz,int isfile,int optional,int exclude)4110Sstevel@tonic-gate eval_all(scf_iter_t *iter, char *value, size_t value_sz,
4120Sstevel@tonic-gate     int isfile, int optional, int exclude)
4130Sstevel@tonic-gate {
4140Sstevel@tonic-gate 	int r;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	for (;;) {
4170Sstevel@tonic-gate 		if ((r = prop_walk_step(iter, value, value_sz)) <= 0)
4180Sstevel@tonic-gate 			return ((r == 0) ? 1 : r);
4190Sstevel@tonic-gate 		if (!fmri_has_potential(value, isfile, optional, exclude,
4200Sstevel@tonic-gate 		    B_FALSE))
4210Sstevel@tonic-gate 			return (0);
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate static int
eval_require_all(scf_iter_t * iter,char * value,size_t value_sz,int isfile)4260Sstevel@tonic-gate eval_require_all(scf_iter_t *iter, char *value, size_t value_sz, int isfile)
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate 	return (eval_all(iter, value, value_sz, isfile, B_FALSE, B_FALSE));
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate static int
eval_optional_all(scf_iter_t * iter,char * value,size_t value_sz,int isfile)4320Sstevel@tonic-gate eval_optional_all(scf_iter_t *iter, char *value, size_t value_sz, int isfile)
4330Sstevel@tonic-gate {
4340Sstevel@tonic-gate 	return (eval_all(iter, value, value_sz, isfile, B_TRUE, B_FALSE));
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate static int
eval_exclude_all(scf_iter_t * iter,char * value,size_t value_sz,int isfile)4380Sstevel@tonic-gate eval_exclude_all(scf_iter_t *iter, char *value, size_t value_sz, int isfile)
4390Sstevel@tonic-gate {
4400Sstevel@tonic-gate 	return (eval_all(iter, value, value_sz, isfile, B_FALSE, B_TRUE));
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate /*
4440Sstevel@tonic-gate  * Examines the state and health of an instance's restarter and
4450Sstevel@tonic-gate  * dependencies, and determines the impact of both on the instance's
4460Sstevel@tonic-gate  * ability to be brought on line.  A true return value indicates that
4470Sstevel@tonic-gate  * instance appears to be a likely candidate for the online club.
4480Sstevel@tonic-gate  * False indicates that there is no hope for the instance.
4490Sstevel@tonic-gate  */
4500Sstevel@tonic-gate int
has_potential(scf_instance_t * inst,int restarter_only)4510Sstevel@tonic-gate has_potential(scf_instance_t *inst, int restarter_only)
4520Sstevel@tonic-gate {
4530Sstevel@tonic-gate 	scf_snapshot_t *snap;
4540Sstevel@tonic-gate 	scf_iter_t *iter, *viter = NULL;
4550Sstevel@tonic-gate 	scf_propertygroup_t *pg;
4560Sstevel@tonic-gate 	scf_property_t *prop;
4570Sstevel@tonic-gate 	scf_value_t *val;
4580Sstevel@tonic-gate 	char *type = NULL, *grouping = NULL;
4590Sstevel@tonic-gate 	char *value;
4600Sstevel@tonic-gate 	size_t value_sz;
4610Sstevel@tonic-gate 	int result = B_TRUE, r;
4620Sstevel@tonic-gate 	int isfile;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	value_sz = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
4650Sstevel@tonic-gate 	if ((iter = scf_iter_create(h)) == NULL ||
4660Sstevel@tonic-gate 	    (snap = scf_snapshot_create(h)) == NULL ||
4670Sstevel@tonic-gate 	    (pg = scf_pg_create(h)) == NULL ||
4680Sstevel@tonic-gate 	    (val = scf_value_create(h)) == NULL ||
4690Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL ||
4700Sstevel@tonic-gate 	    (value = malloc(value_sz)) == NULL)
4710Sstevel@tonic-gate 		scfdie();
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	/*
4740Sstevel@tonic-gate 	 * First we check our restarter as an implicit dependency.
4750Sstevel@tonic-gate 	 */
4760Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(inst, NULL, SCF_PG_GENERAL, pg) != 0)
4770Sstevel@tonic-gate 		scfdie();
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	r = get_astring_prop(pg, SCF_PROPERTY_RESTARTER, prop, val, value,
4800Sstevel@tonic-gate 	    value_sz);
4810Sstevel@tonic-gate 	if (r == -ENOENT) {
4820Sstevel@tonic-gate 		(void) strlcpy(value, SCF_SERVICE_STARTD, value_sz);
4830Sstevel@tonic-gate 	} else if (r < 0 || r > max_scf_fmri_sz) {
4840Sstevel@tonic-gate 		/*
4850Sstevel@tonic-gate 		 * Normally we would return true and let the restarter
4860Sstevel@tonic-gate 		 * tell our caller there is a problem by changing the
4870Sstevel@tonic-gate 		 * instance's state, but that's not going to happen if
4880Sstevel@tonic-gate 		 * the restarter is invalid.
4890Sstevel@tonic-gate 		 */
4900Sstevel@tonic-gate 		result = B_FALSE;
4910Sstevel@tonic-gate 		goto out;
4920Sstevel@tonic-gate 	}
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	if (!fmri_has_potential(value, B_FALSE, B_FALSE, B_FALSE, B_TRUE)) {
4950Sstevel@tonic-gate 		result = B_FALSE;
4960Sstevel@tonic-gate 		goto out;
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	if (restarter_only)
5000Sstevel@tonic-gate 		goto out;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	/*
5030Sstevel@tonic-gate 	 * Now we check explicit dependencies.
5040Sstevel@tonic-gate 	 */
5050Sstevel@tonic-gate 	if (scf_instance_get_snapshot(inst, "running", snap) != 0) {
5060Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
5070Sstevel@tonic-gate 			scfdie();
5080Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
5090Sstevel@tonic-gate 		snap = NULL;
5100Sstevel@tonic-gate 	}
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	if (scf_iter_instance_pgs_typed_composed(iter, inst, snap,
5130Sstevel@tonic-gate 	    SCF_GROUP_DEPENDENCY) != 0) {
5140Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_DELETED)
5150Sstevel@tonic-gate 			scfdie();
5160Sstevel@tonic-gate 		goto out;
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	for (;;) {
5200Sstevel@tonic-gate 		r = scf_iter_next_pg(iter, pg);
5210Sstevel@tonic-gate 		if (r == 0)
5220Sstevel@tonic-gate 			break;
5230Sstevel@tonic-gate 		if (r == -1) {
5240Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_DELETED)
5250Sstevel@tonic-gate 				scfdie();
5260Sstevel@tonic-gate 			goto out;
5270Sstevel@tonic-gate 		}
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 		if ((grouping = read_astring_prop(pg, val, prop,
5300Sstevel@tonic-gate 		    SCF_PROPERTY_GROUPING)) == NULL)
5310Sstevel@tonic-gate 			goto out;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 		if ((type = read_astring_prop(pg, val, prop,
5340Sstevel@tonic-gate 		    SCF_PROPERTY_TYPE)) == NULL)
5350Sstevel@tonic-gate 			goto out;
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 		if (strcmp(type, "path") == 0) {
5380Sstevel@tonic-gate 			isfile = B_TRUE;
5390Sstevel@tonic-gate 		} else if (strcmp(type, "service") == 0) {
5400Sstevel@tonic-gate 			isfile = B_FALSE;
5410Sstevel@tonic-gate 		} else {
5420Sstevel@tonic-gate 			free(type);
5430Sstevel@tonic-gate 			goto out;
5440Sstevel@tonic-gate 		}
5450Sstevel@tonic-gate 		free(type);
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 		if ((viter = prop_walk_init(pg, SCF_PROPERTY_ENTITIES)) == NULL)
5480Sstevel@tonic-gate 			goto out;
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 		if (strcmp(grouping, SCF_DEP_REQUIRE_ALL) == 0) {
5510Sstevel@tonic-gate 			r = eval_require_all(viter, value, value_sz, isfile);
5520Sstevel@tonic-gate 		} else if (strcmp(grouping, SCF_DEP_REQUIRE_ANY) == 0) {
5530Sstevel@tonic-gate 			r = eval_require_any(viter, value, value_sz, isfile);
5540Sstevel@tonic-gate 		} else if (strcmp(grouping, SCF_DEP_EXCLUDE_ALL) == 0) {
5550Sstevel@tonic-gate 			r = eval_exclude_all(viter, value, value_sz, isfile);
5560Sstevel@tonic-gate 		} else if (strcmp(grouping, SCF_DEP_OPTIONAL_ALL) == 0) {
5570Sstevel@tonic-gate 			r = eval_optional_all(viter, value, value_sz, isfile);
5580Sstevel@tonic-gate 		} else {
5590Sstevel@tonic-gate 			scf_iter_destroy(viter);
5600Sstevel@tonic-gate 			free(grouping);
5610Sstevel@tonic-gate 			grouping = NULL;
5620Sstevel@tonic-gate 			goto out;
5630Sstevel@tonic-gate 		}
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 		scf_iter_destroy(viter);
5660Sstevel@tonic-gate 		free(grouping);
5670Sstevel@tonic-gate 		grouping = NULL;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 		if (r == 0) {
5700Sstevel@tonic-gate 			result = B_FALSE;
5710Sstevel@tonic-gate 			goto out;
5720Sstevel@tonic-gate 		} else if (r == -1) {
5730Sstevel@tonic-gate 			goto out;
5740Sstevel@tonic-gate 		}
5750Sstevel@tonic-gate 	}
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate out:
5780Sstevel@tonic-gate 	free(value);
5790Sstevel@tonic-gate 	scf_property_destroy(prop);
5800Sstevel@tonic-gate 	scf_value_destroy(val);
5810Sstevel@tonic-gate 	scf_pg_destroy(pg);
5820Sstevel@tonic-gate 	if (snap != NULL)
5830Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
5840Sstevel@tonic-gate 	if (grouping != NULL)
5850Sstevel@tonic-gate 		free(grouping);
5860Sstevel@tonic-gate 	scf_iter_destroy(iter);
5870Sstevel@tonic-gate 	return (result);
5880Sstevel@tonic-gate }
589