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 51597Slianep * Common Development and Distribution License (the "License"). 61597Slianep * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*4171Stn143363 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * svcadm - request adminstrative actions for service instances 300Sstevel@tonic-gate */ 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <locale.h> 330Sstevel@tonic-gate #include <libintl.h> 340Sstevel@tonic-gate #include <libscf.h> 350Sstevel@tonic-gate #include <libscf_priv.h> 360Sstevel@tonic-gate #include <libuutil.h> 370Sstevel@tonic-gate #include <stddef.h> 380Sstevel@tonic-gate #include <stdio.h> 390Sstevel@tonic-gate #include <stdlib.h> 400Sstevel@tonic-gate #include <string.h> 410Sstevel@tonic-gate #include <unistd.h> 420Sstevel@tonic-gate #include <assert.h> 430Sstevel@tonic-gate #include <errno.h> 440Sstevel@tonic-gate 450Sstevel@tonic-gate #ifndef TEXT_DOMAIN 460Sstevel@tonic-gate #define TEXT_DOMAIN "SUNW_OST_OSCMD" 470Sstevel@tonic-gate #endif /* TEXT_DOMAIN */ 480Sstevel@tonic-gate 490Sstevel@tonic-gate /* Must be a power of two */ 500Sstevel@tonic-gate #define HT_BUCKETS 64 510Sstevel@tonic-gate 520Sstevel@tonic-gate /* 530Sstevel@tonic-gate * Exit codes for enable and disable -s. 540Sstevel@tonic-gate */ 550Sstevel@tonic-gate #define EXIT_SVC_FAILURE 3 560Sstevel@tonic-gate #define EXIT_DEP_FAILURE 4 570Sstevel@tonic-gate 580Sstevel@tonic-gate /* 590Sstevel@tonic-gate * How long we will wait (in seconds) for a service to change state 600Sstevel@tonic-gate * before re-checking its dependencies. 610Sstevel@tonic-gate */ 620Sstevel@tonic-gate #define WAIT_INTERVAL 3 630Sstevel@tonic-gate 640Sstevel@tonic-gate #ifndef NDEBUG 650Sstevel@tonic-gate #define bad_error(func, err) { \ 660Sstevel@tonic-gate uu_warn("%s:%d: %s() failed with unexpected error %d.\n", \ 670Sstevel@tonic-gate __FILE__, __LINE__, (func), (err)); \ 680Sstevel@tonic-gate abort(); \ 690Sstevel@tonic-gate } 700Sstevel@tonic-gate #else 710Sstevel@tonic-gate #define bad_error(func, err) abort() 720Sstevel@tonic-gate #endif 730Sstevel@tonic-gate 740Sstevel@tonic-gate 750Sstevel@tonic-gate struct ht_elt { 760Sstevel@tonic-gate struct ht_elt *next; 770Sstevel@tonic-gate boolean_t active; 780Sstevel@tonic-gate char str[1]; 790Sstevel@tonic-gate }; 800Sstevel@tonic-gate 810Sstevel@tonic-gate 820Sstevel@tonic-gate scf_handle_t *h; 830Sstevel@tonic-gate ssize_t max_scf_fmri_sz; 840Sstevel@tonic-gate static const char *emsg_permission_denied; 850Sstevel@tonic-gate static const char *emsg_nomem; 860Sstevel@tonic-gate static const char *emsg_create_pg_perm_denied; 870Sstevel@tonic-gate static const char *emsg_pg_perm_denied; 880Sstevel@tonic-gate static const char *emsg_prop_perm_denied; 890Sstevel@tonic-gate static const char *emsg_no_service; 900Sstevel@tonic-gate 910Sstevel@tonic-gate static int exit_status = 0; 920Sstevel@tonic-gate static int verbose = 0; 930Sstevel@tonic-gate static char *scratch_fmri; 940Sstevel@tonic-gate 950Sstevel@tonic-gate static struct ht_elt **visited; 960Sstevel@tonic-gate 97471Shg115875 void do_scfdie(int lineno) __NORETURN; 98471Shg115875 static void usage_milestone(void) __NORETURN; 99471Shg115875 1000Sstevel@tonic-gate /* 1010Sstevel@tonic-gate * Visitors from synch.c, needed for enable -s and disable -s. 1020Sstevel@tonic-gate */ 1030Sstevel@tonic-gate extern int is_enabled(scf_instance_t *); 1040Sstevel@tonic-gate extern int has_potential(scf_instance_t *, int); 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate void 1070Sstevel@tonic-gate do_scfdie(int lineno) 1080Sstevel@tonic-gate { 1090Sstevel@tonic-gate scf_error_t err; 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate switch (err = scf_error()) { 1120Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 1130Sstevel@tonic-gate uu_die(gettext("Connection to repository server broken. " 1140Sstevel@tonic-gate "Exiting.\n")); 1150Sstevel@tonic-gate /* NOTREACHED */ 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate case SCF_ERROR_BACKEND_READONLY: 1180Sstevel@tonic-gate uu_die(gettext("Repository is read-only. Exiting.\n")); 1190Sstevel@tonic-gate /* NOTREACHED */ 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate default: 1220Sstevel@tonic-gate #ifdef NDEBUG 1230Sstevel@tonic-gate uu_die(gettext("Unexpected libscf error: %s. Exiting.\n"), 1240Sstevel@tonic-gate scf_strerror(err)); 1250Sstevel@tonic-gate #else 1260Sstevel@tonic-gate uu_die("Unexpected libscf error on line %d: %s.\n", lineno, 1270Sstevel@tonic-gate scf_strerror(err)); 1280Sstevel@tonic-gate #endif 1290Sstevel@tonic-gate } 1300Sstevel@tonic-gate } 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate #define scfdie() do_scfdie(__LINE__) 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate static void 1350Sstevel@tonic-gate usage() 1360Sstevel@tonic-gate { 1370Sstevel@tonic-gate (void) fprintf(stderr, gettext( 1380Sstevel@tonic-gate "Usage: %1$s [-v] [cmd [args ... ]]\n\n" 1390Sstevel@tonic-gate "\t%1$s enable [-rst] <service> ...\t- enable and online service(s)\n" 1400Sstevel@tonic-gate "\t%1$s disable [-st] <service> ...\t- disable and offline service(s)\n" 1410Sstevel@tonic-gate "\t%1$s restart <service> ...\t\t- restart specified service(s)\n" 1420Sstevel@tonic-gate "\t%1$s refresh <service> ...\t\t- re-read service configuration\n" 1430Sstevel@tonic-gate "\t%1$s mark [-It] <state> <service> ...\t- set maintenance state\n" 1440Sstevel@tonic-gate "\t%1$s clear <service> ...\t\t- clear maintenance state\n" 1450Sstevel@tonic-gate "\t%1$s milestone [-d] <milestone>\t- advance to a service milestone\n" 1460Sstevel@tonic-gate "\n\t" 1470Sstevel@tonic-gate "Services can be specified using an FMRI, abbreviation, or fnmatch(5)\n" 1480Sstevel@tonic-gate "\tpattern, as shown in these examples for svc:/network/smtp:sendmail\n" 1490Sstevel@tonic-gate "\n" 1500Sstevel@tonic-gate "\t%1$s <cmd> svc:/network/smtp:sendmail\n" 1510Sstevel@tonic-gate "\t%1$s <cmd> network/smtp:sendmail\n" 1520Sstevel@tonic-gate "\t%1$s <cmd> network/*mail\n" 1530Sstevel@tonic-gate "\t%1$s <cmd> network/smtp\n" 1540Sstevel@tonic-gate "\t%1$s <cmd> smtp:sendmail\n" 1550Sstevel@tonic-gate "\t%1$s <cmd> smtp\n" 1560Sstevel@tonic-gate "\t%1$s <cmd> sendmail\n"), uu_getpname()); 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate exit(UU_EXIT_USAGE); 1590Sstevel@tonic-gate } 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate /* 1630Sstevel@tonic-gate * FMRI hash table for recursive enable. 1640Sstevel@tonic-gate */ 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate static uint32_t 1670Sstevel@tonic-gate hash_fmri(const char *str) 1680Sstevel@tonic-gate { 1690Sstevel@tonic-gate uint32_t h = 0, g; 1700Sstevel@tonic-gate const char *p; 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate /* Generic hash function from uts/common/os/modhash.c . */ 1730Sstevel@tonic-gate for (p = str; *p != '\0'; ++p) { 1740Sstevel@tonic-gate h = (h << 4) + *p; 1750Sstevel@tonic-gate if ((g = (h & 0xf0000000)) != 0) { 1760Sstevel@tonic-gate h ^= (g >> 24); 1770Sstevel@tonic-gate h ^= g; 1780Sstevel@tonic-gate } 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate return (h); 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate /* 1850Sstevel@tonic-gate * Return 1 if str has been visited, 0 if it has not, and -1 if memory could not 1860Sstevel@tonic-gate * be allocated. 1870Sstevel@tonic-gate */ 1880Sstevel@tonic-gate static int 1890Sstevel@tonic-gate visited_find_or_add(const char *str, struct ht_elt **hep) 1900Sstevel@tonic-gate { 1910Sstevel@tonic-gate uint32_t h; 1920Sstevel@tonic-gate uint_t i; 1930Sstevel@tonic-gate struct ht_elt *he; 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate h = hash_fmri(str); 1960Sstevel@tonic-gate i = h & (HT_BUCKETS - 1); 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate for (he = visited[i]; he != NULL; he = he->next) { 1990Sstevel@tonic-gate if (strcmp(he->str, str) == 0) { 2000Sstevel@tonic-gate if (hep) 2010Sstevel@tonic-gate *hep = he; 2020Sstevel@tonic-gate return (1); 2030Sstevel@tonic-gate } 2040Sstevel@tonic-gate } 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate he = malloc(offsetof(struct ht_elt, str) + strlen(str) + 1); 2070Sstevel@tonic-gate if (he == NULL) 2080Sstevel@tonic-gate return (-1); 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate (void) strcpy(he->str, str); 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate he->next = visited[i]; 2130Sstevel@tonic-gate visited[i] = he; 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate if (hep) 2160Sstevel@tonic-gate *hep = he; 2170Sstevel@tonic-gate return (0); 2180Sstevel@tonic-gate } 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate /* 2220Sstevel@tonic-gate * Returns 0, ECANCELED if pg is deleted, ENOENT if propname doesn't exist, 2230Sstevel@tonic-gate * EINVAL if the property is not of boolean type or has no values, and E2BIG 2240Sstevel@tonic-gate * if it has more than one value. *bp is set if 0 or E2BIG is returned. 2250Sstevel@tonic-gate */ 2260Sstevel@tonic-gate int 2270Sstevel@tonic-gate get_bool_prop(scf_propertygroup_t *pg, const char *propname, uint8_t *bp) 2280Sstevel@tonic-gate { 2290Sstevel@tonic-gate scf_property_t *prop; 2300Sstevel@tonic-gate scf_value_t *val; 2310Sstevel@tonic-gate int ret; 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate if ((prop = scf_property_create(h)) == NULL || 2340Sstevel@tonic-gate (val = scf_value_create(h)) == NULL) 2350Sstevel@tonic-gate scfdie(); 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate if (scf_pg_get_property(pg, propname, prop) != 0) { 2380Sstevel@tonic-gate switch (scf_error()) { 2390Sstevel@tonic-gate case SCF_ERROR_DELETED: 2400Sstevel@tonic-gate ret = ECANCELED; 2410Sstevel@tonic-gate goto out; 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 2440Sstevel@tonic-gate ret = ENOENT; 2450Sstevel@tonic-gate goto out; 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 2480Sstevel@tonic-gate assert(0); 2490Sstevel@tonic-gate abort(); 2500Sstevel@tonic-gate /* NOTREACHED */ 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate default: 2530Sstevel@tonic-gate scfdie(); 2540Sstevel@tonic-gate } 2550Sstevel@tonic-gate } 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate if (scf_property_get_value(prop, val) == 0) { 2580Sstevel@tonic-gate ret = 0; 2590Sstevel@tonic-gate } else { 2600Sstevel@tonic-gate switch (scf_error()) { 2610Sstevel@tonic-gate case SCF_ERROR_DELETED: 2620Sstevel@tonic-gate ret = ENOENT; 2630Sstevel@tonic-gate goto out; 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 2660Sstevel@tonic-gate ret = EINVAL; 2670Sstevel@tonic-gate goto out; 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED: 2700Sstevel@tonic-gate ret = E2BIG; 2710Sstevel@tonic-gate break; 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 2740Sstevel@tonic-gate assert(0); 2750Sstevel@tonic-gate abort(); 2760Sstevel@tonic-gate /* NOTREACHED */ 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate default: 2790Sstevel@tonic-gate scfdie(); 2800Sstevel@tonic-gate } 2810Sstevel@tonic-gate } 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate if (scf_value_get_boolean(val, bp) != 0) { 2840Sstevel@tonic-gate if (scf_error() != SCF_ERROR_TYPE_MISMATCH) 2850Sstevel@tonic-gate scfdie(); 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate ret = EINVAL; 2880Sstevel@tonic-gate goto out; 2890Sstevel@tonic-gate } 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate out: 2920Sstevel@tonic-gate scf_value_destroy(val); 2930Sstevel@tonic-gate scf_property_destroy(prop); 2940Sstevel@tonic-gate return (ret); 2950Sstevel@tonic-gate } 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate /* 2980Sstevel@tonic-gate * Returns 0, EPERM, or EROFS. 2990Sstevel@tonic-gate */ 3000Sstevel@tonic-gate static int 3010Sstevel@tonic-gate set_bool_prop(scf_propertygroup_t *pg, const char *propname, boolean_t b) 3020Sstevel@tonic-gate { 3030Sstevel@tonic-gate scf_value_t *v; 3040Sstevel@tonic-gate scf_transaction_t *tx; 3050Sstevel@tonic-gate scf_transaction_entry_t *ent; 3060Sstevel@tonic-gate int ret = 0, r; 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate if ((tx = scf_transaction_create(h)) == NULL || 3090Sstevel@tonic-gate (ent = scf_entry_create(h)) == NULL || 3100Sstevel@tonic-gate (v = scf_value_create(h)) == NULL) 3110Sstevel@tonic-gate scfdie(); 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate scf_value_set_boolean(v, b); 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate for (;;) { 3160Sstevel@tonic-gate if (scf_transaction_start(tx, pg) == -1) { 3170Sstevel@tonic-gate switch (scf_error()) { 3180Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED: 3190Sstevel@tonic-gate ret = EPERM; 3200Sstevel@tonic-gate goto out; 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate case SCF_ERROR_BACKEND_READONLY: 3230Sstevel@tonic-gate ret = EROFS; 3240Sstevel@tonic-gate goto out; 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate default: 3270Sstevel@tonic-gate scfdie(); 3280Sstevel@tonic-gate } 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate if (scf_transaction_property_change_type(tx, ent, propname, 3320Sstevel@tonic-gate SCF_TYPE_BOOLEAN) != 0) { 3330Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) 3340Sstevel@tonic-gate scfdie(); 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate if (scf_transaction_property_new(tx, ent, propname, 3370Sstevel@tonic-gate SCF_TYPE_BOOLEAN) != 0) 3380Sstevel@tonic-gate scfdie(); 3390Sstevel@tonic-gate } 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate r = scf_entry_add_value(ent, v); 3420Sstevel@tonic-gate assert(r == 0); 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate r = scf_transaction_commit(tx); 3450Sstevel@tonic-gate if (r == 1) 3460Sstevel@tonic-gate break; 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate scf_transaction_reset(tx); 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate if (r != 0) { 3510Sstevel@tonic-gate switch (scf_error()) { 3520Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED: 3530Sstevel@tonic-gate ret = EPERM; 3540Sstevel@tonic-gate goto out; 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate case SCF_ERROR_BACKEND_READONLY: 3570Sstevel@tonic-gate ret = EROFS; 3580Sstevel@tonic-gate goto out; 3590Sstevel@tonic-gate 3600Sstevel@tonic-gate default: 3610Sstevel@tonic-gate scfdie(); 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate } 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate if (scf_pg_update(pg) == -1) 3660Sstevel@tonic-gate scfdie(); 3670Sstevel@tonic-gate } 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate out: 3700Sstevel@tonic-gate scf_transaction_destroy(tx); 3710Sstevel@tonic-gate scf_entry_destroy(ent); 3720Sstevel@tonic-gate scf_value_destroy(v); 3730Sstevel@tonic-gate return (ret); 3740Sstevel@tonic-gate } 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate /* 3770Sstevel@tonic-gate * Gets the single astring value of the propname property of pg. prop & v are 3780Sstevel@tonic-gate * scratch space. Returns the length of the string on success or 3790Sstevel@tonic-gate * -ENOENT - pg has no property named propname 3800Sstevel@tonic-gate * -E2BIG - property has no values or multiple values 3810Sstevel@tonic-gate * -EINVAL - property type is not compatible with astring 3820Sstevel@tonic-gate */ 3830Sstevel@tonic-gate ssize_t 3840Sstevel@tonic-gate get_astring_prop(const scf_propertygroup_t *pg, const char *propname, 3850Sstevel@tonic-gate scf_property_t *prop, scf_value_t *v, char *buf, size_t bufsz) 3860Sstevel@tonic-gate { 3870Sstevel@tonic-gate ssize_t sz; 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate if (scf_pg_get_property(pg, propname, prop) != 0) { 3900Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) 3910Sstevel@tonic-gate scfdie(); 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate return (-ENOENT); 3940Sstevel@tonic-gate } 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate if (scf_property_get_value(prop, v) != 0) { 3970Sstevel@tonic-gate switch (scf_error()) { 3980Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 3990Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED: 4000Sstevel@tonic-gate return (-E2BIG); 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate default: 4030Sstevel@tonic-gate scfdie(); 4040Sstevel@tonic-gate } 4050Sstevel@tonic-gate } 4060Sstevel@tonic-gate 4070Sstevel@tonic-gate sz = scf_value_get_astring(v, buf, bufsz); 4080Sstevel@tonic-gate if (sz < 0) { 4090Sstevel@tonic-gate if (scf_error() != SCF_ERROR_TYPE_MISMATCH) 4100Sstevel@tonic-gate scfdie(); 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate return (-EINVAL); 4130Sstevel@tonic-gate } 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate return (sz); 4160Sstevel@tonic-gate } 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate /* 4190Sstevel@tonic-gate * Returns 4200Sstevel@tonic-gate * 0 - success 4210Sstevel@tonic-gate * ECANCELED - pg was deleted 4220Sstevel@tonic-gate * EPERM - permission denied 4230Sstevel@tonic-gate * EACCES - access denied 4240Sstevel@tonic-gate * EROFS - readonly 4250Sstevel@tonic-gate */ 4260Sstevel@tonic-gate static int 4270Sstevel@tonic-gate delete_prop(scf_propertygroup_t *pg, const char *propname) 4280Sstevel@tonic-gate { 4290Sstevel@tonic-gate scf_transaction_t *tx; 4300Sstevel@tonic-gate scf_transaction_entry_t *ent; 4310Sstevel@tonic-gate int ret = 0, r; 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate if ((tx = scf_transaction_create(h)) == NULL || 4340Sstevel@tonic-gate (ent = scf_entry_create(h)) == NULL) 4350Sstevel@tonic-gate scfdie(); 4360Sstevel@tonic-gate 4370Sstevel@tonic-gate for (;;) { 4380Sstevel@tonic-gate if (scf_transaction_start(tx, pg) == -1) { 4390Sstevel@tonic-gate switch (scf_error()) { 4400Sstevel@tonic-gate case SCF_ERROR_DELETED: 4410Sstevel@tonic-gate ret = ECANCELED; 4420Sstevel@tonic-gate goto out; 4430Sstevel@tonic-gate 4440Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED: 4450Sstevel@tonic-gate ret = EPERM; 4460Sstevel@tonic-gate goto out; 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate case SCF_ERROR_BACKEND_ACCESS: 4490Sstevel@tonic-gate ret = EACCES; 4500Sstevel@tonic-gate goto out; 4510Sstevel@tonic-gate 4520Sstevel@tonic-gate case SCF_ERROR_BACKEND_READONLY: 4530Sstevel@tonic-gate ret = EROFS; 4540Sstevel@tonic-gate goto out; 4550Sstevel@tonic-gate 4560Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 4570Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 4580Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 4590Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 4600Sstevel@tonic-gate case SCF_ERROR_IN_USE: 4610Sstevel@tonic-gate default: 4620Sstevel@tonic-gate scfdie(); 4630Sstevel@tonic-gate } 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate if (scf_transaction_property_delete(tx, ent, propname) == -1) 4670Sstevel@tonic-gate switch (scf_error()) { 4680Sstevel@tonic-gate case SCF_ERROR_DELETED: 4690Sstevel@tonic-gate ret = ECANCELED; 4700Sstevel@tonic-gate goto out; 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 4730Sstevel@tonic-gate ret = 0; 4740Sstevel@tonic-gate goto out; 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 4770Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 4780Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 4790Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT: 4800Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 4810Sstevel@tonic-gate default: 4820Sstevel@tonic-gate scfdie(); 4830Sstevel@tonic-gate } 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate r = scf_transaction_commit(tx); 4860Sstevel@tonic-gate if (r == 1) 4870Sstevel@tonic-gate break; 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate scf_transaction_reset(tx); 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate if (r != 0) { 4920Sstevel@tonic-gate switch (scf_error()) { 4930Sstevel@tonic-gate case SCF_ERROR_DELETED: 4940Sstevel@tonic-gate ret = ECANCELED; 4950Sstevel@tonic-gate goto out; 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED: 4980Sstevel@tonic-gate ret = EPERM; 4990Sstevel@tonic-gate goto out; 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate case SCF_ERROR_BACKEND_ACCESS: 5020Sstevel@tonic-gate ret = EACCES; 5030Sstevel@tonic-gate goto out; 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate case SCF_ERROR_BACKEND_READONLY: 5060Sstevel@tonic-gate ret = EROFS; 5070Sstevel@tonic-gate goto out; 5080Sstevel@tonic-gate 5090Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT: 5100Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 5110Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 5120Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 5130Sstevel@tonic-gate default: 5140Sstevel@tonic-gate scfdie(); 5150Sstevel@tonic-gate } 5160Sstevel@tonic-gate } 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate if (scf_pg_update(pg) == -1) { 5190Sstevel@tonic-gate if (scf_error() != SCF_ERROR_DELETED) 5200Sstevel@tonic-gate scfdie(); 5210Sstevel@tonic-gate 5220Sstevel@tonic-gate ret = ECANCELED; 5230Sstevel@tonic-gate goto out; 5240Sstevel@tonic-gate } 5250Sstevel@tonic-gate } 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate out: 5280Sstevel@tonic-gate scf_transaction_destroy(tx); 5290Sstevel@tonic-gate scf_entry_destroy(ent); 5300Sstevel@tonic-gate return (ret); 5310Sstevel@tonic-gate } 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate /* 5340Sstevel@tonic-gate * Returns 0 or EPERM. 5350Sstevel@tonic-gate */ 5360Sstevel@tonic-gate static int 5370Sstevel@tonic-gate pg_get_or_add(scf_instance_t *inst, const char *pgname, const char *pgtype, 5380Sstevel@tonic-gate uint32_t pgflags, scf_propertygroup_t *pg) 5390Sstevel@tonic-gate { 5400Sstevel@tonic-gate again: 5410Sstevel@tonic-gate if (scf_instance_get_pg(inst, pgname, pg) == 0) 5420Sstevel@tonic-gate return (0); 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) 5450Sstevel@tonic-gate scfdie(); 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate if (scf_instance_add_pg(inst, pgname, pgtype, pgflags, pg) == 0) 5480Sstevel@tonic-gate return (0); 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate switch (scf_error()) { 5510Sstevel@tonic-gate case SCF_ERROR_EXISTS: 5520Sstevel@tonic-gate goto again; 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED: 5550Sstevel@tonic-gate return (EPERM); 5560Sstevel@tonic-gate 5570Sstevel@tonic-gate default: 5580Sstevel@tonic-gate scfdie(); 5590Sstevel@tonic-gate /* NOTREACHED */ 5600Sstevel@tonic-gate } 5610Sstevel@tonic-gate } 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate /* 5640Sstevel@tonic-gate * Enable or disable inst, per enable. If temp is true, set 5650Sstevel@tonic-gate * general_ovr/enabled. Otherwise set general/enabled and delete 5660Sstevel@tonic-gate * general_ovr/enabled if it exists (order is important here: we don't want the 5670Sstevel@tonic-gate * enabled status to glitch). 5680Sstevel@tonic-gate */ 5690Sstevel@tonic-gate static void 5700Sstevel@tonic-gate set_inst_enabled(const char *fmri, scf_instance_t *inst, boolean_t temp, 5710Sstevel@tonic-gate boolean_t enable) 5720Sstevel@tonic-gate { 5730Sstevel@tonic-gate scf_propertygroup_t *pg; 5740Sstevel@tonic-gate uint8_t b; 5750Sstevel@tonic-gate const char *pgname = NULL; /* For emsg_pg_perm_denied */ 5760Sstevel@tonic-gate int r; 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate pg = scf_pg_create(h); 5790Sstevel@tonic-gate if (pg == NULL) 5800Sstevel@tonic-gate scfdie(); 5810Sstevel@tonic-gate 582*4171Stn143363 /* 583*4171Stn143363 * An instance's configuration is incomplete if general/enabled 584*4171Stn143363 * doesn't exist. Create both the property group and property 585*4171Stn143363 * here if they don't exist. 586*4171Stn143363 */ 587*4171Stn143363 pgname = SCF_PG_GENERAL; 588*4171Stn143363 if (pg_get_or_add(inst, pgname, SCF_PG_GENERAL_TYPE, 589*4171Stn143363 SCF_PG_GENERAL_FLAGS, pg) != 0) 590*4171Stn143363 goto eperm; 591*4171Stn143363 592*4171Stn143363 if (get_bool_prop(pg, SCF_PROPERTY_ENABLED, &b) != 0) { 593*4171Stn143363 /* Create and set state to disabled */ 594*4171Stn143363 switch (set_bool_prop(pg, SCF_PROPERTY_ENABLED, B_FALSE) != 0) { 595*4171Stn143363 case 0: 596*4171Stn143363 break; 597*4171Stn143363 598*4171Stn143363 case EPERM: 599*4171Stn143363 goto eperm; 600*4171Stn143363 601*4171Stn143363 case EROFS: 602*4171Stn143363 /* Shouldn't happen, but it can. */ 603*4171Stn143363 if (!verbose) 604*4171Stn143363 uu_warn(gettext("%s: Repository read-only.\n"), 605*4171Stn143363 fmri); 606*4171Stn143363 else 607*4171Stn143363 uu_warn(gettext("%s: Could not set %s/%s " 608*4171Stn143363 "(repository read-only).\n"), fmri, 609*4171Stn143363 SCF_PG_GENERAL, SCF_PROPERTY_ENABLED); 610*4171Stn143363 goto out; 611*4171Stn143363 612*4171Stn143363 default: 613*4171Stn143363 assert(0); 614*4171Stn143363 abort(); 615*4171Stn143363 } 616*4171Stn143363 } 617*4171Stn143363 6180Sstevel@tonic-gate if (temp) { 6190Sstevel@tonic-gate /* Set general_ovr/enabled */ 6200Sstevel@tonic-gate pgname = SCF_PG_GENERAL_OVR; 6210Sstevel@tonic-gate if (pg_get_or_add(inst, pgname, SCF_PG_GENERAL_OVR_TYPE, 6220Sstevel@tonic-gate SCF_PG_GENERAL_OVR_FLAGS, pg) != 0) 6230Sstevel@tonic-gate goto eperm; 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate switch (set_bool_prop(pg, SCF_PROPERTY_ENABLED, enable) != 0) { 6260Sstevel@tonic-gate case 0: 6270Sstevel@tonic-gate break; 6280Sstevel@tonic-gate 6290Sstevel@tonic-gate case EPERM: 6300Sstevel@tonic-gate goto eperm; 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate case EROFS: 6330Sstevel@tonic-gate /* Shouldn't happen, but it can. */ 6340Sstevel@tonic-gate if (!verbose) 6350Sstevel@tonic-gate uu_warn(gettext("%s: Repository read-only.\n"), 6360Sstevel@tonic-gate fmri); 6370Sstevel@tonic-gate else 6380Sstevel@tonic-gate uu_warn(gettext("%s: Could not set %s/%s " 6390Sstevel@tonic-gate "(repository read-only).\n"), fmri, 6400Sstevel@tonic-gate SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED); 6410Sstevel@tonic-gate goto out; 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate default: 6440Sstevel@tonic-gate assert(0); 6450Sstevel@tonic-gate abort(); 6460Sstevel@tonic-gate } 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate if (verbose) 6490Sstevel@tonic-gate (void) printf(enable ? 6500Sstevel@tonic-gate gettext("%s temporarily enabled.\n") : 6510Sstevel@tonic-gate gettext("%s temporarily disabled.\n"), fmri); 6520Sstevel@tonic-gate } else { 6530Sstevel@tonic-gate again: 654*4171Stn143363 /* 655*4171Stn143363 * Both pg and property should exist since we created 656*4171Stn143363 * them earlier. However, there's still a chance that 657*4171Stn143363 * someone may have deleted the property out from under 658*4171Stn143363 * us. 659*4171Stn143363 */ 6600Sstevel@tonic-gate if (pg_get_or_add(inst, pgname, SCF_PG_GENERAL_TYPE, 6610Sstevel@tonic-gate SCF_PG_GENERAL_FLAGS, pg) != 0) 6620Sstevel@tonic-gate goto eperm; 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate switch (set_bool_prop(pg, SCF_PROPERTY_ENABLED, enable)) { 6650Sstevel@tonic-gate case 0: 6660Sstevel@tonic-gate break; 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate case EPERM: 6690Sstevel@tonic-gate goto eperm; 6700Sstevel@tonic-gate 6710Sstevel@tonic-gate case EROFS: 6720Sstevel@tonic-gate /* 6730Sstevel@tonic-gate * If general/enabled is already set the way we want, 6740Sstevel@tonic-gate * proceed. 6750Sstevel@tonic-gate */ 6760Sstevel@tonic-gate switch (get_bool_prop(pg, SCF_PROPERTY_ENABLED, &b)) { 6770Sstevel@tonic-gate case 0: 6780Sstevel@tonic-gate if ((b != 0) == (enable != B_FALSE)) 6790Sstevel@tonic-gate break; 6800Sstevel@tonic-gate /* FALLTHROUGH */ 6810Sstevel@tonic-gate 6820Sstevel@tonic-gate case ENOENT: 6830Sstevel@tonic-gate case EINVAL: 6840Sstevel@tonic-gate case E2BIG: 6850Sstevel@tonic-gate if (!verbose) 6860Sstevel@tonic-gate uu_warn(gettext("%s: Repository " 6870Sstevel@tonic-gate "read-only.\n"), fmri); 6880Sstevel@tonic-gate else 6890Sstevel@tonic-gate uu_warn(gettext("%s: Could not set " 6900Sstevel@tonic-gate "%s/%s (repository read-only).\n"), 6910Sstevel@tonic-gate fmri, SCF_PG_GENERAL, 6920Sstevel@tonic-gate SCF_PROPERTY_ENABLED); 6930Sstevel@tonic-gate goto out; 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate case ECANCELED: 6960Sstevel@tonic-gate goto again; 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate default: 6990Sstevel@tonic-gate assert(0); 7000Sstevel@tonic-gate abort(); 7010Sstevel@tonic-gate } 7020Sstevel@tonic-gate break; 7030Sstevel@tonic-gate 7040Sstevel@tonic-gate default: 7050Sstevel@tonic-gate assert(0); 7060Sstevel@tonic-gate abort(); 7070Sstevel@tonic-gate } 7080Sstevel@tonic-gate 7090Sstevel@tonic-gate pgname = SCF_PG_GENERAL_OVR; 7100Sstevel@tonic-gate if (scf_instance_get_pg(inst, pgname, pg) == 0) { 7110Sstevel@tonic-gate r = delete_prop(pg, SCF_PROPERTY_ENABLED); 7120Sstevel@tonic-gate switch (r) { 7130Sstevel@tonic-gate case 0: 7140Sstevel@tonic-gate break; 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate case ECANCELED: 7170Sstevel@tonic-gate uu_warn(emsg_no_service, fmri); 7180Sstevel@tonic-gate goto out; 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate case EPERM: 7210Sstevel@tonic-gate goto eperm; 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate case EACCES: 7240Sstevel@tonic-gate uu_warn(gettext("Could not delete %s/%s " 7250Sstevel@tonic-gate "property of %s: backend access denied.\n"), 7260Sstevel@tonic-gate pgname, SCF_PROPERTY_ENABLED, fmri); 7270Sstevel@tonic-gate goto out; 7280Sstevel@tonic-gate 7290Sstevel@tonic-gate case EROFS: 7300Sstevel@tonic-gate uu_warn(gettext("Could not delete %s/%s " 7310Sstevel@tonic-gate "property of %s: backend is read-only.\n"), 7320Sstevel@tonic-gate pgname, SCF_PROPERTY_ENABLED, fmri); 7330Sstevel@tonic-gate goto out; 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate default: 7360Sstevel@tonic-gate bad_error("delete_prop", r); 7370Sstevel@tonic-gate } 7380Sstevel@tonic-gate } else { 7390Sstevel@tonic-gate switch (scf_error()) { 7400Sstevel@tonic-gate case SCF_ERROR_DELETED: 7410Sstevel@tonic-gate /* Print something? */ 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 7440Sstevel@tonic-gate break; 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 7470Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT: 7480Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 7490Sstevel@tonic-gate assert(0); 7500Sstevel@tonic-gate abort(); 7510Sstevel@tonic-gate /* NOTREACHED */ 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 7540Sstevel@tonic-gate default: 7550Sstevel@tonic-gate scfdie(); 7560Sstevel@tonic-gate } 7570Sstevel@tonic-gate } 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate if (verbose) 7600Sstevel@tonic-gate (void) printf(enable ? gettext("%s enabled.\n") : 7610Sstevel@tonic-gate gettext("%s disabled.\n"), fmri); 7620Sstevel@tonic-gate } 7630Sstevel@tonic-gate 7640Sstevel@tonic-gate scf_pg_destroy(pg); 7650Sstevel@tonic-gate return; 7660Sstevel@tonic-gate 7670Sstevel@tonic-gate eperm: 7680Sstevel@tonic-gate assert(pgname != NULL); 7690Sstevel@tonic-gate if (!verbose) 7700Sstevel@tonic-gate uu_warn(emsg_permission_denied, fmri); 7710Sstevel@tonic-gate else 7720Sstevel@tonic-gate uu_warn(emsg_pg_perm_denied, fmri, pgname); 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate out: 7750Sstevel@tonic-gate scf_pg_destroy(pg); 7760Sstevel@tonic-gate exit_status = 1; 7770Sstevel@tonic-gate } 7780Sstevel@tonic-gate 7790Sstevel@tonic-gate /* 7800Sstevel@tonic-gate * Set inst to the instance which corresponds to fmri. If fmri identifies 7810Sstevel@tonic-gate * a service with a single instance, get that instance. 7820Sstevel@tonic-gate * 7830Sstevel@tonic-gate * Fails with 7840Sstevel@tonic-gate * ENOTSUP - fmri has an unsupported scheme 7850Sstevel@tonic-gate * EINVAL - fmri is invalid 7860Sstevel@tonic-gate * ENOTDIR - fmri does not identify a service or instance 7870Sstevel@tonic-gate * ENOENT - could not locate instance 7880Sstevel@tonic-gate * E2BIG - fmri is a service with multiple instances (warning not printed) 7890Sstevel@tonic-gate */ 7900Sstevel@tonic-gate static int 7910Sstevel@tonic-gate get_inst_mult(const char *fmri, scf_instance_t *inst) 7920Sstevel@tonic-gate { 7930Sstevel@tonic-gate char *cfmri; 7940Sstevel@tonic-gate const char *svc_name, *inst_name, *pg_name; 7950Sstevel@tonic-gate scf_service_t *svc; 7960Sstevel@tonic-gate scf_instance_t *inst2; 7970Sstevel@tonic-gate scf_iter_t *iter; 7980Sstevel@tonic-gate int ret; 7990Sstevel@tonic-gate 8000Sstevel@tonic-gate if (strncmp(fmri, "lrc:", sizeof ("lrc:") - 1) == 0) { 8010Sstevel@tonic-gate uu_warn(gettext("FMRI \"%s\" is a legacy service.\n"), fmri); 8020Sstevel@tonic-gate exit_status = 1; 8030Sstevel@tonic-gate return (ENOTSUP); 8040Sstevel@tonic-gate } 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate cfmri = strdup(fmri); 8070Sstevel@tonic-gate if (cfmri == NULL) 8080Sstevel@tonic-gate uu_die(emsg_nomem); 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate if (scf_parse_svc_fmri(cfmri, NULL, &svc_name, &inst_name, &pg_name, 8110Sstevel@tonic-gate NULL) != SCF_SUCCESS) { 8120Sstevel@tonic-gate free(cfmri); 8130Sstevel@tonic-gate uu_warn(gettext("FMRI \"%s\" is invalid.\n"), fmri); 8140Sstevel@tonic-gate exit_status = 1; 8150Sstevel@tonic-gate return (EINVAL); 8160Sstevel@tonic-gate } 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate free(cfmri); 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate if (svc_name == NULL || pg_name != NULL) { 8210Sstevel@tonic-gate uu_warn(gettext( 8220Sstevel@tonic-gate "FMRI \"%s\" does not designate a service or instance.\n"), 8230Sstevel@tonic-gate fmri); 8240Sstevel@tonic-gate exit_status = 1; 8250Sstevel@tonic-gate return (ENOTDIR); 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate if (inst_name != NULL) { 8290Sstevel@tonic-gate if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL, 8300Sstevel@tonic-gate NULL, SCF_DECODE_FMRI_EXACT) == 0) 8310Sstevel@tonic-gate return (0); 8320Sstevel@tonic-gate 8330Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) 8340Sstevel@tonic-gate scfdie(); 8350Sstevel@tonic-gate 8360Sstevel@tonic-gate uu_warn(gettext("No such instance \"%s\".\n"), fmri); 8370Sstevel@tonic-gate exit_status = 1; 8380Sstevel@tonic-gate 8390Sstevel@tonic-gate return (ENOENT); 8400Sstevel@tonic-gate } 8410Sstevel@tonic-gate 8420Sstevel@tonic-gate if ((svc = scf_service_create(h)) == NULL || 8430Sstevel@tonic-gate (inst2 = scf_instance_create(h)) == NULL || 8440Sstevel@tonic-gate (iter = scf_iter_create(h)) == NULL) 8450Sstevel@tonic-gate scfdie(); 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL, 8480Sstevel@tonic-gate SCF_DECODE_FMRI_EXACT) != SCF_SUCCESS) { 8490Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) 8500Sstevel@tonic-gate scfdie(); 8510Sstevel@tonic-gate 8520Sstevel@tonic-gate uu_warn(emsg_no_service, fmri); 8530Sstevel@tonic-gate exit_status = 1; 8540Sstevel@tonic-gate 8550Sstevel@tonic-gate ret = ENOENT; 8560Sstevel@tonic-gate goto out; 8570Sstevel@tonic-gate } 8580Sstevel@tonic-gate 8590Sstevel@tonic-gate /* If the service has only one child, use it. */ 8600Sstevel@tonic-gate if (scf_iter_service_instances(iter, svc) != SCF_SUCCESS) 8610Sstevel@tonic-gate scfdie(); 8620Sstevel@tonic-gate 8630Sstevel@tonic-gate ret = scf_iter_next_instance(iter, inst); 8640Sstevel@tonic-gate if (ret < 0) 8650Sstevel@tonic-gate scfdie(); 8660Sstevel@tonic-gate if (ret != 1) { 8670Sstevel@tonic-gate uu_warn(gettext("Service \"%s\" has no instances.\n"), 8680Sstevel@tonic-gate fmri); 8690Sstevel@tonic-gate exit_status = 1; 8700Sstevel@tonic-gate ret = ENOENT; 8710Sstevel@tonic-gate goto out; 8720Sstevel@tonic-gate } 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate ret = scf_iter_next_instance(iter, inst2); 8750Sstevel@tonic-gate if (ret < 0) 8760Sstevel@tonic-gate scfdie(); 8770Sstevel@tonic-gate 8780Sstevel@tonic-gate if (ret != 0) { 8790Sstevel@tonic-gate ret = E2BIG; 8800Sstevel@tonic-gate goto out; 8810Sstevel@tonic-gate } 8820Sstevel@tonic-gate 8830Sstevel@tonic-gate ret = 0; 8840Sstevel@tonic-gate 8850Sstevel@tonic-gate out: 8860Sstevel@tonic-gate scf_iter_destroy(iter); 8870Sstevel@tonic-gate scf_instance_destroy(inst2); 8880Sstevel@tonic-gate scf_service_destroy(svc); 8890Sstevel@tonic-gate return (ret); 8900Sstevel@tonic-gate } 8910Sstevel@tonic-gate 8920Sstevel@tonic-gate /* 8930Sstevel@tonic-gate * Same as get_inst_mult(), but on E2BIG prints a warning and returns ENOENT. 8940Sstevel@tonic-gate */ 8950Sstevel@tonic-gate static int 8960Sstevel@tonic-gate get_inst(const char *fmri, scf_instance_t *inst) 8970Sstevel@tonic-gate { 8980Sstevel@tonic-gate int r; 8990Sstevel@tonic-gate 9000Sstevel@tonic-gate r = get_inst_mult(fmri, inst); 9010Sstevel@tonic-gate if (r != E2BIG) 9020Sstevel@tonic-gate return (r); 9030Sstevel@tonic-gate 9040Sstevel@tonic-gate uu_warn(gettext("operation on service %s is ambiguous; " 9050Sstevel@tonic-gate "instance specification needed.\n"), fmri); 9060Sstevel@tonic-gate return (ENOENT); 9070Sstevel@tonic-gate } 9080Sstevel@tonic-gate 9090Sstevel@tonic-gate static char * 9100Sstevel@tonic-gate inst_get_fmri(const scf_instance_t *inst) 9110Sstevel@tonic-gate { 9120Sstevel@tonic-gate ssize_t sz; 9130Sstevel@tonic-gate 9140Sstevel@tonic-gate sz = scf_instance_to_fmri(inst, scratch_fmri, max_scf_fmri_sz); 9150Sstevel@tonic-gate if (sz < 0) 9160Sstevel@tonic-gate scfdie(); 9170Sstevel@tonic-gate if (sz >= max_scf_fmri_sz) 9180Sstevel@tonic-gate uu_die(gettext("scf_instance_to_fmri() returned unexpectedly " 9190Sstevel@tonic-gate "long value.\n")); 9200Sstevel@tonic-gate 9210Sstevel@tonic-gate return (scratch_fmri); 9220Sstevel@tonic-gate } 9230Sstevel@tonic-gate 9240Sstevel@tonic-gate static ssize_t 9250Sstevel@tonic-gate dep_get_astring(const char *fmri, const char *pgname, 9260Sstevel@tonic-gate const scf_propertygroup_t *pg, const char *propname, scf_property_t *prop, 9270Sstevel@tonic-gate scf_value_t *v, char *buf, size_t bufsz) 9280Sstevel@tonic-gate { 9290Sstevel@tonic-gate ssize_t sz; 9300Sstevel@tonic-gate 9310Sstevel@tonic-gate sz = get_astring_prop(pg, propname, prop, v, buf, bufsz); 9320Sstevel@tonic-gate if (sz >= 0) 9330Sstevel@tonic-gate return (sz); 9340Sstevel@tonic-gate 9350Sstevel@tonic-gate switch (-sz) { 9360Sstevel@tonic-gate case ENOENT: 9370Sstevel@tonic-gate uu_warn(gettext("\"%s\" is misconfigured (\"%s\" dependency " 9380Sstevel@tonic-gate "lacks \"%s\" property.)\n"), fmri, pgname, propname); 9390Sstevel@tonic-gate return (-1); 9400Sstevel@tonic-gate 9410Sstevel@tonic-gate case E2BIG: 9420Sstevel@tonic-gate uu_warn(gettext("\"%s\" is misconfigured (\"%s/%s\" property " 9430Sstevel@tonic-gate "is not single-valued.)\n"), fmri, pgname, propname); 9440Sstevel@tonic-gate return (-1); 9450Sstevel@tonic-gate 9460Sstevel@tonic-gate case EINVAL: 9470Sstevel@tonic-gate uu_warn(gettext("\"%s\" is misconfigured (\"%s/%s\" property " 9480Sstevel@tonic-gate "is not of astring type.)\n"), fmri, pgname, propname); 9490Sstevel@tonic-gate return (-1); 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate default: 9520Sstevel@tonic-gate assert(0); 9530Sstevel@tonic-gate abort(); 9540Sstevel@tonic-gate /* NOTREACHED */ 9550Sstevel@tonic-gate } 9560Sstevel@tonic-gate } 9570Sstevel@tonic-gate 9580Sstevel@tonic-gate static boolean_t 9590Sstevel@tonic-gate multiple_instances(scf_iter_t *iter, scf_value_t *v, char *buf) 9600Sstevel@tonic-gate { 9610Sstevel@tonic-gate int count = 0, r; 9620Sstevel@tonic-gate boolean_t ret; 9630Sstevel@tonic-gate scf_instance_t *inst; 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate inst = scf_instance_create(h); 9660Sstevel@tonic-gate if (inst == NULL) 9670Sstevel@tonic-gate scfdie(); 9680Sstevel@tonic-gate 9690Sstevel@tonic-gate for (;;) { 9700Sstevel@tonic-gate r = scf_iter_next_value(iter, v); 9710Sstevel@tonic-gate if (r == 0) { 9720Sstevel@tonic-gate ret = B_FALSE; 9730Sstevel@tonic-gate goto out; 9740Sstevel@tonic-gate } 9750Sstevel@tonic-gate if (r != 1) 9760Sstevel@tonic-gate scfdie(); 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate if (scf_value_get_astring(v, buf, max_scf_fmri_sz) < 0) 9790Sstevel@tonic-gate scfdie(); 9800Sstevel@tonic-gate 9810Sstevel@tonic-gate switch (get_inst_mult(buf, inst)) { 9820Sstevel@tonic-gate case 0: 9830Sstevel@tonic-gate ++count; 9840Sstevel@tonic-gate if (count > 1) { 9850Sstevel@tonic-gate ret = B_TRUE; 9860Sstevel@tonic-gate goto out; 9870Sstevel@tonic-gate } 9880Sstevel@tonic-gate break; 9890Sstevel@tonic-gate 9900Sstevel@tonic-gate case ENOTSUP: 9910Sstevel@tonic-gate case EINVAL: 9920Sstevel@tonic-gate case ENOTDIR: 9930Sstevel@tonic-gate case ENOENT: 9940Sstevel@tonic-gate continue; 9950Sstevel@tonic-gate 9960Sstevel@tonic-gate case E2BIG: 9970Sstevel@tonic-gate ret = B_TRUE; 9980Sstevel@tonic-gate goto out; 9990Sstevel@tonic-gate 10000Sstevel@tonic-gate default: 10010Sstevel@tonic-gate assert(0); 10020Sstevel@tonic-gate abort(); 10030Sstevel@tonic-gate } 10040Sstevel@tonic-gate } 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate out: 10070Sstevel@tonic-gate scf_instance_destroy(inst); 10080Sstevel@tonic-gate return (ret); 10090Sstevel@tonic-gate } 10100Sstevel@tonic-gate 10110Sstevel@tonic-gate /* 10120Sstevel@tonic-gate * Enable the service or instance identified by fmri and its dependencies, 10130Sstevel@tonic-gate * recursively. Specifically, call get_inst(fmri), enable the result, and 10140Sstevel@tonic-gate * recurse on its restarter and the dependencies. To avoid duplication of 10150Sstevel@tonic-gate * effort or looping around a dependency cycle, each FMRI is entered into the 10160Sstevel@tonic-gate * "visited" hash table. While recursing, the hash table entry is marked 10170Sstevel@tonic-gate * "active", so that if we come upon it again, we know we've hit a cycle. 10180Sstevel@tonic-gate * exclude_all and optional_all dependencies are ignored. require_any 10190Sstevel@tonic-gate * dependencies are followed only if they comprise a single service; otherwise 10200Sstevel@tonic-gate * the user is warned. 10210Sstevel@tonic-gate * 10220Sstevel@tonic-gate * fmri must point to a writable max_scf_fmri_sz buffer. Returns EINVAL if fmri 10230Sstevel@tonic-gate * is invalid, E2BIG if fmri identifies a service with multiple instances, ELOOP 10240Sstevel@tonic-gate * on cycle detection, or 0 on success. 10250Sstevel@tonic-gate */ 10260Sstevel@tonic-gate static int 10270Sstevel@tonic-gate enable_fmri_rec(char *fmri, boolean_t temp) 10280Sstevel@tonic-gate { 10290Sstevel@tonic-gate scf_instance_t *inst; 10300Sstevel@tonic-gate scf_snapshot_t *snap; 10310Sstevel@tonic-gate scf_propertygroup_t *pg; 10320Sstevel@tonic-gate scf_property_t *prop; 10330Sstevel@tonic-gate scf_value_t *v; 10340Sstevel@tonic-gate scf_iter_t *pg_iter, *val_iter; 10350Sstevel@tonic-gate scf_type_t ty; 10360Sstevel@tonic-gate char *buf, *pgname; 10370Sstevel@tonic-gate ssize_t name_sz, len, sz; 10380Sstevel@tonic-gate int ret; 10390Sstevel@tonic-gate struct ht_elt *he; 10400Sstevel@tonic-gate 10410Sstevel@tonic-gate len = scf_canonify_fmri(fmri, fmri, max_scf_fmri_sz); 10420Sstevel@tonic-gate if (len < 0) { 10430Sstevel@tonic-gate assert(scf_error() == SCF_ERROR_INVALID_ARGUMENT); 10440Sstevel@tonic-gate return (EINVAL); 10450Sstevel@tonic-gate } 10460Sstevel@tonic-gate assert(len < max_scf_fmri_sz); 10470Sstevel@tonic-gate 10480Sstevel@tonic-gate switch (visited_find_or_add(fmri, &he)) { 10490Sstevel@tonic-gate case 0: 10500Sstevel@tonic-gate he->active = B_TRUE; 10510Sstevel@tonic-gate break; 10520Sstevel@tonic-gate 10530Sstevel@tonic-gate case 1: 10540Sstevel@tonic-gate return (he->active ? ELOOP : 0); 10550Sstevel@tonic-gate 10560Sstevel@tonic-gate case -1: 10570Sstevel@tonic-gate uu_die(emsg_nomem); 10580Sstevel@tonic-gate 10590Sstevel@tonic-gate default: 10600Sstevel@tonic-gate assert(0); 10610Sstevel@tonic-gate abort(); 10620Sstevel@tonic-gate } 10630Sstevel@tonic-gate 10640Sstevel@tonic-gate inst = scf_instance_create(h); 10650Sstevel@tonic-gate if (inst == NULL) 10660Sstevel@tonic-gate scfdie(); 10670Sstevel@tonic-gate 10680Sstevel@tonic-gate switch (get_inst_mult(fmri, inst)) { 10690Sstevel@tonic-gate case 0: 10700Sstevel@tonic-gate break; 10710Sstevel@tonic-gate 10720Sstevel@tonic-gate case E2BIG: 10730Sstevel@tonic-gate he->active = B_FALSE; 10740Sstevel@tonic-gate return (E2BIG); 10750Sstevel@tonic-gate 10760Sstevel@tonic-gate default: 10770Sstevel@tonic-gate he->active = B_FALSE; 10780Sstevel@tonic-gate return (0); 10790Sstevel@tonic-gate } 10800Sstevel@tonic-gate 10810Sstevel@tonic-gate set_inst_enabled(fmri, inst, temp, B_TRUE); 10820Sstevel@tonic-gate 10830Sstevel@tonic-gate if ((snap = scf_snapshot_create(h)) == NULL || 10840Sstevel@tonic-gate (pg = scf_pg_create(h)) == NULL || 10850Sstevel@tonic-gate (prop = scf_property_create(h)) == NULL || 10860Sstevel@tonic-gate (v = scf_value_create(h)) == NULL || 10870Sstevel@tonic-gate (pg_iter = scf_iter_create(h)) == NULL || 10880Sstevel@tonic-gate (val_iter = scf_iter_create(h)) == NULL) 10890Sstevel@tonic-gate scfdie(); 10900Sstevel@tonic-gate 10910Sstevel@tonic-gate buf = malloc(max_scf_fmri_sz); 10920Sstevel@tonic-gate if (buf == NULL) 10930Sstevel@tonic-gate uu_die(emsg_nomem); 10940Sstevel@tonic-gate 10950Sstevel@tonic-gate name_sz = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH); 10960Sstevel@tonic-gate if (name_sz < 0) 10970Sstevel@tonic-gate scfdie(); 10980Sstevel@tonic-gate ++name_sz; 10990Sstevel@tonic-gate pgname = malloc(name_sz); 11000Sstevel@tonic-gate if (pgname == NULL) 11010Sstevel@tonic-gate uu_die(emsg_nomem); 11020Sstevel@tonic-gate 11030Sstevel@tonic-gate if (scf_instance_get_snapshot(inst, "running", snap) != 0) { 11040Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) 11050Sstevel@tonic-gate scfdie(); 11060Sstevel@tonic-gate 11070Sstevel@tonic-gate scf_snapshot_destroy(snap); 11080Sstevel@tonic-gate snap = NULL; 11090Sstevel@tonic-gate } 11100Sstevel@tonic-gate 11110Sstevel@tonic-gate /* Enable restarter */ 11120Sstevel@tonic-gate if (scf_instance_get_pg_composed(inst, snap, SCF_PG_GENERAL, pg) != 0) { 11130Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) 11140Sstevel@tonic-gate scfdie(); 11150Sstevel@tonic-gate 11160Sstevel@tonic-gate uu_warn(gettext("\"%s\" is misconfigured (lacks \"%s\" " 11170Sstevel@tonic-gate "property group).\n"), fmri, SCF_PG_GENERAL); 11180Sstevel@tonic-gate ret = 0; 11190Sstevel@tonic-gate goto out; 11200Sstevel@tonic-gate } 11210Sstevel@tonic-gate 11220Sstevel@tonic-gate sz = get_astring_prop(pg, SCF_PROPERTY_RESTARTER, prop, v, buf, 11230Sstevel@tonic-gate max_scf_fmri_sz); 11240Sstevel@tonic-gate if (sz > max_scf_fmri_sz) { 11250Sstevel@tonic-gate uu_warn(gettext("\"%s\" is misconfigured (the value of " 11260Sstevel@tonic-gate "\"%s/%s\" is too long).\n"), fmri, SCF_PG_GENERAL, 11270Sstevel@tonic-gate SCF_PROPERTY_RESTARTER); 11280Sstevel@tonic-gate ret = 0; 11290Sstevel@tonic-gate goto out; 11300Sstevel@tonic-gate } else if (sz >= 0) { 11310Sstevel@tonic-gate switch (enable_fmri_rec(buf, temp)) { 11320Sstevel@tonic-gate case 0: 11330Sstevel@tonic-gate break; 11340Sstevel@tonic-gate 11350Sstevel@tonic-gate case EINVAL: 11360Sstevel@tonic-gate uu_warn(gettext("Restarter FMRI for \"%s\" is " 11370Sstevel@tonic-gate "invalid.\n"), fmri); 11380Sstevel@tonic-gate break; 11390Sstevel@tonic-gate 11400Sstevel@tonic-gate case E2BIG: 11410Sstevel@tonic-gate uu_warn(gettext("Restarter FMRI for \"%s\" identifies " 11420Sstevel@tonic-gate "a service with multiple instances.\n"), fmri); 11430Sstevel@tonic-gate break; 11440Sstevel@tonic-gate 11450Sstevel@tonic-gate case ELOOP: 11460Sstevel@tonic-gate ret = ELOOP; 11470Sstevel@tonic-gate goto out; 11480Sstevel@tonic-gate 11490Sstevel@tonic-gate default: 11500Sstevel@tonic-gate assert(0); 11510Sstevel@tonic-gate abort(); 11520Sstevel@tonic-gate } 11530Sstevel@tonic-gate } else if (sz < 0) { 11540Sstevel@tonic-gate switch (-sz) { 11550Sstevel@tonic-gate case ENOENT: 11560Sstevel@tonic-gate break; 11570Sstevel@tonic-gate 11580Sstevel@tonic-gate case E2BIG: 11590Sstevel@tonic-gate uu_warn(gettext("\"%s\" is misconfigured (\"%s/%s\" " 11600Sstevel@tonic-gate "property is not single-valued).\n"), fmri, 11610Sstevel@tonic-gate SCF_PG_GENERAL, SCF_PROPERTY_RESTARTER); 11620Sstevel@tonic-gate ret = 0; 11630Sstevel@tonic-gate goto out; 11640Sstevel@tonic-gate 11650Sstevel@tonic-gate case EINVAL: 11660Sstevel@tonic-gate uu_warn(gettext("\"%s\" is misconfigured (\"%s/%s\" " 11670Sstevel@tonic-gate "property is not of astring type).\n"), fmri, 11680Sstevel@tonic-gate SCF_PG_GENERAL, SCF_PROPERTY_RESTARTER); 11690Sstevel@tonic-gate ret = 0; 11700Sstevel@tonic-gate goto out; 11710Sstevel@tonic-gate 11720Sstevel@tonic-gate default: 11730Sstevel@tonic-gate assert(0); 11740Sstevel@tonic-gate abort(); 11750Sstevel@tonic-gate } 11760Sstevel@tonic-gate } 11770Sstevel@tonic-gate 11780Sstevel@tonic-gate if (scf_iter_instance_pgs_typed_composed(pg_iter, inst, snap, 11790Sstevel@tonic-gate SCF_GROUP_DEPENDENCY) == -1) 11800Sstevel@tonic-gate scfdie(); 11810Sstevel@tonic-gate 11820Sstevel@tonic-gate while (scf_iter_next_pg(pg_iter, pg) > 0) { 11830Sstevel@tonic-gate len = scf_pg_get_name(pg, pgname, name_sz); 11840Sstevel@tonic-gate if (len < 0) 11850Sstevel@tonic-gate scfdie(); 11860Sstevel@tonic-gate assert(len < name_sz); 11870Sstevel@tonic-gate 11880Sstevel@tonic-gate if (dep_get_astring(fmri, pgname, pg, SCF_PROPERTY_TYPE, prop, 11890Sstevel@tonic-gate v, buf, max_scf_fmri_sz) < 0) 11900Sstevel@tonic-gate continue; 11910Sstevel@tonic-gate 11920Sstevel@tonic-gate if (strcmp(buf, "service") != 0) 11930Sstevel@tonic-gate continue; 11940Sstevel@tonic-gate 11950Sstevel@tonic-gate if (dep_get_astring(fmri, pgname, pg, SCF_PROPERTY_GROUPING, 11960Sstevel@tonic-gate prop, v, buf, max_scf_fmri_sz) < 0) 11970Sstevel@tonic-gate continue; 11980Sstevel@tonic-gate 11990Sstevel@tonic-gate if (strcmp(buf, SCF_DEP_EXCLUDE_ALL) == 0 || 12000Sstevel@tonic-gate strcmp(buf, SCF_DEP_OPTIONAL_ALL) == 0) 12010Sstevel@tonic-gate continue; 12020Sstevel@tonic-gate 12030Sstevel@tonic-gate if (strcmp(buf, SCF_DEP_REQUIRE_ALL) != 0 && 12040Sstevel@tonic-gate strcmp(buf, SCF_DEP_REQUIRE_ANY) != 0) { 12050Sstevel@tonic-gate uu_warn(gettext("Dependency \"%s\" of \"%s\" has " 12060Sstevel@tonic-gate "unknown type \"%s\".\n"), pgname, fmri, buf); 12070Sstevel@tonic-gate continue; 12080Sstevel@tonic-gate } 12090Sstevel@tonic-gate 12100Sstevel@tonic-gate if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) == 12110Sstevel@tonic-gate -1) { 12120Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) 12130Sstevel@tonic-gate scfdie(); 12140Sstevel@tonic-gate 12150Sstevel@tonic-gate uu_warn(gettext("\"%s\" is misconfigured (\"%s\" " 12160Sstevel@tonic-gate "dependency lacks \"%s\" property.)\n"), fmri, 12170Sstevel@tonic-gate pgname, SCF_PROPERTY_ENTITIES); 12180Sstevel@tonic-gate continue; 12190Sstevel@tonic-gate } 12200Sstevel@tonic-gate 12210Sstevel@tonic-gate if (scf_property_type(prop, &ty) != SCF_SUCCESS) 12220Sstevel@tonic-gate scfdie(); 12230Sstevel@tonic-gate 12240Sstevel@tonic-gate if (ty != SCF_TYPE_FMRI) { 12250Sstevel@tonic-gate uu_warn(gettext("\"%s\" is misconfigured (property " 12260Sstevel@tonic-gate "\"%s/%s\" is not of fmri type).\n"), fmri, pgname, 12270Sstevel@tonic-gate SCF_PROPERTY_ENTITIES); 12280Sstevel@tonic-gate continue; 12290Sstevel@tonic-gate } 12300Sstevel@tonic-gate 12310Sstevel@tonic-gate if (scf_iter_property_values(val_iter, prop) == -1) 12320Sstevel@tonic-gate scfdie(); 12330Sstevel@tonic-gate 12340Sstevel@tonic-gate if (strcmp(buf, SCF_DEP_REQUIRE_ANY) == 0) { 12350Sstevel@tonic-gate if (multiple_instances(val_iter, v, buf)) { 12360Sstevel@tonic-gate (void) printf(gettext("%s requires one of:\n"), 12370Sstevel@tonic-gate fmri); 12380Sstevel@tonic-gate 12390Sstevel@tonic-gate if (scf_iter_property_values(val_iter, prop) != 12400Sstevel@tonic-gate 0) 12410Sstevel@tonic-gate scfdie(); 12420Sstevel@tonic-gate 12430Sstevel@tonic-gate for (;;) { 12440Sstevel@tonic-gate int r; 12450Sstevel@tonic-gate 12460Sstevel@tonic-gate r = scf_iter_next_value(val_iter, v); 12470Sstevel@tonic-gate if (r == 0) 12480Sstevel@tonic-gate break; 12490Sstevel@tonic-gate if (r != 1) 12500Sstevel@tonic-gate scfdie(); 12510Sstevel@tonic-gate 12520Sstevel@tonic-gate if (scf_value_get_astring(v, buf, 12530Sstevel@tonic-gate max_scf_fmri_sz) < 0) 12540Sstevel@tonic-gate scfdie(); 12550Sstevel@tonic-gate 12560Sstevel@tonic-gate (void) fputs(" ", stdout); 12570Sstevel@tonic-gate (void) puts(buf); 12580Sstevel@tonic-gate } 12590Sstevel@tonic-gate 12600Sstevel@tonic-gate continue; 12610Sstevel@tonic-gate } 12620Sstevel@tonic-gate 12630Sstevel@tonic-gate /* 12640Sstevel@tonic-gate * Since there's only one instance, we can enable it. 12650Sstevel@tonic-gate * Reset val_iter and continue. 12660Sstevel@tonic-gate */ 12670Sstevel@tonic-gate if (scf_iter_property_values(val_iter, prop) != 0) 12680Sstevel@tonic-gate scfdie(); 12690Sstevel@tonic-gate } 12700Sstevel@tonic-gate 12710Sstevel@tonic-gate for (;;) { 12720Sstevel@tonic-gate ret = scf_iter_next_value(val_iter, v); 12730Sstevel@tonic-gate if (ret == 0) 12740Sstevel@tonic-gate break; 12750Sstevel@tonic-gate if (ret != 1) 12760Sstevel@tonic-gate scfdie(); 12770Sstevel@tonic-gate 12780Sstevel@tonic-gate if (scf_value_get_astring(v, buf, max_scf_fmri_sz) == 12790Sstevel@tonic-gate -1) 12800Sstevel@tonic-gate scfdie(); 12810Sstevel@tonic-gate 12820Sstevel@tonic-gate switch (enable_fmri_rec(buf, temp)) { 12830Sstevel@tonic-gate case 0: 12840Sstevel@tonic-gate break; 12850Sstevel@tonic-gate 12860Sstevel@tonic-gate case EINVAL: 12870Sstevel@tonic-gate uu_warn(gettext("\"%s\" dependency of \"%s\" " 12880Sstevel@tonic-gate "has invalid FMRI \"%s\".\n"), pgname, 12890Sstevel@tonic-gate fmri, buf); 12900Sstevel@tonic-gate break; 12910Sstevel@tonic-gate 12920Sstevel@tonic-gate case E2BIG: 12930Sstevel@tonic-gate uu_warn(gettext("%s depends on %s, which has " 12940Sstevel@tonic-gate "multiple instances.\n"), fmri, buf); 12950Sstevel@tonic-gate break; 12960Sstevel@tonic-gate 12970Sstevel@tonic-gate case ELOOP: 12980Sstevel@tonic-gate ret = ELOOP; 12990Sstevel@tonic-gate goto out; 13000Sstevel@tonic-gate 13010Sstevel@tonic-gate default: 13020Sstevel@tonic-gate assert(0); 13030Sstevel@tonic-gate abort(); 13040Sstevel@tonic-gate } 13050Sstevel@tonic-gate } 13060Sstevel@tonic-gate } 13070Sstevel@tonic-gate 13080Sstevel@tonic-gate ret = 0; 13090Sstevel@tonic-gate 13100Sstevel@tonic-gate out: 13110Sstevel@tonic-gate he->active = B_FALSE; 13120Sstevel@tonic-gate 13130Sstevel@tonic-gate free(buf); 13140Sstevel@tonic-gate free(pgname); 13150Sstevel@tonic-gate 13160Sstevel@tonic-gate (void) scf_value_destroy(v); 13170Sstevel@tonic-gate scf_property_destroy(prop); 13180Sstevel@tonic-gate scf_pg_destroy(pg); 13190Sstevel@tonic-gate scf_snapshot_destroy(snap); 13200Sstevel@tonic-gate scf_iter_destroy(pg_iter); 13210Sstevel@tonic-gate scf_iter_destroy(val_iter); 13220Sstevel@tonic-gate 13230Sstevel@tonic-gate return (ret); 13240Sstevel@tonic-gate } 13250Sstevel@tonic-gate 13260Sstevel@tonic-gate /* 13270Sstevel@tonic-gate * fmri here is only used for verbose messages. 13280Sstevel@tonic-gate */ 13290Sstevel@tonic-gate static void 13300Sstevel@tonic-gate set_inst_action(const char *fmri, const scf_instance_t *inst, 13310Sstevel@tonic-gate const char *action) 13320Sstevel@tonic-gate { 13330Sstevel@tonic-gate scf_transaction_t *tx; 13340Sstevel@tonic-gate scf_transaction_entry_t *ent; 13350Sstevel@tonic-gate scf_propertygroup_t *pg; 13360Sstevel@tonic-gate scf_property_t *prop; 13370Sstevel@tonic-gate scf_value_t *v; 13380Sstevel@tonic-gate int ret; 13390Sstevel@tonic-gate int64_t t; 13400Sstevel@tonic-gate hrtime_t timestamp; 13410Sstevel@tonic-gate 13420Sstevel@tonic-gate const char * const scf_pg_restarter_actions = SCF_PG_RESTARTER_ACTIONS; 13430Sstevel@tonic-gate 13440Sstevel@tonic-gate if ((pg = scf_pg_create(h)) == NULL || 13450Sstevel@tonic-gate (prop = scf_property_create(h)) == NULL || 13460Sstevel@tonic-gate (v = scf_value_create(h)) == NULL || 13470Sstevel@tonic-gate (tx = scf_transaction_create(h)) == NULL || 13480Sstevel@tonic-gate (ent = scf_entry_create(h)) == NULL) 13490Sstevel@tonic-gate scfdie(); 13500Sstevel@tonic-gate 13510Sstevel@tonic-gate if (scf_instance_get_pg(inst, scf_pg_restarter_actions, pg) == -1) { 13520Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) 13530Sstevel@tonic-gate scfdie(); 13540Sstevel@tonic-gate 13550Sstevel@tonic-gate /* Try creating the restarter_actions property group. */ 13560Sstevel@tonic-gate if (scf_instance_add_pg(inst, scf_pg_restarter_actions, 13570Sstevel@tonic-gate SCF_PG_RESTARTER_ACTIONS_TYPE, 13580Sstevel@tonic-gate SCF_PG_RESTARTER_ACTIONS_FLAGS, pg) == -1) { 13590Sstevel@tonic-gate switch (scf_error()) { 13600Sstevel@tonic-gate case SCF_ERROR_EXISTS: 13610Sstevel@tonic-gate /* Someone must have added it. */ 13620Sstevel@tonic-gate break; 13630Sstevel@tonic-gate 13640Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED: 13650Sstevel@tonic-gate if (!verbose) 13660Sstevel@tonic-gate uu_warn(emsg_permission_denied, fmri); 13670Sstevel@tonic-gate else 13680Sstevel@tonic-gate uu_warn(emsg_create_pg_perm_denied, 13690Sstevel@tonic-gate fmri, scf_pg_restarter_actions); 13700Sstevel@tonic-gate goto out; 13710Sstevel@tonic-gate 13720Sstevel@tonic-gate default: 13730Sstevel@tonic-gate scfdie(); 13740Sstevel@tonic-gate } 13750Sstevel@tonic-gate } 13760Sstevel@tonic-gate } 13770Sstevel@tonic-gate 13780Sstevel@tonic-gate /* 13790Sstevel@tonic-gate * If we lose the transaction race and need to retry, there are 2 13800Sstevel@tonic-gate * potential other winners: 13810Sstevel@tonic-gate * - another process setting actions 13820Sstevel@tonic-gate * - the restarter marking the action complete 13830Sstevel@tonic-gate * Therefore, re-read the property every time through the loop before 13840Sstevel@tonic-gate * making any decisions based on their values. 13850Sstevel@tonic-gate */ 13860Sstevel@tonic-gate do { 13870Sstevel@tonic-gate timestamp = gethrtime(); 13880Sstevel@tonic-gate 13890Sstevel@tonic-gate if (scf_transaction_start(tx, pg) == -1) { 13900Sstevel@tonic-gate if (scf_error() != SCF_ERROR_PERMISSION_DENIED) 13910Sstevel@tonic-gate scfdie(); 13920Sstevel@tonic-gate 13930Sstevel@tonic-gate if (!verbose) 13940Sstevel@tonic-gate uu_warn(emsg_permission_denied, fmri); 13950Sstevel@tonic-gate else 13960Sstevel@tonic-gate uu_warn(emsg_pg_perm_denied, fmri, 13970Sstevel@tonic-gate scf_pg_restarter_actions); 13980Sstevel@tonic-gate goto out; 13990Sstevel@tonic-gate } 14000Sstevel@tonic-gate 14010Sstevel@tonic-gate if (scf_pg_get_property(pg, action, prop) == -1) { 14020Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) 14030Sstevel@tonic-gate scfdie(); 14040Sstevel@tonic-gate if (scf_transaction_property_new(tx, ent, 14050Sstevel@tonic-gate action, SCF_TYPE_INTEGER) == -1) 14060Sstevel@tonic-gate scfdie(); 14070Sstevel@tonic-gate goto action_set; 14080Sstevel@tonic-gate } else { 14090Sstevel@tonic-gate if (scf_transaction_property_change_type(tx, ent, 14100Sstevel@tonic-gate action, SCF_TYPE_INTEGER) == -1) 14110Sstevel@tonic-gate scfdie(); 14120Sstevel@tonic-gate } 14130Sstevel@tonic-gate 14140Sstevel@tonic-gate if (scf_property_get_value(prop, v) == -1) { 14150Sstevel@tonic-gate switch (scf_error()) { 14160Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED: 14170Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 14180Sstevel@tonic-gate /* Misconfigured, so set anyway. */ 14190Sstevel@tonic-gate goto action_set; 14200Sstevel@tonic-gate 14210Sstevel@tonic-gate default: 14220Sstevel@tonic-gate scfdie(); 14230Sstevel@tonic-gate } 14240Sstevel@tonic-gate } else { 14250Sstevel@tonic-gate if (scf_value_get_integer(v, &t) == -1) { 14260Sstevel@tonic-gate assert(scf_error() == SCF_ERROR_TYPE_MISMATCH); 14270Sstevel@tonic-gate goto action_set; 14280Sstevel@tonic-gate } 14290Sstevel@tonic-gate if (t > timestamp) 14300Sstevel@tonic-gate break; 14310Sstevel@tonic-gate } 14320Sstevel@tonic-gate 14330Sstevel@tonic-gate action_set: 14340Sstevel@tonic-gate scf_value_set_integer(v, timestamp); 14350Sstevel@tonic-gate if (scf_entry_add_value(ent, v) == -1) 14360Sstevel@tonic-gate scfdie(); 14370Sstevel@tonic-gate 14380Sstevel@tonic-gate ret = scf_transaction_commit(tx); 14390Sstevel@tonic-gate if (ret == -1) { 14400Sstevel@tonic-gate if (scf_error() != SCF_ERROR_PERMISSION_DENIED) 14410Sstevel@tonic-gate scfdie(); 14420Sstevel@tonic-gate 14430Sstevel@tonic-gate if (!verbose) 14440Sstevel@tonic-gate uu_warn(emsg_permission_denied, fmri); 14450Sstevel@tonic-gate else 14460Sstevel@tonic-gate uu_warn(emsg_prop_perm_denied, fmri, 14470Sstevel@tonic-gate scf_pg_restarter_actions, action); 14480Sstevel@tonic-gate scf_transaction_reset(tx); 14490Sstevel@tonic-gate goto out; 14500Sstevel@tonic-gate } 14510Sstevel@tonic-gate 14520Sstevel@tonic-gate scf_transaction_reset(tx); 14530Sstevel@tonic-gate 14540Sstevel@tonic-gate if (ret == 0) { 14550Sstevel@tonic-gate if (scf_pg_update(pg) == -1) 14560Sstevel@tonic-gate scfdie(); 14570Sstevel@tonic-gate } 14580Sstevel@tonic-gate } while (ret == 0); 14590Sstevel@tonic-gate 14600Sstevel@tonic-gate if (verbose) 14610Sstevel@tonic-gate (void) printf(gettext("Action %s set for %s.\n"), action, fmri); 14620Sstevel@tonic-gate 14630Sstevel@tonic-gate out: 14640Sstevel@tonic-gate scf_value_destroy(v); 14650Sstevel@tonic-gate scf_entry_destroy(ent); 14660Sstevel@tonic-gate scf_transaction_destroy(tx); 14670Sstevel@tonic-gate scf_property_destroy(prop); 14680Sstevel@tonic-gate scf_pg_destroy(pg); 14690Sstevel@tonic-gate } 14700Sstevel@tonic-gate 14710Sstevel@tonic-gate /* 14720Sstevel@tonic-gate * Get the state of inst. state should point to a buffer of 14730Sstevel@tonic-gate * MAX_SCF_STATE_STRING_SZ bytes. Returns 0 on success or -1 if 14740Sstevel@tonic-gate * no restarter property group 14750Sstevel@tonic-gate * no state property 14760Sstevel@tonic-gate * state property is misconfigured (wrong type, not single-valued) 14770Sstevel@tonic-gate * state value is too long 14780Sstevel@tonic-gate * In these cases, fmri is used to print a warning. 14790Sstevel@tonic-gate * 14800Sstevel@tonic-gate * If pgp is non-NULL, a successful call to inst_get_state will store 14810Sstevel@tonic-gate * the SCF_PG_RESTARTER property group in *pgp, and the caller will be 14820Sstevel@tonic-gate * responsible for calling scf_pg_destroy on the property group. 14830Sstevel@tonic-gate */ 14840Sstevel@tonic-gate int 14850Sstevel@tonic-gate inst_get_state(scf_instance_t *inst, char *state, const char *fmri, 14860Sstevel@tonic-gate scf_propertygroup_t **pgp) 14870Sstevel@tonic-gate { 14880Sstevel@tonic-gate scf_propertygroup_t *pg; 14890Sstevel@tonic-gate scf_property_t *prop; 14900Sstevel@tonic-gate scf_value_t *val; 14910Sstevel@tonic-gate int ret = -1; 14920Sstevel@tonic-gate ssize_t szret; 14930Sstevel@tonic-gate 14940Sstevel@tonic-gate if ((pg = scf_pg_create(h)) == NULL || 14950Sstevel@tonic-gate (prop = scf_property_create(h)) == NULL || 14960Sstevel@tonic-gate (val = scf_value_create(h)) == NULL) 14970Sstevel@tonic-gate scfdie(); 14980Sstevel@tonic-gate 14990Sstevel@tonic-gate if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) != SCF_SUCCESS) { 15000Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) 15010Sstevel@tonic-gate scfdie(); 15020Sstevel@tonic-gate 15030Sstevel@tonic-gate uu_warn(gettext("%s is misconfigured (lacks \"%s\" property " 15040Sstevel@tonic-gate "group).\n"), fmri ? fmri : inst_get_fmri(inst), 15050Sstevel@tonic-gate SCF_PG_RESTARTER); 15060Sstevel@tonic-gate goto out; 15070Sstevel@tonic-gate } 15080Sstevel@tonic-gate 15090Sstevel@tonic-gate szret = get_astring_prop(pg, SCF_PROPERTY_STATE, prop, val, state, 15100Sstevel@tonic-gate MAX_SCF_STATE_STRING_SZ); 15110Sstevel@tonic-gate if (szret < 0) { 15120Sstevel@tonic-gate switch (-szret) { 15130Sstevel@tonic-gate case ENOENT: 15140Sstevel@tonic-gate uu_warn(gettext("%s is misconfigured (\"%s\" property " 15150Sstevel@tonic-gate "group lacks \"%s\" property).\n"), 15160Sstevel@tonic-gate fmri ? fmri : inst_get_fmri(inst), SCF_PG_RESTARTER, 15170Sstevel@tonic-gate SCF_PROPERTY_STATE); 15180Sstevel@tonic-gate goto out; 15190Sstevel@tonic-gate 15200Sstevel@tonic-gate case E2BIG: 15210Sstevel@tonic-gate uu_warn(gettext("%s is misconfigured (\"%s/%s\" " 15220Sstevel@tonic-gate "property is not single-valued).\n"), 15230Sstevel@tonic-gate fmri ? fmri : inst_get_fmri(inst), SCF_PG_RESTARTER, 15240Sstevel@tonic-gate SCF_PROPERTY_STATE); 15250Sstevel@tonic-gate goto out; 15260Sstevel@tonic-gate 15270Sstevel@tonic-gate case EINVAL: 15280Sstevel@tonic-gate uu_warn(gettext("%s is misconfigured (\"%s/%s\" " 15290Sstevel@tonic-gate "property is not of type astring).\n"), 15300Sstevel@tonic-gate fmri ? fmri : inst_get_fmri(inst), SCF_PG_RESTARTER, 15310Sstevel@tonic-gate SCF_PROPERTY_STATE); 15320Sstevel@tonic-gate goto out; 15330Sstevel@tonic-gate 15340Sstevel@tonic-gate default: 15350Sstevel@tonic-gate assert(0); 15360Sstevel@tonic-gate abort(); 15370Sstevel@tonic-gate } 15380Sstevel@tonic-gate } 15390Sstevel@tonic-gate if (szret >= MAX_SCF_STATE_STRING_SZ) { 15400Sstevel@tonic-gate uu_warn(gettext("%s is misconfigured (\"%s/%s\" property value " 15410Sstevel@tonic-gate "is too long).\n"), fmri ? fmri : inst_get_fmri(inst), 15420Sstevel@tonic-gate SCF_PG_RESTARTER, SCF_PROPERTY_STATE); 15430Sstevel@tonic-gate goto out; 15440Sstevel@tonic-gate } 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate ret = 0; 15470Sstevel@tonic-gate if (pgp) 15480Sstevel@tonic-gate *pgp = pg; 15490Sstevel@tonic-gate 15500Sstevel@tonic-gate out: 15510Sstevel@tonic-gate (void) scf_value_destroy(val); 15520Sstevel@tonic-gate scf_property_destroy(prop); 15530Sstevel@tonic-gate if (ret || pgp == NULL) 15540Sstevel@tonic-gate scf_pg_destroy(pg); 15550Sstevel@tonic-gate return (ret); 15560Sstevel@tonic-gate } 15570Sstevel@tonic-gate 15580Sstevel@tonic-gate static void 15590Sstevel@tonic-gate set_astring_prop(const char *fmri, const char *pgname, const char *pgtype, 15600Sstevel@tonic-gate uint32_t pgflags, const char *propname, const char *str) 15610Sstevel@tonic-gate { 15620Sstevel@tonic-gate scf_instance_t *inst; 15630Sstevel@tonic-gate scf_propertygroup_t *pg; 15640Sstevel@tonic-gate scf_property_t *prop; 15650Sstevel@tonic-gate scf_value_t *val; 15660Sstevel@tonic-gate scf_transaction_t *tx; 15670Sstevel@tonic-gate scf_transaction_entry_t *txent; 15680Sstevel@tonic-gate int ret; 15690Sstevel@tonic-gate 15700Sstevel@tonic-gate inst = scf_instance_create(h); 15710Sstevel@tonic-gate if (inst == NULL) 15720Sstevel@tonic-gate scfdie(); 15730Sstevel@tonic-gate 15740Sstevel@tonic-gate if (get_inst(fmri, inst) != 0) 15750Sstevel@tonic-gate return; 15760Sstevel@tonic-gate 15770Sstevel@tonic-gate if ((pg = scf_pg_create(h)) == NULL || 15780Sstevel@tonic-gate (prop = scf_property_create(h)) == NULL || 15790Sstevel@tonic-gate (val = scf_value_create(h)) == NULL || 15800Sstevel@tonic-gate (tx = scf_transaction_create(h)) == NULL || 15810Sstevel@tonic-gate (txent = scf_entry_create(h)) == NULL) 15820Sstevel@tonic-gate scfdie(); 15830Sstevel@tonic-gate 15840Sstevel@tonic-gate if (scf_instance_get_pg(inst, pgname, pg) != SCF_SUCCESS) { 15850Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) 15860Sstevel@tonic-gate scfdie(); 15870Sstevel@tonic-gate 15880Sstevel@tonic-gate if (scf_instance_add_pg(inst, pgname, pgtype, pgflags, pg) != 15890Sstevel@tonic-gate SCF_SUCCESS) { 15900Sstevel@tonic-gate switch (scf_error()) { 15910Sstevel@tonic-gate case SCF_ERROR_EXISTS: 15920Sstevel@tonic-gate if (scf_instance_get_pg(inst, pgname, pg) != 15930Sstevel@tonic-gate SCF_SUCCESS) { 15940Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) 15950Sstevel@tonic-gate scfdie(); 15960Sstevel@tonic-gate 15970Sstevel@tonic-gate uu_warn(gettext("Repository write " 15980Sstevel@tonic-gate "contention.\n")); 15990Sstevel@tonic-gate goto out; 16000Sstevel@tonic-gate } 16010Sstevel@tonic-gate break; 16020Sstevel@tonic-gate 16030Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED: 16040Sstevel@tonic-gate if (!verbose) 16050Sstevel@tonic-gate uu_warn(emsg_permission_denied, fmri); 16060Sstevel@tonic-gate else 16070Sstevel@tonic-gate uu_warn(emsg_create_pg_perm_denied, 16080Sstevel@tonic-gate fmri, pgname); 16090Sstevel@tonic-gate goto out; 16100Sstevel@tonic-gate 16110Sstevel@tonic-gate default: 16120Sstevel@tonic-gate scfdie(); 16130Sstevel@tonic-gate } 16140Sstevel@tonic-gate } 16150Sstevel@tonic-gate } 16160Sstevel@tonic-gate 16170Sstevel@tonic-gate do { 16180Sstevel@tonic-gate if (scf_transaction_start(tx, pg) != SCF_SUCCESS) { 16190Sstevel@tonic-gate if (scf_error() != SCF_ERROR_PERMISSION_DENIED) 16200Sstevel@tonic-gate scfdie(); 16210Sstevel@tonic-gate 16220Sstevel@tonic-gate if (!verbose) 16230Sstevel@tonic-gate uu_warn(emsg_permission_denied, fmri); 16240Sstevel@tonic-gate else 16250Sstevel@tonic-gate uu_warn(emsg_pg_perm_denied, fmri, pgname); 16260Sstevel@tonic-gate goto out; 16270Sstevel@tonic-gate } 16280Sstevel@tonic-gate 16290Sstevel@tonic-gate if (scf_transaction_property_change_type(tx, txent, propname, 16300Sstevel@tonic-gate SCF_TYPE_ASTRING) != 0) { 16310Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) 16320Sstevel@tonic-gate scfdie(); 16330Sstevel@tonic-gate 16340Sstevel@tonic-gate if (scf_transaction_property_new(tx, txent, propname, 16350Sstevel@tonic-gate SCF_TYPE_ASTRING) != 0) 16360Sstevel@tonic-gate scfdie(); 16370Sstevel@tonic-gate } 16380Sstevel@tonic-gate 16390Sstevel@tonic-gate if (scf_value_set_astring(val, str) != SCF_SUCCESS) 16400Sstevel@tonic-gate scfdie(); 16410Sstevel@tonic-gate 16420Sstevel@tonic-gate if (scf_entry_add_value(txent, val) != SCF_SUCCESS) 16430Sstevel@tonic-gate scfdie(); 16440Sstevel@tonic-gate 16450Sstevel@tonic-gate ret = scf_transaction_commit(tx); 16460Sstevel@tonic-gate if (ret == -1) { 16470Sstevel@tonic-gate if (scf_error() != SCF_ERROR_PERMISSION_DENIED) 16480Sstevel@tonic-gate scfdie(); 16490Sstevel@tonic-gate 16500Sstevel@tonic-gate if (!verbose) 16510Sstevel@tonic-gate uu_warn(emsg_permission_denied, fmri); 16520Sstevel@tonic-gate else 16530Sstevel@tonic-gate uu_warn(emsg_prop_perm_denied, fmri, pgname, 16540Sstevel@tonic-gate propname); 16550Sstevel@tonic-gate goto out; 16560Sstevel@tonic-gate } 16570Sstevel@tonic-gate 16580Sstevel@tonic-gate if (ret == 0) { 16590Sstevel@tonic-gate scf_transaction_reset(tx); 16600Sstevel@tonic-gate 16610Sstevel@tonic-gate if (scf_pg_update(pg) != SCF_SUCCESS) 16620Sstevel@tonic-gate scfdie(); 16630Sstevel@tonic-gate } 16640Sstevel@tonic-gate } while (ret == 0); 16650Sstevel@tonic-gate 16660Sstevel@tonic-gate out: 16670Sstevel@tonic-gate scf_transaction_destroy(tx); 16680Sstevel@tonic-gate scf_entry_destroy(txent); 16690Sstevel@tonic-gate scf_value_destroy(val); 16700Sstevel@tonic-gate scf_property_destroy(prop); 16710Sstevel@tonic-gate scf_pg_destroy(pg); 16720Sstevel@tonic-gate scf_instance_destroy(inst); 16730Sstevel@tonic-gate } 16740Sstevel@tonic-gate 16750Sstevel@tonic-gate 16760Sstevel@tonic-gate /* 16770Sstevel@tonic-gate * Flags to control enable and disable actions. 16780Sstevel@tonic-gate */ 16790Sstevel@tonic-gate #define SET_ENABLED 0x1 16800Sstevel@tonic-gate #define SET_TEMPORARY 0x2 16810Sstevel@tonic-gate #define SET_RECURSIVE 0x4 16820Sstevel@tonic-gate 16830Sstevel@tonic-gate static int 16840Sstevel@tonic-gate set_fmri_enabled(void *data, scf_walkinfo_t *wip) 16850Sstevel@tonic-gate { 16860Sstevel@tonic-gate int flags = (int)data; 16870Sstevel@tonic-gate 16880Sstevel@tonic-gate assert(wip->inst != NULL); 16890Sstevel@tonic-gate assert(wip->pg == NULL); 16900Sstevel@tonic-gate 16910Sstevel@tonic-gate if (flags & SET_RECURSIVE) { 16920Sstevel@tonic-gate char *fmri_buf = malloc(max_scf_fmri_sz); 16930Sstevel@tonic-gate if (fmri_buf == NULL) 16940Sstevel@tonic-gate uu_die(emsg_nomem); 16950Sstevel@tonic-gate 16960Sstevel@tonic-gate visited = calloc(HT_BUCKETS, sizeof (*visited)); 16970Sstevel@tonic-gate if (visited == NULL) 16980Sstevel@tonic-gate uu_die(emsg_nomem); 16990Sstevel@tonic-gate 17000Sstevel@tonic-gate /* scf_walk_fmri() guarantees that fmri isn't too long */ 17010Sstevel@tonic-gate assert(strlen(wip->fmri) <= max_scf_fmri_sz); 17020Sstevel@tonic-gate (void) strlcpy(fmri_buf, wip->fmri, max_scf_fmri_sz); 17030Sstevel@tonic-gate 17040Sstevel@tonic-gate switch (enable_fmri_rec(fmri_buf, (flags & SET_TEMPORARY))) { 17050Sstevel@tonic-gate case E2BIG: 17060Sstevel@tonic-gate uu_warn(gettext("operation on service %s is ambiguous; " 17070Sstevel@tonic-gate "instance specification needed.\n"), fmri_buf); 17080Sstevel@tonic-gate break; 17090Sstevel@tonic-gate 17100Sstevel@tonic-gate case ELOOP: 17110Sstevel@tonic-gate uu_warn(gettext("%s: Dependency cycle detected.\n"), 17120Sstevel@tonic-gate fmri_buf); 17130Sstevel@tonic-gate } 17140Sstevel@tonic-gate 17150Sstevel@tonic-gate free(visited); 17160Sstevel@tonic-gate free(fmri_buf); 17170Sstevel@tonic-gate 17180Sstevel@tonic-gate } else { 17190Sstevel@tonic-gate set_inst_enabled(wip->fmri, wip->inst, 17200Sstevel@tonic-gate (flags & SET_TEMPORARY) != 0, (flags & SET_ENABLED) != 0); 17210Sstevel@tonic-gate } 17220Sstevel@tonic-gate 17230Sstevel@tonic-gate return (0); 17240Sstevel@tonic-gate } 17250Sstevel@tonic-gate 17260Sstevel@tonic-gate /* ARGSUSED */ 17270Sstevel@tonic-gate static int 17280Sstevel@tonic-gate wait_fmri_enabled(void *data, scf_walkinfo_t *wip) 17290Sstevel@tonic-gate { 17300Sstevel@tonic-gate scf_propertygroup_t *pg = NULL; 17310Sstevel@tonic-gate char state[MAX_SCF_STATE_STRING_SZ]; 17320Sstevel@tonic-gate 17330Sstevel@tonic-gate assert(wip->inst != NULL); 17340Sstevel@tonic-gate assert(wip->pg == NULL); 17350Sstevel@tonic-gate 17360Sstevel@tonic-gate do { 17370Sstevel@tonic-gate if (pg) 17380Sstevel@tonic-gate scf_pg_destroy(pg); 17390Sstevel@tonic-gate if (inst_get_state(wip->inst, state, wip->fmri, &pg) != 0) { 17400Sstevel@tonic-gate exit_status = EXIT_SVC_FAILURE; 17410Sstevel@tonic-gate return (0); 17420Sstevel@tonic-gate } 17430Sstevel@tonic-gate 17440Sstevel@tonic-gate if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0 || 17450Sstevel@tonic-gate strcmp(state, SCF_STATE_STRING_DEGRADED) == 0) { 17460Sstevel@tonic-gate /* 17470Sstevel@tonic-gate * We're done. 17480Sstevel@tonic-gate */ 17490Sstevel@tonic-gate goto out; 17500Sstevel@tonic-gate } 17510Sstevel@tonic-gate 17520Sstevel@tonic-gate if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) { 17530Sstevel@tonic-gate /* 17540Sstevel@tonic-gate * The service is ill. 17550Sstevel@tonic-gate */ 17560Sstevel@tonic-gate uu_warn(gettext("Instance \"%s\" is in maintenance" 17570Sstevel@tonic-gate " state.\n"), wip->fmri); 17580Sstevel@tonic-gate exit_status = EXIT_SVC_FAILURE; 17590Sstevel@tonic-gate goto out; 17600Sstevel@tonic-gate } 17610Sstevel@tonic-gate 17620Sstevel@tonic-gate if (!is_enabled(wip->inst)) { 17630Sstevel@tonic-gate /* 17640Sstevel@tonic-gate * Someone stepped in and disabled the service. 17650Sstevel@tonic-gate */ 17660Sstevel@tonic-gate uu_warn(gettext("Instance \"%s\" has been disabled" 17670Sstevel@tonic-gate " by another entity.\n"), wip->fmri); 17680Sstevel@tonic-gate exit_status = EXIT_SVC_FAILURE; 17690Sstevel@tonic-gate goto out; 17700Sstevel@tonic-gate } 17710Sstevel@tonic-gate 17720Sstevel@tonic-gate if (!has_potential(wip->inst, B_FALSE)) { 17730Sstevel@tonic-gate /* 17740Sstevel@tonic-gate * Our dependencies aren't met. We'll never 17750Sstevel@tonic-gate * amount to anything. 17760Sstevel@tonic-gate */ 17770Sstevel@tonic-gate uu_warn(gettext("Instance \"%s\" has unsatisfied" 17780Sstevel@tonic-gate " dependencies.\n"), wip->fmri); 17790Sstevel@tonic-gate /* 17800Sstevel@tonic-gate * EXIT_SVC_FAILURE takes precedence over 17810Sstevel@tonic-gate * EXIT_DEP_FAILURE 17820Sstevel@tonic-gate */ 17830Sstevel@tonic-gate if (exit_status == 0) 17840Sstevel@tonic-gate exit_status = EXIT_DEP_FAILURE; 17850Sstevel@tonic-gate goto out; 17860Sstevel@tonic-gate } 17870Sstevel@tonic-gate } while (_scf_pg_wait(pg, WAIT_INTERVAL) >= 0); 17880Sstevel@tonic-gate scfdie(); 17890Sstevel@tonic-gate /* NOTREACHED */ 17900Sstevel@tonic-gate 17910Sstevel@tonic-gate out: 17920Sstevel@tonic-gate scf_pg_destroy(pg); 17930Sstevel@tonic-gate return (0); 17940Sstevel@tonic-gate } 17950Sstevel@tonic-gate 17960Sstevel@tonic-gate /* ARGSUSED */ 17970Sstevel@tonic-gate static int 17980Sstevel@tonic-gate wait_fmri_disabled(void *data, scf_walkinfo_t *wip) 17990Sstevel@tonic-gate { 18000Sstevel@tonic-gate scf_propertygroup_t *pg = NULL; 18010Sstevel@tonic-gate char state[MAX_SCF_STATE_STRING_SZ]; 18020Sstevel@tonic-gate 18030Sstevel@tonic-gate assert(wip->inst != NULL); 18040Sstevel@tonic-gate assert(wip->pg == NULL); 18050Sstevel@tonic-gate 18060Sstevel@tonic-gate do { 18070Sstevel@tonic-gate if (pg) 18080Sstevel@tonic-gate scf_pg_destroy(pg); 18090Sstevel@tonic-gate if (inst_get_state(wip->inst, state, wip->fmri, &pg) != 0) { 18100Sstevel@tonic-gate exit_status = EXIT_SVC_FAILURE; 18110Sstevel@tonic-gate return (0); 18120Sstevel@tonic-gate } 18130Sstevel@tonic-gate 18140Sstevel@tonic-gate if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0) { 18150Sstevel@tonic-gate /* 18160Sstevel@tonic-gate * We're done. 18170Sstevel@tonic-gate */ 18180Sstevel@tonic-gate goto out; 18190Sstevel@tonic-gate } 18200Sstevel@tonic-gate 18210Sstevel@tonic-gate if (is_enabled(wip->inst)) { 18220Sstevel@tonic-gate /* 18230Sstevel@tonic-gate * Someone stepped in and enabled the service. 18240Sstevel@tonic-gate */ 18250Sstevel@tonic-gate uu_warn(gettext("Instance \"%s\" has been enabled" 18260Sstevel@tonic-gate " by another entity.\n"), wip->fmri); 18270Sstevel@tonic-gate exit_status = EXIT_SVC_FAILURE; 18280Sstevel@tonic-gate goto out; 18290Sstevel@tonic-gate } 18300Sstevel@tonic-gate 18310Sstevel@tonic-gate if (!has_potential(wip->inst, B_TRUE)) { 18320Sstevel@tonic-gate /* 18330Sstevel@tonic-gate * Our restarter is hopeless. 18340Sstevel@tonic-gate */ 18350Sstevel@tonic-gate uu_warn(gettext("Restarter for instance \"%s\" is" 18360Sstevel@tonic-gate " unavailable.\n"), wip->fmri); 18370Sstevel@tonic-gate /* 18380Sstevel@tonic-gate * EXIT_SVC_FAILURE takes precedence over 18390Sstevel@tonic-gate * EXIT_DEP_FAILURE 18400Sstevel@tonic-gate */ 18410Sstevel@tonic-gate if (exit_status == 0) 18420Sstevel@tonic-gate exit_status = EXIT_DEP_FAILURE; 18430Sstevel@tonic-gate goto out; 18440Sstevel@tonic-gate } 18450Sstevel@tonic-gate 18460Sstevel@tonic-gate } while (_scf_pg_wait(pg, WAIT_INTERVAL) >= 0); 18470Sstevel@tonic-gate scfdie(); 18480Sstevel@tonic-gate /* NOTREACHED */ 18490Sstevel@tonic-gate 18500Sstevel@tonic-gate out: 18510Sstevel@tonic-gate scf_pg_destroy(pg); 18520Sstevel@tonic-gate return (0); 18530Sstevel@tonic-gate } 18540Sstevel@tonic-gate 18550Sstevel@tonic-gate /* ARGSUSED */ 18560Sstevel@tonic-gate static int 18570Sstevel@tonic-gate clear_instance(void *data, scf_walkinfo_t *wip) 18580Sstevel@tonic-gate { 18590Sstevel@tonic-gate char state[MAX_SCF_STATE_STRING_SZ]; 18600Sstevel@tonic-gate 18610Sstevel@tonic-gate assert(wip->inst != NULL); 18620Sstevel@tonic-gate assert(wip->pg == NULL); 18630Sstevel@tonic-gate 18640Sstevel@tonic-gate if (inst_get_state(wip->inst, state, wip->fmri, NULL) != 0) 18650Sstevel@tonic-gate return (0); 18660Sstevel@tonic-gate 18670Sstevel@tonic-gate if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) { 18680Sstevel@tonic-gate set_inst_action(wip->fmri, wip->inst, SCF_PROPERTY_MAINT_OFF); 18690Sstevel@tonic-gate } else if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 18700Sstevel@tonic-gate 0) { 18710Sstevel@tonic-gate set_inst_action(wip->fmri, wip->inst, SCF_PROPERTY_RESTORE); 18720Sstevel@tonic-gate } else { 18730Sstevel@tonic-gate uu_warn(gettext("Instance \"%s\" is not in a " 18740Sstevel@tonic-gate "maintenance or degraded state.\n"), wip->fmri); 18750Sstevel@tonic-gate 18760Sstevel@tonic-gate exit_status = 1; 18770Sstevel@tonic-gate } 18780Sstevel@tonic-gate 18790Sstevel@tonic-gate return (0); 18800Sstevel@tonic-gate } 18810Sstevel@tonic-gate 18820Sstevel@tonic-gate static int 18830Sstevel@tonic-gate set_fmri_action(void *action, scf_walkinfo_t *wip) 18840Sstevel@tonic-gate { 18850Sstevel@tonic-gate assert(wip->inst != NULL && wip->pg == NULL); 18860Sstevel@tonic-gate 18870Sstevel@tonic-gate set_inst_action(wip->fmri, wip->inst, action); 18880Sstevel@tonic-gate 18890Sstevel@tonic-gate return (0); 18900Sstevel@tonic-gate } 18910Sstevel@tonic-gate 18920Sstevel@tonic-gate /* 18930Sstevel@tonic-gate * Flags to control 'mark' action. 18940Sstevel@tonic-gate */ 18950Sstevel@tonic-gate #define MARK_IMMEDIATE 0x1 18960Sstevel@tonic-gate #define MARK_TEMPORARY 0x2 18970Sstevel@tonic-gate 18980Sstevel@tonic-gate static int 18990Sstevel@tonic-gate force_degraded(void *data, scf_walkinfo_t *wip) 19000Sstevel@tonic-gate { 19010Sstevel@tonic-gate int flags = (int)data; 19020Sstevel@tonic-gate char state[MAX_SCF_STATE_STRING_SZ]; 19030Sstevel@tonic-gate 19040Sstevel@tonic-gate if (inst_get_state(wip->inst, state, wip->fmri, NULL) != 0) { 19050Sstevel@tonic-gate exit_status = 1; 19060Sstevel@tonic-gate return (0); 19070Sstevel@tonic-gate } 19080Sstevel@tonic-gate 19090Sstevel@tonic-gate if (strcmp(state, SCF_STATE_STRING_ONLINE) != 0) { 19100Sstevel@tonic-gate uu_warn(gettext("Instance \"%s\" is not online.\n"), wip->fmri); 19110Sstevel@tonic-gate exit_status = 1; 19120Sstevel@tonic-gate return (0); 19130Sstevel@tonic-gate } 19140Sstevel@tonic-gate 19150Sstevel@tonic-gate set_inst_action(wip->fmri, wip->inst, (flags & MARK_IMMEDIATE) ? 19160Sstevel@tonic-gate SCF_PROPERTY_DEGRADE_IMMEDIATE : SCF_PROPERTY_DEGRADED); 19170Sstevel@tonic-gate 19180Sstevel@tonic-gate return (0); 19190Sstevel@tonic-gate } 19200Sstevel@tonic-gate 19210Sstevel@tonic-gate static int 19220Sstevel@tonic-gate force_maintenance(void *data, scf_walkinfo_t *wip) 19230Sstevel@tonic-gate { 19240Sstevel@tonic-gate int flags = (int)data; 19250Sstevel@tonic-gate const char *prop; 19260Sstevel@tonic-gate 19270Sstevel@tonic-gate if (flags & MARK_IMMEDIATE) { 19280Sstevel@tonic-gate prop = (flags & MARK_TEMPORARY) ? 19290Sstevel@tonic-gate SCF_PROPERTY_MAINT_ON_IMMTEMP : 19300Sstevel@tonic-gate SCF_PROPERTY_MAINT_ON_IMMEDIATE; 19310Sstevel@tonic-gate } else { 19320Sstevel@tonic-gate prop = (flags & MARK_TEMPORARY) ? 19330Sstevel@tonic-gate SCF_PROPERTY_MAINT_ON_TEMPORARY : 19340Sstevel@tonic-gate SCF_PROPERTY_MAINT_ON; 19350Sstevel@tonic-gate } 19360Sstevel@tonic-gate 19370Sstevel@tonic-gate set_inst_action(wip->fmri, wip->inst, prop); 19380Sstevel@tonic-gate 19390Sstevel@tonic-gate return (0); 19400Sstevel@tonic-gate } 19410Sstevel@tonic-gate 19420Sstevel@tonic-gate static void 19430Sstevel@tonic-gate set_milestone(const char *fmri, boolean_t temporary) 19440Sstevel@tonic-gate { 19450Sstevel@tonic-gate scf_instance_t *inst; 19460Sstevel@tonic-gate scf_propertygroup_t *pg; 19470Sstevel@tonic-gate int r; 19480Sstevel@tonic-gate 19490Sstevel@tonic-gate if (temporary) { 19500Sstevel@tonic-gate set_astring_prop(SCF_SERVICE_STARTD, SCF_PG_OPTIONS_OVR, 19510Sstevel@tonic-gate SCF_PG_OPTIONS_OVR_TYPE, SCF_PG_OPTIONS_OVR_FLAGS, 19520Sstevel@tonic-gate SCF_PROPERTY_MILESTONE, fmri); 19530Sstevel@tonic-gate return; 19540Sstevel@tonic-gate } 19550Sstevel@tonic-gate 19560Sstevel@tonic-gate if ((inst = scf_instance_create(h)) == NULL || 19570Sstevel@tonic-gate (pg = scf_pg_create(h)) == NULL) 19580Sstevel@tonic-gate scfdie(); 19590Sstevel@tonic-gate 19600Sstevel@tonic-gate if (get_inst(SCF_SERVICE_STARTD, inst) != 0) { 19610Sstevel@tonic-gate scf_instance_destroy(inst); 19620Sstevel@tonic-gate return; 19630Sstevel@tonic-gate } 19640Sstevel@tonic-gate 19650Sstevel@tonic-gate /* 19660Sstevel@tonic-gate * Set the persistent milestone before deleting the override so we don't 19670Sstevel@tonic-gate * glitch. 19680Sstevel@tonic-gate */ 19690Sstevel@tonic-gate set_astring_prop(SCF_SERVICE_STARTD, SCF_PG_OPTIONS, 19700Sstevel@tonic-gate SCF_PG_OPTIONS_TYPE, SCF_PG_OPTIONS_FLAGS, SCF_PROPERTY_MILESTONE, 19710Sstevel@tonic-gate fmri); 19720Sstevel@tonic-gate 19730Sstevel@tonic-gate if (scf_instance_get_pg(inst, SCF_PG_OPTIONS_OVR, pg) == 0) { 19740Sstevel@tonic-gate r = delete_prop(pg, SCF_PROPERTY_MILESTONE); 19750Sstevel@tonic-gate switch (r) { 19760Sstevel@tonic-gate case 0: 19770Sstevel@tonic-gate break; 19780Sstevel@tonic-gate 19790Sstevel@tonic-gate case ECANCELED: 19800Sstevel@tonic-gate uu_warn(emsg_no_service, fmri); 19810Sstevel@tonic-gate exit_status = 1; 19820Sstevel@tonic-gate goto out; 19830Sstevel@tonic-gate 19840Sstevel@tonic-gate case EPERM: 19850Sstevel@tonic-gate uu_warn(gettext("Could not delete %s/%s property of " 19860Sstevel@tonic-gate "%s: permission denied.\n"), SCF_PG_OPTIONS_OVR, 19870Sstevel@tonic-gate SCF_PROPERTY_MILESTONE, SCF_SERVICE_STARTD); 19880Sstevel@tonic-gate exit_status = 1; 19890Sstevel@tonic-gate goto out; 19900Sstevel@tonic-gate 19910Sstevel@tonic-gate case EACCES: 19920Sstevel@tonic-gate uu_warn(gettext("Could not delete %s/%s property of " 19930Sstevel@tonic-gate "%s: access denied.\n"), SCF_PG_OPTIONS_OVR, 19940Sstevel@tonic-gate SCF_PROPERTY_MILESTONE, SCF_SERVICE_STARTD); 19950Sstevel@tonic-gate exit_status = 1; 19960Sstevel@tonic-gate goto out; 19970Sstevel@tonic-gate 19980Sstevel@tonic-gate case EROFS: 19990Sstevel@tonic-gate uu_warn(gettext("Could not delete %s/%s property of " 20000Sstevel@tonic-gate "%s: backend read-only.\n"), SCF_PG_OPTIONS_OVR, 20010Sstevel@tonic-gate SCF_PROPERTY_MILESTONE, SCF_SERVICE_STARTD); 20020Sstevel@tonic-gate exit_status = 1; 20030Sstevel@tonic-gate goto out; 20040Sstevel@tonic-gate 20050Sstevel@tonic-gate default: 20060Sstevel@tonic-gate bad_error("delete_prop", r); 20070Sstevel@tonic-gate } 20080Sstevel@tonic-gate } else { 20090Sstevel@tonic-gate switch (scf_error()) { 20100Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 20110Sstevel@tonic-gate break; 20120Sstevel@tonic-gate 20130Sstevel@tonic-gate case SCF_ERROR_DELETED: 20140Sstevel@tonic-gate uu_warn(emsg_no_service, fmri); 20150Sstevel@tonic-gate exit_status = 1; 20160Sstevel@tonic-gate goto out; 20170Sstevel@tonic-gate 20180Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 20190Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 20200Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 20210Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT: 20220Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 20230Sstevel@tonic-gate default: 20240Sstevel@tonic-gate scfdie(); 20250Sstevel@tonic-gate } 20260Sstevel@tonic-gate } 20270Sstevel@tonic-gate 20280Sstevel@tonic-gate out: 20290Sstevel@tonic-gate scf_pg_destroy(pg); 20300Sstevel@tonic-gate scf_instance_destroy(inst); 20310Sstevel@tonic-gate } 20320Sstevel@tonic-gate 20330Sstevel@tonic-gate static char const *milestones[] = { 20340Sstevel@tonic-gate SCF_MILESTONE_SINGLE_USER, 20350Sstevel@tonic-gate SCF_MILESTONE_MULTI_USER, 20360Sstevel@tonic-gate SCF_MILESTONE_MULTI_USER_SERVER, 20370Sstevel@tonic-gate NULL 20380Sstevel@tonic-gate }; 20390Sstevel@tonic-gate 20400Sstevel@tonic-gate static void 2041471Shg115875 usage_milestone(void) 20420Sstevel@tonic-gate { 20430Sstevel@tonic-gate const char **ms; 20440Sstevel@tonic-gate 20450Sstevel@tonic-gate (void) fprintf(stderr, gettext( 20460Sstevel@tonic-gate "Usage: svcadm milestone [-d] <milestone>\n\n" 20470Sstevel@tonic-gate "\t-d\tmake the specified milestone the default for system boot\n\n" 20480Sstevel@tonic-gate "\tMilestones can be specified using an FMRI or abbreviation.\n" 20490Sstevel@tonic-gate "\tThe major milestones are as follows:\n\n" 20500Sstevel@tonic-gate "\tall\n" 20510Sstevel@tonic-gate "\tnone\n")); 20520Sstevel@tonic-gate 20530Sstevel@tonic-gate for (ms = milestones; *ms != NULL; ms++) 20540Sstevel@tonic-gate (void) fprintf(stderr, "\t%s\n", *ms); 20550Sstevel@tonic-gate 20560Sstevel@tonic-gate exit(UU_EXIT_USAGE); 20570Sstevel@tonic-gate } 20580Sstevel@tonic-gate 20590Sstevel@tonic-gate static const char * 20600Sstevel@tonic-gate validate_milestone(const char *milestone) 20610Sstevel@tonic-gate { 20620Sstevel@tonic-gate const char **ms; 20630Sstevel@tonic-gate const char *tmp; 20640Sstevel@tonic-gate size_t len; 20650Sstevel@tonic-gate 20660Sstevel@tonic-gate if (strcmp(milestone, "all") == 0) 20670Sstevel@tonic-gate return (milestone); 20680Sstevel@tonic-gate 20690Sstevel@tonic-gate if (strcmp(milestone, "none") == 0) 20700Sstevel@tonic-gate return (milestone); 20710Sstevel@tonic-gate 20720Sstevel@tonic-gate /* 20730Sstevel@tonic-gate * Determine if this is a full or partial milestone 20740Sstevel@tonic-gate */ 20750Sstevel@tonic-gate for (ms = milestones; *ms != NULL; ms++) { 20760Sstevel@tonic-gate if ((tmp = strstr(*ms, milestone)) != NULL) { 20770Sstevel@tonic-gate len = strlen(milestone); 20780Sstevel@tonic-gate 20790Sstevel@tonic-gate /* 20800Sstevel@tonic-gate * The beginning of the string must align with the start 20810Sstevel@tonic-gate * of a milestone fmri, or on the boundary between 20820Sstevel@tonic-gate * elements. The end of the string must align with the 20830Sstevel@tonic-gate * end of the milestone, or at the instance boundary. 20840Sstevel@tonic-gate */ 20850Sstevel@tonic-gate if ((tmp == *ms || tmp[-1] == '/') && 20860Sstevel@tonic-gate (tmp[len] == '\0' || tmp[len] == ':')) 20870Sstevel@tonic-gate return (*ms); 20880Sstevel@tonic-gate } 20890Sstevel@tonic-gate } 20900Sstevel@tonic-gate 20910Sstevel@tonic-gate (void) fprintf(stderr, 20920Sstevel@tonic-gate gettext("\"%s\" is not a valid major milestone.\n"), milestone); 20930Sstevel@tonic-gate 20940Sstevel@tonic-gate usage_milestone(); 20950Sstevel@tonic-gate /* NOTREACHED */ 20960Sstevel@tonic-gate } 20970Sstevel@tonic-gate 20980Sstevel@tonic-gate /*ARGSUSED*/ 20990Sstevel@tonic-gate static void 21000Sstevel@tonic-gate quiet(const char *fmt, ...) 21010Sstevel@tonic-gate { 21020Sstevel@tonic-gate /* Do nothing */ 21030Sstevel@tonic-gate } 21040Sstevel@tonic-gate 21050Sstevel@tonic-gate int 21060Sstevel@tonic-gate main(int argc, char *argv[]) 21070Sstevel@tonic-gate { 21080Sstevel@tonic-gate int o; 21090Sstevel@tonic-gate int err; 21100Sstevel@tonic-gate 21110Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 21120Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 21130Sstevel@tonic-gate 21140Sstevel@tonic-gate (void) uu_setpname(argv[0]); 21150Sstevel@tonic-gate 21160Sstevel@tonic-gate if (argc < 2) 21170Sstevel@tonic-gate usage(); 21180Sstevel@tonic-gate 21190Sstevel@tonic-gate max_scf_fmri_sz = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH); 21200Sstevel@tonic-gate if (max_scf_fmri_sz < 0) 21210Sstevel@tonic-gate scfdie(); 21220Sstevel@tonic-gate ++max_scf_fmri_sz; 21230Sstevel@tonic-gate 21240Sstevel@tonic-gate scratch_fmri = malloc(max_scf_fmri_sz); 21250Sstevel@tonic-gate if (scratch_fmri == NULL) 21260Sstevel@tonic-gate uu_die(emsg_nomem); 21270Sstevel@tonic-gate 21280Sstevel@tonic-gate h = scf_handle_create(SCF_VERSION); 21290Sstevel@tonic-gate if (h == NULL) 21300Sstevel@tonic-gate scfdie(); 21310Sstevel@tonic-gate 21320Sstevel@tonic-gate if (scf_handle_bind(h) == -1) 21330Sstevel@tonic-gate uu_die(gettext("Couldn't bind to svc.configd.\n")); 21340Sstevel@tonic-gate 21350Sstevel@tonic-gate while ((o = getopt(argc, argv, "v")) != -1) { 21360Sstevel@tonic-gate if (o == 'v') 21370Sstevel@tonic-gate verbose = 1; 21380Sstevel@tonic-gate else 21390Sstevel@tonic-gate usage(); 21400Sstevel@tonic-gate } 21410Sstevel@tonic-gate 21420Sstevel@tonic-gate if (optind >= argc) 21430Sstevel@tonic-gate usage(); 21440Sstevel@tonic-gate 21450Sstevel@tonic-gate emsg_permission_denied = gettext("%s: Permission denied.\n"); 21460Sstevel@tonic-gate emsg_nomem = gettext("Out of memory.\n"); 21470Sstevel@tonic-gate emsg_create_pg_perm_denied = gettext("%s: Couldn't create \"%s\" " 21480Sstevel@tonic-gate "property group (permission denied).\n"); 21490Sstevel@tonic-gate emsg_pg_perm_denied = gettext("%s: Couldn't modify \"%s\" property " 21500Sstevel@tonic-gate "group (permission denied).\n"); 21510Sstevel@tonic-gate emsg_prop_perm_denied = gettext("%s: Couldn't modify \"%s/%s\" " 21520Sstevel@tonic-gate "property (permission denied).\n"); 21530Sstevel@tonic-gate emsg_no_service = gettext("No such service \"%s\".\n"); 21540Sstevel@tonic-gate 21550Sstevel@tonic-gate if (strcmp(argv[optind], "enable") == 0) { 21560Sstevel@tonic-gate int flags = SET_ENABLED; 21570Sstevel@tonic-gate int wait = 0; 21580Sstevel@tonic-gate int error = 0; 21590Sstevel@tonic-gate 21600Sstevel@tonic-gate ++optind; 21610Sstevel@tonic-gate 21620Sstevel@tonic-gate while ((o = getopt(argc, argv, "rst")) != -1) { 21630Sstevel@tonic-gate if (o == 'r') 21640Sstevel@tonic-gate flags |= SET_RECURSIVE; 21650Sstevel@tonic-gate else if (o == 't') 21660Sstevel@tonic-gate flags |= SET_TEMPORARY; 21670Sstevel@tonic-gate else if (o == 's') 21680Sstevel@tonic-gate wait = 1; 21690Sstevel@tonic-gate else if (o == '?') 21700Sstevel@tonic-gate usage(); 21710Sstevel@tonic-gate else { 21720Sstevel@tonic-gate assert(0); 21730Sstevel@tonic-gate abort(); 21740Sstevel@tonic-gate } 21750Sstevel@tonic-gate } 21760Sstevel@tonic-gate argc -= optind; 21770Sstevel@tonic-gate argv += optind; 21780Sstevel@tonic-gate 21790Sstevel@tonic-gate if (argc <= 0) 21800Sstevel@tonic-gate usage(); 21810Sstevel@tonic-gate 21820Sstevel@tonic-gate /* 21830Sstevel@tonic-gate * We want to continue with -s processing if we had 21840Sstevel@tonic-gate * invalid options, but not if an enable failed. We 21850Sstevel@tonic-gate * squelch output the second time we walk fmris; we saw 21860Sstevel@tonic-gate * the errors the first time. 21870Sstevel@tonic-gate */ 21880Sstevel@tonic-gate if ((err = scf_walk_fmri(h, argc, argv, 0, set_fmri_enabled, 21890Sstevel@tonic-gate (void *)flags, &error, uu_warn)) != 0) { 21900Sstevel@tonic-gate 21910Sstevel@tonic-gate uu_warn(gettext("failed to iterate over " 21920Sstevel@tonic-gate "instances: %s\n"), scf_strerror(err)); 21930Sstevel@tonic-gate exit_status = UU_EXIT_FATAL; 21940Sstevel@tonic-gate 21950Sstevel@tonic-gate } else if (wait && exit_status == 0 && 21960Sstevel@tonic-gate (err = scf_walk_fmri(h, argc, argv, 0, wait_fmri_enabled, 21970Sstevel@tonic-gate (void *)flags, &error, quiet)) != 0) { 21980Sstevel@tonic-gate 21990Sstevel@tonic-gate uu_warn(gettext("failed to iterate over " 22000Sstevel@tonic-gate "instances: %s\n"), scf_strerror(err)); 22010Sstevel@tonic-gate exit_status = UU_EXIT_FATAL; 22020Sstevel@tonic-gate } 22030Sstevel@tonic-gate 22040Sstevel@tonic-gate if (error > 0) 22050Sstevel@tonic-gate exit_status = error; 22060Sstevel@tonic-gate 22070Sstevel@tonic-gate } else if (strcmp(argv[optind], "disable") == 0) { 22080Sstevel@tonic-gate int flags = 0; 22090Sstevel@tonic-gate int wait = 0; 22100Sstevel@tonic-gate int error = 0; 22110Sstevel@tonic-gate 22120Sstevel@tonic-gate ++optind; 22130Sstevel@tonic-gate 22140Sstevel@tonic-gate while ((o = getopt(argc, argv, "st")) != -1) { 22150Sstevel@tonic-gate if (o == 't') 22160Sstevel@tonic-gate flags |= SET_TEMPORARY; 22170Sstevel@tonic-gate else if (o == 's') 22180Sstevel@tonic-gate wait = 1; 22190Sstevel@tonic-gate else if (o == '?') 22200Sstevel@tonic-gate usage(); 22210Sstevel@tonic-gate else { 22220Sstevel@tonic-gate assert(0); 22230Sstevel@tonic-gate abort(); 22240Sstevel@tonic-gate } 22250Sstevel@tonic-gate } 22260Sstevel@tonic-gate argc -= optind; 22270Sstevel@tonic-gate argv += optind; 22280Sstevel@tonic-gate 22290Sstevel@tonic-gate if (argc <= 0) 22300Sstevel@tonic-gate usage(); 22310Sstevel@tonic-gate 22320Sstevel@tonic-gate /* 22330Sstevel@tonic-gate * We want to continue with -s processing if we had 22340Sstevel@tonic-gate * invalid options, but not if a disable failed. We 22350Sstevel@tonic-gate * squelch output the second time we walk fmris; we saw 22360Sstevel@tonic-gate * the errors the first time. 22370Sstevel@tonic-gate */ 22380Sstevel@tonic-gate if ((err = scf_walk_fmri(h, argc, argv, 0, set_fmri_enabled, 22390Sstevel@tonic-gate (void *)flags, &exit_status, uu_warn)) != 0) { 22400Sstevel@tonic-gate 22410Sstevel@tonic-gate uu_warn(gettext("failed to iterate over " 22420Sstevel@tonic-gate "instances: %s\n"), scf_strerror(err)); 22430Sstevel@tonic-gate exit_status = UU_EXIT_FATAL; 22440Sstevel@tonic-gate 22450Sstevel@tonic-gate } else if (wait && exit_status == 0 && 22460Sstevel@tonic-gate (err = scf_walk_fmri(h, argc, argv, 0, wait_fmri_disabled, 22470Sstevel@tonic-gate (void *)flags, &error, quiet)) != 0) { 22480Sstevel@tonic-gate 22490Sstevel@tonic-gate uu_warn(gettext("failed to iterate over " 22500Sstevel@tonic-gate "instances: %s\n"), scf_strerror(err)); 22510Sstevel@tonic-gate exit_status = UU_EXIT_FATAL; 22520Sstevel@tonic-gate } 22530Sstevel@tonic-gate 22540Sstevel@tonic-gate if (error > 0) 22550Sstevel@tonic-gate exit_status = error; 22560Sstevel@tonic-gate 22570Sstevel@tonic-gate } else if (strcmp(argv[optind], "restart") == 0) { 22580Sstevel@tonic-gate ++optind; 22590Sstevel@tonic-gate 22600Sstevel@tonic-gate if (optind >= argc) 22610Sstevel@tonic-gate usage(); 22620Sstevel@tonic-gate 22630Sstevel@tonic-gate if ((err = scf_walk_fmri(h, argc - optind, argv + optind, 0, 22640Sstevel@tonic-gate set_fmri_action, (void *)SCF_PROPERTY_RESTART, 22650Sstevel@tonic-gate &exit_status, uu_warn)) != 0) { 22660Sstevel@tonic-gate uu_warn(gettext("failed to iterate over " 22670Sstevel@tonic-gate "instances: %s\n"), scf_strerror(err)); 22680Sstevel@tonic-gate exit_status = UU_EXIT_FATAL; 22690Sstevel@tonic-gate } 22700Sstevel@tonic-gate 22710Sstevel@tonic-gate } else if (strcmp(argv[optind], "refresh") == 0) { 22720Sstevel@tonic-gate ++optind; 22730Sstevel@tonic-gate 22740Sstevel@tonic-gate if (optind >= argc) 22750Sstevel@tonic-gate usage(); 22760Sstevel@tonic-gate 22770Sstevel@tonic-gate if ((err = scf_walk_fmri(h, argc - optind, argv + optind, 0, 22780Sstevel@tonic-gate set_fmri_action, (void *)SCF_PROPERTY_REFRESH, 22790Sstevel@tonic-gate &exit_status, uu_warn)) != 0) { 22800Sstevel@tonic-gate uu_warn(gettext("failed to iterate over " 22810Sstevel@tonic-gate "instances: %s\n"), scf_strerror(scf_error())); 22820Sstevel@tonic-gate exit_status = UU_EXIT_FATAL; 22830Sstevel@tonic-gate } 22840Sstevel@tonic-gate 22850Sstevel@tonic-gate } else if (strcmp(argv[optind], "mark") == 0) { 22860Sstevel@tonic-gate int flags = 0; 22870Sstevel@tonic-gate scf_walk_callback callback; 22880Sstevel@tonic-gate 22890Sstevel@tonic-gate ++optind; 22900Sstevel@tonic-gate 22910Sstevel@tonic-gate while ((o = getopt(argc, argv, "It")) != -1) { 22920Sstevel@tonic-gate if (o == 'I') 22930Sstevel@tonic-gate flags |= MARK_IMMEDIATE; 22940Sstevel@tonic-gate else if (o == 't') 22950Sstevel@tonic-gate flags |= MARK_TEMPORARY; 22960Sstevel@tonic-gate else if (o == '?') 22970Sstevel@tonic-gate usage(); 22980Sstevel@tonic-gate else { 22990Sstevel@tonic-gate assert(0); 23000Sstevel@tonic-gate abort(); 23010Sstevel@tonic-gate } 23020Sstevel@tonic-gate } 23030Sstevel@tonic-gate 23040Sstevel@tonic-gate if (argc - optind < 2) 23050Sstevel@tonic-gate usage(); 23060Sstevel@tonic-gate 23070Sstevel@tonic-gate if (strcmp(argv[optind], "degraded") == 0) { 23080Sstevel@tonic-gate if (flags & MARK_TEMPORARY) 23090Sstevel@tonic-gate uu_xdie(UU_EXIT_USAGE, gettext("-t may not be " 23100Sstevel@tonic-gate "used with degraded.\n")); 23110Sstevel@tonic-gate callback = force_degraded; 23120Sstevel@tonic-gate 23130Sstevel@tonic-gate } else if (strcmp(argv[optind], "maintenance") == 0) { 23140Sstevel@tonic-gate callback = force_maintenance; 23150Sstevel@tonic-gate } else { 23160Sstevel@tonic-gate usage(); 23170Sstevel@tonic-gate } 23180Sstevel@tonic-gate 23190Sstevel@tonic-gate if ((err = scf_walk_fmri(h, argc - optind - 1, 23200Sstevel@tonic-gate argv + optind + 1, 0, callback, NULL, &exit_status, 23210Sstevel@tonic-gate uu_warn)) != 0) { 23220Sstevel@tonic-gate uu_warn(gettext("failed to iterate over " 23230Sstevel@tonic-gate "instances: %s\n"), 23240Sstevel@tonic-gate scf_strerror(err)); 23250Sstevel@tonic-gate exit_status = UU_EXIT_FATAL; 23260Sstevel@tonic-gate } 23270Sstevel@tonic-gate 23280Sstevel@tonic-gate } else if (strcmp(argv[optind], "clear") == 0) { 23290Sstevel@tonic-gate ++optind; 23300Sstevel@tonic-gate 23310Sstevel@tonic-gate if (optind >= argc) 23320Sstevel@tonic-gate usage(); 23330Sstevel@tonic-gate 23340Sstevel@tonic-gate if ((err = scf_walk_fmri(h, argc - optind, argv + optind, 0, 23350Sstevel@tonic-gate clear_instance, NULL, &exit_status, uu_warn)) != 0) { 23360Sstevel@tonic-gate uu_warn(gettext("failed to iterate over " 23370Sstevel@tonic-gate "instances: %s\n"), scf_strerror(err)); 23380Sstevel@tonic-gate exit_status = UU_EXIT_FATAL; 23390Sstevel@tonic-gate } 23400Sstevel@tonic-gate 23410Sstevel@tonic-gate } else if (strcmp(argv[optind], "milestone") == 0) { 23420Sstevel@tonic-gate boolean_t temporary = B_TRUE; 23430Sstevel@tonic-gate const char *milestone; 23440Sstevel@tonic-gate 23450Sstevel@tonic-gate ++optind; 23460Sstevel@tonic-gate 23470Sstevel@tonic-gate while ((o = getopt(argc, argv, "d")) != -1) { 23480Sstevel@tonic-gate if (o == 'd') 23490Sstevel@tonic-gate temporary = B_FALSE; 23500Sstevel@tonic-gate else if (o == '?') 23510Sstevel@tonic-gate usage_milestone(); 23520Sstevel@tonic-gate else { 23530Sstevel@tonic-gate assert(0); 23540Sstevel@tonic-gate abort(); 23550Sstevel@tonic-gate } 23560Sstevel@tonic-gate } 23570Sstevel@tonic-gate 23580Sstevel@tonic-gate if (optind >= argc) 23590Sstevel@tonic-gate usage_milestone(); 23600Sstevel@tonic-gate 23610Sstevel@tonic-gate milestone = validate_milestone(argv[optind]); 23620Sstevel@tonic-gate 23630Sstevel@tonic-gate set_milestone(milestone, temporary); 23640Sstevel@tonic-gate } else if (strcmp(argv[optind], "_smf_backup") == 0) { 23650Sstevel@tonic-gate const char *reason = NULL; 23660Sstevel@tonic-gate 23670Sstevel@tonic-gate ++optind; 23680Sstevel@tonic-gate 23690Sstevel@tonic-gate if (optind != argc - 1) 23700Sstevel@tonic-gate usage(); 23710Sstevel@tonic-gate 23720Sstevel@tonic-gate if ((err = _scf_request_backup(h, argv[optind])) != 23730Sstevel@tonic-gate SCF_SUCCESS) { 23740Sstevel@tonic-gate switch (scf_error()) { 23750Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 23760Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 23770Sstevel@tonic-gate case SCF_ERROR_BACKEND_READONLY: 23780Sstevel@tonic-gate scfdie(); 23790Sstevel@tonic-gate break; 23800Sstevel@tonic-gate 23810Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED: 23820Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT: 23830Sstevel@tonic-gate reason = scf_strerror(scf_error()); 23840Sstevel@tonic-gate break; 23850Sstevel@tonic-gate 23860Sstevel@tonic-gate case SCF_ERROR_INTERNAL: 23870Sstevel@tonic-gate reason = 23880Sstevel@tonic-gate "unknown error (see console for details)"; 23890Sstevel@tonic-gate break; 23900Sstevel@tonic-gate } 23910Sstevel@tonic-gate uu_warn("failed to backup repository: %s\n", reason); 23920Sstevel@tonic-gate exit_status = UU_EXIT_FATAL; 23930Sstevel@tonic-gate } 23940Sstevel@tonic-gate } else { 23950Sstevel@tonic-gate usage(); 23960Sstevel@tonic-gate } 23970Sstevel@tonic-gate 23980Sstevel@tonic-gate if (scf_handle_unbind(h) == -1) 23990Sstevel@tonic-gate scfdie(); 24000Sstevel@tonic-gate scf_handle_destroy(h); 24010Sstevel@tonic-gate 24020Sstevel@tonic-gate return (exit_status); 24030Sstevel@tonic-gate } 2404