17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
594501b61Sskamm * Common Development and Distribution License (the "License").
694501b61Sskamm * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
213eae19d9Swesolows
227c478bd9Sstevel@tonic-gate /*
23c42520ebSJan Friedel * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24afffa6e9SMarcel Telka * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
25e6ccb06dSAndrew Stormont * Copyright 2018 RackTop Systems.
26*8fff7887SJohn Levon * Copyright 2020 Joyent, Inc.
277c478bd9Sstevel@tonic-gate */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include "libscf_impl.h"
307c478bd9Sstevel@tonic-gate
311f6eb021SLiane Praza #include <assert.h>
327c478bd9Sstevel@tonic-gate #include <libuutil.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
3694501b61Sskamm #include <sys/param.h>
3794501b61Sskamm #include <errno.h>
3894501b61Sskamm #include <libgen.h>
39dfac3eb2SDavid Powell #include <assert.h>
407c478bd9Sstevel@tonic-gate #include "midlevel_impl.h"
41d75e6a5dStn #include "lowlevel_impl.h"
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate #ifndef NDEBUG
447c478bd9Sstevel@tonic-gate #define bad_error(func, err) { \
457c478bd9Sstevel@tonic-gate uu_warn("%s:%d: %s failed with unexpected error %d. Aborting.\n", \
467c478bd9Sstevel@tonic-gate __FILE__, __LINE__, func, err); \
477c478bd9Sstevel@tonic-gate abort(); \
487c478bd9Sstevel@tonic-gate }
497c478bd9Sstevel@tonic-gate #else
507c478bd9Sstevel@tonic-gate #define bad_error(func, err) abort()
517c478bd9Sstevel@tonic-gate #endif
527c478bd9Sstevel@tonic-gate
5394501b61Sskamm /* Path to speedy files area must end with a slash */
5494501b61Sskamm #define SMF_SPEEDY_FILES_PATH "/etc/svc/volatile/"
5594501b61Sskamm
56d75e6a5dStn void
scf_simple_handle_destroy(scf_simple_handle_t * simple_h)57d75e6a5dStn scf_simple_handle_destroy(scf_simple_handle_t *simple_h)
58d75e6a5dStn {
59d75e6a5dStn if (simple_h == NULL)
60d75e6a5dStn return;
61d75e6a5dStn
62d75e6a5dStn scf_pg_destroy(simple_h->running_pg);
63d75e6a5dStn scf_pg_destroy(simple_h->editing_pg);
64d75e6a5dStn scf_snapshot_destroy(simple_h->snap);
65d75e6a5dStn scf_instance_destroy(simple_h->inst);
66d75e6a5dStn scf_handle_destroy(simple_h->h);
67d75e6a5dStn uu_free(simple_h);
68d75e6a5dStn }
69d75e6a5dStn
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate * Given a base service FMRI and the names of a property group and property,
727c478bd9Sstevel@tonic-gate * assemble_fmri() merges them into a property FMRI. Note that if the base
737c478bd9Sstevel@tonic-gate * FMRI is NULL, assemble_fmri() gets the base FMRI from scf_myname().
747c478bd9Sstevel@tonic-gate */
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate static char *
assemble_fmri(scf_handle_t * h,const char * base,const char * pg,const char * prop)777c478bd9Sstevel@tonic-gate assemble_fmri(scf_handle_t *h, const char *base, const char *pg,
787c478bd9Sstevel@tonic-gate const char *prop)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate size_t fmri_sz, pglen;
817c478bd9Sstevel@tonic-gate ssize_t baselen;
827c478bd9Sstevel@tonic-gate char *fmri_buf;
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate if (prop == NULL) {
857c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
867c478bd9Sstevel@tonic-gate return (NULL);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate if (pg == NULL)
907c478bd9Sstevel@tonic-gate pglen = strlen(SCF_PG_APP_DEFAULT);
917c478bd9Sstevel@tonic-gate else
927c478bd9Sstevel@tonic-gate pglen = strlen(pg);
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate if (base == NULL) {
957c478bd9Sstevel@tonic-gate if ((baselen = scf_myname(h, NULL, 0)) == -1)
967c478bd9Sstevel@tonic-gate return (NULL);
977c478bd9Sstevel@tonic-gate } else {
987c478bd9Sstevel@tonic-gate baselen = strlen(base);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate fmri_sz = baselen + sizeof (SCF_FMRI_PROPERTYGRP_PREFIX) - 1 +
1027c478bd9Sstevel@tonic-gate pglen + sizeof (SCF_FMRI_PROPERTY_PREFIX) - 1 +
1037c478bd9Sstevel@tonic-gate strlen(prop) + 1;
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate if ((fmri_buf = malloc(fmri_sz)) == NULL) {
1067c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY);
1077c478bd9Sstevel@tonic-gate return (NULL);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate if (base == NULL) {
1117c478bd9Sstevel@tonic-gate if (scf_myname(h, fmri_buf, fmri_sz) == -1) {
1127c478bd9Sstevel@tonic-gate free(fmri_buf);
1137c478bd9Sstevel@tonic-gate return (NULL);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate } else {
1167c478bd9Sstevel@tonic-gate (void) strcpy(fmri_buf, base);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate (void) strcat(fmri_buf, SCF_FMRI_PROPERTYGRP_PREFIX);
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate if (pg == NULL)
1227c478bd9Sstevel@tonic-gate (void) strcat(fmri_buf, SCF_PG_APP_DEFAULT);
1237c478bd9Sstevel@tonic-gate else
1247c478bd9Sstevel@tonic-gate (void) strcat(fmri_buf, pg);
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate (void) strcat(fmri_buf, SCF_FMRI_PROPERTY_PREFIX);
1277c478bd9Sstevel@tonic-gate (void) strcat(fmri_buf, prop);
1287c478bd9Sstevel@tonic-gate return (fmri_buf);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate * Given a property, this function allocates and fills an scf_simple_prop_t
1337c478bd9Sstevel@tonic-gate * with the data it contains.
1347c478bd9Sstevel@tonic-gate */
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate static scf_simple_prop_t *
fill_prop(scf_property_t * prop,const char * pgname,const char * propname,scf_handle_t * h)1377c478bd9Sstevel@tonic-gate fill_prop(scf_property_t *prop, const char *pgname, const char *propname,
1387c478bd9Sstevel@tonic-gate scf_handle_t *h)
1397c478bd9Sstevel@tonic-gate {
1404a014300SToomas Soome scf_simple_prop_t *ret;
1414a014300SToomas Soome scf_iter_t *iter;
1424a014300SToomas Soome scf_value_t *val;
1434a014300SToomas Soome int iterret, i;
1444a014300SToomas Soome ssize_t valsize, numvals;
1454a014300SToomas Soome union scf_simple_prop_val *vallist = NULL, *vallist_backup = NULL;
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate if ((ret = malloc(sizeof (*ret))) == NULL) {
1487c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY);
1497c478bd9Sstevel@tonic-gate return (NULL);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate ret->pr_next = NULL;
1537c478bd9Sstevel@tonic-gate ret->pr_pg = NULL;
1547c478bd9Sstevel@tonic-gate ret->pr_iter = 0;
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate if (pgname == NULL)
1577c478bd9Sstevel@tonic-gate ret->pr_pgname = strdup(SCF_PG_APP_DEFAULT);
1587c478bd9Sstevel@tonic-gate else
1597c478bd9Sstevel@tonic-gate ret->pr_pgname = strdup(pgname);
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate if (ret->pr_pgname == NULL) {
1627c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY);
1637c478bd9Sstevel@tonic-gate free(ret);
1647c478bd9Sstevel@tonic-gate return (NULL);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate if ((ret->pr_propname = strdup(propname)) == NULL) {
1687c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY);
1697c478bd9Sstevel@tonic-gate free(ret->pr_pgname);
1707c478bd9Sstevel@tonic-gate free(ret);
1717c478bd9Sstevel@tonic-gate return (NULL);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate if (scf_property_type(prop, &ret->pr_type) == -1)
1757c478bd9Sstevel@tonic-gate goto error3;
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate if ((iter = scf_iter_create(h)) == NULL)
1787c478bd9Sstevel@tonic-gate goto error3;
1797c478bd9Sstevel@tonic-gate if ((val = scf_value_create(h)) == NULL) {
1807c478bd9Sstevel@tonic-gate scf_iter_destroy(iter);
1817c478bd9Sstevel@tonic-gate goto error3;
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate if (scf_iter_property_values(iter, prop) == -1)
1857c478bd9Sstevel@tonic-gate goto error1;
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate for (numvals = 0; (iterret = scf_iter_next_value(iter, val)) == 1;
1887c478bd9Sstevel@tonic-gate numvals++) {
1897c478bd9Sstevel@tonic-gate vallist_backup = vallist;
1907c478bd9Sstevel@tonic-gate if ((vallist = realloc(vallist, (numvals + 1) *
1917c478bd9Sstevel@tonic-gate sizeof (*vallist))) == NULL) {
1927c478bd9Sstevel@tonic-gate vallist = vallist_backup;
1937c478bd9Sstevel@tonic-gate goto error1;
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate switch (ret->pr_type) {
1977c478bd9Sstevel@tonic-gate case SCF_TYPE_BOOLEAN:
1987c478bd9Sstevel@tonic-gate if (scf_value_get_boolean(val,
1997c478bd9Sstevel@tonic-gate &vallist[numvals].pv_bool) == -1)
2007c478bd9Sstevel@tonic-gate goto error1;
2017c478bd9Sstevel@tonic-gate break;
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate case SCF_TYPE_COUNT:
2047c478bd9Sstevel@tonic-gate if (scf_value_get_count(val,
2057c478bd9Sstevel@tonic-gate &vallist[numvals].pv_uint) == -1)
2067c478bd9Sstevel@tonic-gate goto error1;
2077c478bd9Sstevel@tonic-gate break;
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate case SCF_TYPE_INTEGER:
2107c478bd9Sstevel@tonic-gate if (scf_value_get_integer(val,
2117c478bd9Sstevel@tonic-gate &vallist[numvals].pv_int) == -1)
2127c478bd9Sstevel@tonic-gate goto error1;
2137c478bd9Sstevel@tonic-gate break;
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate case SCF_TYPE_TIME:
2167c478bd9Sstevel@tonic-gate if (scf_value_get_time(val,
2177c478bd9Sstevel@tonic-gate &vallist[numvals].pv_time.t_sec,
2187c478bd9Sstevel@tonic-gate &vallist[numvals].pv_time.t_nsec) == -1)
2197c478bd9Sstevel@tonic-gate goto error1;
2207c478bd9Sstevel@tonic-gate break;
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate case SCF_TYPE_ASTRING:
2237c478bd9Sstevel@tonic-gate vallist[numvals].pv_str = NULL;
2247c478bd9Sstevel@tonic-gate if ((valsize = scf_value_get_astring(val, NULL, 0)) ==
2257c478bd9Sstevel@tonic-gate -1)
2267c478bd9Sstevel@tonic-gate goto error1;
2277c478bd9Sstevel@tonic-gate if ((vallist[numvals].pv_str = malloc(valsize+1)) ==
2287c478bd9Sstevel@tonic-gate NULL) {
2297c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2307c478bd9Sstevel@tonic-gate goto error1;
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate if (scf_value_get_astring(val,
2337c478bd9Sstevel@tonic-gate vallist[numvals].pv_str, valsize+1) == -1) {
2347c478bd9Sstevel@tonic-gate free(vallist[numvals].pv_str);
2357c478bd9Sstevel@tonic-gate goto error1;
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate break;
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate case SCF_TYPE_USTRING:
2407c478bd9Sstevel@tonic-gate case SCF_TYPE_HOST:
2417c478bd9Sstevel@tonic-gate case SCF_TYPE_HOSTNAME:
242b56bf881SAntonello Cruz case SCF_TYPE_NET_ADDR:
2437c478bd9Sstevel@tonic-gate case SCF_TYPE_NET_ADDR_V4:
2447c478bd9Sstevel@tonic-gate case SCF_TYPE_NET_ADDR_V6:
2457c478bd9Sstevel@tonic-gate case SCF_TYPE_URI:
2467c478bd9Sstevel@tonic-gate case SCF_TYPE_FMRI:
2477c478bd9Sstevel@tonic-gate vallist[numvals].pv_str = NULL;
2487c478bd9Sstevel@tonic-gate if ((valsize = scf_value_get_ustring(val, NULL, 0)) ==
2497c478bd9Sstevel@tonic-gate -1)
2507c478bd9Sstevel@tonic-gate goto error1;
2517c478bd9Sstevel@tonic-gate if ((vallist[numvals].pv_str = malloc(valsize+1)) ==
2527c478bd9Sstevel@tonic-gate NULL) {
2537c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2547c478bd9Sstevel@tonic-gate goto error1;
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate if (scf_value_get_ustring(val,
2577c478bd9Sstevel@tonic-gate vallist[numvals].pv_str, valsize+1) == -1) {
2587c478bd9Sstevel@tonic-gate free(vallist[numvals].pv_str);
2597c478bd9Sstevel@tonic-gate goto error1;
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate break;
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate case SCF_TYPE_OPAQUE:
2647c478bd9Sstevel@tonic-gate vallist[numvals].pv_opaque.o_value = NULL;
2657c478bd9Sstevel@tonic-gate if ((valsize = scf_value_get_opaque(val, NULL, 0)) ==
2667c478bd9Sstevel@tonic-gate -1)
2677c478bd9Sstevel@tonic-gate goto error1;
2687c478bd9Sstevel@tonic-gate if ((vallist[numvals].pv_opaque.o_value =
2697c478bd9Sstevel@tonic-gate malloc(valsize)) == NULL) {
2707c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2717c478bd9Sstevel@tonic-gate goto error1;
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate vallist[numvals].pv_opaque.o_size = valsize;
2747c478bd9Sstevel@tonic-gate if (scf_value_get_opaque(val,
2757c478bd9Sstevel@tonic-gate vallist[numvals].pv_opaque.o_value,
2767c478bd9Sstevel@tonic-gate valsize) == -1) {
2777c478bd9Sstevel@tonic-gate free(vallist[numvals].pv_opaque.o_value);
2787c478bd9Sstevel@tonic-gate goto error1;
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate break;
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate default:
2837c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INTERNAL);
2847c478bd9Sstevel@tonic-gate goto error1;
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate if (iterret == -1) {
2903eae19d9Swesolows int err = scf_error();
2913eae19d9Swesolows if (err != SCF_ERROR_CONNECTION_BROKEN &&
2923eae19d9Swesolows err != SCF_ERROR_PERMISSION_DENIED)
2937c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INTERNAL);
2947c478bd9Sstevel@tonic-gate goto error1;
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate
2977c478bd9Sstevel@tonic-gate ret->pr_vallist = vallist;
2987c478bd9Sstevel@tonic-gate ret->pr_numvalues = numvals;
2997c478bd9Sstevel@tonic-gate
3007c478bd9Sstevel@tonic-gate scf_iter_destroy(iter);
3017c478bd9Sstevel@tonic-gate (void) scf_value_destroy(val);
3027c478bd9Sstevel@tonic-gate
3037c478bd9Sstevel@tonic-gate return (ret);
3047c478bd9Sstevel@tonic-gate
3057c478bd9Sstevel@tonic-gate /*
3067c478bd9Sstevel@tonic-gate * Exit point for a successful call. Below this line are exit points
3077c478bd9Sstevel@tonic-gate * for failures at various stages during the function.
3087c478bd9Sstevel@tonic-gate */
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate error1:
3117c478bd9Sstevel@tonic-gate if (vallist == NULL)
3127c478bd9Sstevel@tonic-gate goto error2;
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate switch (ret->pr_type) {
3157c478bd9Sstevel@tonic-gate case SCF_TYPE_ASTRING:
3167c478bd9Sstevel@tonic-gate case SCF_TYPE_USTRING:
3177c478bd9Sstevel@tonic-gate case SCF_TYPE_HOST:
3187c478bd9Sstevel@tonic-gate case SCF_TYPE_HOSTNAME:
319b56bf881SAntonello Cruz case SCF_TYPE_NET_ADDR:
3207c478bd9Sstevel@tonic-gate case SCF_TYPE_NET_ADDR_V4:
3217c478bd9Sstevel@tonic-gate case SCF_TYPE_NET_ADDR_V6:
3227c478bd9Sstevel@tonic-gate case SCF_TYPE_URI:
3237c478bd9Sstevel@tonic-gate case SCF_TYPE_FMRI: {
3247c478bd9Sstevel@tonic-gate for (i = 0; i < numvals; i++) {
3257c478bd9Sstevel@tonic-gate free(vallist[i].pv_str);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate break;
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate case SCF_TYPE_OPAQUE: {
3307c478bd9Sstevel@tonic-gate for (i = 0; i < numvals; i++) {
3317c478bd9Sstevel@tonic-gate free(vallist[i].pv_opaque.o_value);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate break;
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate default:
3367c478bd9Sstevel@tonic-gate break;
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate
3397c478bd9Sstevel@tonic-gate free(vallist);
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate error2:
3427c478bd9Sstevel@tonic-gate scf_iter_destroy(iter);
3437c478bd9Sstevel@tonic-gate (void) scf_value_destroy(val);
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate error3:
3467c478bd9Sstevel@tonic-gate free(ret->pr_pgname);
3477c478bd9Sstevel@tonic-gate free(ret->pr_propname);
3487c478bd9Sstevel@tonic-gate free(ret);
3497c478bd9Sstevel@tonic-gate return (NULL);
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate * insert_app_props iterates over a property iterator, getting all the
3547c478bd9Sstevel@tonic-gate * properties from a property group, and adding or overwriting them into
3557c478bd9Sstevel@tonic-gate * a simple_app_props_t. This is used by scf_simple_app_props_get to provide
3567c478bd9Sstevel@tonic-gate * service/instance composition while filling the app_props_t.
3577c478bd9Sstevel@tonic-gate * insert_app_props iterates over a single property group.
3587c478bd9Sstevel@tonic-gate */
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate static int
insert_app_props(scf_iter_t * propiter,char * pgname,char * propname,struct scf_simple_pg * thispg,scf_property_t * prop,size_t namelen,scf_handle_t * h)3617c478bd9Sstevel@tonic-gate insert_app_props(scf_iter_t *propiter, char *pgname, char *propname, struct
3627c478bd9Sstevel@tonic-gate scf_simple_pg *thispg, scf_property_t *prop, size_t namelen,
3637c478bd9Sstevel@tonic-gate scf_handle_t *h)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate scf_simple_prop_t *thisprop, *prevprop, *newprop;
3667c478bd9Sstevel@tonic-gate uint8_t found;
3677c478bd9Sstevel@tonic-gate int propiter_ret;
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate while ((propiter_ret = scf_iter_next_property(propiter, prop)) == 1) {
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate if (scf_property_get_name(prop, propname, namelen) < 0) {
3727c478bd9Sstevel@tonic-gate if (scf_error() == SCF_ERROR_NOT_SET)
3737c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INTERNAL);
3747c478bd9Sstevel@tonic-gate return (-1);
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate
3777c478bd9Sstevel@tonic-gate thisprop = thispg->pg_proplist;
3787c478bd9Sstevel@tonic-gate prevprop = thispg->pg_proplist;
3797c478bd9Sstevel@tonic-gate found = 0;
3807c478bd9Sstevel@tonic-gate
3817c478bd9Sstevel@tonic-gate while ((thisprop != NULL) && (!found)) {
3827c478bd9Sstevel@tonic-gate if (strcmp(thisprop->pr_propname, propname) == 0) {
3837c478bd9Sstevel@tonic-gate found = 1;
3847c478bd9Sstevel@tonic-gate if ((newprop = fill_prop(prop, pgname,
3857c478bd9Sstevel@tonic-gate propname, h)) == NULL)
3867c478bd9Sstevel@tonic-gate return (-1);
3877c478bd9Sstevel@tonic-gate
3887c478bd9Sstevel@tonic-gate if (thisprop == thispg->pg_proplist)
3897c478bd9Sstevel@tonic-gate thispg->pg_proplist = newprop;
3907c478bd9Sstevel@tonic-gate else
3917c478bd9Sstevel@tonic-gate prevprop->pr_next = newprop;
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gate newprop->pr_pg = thispg;
3947c478bd9Sstevel@tonic-gate newprop->pr_next = thisprop->pr_next;
3957c478bd9Sstevel@tonic-gate scf_simple_prop_free(thisprop);
3967c478bd9Sstevel@tonic-gate thisprop = NULL;
3977c478bd9Sstevel@tonic-gate } else {
3987c478bd9Sstevel@tonic-gate if (thisprop != thispg->pg_proplist)
3997c478bd9Sstevel@tonic-gate prevprop = prevprop->pr_next;
4007c478bd9Sstevel@tonic-gate thisprop = thisprop->pr_next;
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate
4047c478bd9Sstevel@tonic-gate if (!found) {
4057c478bd9Sstevel@tonic-gate if ((newprop = fill_prop(prop, pgname, propname, h)) ==
4067c478bd9Sstevel@tonic-gate NULL)
4077c478bd9Sstevel@tonic-gate return (-1);
4087c478bd9Sstevel@tonic-gate
4097c478bd9Sstevel@tonic-gate if (thispg->pg_proplist == NULL)
4107c478bd9Sstevel@tonic-gate thispg->pg_proplist = newprop;
4117c478bd9Sstevel@tonic-gate else
4127c478bd9Sstevel@tonic-gate prevprop->pr_next = newprop;
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate newprop->pr_pg = thispg;
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate
4187c478bd9Sstevel@tonic-gate if (propiter_ret == -1) {
4197c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
4207c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INTERNAL);
4217c478bd9Sstevel@tonic-gate return (-1);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate
4247c478bd9Sstevel@tonic-gate return (0);
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate
4277c478bd9Sstevel@tonic-gate
4287c478bd9Sstevel@tonic-gate /*
4297c478bd9Sstevel@tonic-gate * Sets up e in tx to set pname's values. Returns 0 on success or -1 on
4307c478bd9Sstevel@tonic-gate * failure, with scf_error() set to
4317c478bd9Sstevel@tonic-gate * SCF_ERROR_HANDLE_MISMATCH - tx & e are derived from different handles
4327c478bd9Sstevel@tonic-gate * SCF_ERROR_INVALID_ARGUMENT - pname or ty are invalid
4337c478bd9Sstevel@tonic-gate * SCF_ERROR_NOT_BOUND - handle is not bound
4347c478bd9Sstevel@tonic-gate * SCF_ERROR_CONNECTION_BROKEN - connection was broken
4357c478bd9Sstevel@tonic-gate * SCF_ERROR_NOT_SET - tx has not been started
4367c478bd9Sstevel@tonic-gate * SCF_ERROR_DELETED - the pg tx was started on was deleted
4377c478bd9Sstevel@tonic-gate */
4387c478bd9Sstevel@tonic-gate static int
transaction_property_set(scf_transaction_t * tx,scf_transaction_entry_t * e,const char * pname,scf_type_t ty)4397c478bd9Sstevel@tonic-gate transaction_property_set(scf_transaction_t *tx, scf_transaction_entry_t *e,
4407c478bd9Sstevel@tonic-gate const char *pname, scf_type_t ty)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate for (;;) {
4437c478bd9Sstevel@tonic-gate if (scf_transaction_property_change_type(tx, e, pname, ty) == 0)
4447c478bd9Sstevel@tonic-gate return (0);
4457c478bd9Sstevel@tonic-gate
4467c478bd9Sstevel@tonic-gate switch (scf_error()) {
4477c478bd9Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
4487c478bd9Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
4497c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
4507c478bd9Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
4517c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
4527c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED:
4537c478bd9Sstevel@tonic-gate default:
4547c478bd9Sstevel@tonic-gate return (-1);
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
4577c478bd9Sstevel@tonic-gate break;
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate if (scf_transaction_property_new(tx, e, pname, ty) == 0)
4617c478bd9Sstevel@tonic-gate return (0);
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate switch (scf_error()) {
4647c478bd9Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
4657c478bd9Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
4667c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
4677c478bd9Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
4687c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
4697c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED:
4707c478bd9Sstevel@tonic-gate default:
4717c478bd9Sstevel@tonic-gate return (-1);
4727c478bd9Sstevel@tonic-gate
4737c478bd9Sstevel@tonic-gate case SCF_ERROR_EXISTS:
4747c478bd9Sstevel@tonic-gate break;
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate static int
get_inst_enabled(const scf_instance_t * inst,const char * pgname)4807c478bd9Sstevel@tonic-gate get_inst_enabled(const scf_instance_t *inst, const char *pgname)
4817c478bd9Sstevel@tonic-gate {
4824a014300SToomas Soome scf_propertygroup_t *gpg = NULL;
4834a014300SToomas Soome scf_property_t *eprop = NULL;
4844a014300SToomas Soome scf_value_t *v = NULL;
4857c478bd9Sstevel@tonic-gate scf_handle_t *h = NULL;
4867c478bd9Sstevel@tonic-gate uint8_t enabled;
4877c478bd9Sstevel@tonic-gate int ret = -1;
4887c478bd9Sstevel@tonic-gate
4897c478bd9Sstevel@tonic-gate if ((h = scf_instance_handle(inst)) == NULL)
4907c478bd9Sstevel@tonic-gate return (-1);
4917c478bd9Sstevel@tonic-gate
4927c478bd9Sstevel@tonic-gate if ((gpg = scf_pg_create(h)) == NULL ||
4937c478bd9Sstevel@tonic-gate (eprop = scf_property_create(h)) == NULL ||
4947c478bd9Sstevel@tonic-gate (v = scf_value_create(h)) == NULL)
4957c478bd9Sstevel@tonic-gate goto out;
4967c478bd9Sstevel@tonic-gate
4977c478bd9Sstevel@tonic-gate if (scf_instance_get_pg(inst, pgname, gpg) ||
4987c478bd9Sstevel@tonic-gate scf_pg_get_property(gpg, SCF_PROPERTY_ENABLED, eprop) ||
4997c478bd9Sstevel@tonic-gate scf_property_get_value(eprop, v) ||
5007c478bd9Sstevel@tonic-gate scf_value_get_boolean(v, &enabled))
5017c478bd9Sstevel@tonic-gate goto out;
5027c478bd9Sstevel@tonic-gate ret = enabled;
5037c478bd9Sstevel@tonic-gate
5047c478bd9Sstevel@tonic-gate out:
5057c478bd9Sstevel@tonic-gate scf_pg_destroy(gpg);
5067c478bd9Sstevel@tonic-gate scf_property_destroy(eprop);
5077c478bd9Sstevel@tonic-gate scf_value_destroy(v);
5087c478bd9Sstevel@tonic-gate return (ret);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate
5117c478bd9Sstevel@tonic-gate /*
5127c478bd9Sstevel@tonic-gate * set_inst_enabled() is a "master" enable/disable call that takes the
5137c478bd9Sstevel@tonic-gate * instance and the desired state for the enabled bit in the instance's
5147c478bd9Sstevel@tonic-gate * named property group. If the group doesn't exist, it's created with the
5157c478bd9Sstevel@tonic-gate * given flags. Called by smf_{dis,en}able_instance().
516*8fff7887SJohn Levon *
517*8fff7887SJohn Levon * Note that if we're enabling, comment will be "", and we use that to clear out
518*8fff7887SJohn Levon * any old disabled comment.
5197c478bd9Sstevel@tonic-gate */
5207c478bd9Sstevel@tonic-gate static int
set_inst_enabled(const scf_instance_t * inst,uint8_t desired,const char * pgname,uint32_t pgflags,const char * comment)5217c478bd9Sstevel@tonic-gate set_inst_enabled(const scf_instance_t *inst, uint8_t desired,
522*8fff7887SJohn Levon const char *pgname, uint32_t pgflags, const char *comment)
5237c478bd9Sstevel@tonic-gate {
5244a014300SToomas Soome scf_transaction_t *tx = NULL;
525*8fff7887SJohn Levon scf_transaction_entry_t *ent1 = NULL;
526*8fff7887SJohn Levon scf_transaction_entry_t *ent2 = NULL;
5274a014300SToomas Soome scf_propertygroup_t *gpg = NULL;
5284a014300SToomas Soome scf_property_t *eprop = NULL;
529*8fff7887SJohn Levon scf_value_t *v1 = NULL;
530*8fff7887SJohn Levon scf_value_t *v2 = NULL;
5317c478bd9Sstevel@tonic-gate scf_handle_t *h = NULL;
5324a014300SToomas Soome int ret = -1;
5337c478bd9Sstevel@tonic-gate int committed;
5347c478bd9Sstevel@tonic-gate uint8_t b;
5357c478bd9Sstevel@tonic-gate
5367c478bd9Sstevel@tonic-gate if ((h = scf_instance_handle(inst)) == NULL)
5377c478bd9Sstevel@tonic-gate return (-1);
5387c478bd9Sstevel@tonic-gate
5397c478bd9Sstevel@tonic-gate if ((gpg = scf_pg_create(h)) == NULL ||
5407c478bd9Sstevel@tonic-gate (eprop = scf_property_create(h)) == NULL ||
541*8fff7887SJohn Levon (v1 = scf_value_create(h)) == NULL ||
542*8fff7887SJohn Levon (v2 = scf_value_create(h)) == NULL ||
5437c478bd9Sstevel@tonic-gate (tx = scf_transaction_create(h)) == NULL ||
544*8fff7887SJohn Levon (ent1 = scf_entry_create(h)) == NULL ||
545*8fff7887SJohn Levon (ent2 = scf_entry_create(h)) == NULL)
5467c478bd9Sstevel@tonic-gate goto out;
5477c478bd9Sstevel@tonic-gate
548fee38f6fStn general_pg_get:
549fee38f6fStn if (scf_instance_get_pg(inst, SCF_PG_GENERAL, gpg) == -1) {
5507c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND)
5517c478bd9Sstevel@tonic-gate goto out;
5527c478bd9Sstevel@tonic-gate
553fee38f6fStn if (scf_instance_add_pg(inst, SCF_PG_GENERAL,
554fee38f6fStn SCF_GROUP_FRAMEWORK, SCF_PG_GENERAL_FLAGS, gpg) == -1) {
5557c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_EXISTS)
5567c478bd9Sstevel@tonic-gate goto out;
557fee38f6fStn goto general_pg_get;
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate }
560fee38f6fStn
561fee38f6fStn if (strcmp(pgname, SCF_PG_GENERAL) != 0) {
562fee38f6fStn get:
563fee38f6fStn if (scf_instance_get_pg(inst, pgname, gpg) == -1) {
564fee38f6fStn if (scf_error() != SCF_ERROR_NOT_FOUND)
565fee38f6fStn goto out;
566fee38f6fStn
567fee38f6fStn if (scf_instance_add_pg(inst, pgname,
568fee38f6fStn SCF_GROUP_FRAMEWORK, pgflags, gpg) == -1) {
569fee38f6fStn if (scf_error() != SCF_ERROR_EXISTS)
570fee38f6fStn goto out;
571fee38f6fStn goto get;
572fee38f6fStn }
573fee38f6fStn }
574fee38f6fStn }
575fee38f6fStn
5767c478bd9Sstevel@tonic-gate if (scf_pg_get_property(gpg, SCF_PROPERTY_ENABLED, eprop) == -1) {
5777c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND)
5787c478bd9Sstevel@tonic-gate goto out;
5797c478bd9Sstevel@tonic-gate else
5807c478bd9Sstevel@tonic-gate goto set;
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate /*
5847c478bd9Sstevel@tonic-gate * If it's already set the way we want, forgo the transaction.
5857c478bd9Sstevel@tonic-gate */
586*8fff7887SJohn Levon if (scf_property_get_value(eprop, v1) == -1) {
5877c478bd9Sstevel@tonic-gate switch (scf_error()) {
5887c478bd9Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED:
5897c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
5907c478bd9Sstevel@tonic-gate /* Misconfigured, so set anyway. */
5917c478bd9Sstevel@tonic-gate goto set;
5927c478bd9Sstevel@tonic-gate
5937c478bd9Sstevel@tonic-gate default:
5947c478bd9Sstevel@tonic-gate goto out;
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate }
597*8fff7887SJohn Levon if (scf_value_get_boolean(v1, &b) == -1) {
5987c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_TYPE_MISMATCH)
5997c478bd9Sstevel@tonic-gate goto out;
6007c478bd9Sstevel@tonic-gate goto set;
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate if (b == desired) {
6037c478bd9Sstevel@tonic-gate ret = 0;
6047c478bd9Sstevel@tonic-gate goto out;
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate
6077c478bd9Sstevel@tonic-gate set:
6087c478bd9Sstevel@tonic-gate do {
6097c478bd9Sstevel@tonic-gate if (scf_transaction_start(tx, gpg) == -1)
6107c478bd9Sstevel@tonic-gate goto out;
6117c478bd9Sstevel@tonic-gate
612*8fff7887SJohn Levon if (transaction_property_set(tx, ent1, SCF_PROPERTY_ENABLED,
6137c478bd9Sstevel@tonic-gate SCF_TYPE_BOOLEAN) != 0) {
6147c478bd9Sstevel@tonic-gate switch (scf_error()) {
6157c478bd9Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
6167c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED:
6177c478bd9Sstevel@tonic-gate default:
6187c478bd9Sstevel@tonic-gate goto out;
6197c478bd9Sstevel@tonic-gate
6207c478bd9Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
6217c478bd9Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
6227c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
6237c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
6247c478bd9Sstevel@tonic-gate bad_error("transaction_property_set",
6257c478bd9Sstevel@tonic-gate scf_error());
6267c478bd9Sstevel@tonic-gate }
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate
629*8fff7887SJohn Levon scf_value_set_boolean(v1, desired);
630*8fff7887SJohn Levon if (scf_entry_add_value(ent1, v1) == -1)
631*8fff7887SJohn Levon goto out;
632*8fff7887SJohn Levon
633*8fff7887SJohn Levon if (transaction_property_set(tx, ent2,
634*8fff7887SJohn Levon SCF_PROPERTY_COMMENT, SCF_TYPE_ASTRING) != 0) {
635*8fff7887SJohn Levon switch (scf_error()) {
636*8fff7887SJohn Levon case SCF_ERROR_CONNECTION_BROKEN:
637*8fff7887SJohn Levon case SCF_ERROR_DELETED:
638*8fff7887SJohn Levon default:
639*8fff7887SJohn Levon goto out;
640*8fff7887SJohn Levon
641*8fff7887SJohn Levon case SCF_ERROR_HANDLE_MISMATCH:
642*8fff7887SJohn Levon case SCF_ERROR_INVALID_ARGUMENT:
643*8fff7887SJohn Levon case SCF_ERROR_NOT_BOUND:
644*8fff7887SJohn Levon case SCF_ERROR_NOT_SET:
645*8fff7887SJohn Levon bad_error("transaction_property_set",
646*8fff7887SJohn Levon scf_error());
647*8fff7887SJohn Levon }
648*8fff7887SJohn Levon }
649*8fff7887SJohn Levon
650*8fff7887SJohn Levon if (scf_value_set_astring(v2, comment) == -1)
651*8fff7887SJohn Levon goto out;
652*8fff7887SJohn Levon
653*8fff7887SJohn Levon if (scf_entry_add_value(ent2, v2) == -1)
6547c478bd9Sstevel@tonic-gate goto out;
6557c478bd9Sstevel@tonic-gate
6567c478bd9Sstevel@tonic-gate committed = scf_transaction_commit(tx);
6577c478bd9Sstevel@tonic-gate if (committed == -1)
6587c478bd9Sstevel@tonic-gate goto out;
6597c478bd9Sstevel@tonic-gate
6607c478bd9Sstevel@tonic-gate scf_transaction_reset(tx);
6617c478bd9Sstevel@tonic-gate
6627c478bd9Sstevel@tonic-gate if (committed == 0) { /* out-of-sync */
6637c478bd9Sstevel@tonic-gate if (scf_pg_update(gpg) == -1)
6647c478bd9Sstevel@tonic-gate goto out;
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate } while (committed == 0);
6677c478bd9Sstevel@tonic-gate
6687c478bd9Sstevel@tonic-gate ret = 0;
6697c478bd9Sstevel@tonic-gate
6707c478bd9Sstevel@tonic-gate out:
671*8fff7887SJohn Levon scf_value_destroy(v1);
672*8fff7887SJohn Levon scf_value_destroy(v2);
673*8fff7887SJohn Levon scf_entry_destroy(ent1);
674*8fff7887SJohn Levon scf_entry_destroy(ent2);
6757c478bd9Sstevel@tonic-gate scf_transaction_destroy(tx);
6767c478bd9Sstevel@tonic-gate scf_property_destroy(eprop);
6777c478bd9Sstevel@tonic-gate scf_pg_destroy(gpg);
6787c478bd9Sstevel@tonic-gate
6797c478bd9Sstevel@tonic-gate return (ret);
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate
6827c478bd9Sstevel@tonic-gate static int
delete_inst_enabled(const scf_instance_t * inst,const char * pgname)6837c478bd9Sstevel@tonic-gate delete_inst_enabled(const scf_instance_t *inst, const char *pgname)
6847c478bd9Sstevel@tonic-gate {
6854a014300SToomas Soome scf_transaction_t *tx = NULL;
686*8fff7887SJohn Levon scf_transaction_entry_t *ent1 = NULL;
687*8fff7887SJohn Levon scf_transaction_entry_t *ent2 = NULL;
6884a014300SToomas Soome scf_propertygroup_t *gpg = NULL;
6897c478bd9Sstevel@tonic-gate scf_handle_t *h = NULL;
6907c478bd9Sstevel@tonic-gate int ret = -1;
6917c478bd9Sstevel@tonic-gate int committed;
6927c478bd9Sstevel@tonic-gate
6937c478bd9Sstevel@tonic-gate if ((h = scf_instance_handle(inst)) == NULL)
6947c478bd9Sstevel@tonic-gate return (-1);
6957c478bd9Sstevel@tonic-gate
6967c478bd9Sstevel@tonic-gate if ((gpg = scf_pg_create(h)) == NULL ||
6977c478bd9Sstevel@tonic-gate (tx = scf_transaction_create(h)) == NULL ||
698*8fff7887SJohn Levon (ent1 = scf_entry_create(h)) == NULL ||
699*8fff7887SJohn Levon (ent2 = scf_entry_create(h)) == NULL)
7007c478bd9Sstevel@tonic-gate goto out;
7017c478bd9Sstevel@tonic-gate
7027c478bd9Sstevel@tonic-gate if (scf_instance_get_pg(inst, pgname, gpg) != 0)
7037c478bd9Sstevel@tonic-gate goto error;
7047c478bd9Sstevel@tonic-gate do {
705*8fff7887SJohn Levon if (scf_transaction_start(tx, gpg) == -1)
706*8fff7887SJohn Levon goto error;
707*8fff7887SJohn Levon
708*8fff7887SJohn Levon ret = scf_transaction_property_delete(tx, ent1,
709*8fff7887SJohn Levon SCF_PROPERTY_ENABLED);
710*8fff7887SJohn Levon
711*8fff7887SJohn Levon if (ret == -1 && scf_error() != SCF_ERROR_DELETED &&
712*8fff7887SJohn Levon scf_error() != SCF_ERROR_NOT_FOUND)
713*8fff7887SJohn Levon goto error;
714*8fff7887SJohn Levon
715*8fff7887SJohn Levon ret = scf_transaction_property_delete(tx, ent2,
716*8fff7887SJohn Levon SCF_PROPERTY_COMMENT);
717*8fff7887SJohn Levon
718*8fff7887SJohn Levon if (ret == -1 && scf_error() != SCF_ERROR_DELETED &&
719*8fff7887SJohn Levon scf_error() != SCF_ERROR_NOT_FOUND)
720*8fff7887SJohn Levon goto error;
721*8fff7887SJohn Levon
722*8fff7887SJohn Levon if ((committed = scf_transaction_commit(tx)) == -1)
7237c478bd9Sstevel@tonic-gate goto error;
7247c478bd9Sstevel@tonic-gate
7257c478bd9Sstevel@tonic-gate scf_transaction_reset(tx);
7267c478bd9Sstevel@tonic-gate
7277c478bd9Sstevel@tonic-gate if (committed == 0 && scf_pg_update(gpg) == -1)
7287c478bd9Sstevel@tonic-gate goto error;
7297c478bd9Sstevel@tonic-gate } while (committed == 0);
7307c478bd9Sstevel@tonic-gate
7317c478bd9Sstevel@tonic-gate ret = 0;
7327c478bd9Sstevel@tonic-gate goto out;
7337c478bd9Sstevel@tonic-gate
7347c478bd9Sstevel@tonic-gate error:
7357c478bd9Sstevel@tonic-gate switch (scf_error()) {
7367c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED:
7377c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
7387c478bd9Sstevel@tonic-gate /* success */
7397c478bd9Sstevel@tonic-gate ret = 0;
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate
7427c478bd9Sstevel@tonic-gate out:
743*8fff7887SJohn Levon scf_entry_destroy(ent1);
744*8fff7887SJohn Levon scf_entry_destroy(ent2);
7457c478bd9Sstevel@tonic-gate scf_transaction_destroy(tx);
7467c478bd9Sstevel@tonic-gate scf_pg_destroy(gpg);
7477c478bd9Sstevel@tonic-gate
7487c478bd9Sstevel@tonic-gate return (ret);
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate
7517c478bd9Sstevel@tonic-gate /*
7527c478bd9Sstevel@tonic-gate * Returns 0 on success or -1 on failure. On failure leaves scf_error() set to
7537c478bd9Sstevel@tonic-gate * SCF_ERROR_HANDLE_DESTROYED - inst's handle has been destroyed
7547c478bd9Sstevel@tonic-gate * SCF_ERROR_NOT_BOUND - inst's handle is not bound
7557c478bd9Sstevel@tonic-gate * SCF_ERROR_CONNECTION_BROKEN - the repository connection was broken
7567c478bd9Sstevel@tonic-gate * SCF_ERROR_NOT_SET - inst is not set
7577c478bd9Sstevel@tonic-gate * SCF_ERROR_DELETED - inst was deleted
7587c478bd9Sstevel@tonic-gate * SCF_ERROR_PERMISSION_DENIED
7597c478bd9Sstevel@tonic-gate * SCF_ERROR_BACKEND_ACCESS
7607c478bd9Sstevel@tonic-gate * SCF_ERROR_BACKEND_READONLY
7617c478bd9Sstevel@tonic-gate */
7627c478bd9Sstevel@tonic-gate static int
set_inst_action_inst(scf_instance_t * inst,const char * action)7637c478bd9Sstevel@tonic-gate set_inst_action_inst(scf_instance_t *inst, const char *action)
7647c478bd9Sstevel@tonic-gate {
7657c478bd9Sstevel@tonic-gate scf_handle_t *h;
7667c478bd9Sstevel@tonic-gate scf_transaction_t *tx = NULL;
7677c478bd9Sstevel@tonic-gate scf_transaction_entry_t *ent = NULL;
7687c478bd9Sstevel@tonic-gate scf_propertygroup_t *pg = NULL;
7697c478bd9Sstevel@tonic-gate scf_property_t *prop = NULL;
7707c478bd9Sstevel@tonic-gate scf_value_t *v = NULL;
7717c478bd9Sstevel@tonic-gate int trans, ret = -1;
7727c478bd9Sstevel@tonic-gate int64_t t;
7737c478bd9Sstevel@tonic-gate hrtime_t timestamp;
7747c478bd9Sstevel@tonic-gate
7757c478bd9Sstevel@tonic-gate if ((h = scf_instance_handle(inst)) == NULL ||
7767c478bd9Sstevel@tonic-gate (pg = scf_pg_create(h)) == NULL ||
7777c478bd9Sstevel@tonic-gate (prop = scf_property_create(h)) == NULL ||
7787c478bd9Sstevel@tonic-gate (v = scf_value_create(h)) == NULL ||
7797c478bd9Sstevel@tonic-gate (tx = scf_transaction_create(h)) == NULL ||
7807c478bd9Sstevel@tonic-gate (ent = scf_entry_create(h)) == NULL)
7817c478bd9Sstevel@tonic-gate goto out;
7827c478bd9Sstevel@tonic-gate
7837c478bd9Sstevel@tonic-gate get:
7847c478bd9Sstevel@tonic-gate if (scf_instance_get_pg(inst, SCF_PG_RESTARTER_ACTIONS, pg) == -1) {
7857c478bd9Sstevel@tonic-gate switch (scf_error()) {
7867c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
7877c478bd9Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
7887c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
7897c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED:
7907c478bd9Sstevel@tonic-gate default:
7917c478bd9Sstevel@tonic-gate goto out;
7927c478bd9Sstevel@tonic-gate
7937c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
7947c478bd9Sstevel@tonic-gate break;
7957c478bd9Sstevel@tonic-gate
7967c478bd9Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
7977c478bd9Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
7987c478bd9Sstevel@tonic-gate bad_error("scf_instance_get_pg", scf_error());
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate
8017c478bd9Sstevel@tonic-gate /* Try creating the restarter_actions property group. */
8027c478bd9Sstevel@tonic-gate add:
8037c478bd9Sstevel@tonic-gate if (scf_instance_add_pg(inst, SCF_PG_RESTARTER_ACTIONS,
8047c478bd9Sstevel@tonic-gate SCF_PG_RESTARTER_ACTIONS_TYPE,
8057c478bd9Sstevel@tonic-gate SCF_PG_RESTARTER_ACTIONS_FLAGS, pg) == -1) {
8067c478bd9Sstevel@tonic-gate switch (scf_error()) {
8077c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
8087c478bd9Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
8097c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
8107c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED:
8117c478bd9Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED:
8127c478bd9Sstevel@tonic-gate case SCF_ERROR_BACKEND_ACCESS:
8137c478bd9Sstevel@tonic-gate case SCF_ERROR_BACKEND_READONLY:
8147c478bd9Sstevel@tonic-gate default:
8157c478bd9Sstevel@tonic-gate goto out;
8167c478bd9Sstevel@tonic-gate
8177c478bd9Sstevel@tonic-gate case SCF_ERROR_EXISTS:
8187c478bd9Sstevel@tonic-gate goto get;
8197c478bd9Sstevel@tonic-gate
8207c478bd9Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
8217c478bd9Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
8227c478bd9Sstevel@tonic-gate bad_error("scf_instance_add_pg", scf_error());
8237c478bd9Sstevel@tonic-gate }
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate
8277c478bd9Sstevel@tonic-gate for (;;) {
8287c478bd9Sstevel@tonic-gate timestamp = gethrtime();
8297c478bd9Sstevel@tonic-gate
8307c478bd9Sstevel@tonic-gate if (scf_pg_get_property(pg, action, prop) != 0) {
8317c478bd9Sstevel@tonic-gate switch (scf_error()) {
8327c478bd9Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
8337c478bd9Sstevel@tonic-gate default:
8347c478bd9Sstevel@tonic-gate goto out;
8357c478bd9Sstevel@tonic-gate
8367c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED:
8377c478bd9Sstevel@tonic-gate goto add;
8387c478bd9Sstevel@tonic-gate
8397c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
8407c478bd9Sstevel@tonic-gate break;
8417c478bd9Sstevel@tonic-gate
8427c478bd9Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
8437c478bd9Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
8447c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
8457c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
8467c478bd9Sstevel@tonic-gate bad_error("scf_pg_get_property", scf_error());
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate } else if (scf_property_get_value(prop, v) != 0) {
8497c478bd9Sstevel@tonic-gate switch (scf_error()) {
8507c478bd9Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
8517c478bd9Sstevel@tonic-gate default:
8527c478bd9Sstevel@tonic-gate goto out;
8537c478bd9Sstevel@tonic-gate
8547c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED:
8557c478bd9Sstevel@tonic-gate goto add;
8567c478bd9Sstevel@tonic-gate
8577c478bd9Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED:
8587c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
8597c478bd9Sstevel@tonic-gate break;
8607c478bd9Sstevel@tonic-gate
8617c478bd9Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
8627c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
8637c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
8647c478bd9Sstevel@tonic-gate bad_error("scf_property_get_value",
8657c478bd9Sstevel@tonic-gate scf_error());
8667c478bd9Sstevel@tonic-gate }
8677c478bd9Sstevel@tonic-gate } else if (scf_value_get_integer(v, &t) != 0) {
8687c478bd9Sstevel@tonic-gate bad_error("scf_value_get_integer", scf_error());
8697c478bd9Sstevel@tonic-gate } else if (t > timestamp) {
8707c478bd9Sstevel@tonic-gate break;
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate
8737c478bd9Sstevel@tonic-gate if (scf_transaction_start(tx, pg) == -1) {
8747c478bd9Sstevel@tonic-gate switch (scf_error()) {
8757c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
8767c478bd9Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
8777c478bd9Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED:
8787c478bd9Sstevel@tonic-gate case SCF_ERROR_BACKEND_ACCESS:
8797c478bd9Sstevel@tonic-gate case SCF_ERROR_BACKEND_READONLY:
8807c478bd9Sstevel@tonic-gate default:
8817c478bd9Sstevel@tonic-gate goto out;
8827c478bd9Sstevel@tonic-gate
8837c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED:
8847c478bd9Sstevel@tonic-gate goto add;
8857c478bd9Sstevel@tonic-gate
8867c478bd9Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
8877c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
8887c478bd9Sstevel@tonic-gate case SCF_ERROR_IN_USE:
8897c478bd9Sstevel@tonic-gate bad_error("scf_transaction_start", scf_error());
8907c478bd9Sstevel@tonic-gate }
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate
8937c478bd9Sstevel@tonic-gate if (transaction_property_set(tx, ent, action,
8947c478bd9Sstevel@tonic-gate SCF_TYPE_INTEGER) != 0) {
8957c478bd9Sstevel@tonic-gate switch (scf_error()) {
8967c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
8977c478bd9Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
8987c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED:
8997c478bd9Sstevel@tonic-gate default:
9007c478bd9Sstevel@tonic-gate goto out;
9017c478bd9Sstevel@tonic-gate
9027c478bd9Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
9037c478bd9Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
9047c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
9057c478bd9Sstevel@tonic-gate bad_error("transaction_property_set",
9067c478bd9Sstevel@tonic-gate scf_error());
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate
9107c478bd9Sstevel@tonic-gate scf_value_set_integer(v, timestamp);
9117c478bd9Sstevel@tonic-gate if (scf_entry_add_value(ent, v) == -1)
9127c478bd9Sstevel@tonic-gate bad_error("scf_entry_add_value", scf_error());
9137c478bd9Sstevel@tonic-gate
9147c478bd9Sstevel@tonic-gate trans = scf_transaction_commit(tx);
9157c478bd9Sstevel@tonic-gate if (trans == 1)
9167c478bd9Sstevel@tonic-gate break;
9177c478bd9Sstevel@tonic-gate
9187c478bd9Sstevel@tonic-gate if (trans != 0) {
9197c478bd9Sstevel@tonic-gate switch (scf_error()) {
9207c478bd9Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
9217c478bd9Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED:
9227c478bd9Sstevel@tonic-gate case SCF_ERROR_BACKEND_ACCESS:
9237c478bd9Sstevel@tonic-gate case SCF_ERROR_BACKEND_READONLY:
9247c478bd9Sstevel@tonic-gate default:
9257c478bd9Sstevel@tonic-gate goto out;
9267c478bd9Sstevel@tonic-gate
9277c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED:
9287c478bd9Sstevel@tonic-gate scf_transaction_reset(tx);
9297c478bd9Sstevel@tonic-gate goto add;
9307c478bd9Sstevel@tonic-gate
9317c478bd9Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
9327c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
9337c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
9347c478bd9Sstevel@tonic-gate bad_error("scf_transaction_commit",
9357c478bd9Sstevel@tonic-gate scf_error());
9367c478bd9Sstevel@tonic-gate }
9377c478bd9Sstevel@tonic-gate }
9387c478bd9Sstevel@tonic-gate
9397c478bd9Sstevel@tonic-gate scf_transaction_reset(tx);
940bc9767f9Stn if (scf_pg_update(pg) == -1) {
9417c478bd9Sstevel@tonic-gate switch (scf_error()) {
9427c478bd9Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
9437c478bd9Sstevel@tonic-gate default:
9447c478bd9Sstevel@tonic-gate goto out;
9457c478bd9Sstevel@tonic-gate
9467c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED:
9477c478bd9Sstevel@tonic-gate goto add;
9487c478bd9Sstevel@tonic-gate
9497c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
9507c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
9517c478bd9Sstevel@tonic-gate bad_error("scf_pg_update", scf_error());
9527c478bd9Sstevel@tonic-gate }
9537c478bd9Sstevel@tonic-gate }
9547c478bd9Sstevel@tonic-gate }
9557c478bd9Sstevel@tonic-gate
9567c478bd9Sstevel@tonic-gate ret = 0;
9577c478bd9Sstevel@tonic-gate
9587c478bd9Sstevel@tonic-gate out:
9597c478bd9Sstevel@tonic-gate scf_value_destroy(v);
9607c478bd9Sstevel@tonic-gate scf_entry_destroy(ent);
9617c478bd9Sstevel@tonic-gate scf_transaction_destroy(tx);
9627c478bd9Sstevel@tonic-gate scf_property_destroy(prop);
9637c478bd9Sstevel@tonic-gate scf_pg_destroy(pg);
9647c478bd9Sstevel@tonic-gate return (ret);
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate
9677c478bd9Sstevel@tonic-gate static int
set_inst_action(const char * fmri,const char * action)9687c478bd9Sstevel@tonic-gate set_inst_action(const char *fmri, const char *action)
9697c478bd9Sstevel@tonic-gate {
9707c478bd9Sstevel@tonic-gate scf_handle_t *h;
9717c478bd9Sstevel@tonic-gate scf_instance_t *inst;
9727c478bd9Sstevel@tonic-gate int ret = -1;
9737c478bd9Sstevel@tonic-gate
974f6e214c7SGavin Maltby h = _scf_handle_create_and_bind(SCF_VERSION);
9757c478bd9Sstevel@tonic-gate if (h == NULL)
9767c478bd9Sstevel@tonic-gate return (-1);
9777c478bd9Sstevel@tonic-gate
9787c478bd9Sstevel@tonic-gate inst = scf_instance_create(h);
9797c478bd9Sstevel@tonic-gate
9807c478bd9Sstevel@tonic-gate if (inst != NULL) {
9817c478bd9Sstevel@tonic-gate if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
982bf6e99ccSGary Mills NULL, SCF_DECODE_FMRI_EXACT) == 0) {
9837c478bd9Sstevel@tonic-gate ret = set_inst_action_inst(inst, action);
984bf6e99ccSGary Mills if (ret == -1 && scf_error() == SCF_ERROR_DELETED)
985bf6e99ccSGary Mills (void) scf_set_error(SCF_ERROR_NOT_FOUND);
986bf6e99ccSGary Mills } else {
987bf6e99ccSGary Mills switch (scf_error()) {
988bf6e99ccSGary Mills case SCF_ERROR_CONSTRAINT_VIOLATED:
989bf6e99ccSGary Mills (void) scf_set_error(
990bf6e99ccSGary Mills SCF_ERROR_INVALID_ARGUMENT);
991bf6e99ccSGary Mills break;
992bf6e99ccSGary Mills case SCF_ERROR_DELETED:
993bf6e99ccSGary Mills (void) scf_set_error(SCF_ERROR_NOT_FOUND);
994bf6e99ccSGary Mills break;
995bf6e99ccSGary Mills }
996bf6e99ccSGary Mills }
9977c478bd9Sstevel@tonic-gate
9987c478bd9Sstevel@tonic-gate scf_instance_destroy(inst);
9997c478bd9Sstevel@tonic-gate }
10007c478bd9Sstevel@tonic-gate
10017c478bd9Sstevel@tonic-gate scf_handle_destroy(h);
10027c478bd9Sstevel@tonic-gate
10037c478bd9Sstevel@tonic-gate return (ret);
10047c478bd9Sstevel@tonic-gate }
10057c478bd9Sstevel@tonic-gate
10067c478bd9Sstevel@tonic-gate
10077c478bd9Sstevel@tonic-gate /*
10087c478bd9Sstevel@tonic-gate * get_inst_state() gets the state string from an instance, and returns
10097c478bd9Sstevel@tonic-gate * the SCF_STATE_* constant that coincides with the instance's current state.
10107c478bd9Sstevel@tonic-gate */
10117c478bd9Sstevel@tonic-gate
10127c478bd9Sstevel@tonic-gate static int
get_inst_state(scf_instance_t * inst,scf_handle_t * h)10137c478bd9Sstevel@tonic-gate get_inst_state(scf_instance_t *inst, scf_handle_t *h)
10147c478bd9Sstevel@tonic-gate {
10157c478bd9Sstevel@tonic-gate scf_propertygroup_t *pg = NULL;
10167c478bd9Sstevel@tonic-gate scf_property_t *prop = NULL;
10177c478bd9Sstevel@tonic-gate scf_value_t *val = NULL;
10187c478bd9Sstevel@tonic-gate char state[MAX_SCF_STATE_STRING_SZ];
10197c478bd9Sstevel@tonic-gate int ret = -1;
10207c478bd9Sstevel@tonic-gate
10217c478bd9Sstevel@tonic-gate if (((pg = scf_pg_create(h)) == NULL) ||
10227c478bd9Sstevel@tonic-gate ((prop = scf_property_create(h)) == NULL) ||
10237c478bd9Sstevel@tonic-gate ((val = scf_value_create(h)) == NULL))
10247c478bd9Sstevel@tonic-gate goto out;
10257c478bd9Sstevel@tonic-gate
10267c478bd9Sstevel@tonic-gate /* Pull the state property from the instance */
10277c478bd9Sstevel@tonic-gate
10287c478bd9Sstevel@tonic-gate if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) == -1 ||
10297c478bd9Sstevel@tonic-gate scf_pg_get_property(pg, SCF_PROPERTY_STATE, prop) == -1 ||
10307c478bd9Sstevel@tonic-gate scf_property_get_value(prop, val) == -1) {
10317c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
10327c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INTERNAL);
10337c478bd9Sstevel@tonic-gate goto out;
10347c478bd9Sstevel@tonic-gate }
10357c478bd9Sstevel@tonic-gate
10367c478bd9Sstevel@tonic-gate if (scf_value_get_astring(val, state, sizeof (state)) <= 0) {
10377c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INTERNAL);
10387c478bd9Sstevel@tonic-gate goto out;
10397c478bd9Sstevel@tonic-gate }
10407c478bd9Sstevel@tonic-gate
10417c478bd9Sstevel@tonic-gate if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0) {
10427c478bd9Sstevel@tonic-gate ret = SCF_STATE_UNINIT;
10437c478bd9Sstevel@tonic-gate } else if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
10447c478bd9Sstevel@tonic-gate ret = SCF_STATE_MAINT;
10457c478bd9Sstevel@tonic-gate } else if (strcmp(state, SCF_STATE_STRING_OFFLINE) == 0) {
10467c478bd9Sstevel@tonic-gate ret = SCF_STATE_OFFLINE;
10477c478bd9Sstevel@tonic-gate } else if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0) {
10487c478bd9Sstevel@tonic-gate ret = SCF_STATE_DISABLED;
10497c478bd9Sstevel@tonic-gate } else if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0) {
10507c478bd9Sstevel@tonic-gate ret = SCF_STATE_ONLINE;
10517c478bd9Sstevel@tonic-gate } else if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 0) {
10527c478bd9Sstevel@tonic-gate ret = SCF_STATE_DEGRADED;
10537c478bd9Sstevel@tonic-gate }
10547c478bd9Sstevel@tonic-gate
10557c478bd9Sstevel@tonic-gate out:
10567c478bd9Sstevel@tonic-gate scf_pg_destroy(pg);
10577c478bd9Sstevel@tonic-gate scf_property_destroy(prop);
10587c478bd9Sstevel@tonic-gate (void) scf_value_destroy(val);
10597c478bd9Sstevel@tonic-gate
10607c478bd9Sstevel@tonic-gate return (ret);
10617c478bd9Sstevel@tonic-gate }
10627c478bd9Sstevel@tonic-gate
10637c478bd9Sstevel@tonic-gate /*
10647c478bd9Sstevel@tonic-gate * Sets an instance to be enabled or disabled after reboot, using the
10657c478bd9Sstevel@tonic-gate * temporary (overriding) general_ovr property group to reflect the
10667c478bd9Sstevel@tonic-gate * present state, if it is different.
10677c478bd9Sstevel@tonic-gate */
10687c478bd9Sstevel@tonic-gate static int
set_inst_enabled_atboot(scf_instance_t * inst,uint8_t desired,const char * comment)1069*8fff7887SJohn Levon set_inst_enabled_atboot(scf_instance_t *inst, uint8_t desired,
1070*8fff7887SJohn Levon const char *comment)
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate int enabled;
10737c478bd9Sstevel@tonic-gate int persistent;
10747c478bd9Sstevel@tonic-gate int ret = -1;
10757c478bd9Sstevel@tonic-gate
10767c478bd9Sstevel@tonic-gate if ((persistent = get_inst_enabled(inst, SCF_PG_GENERAL)) < 0) {
10777c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND)
10787c478bd9Sstevel@tonic-gate goto out;
10797c478bd9Sstevel@tonic-gate persistent = B_FALSE;
10807c478bd9Sstevel@tonic-gate }
10817c478bd9Sstevel@tonic-gate if ((enabled = get_inst_enabled(inst, SCF_PG_GENERAL_OVR)) < 0) {
10827c478bd9Sstevel@tonic-gate enabled = persistent;
10837c478bd9Sstevel@tonic-gate if (persistent != desired) {
10847c478bd9Sstevel@tonic-gate /*
10857c478bd9Sstevel@tonic-gate * Temporarily store the present enabled state.
10867c478bd9Sstevel@tonic-gate */
10877c478bd9Sstevel@tonic-gate if (set_inst_enabled(inst, persistent,
1088*8fff7887SJohn Levon SCF_PG_GENERAL_OVR, SCF_PG_GENERAL_OVR_FLAGS,
1089*8fff7887SJohn Levon comment))
10907c478bd9Sstevel@tonic-gate goto out;
10917c478bd9Sstevel@tonic-gate }
10927c478bd9Sstevel@tonic-gate }
10937c478bd9Sstevel@tonic-gate if (persistent != desired)
10947c478bd9Sstevel@tonic-gate if (set_inst_enabled(inst, desired, SCF_PG_GENERAL,
1095*8fff7887SJohn Levon SCF_PG_GENERAL_FLAGS, comment))
10967c478bd9Sstevel@tonic-gate goto out;
10977c478bd9Sstevel@tonic-gate if (enabled == desired)
10987c478bd9Sstevel@tonic-gate ret = delete_inst_enabled(inst, SCF_PG_GENERAL_OVR);
10997c478bd9Sstevel@tonic-gate else
11007c478bd9Sstevel@tonic-gate ret = 0;
11017c478bd9Sstevel@tonic-gate
11027c478bd9Sstevel@tonic-gate out:
11037c478bd9Sstevel@tonic-gate return (ret);
11047c478bd9Sstevel@tonic-gate }
11057c478bd9Sstevel@tonic-gate
11067c478bd9Sstevel@tonic-gate static int
set_inst_enabled_flags(const char * fmri,int flags,uint8_t desired,const char * comment)1107*8fff7887SJohn Levon set_inst_enabled_flags(const char *fmri, int flags, uint8_t desired,
1108*8fff7887SJohn Levon const char *comment)
11097c478bd9Sstevel@tonic-gate {
11107c478bd9Sstevel@tonic-gate int ret = -1;
11117c478bd9Sstevel@tonic-gate scf_handle_t *h;
11127c478bd9Sstevel@tonic-gate scf_instance_t *inst;
11137c478bd9Sstevel@tonic-gate
11147c478bd9Sstevel@tonic-gate if (flags & ~(SMF_TEMPORARY | SMF_AT_NEXT_BOOT) ||
11157c478bd9Sstevel@tonic-gate flags & SMF_TEMPORARY && flags & SMF_AT_NEXT_BOOT) {
11167c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
11177c478bd9Sstevel@tonic-gate return (ret);
11187c478bd9Sstevel@tonic-gate }
11197c478bd9Sstevel@tonic-gate
1120f6e214c7SGavin Maltby if ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL)
11217c478bd9Sstevel@tonic-gate return (ret);
11227c478bd9Sstevel@tonic-gate
11237c478bd9Sstevel@tonic-gate if ((inst = scf_instance_create(h)) == NULL) {
11247c478bd9Sstevel@tonic-gate scf_handle_destroy(h);
11257c478bd9Sstevel@tonic-gate return (ret);
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate
11287c478bd9Sstevel@tonic-gate if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL, NULL,
11297c478bd9Sstevel@tonic-gate SCF_DECODE_FMRI_EXACT) == -1) {
11307c478bd9Sstevel@tonic-gate if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
11317c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
11327c478bd9Sstevel@tonic-gate goto out;
11337c478bd9Sstevel@tonic-gate }
11347c478bd9Sstevel@tonic-gate
11357c478bd9Sstevel@tonic-gate if (flags & SMF_AT_NEXT_BOOT) {
1136*8fff7887SJohn Levon ret = set_inst_enabled_atboot(inst, desired, comment);
11377c478bd9Sstevel@tonic-gate } else {
11387c478bd9Sstevel@tonic-gate if (set_inst_enabled(inst, desired, flags & SMF_TEMPORARY ?
11397c478bd9Sstevel@tonic-gate SCF_PG_GENERAL_OVR : SCF_PG_GENERAL, flags & SMF_TEMPORARY ?
1140*8fff7887SJohn Levon SCF_PG_GENERAL_OVR_FLAGS : SCF_PG_GENERAL_FLAGS, comment))
11417c478bd9Sstevel@tonic-gate goto out;
11427c478bd9Sstevel@tonic-gate
11437c478bd9Sstevel@tonic-gate /*
11447c478bd9Sstevel@tonic-gate * Make the persistent value effective by deleting the
11457c478bd9Sstevel@tonic-gate * temporary one.
11467c478bd9Sstevel@tonic-gate */
11477c478bd9Sstevel@tonic-gate if (flags & SMF_TEMPORARY)
11487c478bd9Sstevel@tonic-gate ret = 0;
11497c478bd9Sstevel@tonic-gate else
11507c478bd9Sstevel@tonic-gate ret = delete_inst_enabled(inst, SCF_PG_GENERAL_OVR);
11517c478bd9Sstevel@tonic-gate }
11527c478bd9Sstevel@tonic-gate
11537c478bd9Sstevel@tonic-gate out:
11547c478bd9Sstevel@tonic-gate scf_instance_destroy(inst);
11557c478bd9Sstevel@tonic-gate scf_handle_destroy(h);
1156bf6e99ccSGary Mills if (ret == -1 && scf_error() == SCF_ERROR_DELETED)
1157bf6e99ccSGary Mills (void) scf_set_error(SCF_ERROR_NOT_FOUND);
11587c478bd9Sstevel@tonic-gate return (ret);
11597c478bd9Sstevel@tonic-gate }
11607c478bd9Sstevel@tonic-gate
1161d75e6a5dStn /*
1162d75e6a5dStn * Create and return a pg from the instance associated with the given handle.
1163d75e6a5dStn * This function is only called in scf_transaction_setup and
1164d75e6a5dStn * scf_transaction_restart where the h->rh_instance pointer is properly filled
1165d75e6a5dStn * in by scf_general_setup_pg().
1166d75e6a5dStn */
1167d75e6a5dStn static scf_propertygroup_t *
get_instance_pg(scf_simple_handle_t * simple_h)1168d75e6a5dStn get_instance_pg(scf_simple_handle_t *simple_h)
1169d75e6a5dStn {
1170d75e6a5dStn scf_propertygroup_t *ret_pg = scf_pg_create(simple_h->h);
1171d75e6a5dStn char *pg_name;
1172d75e6a5dStn ssize_t namelen;
1173d75e6a5dStn
1174d75e6a5dStn if (ret_pg == NULL) {
1175d75e6a5dStn return (NULL);
1176d75e6a5dStn }
1177d75e6a5dStn
11781f6eb021SLiane Praza namelen = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) + 1;
11791f6eb021SLiane Praza assert(namelen > 0);
1180d75e6a5dStn
1181d75e6a5dStn if ((pg_name = malloc(namelen)) == NULL) {
1182d75e6a5dStn if (scf_error() == SCF_ERROR_NOT_SET) {
1183d75e6a5dStn (void) scf_set_error(SCF_ERROR_NO_MEMORY);
1184d75e6a5dStn }
1185d75e6a5dStn return (NULL);
1186d75e6a5dStn }
1187d75e6a5dStn
1188d75e6a5dStn if (scf_pg_get_name(simple_h->running_pg, pg_name, namelen) < 0) {
1189d75e6a5dStn if (scf_error() == SCF_ERROR_NOT_SET) {
1190d75e6a5dStn (void) scf_set_error(SCF_ERROR_INTERNAL);
1191d75e6a5dStn }
1192d75e6a5dStn return (NULL);
1193d75e6a5dStn }
1194d75e6a5dStn
1195d75e6a5dStn /* Get pg from instance */
1196d75e6a5dStn if (scf_instance_get_pg(simple_h->inst, pg_name, ret_pg) == -1) {
1197d75e6a5dStn return (NULL);
1198d75e6a5dStn }
1199d75e6a5dStn
1200d75e6a5dStn return (ret_pg);
1201d75e6a5dStn }
1202d75e6a5dStn
12037c478bd9Sstevel@tonic-gate int
smf_enable_instance(const char * fmri,int flags)12047c478bd9Sstevel@tonic-gate smf_enable_instance(const char *fmri, int flags)
12057c478bd9Sstevel@tonic-gate {
1206*8fff7887SJohn Levon return (set_inst_enabled_flags(fmri, flags, B_TRUE, ""));
12077c478bd9Sstevel@tonic-gate }
12087c478bd9Sstevel@tonic-gate
12097c478bd9Sstevel@tonic-gate int
smf_disable_instance_with_comment(const char * fmri,int flags,const char * comment)1210*8fff7887SJohn Levon smf_disable_instance_with_comment(const char *fmri, int flags,
1211*8fff7887SJohn Levon const char *comment)
12127c478bd9Sstevel@tonic-gate {
1213*8fff7887SJohn Levon return (set_inst_enabled_flags(fmri, flags, B_FALSE, comment));
12147c478bd9Sstevel@tonic-gate }
12157c478bd9Sstevel@tonic-gate
1216*8fff7887SJohn Levon int
smf_disable_instance(const char * fmri,int flags)1217*8fff7887SJohn Levon smf_disable_instance(const char *fmri, int flags)
1218*8fff7887SJohn Levon {
1219*8fff7887SJohn Levon return (set_inst_enabled_flags(fmri, flags, B_FALSE, ""));
1220*8fff7887SJohn Levon }
12217c478bd9Sstevel@tonic-gate int
_smf_refresh_instance_i(scf_instance_t * inst)12227c478bd9Sstevel@tonic-gate _smf_refresh_instance_i(scf_instance_t *inst)
12237c478bd9Sstevel@tonic-gate {
12247c478bd9Sstevel@tonic-gate return (set_inst_action_inst(inst, SCF_PROPERTY_REFRESH));
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate
1227f6e214c7SGavin Maltby int
_smf_refresh_all_instances(scf_service_t * s)1228f6e214c7SGavin Maltby _smf_refresh_all_instances(scf_service_t *s)
1229f6e214c7SGavin Maltby {
1230f6e214c7SGavin Maltby scf_handle_t *h = scf_service_handle(s);
1231f6e214c7SGavin Maltby scf_instance_t *i = scf_instance_create(h);
1232f6e214c7SGavin Maltby scf_iter_t *it = scf_iter_create(h);
1233f6e214c7SGavin Maltby int err, r = -1;
1234f6e214c7SGavin Maltby
1235f6e214c7SGavin Maltby if (h == NULL || i == NULL || it == NULL)
1236f6e214c7SGavin Maltby goto error;
1237f6e214c7SGavin Maltby
1238f6e214c7SGavin Maltby if (scf_iter_service_instances(it, s) != 0)
1239f6e214c7SGavin Maltby goto error;
1240f6e214c7SGavin Maltby
1241f6e214c7SGavin Maltby while ((err = scf_iter_next_instance(it, i)) == 1)
1242f6e214c7SGavin Maltby if (_smf_refresh_instance_i(i) != 0)
1243f6e214c7SGavin Maltby goto error;
1244f6e214c7SGavin Maltby
1245f6e214c7SGavin Maltby if (err == -1)
1246f6e214c7SGavin Maltby goto error;
1247f6e214c7SGavin Maltby
1248f6e214c7SGavin Maltby r = 0;
1249f6e214c7SGavin Maltby error:
1250f6e214c7SGavin Maltby scf_instance_destroy(i);
1251f6e214c7SGavin Maltby scf_iter_destroy(it);
1252f6e214c7SGavin Maltby
1253f6e214c7SGavin Maltby return (r);
1254f6e214c7SGavin Maltby }
1255f6e214c7SGavin Maltby
12567c478bd9Sstevel@tonic-gate int
smf_refresh_instance(const char * instance)12577c478bd9Sstevel@tonic-gate smf_refresh_instance(const char *instance)
12587c478bd9Sstevel@tonic-gate {
12597c478bd9Sstevel@tonic-gate return (set_inst_action(instance, SCF_PROPERTY_REFRESH));
12607c478bd9Sstevel@tonic-gate }
12617c478bd9Sstevel@tonic-gate
12627c478bd9Sstevel@tonic-gate int
smf_restart_instance(const char * instance)12637c478bd9Sstevel@tonic-gate smf_restart_instance(const char *instance)
12647c478bd9Sstevel@tonic-gate {
12657c478bd9Sstevel@tonic-gate return (set_inst_action(instance, SCF_PROPERTY_RESTART));
12667c478bd9Sstevel@tonic-gate }
12677c478bd9Sstevel@tonic-gate
12687c478bd9Sstevel@tonic-gate int
smf_maintain_instance(const char * instance,int flags)12697c478bd9Sstevel@tonic-gate smf_maintain_instance(const char *instance, int flags)
12707c478bd9Sstevel@tonic-gate {
12717c478bd9Sstevel@tonic-gate if (flags & SMF_TEMPORARY)
12727c478bd9Sstevel@tonic-gate return (set_inst_action(instance,
12737c478bd9Sstevel@tonic-gate (flags & SMF_IMMEDIATE) ?
12747c478bd9Sstevel@tonic-gate SCF_PROPERTY_MAINT_ON_IMMTEMP :
12757c478bd9Sstevel@tonic-gate SCF_PROPERTY_MAINT_ON_TEMPORARY));
12767c478bd9Sstevel@tonic-gate else
12777c478bd9Sstevel@tonic-gate return (set_inst_action(instance,
12787c478bd9Sstevel@tonic-gate (flags & SMF_IMMEDIATE) ?
12797c478bd9Sstevel@tonic-gate SCF_PROPERTY_MAINT_ON_IMMEDIATE :
12807c478bd9Sstevel@tonic-gate SCF_PROPERTY_MAINT_ON));
12817c478bd9Sstevel@tonic-gate }
12827c478bd9Sstevel@tonic-gate
12837c478bd9Sstevel@tonic-gate int
smf_degrade_instance(const char * instance,int flags)12847c478bd9Sstevel@tonic-gate smf_degrade_instance(const char *instance, int flags)
12857c478bd9Sstevel@tonic-gate {
12867c478bd9Sstevel@tonic-gate scf_simple_prop_t *prop;
12877c478bd9Sstevel@tonic-gate const char *state_str;
12887c478bd9Sstevel@tonic-gate
12897c478bd9Sstevel@tonic-gate if (flags & SMF_TEMPORARY)
12907c478bd9Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
12917c478bd9Sstevel@tonic-gate
12927c478bd9Sstevel@tonic-gate if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
12937c478bd9Sstevel@tonic-gate SCF_PROPERTY_STATE)) == NULL)
12947c478bd9Sstevel@tonic-gate return (SCF_FAILED);
12957c478bd9Sstevel@tonic-gate
12967c478bd9Sstevel@tonic-gate if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
12977c478bd9Sstevel@tonic-gate scf_simple_prop_free(prop);
12987c478bd9Sstevel@tonic-gate return (SCF_FAILED);
12997c478bd9Sstevel@tonic-gate }
13007c478bd9Sstevel@tonic-gate
13017c478bd9Sstevel@tonic-gate if (strcmp(state_str, SCF_STATE_STRING_ONLINE) != 0) {
13027c478bd9Sstevel@tonic-gate scf_simple_prop_free(prop);
13037c478bd9Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED));
13047c478bd9Sstevel@tonic-gate }
13057c478bd9Sstevel@tonic-gate scf_simple_prop_free(prop);
13067c478bd9Sstevel@tonic-gate
13077c478bd9Sstevel@tonic-gate return (set_inst_action(instance, (flags & SMF_IMMEDIATE) ?
13087c478bd9Sstevel@tonic-gate SCF_PROPERTY_DEGRADE_IMMEDIATE : SCF_PROPERTY_DEGRADED));
13097c478bd9Sstevel@tonic-gate }
13107c478bd9Sstevel@tonic-gate
13117c478bd9Sstevel@tonic-gate int
smf_restore_instance(const char * instance)13127c478bd9Sstevel@tonic-gate smf_restore_instance(const char *instance)
13137c478bd9Sstevel@tonic-gate {
13147c478bd9Sstevel@tonic-gate scf_simple_prop_t *prop;
13157c478bd9Sstevel@tonic-gate const char *state_str;
13167c478bd9Sstevel@tonic-gate int ret;
13177c478bd9Sstevel@tonic-gate
13187c478bd9Sstevel@tonic-gate if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
13197c478bd9Sstevel@tonic-gate SCF_PROPERTY_STATE)) == NULL)
13207c478bd9Sstevel@tonic-gate return (SCF_FAILED);
13217c478bd9Sstevel@tonic-gate
13227c478bd9Sstevel@tonic-gate if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
13237c478bd9Sstevel@tonic-gate scf_simple_prop_free(prop);
13247c478bd9Sstevel@tonic-gate return (SCF_FAILED);
13257c478bd9Sstevel@tonic-gate }
13267c478bd9Sstevel@tonic-gate
13277c478bd9Sstevel@tonic-gate if (strcmp(state_str, SCF_STATE_STRING_MAINT) == 0) {
13287c478bd9Sstevel@tonic-gate ret = set_inst_action(instance, SCF_PROPERTY_MAINT_OFF);
13297c478bd9Sstevel@tonic-gate } else if (strcmp(state_str, SCF_STATE_STRING_DEGRADED) == 0) {
13307c478bd9Sstevel@tonic-gate ret = set_inst_action(instance, SCF_PROPERTY_RESTORE);
13317c478bd9Sstevel@tonic-gate } else {
13327c478bd9Sstevel@tonic-gate ret = scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED);
13337c478bd9Sstevel@tonic-gate }
13347c478bd9Sstevel@tonic-gate
13357c478bd9Sstevel@tonic-gate scf_simple_prop_free(prop);
13367c478bd9Sstevel@tonic-gate return (ret);
13377c478bd9Sstevel@tonic-gate }
13387c478bd9Sstevel@tonic-gate
13397c478bd9Sstevel@tonic-gate char *
smf_get_state(const char * instance)13407c478bd9Sstevel@tonic-gate smf_get_state(const char *instance)
13417c478bd9Sstevel@tonic-gate {
13427c478bd9Sstevel@tonic-gate scf_simple_prop_t *prop;
13437c478bd9Sstevel@tonic-gate const char *state_str;
13447c478bd9Sstevel@tonic-gate char *ret;
13457c478bd9Sstevel@tonic-gate
13467c478bd9Sstevel@tonic-gate if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
13477c478bd9Sstevel@tonic-gate SCF_PROPERTY_STATE)) == NULL)
13487c478bd9Sstevel@tonic-gate return (NULL);
13497c478bd9Sstevel@tonic-gate
13507c478bd9Sstevel@tonic-gate if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
13517c478bd9Sstevel@tonic-gate scf_simple_prop_free(prop);
13527c478bd9Sstevel@tonic-gate return (NULL);
13537c478bd9Sstevel@tonic-gate }
13547c478bd9Sstevel@tonic-gate
13557c478bd9Sstevel@tonic-gate if ((ret = strdup(state_str)) == NULL)
13567c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY);
13577c478bd9Sstevel@tonic-gate
13587c478bd9Sstevel@tonic-gate scf_simple_prop_free(prop);
13597c478bd9Sstevel@tonic-gate return (ret);
13607c478bd9Sstevel@tonic-gate }
13617c478bd9Sstevel@tonic-gate
1362d75e6a5dStn /*
1363d75e6a5dStn * scf_general_pg_setup(fmri, pg_name)
1364d75e6a5dStn * Create a scf_simple_handle_t and fill in the instance, snapshot, and
1365d75e6a5dStn * property group fields associated with the given fmri and property group
1366d75e6a5dStn * name.
1367d75e6a5dStn * Returns:
1368d75e6a5dStn * Handle on success
1369d75e6a5dStn * Null on error with scf_error set to:
1370d75e6a5dStn * SCF_ERROR_HANDLE_MISMATCH,
1371d75e6a5dStn * SCF_ERROR_INVALID_ARGUMENT,
1372d75e6a5dStn * SCF_ERROR_CONSTRAINT_VIOLATED,
1373d75e6a5dStn * SCF_ERROR_NOT_FOUND,
1374d75e6a5dStn * SCF_ERROR_NOT_SET,
1375d75e6a5dStn * SCF_ERROR_DELETED,
1376d75e6a5dStn * SCF_ERROR_NOT_BOUND,
1377d75e6a5dStn * SCF_ERROR_CONNECTION_BROKEN,
1378d75e6a5dStn * SCF_ERROR_INTERNAL,
1379d75e6a5dStn * SCF_ERROR_NO_RESOURCES,
1380d75e6a5dStn * SCF_ERROR_BACKEND_ACCESS
1381d75e6a5dStn */
1382d75e6a5dStn scf_simple_handle_t *
scf_general_pg_setup(const char * fmri,const char * pg_name)1383d75e6a5dStn scf_general_pg_setup(const char *fmri, const char *pg_name)
1384d75e6a5dStn {
1385d75e6a5dStn scf_simple_handle_t *ret;
1386d75e6a5dStn
1387d75e6a5dStn ret = uu_zalloc(sizeof (*ret));
1388d75e6a5dStn if (ret == NULL) {
1389d75e6a5dStn (void) scf_set_error(SCF_ERROR_NO_MEMORY);
1390d75e6a5dStn return (NULL);
1391d75e6a5dStn } else {
1392d75e6a5dStn
1393f6e214c7SGavin Maltby ret->h = _scf_handle_create_and_bind(SCF_VERSION);
1394d75e6a5dStn ret->inst = scf_instance_create(ret->h);
1395d75e6a5dStn ret->snap = scf_snapshot_create(ret->h);
1396d75e6a5dStn ret->running_pg = scf_pg_create(ret->h);
1397d75e6a5dStn }
1398d75e6a5dStn
1399d75e6a5dStn if ((ret->h == NULL) || (ret->inst == NULL) ||
1400d75e6a5dStn (ret->snap == NULL) || (ret->running_pg == NULL)) {
1401d75e6a5dStn goto out;
1402d75e6a5dStn }
1403d75e6a5dStn
1404d75e6a5dStn if (scf_handle_decode_fmri(ret->h, fmri, NULL, NULL, ret->inst,
14054a014300SToomas Soome NULL, NULL, 0) == -1) {
1406d75e6a5dStn goto out;
1407d75e6a5dStn }
1408d75e6a5dStn
1409d75e6a5dStn if ((scf_instance_get_snapshot(ret->inst, "running", ret->snap))
1410d75e6a5dStn != 0) {
1411d75e6a5dStn goto out;
1412d75e6a5dStn }
1413d75e6a5dStn
1414d75e6a5dStn if (scf_instance_get_pg_composed(ret->inst, ret->snap, pg_name,
1415d75e6a5dStn ret->running_pg) != 0) {
1416d75e6a5dStn goto out;
1417d75e6a5dStn }
1418d75e6a5dStn
1419d75e6a5dStn return (ret);
1420d75e6a5dStn
1421d75e6a5dStn out:
1422d75e6a5dStn scf_simple_handle_destroy(ret);
1423d75e6a5dStn return (NULL);
1424d75e6a5dStn }
1425d75e6a5dStn
1426d75e6a5dStn /*
1427d75e6a5dStn * scf_transaction_setup(h)
1428d75e6a5dStn * creates and starts the transaction
1429d75e6a5dStn * Returns:
1430d75e6a5dStn * transaction on success
1431d75e6a5dStn * NULL on failure with scf_error set to:
1432d75e6a5dStn * SCF_ERROR_NO_MEMORY,
1433d75e6a5dStn * SCF_ERROR_INVALID_ARGUMENT,
1434d75e6a5dStn * SCF_ERROR_HANDLE_DESTROYED,
1435d75e6a5dStn * SCF_ERROR_INTERNAL,
1436d75e6a5dStn * SCF_ERROR_NO_RESOURCES,
1437d75e6a5dStn * SCF_ERROR_NOT_BOUND,
1438d75e6a5dStn * SCF_ERROR_CONNECTION_BROKEN,
1439d75e6a5dStn * SCF_ERROR_NOT_SET,
1440d75e6a5dStn * SCF_ERROR_DELETED,
1441d75e6a5dStn * SCF_ERROR_CONSTRAINT_VIOLATED,
1442d75e6a5dStn * SCF_ERROR_HANDLE_MISMATCH,
1443d75e6a5dStn * SCF_ERROR_BACKEND_ACCESS,
1444d75e6a5dStn * SCF_ERROR_IN_USE
1445d75e6a5dStn */
1446d75e6a5dStn scf_transaction_t *
scf_transaction_setup(scf_simple_handle_t * simple_h)1447d75e6a5dStn scf_transaction_setup(scf_simple_handle_t *simple_h)
1448d75e6a5dStn {
1449d75e6a5dStn scf_transaction_t *tx = NULL;
1450d75e6a5dStn
1451d75e6a5dStn if ((tx = scf_transaction_create(simple_h->h)) == NULL) {
1452d75e6a5dStn return (NULL);
1453d75e6a5dStn }
1454d75e6a5dStn
1455d75e6a5dStn if ((simple_h->editing_pg = get_instance_pg(simple_h)) == NULL) {
1456d75e6a5dStn return (NULL);
1457d75e6a5dStn }
1458d75e6a5dStn
1459d75e6a5dStn if (scf_transaction_start(tx, simple_h->editing_pg) == -1) {
1460d75e6a5dStn scf_pg_destroy(simple_h->editing_pg);
1461d75e6a5dStn simple_h->editing_pg = NULL;
1462d75e6a5dStn return (NULL);
1463d75e6a5dStn }
1464d75e6a5dStn
1465d75e6a5dStn return (tx);
1466d75e6a5dStn }
1467d75e6a5dStn
1468d75e6a5dStn int
scf_transaction_restart(scf_simple_handle_t * simple_h,scf_transaction_t * tx)1469d75e6a5dStn scf_transaction_restart(scf_simple_handle_t *simple_h, scf_transaction_t *tx)
1470d75e6a5dStn {
1471d75e6a5dStn scf_transaction_reset(tx);
1472d75e6a5dStn
1473d75e6a5dStn if (scf_pg_update(simple_h->editing_pg) == -1) {
1474d75e6a5dStn return (SCF_FAILED);
1475d75e6a5dStn }
1476d75e6a5dStn
1477d75e6a5dStn if (scf_transaction_start(tx, simple_h->editing_pg) == -1) {
1478d75e6a5dStn return (SCF_FAILED);
1479d75e6a5dStn }
1480d75e6a5dStn
1481d75e6a5dStn return (SCF_SUCCESS);
1482d75e6a5dStn }
1483d75e6a5dStn
1484d75e6a5dStn /*
1485d75e6a5dStn * scf_read_count_property(scf_simple_handle_t *simple_h, char *prop_name,
1486d75e6a5dStn * uint64_t *ret_count)
1487d75e6a5dStn *
1488d75e6a5dStn * For the given property name, return the count value.
1489d75e6a5dStn * RETURNS:
1490d75e6a5dStn * SCF_SUCCESS
1491d75e6a5dStn * SCF_FAILED on failure with scf_error() set to:
1492d75e6a5dStn * SCF_ERROR_HANDLE_DESTROYED
1493d75e6a5dStn * SCF_ERROR_INTERNAL
1494d75e6a5dStn * SCF_ERROR_NO_RESOURCES
1495d75e6a5dStn * SCF_ERROR_NO_MEMORY
1496d75e6a5dStn * SCF_ERROR_HANDLE_MISMATCH
1497d75e6a5dStn * SCF_ERROR_INVALID_ARGUMENT
1498d75e6a5dStn * SCF_ERROR_NOT_BOUND
1499d75e6a5dStn * SCF_ERROR_CONNECTION_BROKEN
1500d75e6a5dStn * SCF_ERROR_NOT_SET
1501d75e6a5dStn * SCF_ERROR_DELETED
1502d75e6a5dStn * SCF_ERROR_BACKEND_ACCESS
1503d75e6a5dStn * SCF_ERROR_CONSTRAINT_VIOLATED
1504d75e6a5dStn * SCF_ERROR_TYPE_MISMATCH
1505d75e6a5dStn */
1506d75e6a5dStn int
scf_read_count_property(scf_simple_handle_t * simple_h,char * prop_name,uint64_t * ret_count)1507d75e6a5dStn scf_read_count_property(
1508d75e6a5dStn scf_simple_handle_t *simple_h,
1509d75e6a5dStn char *prop_name,
1510d75e6a5dStn uint64_t *ret_count)
1511d75e6a5dStn {
1512d75e6a5dStn scf_property_t *prop = scf_property_create(simple_h->h);
1513d75e6a5dStn scf_value_t *val = scf_value_create(simple_h->h);
1514d75e6a5dStn int ret = SCF_FAILED;
1515d75e6a5dStn
1516d75e6a5dStn if ((val == NULL) || (prop == NULL)) {
1517d75e6a5dStn goto out;
1518d75e6a5dStn }
1519d75e6a5dStn
1520d75e6a5dStn /*
1521d75e6a5dStn * Get the property struct that goes with this property group and
1522d75e6a5dStn * property name.
1523d75e6a5dStn */
1524d75e6a5dStn if (scf_pg_get_property(simple_h->running_pg, prop_name, prop) != 0) {
1525d75e6a5dStn goto out;
1526d75e6a5dStn }
1527d75e6a5dStn
1528d75e6a5dStn /* Get the value structure */
1529d75e6a5dStn if (scf_property_get_value(prop, val) == -1) {
1530d75e6a5dStn goto out;
1531d75e6a5dStn }
1532d75e6a5dStn
1533d75e6a5dStn /*
1534d75e6a5dStn * Now get the count value.
1535d75e6a5dStn */
1536d75e6a5dStn if (scf_value_get_count(val, ret_count) == -1) {
1537d75e6a5dStn goto out;
1538d75e6a5dStn }
1539d75e6a5dStn
1540d75e6a5dStn ret = SCF_SUCCESS;
1541d75e6a5dStn
1542d75e6a5dStn out:
1543d75e6a5dStn scf_property_destroy(prop);
1544d75e6a5dStn scf_value_destroy(val);
1545d75e6a5dStn return (ret);
1546d75e6a5dStn }
1547d75e6a5dStn
1548d75e6a5dStn /*
1549d75e6a5dStn * scf_trans_add_count_property(trans, propname, count, create_flag)
1550d75e6a5dStn *
1551d75e6a5dStn * Set a count property transaction entry into the pending SMF transaction.
1552d75e6a5dStn * The transaction is created and committed outside of this function.
1553d75e6a5dStn * Returns:
1554d75e6a5dStn * SCF_SUCCESS
1555d75e6a5dStn * SCF_FAILED on failure with scf_error() set to:
1556d75e6a5dStn * SCF_ERROR_HANDLE_DESTROYED,
1557d75e6a5dStn * SCF_ERROR_INVALID_ARGUMENT,
1558d75e6a5dStn * SCF_ERROR_NO_MEMORY,
1559d75e6a5dStn * SCF_ERROR_HANDLE_MISMATCH,
1560d75e6a5dStn * SCF_ERROR_NOT_SET,
1561d75e6a5dStn * SCF_ERROR_IN_USE,
1562d75e6a5dStn * SCF_ERROR_NOT_FOUND,
1563d75e6a5dStn * SCF_ERROR_EXISTS,
1564d75e6a5dStn * SCF_ERROR_TYPE_MISMATCH,
1565d75e6a5dStn * SCF_ERROR_NOT_BOUND,
1566d75e6a5dStn * SCF_ERROR_CONNECTION_BROKEN,
1567d75e6a5dStn * SCF_ERROR_INTERNAL,
1568d75e6a5dStn * SCF_ERROR_DELETED,
1569d75e6a5dStn * SCF_ERROR_NO_RESOURCES,
1570d75e6a5dStn * SCF_ERROR_BACKEND_ACCESS
1571d75e6a5dStn */
1572d75e6a5dStn int
scf_set_count_property(scf_transaction_t * trans,char * propname,uint64_t count,boolean_t create_flag)1573d75e6a5dStn scf_set_count_property(
1574d75e6a5dStn scf_transaction_t *trans,
1575d75e6a5dStn char *propname,
1576d75e6a5dStn uint64_t count,
1577d75e6a5dStn boolean_t create_flag)
1578d75e6a5dStn {
1579d75e6a5dStn scf_handle_t *handle = scf_transaction_handle(trans);
1580d75e6a5dStn scf_value_t *value = scf_value_create(handle);
1581d75e6a5dStn scf_transaction_entry_t *entry = scf_entry_create(handle);
1582d75e6a5dStn
1583d75e6a5dStn if ((value == NULL) || (entry == NULL)) {
1584d75e6a5dStn return (SCF_FAILED);
1585d75e6a5dStn }
1586d75e6a5dStn
1587d75e6a5dStn /*
1588d75e6a5dStn * Property must be set in transaction and won't take
1589d75e6a5dStn * effect until the transaction is committed.
1590d75e6a5dStn *
1591d75e6a5dStn * Attempt to change the current value. However, create new property
1592d75e6a5dStn * if it doesn't exist and the create flag is set.
1593d75e6a5dStn */
1594d75e6a5dStn if (scf_transaction_property_change(trans, entry, propname,
1595d75e6a5dStn SCF_TYPE_COUNT) == 0) {
1596d75e6a5dStn scf_value_set_count(value, count);
1597d75e6a5dStn if (scf_entry_add_value(entry, value) == 0) {
1598d75e6a5dStn return (SCF_SUCCESS);
1599d75e6a5dStn }
1600d75e6a5dStn } else {
1601d75e6a5dStn if ((create_flag == B_TRUE) &&
1602d75e6a5dStn (scf_error() == SCF_ERROR_NOT_FOUND)) {
1603d75e6a5dStn if (scf_transaction_property_new(trans, entry, propname,
1604d75e6a5dStn SCF_TYPE_COUNT) == 0) {
1605d75e6a5dStn scf_value_set_count(value, count);
1606d75e6a5dStn if (scf_entry_add_value(entry, value) == 0) {
1607d75e6a5dStn return (SCF_SUCCESS);
1608d75e6a5dStn }
1609d75e6a5dStn }
1610d75e6a5dStn }
1611d75e6a5dStn }
1612d75e6a5dStn
1613d75e6a5dStn /*
1614d75e6a5dStn * cleanup if there were any errors that didn't leave these
1615d75e6a5dStn * values where they would be cleaned up later.
1616d75e6a5dStn */
1617d75e6a5dStn if (value != NULL)
1618d75e6a5dStn scf_value_destroy(value);
1619d75e6a5dStn if (entry != NULL)
1620d75e6a5dStn scf_entry_destroy(entry);
1621d75e6a5dStn return (SCF_FAILED);
1622d75e6a5dStn }
1623d75e6a5dStn
16247c478bd9Sstevel@tonic-gate int
scf_simple_walk_instances(uint_t state_flags,void * private,int (* inst_callback)(scf_handle_t *,scf_instance_t *,void *))16257c478bd9Sstevel@tonic-gate scf_simple_walk_instances(uint_t state_flags, void *private,
16267c478bd9Sstevel@tonic-gate int (*inst_callback)(scf_handle_t *, scf_instance_t *, void *))
16277c478bd9Sstevel@tonic-gate {
16284a014300SToomas Soome scf_scope_t *scope = NULL;
16297c478bd9Sstevel@tonic-gate scf_service_t *svc = NULL;
16307c478bd9Sstevel@tonic-gate scf_instance_t *inst = NULL;
16317c478bd9Sstevel@tonic-gate scf_iter_t *svc_iter = NULL, *inst_iter = NULL;
16327c478bd9Sstevel@tonic-gate scf_handle_t *h = NULL;
16337c478bd9Sstevel@tonic-gate int ret = SCF_FAILED;
16347c478bd9Sstevel@tonic-gate int svc_iter_ret, inst_iter_ret;
16357c478bd9Sstevel@tonic-gate int inst_state;
16367c478bd9Sstevel@tonic-gate
1637f6e214c7SGavin Maltby if ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL)
16387c478bd9Sstevel@tonic-gate return (ret);
16397c478bd9Sstevel@tonic-gate
16407c478bd9Sstevel@tonic-gate if (((scope = scf_scope_create(h)) == NULL) ||
16417c478bd9Sstevel@tonic-gate ((svc = scf_service_create(h)) == NULL) ||
16427c478bd9Sstevel@tonic-gate ((inst = scf_instance_create(h)) == NULL) ||
16437c478bd9Sstevel@tonic-gate ((svc_iter = scf_iter_create(h)) == NULL) ||
16447c478bd9Sstevel@tonic-gate ((inst_iter = scf_iter_create(h)) == NULL))
16457c478bd9Sstevel@tonic-gate goto out;
16467c478bd9Sstevel@tonic-gate
16477c478bd9Sstevel@tonic-gate /*
16487c478bd9Sstevel@tonic-gate * Get the local scope, and set up nested iteration through every
16497c478bd9Sstevel@tonic-gate * local service, and every instance of every service.
16507c478bd9Sstevel@tonic-gate */
16517c478bd9Sstevel@tonic-gate
16527c478bd9Sstevel@tonic-gate if ((scf_handle_get_local_scope(h, scope) != SCF_SUCCESS) ||
16537c478bd9Sstevel@tonic-gate (scf_iter_scope_services(svc_iter, scope) != SCF_SUCCESS))
16547c478bd9Sstevel@tonic-gate goto out;
16557c478bd9Sstevel@tonic-gate
16567c478bd9Sstevel@tonic-gate while ((svc_iter_ret = scf_iter_next_service(svc_iter, svc)) > 0) {
16577c478bd9Sstevel@tonic-gate
16587c478bd9Sstevel@tonic-gate if ((scf_iter_service_instances(inst_iter, svc)) !=
16597c478bd9Sstevel@tonic-gate SCF_SUCCESS)
16607c478bd9Sstevel@tonic-gate goto out;
16617c478bd9Sstevel@tonic-gate
16627c478bd9Sstevel@tonic-gate while ((inst_iter_ret =
16637c478bd9Sstevel@tonic-gate scf_iter_next_instance(inst_iter, inst)) > 0) {
16647c478bd9Sstevel@tonic-gate /*
16657c478bd9Sstevel@tonic-gate * If get_inst_state fails from an internal error,
16667c478bd9Sstevel@tonic-gate * IE, being unable to get the property group or
16677c478bd9Sstevel@tonic-gate * property containing the state of the instance,
16687c478bd9Sstevel@tonic-gate * we continue instead of failing, as this might just
16697c478bd9Sstevel@tonic-gate * be an improperly configured instance.
16707c478bd9Sstevel@tonic-gate */
16717c478bd9Sstevel@tonic-gate if ((inst_state = get_inst_state(inst, h)) == -1) {
16727c478bd9Sstevel@tonic-gate if (scf_error() == SCF_ERROR_INTERNAL) {
16737c478bd9Sstevel@tonic-gate continue;
16747c478bd9Sstevel@tonic-gate } else {
16757c478bd9Sstevel@tonic-gate goto out;
16767c478bd9Sstevel@tonic-gate }
16777c478bd9Sstevel@tonic-gate }
16787c478bd9Sstevel@tonic-gate
16797c478bd9Sstevel@tonic-gate if ((uint_t)inst_state & state_flags) {
16807c478bd9Sstevel@tonic-gate if (inst_callback(h, inst, private) !=
16817c478bd9Sstevel@tonic-gate SCF_SUCCESS) {
16827c478bd9Sstevel@tonic-gate (void) scf_set_error(
16837c478bd9Sstevel@tonic-gate SCF_ERROR_CALLBACK_FAILED);
16847c478bd9Sstevel@tonic-gate goto out;
16857c478bd9Sstevel@tonic-gate }
16867c478bd9Sstevel@tonic-gate }
16877c478bd9Sstevel@tonic-gate }
16887c478bd9Sstevel@tonic-gate
16897c478bd9Sstevel@tonic-gate if (inst_iter_ret == -1)
16907c478bd9Sstevel@tonic-gate goto out;
16917c478bd9Sstevel@tonic-gate scf_iter_reset(inst_iter);
16927c478bd9Sstevel@tonic-gate }
16937c478bd9Sstevel@tonic-gate
16947c478bd9Sstevel@tonic-gate if (svc_iter_ret != -1)
16957c478bd9Sstevel@tonic-gate ret = SCF_SUCCESS;
16967c478bd9Sstevel@tonic-gate
16977c478bd9Sstevel@tonic-gate out:
16987c478bd9Sstevel@tonic-gate scf_scope_destroy(scope);
16997c478bd9Sstevel@tonic-gate scf_service_destroy(svc);
17007c478bd9Sstevel@tonic-gate scf_instance_destroy(inst);
17017c478bd9Sstevel@tonic-gate scf_iter_destroy(svc_iter);
17027c478bd9Sstevel@tonic-gate scf_iter_destroy(inst_iter);
17037c478bd9Sstevel@tonic-gate scf_handle_destroy(h);
17047c478bd9Sstevel@tonic-gate
17057c478bd9Sstevel@tonic-gate return (ret);
17067c478bd9Sstevel@tonic-gate }
17077c478bd9Sstevel@tonic-gate
17087c478bd9Sstevel@tonic-gate
17097c478bd9Sstevel@tonic-gate scf_simple_prop_t *
scf_simple_prop_get(scf_handle_t * hin,const char * instance,const char * pgname,const char * propname)17107c478bd9Sstevel@tonic-gate scf_simple_prop_get(scf_handle_t *hin, const char *instance, const char *pgname,
17114a014300SToomas Soome const char *propname)
17127c478bd9Sstevel@tonic-gate {
17134a014300SToomas Soome char *fmri_buf, *svcfmri = NULL;
17144a014300SToomas Soome ssize_t fmri_sz;
17154a014300SToomas Soome scf_property_t *prop = NULL;
17164a014300SToomas Soome scf_service_t *svc = NULL;
17174a014300SToomas Soome scf_simple_prop_t *ret;
17187c478bd9Sstevel@tonic-gate scf_handle_t *h = NULL;
17197c478bd9Sstevel@tonic-gate boolean_t local_h = B_TRUE;
17207c478bd9Sstevel@tonic-gate
17217c478bd9Sstevel@tonic-gate /* If the user passed in a handle, use it. */
17227c478bd9Sstevel@tonic-gate if (hin != NULL) {
17237c478bd9Sstevel@tonic-gate h = hin;
17247c478bd9Sstevel@tonic-gate local_h = B_FALSE;
17257c478bd9Sstevel@tonic-gate }
17267c478bd9Sstevel@tonic-gate
1727f6e214c7SGavin Maltby if (local_h && ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL))
17287c478bd9Sstevel@tonic-gate return (NULL);
17297c478bd9Sstevel@tonic-gate
17307c478bd9Sstevel@tonic-gate if ((fmri_buf = assemble_fmri(h, instance, pgname, propname)) == NULL) {
17317c478bd9Sstevel@tonic-gate if (local_h)
17327c478bd9Sstevel@tonic-gate scf_handle_destroy(h);
17337c478bd9Sstevel@tonic-gate return (NULL);
17347c478bd9Sstevel@tonic-gate }
17357c478bd9Sstevel@tonic-gate
17367c478bd9Sstevel@tonic-gate if ((svc = scf_service_create(h)) == NULL ||
17377c478bd9Sstevel@tonic-gate (prop = scf_property_create(h)) == NULL)
17387c478bd9Sstevel@tonic-gate goto error1;
17397c478bd9Sstevel@tonic-gate if (scf_handle_decode_fmri(h, fmri_buf, NULL, NULL, NULL, NULL, prop,
17407c478bd9Sstevel@tonic-gate SCF_DECODE_FMRI_REQUIRE_INSTANCE) == -1) {
17417c478bd9Sstevel@tonic-gate switch (scf_error()) {
17427c478bd9Sstevel@tonic-gate /*
17437c478bd9Sstevel@tonic-gate * If the property isn't found in the instance, we grab the
17447c478bd9Sstevel@tonic-gate * underlying service, create an FMRI out of it, and then
17457c478bd9Sstevel@tonic-gate * query the datastore again at the service level for the
17467c478bd9Sstevel@tonic-gate * property.
17477c478bd9Sstevel@tonic-gate */
17487c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
17497c478bd9Sstevel@tonic-gate if (scf_handle_decode_fmri(h, fmri_buf, NULL, svc,
17507c478bd9Sstevel@tonic-gate NULL, NULL, NULL, SCF_DECODE_FMRI_TRUNCATE) == -1)
17517c478bd9Sstevel@tonic-gate goto error1;
17521f6eb021SLiane Praza
17531f6eb021SLiane Praza fmri_sz = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH) + 1;
17541f6eb021SLiane Praza assert(fmri_sz > 0);
17551f6eb021SLiane Praza
17567c478bd9Sstevel@tonic-gate if (scf_service_to_fmri(svc, fmri_buf, fmri_sz) == -1)
17577c478bd9Sstevel@tonic-gate goto error1;
17587c478bd9Sstevel@tonic-gate if ((svcfmri = assemble_fmri(h, fmri_buf, pgname,
17597c478bd9Sstevel@tonic-gate propname)) == NULL)
17607c478bd9Sstevel@tonic-gate goto error1;
17617c478bd9Sstevel@tonic-gate if (scf_handle_decode_fmri(h, svcfmri, NULL, NULL,
17627c478bd9Sstevel@tonic-gate NULL, NULL, prop, 0) == -1) {
17637c478bd9Sstevel@tonic-gate free(svcfmri);
17647c478bd9Sstevel@tonic-gate goto error1;
17657c478bd9Sstevel@tonic-gate }
17667c478bd9Sstevel@tonic-gate free(svcfmri);
17677c478bd9Sstevel@tonic-gate break;
17687c478bd9Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED:
17697c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
17707c478bd9Sstevel@tonic-gate default:
17717c478bd9Sstevel@tonic-gate goto error1;
17727c478bd9Sstevel@tonic-gate }
17737c478bd9Sstevel@tonic-gate }
17747c478bd9Sstevel@tonic-gate /*
17757c478bd9Sstevel@tonic-gate * At this point, we've successfully pulled the property from the
17767c478bd9Sstevel@tonic-gate * datastore, and simply need to copy its innards into an
17777c478bd9Sstevel@tonic-gate * scf_simple_prop_t.
17787c478bd9Sstevel@tonic-gate */
17797c478bd9Sstevel@tonic-gate if ((ret = fill_prop(prop, pgname, propname, h)) == NULL)
17807c478bd9Sstevel@tonic-gate goto error1;
17817c478bd9Sstevel@tonic-gate
17827c478bd9Sstevel@tonic-gate scf_service_destroy(svc);
17837c478bd9Sstevel@tonic-gate scf_property_destroy(prop);
17847c478bd9Sstevel@tonic-gate free(fmri_buf);
17857c478bd9Sstevel@tonic-gate if (local_h)
17867c478bd9Sstevel@tonic-gate scf_handle_destroy(h);
17877c478bd9Sstevel@tonic-gate return (ret);
17887c478bd9Sstevel@tonic-gate
17897c478bd9Sstevel@tonic-gate /*
17907c478bd9Sstevel@tonic-gate * Exit point for a successful call. Below this line are exit points
17917c478bd9Sstevel@tonic-gate * for failures at various stages during the function.
17927c478bd9Sstevel@tonic-gate */
17937c478bd9Sstevel@tonic-gate
17947c478bd9Sstevel@tonic-gate error1:
17957c478bd9Sstevel@tonic-gate scf_service_destroy(svc);
17967c478bd9Sstevel@tonic-gate scf_property_destroy(prop);
17977c478bd9Sstevel@tonic-gate error2:
17987c478bd9Sstevel@tonic-gate free(fmri_buf);
17997c478bd9Sstevel@tonic-gate if (local_h)
18007c478bd9Sstevel@tonic-gate scf_handle_destroy(h);
18017c478bd9Sstevel@tonic-gate return (NULL);
18027c478bd9Sstevel@tonic-gate }
18037c478bd9Sstevel@tonic-gate
18047c478bd9Sstevel@tonic-gate
18057c478bd9Sstevel@tonic-gate void
scf_simple_prop_free(scf_simple_prop_t * prop)18067c478bd9Sstevel@tonic-gate scf_simple_prop_free(scf_simple_prop_t *prop)
18077c478bd9Sstevel@tonic-gate {
18087c478bd9Sstevel@tonic-gate int i;
18097c478bd9Sstevel@tonic-gate
18107c478bd9Sstevel@tonic-gate if (prop == NULL)
18117c478bd9Sstevel@tonic-gate return;
18127c478bd9Sstevel@tonic-gate
18137c478bd9Sstevel@tonic-gate free(prop->pr_propname);
18147c478bd9Sstevel@tonic-gate free(prop->pr_pgname);
18157c478bd9Sstevel@tonic-gate switch (prop->pr_type) {
18167c478bd9Sstevel@tonic-gate case SCF_TYPE_OPAQUE: {
18177c478bd9Sstevel@tonic-gate for (i = 0; i < prop->pr_numvalues; i++) {
18187c478bd9Sstevel@tonic-gate free(prop->pr_vallist[i].pv_opaque.o_value);
18197c478bd9Sstevel@tonic-gate }
18207c478bd9Sstevel@tonic-gate break;
18217c478bd9Sstevel@tonic-gate }
18227c478bd9Sstevel@tonic-gate case SCF_TYPE_ASTRING:
18237c478bd9Sstevel@tonic-gate case SCF_TYPE_USTRING:
18247c478bd9Sstevel@tonic-gate case SCF_TYPE_HOST:
18257c478bd9Sstevel@tonic-gate case SCF_TYPE_HOSTNAME:
1826b56bf881SAntonello Cruz case SCF_TYPE_NET_ADDR:
18277c478bd9Sstevel@tonic-gate case SCF_TYPE_NET_ADDR_V4:
18287c478bd9Sstevel@tonic-gate case SCF_TYPE_NET_ADDR_V6:
18297c478bd9Sstevel@tonic-gate case SCF_TYPE_URI:
18307c478bd9Sstevel@tonic-gate case SCF_TYPE_FMRI: {
18317c478bd9Sstevel@tonic-gate for (i = 0; i < prop->pr_numvalues; i++) {
18327c478bd9Sstevel@tonic-gate free(prop->pr_vallist[i].pv_str);
18337c478bd9Sstevel@tonic-gate }
18347c478bd9Sstevel@tonic-gate break;
18357c478bd9Sstevel@tonic-gate }
18367c478bd9Sstevel@tonic-gate default:
18377c478bd9Sstevel@tonic-gate break;
18387c478bd9Sstevel@tonic-gate }
18397c478bd9Sstevel@tonic-gate free(prop->pr_vallist);
18407c478bd9Sstevel@tonic-gate free(prop);
18417c478bd9Sstevel@tonic-gate }
18427c478bd9Sstevel@tonic-gate
18437c478bd9Sstevel@tonic-gate
18447c478bd9Sstevel@tonic-gate scf_simple_app_props_t *
scf_simple_app_props_get(scf_handle_t * hin,const char * inst_fmri)18457c478bd9Sstevel@tonic-gate scf_simple_app_props_get(scf_handle_t *hin, const char *inst_fmri)
18467c478bd9Sstevel@tonic-gate {
18474a014300SToomas Soome scf_instance_t *inst = NULL;
18484a014300SToomas Soome scf_service_t *svc = NULL;
18494a014300SToomas Soome scf_propertygroup_t *pg = NULL;
18504a014300SToomas Soome scf_property_t *prop = NULL;
18517c478bd9Sstevel@tonic-gate scf_simple_app_props_t *ret = NULL;
18527c478bd9Sstevel@tonic-gate scf_iter_t *pgiter = NULL, *propiter = NULL;
18537c478bd9Sstevel@tonic-gate struct scf_simple_pg *thispg = NULL, *nextpg;
18547c478bd9Sstevel@tonic-gate scf_simple_prop_t *thisprop, *nextprop;
18557c478bd9Sstevel@tonic-gate scf_handle_t *h = NULL;
18567c478bd9Sstevel@tonic-gate int pgiter_ret, propiter_ret;
18577c478bd9Sstevel@tonic-gate ssize_t namelen;
18584a014300SToomas Soome char *propname = NULL, *pgname = NULL, *sys_fmri;
18597c478bd9Sstevel@tonic-gate uint8_t found;
18607c478bd9Sstevel@tonic-gate boolean_t local_h = B_TRUE;
18617c478bd9Sstevel@tonic-gate
18627c478bd9Sstevel@tonic-gate /* If the user passed in a handle, use it. */
18637c478bd9Sstevel@tonic-gate if (hin != NULL) {
18647c478bd9Sstevel@tonic-gate h = hin;
18657c478bd9Sstevel@tonic-gate local_h = B_FALSE;
18667c478bd9Sstevel@tonic-gate }
18677c478bd9Sstevel@tonic-gate
1868f6e214c7SGavin Maltby if (local_h && ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL))
18697c478bd9Sstevel@tonic-gate return (NULL);
18707c478bd9Sstevel@tonic-gate
18717c478bd9Sstevel@tonic-gate if (inst_fmri == NULL) {
18727c478bd9Sstevel@tonic-gate if ((namelen = scf_myname(h, NULL, 0)) == -1) {
18737c478bd9Sstevel@tonic-gate if (local_h)
18747c478bd9Sstevel@tonic-gate scf_handle_destroy(h);
18757c478bd9Sstevel@tonic-gate return (NULL);
18767c478bd9Sstevel@tonic-gate }
18777c478bd9Sstevel@tonic-gate if ((sys_fmri = malloc(namelen + 1)) == NULL) {
18787c478bd9Sstevel@tonic-gate if (local_h)
18797c478bd9Sstevel@tonic-gate scf_handle_destroy(h);
18807c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY);
18817c478bd9Sstevel@tonic-gate return (NULL);
18827c478bd9Sstevel@tonic-gate }
18837c478bd9Sstevel@tonic-gate if (scf_myname(h, sys_fmri, namelen + 1) == -1) {
18847c478bd9Sstevel@tonic-gate if (local_h)
18857c478bd9Sstevel@tonic-gate scf_handle_destroy(h);
18867c478bd9Sstevel@tonic-gate free(sys_fmri);
18877c478bd9Sstevel@tonic-gate return (NULL);
18887c478bd9Sstevel@tonic-gate }
18897c478bd9Sstevel@tonic-gate } else {
18903eae19d9Swesolows if ((sys_fmri = strdup(inst_fmri)) == NULL) {
18913eae19d9Swesolows if (local_h)
18923eae19d9Swesolows scf_handle_destroy(h);
18933eae19d9Swesolows (void) scf_set_error(SCF_ERROR_NO_MEMORY);
18943eae19d9Swesolows return (NULL);
18953eae19d9Swesolows }
18967c478bd9Sstevel@tonic-gate }
18977c478bd9Sstevel@tonic-gate
18981f6eb021SLiane Praza namelen = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) + 1;
18991f6eb021SLiane Praza assert(namelen > 0);
19007c478bd9Sstevel@tonic-gate
19017c478bd9Sstevel@tonic-gate if ((inst = scf_instance_create(h)) == NULL ||
19027c478bd9Sstevel@tonic-gate (svc = scf_service_create(h)) == NULL ||
19037c478bd9Sstevel@tonic-gate (pgiter = scf_iter_create(h)) == NULL ||
19047c478bd9Sstevel@tonic-gate (propiter = scf_iter_create(h)) == NULL ||
19057c478bd9Sstevel@tonic-gate (pg = scf_pg_create(h)) == NULL ||
1906afffa6e9SMarcel Telka (prop = scf_property_create(h)) == NULL) {
1907afffa6e9SMarcel Telka free(sys_fmri);
19087c478bd9Sstevel@tonic-gate goto error2;
1909afffa6e9SMarcel Telka }
19107c478bd9Sstevel@tonic-gate
19117c478bd9Sstevel@tonic-gate if (scf_handle_decode_fmri(h, sys_fmri, NULL, svc, inst, NULL, NULL,
19127c478bd9Sstevel@tonic-gate SCF_DECODE_FMRI_REQUIRE_INSTANCE) == -1) {
1913afffa6e9SMarcel Telka free(sys_fmri);
19147c478bd9Sstevel@tonic-gate if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
19157c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
19167c478bd9Sstevel@tonic-gate goto error2;
19177c478bd9Sstevel@tonic-gate }
19187c478bd9Sstevel@tonic-gate
19197c478bd9Sstevel@tonic-gate if ((ret = malloc(sizeof (*ret))) == NULL ||
19207c478bd9Sstevel@tonic-gate (thispg = malloc(sizeof (*thispg))) == NULL ||
19217c478bd9Sstevel@tonic-gate (propname = malloc(namelen)) == NULL ||
19227c478bd9Sstevel@tonic-gate (pgname = malloc(namelen)) == NULL) {
19237c478bd9Sstevel@tonic-gate free(thispg);
19247c478bd9Sstevel@tonic-gate free(ret);
1925afffa6e9SMarcel Telka free(sys_fmri);
19267c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY);
19277c478bd9Sstevel@tonic-gate goto error2;
19287c478bd9Sstevel@tonic-gate }
19297c478bd9Sstevel@tonic-gate
19307c478bd9Sstevel@tonic-gate ret->ap_fmri = sys_fmri;
19317c478bd9Sstevel@tonic-gate thispg->pg_name = NULL;
19327c478bd9Sstevel@tonic-gate thispg->pg_proplist = NULL;
19337c478bd9Sstevel@tonic-gate thispg->pg_next = NULL;
19347c478bd9Sstevel@tonic-gate ret->ap_pglist = thispg;
19357c478bd9Sstevel@tonic-gate
19367c478bd9Sstevel@tonic-gate if (scf_iter_service_pgs_typed(pgiter, svc, SCF_GROUP_APPLICATION) !=
19377c478bd9Sstevel@tonic-gate 0) {
19387c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
19397c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INTERNAL);
19407c478bd9Sstevel@tonic-gate goto error1;
19417c478bd9Sstevel@tonic-gate }
19427c478bd9Sstevel@tonic-gate
19437c478bd9Sstevel@tonic-gate while ((pgiter_ret = scf_iter_next_pg(pgiter, pg)) == 1) {
19447c478bd9Sstevel@tonic-gate if (thispg->pg_name != NULL) {
19457c478bd9Sstevel@tonic-gate if ((nextpg = malloc(sizeof (*nextpg))) == NULL) {
19467c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY);
19477c478bd9Sstevel@tonic-gate goto error1;
19487c478bd9Sstevel@tonic-gate }
19493eae19d9Swesolows nextpg->pg_name = NULL;
19503eae19d9Swesolows nextpg->pg_next = NULL;
19513eae19d9Swesolows nextpg->pg_proplist = NULL;
19527c478bd9Sstevel@tonic-gate thispg->pg_next = nextpg;
19537c478bd9Sstevel@tonic-gate thispg = nextpg;
19547c478bd9Sstevel@tonic-gate } else {
19557c478bd9Sstevel@tonic-gate /* This is the first iteration */
19567c478bd9Sstevel@tonic-gate nextpg = thispg;
19577c478bd9Sstevel@tonic-gate }
19587c478bd9Sstevel@tonic-gate
19597c478bd9Sstevel@tonic-gate if ((nextpg->pg_name = malloc(namelen)) == NULL) {
19607c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY);
19617c478bd9Sstevel@tonic-gate goto error1;
19627c478bd9Sstevel@tonic-gate }
19637c478bd9Sstevel@tonic-gate
19647c478bd9Sstevel@tonic-gate if (scf_pg_get_name(pg, nextpg->pg_name, namelen) < 0) {
19657c478bd9Sstevel@tonic-gate if (scf_error() == SCF_ERROR_NOT_SET)
19667c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INTERNAL);
19677c478bd9Sstevel@tonic-gate goto error1;
19687c478bd9Sstevel@tonic-gate }
19697c478bd9Sstevel@tonic-gate
19707c478bd9Sstevel@tonic-gate thisprop = NULL;
19717c478bd9Sstevel@tonic-gate
19727c478bd9Sstevel@tonic-gate scf_iter_reset(propiter);
19737c478bd9Sstevel@tonic-gate
19747c478bd9Sstevel@tonic-gate if (scf_iter_pg_properties(propiter, pg) != 0) {
19757c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
19767c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INTERNAL);
19777c478bd9Sstevel@tonic-gate goto error1;
19787c478bd9Sstevel@tonic-gate }
19797c478bd9Sstevel@tonic-gate
19807c478bd9Sstevel@tonic-gate while ((propiter_ret = scf_iter_next_property(propiter, prop))
19817c478bd9Sstevel@tonic-gate == 1) {
19827c478bd9Sstevel@tonic-gate if (scf_property_get_name(prop, propname, namelen) <
19837c478bd9Sstevel@tonic-gate 0) {
19847c478bd9Sstevel@tonic-gate if (scf_error() == SCF_ERROR_NOT_SET)
19857c478bd9Sstevel@tonic-gate (void) scf_set_error(
19867c478bd9Sstevel@tonic-gate SCF_ERROR_INTERNAL);
19877c478bd9Sstevel@tonic-gate goto error1;
19887c478bd9Sstevel@tonic-gate }
19897c478bd9Sstevel@tonic-gate if (thisprop != NULL) {
19907c478bd9Sstevel@tonic-gate if ((nextprop = fill_prop(prop,
19917c478bd9Sstevel@tonic-gate nextpg->pg_name, propname, h)) == NULL)
19927c478bd9Sstevel@tonic-gate goto error1;
19937c478bd9Sstevel@tonic-gate thisprop->pr_next = nextprop;
19947c478bd9Sstevel@tonic-gate thisprop = nextprop;
19957c478bd9Sstevel@tonic-gate } else {
19967c478bd9Sstevel@tonic-gate /* This is the first iteration */
19977c478bd9Sstevel@tonic-gate if ((thisprop = fill_prop(prop,
19987c478bd9Sstevel@tonic-gate nextpg->pg_name, propname, h)) == NULL)
19997c478bd9Sstevel@tonic-gate goto error1;
20007c478bd9Sstevel@tonic-gate nextpg->pg_proplist = thisprop;
20017c478bd9Sstevel@tonic-gate nextprop = thisprop;
20027c478bd9Sstevel@tonic-gate }
20037c478bd9Sstevel@tonic-gate nextprop->pr_pg = nextpg;
20047c478bd9Sstevel@tonic-gate nextprop->pr_next = NULL;
20057c478bd9Sstevel@tonic-gate }
20067c478bd9Sstevel@tonic-gate
20077c478bd9Sstevel@tonic-gate if (propiter_ret == -1) {
20087c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
20097c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INTERNAL);
20107c478bd9Sstevel@tonic-gate goto error1;
20117c478bd9Sstevel@tonic-gate }
20127c478bd9Sstevel@tonic-gate }
20137c478bd9Sstevel@tonic-gate
20147c478bd9Sstevel@tonic-gate if (pgiter_ret == -1) {
20157c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
20167c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INTERNAL);
20177c478bd9Sstevel@tonic-gate goto error1;
20187c478bd9Sstevel@tonic-gate }
20197c478bd9Sstevel@tonic-gate
20207c478bd9Sstevel@tonic-gate /*
20217c478bd9Sstevel@tonic-gate * At this point, we've filled the scf_simple_app_props_t with all the
20227c478bd9Sstevel@tonic-gate * properties at the service level. Now we iterate over all the
20237c478bd9Sstevel@tonic-gate * properties at the instance level, overwriting any duplicate
20247c478bd9Sstevel@tonic-gate * properties, in order to provide service/instance composition.
20257c478bd9Sstevel@tonic-gate */
20267c478bd9Sstevel@tonic-gate
20277c478bd9Sstevel@tonic-gate scf_iter_reset(pgiter);
20287c478bd9Sstevel@tonic-gate scf_iter_reset(propiter);
20297c478bd9Sstevel@tonic-gate
20307c478bd9Sstevel@tonic-gate if (scf_iter_instance_pgs_typed(pgiter, inst, SCF_GROUP_APPLICATION)
20317c478bd9Sstevel@tonic-gate != 0) {
20327c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
20337c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INTERNAL);
20347c478bd9Sstevel@tonic-gate goto error1;
20357c478bd9Sstevel@tonic-gate }
20367c478bd9Sstevel@tonic-gate
20377c478bd9Sstevel@tonic-gate while ((pgiter_ret = scf_iter_next_pg(pgiter, pg)) == 1) {
20387c478bd9Sstevel@tonic-gate
20397c478bd9Sstevel@tonic-gate thispg = ret->ap_pglist;
20407c478bd9Sstevel@tonic-gate found = 0;
20417c478bd9Sstevel@tonic-gate
20427c478bd9Sstevel@tonic-gate /*
20437c478bd9Sstevel@tonic-gate * Find either the end of the list, so we can append the
20447c478bd9Sstevel@tonic-gate * property group, or an existing property group that matches
20457c478bd9Sstevel@tonic-gate * it, so we can insert/overwrite its properties.
20467c478bd9Sstevel@tonic-gate */
20477c478bd9Sstevel@tonic-gate
20487c478bd9Sstevel@tonic-gate if (scf_pg_get_name(pg, pgname, namelen) < 0) {
20497c478bd9Sstevel@tonic-gate if (scf_error() == SCF_ERROR_NOT_SET)
20507c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INTERNAL);
20517c478bd9Sstevel@tonic-gate goto error1;
20527c478bd9Sstevel@tonic-gate }
20537c478bd9Sstevel@tonic-gate
20547c478bd9Sstevel@tonic-gate while ((thispg != NULL) && (thispg->pg_name != NULL)) {
20557c478bd9Sstevel@tonic-gate if (strcmp(thispg->pg_name, pgname) == 0) {
20567c478bd9Sstevel@tonic-gate found = 1;
20577c478bd9Sstevel@tonic-gate break;
20587c478bd9Sstevel@tonic-gate }
20597c478bd9Sstevel@tonic-gate if (thispg->pg_next == NULL)
20607c478bd9Sstevel@tonic-gate break;
20617c478bd9Sstevel@tonic-gate
20627c478bd9Sstevel@tonic-gate thispg = thispg->pg_next;
20637c478bd9Sstevel@tonic-gate }
20647c478bd9Sstevel@tonic-gate
20657c478bd9Sstevel@tonic-gate scf_iter_reset(propiter);
20667c478bd9Sstevel@tonic-gate
20677c478bd9Sstevel@tonic-gate if (scf_iter_pg_properties(propiter, pg) != 0) {
20687c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
20697c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INTERNAL);
20707c478bd9Sstevel@tonic-gate goto error1;
20717c478bd9Sstevel@tonic-gate }
20727c478bd9Sstevel@tonic-gate
20737c478bd9Sstevel@tonic-gate if (found) {
20747c478bd9Sstevel@tonic-gate /*
20757c478bd9Sstevel@tonic-gate * insert_app_props inserts or overwrites the
20767c478bd9Sstevel@tonic-gate * properties in thispg.
20777c478bd9Sstevel@tonic-gate */
20787c478bd9Sstevel@tonic-gate
20797c478bd9Sstevel@tonic-gate if (insert_app_props(propiter, pgname, propname,
20807c478bd9Sstevel@tonic-gate thispg, prop, namelen, h) == -1)
20817c478bd9Sstevel@tonic-gate goto error1;
20827c478bd9Sstevel@tonic-gate
20837c478bd9Sstevel@tonic-gate } else {
20847c478bd9Sstevel@tonic-gate /*
20857c478bd9Sstevel@tonic-gate * If the property group wasn't found, we're adding
20867c478bd9Sstevel@tonic-gate * a newly allocated property group to the end of the
20877c478bd9Sstevel@tonic-gate * list.
20887c478bd9Sstevel@tonic-gate */
20897c478bd9Sstevel@tonic-gate
20907c478bd9Sstevel@tonic-gate if ((nextpg = malloc(sizeof (*nextpg))) == NULL) {
20917c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NO_MEMORY);
20927c478bd9Sstevel@tonic-gate goto error1;
20937c478bd9Sstevel@tonic-gate }
20947c478bd9Sstevel@tonic-gate nextpg->pg_next = NULL;
20957c478bd9Sstevel@tonic-gate nextpg->pg_proplist = NULL;
20967c478bd9Sstevel@tonic-gate thisprop = NULL;
20977c478bd9Sstevel@tonic-gate
20987c478bd9Sstevel@tonic-gate if ((nextpg->pg_name = strdup(pgname)) == NULL) {
20993eae19d9Swesolows (void) scf_set_error(SCF_ERROR_NO_MEMORY);
21007c478bd9Sstevel@tonic-gate free(nextpg);
21017c478bd9Sstevel@tonic-gate goto error1;
21027c478bd9Sstevel@tonic-gate }
21037c478bd9Sstevel@tonic-gate
21047c478bd9Sstevel@tonic-gate if (thispg->pg_name == NULL) {
21057c478bd9Sstevel@tonic-gate free(thispg);
21067c478bd9Sstevel@tonic-gate ret->ap_pglist = nextpg;
21077c478bd9Sstevel@tonic-gate } else {
21087c478bd9Sstevel@tonic-gate thispg->pg_next = nextpg;
21097c478bd9Sstevel@tonic-gate }
21107c478bd9Sstevel@tonic-gate
21117c478bd9Sstevel@tonic-gate while ((propiter_ret =
21127c478bd9Sstevel@tonic-gate scf_iter_next_property(propiter, prop)) == 1) {
21137c478bd9Sstevel@tonic-gate if (scf_property_get_name(prop, propname,
21147c478bd9Sstevel@tonic-gate namelen) < 0) {
21157c478bd9Sstevel@tonic-gate if (scf_error() == SCF_ERROR_NOT_SET)
21167c478bd9Sstevel@tonic-gate (void) scf_set_error(
21177c478bd9Sstevel@tonic-gate SCF_ERROR_INTERNAL);
21187c478bd9Sstevel@tonic-gate goto error1;
21197c478bd9Sstevel@tonic-gate }
21207c478bd9Sstevel@tonic-gate if (thisprop != NULL) {
21217c478bd9Sstevel@tonic-gate if ((nextprop = fill_prop(prop,
21227c478bd9Sstevel@tonic-gate pgname, propname, h)) ==
21237c478bd9Sstevel@tonic-gate NULL)
21247c478bd9Sstevel@tonic-gate goto error1;
21257c478bd9Sstevel@tonic-gate thisprop->pr_next = nextprop;
21267c478bd9Sstevel@tonic-gate thisprop = nextprop;
21277c478bd9Sstevel@tonic-gate } else {
21287c478bd9Sstevel@tonic-gate /* This is the first iteration */
21297c478bd9Sstevel@tonic-gate if ((thisprop = fill_prop(prop,
21307c478bd9Sstevel@tonic-gate pgname, propname, h)) ==
21317c478bd9Sstevel@tonic-gate NULL)
21327c478bd9Sstevel@tonic-gate goto error1;
21337c478bd9Sstevel@tonic-gate nextpg->pg_proplist = thisprop;
21347c478bd9Sstevel@tonic-gate nextprop = thisprop;
21357c478bd9Sstevel@tonic-gate }
21367c478bd9Sstevel@tonic-gate nextprop->pr_pg = nextpg;
21377c478bd9Sstevel@tonic-gate nextprop->pr_next = NULL;
21387c478bd9Sstevel@tonic-gate }
21397c478bd9Sstevel@tonic-gate
21407c478bd9Sstevel@tonic-gate if (propiter_ret == -1) {
21417c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
21427c478bd9Sstevel@tonic-gate (void) scf_set_error(
21437c478bd9Sstevel@tonic-gate SCF_ERROR_INTERNAL);
21447c478bd9Sstevel@tonic-gate goto error1;
21457c478bd9Sstevel@tonic-gate }
21467c478bd9Sstevel@tonic-gate }
21477c478bd9Sstevel@tonic-gate
21487c478bd9Sstevel@tonic-gate }
21497c478bd9Sstevel@tonic-gate
21507c478bd9Sstevel@tonic-gate if (pgiter_ret == -1) {
21517c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
21527c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_INTERNAL);
21537c478bd9Sstevel@tonic-gate goto error1;
21547c478bd9Sstevel@tonic-gate }
21557c478bd9Sstevel@tonic-gate
2156afffa6e9SMarcel Telka if (ret->ap_pglist->pg_name == NULL)
2157afffa6e9SMarcel Telka goto error1;
2158afffa6e9SMarcel Telka
21597c478bd9Sstevel@tonic-gate scf_iter_destroy(pgiter);
21607c478bd9Sstevel@tonic-gate scf_iter_destroy(propiter);
21617c478bd9Sstevel@tonic-gate scf_pg_destroy(pg);
21627c478bd9Sstevel@tonic-gate scf_property_destroy(prop);
21637c478bd9Sstevel@tonic-gate scf_instance_destroy(inst);
21647c478bd9Sstevel@tonic-gate scf_service_destroy(svc);
21657c478bd9Sstevel@tonic-gate free(propname);
21667c478bd9Sstevel@tonic-gate free(pgname);
21677c478bd9Sstevel@tonic-gate if (local_h)
21687c478bd9Sstevel@tonic-gate scf_handle_destroy(h);
21697c478bd9Sstevel@tonic-gate
21707c478bd9Sstevel@tonic-gate return (ret);
21717c478bd9Sstevel@tonic-gate
21727c478bd9Sstevel@tonic-gate /*
21737c478bd9Sstevel@tonic-gate * Exit point for a successful call. Below this line are exit points
21747c478bd9Sstevel@tonic-gate * for failures at various stages during the function.
21757c478bd9Sstevel@tonic-gate */
21767c478bd9Sstevel@tonic-gate
21777c478bd9Sstevel@tonic-gate error1:
21787c478bd9Sstevel@tonic-gate scf_simple_app_props_free(ret);
21797c478bd9Sstevel@tonic-gate
21807c478bd9Sstevel@tonic-gate error2:
21817c478bd9Sstevel@tonic-gate scf_iter_destroy(pgiter);
21827c478bd9Sstevel@tonic-gate scf_iter_destroy(propiter);
21837c478bd9Sstevel@tonic-gate scf_pg_destroy(pg);
21847c478bd9Sstevel@tonic-gate scf_property_destroy(prop);
21857c478bd9Sstevel@tonic-gate scf_instance_destroy(inst);
21867c478bd9Sstevel@tonic-gate scf_service_destroy(svc);
21877c478bd9Sstevel@tonic-gate free(propname);
21887c478bd9Sstevel@tonic-gate free(pgname);
21897c478bd9Sstevel@tonic-gate if (local_h)
21907c478bd9Sstevel@tonic-gate scf_handle_destroy(h);
21917c478bd9Sstevel@tonic-gate return (NULL);
21927c478bd9Sstevel@tonic-gate }
21937c478bd9Sstevel@tonic-gate
21947c478bd9Sstevel@tonic-gate
21957c478bd9Sstevel@tonic-gate void
scf_simple_app_props_free(scf_simple_app_props_t * propblock)21967c478bd9Sstevel@tonic-gate scf_simple_app_props_free(scf_simple_app_props_t *propblock)
21977c478bd9Sstevel@tonic-gate {
21984a014300SToomas Soome struct scf_simple_pg *pgthis, *pgnext;
21994a014300SToomas Soome scf_simple_prop_t *propthis, *propnext;
22007c478bd9Sstevel@tonic-gate
22017c478bd9Sstevel@tonic-gate if ((propblock == NULL) || (propblock->ap_pglist == NULL))
22027c478bd9Sstevel@tonic-gate return;
22037c478bd9Sstevel@tonic-gate
22047c478bd9Sstevel@tonic-gate for (pgthis = propblock->ap_pglist; pgthis != NULL; pgthis = pgnext) {
22057c478bd9Sstevel@tonic-gate pgnext = pgthis->pg_next;
22067c478bd9Sstevel@tonic-gate
22077c478bd9Sstevel@tonic-gate propthis = pgthis->pg_proplist;
22087c478bd9Sstevel@tonic-gate
22097c478bd9Sstevel@tonic-gate while (propthis != NULL) {
22107c478bd9Sstevel@tonic-gate propnext = propthis->pr_next;
22117c478bd9Sstevel@tonic-gate scf_simple_prop_free(propthis);
22127c478bd9Sstevel@tonic-gate propthis = propnext;
22137c478bd9Sstevel@tonic-gate }
22147c478bd9Sstevel@tonic-gate
22157c478bd9Sstevel@tonic-gate free(pgthis->pg_name);
22167c478bd9Sstevel@tonic-gate free(pgthis);
22177c478bd9Sstevel@tonic-gate }
22187c478bd9Sstevel@tonic-gate
22197c478bd9Sstevel@tonic-gate free(propblock->ap_fmri);
22207c478bd9Sstevel@tonic-gate free(propblock);
22217c478bd9Sstevel@tonic-gate }
22227c478bd9Sstevel@tonic-gate
22237c478bd9Sstevel@tonic-gate const scf_simple_prop_t *
scf_simple_app_props_next(const scf_simple_app_props_t * propblock,scf_simple_prop_t * last)22247c478bd9Sstevel@tonic-gate scf_simple_app_props_next(const scf_simple_app_props_t *propblock,
22257c478bd9Sstevel@tonic-gate scf_simple_prop_t *last)
22267c478bd9Sstevel@tonic-gate {
22274a014300SToomas Soome struct scf_simple_pg *this;
22287c478bd9Sstevel@tonic-gate
22297c478bd9Sstevel@tonic-gate if (propblock == NULL) {
22307c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NOT_SET);
22317c478bd9Sstevel@tonic-gate return (NULL);
22327c478bd9Sstevel@tonic-gate }
22337c478bd9Sstevel@tonic-gate
22347c478bd9Sstevel@tonic-gate this = propblock->ap_pglist;
22357c478bd9Sstevel@tonic-gate
22367c478bd9Sstevel@tonic-gate /*
22377c478bd9Sstevel@tonic-gate * We're looking for the first property in this block if last is
22387c478bd9Sstevel@tonic-gate * NULL
22397c478bd9Sstevel@tonic-gate */
22407c478bd9Sstevel@tonic-gate
22417c478bd9Sstevel@tonic-gate if (last == NULL) {
22427c478bd9Sstevel@tonic-gate /* An empty pglist is legal, it just means no properties */
22437c478bd9Sstevel@tonic-gate if (this == NULL) {
22447c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NONE);
22457c478bd9Sstevel@tonic-gate return (NULL);
22467c478bd9Sstevel@tonic-gate }
22477c478bd9Sstevel@tonic-gate /*
22487c478bd9Sstevel@tonic-gate * Walk until we find a pg with a property in it, or we run
22497c478bd9Sstevel@tonic-gate * out of property groups.
22507c478bd9Sstevel@tonic-gate */
22517c478bd9Sstevel@tonic-gate while ((this->pg_proplist == NULL) && (this->pg_next != NULL))
22527c478bd9Sstevel@tonic-gate this = this->pg_next;
22537c478bd9Sstevel@tonic-gate
22547c478bd9Sstevel@tonic-gate if (this->pg_proplist == NULL) {
22557c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NONE);
22567c478bd9Sstevel@tonic-gate return (NULL);
22577c478bd9Sstevel@tonic-gate }
22587c478bd9Sstevel@tonic-gate
22597c478bd9Sstevel@tonic-gate return (this->pg_proplist);
22607c478bd9Sstevel@tonic-gate
22617c478bd9Sstevel@tonic-gate }
22627c478bd9Sstevel@tonic-gate /*
22637c478bd9Sstevel@tonic-gate * If last isn't NULL, then return the next prop in the property group,
22647c478bd9Sstevel@tonic-gate * or walk the property groups until we find another property, or
22657c478bd9Sstevel@tonic-gate * run out of property groups.
22667c478bd9Sstevel@tonic-gate */
22677c478bd9Sstevel@tonic-gate if (last->pr_next != NULL)
22687c478bd9Sstevel@tonic-gate return (last->pr_next);
22697c478bd9Sstevel@tonic-gate
22707c478bd9Sstevel@tonic-gate if (last->pr_pg->pg_next == NULL) {
22717c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NONE);
22727c478bd9Sstevel@tonic-gate return (NULL);
22737c478bd9Sstevel@tonic-gate }
22747c478bd9Sstevel@tonic-gate
22757c478bd9Sstevel@tonic-gate this = last->pr_pg->pg_next;
22767c478bd9Sstevel@tonic-gate
22777c478bd9Sstevel@tonic-gate while ((this->pg_proplist == NULL) && (this->pg_next != NULL))
22787c478bd9Sstevel@tonic-gate this = this->pg_next;
22797c478bd9Sstevel@tonic-gate
22807c478bd9Sstevel@tonic-gate if (this->pg_proplist == NULL) {
22817c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NONE);
22827c478bd9Sstevel@tonic-gate return (NULL);
22837c478bd9Sstevel@tonic-gate }
22847c478bd9Sstevel@tonic-gate
22857c478bd9Sstevel@tonic-gate return (this->pg_proplist);
22867c478bd9Sstevel@tonic-gate }
22877c478bd9Sstevel@tonic-gate
22887c478bd9Sstevel@tonic-gate const scf_simple_prop_t *
scf_simple_app_props_search(const scf_simple_app_props_t * propblock,const char * pgname,const char * propname)22897c478bd9Sstevel@tonic-gate scf_simple_app_props_search(const scf_simple_app_props_t *propblock,
22907c478bd9Sstevel@tonic-gate const char *pgname, const char *propname)
22917c478bd9Sstevel@tonic-gate {
22924a014300SToomas Soome struct scf_simple_pg *pg;
22934a014300SToomas Soome scf_simple_prop_t *prop;
22947c478bd9Sstevel@tonic-gate
22957c478bd9Sstevel@tonic-gate if ((propblock == NULL) || (propname == NULL)) {
22967c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NOT_SET);
22977c478bd9Sstevel@tonic-gate return (NULL);
22987c478bd9Sstevel@tonic-gate }
22997c478bd9Sstevel@tonic-gate
23007c478bd9Sstevel@tonic-gate pg = propblock->ap_pglist;
23017c478bd9Sstevel@tonic-gate
23027c478bd9Sstevel@tonic-gate /*
23037c478bd9Sstevel@tonic-gate * If pgname is NULL, we're searching the default application
23047c478bd9Sstevel@tonic-gate * property group, otherwise we look for the specified group.
23057c478bd9Sstevel@tonic-gate */
23067c478bd9Sstevel@tonic-gate if (pgname == NULL) {
23077c478bd9Sstevel@tonic-gate while ((pg != NULL) &&
23087c478bd9Sstevel@tonic-gate (strcmp(SCF_PG_APP_DEFAULT, pg->pg_name) != 0))
23097c478bd9Sstevel@tonic-gate pg = pg->pg_next;
23107c478bd9Sstevel@tonic-gate } else {
23117c478bd9Sstevel@tonic-gate while ((pg != NULL) && (strcmp(pgname, pg->pg_name) != 0))
23127c478bd9Sstevel@tonic-gate pg = pg->pg_next;
23137c478bd9Sstevel@tonic-gate }
23147c478bd9Sstevel@tonic-gate
23157c478bd9Sstevel@tonic-gate if (pg == NULL) {
23167c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NOT_FOUND);
23177c478bd9Sstevel@tonic-gate return (NULL);
23187c478bd9Sstevel@tonic-gate }
23197c478bd9Sstevel@tonic-gate
23207c478bd9Sstevel@tonic-gate prop = pg->pg_proplist;
23217c478bd9Sstevel@tonic-gate
23227c478bd9Sstevel@tonic-gate while ((prop != NULL) && (strcmp(propname, prop->pr_propname) != 0))
23237c478bd9Sstevel@tonic-gate prop = prop->pr_next;
23247c478bd9Sstevel@tonic-gate
23257c478bd9Sstevel@tonic-gate if (prop == NULL) {
23267c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NOT_FOUND);
23277c478bd9Sstevel@tonic-gate return (NULL);
23287c478bd9Sstevel@tonic-gate }
23297c478bd9Sstevel@tonic-gate
23307c478bd9Sstevel@tonic-gate return (prop);
23317c478bd9Sstevel@tonic-gate }
23327c478bd9Sstevel@tonic-gate
23337c478bd9Sstevel@tonic-gate void
scf_simple_prop_next_reset(scf_simple_prop_t * prop)23347c478bd9Sstevel@tonic-gate scf_simple_prop_next_reset(scf_simple_prop_t *prop)
23357c478bd9Sstevel@tonic-gate {
23367c478bd9Sstevel@tonic-gate if (prop == NULL)
23377c478bd9Sstevel@tonic-gate return;
23387c478bd9Sstevel@tonic-gate prop->pr_iter = 0;
23397c478bd9Sstevel@tonic-gate }
23407c478bd9Sstevel@tonic-gate
23417c478bd9Sstevel@tonic-gate ssize_t
scf_simple_prop_numvalues(const scf_simple_prop_t * prop)23427c478bd9Sstevel@tonic-gate scf_simple_prop_numvalues(const scf_simple_prop_t *prop)
23437c478bd9Sstevel@tonic-gate {
23447c478bd9Sstevel@tonic-gate if (prop == NULL)
23457c478bd9Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_NOT_SET));
23467c478bd9Sstevel@tonic-gate
23477c478bd9Sstevel@tonic-gate return (prop->pr_numvalues);
23487c478bd9Sstevel@tonic-gate }
23497c478bd9Sstevel@tonic-gate
23507c478bd9Sstevel@tonic-gate
23517c478bd9Sstevel@tonic-gate scf_type_t
scf_simple_prop_type(const scf_simple_prop_t * prop)23527c478bd9Sstevel@tonic-gate scf_simple_prop_type(const scf_simple_prop_t *prop)
23537c478bd9Sstevel@tonic-gate {
23547c478bd9Sstevel@tonic-gate if (prop == NULL)
23557c478bd9Sstevel@tonic-gate return (scf_set_error(SCF_ERROR_NOT_SET));
23567c478bd9Sstevel@tonic-gate
23577c478bd9Sstevel@tonic-gate return (prop->pr_type);
23587c478bd9Sstevel@tonic-gate }
23597c478bd9Sstevel@tonic-gate
23607c478bd9Sstevel@tonic-gate
23617c478bd9Sstevel@tonic-gate char *
scf_simple_prop_name(const scf_simple_prop_t * prop)23627c478bd9Sstevel@tonic-gate scf_simple_prop_name(const scf_simple_prop_t *prop)
23637c478bd9Sstevel@tonic-gate {
23647c478bd9Sstevel@tonic-gate if ((prop == NULL) || (prop->pr_propname == NULL)) {
23657c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NOT_SET);
23667c478bd9Sstevel@tonic-gate return (NULL);
23677c478bd9Sstevel@tonic-gate }
23687c478bd9Sstevel@tonic-gate
23697c478bd9Sstevel@tonic-gate return (prop->pr_propname);
23707c478bd9Sstevel@tonic-gate }
23717c478bd9Sstevel@tonic-gate
23727c478bd9Sstevel@tonic-gate
23737c478bd9Sstevel@tonic-gate char *
scf_simple_prop_pgname(const scf_simple_prop_t * prop)23747c478bd9Sstevel@tonic-gate scf_simple_prop_pgname(const scf_simple_prop_t *prop)
23757c478bd9Sstevel@tonic-gate {
23767c478bd9Sstevel@tonic-gate if ((prop == NULL) || (prop->pr_pgname == NULL)) {
23777c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NOT_SET);
23787c478bd9Sstevel@tonic-gate return (NULL);
23797c478bd9Sstevel@tonic-gate }
23807c478bd9Sstevel@tonic-gate
23817c478bd9Sstevel@tonic-gate return (prop->pr_pgname);
23827c478bd9Sstevel@tonic-gate }
23837c478bd9Sstevel@tonic-gate
23847c478bd9Sstevel@tonic-gate
23857c478bd9Sstevel@tonic-gate static union scf_simple_prop_val *
scf_next_val(scf_simple_prop_t * prop,scf_type_t type)23867c478bd9Sstevel@tonic-gate scf_next_val(scf_simple_prop_t *prop, scf_type_t type)
23877c478bd9Sstevel@tonic-gate {
23887c478bd9Sstevel@tonic-gate if (prop == NULL) {
23897c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NOT_SET);
23907c478bd9Sstevel@tonic-gate return (NULL);
23917c478bd9Sstevel@tonic-gate }
23927c478bd9Sstevel@tonic-gate
23937c478bd9Sstevel@tonic-gate switch (prop->pr_type) {
23947c478bd9Sstevel@tonic-gate case SCF_TYPE_USTRING:
23957c478bd9Sstevel@tonic-gate case SCF_TYPE_HOST:
23967c478bd9Sstevel@tonic-gate case SCF_TYPE_HOSTNAME:
2397b56bf881SAntonello Cruz case SCF_TYPE_NET_ADDR:
23987c478bd9Sstevel@tonic-gate case SCF_TYPE_NET_ADDR_V4:
23997c478bd9Sstevel@tonic-gate case SCF_TYPE_NET_ADDR_V6:
24007c478bd9Sstevel@tonic-gate case SCF_TYPE_URI:
24017c478bd9Sstevel@tonic-gate case SCF_TYPE_FMRI: {
24027c478bd9Sstevel@tonic-gate if (type != SCF_TYPE_USTRING) {
24037c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
24047c478bd9Sstevel@tonic-gate return (NULL);
24057c478bd9Sstevel@tonic-gate }
24067c478bd9Sstevel@tonic-gate break;
24077c478bd9Sstevel@tonic-gate }
24087c478bd9Sstevel@tonic-gate default: {
24097c478bd9Sstevel@tonic-gate if (type != prop->pr_type) {
24107c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
24117c478bd9Sstevel@tonic-gate return (NULL);
24127c478bd9Sstevel@tonic-gate }
24137c478bd9Sstevel@tonic-gate break;
24147c478bd9Sstevel@tonic-gate }
24157c478bd9Sstevel@tonic-gate }
24167c478bd9Sstevel@tonic-gate
24177c478bd9Sstevel@tonic-gate if (prop->pr_iter >= prop->pr_numvalues) {
24187c478bd9Sstevel@tonic-gate (void) scf_set_error(SCF_ERROR_NONE);
24197c478bd9Sstevel@tonic-gate return (NULL);
24207c478bd9Sstevel@tonic-gate }
24217c478bd9Sstevel@tonic-gate
24227c478bd9Sstevel@tonic-gate return (&prop->pr_vallist[prop->pr_iter++]);
24237c478bd9Sstevel@tonic-gate }
24247c478bd9Sstevel@tonic-gate
24257c478bd9Sstevel@tonic-gate
24267c478bd9Sstevel@tonic-gate uint8_t *
scf_simple_prop_next_boolean(scf_simple_prop_t * prop)24277c478bd9Sstevel@tonic-gate scf_simple_prop_next_boolean(scf_simple_prop_t *prop)
24287c478bd9Sstevel@tonic-gate {
24297c478bd9Sstevel@tonic-gate union scf_simple_prop_val *ret;
24307c478bd9Sstevel@tonic-gate
24317c478bd9Sstevel@tonic-gate ret = scf_next_val(prop, SCF_TYPE_BOOLEAN);
24327c478bd9Sstevel@tonic-gate
24337c478bd9Sstevel@tonic-gate if (ret == NULL)
24347c478bd9Sstevel@tonic-gate return (NULL);
24357c478bd9Sstevel@tonic-gate
24367c478bd9Sstevel@tonic-gate return (&ret->pv_bool);
24377c478bd9Sstevel@tonic-gate }
24387c478bd9Sstevel@tonic-gate
24397c478bd9Sstevel@tonic-gate
24407c478bd9Sstevel@tonic-gate uint64_t *
scf_simple_prop_next_count(scf_simple_prop_t * prop)24417c478bd9Sstevel@tonic-gate scf_simple_prop_next_count(scf_simple_prop_t *prop)
24427c478bd9Sstevel@tonic-gate {
24437c478bd9Sstevel@tonic-gate union scf_simple_prop_val *ret;
24447c478bd9Sstevel@tonic-gate
24457c478bd9Sstevel@tonic-gate ret = scf_next_val(prop, SCF_TYPE_COUNT);
24467c478bd9Sstevel@tonic-gate
24477c478bd9Sstevel@tonic-gate if (ret == NULL)
24487c478bd9Sstevel@tonic-gate return (NULL);
24497c478bd9Sstevel@tonic-gate
24507c478bd9Sstevel@tonic-gate return (&ret->pv_uint);
24517c478bd9Sstevel@tonic-gate }
24527c478bd9Sstevel@tonic-gate
24537c478bd9Sstevel@tonic-gate
24547c478bd9Sstevel@tonic-gate int64_t *
scf_simple_prop_next_integer(scf_simple_prop_t * prop)24557c478bd9Sstevel@tonic-gate scf_simple_prop_next_integer(scf_simple_prop_t *prop)
24567c478bd9Sstevel@tonic-gate {
24577c478bd9Sstevel@tonic-gate union scf_simple_prop_val *ret;
24587c478bd9Sstevel@tonic-gate
24597c478bd9Sstevel@tonic-gate ret = scf_next_val(prop, SCF_TYPE_INTEGER);
24607c478bd9Sstevel@tonic-gate
24617c478bd9Sstevel@tonic-gate if (ret == NULL)
24627c478bd9Sstevel@tonic-gate return (NULL);
24637c478bd9Sstevel@tonic-gate
24647c478bd9Sstevel@tonic-gate return (&ret->pv_int);
24657c478bd9Sstevel@tonic-gate }
24667c478bd9Sstevel@tonic-gate
24677c478bd9Sstevel@tonic-gate int64_t *
scf_simple_prop_next_time(scf_simple_prop_t * prop,int32_t * nsec)24687c478bd9Sstevel@tonic-gate scf_simple_prop_next_time(scf_simple_prop_t *prop, int32_t *nsec)
24697c478bd9Sstevel@tonic-gate {
24707c478bd9Sstevel@tonic-gate union scf_simple_prop_val *ret;
24717c478bd9Sstevel@tonic-gate
24727c478bd9Sstevel@tonic-gate ret = scf_next_val(prop, SCF_TYPE_TIME);
24737c478bd9Sstevel@tonic-gate
24747c478bd9Sstevel@tonic-gate if (ret == NULL)
24757c478bd9Sstevel@tonic-gate return (NULL);
24767c478bd9Sstevel@tonic-gate
24777c478bd9Sstevel@tonic-gate if (nsec != NULL)
24787c478bd9Sstevel@tonic-gate *nsec = ret->pv_time.t_nsec;
24797c478bd9Sstevel@tonic-gate
24807c478bd9Sstevel@tonic-gate return (&ret->pv_time.t_sec);
24817c478bd9Sstevel@tonic-gate }
24827c478bd9Sstevel@tonic-gate
24837c478bd9Sstevel@tonic-gate char *
scf_simple_prop_next_astring(scf_simple_prop_t * prop)24847c478bd9Sstevel@tonic-gate scf_simple_prop_next_astring(scf_simple_prop_t *prop)
24857c478bd9Sstevel@tonic-gate {
24867c478bd9Sstevel@tonic-gate union scf_simple_prop_val *ret;
24877c478bd9Sstevel@tonic-gate
24887c478bd9Sstevel@tonic-gate ret = scf_next_val(prop, SCF_TYPE_ASTRING);
24897c478bd9Sstevel@tonic-gate
24907c478bd9Sstevel@tonic-gate if (ret == NULL)
24917c478bd9Sstevel@tonic-gate return (NULL);
24927c478bd9Sstevel@tonic-gate
24937c478bd9Sstevel@tonic-gate return (ret->pv_str);
24947c478bd9Sstevel@tonic-gate }
24957c478bd9Sstevel@tonic-gate
24967c478bd9Sstevel@tonic-gate char *
scf_simple_prop_next_ustring(scf_simple_prop_t * prop)24977c478bd9Sstevel@tonic-gate scf_simple_prop_next_ustring(scf_simple_prop_t *prop)
24987c478bd9Sstevel@tonic-gate {
24997c478bd9Sstevel@tonic-gate union scf_simple_prop_val *ret;
25007c478bd9Sstevel@tonic-gate
25017c478bd9Sstevel@tonic-gate ret = scf_next_val(prop, SCF_TYPE_USTRING);
25027c478bd9Sstevel@tonic-gate
25037c478bd9Sstevel@tonic-gate if (ret == NULL)
25047c478bd9Sstevel@tonic-gate return (NULL);
25057c478bd9Sstevel@tonic-gate
25067c478bd9Sstevel@tonic-gate return (ret->pv_str);
25077c478bd9Sstevel@tonic-gate }
25087c478bd9Sstevel@tonic-gate
25097c478bd9Sstevel@tonic-gate void *
scf_simple_prop_next_opaque(scf_simple_prop_t * prop,size_t * length)25107c478bd9Sstevel@tonic-gate scf_simple_prop_next_opaque(scf_simple_prop_t *prop, size_t *length)
25117c478bd9Sstevel@tonic-gate {
25127c478bd9Sstevel@tonic-gate union scf_simple_prop_val *ret;
25137c478bd9Sstevel@tonic-gate
25147c478bd9Sstevel@tonic-gate ret = scf_next_val(prop, SCF_TYPE_OPAQUE);
25157c478bd9Sstevel@tonic-gate
25167c478bd9Sstevel@tonic-gate if (ret == NULL) {
25177c478bd9Sstevel@tonic-gate *length = 0;
25187c478bd9Sstevel@tonic-gate return (NULL);
25197c478bd9Sstevel@tonic-gate }
25207c478bd9Sstevel@tonic-gate
25217c478bd9Sstevel@tonic-gate *length = ret->pv_opaque.o_size;
25227c478bd9Sstevel@tonic-gate return (ret->pv_opaque.o_value);
25237c478bd9Sstevel@tonic-gate }
252494501b61Sskamm
252594501b61Sskamm /*
252694501b61Sskamm * Generate a filename based on the fmri and the given name and return
252794501b61Sskamm * it in the buffer of MAXPATHLEN provided by the caller.
252894501b61Sskamm * If temp_filename is non-zero, also generate a temporary, unique filename
252994501b61Sskamm * and return it in the temp buffer of MAXPATHLEN provided by the caller.
253094501b61Sskamm * The path to the generated pathname is also created.
253194501b61Sskamm * Given fmri should begin with a scheme such as "svc:".
253294501b61Sskamm * Returns
253394501b61Sskamm * 0 on success
253494501b61Sskamm * -1 if filename would exceed MAXPATHLEN or
253594501b61Sskamm * -2 if unable to create directory to filename path
253694501b61Sskamm */
253794501b61Sskamm int
gen_filenms_from_fmri(const char * fmri,const char * name,char * filename,char * temp_filename)253894501b61Sskamm gen_filenms_from_fmri(const char *fmri, const char *name, char *filename,
253994501b61Sskamm char *temp_filename)
254094501b61Sskamm {
254194501b61Sskamm int len;
254294501b61Sskamm
254394501b61Sskamm len = strlen(SMF_SPEEDY_FILES_PATH);
254494501b61Sskamm len += strlen(fmri);
254594501b61Sskamm len += 2; /* for slash and null */
254694501b61Sskamm len += strlen(name);
254794501b61Sskamm len += 6; /* For X's needed for mkstemp */
254894501b61Sskamm
254994501b61Sskamm if (len > MAXPATHLEN)
255094501b61Sskamm return (-1);
255194501b61Sskamm
255294501b61Sskamm /* Construct directory name first - speedy path ends in slash */
255394501b61Sskamm (void) strcpy(filename, SMF_SPEEDY_FILES_PATH);
255494501b61Sskamm (void) strcat(filename, fmri);
255594501b61Sskamm if (mkdirp(filename, 0755) == -1) {
255694501b61Sskamm /* errno is set */
255794501b61Sskamm if (errno != EEXIST)
255894501b61Sskamm return (-2);
255994501b61Sskamm }
256094501b61Sskamm
256194501b61Sskamm (void) strcat(filename, "/");
256294501b61Sskamm (void) strcat(filename, name);
256394501b61Sskamm
256494501b61Sskamm if (temp_filename) {
256594501b61Sskamm (void) strcpy(temp_filename, filename);
256694501b61Sskamm (void) strcat(temp_filename, "XXXXXX");
256794501b61Sskamm }
256894501b61Sskamm
256994501b61Sskamm return (0);
257094501b61Sskamm }
2571dfac3eb2SDavid Powell
2572f6e214c7SGavin Maltby scf_type_t
scf_true_base_type(scf_type_t type)2573dfac3eb2SDavid Powell scf_true_base_type(scf_type_t type)
2574dfac3eb2SDavid Powell {
2575dfac3eb2SDavid Powell scf_type_t base = type;
2576dfac3eb2SDavid Powell
2577dfac3eb2SDavid Powell do {
2578dfac3eb2SDavid Powell type = base;
2579dfac3eb2SDavid Powell (void) scf_type_base_type(type, &base);
2580dfac3eb2SDavid Powell } while (base != type);
2581dfac3eb2SDavid Powell
2582dfac3eb2SDavid Powell return (base);
2583dfac3eb2SDavid Powell }
2584dfac3eb2SDavid Powell
2585dfac3eb2SDavid Powell /*
2586dfac3eb2SDavid Powell * Convenience routine which frees all strings and opaque data
2587dfac3eb2SDavid Powell * allocated by scf_read_propvec.
2588dfac3eb2SDavid Powell *
2589dfac3eb2SDavid Powell * Like free(3C), this function preserves the value of errno.
2590dfac3eb2SDavid Powell */
2591dfac3eb2SDavid Powell void
scf_clean_propvec(scf_propvec_t * propvec)2592dfac3eb2SDavid Powell scf_clean_propvec(scf_propvec_t *propvec)
2593dfac3eb2SDavid Powell {
2594dfac3eb2SDavid Powell int saved_errno = errno;
2595dfac3eb2SDavid Powell scf_propvec_t *prop;
2596dfac3eb2SDavid Powell
2597dfac3eb2SDavid Powell for (prop = propvec; prop->pv_prop != NULL; prop++) {
2598dfac3eb2SDavid Powell assert(prop->pv_type != SCF_TYPE_INVALID);
2599dfac3eb2SDavid Powell if (prop->pv_type == SCF_TYPE_OPAQUE) {
2600dfac3eb2SDavid Powell scf_opaque_t *o = prop->pv_ptr;
2601dfac3eb2SDavid Powell
2602dfac3eb2SDavid Powell if (o->so_addr != NULL)
2603dfac3eb2SDavid Powell free(o->so_addr);
2604dfac3eb2SDavid Powell } else if (scf_true_base_type(prop->pv_type) ==
2605dfac3eb2SDavid Powell SCF_TYPE_ASTRING) {
2606dfac3eb2SDavid Powell if (*(char **)prop->pv_ptr != NULL)
2607dfac3eb2SDavid Powell free(*(char **)prop->pv_ptr);
2608dfac3eb2SDavid Powell }
2609dfac3eb2SDavid Powell }
2610dfac3eb2SDavid Powell
2611dfac3eb2SDavid Powell errno = saved_errno;
2612dfac3eb2SDavid Powell }
2613dfac3eb2SDavid Powell
2614dfac3eb2SDavid Powell static int
count_props(scf_propvec_t * props)2615dfac3eb2SDavid Powell count_props(scf_propvec_t *props)
2616dfac3eb2SDavid Powell {
2617dfac3eb2SDavid Powell int count = 0;
2618dfac3eb2SDavid Powell
2619dfac3eb2SDavid Powell for (; props->pv_prop != NULL; props++)
2620dfac3eb2SDavid Powell count++;
2621dfac3eb2SDavid Powell return (count);
2622dfac3eb2SDavid Powell }
2623dfac3eb2SDavid Powell
2624dfac3eb2SDavid Powell /*
2625dfac3eb2SDavid Powell * Reads a vector of properties from the specified fmri/property group.
2626dfac3eb2SDavid Powell * If 'running' is true, reads from the running snapshot instead of the
2627dfac3eb2SDavid Powell * editing snapshot.
2628dfac3eb2SDavid Powell *
2629dfac3eb2SDavid Powell * For string types, a buffer is allocated using malloc(3C) to hold the
2630dfac3eb2SDavid Powell * zero-terminated string, a pointer to which is stored in the
2631dfac3eb2SDavid Powell * caller-provided char **. It is the caller's responsbility to free
2632dfac3eb2SDavid Powell * this string. To simplify error handling, unread strings are
2633dfac3eb2SDavid Powell * initialized to NULL.
2634dfac3eb2SDavid Powell *
2635dfac3eb2SDavid Powell * For opaque types, a buffer is allocated using malloc(3C) to hold the
2636dfac3eb2SDavid Powell * opaque data. A pointer to this buffer and its size are stored in
2637dfac3eb2SDavid Powell * the caller-provided scf_opaque_t. It is the caller's responsibility
2638dfac3eb2SDavid Powell * to free this buffer. To simplify error handling, the address fields
2639dfac3eb2SDavid Powell * for unread opaque data are initialized to NULL.
2640dfac3eb2SDavid Powell *
2641dfac3eb2SDavid Powell * All other data is stored directly in caller-provided variables or
2642dfac3eb2SDavid Powell * structures.
2643dfac3eb2SDavid Powell *
2644dfac3eb2SDavid Powell * If this function fails to read a specific property, *badprop is set
2645dfac3eb2SDavid Powell * to point at that property's entry in the properties array.
2646dfac3eb2SDavid Powell *
2647dfac3eb2SDavid Powell * On all failures, all memory allocated by this function is freed.
2648dfac3eb2SDavid Powell */
2649dfac3eb2SDavid Powell int
scf_read_propvec(const char * fmri,const char * pgname,boolean_t running,scf_propvec_t * properties,scf_propvec_t ** badprop)2650dfac3eb2SDavid Powell scf_read_propvec(const char *fmri, const char *pgname, boolean_t running,
2651dfac3eb2SDavid Powell scf_propvec_t *properties, scf_propvec_t **badprop)
2652dfac3eb2SDavid Powell {
2653f6e214c7SGavin Maltby scf_handle_t *h = _scf_handle_create_and_bind(SCF_VERSION);
2654dfac3eb2SDavid Powell scf_service_t *s = scf_service_create(h);
2655dfac3eb2SDavid Powell scf_instance_t *i = scf_instance_create(h);
2656dfac3eb2SDavid Powell scf_snapshot_t *snap = running ? scf_snapshot_create(h) : NULL;
2657dfac3eb2SDavid Powell scf_propertygroup_t *pg = scf_pg_create(h);
2658dfac3eb2SDavid Powell scf_property_t *p = scf_property_create(h);
2659dfac3eb2SDavid Powell scf_value_t *v = scf_value_create(h);
2660dfac3eb2SDavid Powell boolean_t instance = B_TRUE;
2661dfac3eb2SDavid Powell scf_propvec_t *prop;
2662dfac3eb2SDavid Powell int error = 0;
2663dfac3eb2SDavid Powell
2664e6ccb06dSAndrew Stormont for (prop = properties; prop->pv_prop != NULL; prop++) {
2665e6ccb06dSAndrew Stormont if (prop->pv_type == SCF_TYPE_OPAQUE)
2666e6ccb06dSAndrew Stormont ((scf_opaque_t *)prop->pv_ptr)->so_addr = NULL;
2667e6ccb06dSAndrew Stormont else if (scf_true_base_type(prop->pv_type) == SCF_TYPE_ASTRING)
2668e6ccb06dSAndrew Stormont *((char **)prop->pv_ptr) = NULL;
2669e6ccb06dSAndrew Stormont }
2670e6ccb06dSAndrew Stormont
2671dfac3eb2SDavid Powell if (h == NULL || s == NULL || i == NULL || (running && snap == NULL) ||
2672dfac3eb2SDavid Powell pg == NULL || p == NULL || v == NULL)
2673dfac3eb2SDavid Powell goto scferror;
2674dfac3eb2SDavid Powell
2675dfac3eb2SDavid Powell if (scf_handle_decode_fmri(h, fmri, NULL, s, i, NULL, NULL, 0) == -1)
2676dfac3eb2SDavid Powell goto scferror;
2677dfac3eb2SDavid Powell
2678dfac3eb2SDavid Powell if (scf_instance_to_fmri(i, NULL, 0) == -1) {
2679dfac3eb2SDavid Powell if (scf_error() != SCF_ERROR_NOT_SET)
2680dfac3eb2SDavid Powell goto scferror;
2681dfac3eb2SDavid Powell instance = B_FALSE;
2682dfac3eb2SDavid Powell }
2683dfac3eb2SDavid Powell
2684dfac3eb2SDavid Powell if (running) {
2685dfac3eb2SDavid Powell if (!instance) {
2686dfac3eb2SDavid Powell error = SCF_ERROR_TYPE_MISMATCH;
2687dfac3eb2SDavid Powell goto out;
2688dfac3eb2SDavid Powell }
2689dfac3eb2SDavid Powell
2690dfac3eb2SDavid Powell if (scf_instance_get_snapshot(i, "running", snap) !=
2691dfac3eb2SDavid Powell SCF_SUCCESS)
2692dfac3eb2SDavid Powell goto scferror;
2693dfac3eb2SDavid Powell }
2694dfac3eb2SDavid Powell
2695dfac3eb2SDavid Powell if ((instance ? scf_instance_get_pg_composed(i, snap, pgname, pg) :
2696dfac3eb2SDavid Powell scf_service_get_pg(s, pgname, pg)) == -1)
2697dfac3eb2SDavid Powell goto scferror;
2698dfac3eb2SDavid Powell
2699dfac3eb2SDavid Powell for (prop = properties; prop->pv_prop != NULL; prop++) {
2700dfac3eb2SDavid Powell int ret = 0;
2701dfac3eb2SDavid Powell
2702dfac3eb2SDavid Powell if (scf_pg_get_property(pg, prop->pv_prop, p) == -1 ||
2703dfac3eb2SDavid Powell scf_property_get_value(p, v) == -1) {
2704dfac3eb2SDavid Powell *badprop = prop;
2705dfac3eb2SDavid Powell goto scferror;
2706dfac3eb2SDavid Powell }
2707dfac3eb2SDavid Powell switch (prop->pv_type) {
2708dfac3eb2SDavid Powell case SCF_TYPE_BOOLEAN: {
2709dfac3eb2SDavid Powell uint8_t b;
2710dfac3eb2SDavid Powell
2711dfac3eb2SDavid Powell ret = scf_value_get_boolean(v, &b);
2712dfac3eb2SDavid Powell if (ret == -1)
2713dfac3eb2SDavid Powell break;
2714dfac3eb2SDavid Powell if (prop->pv_aux != 0) {
2715dfac3eb2SDavid Powell uint64_t *bits = prop->pv_ptr;
2716dfac3eb2SDavid Powell *bits = b ? (*bits | prop->pv_aux) :
2717dfac3eb2SDavid Powell (*bits & ~prop->pv_aux);
2718dfac3eb2SDavid Powell } else {
2719dfac3eb2SDavid Powell boolean_t *bool = prop->pv_ptr;
2720dfac3eb2SDavid Powell *bool = b ? B_TRUE : B_FALSE;
2721dfac3eb2SDavid Powell }
2722dfac3eb2SDavid Powell break;
2723dfac3eb2SDavid Powell }
2724dfac3eb2SDavid Powell case SCF_TYPE_COUNT:
2725dfac3eb2SDavid Powell ret = scf_value_get_count(v, prop->pv_ptr);
2726dfac3eb2SDavid Powell break;
2727dfac3eb2SDavid Powell case SCF_TYPE_INTEGER:
2728dfac3eb2SDavid Powell ret = scf_value_get_integer(v, prop->pv_ptr);
2729dfac3eb2SDavid Powell break;
2730dfac3eb2SDavid Powell case SCF_TYPE_TIME: {
2731dfac3eb2SDavid Powell scf_time_t *time = prop->pv_ptr;
2732dfac3eb2SDavid Powell
27331f6eb021SLiane Praza ret = scf_value_get_time(v, &time->t_seconds,
27341f6eb021SLiane Praza &time->t_ns);
2735dfac3eb2SDavid Powell break;
2736dfac3eb2SDavid Powell }
2737dfac3eb2SDavid Powell case SCF_TYPE_OPAQUE: {
2738dfac3eb2SDavid Powell scf_opaque_t *opaque = prop->pv_ptr;
2739dfac3eb2SDavid Powell ssize_t size = scf_value_get_opaque(v, NULL, 0);
2740dfac3eb2SDavid Powell
2741dfac3eb2SDavid Powell if (size == -1) {
2742dfac3eb2SDavid Powell *badprop = prop;
2743dfac3eb2SDavid Powell goto scferror;
2744dfac3eb2SDavid Powell }
2745dfac3eb2SDavid Powell if ((opaque->so_addr = malloc(size)) == NULL) {
2746dfac3eb2SDavid Powell error = SCF_ERROR_NO_MEMORY;
2747dfac3eb2SDavid Powell goto out;
2748dfac3eb2SDavid Powell }
2749dfac3eb2SDavid Powell opaque->so_size = size;
2750dfac3eb2SDavid Powell ret = scf_value_get_opaque(v, opaque->so_addr, size);
2751dfac3eb2SDavid Powell break;
2752dfac3eb2SDavid Powell }
2753dfac3eb2SDavid Powell default: {
2754dfac3eb2SDavid Powell char *s;
2755dfac3eb2SDavid Powell ssize_t size;
2756dfac3eb2SDavid Powell
2757dfac3eb2SDavid Powell assert(scf_true_base_type(prop->pv_type) ==
2758dfac3eb2SDavid Powell SCF_TYPE_ASTRING);
2759dfac3eb2SDavid Powell
2760dfac3eb2SDavid Powell size = scf_value_get_astring(v, NULL, 0);
2761dfac3eb2SDavid Powell if (size == -1) {
2762dfac3eb2SDavid Powell *badprop = prop;
2763dfac3eb2SDavid Powell goto scferror;
2764dfac3eb2SDavid Powell }
2765dfac3eb2SDavid Powell if ((s = malloc(++size)) == NULL) {
2766dfac3eb2SDavid Powell error = SCF_ERROR_NO_MEMORY;
2767dfac3eb2SDavid Powell goto out;
2768dfac3eb2SDavid Powell }
2769dfac3eb2SDavid Powell ret = scf_value_get_astring(v, s, size);
2770dfac3eb2SDavid Powell *(char **)prop->pv_ptr = s;
2771dfac3eb2SDavid Powell }
2772dfac3eb2SDavid Powell
2773dfac3eb2SDavid Powell if (ret == -1) {
2774dfac3eb2SDavid Powell *badprop = prop;
2775dfac3eb2SDavid Powell goto scferror;
2776dfac3eb2SDavid Powell }
2777dfac3eb2SDavid Powell
2778dfac3eb2SDavid Powell }
2779dfac3eb2SDavid Powell }
2780dfac3eb2SDavid Powell
2781dfac3eb2SDavid Powell goto out;
2782dfac3eb2SDavid Powell
2783dfac3eb2SDavid Powell scferror:
2784dfac3eb2SDavid Powell error = scf_error();
2785dfac3eb2SDavid Powell scf_clean_propvec(properties);
2786dfac3eb2SDavid Powell
2787dfac3eb2SDavid Powell out:
2788c42520ebSJan Friedel scf_value_destroy(v);
2789c42520ebSJan Friedel scf_property_destroy(p);
2790dfac3eb2SDavid Powell scf_pg_destroy(pg);
2791dfac3eb2SDavid Powell scf_snapshot_destroy(snap);
2792dfac3eb2SDavid Powell scf_instance_destroy(i);
2793dfac3eb2SDavid Powell scf_service_destroy(s);
2794dfac3eb2SDavid Powell scf_handle_destroy(h);
2795dfac3eb2SDavid Powell
2796dfac3eb2SDavid Powell if (error != 0) {
2797dfac3eb2SDavid Powell (void) scf_set_error(error);
2798dfac3eb2SDavid Powell return (SCF_FAILED);
2799dfac3eb2SDavid Powell }
2800dfac3eb2SDavid Powell
2801dfac3eb2SDavid Powell return (SCF_SUCCESS);
2802dfac3eb2SDavid Powell }
2803dfac3eb2SDavid Powell
2804dfac3eb2SDavid Powell /*
2805dfac3eb2SDavid Powell * Writes a vector of properties to the specified fmri/property group.
2806dfac3eb2SDavid Powell *
2807dfac3eb2SDavid Powell * If this function fails to write a specific property, *badprop is set
2808dfac3eb2SDavid Powell * to point at that property's entry in the properties array.
2809dfac3eb2SDavid Powell *
2810dfac3eb2SDavid Powell * One significant difference between this function and the
2811dfac3eb2SDavid Powell * scf_read_propvec function is that for string types, pv_ptr is a
2812dfac3eb2SDavid Powell * char *, not a char **. This means that you can't write a propvec
2813dfac3eb2SDavid Powell * you just read, but makes other uses (hopefully the majority) simpler.
2814dfac3eb2SDavid Powell */
2815dfac3eb2SDavid Powell int
scf_write_propvec(const char * fmri,const char * pgname,scf_propvec_t * properties,scf_propvec_t ** badprop)2816dfac3eb2SDavid Powell scf_write_propvec(const char *fmri, const char *pgname,
2817dfac3eb2SDavid Powell scf_propvec_t *properties, scf_propvec_t **badprop)
2818dfac3eb2SDavid Powell {
2819f6e214c7SGavin Maltby scf_handle_t *h = _scf_handle_create_and_bind(SCF_VERSION);
2820dfac3eb2SDavid Powell scf_service_t *s = scf_service_create(h);
2821dfac3eb2SDavid Powell scf_instance_t *inst = scf_instance_create(h);
2822dfac3eb2SDavid Powell scf_snapshot_t *snap = scf_snapshot_create(h);
2823dfac3eb2SDavid Powell scf_propertygroup_t *pg = scf_pg_create(h);
2824dfac3eb2SDavid Powell scf_property_t *p = scf_property_create(h);
2825dfac3eb2SDavid Powell scf_transaction_t *tx = scf_transaction_create(h);
2826dfac3eb2SDavid Powell scf_value_t **v = NULL;
2827dfac3eb2SDavid Powell scf_transaction_entry_t **e = NULL;
2828dfac3eb2SDavid Powell boolean_t instance = B_TRUE;
2829dfac3eb2SDavid Powell int i, n;
2830dfac3eb2SDavid Powell scf_propvec_t *prop;
2831dfac3eb2SDavid Powell int error = 0, ret;
2832dfac3eb2SDavid Powell
2833dfac3eb2SDavid Powell n = count_props(properties);
2834dfac3eb2SDavid Powell v = calloc(n, sizeof (scf_value_t *));
2835dfac3eb2SDavid Powell e = calloc(n, sizeof (scf_transaction_entry_t *));
2836dfac3eb2SDavid Powell
2837dfac3eb2SDavid Powell if (v == NULL || e == NULL) {
2838dfac3eb2SDavid Powell error = SCF_ERROR_NO_MEMORY;
2839dfac3eb2SDavid Powell goto out;
2840dfac3eb2SDavid Powell }
2841dfac3eb2SDavid Powell
2842dfac3eb2SDavid Powell if (h == NULL || s == NULL || inst == NULL || pg == NULL || p == NULL ||
2843dfac3eb2SDavid Powell tx == NULL)
2844dfac3eb2SDavid Powell goto scferror;
2845dfac3eb2SDavid Powell
2846dfac3eb2SDavid Powell for (i = 0; i < n; i++) {
2847dfac3eb2SDavid Powell v[i] = scf_value_create(h);
2848dfac3eb2SDavid Powell e[i] = scf_entry_create(h);
2849dfac3eb2SDavid Powell if (v[i] == NULL || e[i] == NULL)
2850dfac3eb2SDavid Powell goto scferror;
2851dfac3eb2SDavid Powell }
2852dfac3eb2SDavid Powell
2853dfac3eb2SDavid Powell if (scf_handle_decode_fmri(h, fmri, NULL, s, inst, NULL, NULL, 0)
2854dfac3eb2SDavid Powell != SCF_SUCCESS)
2855dfac3eb2SDavid Powell goto scferror;
2856dfac3eb2SDavid Powell
2857dfac3eb2SDavid Powell if (scf_instance_to_fmri(inst, NULL, 0) == -1) {
2858dfac3eb2SDavid Powell if (scf_error() != SCF_ERROR_NOT_SET)
2859dfac3eb2SDavid Powell goto scferror;
2860dfac3eb2SDavid Powell instance = B_FALSE;
2861dfac3eb2SDavid Powell }
2862dfac3eb2SDavid Powell
2863dfac3eb2SDavid Powell if ((instance ? scf_instance_get_pg(inst, pgname, pg) :
2864dfac3eb2SDavid Powell scf_service_get_pg(s, pgname, pg)) == -1)
2865dfac3eb2SDavid Powell goto scferror;
2866dfac3eb2SDavid Powell
2867dfac3eb2SDavid Powell top:
2868dfac3eb2SDavid Powell if (scf_transaction_start(tx, pg) == -1)
2869dfac3eb2SDavid Powell goto scferror;
2870dfac3eb2SDavid Powell
2871dfac3eb2SDavid Powell for (prop = properties, i = 0; prop->pv_prop != NULL; prop++, i++) {
2872dfac3eb2SDavid Powell ret = scf_transaction_property_change(tx, e[i], prop->pv_prop,
2873dfac3eb2SDavid Powell prop->pv_type);
2874dfac3eb2SDavid Powell if (ret == -1 && scf_error() == SCF_ERROR_NOT_FOUND)
2875dfac3eb2SDavid Powell ret = scf_transaction_property_new(tx, e[i],
2876dfac3eb2SDavid Powell prop->pv_prop, prop->pv_type);
2877dfac3eb2SDavid Powell
2878dfac3eb2SDavid Powell if (ret == -1) {
2879dfac3eb2SDavid Powell *badprop = prop;
2880dfac3eb2SDavid Powell goto scferror;
2881dfac3eb2SDavid Powell }
2882dfac3eb2SDavid Powell
2883dfac3eb2SDavid Powell switch (prop->pv_type) {
2884dfac3eb2SDavid Powell case SCF_TYPE_BOOLEAN: {
2885dfac3eb2SDavid Powell boolean_t b = (prop->pv_aux != 0) ?
2886dfac3eb2SDavid Powell (*(uint64_t *)prop->pv_ptr & prop->pv_aux) != 0 :
2887dfac3eb2SDavid Powell *(boolean_t *)prop->pv_ptr;
2888dfac3eb2SDavid Powell
2889dfac3eb2SDavid Powell scf_value_set_boolean(v[i], b ? 1 : 0);
2890dfac3eb2SDavid Powell break;
2891dfac3eb2SDavid Powell }
2892dfac3eb2SDavid Powell case SCF_TYPE_COUNT:
2893dfac3eb2SDavid Powell scf_value_set_count(v[i], *(uint64_t *)prop->pv_ptr);
2894dfac3eb2SDavid Powell break;
2895dfac3eb2SDavid Powell case SCF_TYPE_INTEGER:
2896dfac3eb2SDavid Powell scf_value_set_integer(v[i], *(int64_t *)prop->pv_ptr);
2897dfac3eb2SDavid Powell break;
2898dfac3eb2SDavid Powell case SCF_TYPE_TIME: {
2899dfac3eb2SDavid Powell scf_time_t *time = prop->pv_ptr;
2900dfac3eb2SDavid Powell
29011f6eb021SLiane Praza ret = scf_value_set_time(v[i], time->t_seconds,
29021f6eb021SLiane Praza time->t_ns);
2903dfac3eb2SDavid Powell break;
2904dfac3eb2SDavid Powell }
2905dfac3eb2SDavid Powell case SCF_TYPE_OPAQUE: {
2906dfac3eb2SDavid Powell scf_opaque_t *opaque = prop->pv_ptr;
2907dfac3eb2SDavid Powell
2908dfac3eb2SDavid Powell ret = scf_value_set_opaque(v[i], opaque->so_addr,
2909dfac3eb2SDavid Powell opaque->so_size);
2910dfac3eb2SDavid Powell break;
2911dfac3eb2SDavid Powell }
2912dfac3eb2SDavid Powell case SCF_TYPE_ASTRING:
2913dfac3eb2SDavid Powell ret = scf_value_set_astring(v[i],
2914dfac3eb2SDavid Powell (const char *)prop->pv_ptr);
2915dfac3eb2SDavid Powell break;
2916dfac3eb2SDavid Powell default:
2917dfac3eb2SDavid Powell ret = scf_value_set_from_string(v[i], prop->pv_type,
2918dfac3eb2SDavid Powell (const char *)prop->pv_ptr);
2919dfac3eb2SDavid Powell }
2920dfac3eb2SDavid Powell
2921dfac3eb2SDavid Powell if (ret == -1 || scf_entry_add_value(e[i], v[i]) == -1) {
2922dfac3eb2SDavid Powell *badprop = prop;
2923dfac3eb2SDavid Powell goto scferror;
2924dfac3eb2SDavid Powell }
2925dfac3eb2SDavid Powell }
2926dfac3eb2SDavid Powell
2927dfac3eb2SDavid Powell ret = scf_transaction_commit(tx);
2928dfac3eb2SDavid Powell if (ret == 1)
2929dfac3eb2SDavid Powell goto out;
2930dfac3eb2SDavid Powell
2931dfac3eb2SDavid Powell if (ret == 0 && scf_pg_update(pg) != -1) {
2932dfac3eb2SDavid Powell scf_transaction_reset(tx);
2933dfac3eb2SDavid Powell goto top;
2934dfac3eb2SDavid Powell }
2935dfac3eb2SDavid Powell
2936dfac3eb2SDavid Powell scferror:
2937dfac3eb2SDavid Powell error = scf_error();
2938dfac3eb2SDavid Powell
2939dfac3eb2SDavid Powell out:
2940dfac3eb2SDavid Powell if (v != NULL) {
2941dfac3eb2SDavid Powell for (i = 0; i < n; i++)
2942dfac3eb2SDavid Powell scf_value_destroy(v[i]);
2943dfac3eb2SDavid Powell free(v);
2944dfac3eb2SDavid Powell }
2945dfac3eb2SDavid Powell
2946dfac3eb2SDavid Powell if (e != NULL) {
2947dfac3eb2SDavid Powell for (i = 0; i < n; i++)
2948dfac3eb2SDavid Powell scf_entry_destroy(e[i]);
2949dfac3eb2SDavid Powell free(e);
2950dfac3eb2SDavid Powell }
2951dfac3eb2SDavid Powell
2952dfac3eb2SDavid Powell scf_transaction_destroy(tx);
2953dfac3eb2SDavid Powell scf_property_destroy(p);
2954dfac3eb2SDavid Powell scf_pg_destroy(pg);
2955dfac3eb2SDavid Powell scf_snapshot_destroy(snap);
2956dfac3eb2SDavid Powell scf_instance_destroy(inst);
2957dfac3eb2SDavid Powell scf_service_destroy(s);
2958dfac3eb2SDavid Powell scf_handle_destroy(h);
2959dfac3eb2SDavid Powell
2960dfac3eb2SDavid Powell if (error != 0) {
2961dfac3eb2SDavid Powell (void) scf_set_error(error);
2962dfac3eb2SDavid Powell return (SCF_FAILED);
2963dfac3eb2SDavid Powell }
2964dfac3eb2SDavid Powell
2965dfac3eb2SDavid Powell return (SCF_SUCCESS);
2966dfac3eb2SDavid Powell }
2967eb1a3463STruong Nguyen
2968eb1a3463STruong Nguyen /*
2969eb1a3463STruong Nguyen * Returns
2970eb1a3463STruong Nguyen * 0 - success
2971eb1a3463STruong Nguyen * ECONNABORTED - repository connection broken
2972eb1a3463STruong Nguyen * ECANCELED - inst was deleted
2973eb1a3463STruong Nguyen * EPERM
2974eb1a3463STruong Nguyen * EACCES
2975eb1a3463STruong Nguyen * EROFS
2976eb1a3463STruong Nguyen * ENOMEM
2977eb1a3463STruong Nguyen */
2978eb1a3463STruong Nguyen int
scf_instance_delete_prop(scf_instance_t * inst,const char * pgname,const char * pname)2979eb1a3463STruong Nguyen scf_instance_delete_prop(scf_instance_t *inst, const char *pgname,
2980eb1a3463STruong Nguyen const char *pname)
2981eb1a3463STruong Nguyen {
2982eb1a3463STruong Nguyen scf_handle_t *h;
2983eb1a3463STruong Nguyen scf_propertygroup_t *pg;
2984eb1a3463STruong Nguyen scf_transaction_t *tx;
2985eb1a3463STruong Nguyen scf_transaction_entry_t *e;
2986eb1a3463STruong Nguyen int error = 0, ret = 1, r;
2987eb1a3463STruong Nguyen
2988eb1a3463STruong Nguyen h = scf_instance_handle(inst);
2989eb1a3463STruong Nguyen
2990eb1a3463STruong Nguyen if ((pg = scf_pg_create(h)) == NULL) {
2991eb1a3463STruong Nguyen return (ENOMEM);
2992eb1a3463STruong Nguyen }
2993eb1a3463STruong Nguyen
2994eb1a3463STruong Nguyen if (scf_instance_get_pg(inst, pgname, pg) != 0) {
2995eb1a3463STruong Nguyen error = scf_error();
2996eb1a3463STruong Nguyen scf_pg_destroy(pg);
2997eb1a3463STruong Nguyen switch (error) {
2998eb1a3463STruong Nguyen case SCF_ERROR_NOT_FOUND:
2999eb1a3463STruong Nguyen return (SCF_SUCCESS);
3000eb1a3463STruong Nguyen
3001eb1a3463STruong Nguyen case SCF_ERROR_DELETED:
3002eb1a3463STruong Nguyen return (ECANCELED);
3003eb1a3463STruong Nguyen
3004eb1a3463STruong Nguyen case SCF_ERROR_CONNECTION_BROKEN:
3005eb1a3463STruong Nguyen default:
3006eb1a3463STruong Nguyen return (ECONNABORTED);
3007eb1a3463STruong Nguyen
3008eb1a3463STruong Nguyen case SCF_ERROR_NOT_SET:
3009eb1a3463STruong Nguyen bad_error("scf_instance_get_pg", scf_error());
3010eb1a3463STruong Nguyen }
3011eb1a3463STruong Nguyen }
3012eb1a3463STruong Nguyen
3013eb1a3463STruong Nguyen tx = scf_transaction_create(h);
3014eb1a3463STruong Nguyen e = scf_entry_create(h);
3015eb1a3463STruong Nguyen if (tx == NULL || e == NULL) {
3016eb1a3463STruong Nguyen ret = ENOMEM;
3017eb1a3463STruong Nguyen goto out;
3018eb1a3463STruong Nguyen }
3019eb1a3463STruong Nguyen
3020eb1a3463STruong Nguyen for (;;) {
3021eb1a3463STruong Nguyen if (scf_transaction_start(tx, pg) != 0) {
3022eb1a3463STruong Nguyen goto scferror;
3023eb1a3463STruong Nguyen }
3024eb1a3463STruong Nguyen
3025eb1a3463STruong Nguyen if (scf_transaction_property_delete(tx, e, pname) != 0) {
3026eb1a3463STruong Nguyen goto scferror;
3027eb1a3463STruong Nguyen }
3028eb1a3463STruong Nguyen
3029eb1a3463STruong Nguyen if ((r = scf_transaction_commit(tx)) == 1) {
3030eb1a3463STruong Nguyen ret = 0;
3031eb1a3463STruong Nguyen goto out;
3032eb1a3463STruong Nguyen }
3033eb1a3463STruong Nguyen
3034eb1a3463STruong Nguyen if (r == -1) {
3035eb1a3463STruong Nguyen goto scferror;
3036eb1a3463STruong Nguyen }
3037eb1a3463STruong Nguyen
3038eb1a3463STruong Nguyen scf_transaction_reset(tx);
3039eb1a3463STruong Nguyen if (scf_pg_update(pg) == -1) {
3040eb1a3463STruong Nguyen goto scferror;
3041eb1a3463STruong Nguyen }
3042eb1a3463STruong Nguyen }
3043eb1a3463STruong Nguyen
3044eb1a3463STruong Nguyen scferror:
3045eb1a3463STruong Nguyen switch (scf_error()) {
3046eb1a3463STruong Nguyen case SCF_ERROR_DELETED:
3047eb1a3463STruong Nguyen case SCF_ERROR_NOT_FOUND:
3048eb1a3463STruong Nguyen ret = 0;
3049eb1a3463STruong Nguyen break;
3050eb1a3463STruong Nguyen
3051eb1a3463STruong Nguyen case SCF_ERROR_PERMISSION_DENIED:
3052eb1a3463STruong Nguyen ret = EPERM;
3053eb1a3463STruong Nguyen break;
3054eb1a3463STruong Nguyen
3055eb1a3463STruong Nguyen case SCF_ERROR_BACKEND_ACCESS:
3056eb1a3463STruong Nguyen ret = EACCES;
3057eb1a3463STruong Nguyen break;
3058eb1a3463STruong Nguyen
3059eb1a3463STruong Nguyen case SCF_ERROR_BACKEND_READONLY:
3060eb1a3463STruong Nguyen ret = EROFS;
3061eb1a3463STruong Nguyen break;
3062eb1a3463STruong Nguyen
3063eb1a3463STruong Nguyen case SCF_ERROR_CONNECTION_BROKEN:
3064eb1a3463STruong Nguyen default:
3065eb1a3463STruong Nguyen ret = ECONNABORTED;
3066eb1a3463STruong Nguyen break;
3067eb1a3463STruong Nguyen
3068eb1a3463STruong Nguyen case SCF_ERROR_HANDLE_MISMATCH:
3069eb1a3463STruong Nguyen case SCF_ERROR_INVALID_ARGUMENT:
3070eb1a3463STruong Nguyen case SCF_ERROR_NOT_BOUND:
3071eb1a3463STruong Nguyen case SCF_ERROR_NOT_SET:
3072eb1a3463STruong Nguyen bad_error("scf_instance_delete_prop", scf_error());
3073eb1a3463STruong Nguyen }
3074eb1a3463STruong Nguyen
3075eb1a3463STruong Nguyen out:
3076eb1a3463STruong Nguyen scf_transaction_destroy(tx);
3077eb1a3463STruong Nguyen scf_entry_destroy(e);
3078eb1a3463STruong Nguyen scf_pg_destroy(pg);
3079eb1a3463STruong Nguyen
3080eb1a3463STruong Nguyen return (ret);
3081eb1a3463STruong Nguyen }
3082b56bf881SAntonello Cruz
3083b56bf881SAntonello Cruz /*
3084b56bf881SAntonello Cruz * Check the "application/auto_enable" property for the passed FMRI.
3085b56bf881SAntonello Cruz * scf_simple_prop_get() should find the property on an instance
3086b56bf881SAntonello Cruz * or on the service FMRI. The routine returns:
3087b56bf881SAntonello Cruz * -1: inconclusive (likely no such property or FMRI)
3088b56bf881SAntonello Cruz * 0: auto_enable is false
3089b56bf881SAntonello Cruz * 1: auto_enable is true
3090b56bf881SAntonello Cruz */
3091b56bf881SAntonello Cruz static int
is_auto_enabled(char * fmri)3092b56bf881SAntonello Cruz is_auto_enabled(char *fmri)
3093b56bf881SAntonello Cruz {
3094b56bf881SAntonello Cruz scf_simple_prop_t *prop;
3095b56bf881SAntonello Cruz int retval = -1;
3096b56bf881SAntonello Cruz uint8_t *ret;
3097b56bf881SAntonello Cruz
3098b56bf881SAntonello Cruz prop = scf_simple_prop_get(NULL, fmri, SCF_GROUP_APPLICATION,
3099b56bf881SAntonello Cruz "auto_enable");
3100b56bf881SAntonello Cruz if (!prop)
3101b56bf881SAntonello Cruz return (retval);
3102b56bf881SAntonello Cruz ret = scf_simple_prop_next_boolean(prop);
3103b56bf881SAntonello Cruz retval = (*ret != 0);
3104b56bf881SAntonello Cruz scf_simple_prop_free(prop);
3105b56bf881SAntonello Cruz return (retval);
3106b56bf881SAntonello Cruz }
3107b56bf881SAntonello Cruz
3108b56bf881SAntonello Cruz /*
3109b56bf881SAntonello Cruz * Check an array of services and enable any that don't have the
3110b56bf881SAntonello Cruz * "application/auto_enable" property set to "false", which is
3111b56bf881SAntonello Cruz * the interface to turn off this behaviour (see PSARC 2004/739).
3112b56bf881SAntonello Cruz */
3113b56bf881SAntonello Cruz void
_check_services(char ** svcs)3114b56bf881SAntonello Cruz _check_services(char **svcs)
3115b56bf881SAntonello Cruz {
3116b56bf881SAntonello Cruz char *s;
3117b56bf881SAntonello Cruz
3118b56bf881SAntonello Cruz for (; *svcs; svcs++) {
3119b56bf881SAntonello Cruz if (is_auto_enabled(*svcs) == 0)
3120b56bf881SAntonello Cruz continue;
3121b56bf881SAntonello Cruz if ((s = smf_get_state(*svcs)) != NULL) {
3122b56bf881SAntonello Cruz if (strcmp(SCF_STATE_STRING_DISABLED, s) == 0)
3123b56bf881SAntonello Cruz (void) smf_enable_instance(*svcs,
3124b56bf881SAntonello Cruz SMF_TEMPORARY);
3125b56bf881SAntonello Cruz free(s);
3126b56bf881SAntonello Cruz }
3127b56bf881SAntonello Cruz }
3128b56bf881SAntonello Cruz }
3129f6e214c7SGavin Maltby
3130f6e214c7SGavin Maltby /*ARGSUSED*/
3131f6e214c7SGavin Maltby static int
str_compare(const char * s1,const char * s2,size_t n)3132f6e214c7SGavin Maltby str_compare(const char *s1, const char *s2, size_t n)
3133f6e214c7SGavin Maltby {
3134f6e214c7SGavin Maltby return (strcmp(s1, s2));
3135f6e214c7SGavin Maltby }
3136f6e214c7SGavin Maltby
3137f6e214c7SGavin Maltby static int
str_n_compare(const char * s1,const char * s2,size_t n)3138f6e214c7SGavin Maltby str_n_compare(const char *s1, const char *s2, size_t n)
3139f6e214c7SGavin Maltby {
3140f6e214c7SGavin Maltby return (strncmp(s1, s2, n));
3141f6e214c7SGavin Maltby }
3142f6e214c7SGavin Maltby
3143f6e214c7SGavin Maltby int32_t
state_from_string(const char * state,size_t l)3144f6e214c7SGavin Maltby state_from_string(const char *state, size_t l)
3145f6e214c7SGavin Maltby {
3146f6e214c7SGavin Maltby int (*str_cmp)(const char *, const char *, size_t);
3147f6e214c7SGavin Maltby
3148f6e214c7SGavin Maltby if (l == 0)
3149f6e214c7SGavin Maltby str_cmp = str_compare;
3150f6e214c7SGavin Maltby else
3151f6e214c7SGavin Maltby str_cmp = str_n_compare;
3152f6e214c7SGavin Maltby
3153f6e214c7SGavin Maltby if (str_cmp(SCF_STATE_STRING_UNINIT, state, l) == 0)
3154f6e214c7SGavin Maltby return (SCF_STATE_UNINIT);
3155f6e214c7SGavin Maltby else if (str_cmp(SCF_STATE_STRING_MAINT, state, l) == 0)
3156f6e214c7SGavin Maltby return (SCF_STATE_MAINT);
3157f6e214c7SGavin Maltby else if (str_cmp(SCF_STATE_STRING_OFFLINE, state, l) == 0)
3158f6e214c7SGavin Maltby return (SCF_STATE_OFFLINE);
3159f6e214c7SGavin Maltby else if (str_cmp(SCF_STATE_STRING_DISABLED, state, l) == 0)
3160f6e214c7SGavin Maltby return (SCF_STATE_DISABLED);
3161f6e214c7SGavin Maltby else if (str_cmp(SCF_STATE_STRING_ONLINE, state, l) == 0)
3162f6e214c7SGavin Maltby return (SCF_STATE_ONLINE);
3163f6e214c7SGavin Maltby else if (str_cmp(SCF_STATE_STRING_DEGRADED, state, l) == 0)
3164f6e214c7SGavin Maltby return (SCF_STATE_DEGRADED);
3165f6e214c7SGavin Maltby else if (str_cmp("all", state, l) == 0)
3166f6e214c7SGavin Maltby return (SCF_STATE_ALL);
3167f6e214c7SGavin Maltby else
3168f6e214c7SGavin Maltby return (-1);
3169f6e214c7SGavin Maltby }
3170f6e214c7SGavin Maltby
3171f6e214c7SGavin Maltby /*
3172f6e214c7SGavin Maltby * int32_t smf_state_from_string()
3173f6e214c7SGavin Maltby * return the value of the macro SCF_STATE_* for the corresponding state
3174f6e214c7SGavin Maltby * it returns SCF_STATE_ALL if "all" is passed. -1 if the string passed doesn't
3175f6e214c7SGavin Maltby * correspond to any valid state.
3176f6e214c7SGavin Maltby */
3177f6e214c7SGavin Maltby int32_t
smf_state_from_string(const char * state)3178f6e214c7SGavin Maltby smf_state_from_string(const char *state)
3179f6e214c7SGavin Maltby {
3180f6e214c7SGavin Maltby return (state_from_string(state, 0));
3181f6e214c7SGavin Maltby }
3182f6e214c7SGavin Maltby
3183f6e214c7SGavin Maltby /*
3184f6e214c7SGavin Maltby * smf_state_to_string()
3185f6e214c7SGavin Maltby * Takes an int32_t representing an SMF state and returns
3186f6e214c7SGavin Maltby * the corresponding string. The string is read only and need not to be
3187f6e214c7SGavin Maltby * freed.
3188f6e214c7SGavin Maltby * returns NULL on invalid input.
3189f6e214c7SGavin Maltby */
3190f6e214c7SGavin Maltby const char *
smf_state_to_string(int32_t s)3191f6e214c7SGavin Maltby smf_state_to_string(int32_t s)
3192f6e214c7SGavin Maltby {
3193f6e214c7SGavin Maltby switch (s) {
3194f6e214c7SGavin Maltby case SCF_STATE_UNINIT:
3195f6e214c7SGavin Maltby return (SCF_STATE_STRING_UNINIT);
3196f6e214c7SGavin Maltby case SCF_STATE_MAINT:
3197f6e214c7SGavin Maltby return (SCF_STATE_STRING_MAINT);
3198f6e214c7SGavin Maltby case SCF_STATE_OFFLINE:
3199f6e214c7SGavin Maltby return (SCF_STATE_STRING_OFFLINE);
3200f6e214c7SGavin Maltby case SCF_STATE_DISABLED:
3201f6e214c7SGavin Maltby return (SCF_STATE_STRING_DISABLED);
3202f6e214c7SGavin Maltby case SCF_STATE_ONLINE:
3203f6e214c7SGavin Maltby return (SCF_STATE_STRING_ONLINE);
3204f6e214c7SGavin Maltby case SCF_STATE_DEGRADED:
3205f6e214c7SGavin Maltby return (SCF_STATE_STRING_DEGRADED);
3206f6e214c7SGavin Maltby case SCF_STATE_ALL:
3207f6e214c7SGavin Maltby return ("all");
3208f6e214c7SGavin Maltby default:
3209f6e214c7SGavin Maltby return (NULL);
3210f6e214c7SGavin Maltby }
3211f6e214c7SGavin Maltby }
3212