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