17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
23*0b5c9250Shg * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate * synchronous svcadm logic
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <locale.h>
327c478bd9Sstevel@tonic-gate #include <libintl.h>
337c478bd9Sstevel@tonic-gate #include <libscf.h>
347c478bd9Sstevel@tonic-gate #include <libscf_priv.h>
357c478bd9Sstevel@tonic-gate #include <libuutil.h>
367c478bd9Sstevel@tonic-gate #include <stddef.h>
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <assert.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <sys/stat.h>
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate * Definitions from svcadm.c.
487c478bd9Sstevel@tonic-gate */
497c478bd9Sstevel@tonic-gate extern scf_handle_t *h;
507c478bd9Sstevel@tonic-gate extern ssize_t max_scf_fmri_sz;
517c478bd9Sstevel@tonic-gate
52*0b5c9250Shg extern void do_scfdie(int) __NORETURN;
537c478bd9Sstevel@tonic-gate extern int inst_get_state(scf_instance_t *, char *, const char *,
547c478bd9Sstevel@tonic-gate scf_propertygroup_t **);
557c478bd9Sstevel@tonic-gate extern ssize_t get_astring_prop(const scf_propertygroup_t *, const char *,
567c478bd9Sstevel@tonic-gate scf_property_t *, scf_value_t *, char *, size_t);
577c478bd9Sstevel@tonic-gate extern int get_bool_prop(scf_propertygroup_t *, const char *, uint8_t *);
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate #define scfdie() do_scfdie(__LINE__)
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate int has_potential(scf_instance_t *, int);
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate * Determines if the specified instance is enabled, composing the
657c478bd9Sstevel@tonic-gate * general and general_ovr property groups. For simplicity, we map
667c478bd9Sstevel@tonic-gate * most errors to "not enabled".
677c478bd9Sstevel@tonic-gate */
687c478bd9Sstevel@tonic-gate int
is_enabled(scf_instance_t * inst)697c478bd9Sstevel@tonic-gate is_enabled(scf_instance_t *inst)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate scf_propertygroup_t *pg;
727c478bd9Sstevel@tonic-gate uint8_t bp;
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate if ((pg = scf_pg_create(h)) == NULL)
757c478bd9Sstevel@tonic-gate scfdie();
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate if (scf_instance_get_pg(inst, SCF_PG_GENERAL_OVR, pg) == 0 &&
787c478bd9Sstevel@tonic-gate get_bool_prop(pg, SCF_PROPERTY_ENABLED, &bp) == 0) {
797c478bd9Sstevel@tonic-gate scf_pg_destroy(pg);
807c478bd9Sstevel@tonic-gate return (bp);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate if (scf_instance_get_pg(inst, SCF_PG_GENERAL, pg) == 0 &&
847c478bd9Sstevel@tonic-gate get_bool_prop(pg, SCF_PROPERTY_ENABLED, &bp) == 0) {
857c478bd9Sstevel@tonic-gate scf_pg_destroy(pg);
867c478bd9Sstevel@tonic-gate return (bp);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate scf_pg_destroy(pg);
907c478bd9Sstevel@tonic-gate return (B_FALSE);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate * Reads an astring property from a property group. If the named
957c478bd9Sstevel@tonic-gate * property doesn't exist, returns NULL. The result of a successful
967c478bd9Sstevel@tonic-gate * call should be freed.
977c478bd9Sstevel@tonic-gate */
987c478bd9Sstevel@tonic-gate static char *
read_astring_prop(scf_propertygroup_t * pg,scf_value_t * val,scf_property_t * prop,const char * name)997c478bd9Sstevel@tonic-gate read_astring_prop(scf_propertygroup_t *pg, scf_value_t *val,
1007c478bd9Sstevel@tonic-gate scf_property_t *prop, const char *name)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate char *value;
1037c478bd9Sstevel@tonic-gate size_t value_sz;
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate if (scf_pg_get_property(pg, name, prop) == -1) {
1067c478bd9Sstevel@tonic-gate switch (scf_error()) {
1077c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
1087c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED:
1097c478bd9Sstevel@tonic-gate return (NULL);
1107c478bd9Sstevel@tonic-gate default:
1117c478bd9Sstevel@tonic-gate scfdie();
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate if (scf_property_get_value(prop, val) != 0) {
1167c478bd9Sstevel@tonic-gate switch (scf_error()) {
1177c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED:
1187c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
1197c478bd9Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED:
1207c478bd9Sstevel@tonic-gate return (NULL);
1217c478bd9Sstevel@tonic-gate default:
1227c478bd9Sstevel@tonic-gate scfdie();
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate value_sz = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
1277c478bd9Sstevel@tonic-gate if ((value = malloc(value_sz)) == NULL)
1287c478bd9Sstevel@tonic-gate scfdie();
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate if (scf_value_get_astring(val, value, value_sz) <= 0) {
1317c478bd9Sstevel@tonic-gate free(value);
1327c478bd9Sstevel@tonic-gate return (NULL);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate return (value);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate * Creates and returns an scf_iter for the values of the named
1407c478bd9Sstevel@tonic-gate * multi-value property. Returns NULL on failure.
1417c478bd9Sstevel@tonic-gate */
1427c478bd9Sstevel@tonic-gate static scf_iter_t *
prop_walk_init(scf_propertygroup_t * pg,const char * name)1437c478bd9Sstevel@tonic-gate prop_walk_init(scf_propertygroup_t *pg, const char *name)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate scf_iter_t *iter;
1467c478bd9Sstevel@tonic-gate scf_property_t *prop;
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate if ((iter = scf_iter_create(h)) == NULL ||
1497c478bd9Sstevel@tonic-gate (prop = scf_property_create(h)) == NULL)
1507c478bd9Sstevel@tonic-gate scfdie();
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate if (scf_pg_get_property(pg, name, prop) != 0) {
1537c478bd9Sstevel@tonic-gate switch (scf_error()) {
1547c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
1557c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED:
1567c478bd9Sstevel@tonic-gate goto error;
1577c478bd9Sstevel@tonic-gate default:
1587c478bd9Sstevel@tonic-gate scfdie();
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate if (scf_iter_property_values(iter, prop) != 0) {
1637c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_DELETED)
1647c478bd9Sstevel@tonic-gate scfdie();
1657c478bd9Sstevel@tonic-gate goto error;
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate scf_property_destroy(prop);
1697c478bd9Sstevel@tonic-gate return (iter);
1707c478bd9Sstevel@tonic-gate error:
1717c478bd9Sstevel@tonic-gate scf_property_destroy(prop);
1727c478bd9Sstevel@tonic-gate scf_iter_destroy(iter);
1737c478bd9Sstevel@tonic-gate return (NULL);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate * Reads the next value from the multi-value property using the
1787c478bd9Sstevel@tonic-gate * scf_iter obtained by prop_walk_init, and places it in the buffer
1797c478bd9Sstevel@tonic-gate * pointed to by fmri. Returns -1 on failure, 0 when done, and non-0
1807c478bd9Sstevel@tonic-gate * when returning a value.
1817c478bd9Sstevel@tonic-gate */
1827c478bd9Sstevel@tonic-gate static int
prop_walk_step(scf_iter_t * iter,char * fmri,size_t len)1837c478bd9Sstevel@tonic-gate prop_walk_step(scf_iter_t *iter, char *fmri, size_t len)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate int r;
1867c478bd9Sstevel@tonic-gate scf_value_t *val;
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate if ((val = scf_value_create(h)) == NULL)
1897c478bd9Sstevel@tonic-gate scfdie();
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate r = scf_iter_next_value(iter, val);
1927c478bd9Sstevel@tonic-gate if (r == 0)
1937c478bd9Sstevel@tonic-gate goto out;
1947c478bd9Sstevel@tonic-gate if (r == -1) {
1957c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_DELETED)
1967c478bd9Sstevel@tonic-gate scfdie();
1977c478bd9Sstevel@tonic-gate goto out;
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate if (scf_value_get_astring(val, fmri, len) <= 0) {
2007c478bd9Sstevel@tonic-gate r = -1;
2017c478bd9Sstevel@tonic-gate goto out;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate out:
2057c478bd9Sstevel@tonic-gate scf_value_destroy(val);
2067c478bd9Sstevel@tonic-gate return (r);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate * Determines if a file dependency is satisfied, taking into account
2117c478bd9Sstevel@tonic-gate * whether it is an exclusion dependency or not. If we can't access
2127c478bd9Sstevel@tonic-gate * the file, we err on the side of caution and assume the dependency
2137c478bd9Sstevel@tonic-gate * isn't satisfied.
2147c478bd9Sstevel@tonic-gate */
2157c478bd9Sstevel@tonic-gate static int
file_has_potential(char * fmri,int exclude)2167c478bd9Sstevel@tonic-gate file_has_potential(char *fmri, int exclude)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate const char *path;
2197c478bd9Sstevel@tonic-gate struct stat st;
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate int good = exclude ? B_FALSE : B_TRUE;
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate if (scf_parse_file_fmri(fmri, NULL, &path) != 0)
2247c478bd9Sstevel@tonic-gate return (good);
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate if (stat(path, &st) == 0)
2277c478bd9Sstevel@tonic-gate return (good);
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate if (errno == EACCES) {
2307c478bd9Sstevel@tonic-gate uu_warn(gettext("Unable to access \"%s\".\n"), path);
2317c478bd9Sstevel@tonic-gate return (B_FALSE);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate return (!good);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate * Determines if a dependency on a service instance is satisfiable.
2397c478bd9Sstevel@tonic-gate * Returns 0 if not, 1 if it is, or 2 if it is an optional or exclude
2407c478bd9Sstevel@tonic-gate * dependency and the service only "weakly" satisfies (i.e. is disabled
2417c478bd9Sstevel@tonic-gate * or is in maintenance state).
2427c478bd9Sstevel@tonic-gate */
2437c478bd9Sstevel@tonic-gate static int
inst_has_potential(scf_instance_t * inst,int enabled,int optional,int exclude)2447c478bd9Sstevel@tonic-gate inst_has_potential(scf_instance_t *inst, int enabled, int optional, int exclude)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate char state[MAX_SCF_STATE_STRING_SZ];
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate if (!enabled)
2497c478bd9Sstevel@tonic-gate return ((optional || exclude) ? 2 : 0);
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate * Normally we would return a positive value on failure;
2537c478bd9Sstevel@tonic-gate * relying on startd to place the service in maintenance. But
2547c478bd9Sstevel@tonic-gate * if we can't read a service's state, we have to assume it is
2557c478bd9Sstevel@tonic-gate * out to lunch.
2567c478bd9Sstevel@tonic-gate */
2577c478bd9Sstevel@tonic-gate if (inst_get_state(inst, state, NULL, NULL) != 0)
2587c478bd9Sstevel@tonic-gate return (0);
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate * Optional dependencies which are offline always have a possibility of
2627c478bd9Sstevel@tonic-gate * coming online.
2637c478bd9Sstevel@tonic-gate */
2647c478bd9Sstevel@tonic-gate if (optional && strcmp(state, SCF_STATE_STRING_OFFLINE) == 0)
2657c478bd9Sstevel@tonic-gate return (2);
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
2687c478bd9Sstevel@tonic-gate /*
2697c478bd9Sstevel@tonic-gate * Enabled services in maintenance state satisfy
2707c478bd9Sstevel@tonic-gate * optional-all dependencies.
2717c478bd9Sstevel@tonic-gate */
2727c478bd9Sstevel@tonic-gate return ((optional || exclude) ? 2 : 0);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate * We're enabled and not in maintenance.
2777c478bd9Sstevel@tonic-gate */
2787c478bd9Sstevel@tonic-gate if (exclude)
2797c478bd9Sstevel@tonic-gate return (0);
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0 ||
2827c478bd9Sstevel@tonic-gate strcmp(state, SCF_STATE_STRING_DEGRADED) == 0)
2837c478bd9Sstevel@tonic-gate return (1);
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate return (has_potential(inst, B_FALSE));
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate * Determines if a dependency on an fmri is satisfiable, handling the
2907c478bd9Sstevel@tonic-gate * separate cases for file, service, and instance fmris. Returns false
2917c478bd9Sstevel@tonic-gate * if not, or true if it is. Takes into account if the dependency is
2927c478bd9Sstevel@tonic-gate * an optional or exclusive one.
2937c478bd9Sstevel@tonic-gate */
2947c478bd9Sstevel@tonic-gate static int
fmri_has_potential(char * fmri,int isfile,int optional,int exclude,int restarter)2957c478bd9Sstevel@tonic-gate fmri_has_potential(char *fmri, int isfile, int optional, int exclude,
2967c478bd9Sstevel@tonic-gate int restarter)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate scf_instance_t *inst;
2997c478bd9Sstevel@tonic-gate scf_service_t *svc;
3007c478bd9Sstevel@tonic-gate scf_iter_t *iter;
3017c478bd9Sstevel@tonic-gate int good = exclude ? B_FALSE : B_TRUE;
3027c478bd9Sstevel@tonic-gate int enabled;
3037c478bd9Sstevel@tonic-gate int r, result;
3047c478bd9Sstevel@tonic-gate int optbad;
3057c478bd9Sstevel@tonic-gate
3067c478bd9Sstevel@tonic-gate assert(!optional || !exclude);
3077c478bd9Sstevel@tonic-gate
3087c478bd9Sstevel@tonic-gate if (isfile)
3097c478bd9Sstevel@tonic-gate return (file_has_potential(fmri, exclude));
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate if ((inst = scf_instance_create(h)) == NULL ||
3127c478bd9Sstevel@tonic-gate (svc = scf_service_create(h)) == NULL ||
3137c478bd9Sstevel@tonic-gate (iter = scf_iter_create(h)) == NULL)
3147c478bd9Sstevel@tonic-gate scfdie();
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL, NULL,
3177c478bd9Sstevel@tonic-gate SCF_DECODE_FMRI_EXACT) == 0) {
3187c478bd9Sstevel@tonic-gate enabled = is_enabled(inst);
3197c478bd9Sstevel@tonic-gate result =
3207c478bd9Sstevel@tonic-gate (inst_has_potential(inst, enabled, optional, exclude) != 0);
3217c478bd9Sstevel@tonic-gate goto out;
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate
3247c478bd9Sstevel@tonic-gate if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL,
3257c478bd9Sstevel@tonic-gate SCF_DECODE_FMRI_EXACT) != 0) {
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate * If we are checking a restarter dependency, a bad
3287c478bd9Sstevel@tonic-gate * or nonexistent service will never be noticed.
3297c478bd9Sstevel@tonic-gate */
3307c478bd9Sstevel@tonic-gate result = restarter ? B_FALSE : good;
3317c478bd9Sstevel@tonic-gate goto out;
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate
3347c478bd9Sstevel@tonic-gate if (scf_iter_service_instances(iter, svc) != 0) {
3357c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_DELETED)
3367c478bd9Sstevel@tonic-gate scfdie();
3377c478bd9Sstevel@tonic-gate result = good;
3387c478bd9Sstevel@tonic-gate goto out;
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate optbad = 0;
3427c478bd9Sstevel@tonic-gate for (;;) {
3437c478bd9Sstevel@tonic-gate r = scf_iter_next_instance(iter, inst);
3447c478bd9Sstevel@tonic-gate if (r == 0) {
3457c478bd9Sstevel@tonic-gate result = exclude || (optional && !optbad);
3467c478bd9Sstevel@tonic-gate goto out;
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate if (r == -1) {
3497c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_DELETED)
3507c478bd9Sstevel@tonic-gate scfdie();
3517c478bd9Sstevel@tonic-gate result = good;
3527c478bd9Sstevel@tonic-gate goto out;
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate
3557c478bd9Sstevel@tonic-gate enabled = is_enabled(inst);
3567c478bd9Sstevel@tonic-gate r = inst_has_potential(inst, enabled, optional, exclude);
3577c478bd9Sstevel@tonic-gate
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate * Exclusion dependencies over services map to
3607c478bd9Sstevel@tonic-gate * require-none for its instances.
3617c478bd9Sstevel@tonic-gate */
3627c478bd9Sstevel@tonic-gate if (exclude)
3637c478bd9Sstevel@tonic-gate r = (r == 0);
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate if (r == 1) {
3667c478bd9Sstevel@tonic-gate /*
3677c478bd9Sstevel@tonic-gate * Remember, if this is an exclusion dependency
3687c478bd9Sstevel@tonic-gate * (which means we are here because there
3697c478bd9Sstevel@tonic-gate * exists an instance which wasn't satisfiable
3707c478bd9Sstevel@tonic-gate * in that regard), good means bad.
3717c478bd9Sstevel@tonic-gate */
3727c478bd9Sstevel@tonic-gate result = good;
3737c478bd9Sstevel@tonic-gate goto out;
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate if (optional && r == 0)
3777c478bd9Sstevel@tonic-gate optbad = 1;
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate out:
3817c478bd9Sstevel@tonic-gate scf_instance_destroy(inst);
3827c478bd9Sstevel@tonic-gate scf_service_destroy(svc);
3837c478bd9Sstevel@tonic-gate scf_iter_destroy(iter);
3847c478bd9Sstevel@tonic-gate return (result);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate
3877c478bd9Sstevel@tonic-gate static int
eval_require_any(scf_iter_t * iter,char * value,size_t value_sz,int isfile)3887c478bd9Sstevel@tonic-gate eval_require_any(scf_iter_t *iter, char *value, size_t value_sz, int isfile)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate int r, empty = B_TRUE;
3917c478bd9Sstevel@tonic-gate
3927c478bd9Sstevel@tonic-gate for (;;) {
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate * For reasons unknown, an empty require_any dependency
3957c478bd9Sstevel@tonic-gate * group is considered by startd to be satisfied.
3967c478bd9Sstevel@tonic-gate * This insanity fortunately doesn't extend to
3977c478bd9Sstevel@tonic-gate * dependencies on services with no instances.
3987c478bd9Sstevel@tonic-gate */
3997c478bd9Sstevel@tonic-gate if ((r = prop_walk_step(iter, value, value_sz)) <= 0)
4007c478bd9Sstevel@tonic-gate return ((r == 0 && empty) ? B_TRUE : r);
4017c478bd9Sstevel@tonic-gate if (fmri_has_potential(value, isfile, B_FALSE, B_FALSE,
4027c478bd9Sstevel@tonic-gate B_FALSE))
4037c478bd9Sstevel@tonic-gate return (1);
4047c478bd9Sstevel@tonic-gate empty = B_FALSE;
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate
4087c478bd9Sstevel@tonic-gate static int
eval_all(scf_iter_t * iter,char * value,size_t value_sz,int isfile,int optional,int exclude)4097c478bd9Sstevel@tonic-gate eval_all(scf_iter_t *iter, char *value, size_t value_sz,
4107c478bd9Sstevel@tonic-gate int isfile, int optional, int exclude)
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate int r;
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate for (;;) {
4157c478bd9Sstevel@tonic-gate if ((r = prop_walk_step(iter, value, value_sz)) <= 0)
4167c478bd9Sstevel@tonic-gate return ((r == 0) ? 1 : r);
4177c478bd9Sstevel@tonic-gate if (!fmri_has_potential(value, isfile, optional, exclude,
4187c478bd9Sstevel@tonic-gate B_FALSE))
4197c478bd9Sstevel@tonic-gate return (0);
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate static int
eval_require_all(scf_iter_t * iter,char * value,size_t value_sz,int isfile)4247c478bd9Sstevel@tonic-gate eval_require_all(scf_iter_t *iter, char *value, size_t value_sz, int isfile)
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate return (eval_all(iter, value, value_sz, isfile, B_FALSE, B_FALSE));
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate static int
eval_optional_all(scf_iter_t * iter,char * value,size_t value_sz,int isfile)4307c478bd9Sstevel@tonic-gate eval_optional_all(scf_iter_t *iter, char *value, size_t value_sz, int isfile)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate return (eval_all(iter, value, value_sz, isfile, B_TRUE, B_FALSE));
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate
4357c478bd9Sstevel@tonic-gate static int
eval_exclude_all(scf_iter_t * iter,char * value,size_t value_sz,int isfile)4367c478bd9Sstevel@tonic-gate eval_exclude_all(scf_iter_t *iter, char *value, size_t value_sz, int isfile)
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate return (eval_all(iter, value, value_sz, isfile, B_FALSE, B_TRUE));
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate
4417c478bd9Sstevel@tonic-gate /*
4427c478bd9Sstevel@tonic-gate * Examines the state and health of an instance's restarter and
4437c478bd9Sstevel@tonic-gate * dependencies, and determines the impact of both on the instance's
4447c478bd9Sstevel@tonic-gate * ability to be brought on line. A true return value indicates that
4457c478bd9Sstevel@tonic-gate * instance appears to be a likely candidate for the online club.
4467c478bd9Sstevel@tonic-gate * False indicates that there is no hope for the instance.
4477c478bd9Sstevel@tonic-gate */
4487c478bd9Sstevel@tonic-gate int
has_potential(scf_instance_t * inst,int restarter_only)4497c478bd9Sstevel@tonic-gate has_potential(scf_instance_t *inst, int restarter_only)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate scf_snapshot_t *snap;
4527c478bd9Sstevel@tonic-gate scf_iter_t *iter, *viter = NULL;
4537c478bd9Sstevel@tonic-gate scf_propertygroup_t *pg;
4547c478bd9Sstevel@tonic-gate scf_property_t *prop;
4557c478bd9Sstevel@tonic-gate scf_value_t *val;
4567c478bd9Sstevel@tonic-gate char *type = NULL, *grouping = NULL;
4577c478bd9Sstevel@tonic-gate char *value;
4587c478bd9Sstevel@tonic-gate size_t value_sz;
4597c478bd9Sstevel@tonic-gate int result = B_TRUE, r;
4607c478bd9Sstevel@tonic-gate int isfile;
4617c478bd9Sstevel@tonic-gate
4627c478bd9Sstevel@tonic-gate value_sz = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
4637c478bd9Sstevel@tonic-gate if ((iter = scf_iter_create(h)) == NULL ||
4647c478bd9Sstevel@tonic-gate (snap = scf_snapshot_create(h)) == NULL ||
4657c478bd9Sstevel@tonic-gate (pg = scf_pg_create(h)) == NULL ||
4667c478bd9Sstevel@tonic-gate (val = scf_value_create(h)) == NULL ||
4677c478bd9Sstevel@tonic-gate (prop = scf_property_create(h)) == NULL ||
4687c478bd9Sstevel@tonic-gate (value = malloc(value_sz)) == NULL)
4697c478bd9Sstevel@tonic-gate scfdie();
4707c478bd9Sstevel@tonic-gate
4717c478bd9Sstevel@tonic-gate /*
4727c478bd9Sstevel@tonic-gate * First we check our restarter as an implicit dependency.
4737c478bd9Sstevel@tonic-gate */
4747c478bd9Sstevel@tonic-gate if (scf_instance_get_pg_composed(inst, NULL, SCF_PG_GENERAL, pg) != 0)
4757c478bd9Sstevel@tonic-gate scfdie();
4767c478bd9Sstevel@tonic-gate
4777c478bd9Sstevel@tonic-gate r = get_astring_prop(pg, SCF_PROPERTY_RESTARTER, prop, val, value,
4787c478bd9Sstevel@tonic-gate value_sz);
4797c478bd9Sstevel@tonic-gate if (r == -ENOENT) {
4807c478bd9Sstevel@tonic-gate (void) strlcpy(value, SCF_SERVICE_STARTD, value_sz);
4817c478bd9Sstevel@tonic-gate } else if (r < 0 || r > max_scf_fmri_sz) {
4827c478bd9Sstevel@tonic-gate /*
4837c478bd9Sstevel@tonic-gate * Normally we would return true and let the restarter
4847c478bd9Sstevel@tonic-gate * tell our caller there is a problem by changing the
4857c478bd9Sstevel@tonic-gate * instance's state, but that's not going to happen if
4867c478bd9Sstevel@tonic-gate * the restarter is invalid.
4877c478bd9Sstevel@tonic-gate */
4887c478bd9Sstevel@tonic-gate result = B_FALSE;
4897c478bd9Sstevel@tonic-gate goto out;
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate
4927c478bd9Sstevel@tonic-gate if (!fmri_has_potential(value, B_FALSE, B_FALSE, B_FALSE, B_TRUE)) {
4937c478bd9Sstevel@tonic-gate result = B_FALSE;
4947c478bd9Sstevel@tonic-gate goto out;
4957c478bd9Sstevel@tonic-gate }
4967c478bd9Sstevel@tonic-gate
4977c478bd9Sstevel@tonic-gate if (restarter_only)
4987c478bd9Sstevel@tonic-gate goto out;
4997c478bd9Sstevel@tonic-gate
5007c478bd9Sstevel@tonic-gate /*
5017c478bd9Sstevel@tonic-gate * Now we check explicit dependencies.
5027c478bd9Sstevel@tonic-gate */
5037c478bd9Sstevel@tonic-gate if (scf_instance_get_snapshot(inst, "running", snap) != 0) {
5047c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND)
5057c478bd9Sstevel@tonic-gate scfdie();
5067c478bd9Sstevel@tonic-gate scf_snapshot_destroy(snap);
5077c478bd9Sstevel@tonic-gate snap = NULL;
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate
5107c478bd9Sstevel@tonic-gate if (scf_iter_instance_pgs_typed_composed(iter, inst, snap,
5117c478bd9Sstevel@tonic-gate SCF_GROUP_DEPENDENCY) != 0) {
5127c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_DELETED)
5137c478bd9Sstevel@tonic-gate scfdie();
5147c478bd9Sstevel@tonic-gate goto out;
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate
5177c478bd9Sstevel@tonic-gate for (;;) {
5187c478bd9Sstevel@tonic-gate r = scf_iter_next_pg(iter, pg);
5197c478bd9Sstevel@tonic-gate if (r == 0)
5207c478bd9Sstevel@tonic-gate break;
5217c478bd9Sstevel@tonic-gate if (r == -1) {
5227c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_DELETED)
5237c478bd9Sstevel@tonic-gate scfdie();
5247c478bd9Sstevel@tonic-gate goto out;
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate
5277c478bd9Sstevel@tonic-gate if ((grouping = read_astring_prop(pg, val, prop,
5287c478bd9Sstevel@tonic-gate SCF_PROPERTY_GROUPING)) == NULL)
5297c478bd9Sstevel@tonic-gate goto out;
5307c478bd9Sstevel@tonic-gate
5317c478bd9Sstevel@tonic-gate if ((type = read_astring_prop(pg, val, prop,
5327c478bd9Sstevel@tonic-gate SCF_PROPERTY_TYPE)) == NULL)
5337c478bd9Sstevel@tonic-gate goto out;
5347c478bd9Sstevel@tonic-gate
5357c478bd9Sstevel@tonic-gate if (strcmp(type, "path") == 0) {
5367c478bd9Sstevel@tonic-gate isfile = B_TRUE;
5377c478bd9Sstevel@tonic-gate } else if (strcmp(type, "service") == 0) {
5387c478bd9Sstevel@tonic-gate isfile = B_FALSE;
5397c478bd9Sstevel@tonic-gate } else {
5407c478bd9Sstevel@tonic-gate free(type);
5417c478bd9Sstevel@tonic-gate goto out;
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate free(type);
5447c478bd9Sstevel@tonic-gate
5457c478bd9Sstevel@tonic-gate if ((viter = prop_walk_init(pg, SCF_PROPERTY_ENTITIES)) == NULL)
5467c478bd9Sstevel@tonic-gate goto out;
5477c478bd9Sstevel@tonic-gate
5487c478bd9Sstevel@tonic-gate if (strcmp(grouping, SCF_DEP_REQUIRE_ALL) == 0) {
5497c478bd9Sstevel@tonic-gate r = eval_require_all(viter, value, value_sz, isfile);
5507c478bd9Sstevel@tonic-gate } else if (strcmp(grouping, SCF_DEP_REQUIRE_ANY) == 0) {
5517c478bd9Sstevel@tonic-gate r = eval_require_any(viter, value, value_sz, isfile);
5527c478bd9Sstevel@tonic-gate } else if (strcmp(grouping, SCF_DEP_EXCLUDE_ALL) == 0) {
5537c478bd9Sstevel@tonic-gate r = eval_exclude_all(viter, value, value_sz, isfile);
5547c478bd9Sstevel@tonic-gate } else if (strcmp(grouping, SCF_DEP_OPTIONAL_ALL) == 0) {
5557c478bd9Sstevel@tonic-gate r = eval_optional_all(viter, value, value_sz, isfile);
5567c478bd9Sstevel@tonic-gate } else {
5577c478bd9Sstevel@tonic-gate scf_iter_destroy(viter);
5587c478bd9Sstevel@tonic-gate free(grouping);
5597c478bd9Sstevel@tonic-gate grouping = NULL;
5607c478bd9Sstevel@tonic-gate goto out;
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate
5637c478bd9Sstevel@tonic-gate scf_iter_destroy(viter);
5647c478bd9Sstevel@tonic-gate free(grouping);
5657c478bd9Sstevel@tonic-gate grouping = NULL;
5667c478bd9Sstevel@tonic-gate
5677c478bd9Sstevel@tonic-gate if (r == 0) {
5687c478bd9Sstevel@tonic-gate result = B_FALSE;
5697c478bd9Sstevel@tonic-gate goto out;
5707c478bd9Sstevel@tonic-gate } else if (r == -1) {
5717c478bd9Sstevel@tonic-gate goto out;
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate
5757c478bd9Sstevel@tonic-gate out:
5767c478bd9Sstevel@tonic-gate free(value);
5777c478bd9Sstevel@tonic-gate scf_property_destroy(prop);
5787c478bd9Sstevel@tonic-gate scf_value_destroy(val);
5797c478bd9Sstevel@tonic-gate scf_pg_destroy(pg);
5807c478bd9Sstevel@tonic-gate if (snap != NULL)
5817c478bd9Sstevel@tonic-gate scf_snapshot_destroy(snap);
5827c478bd9Sstevel@tonic-gate if (grouping != NULL)
5837c478bd9Sstevel@tonic-gate free(grouping);
5847c478bd9Sstevel@tonic-gate scf_iter_destroy(iter);
5857c478bd9Sstevel@tonic-gate return (result);
5867c478bd9Sstevel@tonic-gate }
587