2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include "libscf_impl.h"
2N/A
2N/A#include <assert.h>
2N/A#include <libuutil.h>
2N/A#include <stdio.h>
2N/A#include <string.h>
2N/A#include <stdlib.h>
2N/A#include <sys/param.h>
2N/A#include <errno.h>
2N/A#include <libgen.h>
2N/A#include <assert.h>
2N/A#include "midlevel_impl.h"
2N/A#include "lowlevel_impl.h"
2N/A
2N/A#ifndef NDEBUG
2N/A#define bad_error(func, err) { \
2N/A uu_warn("%s:%d: %s failed with unexpected error %d. Aborting.\n", \
2N/A __FILE__, __LINE__, func, err); \
2N/A abort(); \
2N/A}
2N/A#else
2N/A#define bad_error(func, err) abort()
2N/A#endif
2N/A
2N/A/* Path to speedy files area must end with a slash */
2N/A#define SMF_SPEEDY_FILES_PATH _PATH_SYSVOL "/"
2N/A
2N/Avoid
2N/Ascf_simple_handle_destroy(scf_simple_handle_t *simple_h)
2N/A{
2N/A if (simple_h == NULL)
2N/A return;
2N/A
2N/A scf_pg_destroy(simple_h->running_pg);
2N/A scf_pg_destroy(simple_h->editing_pg);
2N/A scf_snapshot_destroy(simple_h->snap);
2N/A scf_instance_destroy(simple_h->inst);
2N/A scf_handle_destroy(simple_h->h);
2N/A uu_free(simple_h);
2N/A}
2N/A
2N/A/*
2N/A * Given a base service FMRI and the names of a property group and property,
2N/A * assemble_fmri() merges them into a property FMRI. Note that if the base
2N/A * FMRI is NULL, assemble_fmri() gets the base FMRI from scf_myname().
2N/A */
2N/A
2N/Astatic char *
2N/Aassemble_fmri(scf_handle_t *h, const char *base, const char *pg,
2N/A const char *prop)
2N/A{
2N/A size_t fmri_sz, pglen;
2N/A ssize_t baselen;
2N/A char *fmri_buf;
2N/A
2N/A if (prop == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
2N/A return (NULL);
2N/A }
2N/A
2N/A if (pg == NULL)
2N/A pglen = strlen(SCF_PG_APP_DEFAULT);
2N/A else
2N/A pglen = strlen(pg);
2N/A
2N/A if (base == NULL) {
2N/A if ((baselen = scf_myname(h, NULL, 0)) == -1)
2N/A return (NULL);
2N/A } else {
2N/A baselen = strlen(base);
2N/A }
2N/A
2N/A fmri_sz = baselen + sizeof (SCF_FMRI_PROPERTYGRP_PREFIX) - 1 +
2N/A pglen + sizeof (SCF_FMRI_PROPERTY_PREFIX) - 1 +
2N/A strlen(prop) + 1;
2N/A
2N/A if ((fmri_buf = malloc(fmri_sz)) == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2N/A return (NULL);
2N/A }
2N/A
2N/A if (base == NULL) {
2N/A if (scf_myname(h, fmri_buf, fmri_sz) == -1) {
2N/A free(fmri_buf);
2N/A return (NULL);
2N/A }
2N/A } else {
2N/A (void) strcpy(fmri_buf, base);
2N/A }
2N/A
2N/A (void) strcat(fmri_buf, SCF_FMRI_PROPERTYGRP_PREFIX);
2N/A
2N/A if (pg == NULL)
2N/A (void) strcat(fmri_buf, SCF_PG_APP_DEFAULT);
2N/A else
2N/A (void) strcat(fmri_buf, pg);
2N/A
2N/A (void) strcat(fmri_buf, SCF_FMRI_PROPERTY_PREFIX);
2N/A (void) strcat(fmri_buf, prop);
2N/A return (fmri_buf);
2N/A}
2N/A
2N/A/*
2N/A * Given a property, this function allocates and fills an scf_simple_prop_t
2N/A * with the data it contains.
2N/A */
2N/A
2N/Astatic scf_simple_prop_t *
2N/Afill_prop(scf_property_t *prop, const char *pgname, const char *propname,
2N/A scf_handle_t *h)
2N/A{
2N/A scf_simple_prop_t *ret;
2N/A scf_iter_t *iter;
2N/A scf_value_t *val;
2N/A int iterret, i;
2N/A ssize_t valsize, numvals;
2N/A union scf_simple_prop_val *vallist = NULL, *vallist_backup = NULL;
2N/A
2N/A if ((ret = malloc(sizeof (*ret))) == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2N/A return (NULL);
2N/A }
2N/A
2N/A ret->pr_next = NULL;
2N/A ret->pr_pg = NULL;
2N/A ret->pr_iter = 0;
2N/A
2N/A if (pgname == NULL)
2N/A ret->pr_pgname = strdup(SCF_PG_APP_DEFAULT);
2N/A else
2N/A ret->pr_pgname = strdup(pgname);
2N/A
2N/A if (ret->pr_pgname == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2N/A free(ret);
2N/A return (NULL);
2N/A }
2N/A
2N/A if ((ret->pr_propname = strdup(propname)) == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2N/A free(ret->pr_pgname);
2N/A free(ret);
2N/A return (NULL);
2N/A }
2N/A
2N/A if (scf_property_type(prop, &ret->pr_type) == -1)
2N/A goto error3;
2N/A
2N/A if ((iter = scf_iter_create(h)) == NULL)
2N/A goto error3;
2N/A if ((val = scf_value_create(h)) == NULL) {
2N/A scf_iter_destroy(iter);
2N/A goto error3;
2N/A }
2N/A
2N/A if (scf_iter_property_values(iter, prop) == -1)
2N/A goto error1;
2N/A
2N/A for (numvals = 0; (iterret = scf_iter_next_value(iter, val)) == 1;
2N/A numvals++) {
2N/A vallist_backup = vallist;
2N/A if ((vallist = realloc(vallist, (numvals + 1) *
2N/A sizeof (*vallist))) == NULL) {
2N/A vallist = vallist_backup;
2N/A goto error1;
2N/A }
2N/A
2N/A switch (ret->pr_type) {
2N/A case SCF_TYPE_BOOLEAN:
2N/A if (scf_value_get_boolean(val,
2N/A &vallist[numvals].pv_bool) == -1)
2N/A goto error1;
2N/A break;
2N/A
2N/A case SCF_TYPE_COUNT:
2N/A if (scf_value_get_count(val,
2N/A &vallist[numvals].pv_uint) == -1)
2N/A goto error1;
2N/A break;
2N/A
2N/A case SCF_TYPE_INTEGER:
2N/A if (scf_value_get_integer(val,
2N/A &vallist[numvals].pv_int) == -1)
2N/A goto error1;
2N/A break;
2N/A
2N/A case SCF_TYPE_TIME:
2N/A if (scf_value_get_time(val,
2N/A &vallist[numvals].pv_time.t_sec,
2N/A &vallist[numvals].pv_time.t_nsec) == -1)
2N/A goto error1;
2N/A break;
2N/A
2N/A case SCF_TYPE_ASTRING:
2N/A vallist[numvals].pv_str = NULL;
2N/A if ((valsize = scf_value_get_astring(val, NULL, 0)) ==
2N/A -1)
2N/A goto error1;
2N/A if ((vallist[numvals].pv_str = malloc(valsize+1)) ==
2N/A NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2N/A goto error1;
2N/A }
2N/A if (scf_value_get_astring(val,
2N/A vallist[numvals].pv_str, valsize+1) == -1) {
2N/A free(vallist[numvals].pv_str);
2N/A goto error1;
2N/A }
2N/A break;
2N/A
2N/A case SCF_TYPE_USTRING:
2N/A case SCF_TYPE_HOST:
2N/A case SCF_TYPE_HOSTNAME:
2N/A case SCF_TYPE_NET_ADDR:
2N/A case SCF_TYPE_NET_ADDR_V4:
2N/A case SCF_TYPE_NET_ADDR_V6:
2N/A case SCF_TYPE_URI:
2N/A case SCF_TYPE_FMRI:
2N/A vallist[numvals].pv_str = NULL;
2N/A if ((valsize = scf_value_get_ustring(val, NULL, 0)) ==
2N/A -1)
2N/A goto error1;
2N/A if ((vallist[numvals].pv_str = malloc(valsize+1)) ==
2N/A NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2N/A goto error1;
2N/A }
2N/A if (scf_value_get_ustring(val,
2N/A vallist[numvals].pv_str, valsize+1) == -1) {
2N/A free(vallist[numvals].pv_str);
2N/A goto error1;
2N/A }
2N/A break;
2N/A
2N/A case SCF_TYPE_OPAQUE:
2N/A vallist[numvals].pv_opaque.o_value = NULL;
2N/A if (scf_value_get_opaque(val, NULL, 0) == -1)
2N/A goto error1;
2N/A if ((vallist[numvals].pv_opaque.o_value =
2N/A malloc(val->value_size)) == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2N/A goto error1;
2N/A }
2N/A vallist[numvals].pv_opaque.o_size = val->value_size;
2N/A if (scf_value_get_opaque(val,
2N/A vallist[numvals].pv_opaque.o_value,
2N/A vallist[numvals].pv_opaque.o_size) == -1) {
2N/A free(vallist[numvals].pv_opaque.o_value);
2N/A goto error1;
2N/A }
2N/A break;
2N/A
2N/A default:
2N/A (void) scf_set_error(SCF_ERROR_INTERNAL);
2N/A goto error1;
2N/A
2N/A }
2N/A }
2N/A
2N/A if (iterret == -1) {
2N/A int err = scf_error();
2N/A if (err != SCF_ERROR_CONNECTION_BROKEN &&
2N/A err != SCF_ERROR_PERMISSION_DENIED)
2N/A (void) scf_set_error(SCF_ERROR_INTERNAL);
2N/A goto error1;
2N/A }
2N/A
2N/A ret->pr_vallist = vallist;
2N/A ret->pr_numvalues = numvals;
2N/A
2N/A scf_iter_destroy(iter);
2N/A (void) scf_value_destroy(val);
2N/A
2N/A return (ret);
2N/A
2N/A /*
2N/A * Exit point for a successful call. Below this line are exit points
2N/A * for failures at various stages during the function.
2N/A */
2N/A
2N/Aerror1:
2N/A if (vallist == NULL)
2N/A goto error2;
2N/A
2N/A switch (ret->pr_type) {
2N/A case SCF_TYPE_ASTRING:
2N/A case SCF_TYPE_USTRING:
2N/A case SCF_TYPE_HOST:
2N/A case SCF_TYPE_HOSTNAME:
2N/A case SCF_TYPE_NET_ADDR:
2N/A case SCF_TYPE_NET_ADDR_V4:
2N/A case SCF_TYPE_NET_ADDR_V6:
2N/A case SCF_TYPE_URI:
2N/A case SCF_TYPE_FMRI: {
2N/A for (i = 0; i < numvals; i++) {
2N/A free(vallist[i].pv_str);
2N/A }
2N/A break;
2N/A }
2N/A case SCF_TYPE_OPAQUE: {
2N/A for (i = 0; i < numvals; i++) {
2N/A free(vallist[i].pv_opaque.o_value);
2N/A }
2N/A break;
2N/A }
2N/A default:
2N/A break;
2N/A }
2N/A
2N/A free(vallist);
2N/A
2N/Aerror2:
2N/A scf_iter_destroy(iter);
2N/A (void) scf_value_destroy(val);
2N/A
2N/Aerror3:
2N/A free(ret->pr_pgname);
2N/A free(ret->pr_propname);
2N/A free(ret);
2N/A return (NULL);
2N/A}
2N/A
2N/A/*
2N/A * insert_app_props iterates over a property iterator, getting all the
2N/A * properties from a property group, and adding or overwriting them into
2N/A * a simple_app_props_t. This is used by scf_simple_app_props_get to provide
2N/A * service/instance composition while filling the app_props_t.
2N/A * insert_app_props iterates over a single property group.
2N/A */
2N/A
2N/Astatic int
2N/Ainsert_app_props(scf_iter_t *propiter, char *pgname, char *propname, struct
2N/A scf_simple_pg *thispg, scf_property_t *prop, size_t namelen,
2N/A scf_handle_t *h)
2N/A{
2N/A scf_simple_prop_t *thisprop, *prevprop, *newprop;
2N/A uint8_t found;
2N/A int propiter_ret;
2N/A
2N/A while ((propiter_ret = scf_iter_next_property(propiter, prop)) == 1) {
2N/A
2N/A if (scf_property_get_name(prop, propname, namelen) < 0) {
2N/A if (scf_error() == SCF_ERROR_NOT_SET)
2N/A (void) scf_set_error(SCF_ERROR_INTERNAL);
2N/A return (-1);
2N/A }
2N/A
2N/A thisprop = thispg->pg_proplist;
2N/A prevprop = thispg->pg_proplist;
2N/A found = 0;
2N/A
2N/A while ((thisprop != NULL) && (!found)) {
2N/A if (strcmp(thisprop->pr_propname, propname) == 0) {
2N/A found = 1;
2N/A if ((newprop = fill_prop(prop, pgname,
2N/A propname, h)) == NULL)
2N/A return (-1);
2N/A
2N/A if (thisprop == thispg->pg_proplist)
2N/A thispg->pg_proplist = newprop;
2N/A else
2N/A prevprop->pr_next = newprop;
2N/A
2N/A newprop->pr_pg = thispg;
2N/A newprop->pr_next = thisprop->pr_next;
2N/A scf_simple_prop_free(thisprop);
2N/A thisprop = NULL;
2N/A } else {
2N/A if (thisprop != thispg->pg_proplist)
2N/A prevprop = prevprop->pr_next;
2N/A thisprop = thisprop->pr_next;
2N/A }
2N/A }
2N/A
2N/A if (!found) {
2N/A if ((newprop = fill_prop(prop, pgname, propname, h)) ==
2N/A NULL)
2N/A return (-1);
2N/A
2N/A if (thispg->pg_proplist == NULL)
2N/A thispg->pg_proplist = newprop;
2N/A else
2N/A prevprop->pr_next = newprop;
2N/A
2N/A newprop->pr_pg = thispg;
2N/A }
2N/A }
2N/A
2N/A if (propiter_ret == -1) {
2N/A if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2N/A (void) scf_set_error(SCF_ERROR_INTERNAL);
2N/A return (-1);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Sets up e in tx to set pname's values. Returns 0 on success or -1 on
2N/A * failure, with scf_error() set to
2N/A * SCF_ERROR_HANDLE_MISMATCH - tx & e are derived from different handles
2N/A * SCF_ERROR_INVALID_ARGUMENT - pname or ty are invalid
2N/A * SCF_ERROR_NOT_BOUND - handle is not bound
2N/A * SCF_ERROR_CONNECTION_BROKEN - connection was broken
2N/A * SCF_ERROR_NOT_SET - tx has not been started
2N/A * SCF_ERROR_DELETED - the pg tx was started on was deleted
2N/A */
2N/Astatic int
2N/Atransaction_property_set(scf_transaction_t *tx, scf_transaction_entry_t *e,
2N/A const char *pname, scf_type_t ty)
2N/A{
2N/A for (;;) {
2N/A if (scf_transaction_property_change_type(tx, e, pname, ty) == 0)
2N/A return (0);
2N/A
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_HANDLE_MISMATCH:
2N/A case SCF_ERROR_INVALID_ARGUMENT:
2N/A case SCF_ERROR_NOT_BOUND:
2N/A case SCF_ERROR_CONNECTION_BROKEN:
2N/A case SCF_ERROR_NOT_SET:
2N/A case SCF_ERROR_DELETED:
2N/A default:
2N/A return (-1);
2N/A
2N/A case SCF_ERROR_NOT_FOUND:
2N/A break;
2N/A }
2N/A
2N/A if (scf_transaction_property_new(tx, e, pname, ty) == 0)
2N/A return (0);
2N/A
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_HANDLE_MISMATCH:
2N/A case SCF_ERROR_INVALID_ARGUMENT:
2N/A case SCF_ERROR_NOT_BOUND:
2N/A case SCF_ERROR_CONNECTION_BROKEN:
2N/A case SCF_ERROR_NOT_SET:
2N/A case SCF_ERROR_DELETED:
2N/A default:
2N/A return (-1);
2N/A
2N/A case SCF_ERROR_EXISTS:
2N/A break;
2N/A }
2N/A }
2N/A}
2N/A
2N/Astatic int
2N/Aget_inst_enabled(const scf_instance_t *inst, const char *pgname)
2N/A{
2N/A scf_propertygroup_t *gpg = NULL;
2N/A scf_property_t *eprop = NULL;
2N/A scf_value_t *v = NULL;
2N/A scf_handle_t *h = NULL;
2N/A uint8_t enabled;
2N/A int ret = -1;
2N/A
2N/A if ((h = scf_instance_handle(inst)) == NULL)
2N/A return (-1);
2N/A
2N/A if ((gpg = scf_pg_create(h)) == NULL ||
2N/A (eprop = scf_property_create(h)) == NULL ||
2N/A (v = scf_value_create(h)) == NULL)
2N/A goto out;
2N/A
2N/A if (scf_instance_get_pg(inst, pgname, gpg) ||
2N/A scf_pg_get_property(gpg, SCF_PROPERTY_ENABLED, eprop) ||
2N/A scf_property_get_value(eprop, v) ||
2N/A scf_value_get_boolean(v, &enabled))
2N/A goto out;
2N/A ret = enabled;
2N/A
2N/Aout:
2N/A scf_pg_destroy(gpg);
2N/A scf_property_destroy(eprop);
2N/A scf_value_destroy(v);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * set_inst_enabled() is a "master" enable/disable call that takes the
2N/A * instance and the desired state for the enabled bit in the instance's
2N/A * named property group. If the group doesn't exist, it's created with the
2N/A * given flags. Called by smf_{dis,en}able_instance().
2N/A */
2N/Astatic int
2N/Aset_inst_enabled(const scf_instance_t *inst, uint8_t desired,
2N/A const char *pgname, uint32_t pgflags)
2N/A{
2N/A scf_transaction_t *tx = NULL;
2N/A scf_transaction_entry_t *ent = NULL;
2N/A scf_propertygroup_t *gpg = NULL;
2N/A scf_property_t *eprop = NULL;
2N/A scf_value_t *v = NULL;
2N/A scf_handle_t *h = NULL;
2N/A int ret = -1;
2N/A int committed;
2N/A uint8_t b;
2N/A
2N/A if ((h = scf_instance_handle(inst)) == NULL)
2N/A return (-1);
2N/A
2N/A if ((gpg = scf_pg_create(h)) == NULL ||
2N/A (eprop = scf_property_create(h)) == NULL ||
2N/A (v = scf_value_create(h)) == NULL ||
2N/A (tx = scf_transaction_create(h)) == NULL ||
2N/A (ent = scf_entry_create(h)) == NULL)
2N/A goto out;
2N/A
2N/Ageneral_pg_get:
2N/A if (scf_instance_get_pg(inst, SCF_PG_GENERAL, gpg) == -1) {
2N/A if (scf_error() != SCF_ERROR_NOT_FOUND)
2N/A goto out;
2N/A
2N/A if (scf_instance_add_pg(inst, SCF_PG_GENERAL,
2N/A SCF_GROUP_FRAMEWORK, SCF_PG_GENERAL_FLAGS, gpg) == -1) {
2N/A if (scf_error() != SCF_ERROR_EXISTS)
2N/A goto out;
2N/A goto general_pg_get;
2N/A }
2N/A }
2N/A
2N/A if (strcmp(pgname, SCF_PG_GENERAL) != 0) {
2N/Aget:
2N/A if (scf_instance_get_pg(inst, pgname, gpg) == -1) {
2N/A if (scf_error() != SCF_ERROR_NOT_FOUND)
2N/A goto out;
2N/A
2N/A if (scf_instance_add_pg(inst, pgname,
2N/A SCF_GROUP_FRAMEWORK, pgflags, gpg) == -1) {
2N/A if (scf_error() != SCF_ERROR_EXISTS)
2N/A goto out;
2N/A goto get;
2N/A }
2N/A }
2N/A }
2N/A
2N/A if (scf_pg_get_property(gpg, SCF_PROPERTY_ENABLED, eprop) == -1) {
2N/A if (scf_error() != SCF_ERROR_NOT_FOUND)
2N/A goto out;
2N/A else
2N/A goto set;
2N/A }
2N/A
2N/A /*
2N/A * If it's already set the way we want, forgo the transaction.
2N/A */
2N/A if (scf_property_get_value(eprop, v) == -1) {
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_CONSTRAINT_VIOLATED:
2N/A case SCF_ERROR_NOT_FOUND:
2N/A /* Misconfigured, so set anyway. */
2N/A goto set;
2N/A
2N/A default:
2N/A goto out;
2N/A }
2N/A }
2N/A if (scf_value_get_boolean(v, &b) == -1) {
2N/A if (scf_error() != SCF_ERROR_TYPE_MISMATCH)
2N/A goto out;
2N/A goto set;
2N/A }
2N/A if (b == desired) {
2N/A ret = 0;
2N/A goto out;
2N/A }
2N/A
2N/Aset:
2N/A do {
2N/A if (scf_transaction_start(tx, gpg) == -1)
2N/A goto out;
2N/A
2N/A if (transaction_property_set(tx, ent, SCF_PROPERTY_ENABLED,
2N/A SCF_TYPE_BOOLEAN) != 0) {
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_CONNECTION_BROKEN:
2N/A case SCF_ERROR_DELETED:
2N/A default:
2N/A goto out;
2N/A
2N/A case SCF_ERROR_HANDLE_MISMATCH:
2N/A case SCF_ERROR_INVALID_ARGUMENT:
2N/A case SCF_ERROR_NOT_BOUND:
2N/A case SCF_ERROR_NOT_SET:
2N/A bad_error("transaction_property_set",
2N/A scf_error());
2N/A }
2N/A }
2N/A
2N/A scf_value_set_boolean(v, desired);
2N/A if (scf_entry_add_value(ent, v) == -1)
2N/A goto out;
2N/A
2N/A committed = scf_transaction_commit(tx);
2N/A if (committed == -1)
2N/A goto out;
2N/A
2N/A scf_transaction_reset(tx);
2N/A
2N/A if (committed == 0) { /* out-of-sync */
2N/A if (scf_pg_update(gpg) == -1)
2N/A goto out;
2N/A }
2N/A } while (committed == 0);
2N/A
2N/A ret = 0;
2N/A
2N/Aout:
2N/A scf_value_destroy(v);
2N/A scf_entry_destroy(ent);
2N/A scf_transaction_destroy(tx);
2N/A scf_property_destroy(eprop);
2N/A scf_pg_destroy(gpg);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/Astatic int
2N/Adelete_inst_enabled(const scf_instance_t *inst, const char *pgname)
2N/A{
2N/A scf_transaction_t *tx = NULL;
2N/A scf_transaction_entry_t *ent = NULL;
2N/A scf_propertygroup_t *gpg = NULL;
2N/A scf_handle_t *h = NULL;
2N/A int ret = -1;
2N/A int committed;
2N/A
2N/A if ((h = scf_instance_handle(inst)) == NULL)
2N/A return (-1);
2N/A
2N/A if ((gpg = scf_pg_create(h)) == NULL ||
2N/A (tx = scf_transaction_create(h)) == NULL ||
2N/A (ent = scf_entry_create(h)) == NULL)
2N/A goto out;
2N/A
2N/A if (scf_instance_get_pg(inst, pgname, gpg) != 0)
2N/A goto error;
2N/A do {
2N/A if (scf_transaction_start(tx, gpg) == -1 ||
2N/A scf_transaction_property_delete(tx, ent,
2N/A SCF_PROPERTY_ENABLED) == -1 ||
2N/A (committed = scf_transaction_commit(tx)) == -1)
2N/A goto error;
2N/A
2N/A scf_transaction_reset(tx);
2N/A
2N/A if (committed == 0 && scf_pg_update(gpg) == -1)
2N/A goto error;
2N/A } while (committed == 0);
2N/A
2N/A ret = 0;
2N/A goto out;
2N/A
2N/Aerror:
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_DELETED:
2N/A case SCF_ERROR_NOT_FOUND:
2N/A /* success */
2N/A ret = 0;
2N/A }
2N/A
2N/Aout:
2N/A scf_entry_destroy(ent);
2N/A scf_transaction_destroy(tx);
2N/A scf_pg_destroy(gpg);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Returns 0 on success or -1 on failure. On failure leaves scf_error() set to
2N/A * SCF_ERROR_HANDLE_DESTROYED - inst's handle has been destroyed
2N/A * SCF_ERROR_NOT_BOUND - inst's handle is not bound
2N/A * SCF_ERROR_CONNECTION_BROKEN - the repository connection was broken
2N/A * SCF_ERROR_NOT_SET - inst is not set
2N/A * SCF_ERROR_DELETED - inst was deleted
2N/A * SCF_ERROR_PERMISSION_DENIED
2N/A * SCF_ERROR_BACKEND_ACCESS
2N/A * SCF_ERROR_BACKEND_READONLY
2N/A */
2N/Astatic int
2N/Aset_inst_action_inst(scf_instance_t *inst, const char *action)
2N/A{
2N/A scf_handle_t *h;
2N/A scf_transaction_t *tx = NULL;
2N/A scf_transaction_entry_t *ent = NULL;
2N/A scf_propertygroup_t *pg = NULL;
2N/A scf_property_t *prop = NULL;
2N/A scf_value_t *v = NULL;
2N/A int trans, ret = -1;
2N/A int64_t t;
2N/A hrtime_t timestamp;
2N/A
2N/A if ((h = scf_instance_handle(inst)) == NULL ||
2N/A (pg = scf_pg_create(h)) == NULL ||
2N/A (prop = scf_property_create(h)) == NULL ||
2N/A (v = scf_value_create(h)) == NULL ||
2N/A (tx = scf_transaction_create(h)) == NULL ||
2N/A (ent = scf_entry_create(h)) == NULL)
2N/A goto out;
2N/A
2N/Aget:
2N/A if (scf_instance_get_pg(inst, SCF_PG_RESTARTER_ACTIONS, pg) == -1) {
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_NOT_BOUND:
2N/A case SCF_ERROR_CONNECTION_BROKEN:
2N/A case SCF_ERROR_NOT_SET:
2N/A case SCF_ERROR_DELETED:
2N/A default:
2N/A goto out;
2N/A
2N/A case SCF_ERROR_NOT_FOUND:
2N/A break;
2N/A
2N/A case SCF_ERROR_HANDLE_MISMATCH:
2N/A case SCF_ERROR_INVALID_ARGUMENT:
2N/A bad_error("scf_instance_get_pg", scf_error());
2N/A }
2N/A
2N/A /* Try creating the restarter_actions property group. */
2N/Aadd:
2N/A if (scf_instance_add_pg(inst, SCF_PG_RESTARTER_ACTIONS,
2N/A SCF_PG_RESTARTER_ACTIONS_TYPE,
2N/A SCF_PG_RESTARTER_ACTIONS_FLAGS, pg) == -1) {
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_NOT_BOUND:
2N/A case SCF_ERROR_CONNECTION_BROKEN:
2N/A case SCF_ERROR_NOT_SET:
2N/A case SCF_ERROR_DELETED:
2N/A case SCF_ERROR_PERMISSION_DENIED:
2N/A case SCF_ERROR_BACKEND_ACCESS:
2N/A case SCF_ERROR_BACKEND_READONLY:
2N/A default:
2N/A goto out;
2N/A
2N/A case SCF_ERROR_EXISTS:
2N/A goto get;
2N/A
2N/A case SCF_ERROR_HANDLE_MISMATCH:
2N/A case SCF_ERROR_INVALID_ARGUMENT:
2N/A bad_error("scf_instance_add_pg", scf_error());
2N/A }
2N/A }
2N/A }
2N/A
2N/A for (;;) {
2N/A timestamp = gethrtime();
2N/A
2N/A if (scf_pg_get_property(pg, action, prop) != 0) {
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_CONNECTION_BROKEN:
2N/A default:
2N/A goto out;
2N/A
2N/A case SCF_ERROR_DELETED:
2N/A goto add;
2N/A
2N/A case SCF_ERROR_NOT_FOUND:
2N/A break;
2N/A
2N/A case SCF_ERROR_HANDLE_MISMATCH:
2N/A case SCF_ERROR_INVALID_ARGUMENT:
2N/A case SCF_ERROR_NOT_BOUND:
2N/A case SCF_ERROR_NOT_SET:
2N/A bad_error("scf_pg_get_property", scf_error());
2N/A }
2N/A } else if (scf_property_get_value(prop, v) != 0) {
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_CONNECTION_BROKEN:
2N/A default:
2N/A goto out;
2N/A
2N/A case SCF_ERROR_DELETED:
2N/A goto add;
2N/A
2N/A case SCF_ERROR_CONSTRAINT_VIOLATED:
2N/A case SCF_ERROR_NOT_FOUND:
2N/A break;
2N/A
2N/A case SCF_ERROR_HANDLE_MISMATCH:
2N/A case SCF_ERROR_NOT_BOUND:
2N/A case SCF_ERROR_NOT_SET:
2N/A bad_error("scf_property_get_value",
2N/A scf_error());
2N/A }
2N/A } else if (scf_value_get_integer(v, &t) != 0) {
2N/A bad_error("scf_value_get_integer", scf_error());
2N/A } else if (t > timestamp) {
2N/A break;
2N/A }
2N/A
2N/A if (scf_transaction_start(tx, pg) == -1) {
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_NOT_BOUND:
2N/A case SCF_ERROR_CONNECTION_BROKEN:
2N/A case SCF_ERROR_PERMISSION_DENIED:
2N/A case SCF_ERROR_BACKEND_ACCESS:
2N/A case SCF_ERROR_BACKEND_READONLY:
2N/A default:
2N/A goto out;
2N/A
2N/A case SCF_ERROR_DELETED:
2N/A goto add;
2N/A
2N/A case SCF_ERROR_HANDLE_MISMATCH:
2N/A case SCF_ERROR_NOT_SET:
2N/A case SCF_ERROR_IN_USE:
2N/A bad_error("scf_transaction_start", scf_error());
2N/A }
2N/A }
2N/A
2N/A if (transaction_property_set(tx, ent, action,
2N/A SCF_TYPE_INTEGER) != 0) {
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_NOT_BOUND:
2N/A case SCF_ERROR_CONNECTION_BROKEN:
2N/A case SCF_ERROR_DELETED:
2N/A default:
2N/A goto out;
2N/A
2N/A case SCF_ERROR_HANDLE_MISMATCH:
2N/A case SCF_ERROR_INVALID_ARGUMENT:
2N/A case SCF_ERROR_NOT_SET:
2N/A bad_error("transaction_property_set",
2N/A scf_error());
2N/A }
2N/A }
2N/A
2N/A scf_value_set_integer(v, timestamp);
2N/A if (scf_entry_add_value(ent, v) == -1)
2N/A bad_error("scf_entry_add_value", scf_error());
2N/A
2N/A trans = scf_transaction_commit(tx);
2N/A if (trans == 1)
2N/A break;
2N/A
2N/A if (trans != 0) {
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_CONNECTION_BROKEN:
2N/A case SCF_ERROR_PERMISSION_DENIED:
2N/A case SCF_ERROR_BACKEND_ACCESS:
2N/A case SCF_ERROR_BACKEND_READONLY:
2N/A default:
2N/A goto out;
2N/A
2N/A case SCF_ERROR_DELETED:
2N/A scf_transaction_reset(tx);
2N/A goto add;
2N/A
2N/A case SCF_ERROR_INVALID_ARGUMENT:
2N/A case SCF_ERROR_NOT_BOUND:
2N/A case SCF_ERROR_NOT_SET:
2N/A bad_error("scf_transaction_commit",
2N/A scf_error());
2N/A }
2N/A }
2N/A
2N/A scf_transaction_reset(tx);
2N/A if (scf_pg_update(pg) == -1) {
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_CONNECTION_BROKEN:
2N/A default:
2N/A goto out;
2N/A
2N/A case SCF_ERROR_DELETED:
2N/A goto add;
2N/A
2N/A case SCF_ERROR_NOT_SET:
2N/A case SCF_ERROR_NOT_BOUND:
2N/A bad_error("scf_pg_update", scf_error());
2N/A }
2N/A }
2N/A }
2N/A
2N/A ret = 0;
2N/A
2N/Aout:
2N/A scf_value_destroy(v);
2N/A scf_entry_destroy(ent);
2N/A scf_transaction_destroy(tx);
2N/A scf_property_destroy(prop);
2N/A scf_pg_destroy(pg);
2N/A return (ret);
2N/A}
2N/A
2N/Astatic int
2N/Aset_inst_action(const char *fmri, const char *action)
2N/A{
2N/A scf_handle_t *h;
2N/A scf_instance_t *inst;
2N/A int ret = -1;
2N/A
2N/A h = _scf_handle_create_and_bind(SCF_VERSION);
2N/A if (h == NULL)
2N/A return (-1);
2N/A
2N/A inst = scf_instance_create(h);
2N/A
2N/A if (inst != NULL) {
2N/A if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
2N/A NULL, SCF_DECODE_FMRI_EXACT) == 0) {
2N/A ret = set_inst_action_inst(inst, action);
2N/A if (ret == -1 && scf_error() == SCF_ERROR_DELETED)
2N/A (void) scf_set_error(SCF_ERROR_NOT_FOUND);
2N/A } else {
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_CONSTRAINT_VIOLATED:
2N/A (void) scf_set_error(
2N/A SCF_ERROR_INVALID_ARGUMENT);
2N/A break;
2N/A case SCF_ERROR_DELETED:
2N/A (void) scf_set_error(SCF_ERROR_NOT_FOUND);
2N/A break;
2N/A }
2N/A }
2N/A
2N/A scf_instance_destroy(inst);
2N/A }
2N/A
2N/A scf_handle_destroy(h);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * get_inst_state() gets the state string from an instance, and returns
2N/A * the SCF_STATE_* constant that coincides with the instance's current state.
2N/A */
2N/A
2N/Astatic int
2N/Aget_inst_state(scf_instance_t *inst, scf_handle_t *h)
2N/A{
2N/A scf_propertygroup_t *pg = NULL;
2N/A scf_property_t *prop = NULL;
2N/A scf_value_t *val = NULL;
2N/A char state[MAX_SCF_STATE_STRING_SZ];
2N/A int ret = -1;
2N/A
2N/A if (((pg = scf_pg_create(h)) == NULL) ||
2N/A ((prop = scf_property_create(h)) == NULL) ||
2N/A ((val = scf_value_create(h)) == NULL))
2N/A goto out;
2N/A
2N/A /* Pull the state property from the instance */
2N/A
2N/A if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) == -1 ||
2N/A scf_pg_get_property(pg, SCF_PROPERTY_STATE, prop) == -1 ||
2N/A scf_property_get_value(prop, val) == -1) {
2N/A if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2N/A (void) scf_set_error(SCF_ERROR_INTERNAL);
2N/A goto out;
2N/A }
2N/A
2N/A if (scf_value_get_astring(val, state, sizeof (state)) <= 0) {
2N/A (void) scf_set_error(SCF_ERROR_INTERNAL);
2N/A goto out;
2N/A }
2N/A
2N/A if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0) {
2N/A ret = SCF_STATE_UNINIT;
2N/A } else if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
2N/A ret = SCF_STATE_MAINT;
2N/A } else if (strcmp(state, SCF_STATE_STRING_OFFLINE) == 0) {
2N/A ret = SCF_STATE_OFFLINE;
2N/A } else if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0) {
2N/A ret = SCF_STATE_DISABLED;
2N/A } else if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0) {
2N/A ret = SCF_STATE_ONLINE;
2N/A } else if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 0) {
2N/A ret = SCF_STATE_DEGRADED;
2N/A }
2N/A
2N/Aout:
2N/A scf_pg_destroy(pg);
2N/A scf_property_destroy(prop);
2N/A (void) scf_value_destroy(val);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Sets an instance to be enabled or disabled after reboot, using the
2N/A * temporary (overriding) general_ovr property group to reflect the
2N/A * present state, if it is different.
2N/A */
2N/Astatic int
2N/Aset_inst_enabled_atboot(scf_instance_t *inst, uint8_t desired)
2N/A{
2N/A int enabled;
2N/A int persistent;
2N/A int ret = -1;
2N/A
2N/A if ((persistent = get_inst_enabled(inst, SCF_PG_GENERAL)) < 0) {
2N/A if (scf_error() != SCF_ERROR_NOT_FOUND)
2N/A goto out;
2N/A persistent = B_FALSE;
2N/A }
2N/A if ((enabled = get_inst_enabled(inst, SCF_PG_GENERAL_OVR)) < 0) {
2N/A enabled = persistent;
2N/A if (persistent != desired) {
2N/A /*
2N/A * Temporarily store the present enabled state.
2N/A */
2N/A if (set_inst_enabled(inst, persistent,
2N/A SCF_PG_GENERAL_OVR, SCF_PG_GENERAL_OVR_FLAGS))
2N/A goto out;
2N/A }
2N/A }
2N/A if (persistent != desired)
2N/A if (set_inst_enabled(inst, desired, SCF_PG_GENERAL,
2N/A SCF_PG_GENERAL_FLAGS))
2N/A goto out;
2N/A if (enabled == desired)
2N/A ret = delete_inst_enabled(inst, SCF_PG_GENERAL_OVR);
2N/A else
2N/A ret = 0;
2N/A
2N/Aout:
2N/A return (ret);
2N/A}
2N/A
2N/Astatic int
2N/Aset_inst_enabled_flags(const char *fmri, int flags, uint8_t desired)
2N/A{
2N/A int ret = -1;
2N/A scf_handle_t *h;
2N/A scf_instance_t *inst;
2N/A
2N/A if (flags & ~(SMF_TEMPORARY | SMF_AT_NEXT_BOOT) ||
2N/A flags & SMF_TEMPORARY && flags & SMF_AT_NEXT_BOOT) {
2N/A (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
2N/A return (ret);
2N/A }
2N/A
2N/A if ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL)
2N/A return (ret);
2N/A
2N/A if ((inst = scf_instance_create(h)) == NULL) {
2N/A scf_handle_destroy(h);
2N/A return (ret);
2N/A }
2N/A
2N/A if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL, NULL,
2N/A SCF_DECODE_FMRI_EXACT) == -1) {
2N/A if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
2N/A (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
2N/A goto out;
2N/A }
2N/A
2N/A if (flags & SMF_AT_NEXT_BOOT) {
2N/A ret = set_inst_enabled_atboot(inst, desired);
2N/A } else {
2N/A if (set_inst_enabled(inst, desired, flags & SMF_TEMPORARY ?
2N/A SCF_PG_GENERAL_OVR : SCF_PG_GENERAL, flags & SMF_TEMPORARY ?
2N/A SCF_PG_GENERAL_OVR_FLAGS : SCF_PG_GENERAL_FLAGS))
2N/A goto out;
2N/A
2N/A /*
2N/A * Make the persistent value effective by deleting the
2N/A * temporary one.
2N/A */
2N/A if (flags & SMF_TEMPORARY)
2N/A ret = 0;
2N/A else
2N/A ret = delete_inst_enabled(inst, SCF_PG_GENERAL_OVR);
2N/A }
2N/A
2N/Aout:
2N/A scf_instance_destroy(inst);
2N/A scf_handle_destroy(h);
2N/A if (ret == -1 && scf_error() == SCF_ERROR_DELETED)
2N/A (void) scf_set_error(SCF_ERROR_NOT_FOUND);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Create and return a pg from the instance associated with the given handle.
2N/A * This function is only called in scf_transaction_setup and
2N/A * scf_transaction_restart where the h->rh_instance pointer is properly filled
2N/A * in by scf_general_setup_pg().
2N/A */
2N/Astatic scf_propertygroup_t *
2N/Aget_instance_pg(scf_simple_handle_t *simple_h)
2N/A{
2N/A scf_propertygroup_t *ret_pg = scf_pg_create(simple_h->h);
2N/A char *pg_name;
2N/A ssize_t namelen;
2N/A
2N/A if (ret_pg == NULL) {
2N/A return (NULL);
2N/A }
2N/A
2N/A namelen = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) + 1;
2N/A assert(namelen > 0);
2N/A
2N/A if ((pg_name = malloc(namelen)) == NULL) {
2N/A if (scf_error() == SCF_ERROR_NOT_SET) {
2N/A (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2N/A }
2N/A return (NULL);
2N/A }
2N/A
2N/A if (scf_pg_get_name(simple_h->running_pg, pg_name, namelen) < 0) {
2N/A if (scf_error() == SCF_ERROR_NOT_SET) {
2N/A (void) scf_set_error(SCF_ERROR_INTERNAL);
2N/A }
2N/A return (NULL);
2N/A }
2N/A
2N/A /* Get pg from instance */
2N/A if (scf_instance_get_pg(simple_h->inst, pg_name, ret_pg) == -1) {
2N/A return (NULL);
2N/A }
2N/A
2N/A return (ret_pg);
2N/A}
2N/A
2N/Aint
2N/Asmf_enable_instance(const char *fmri, int flags)
2N/A{
2N/A return (set_inst_enabled_flags(fmri, flags, B_TRUE));
2N/A}
2N/A
2N/Aint
2N/Asmf_disable_instance(const char *fmri, int flags)
2N/A{
2N/A return (set_inst_enabled_flags(fmri, flags, B_FALSE));
2N/A}
2N/A
2N/Aint
2N/A_smf_refresh_instance_i(scf_instance_t *inst)
2N/A{
2N/A return (set_inst_action_inst(inst, SCF_PROPERTY_REFRESH));
2N/A}
2N/A
2N/Aint
2N/A_smf_refresh_all_instances(scf_service_t *s)
2N/A{
2N/A scf_handle_t *h = scf_service_handle(s);
2N/A scf_instance_t *i = scf_instance_create(h);
2N/A scf_iter_t *it = scf_iter_create(h);
2N/A int err, r = -1;
2N/A
2N/A if (h == NULL || i == NULL || it == NULL)
2N/A goto error;
2N/A
2N/A if (scf_iter_service_instances(it, s) != 0)
2N/A goto error;
2N/A
2N/A while ((err = scf_iter_next_instance(it, i)) == 1)
2N/A if (_smf_refresh_instance_i(i) != 0)
2N/A goto error;
2N/A
2N/A if (err == -1)
2N/A goto error;
2N/A
2N/A r = 0;
2N/Aerror:
2N/A scf_instance_destroy(i);
2N/A scf_iter_destroy(it);
2N/A
2N/A return (r);
2N/A}
2N/A
2N/Aint
2N/Asmf_refresh_instance(const char *instance)
2N/A{
2N/A return (set_inst_action(instance, SCF_PROPERTY_REFRESH));
2N/A}
2N/A
2N/Aint
2N/Asmf_restart_instance(const char *instance)
2N/A{
2N/A return (set_inst_action(instance, SCF_PROPERTY_RESTART));
2N/A}
2N/A
2N/Aint
2N/Asmf_maintain_instance(const char *instance, int flags)
2N/A{
2N/A if (flags & SMF_TEMPORARY)
2N/A return (set_inst_action(instance,
2N/A (flags & SMF_IMMEDIATE) ?
2N/A SCF_PROPERTY_MAINT_ON_IMMTEMP :
2N/A SCF_PROPERTY_MAINT_ON_TEMPORARY));
2N/A else
2N/A return (set_inst_action(instance,
2N/A (flags & SMF_IMMEDIATE) ?
2N/A SCF_PROPERTY_MAINT_ON_IMMEDIATE :
2N/A SCF_PROPERTY_MAINT_ON));
2N/A}
2N/A
2N/Aint
2N/Asmf_degrade_instance(const char *instance, int flags)
2N/A{
2N/A scf_simple_prop_t *prop;
2N/A const char *state_str;
2N/A
2N/A if (flags & SMF_TEMPORARY)
2N/A return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
2N/A
2N/A if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
2N/A SCF_PROPERTY_STATE)) == NULL)
2N/A return (SCF_FAILED);
2N/A
2N/A if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
2N/A scf_simple_prop_free(prop);
2N/A return (SCF_FAILED);
2N/A }
2N/A
2N/A if (strcmp(state_str, SCF_STATE_STRING_ONLINE) != 0) {
2N/A scf_simple_prop_free(prop);
2N/A return (scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED));
2N/A }
2N/A scf_simple_prop_free(prop);
2N/A
2N/A return (set_inst_action(instance, (flags & SMF_IMMEDIATE) ?
2N/A SCF_PROPERTY_DEGRADE_IMMEDIATE : SCF_PROPERTY_DEGRADED));
2N/A}
2N/A
2N/Aint
2N/Asmf_restore_instance(const char *instance)
2N/A{
2N/A scf_simple_prop_t *prop;
2N/A const char *state_str;
2N/A int ret;
2N/A
2N/A if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
2N/A SCF_PROPERTY_STATE)) == NULL)
2N/A return (SCF_FAILED);
2N/A
2N/A if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
2N/A scf_simple_prop_free(prop);
2N/A return (SCF_FAILED);
2N/A }
2N/A
2N/A if (strcmp(state_str, SCF_STATE_STRING_MAINT) == 0) {
2N/A ret = set_inst_action(instance, SCF_PROPERTY_MAINT_OFF);
2N/A } else if (strcmp(state_str, SCF_STATE_STRING_DEGRADED) == 0) {
2N/A ret = set_inst_action(instance, SCF_PROPERTY_RESTORE);
2N/A } else {
2N/A ret = scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED);
2N/A }
2N/A
2N/A scf_simple_prop_free(prop);
2N/A return (ret);
2N/A}
2N/A
2N/Achar *
2N/Asmf_get_state(const char *instance)
2N/A{
2N/A scf_simple_prop_t *prop;
2N/A const char *state_str;
2N/A char *ret;
2N/A
2N/A if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
2N/A SCF_PROPERTY_STATE)) == NULL)
2N/A return (NULL);
2N/A
2N/A if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
2N/A scf_simple_prop_free(prop);
2N/A return (NULL);
2N/A }
2N/A
2N/A if ((ret = strdup(state_str)) == NULL)
2N/A (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2N/A
2N/A scf_simple_prop_free(prop);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * scf_general_pg_setup(fmri, pg_name)
2N/A * Create a scf_simple_handle_t and fill in the instance, snapshot, and
2N/A * property group fields associated with the given fmri and property group
2N/A * name.
2N/A * Returns:
2N/A * Handle on success
2N/A * Null on error with scf_error set to:
2N/A * SCF_ERROR_HANDLE_MISMATCH,
2N/A * SCF_ERROR_INVALID_ARGUMENT,
2N/A * SCF_ERROR_CONSTRAINT_VIOLATED,
2N/A * SCF_ERROR_NOT_FOUND,
2N/A * SCF_ERROR_NOT_SET,
2N/A * SCF_ERROR_DELETED,
2N/A * SCF_ERROR_NOT_BOUND,
2N/A * SCF_ERROR_CONNECTION_BROKEN,
2N/A * SCF_ERROR_INTERNAL,
2N/A * SCF_ERROR_NO_RESOURCES,
2N/A * SCF_ERROR_BACKEND_ACCESS
2N/A */
2N/Ascf_simple_handle_t *
2N/Ascf_general_pg_setup(const char *fmri, const char *pg_name)
2N/A{
2N/A scf_simple_handle_t *ret;
2N/A
2N/A ret = uu_zalloc(sizeof (*ret));
2N/A if (ret == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2N/A return (NULL);
2N/A } else {
2N/A
2N/A ret->h = _scf_handle_create_and_bind(SCF_VERSION);
2N/A ret->inst = scf_instance_create(ret->h);
2N/A ret->snap = scf_snapshot_create(ret->h);
2N/A ret->running_pg = scf_pg_create(ret->h);
2N/A }
2N/A
2N/A if ((ret->h == NULL) || (ret->inst == NULL) ||
2N/A (ret->snap == NULL) || (ret->running_pg == NULL)) {
2N/A goto out;
2N/A }
2N/A
2N/A if (scf_handle_decode_fmri(ret->h, fmri, NULL, NULL, ret->inst,
2N/A NULL, NULL, NULL) == -1) {
2N/A goto out;
2N/A }
2N/A
2N/A if ((scf_instance_get_snapshot(ret->inst, "running", ret->snap))
2N/A != 0) {
2N/A goto out;
2N/A }
2N/A
2N/A if (scf_instance_get_pg_composed(ret->inst, ret->snap, pg_name,
2N/A ret->running_pg) != 0) {
2N/A goto out;
2N/A }
2N/A
2N/A return (ret);
2N/A
2N/Aout:
2N/A scf_simple_handle_destroy(ret);
2N/A return (NULL);
2N/A}
2N/A
2N/A/*
2N/A * scf_transaction_setup(h)
2N/A * creates and starts the transaction
2N/A * Returns:
2N/A * transaction on success
2N/A * NULL on failure with scf_error set to:
2N/A * SCF_ERROR_NO_MEMORY,
2N/A * SCF_ERROR_INVALID_ARGUMENT,
2N/A * SCF_ERROR_HANDLE_DESTROYED,
2N/A * SCF_ERROR_INTERNAL,
2N/A * SCF_ERROR_NO_RESOURCES,
2N/A * SCF_ERROR_NOT_BOUND,
2N/A * SCF_ERROR_CONNECTION_BROKEN,
2N/A * SCF_ERROR_NOT_SET,
2N/A * SCF_ERROR_DELETED,
2N/A * SCF_ERROR_CONSTRAINT_VIOLATED,
2N/A * SCF_ERROR_HANDLE_MISMATCH,
2N/A * SCF_ERROR_BACKEND_ACCESS,
2N/A * SCF_ERROR_IN_USE
2N/A */
2N/Ascf_transaction_t *
2N/Ascf_transaction_setup(scf_simple_handle_t *simple_h)
2N/A{
2N/A scf_transaction_t *tx = NULL;
2N/A
2N/A if ((tx = scf_transaction_create(simple_h->h)) == NULL) {
2N/A return (NULL);
2N/A }
2N/A
2N/A if ((simple_h->editing_pg = get_instance_pg(simple_h)) == NULL) {
2N/A return (NULL);
2N/A }
2N/A
2N/A if (scf_transaction_start(tx, simple_h->editing_pg) == -1) {
2N/A scf_pg_destroy(simple_h->editing_pg);
2N/A simple_h->editing_pg = NULL;
2N/A return (NULL);
2N/A }
2N/A
2N/A return (tx);
2N/A}
2N/A
2N/Aint
2N/Ascf_transaction_restart(scf_simple_handle_t *simple_h, scf_transaction_t *tx)
2N/A{
2N/A scf_transaction_reset(tx);
2N/A
2N/A if (scf_pg_update(simple_h->editing_pg) == -1) {
2N/A return (SCF_FAILED);
2N/A }
2N/A
2N/A if (scf_transaction_start(tx, simple_h->editing_pg) == -1) {
2N/A return (SCF_FAILED);
2N/A }
2N/A
2N/A return (SCF_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * scf_read_count_property(scf_simple_handle_t *simple_h, char *prop_name,
2N/A * uint64_t *ret_count)
2N/A *
2N/A * For the given property name, return the count value.
2N/A * RETURNS:
2N/A * SCF_SUCCESS
2N/A * SCF_FAILED on failure with scf_error() set to:
2N/A * SCF_ERROR_HANDLE_DESTROYED
2N/A * SCF_ERROR_INTERNAL
2N/A * SCF_ERROR_NO_RESOURCES
2N/A * SCF_ERROR_NO_MEMORY
2N/A * SCF_ERROR_HANDLE_MISMATCH
2N/A * SCF_ERROR_INVALID_ARGUMENT
2N/A * SCF_ERROR_NOT_BOUND
2N/A * SCF_ERROR_CONNECTION_BROKEN
2N/A * SCF_ERROR_NOT_SET
2N/A * SCF_ERROR_DELETED
2N/A * SCF_ERROR_BACKEND_ACCESS
2N/A * SCF_ERROR_CONSTRAINT_VIOLATED
2N/A * SCF_ERROR_TYPE_MISMATCH
2N/A */
2N/Aint
2N/Ascf_read_count_property(
2N/A scf_simple_handle_t *simple_h,
2N/A char *prop_name,
2N/A uint64_t *ret_count)
2N/A{
2N/A scf_property_t *prop = scf_property_create(simple_h->h);
2N/A scf_value_t *val = scf_value_create(simple_h->h);
2N/A int ret = SCF_FAILED;
2N/A
2N/A if ((val == NULL) || (prop == NULL)) {
2N/A goto out;
2N/A }
2N/A
2N/A /*
2N/A * Get the property struct that goes with this property group and
2N/A * property name.
2N/A */
2N/A if (scf_pg_get_property(simple_h->running_pg, prop_name, prop) != 0) {
2N/A goto out;
2N/A }
2N/A
2N/A /* Get the value structure */
2N/A if (scf_property_get_value(prop, val) == -1) {
2N/A goto out;
2N/A }
2N/A
2N/A /*
2N/A * Now get the count value.
2N/A */
2N/A if (scf_value_get_count(val, ret_count) == -1) {
2N/A goto out;
2N/A }
2N/A
2N/A ret = SCF_SUCCESS;
2N/A
2N/Aout:
2N/A scf_property_destroy(prop);
2N/A scf_value_destroy(val);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * scf_trans_add_count_property(trans, propname, count, create_flag)
2N/A *
2N/A * Set a count property transaction entry into the pending SMF transaction.
2N/A * The transaction is created and committed outside of this function.
2N/A * Returns:
2N/A * SCF_SUCCESS
2N/A * SCF_FAILED on failure with scf_error() set to:
2N/A * SCF_ERROR_HANDLE_DESTROYED,
2N/A * SCF_ERROR_INVALID_ARGUMENT,
2N/A * SCF_ERROR_NO_MEMORY,
2N/A * SCF_ERROR_HANDLE_MISMATCH,
2N/A * SCF_ERROR_NOT_SET,
2N/A * SCF_ERROR_IN_USE,
2N/A * SCF_ERROR_NOT_FOUND,
2N/A * SCF_ERROR_EXISTS,
2N/A * SCF_ERROR_TYPE_MISMATCH,
2N/A * SCF_ERROR_NOT_BOUND,
2N/A * SCF_ERROR_CONNECTION_BROKEN,
2N/A * SCF_ERROR_INTERNAL,
2N/A * SCF_ERROR_DELETED,
2N/A * SCF_ERROR_NO_RESOURCES,
2N/A * SCF_ERROR_BACKEND_ACCESS
2N/A */
2N/Aint
2N/Ascf_set_count_property(
2N/A scf_transaction_t *trans,
2N/A char *propname,
2N/A uint64_t count,
2N/A boolean_t create_flag)
2N/A{
2N/A scf_handle_t *handle = scf_transaction_handle(trans);
2N/A scf_value_t *value = scf_value_create(handle);
2N/A scf_transaction_entry_t *entry = scf_entry_create(handle);
2N/A
2N/A if ((value == NULL) || (entry == NULL)) {
2N/A return (SCF_FAILED);
2N/A }
2N/A
2N/A /*
2N/A * Property must be set in transaction and won't take
2N/A * effect until the transaction is committed.
2N/A *
2N/A * Attempt to change the current value. However, create new property
2N/A * if it doesn't exist and the create flag is set.
2N/A */
2N/A if (scf_transaction_property_change(trans, entry, propname,
2N/A SCF_TYPE_COUNT) == 0) {
2N/A scf_value_set_count(value, count);
2N/A if (scf_entry_add_value(entry, value) == 0) {
2N/A return (SCF_SUCCESS);
2N/A }
2N/A } else {
2N/A if ((create_flag == B_TRUE) &&
2N/A (scf_error() == SCF_ERROR_NOT_FOUND)) {
2N/A if (scf_transaction_property_new(trans, entry, propname,
2N/A SCF_TYPE_COUNT) == 0) {
2N/A scf_value_set_count(value, count);
2N/A if (scf_entry_add_value(entry, value) == 0) {
2N/A return (SCF_SUCCESS);
2N/A }
2N/A }
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * cleanup if there were any errors that didn't leave these
2N/A * values where they would be cleaned up later.
2N/A */
2N/A if (value != NULL)
2N/A scf_value_destroy(value);
2N/A if (entry != NULL)
2N/A scf_entry_destroy(entry);
2N/A return (SCF_FAILED);
2N/A}
2N/A
2N/Aint
2N/Ascf_simple_walk_instances(uint_t state_flags, void *private,
2N/A int (*inst_callback)(scf_handle_t *, scf_instance_t *, void *))
2N/A{
2N/A scf_scope_t *scope = NULL;
2N/A scf_service_t *svc = NULL;
2N/A scf_instance_t *inst = NULL;
2N/A scf_iter_t *svc_iter = NULL, *inst_iter = NULL;
2N/A scf_handle_t *h = NULL;
2N/A int ret = SCF_FAILED;
2N/A int svc_iter_ret, inst_iter_ret;
2N/A int inst_state;
2N/A
2N/A if ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL)
2N/A return (ret);
2N/A
2N/A if (((scope = scf_scope_create(h)) == NULL) ||
2N/A ((svc = scf_service_create(h)) == NULL) ||
2N/A ((inst = scf_instance_create(h)) == NULL) ||
2N/A ((svc_iter = scf_iter_create(h)) == NULL) ||
2N/A ((inst_iter = scf_iter_create(h)) == NULL))
2N/A goto out;
2N/A
2N/A /*
2N/A * Get the local scope, and set up nested iteration through every
2N/A * local service, and every instance of every service.
2N/A */
2N/A
2N/A if ((scf_handle_get_local_scope(h, scope) != SCF_SUCCESS) ||
2N/A (scf_iter_scope_services(svc_iter, scope) != SCF_SUCCESS))
2N/A goto out;
2N/A
2N/A while ((svc_iter_ret = scf_iter_next_service(svc_iter, svc)) > 0) {
2N/A
2N/A if ((scf_iter_service_instances(inst_iter, svc)) !=
2N/A SCF_SUCCESS)
2N/A goto out;
2N/A
2N/A while ((inst_iter_ret =
2N/A scf_iter_next_instance(inst_iter, inst)) > 0) {
2N/A /*
2N/A * If get_inst_state fails from an internal error,
2N/A * IE, being unable to get the property group or
2N/A * property containing the state of the instance,
2N/A * we continue instead of failing, as this might just
2N/A * be an improperly configured instance.
2N/A */
2N/A if ((inst_state = get_inst_state(inst, h)) == -1) {
2N/A if (scf_error() == SCF_ERROR_INTERNAL) {
2N/A continue;
2N/A } else {
2N/A goto out;
2N/A }
2N/A }
2N/A
2N/A if ((uint_t)inst_state & state_flags) {
2N/A if (inst_callback(h, inst, private) !=
2N/A SCF_SUCCESS) {
2N/A (void) scf_set_error(
2N/A SCF_ERROR_CALLBACK_FAILED);
2N/A goto out;
2N/A }
2N/A }
2N/A }
2N/A
2N/A if (inst_iter_ret == -1)
2N/A goto out;
2N/A scf_iter_reset(inst_iter);
2N/A }
2N/A
2N/A if (svc_iter_ret != -1)
2N/A ret = SCF_SUCCESS;
2N/A
2N/Aout:
2N/A scf_scope_destroy(scope);
2N/A scf_service_destroy(svc);
2N/A scf_instance_destroy(inst);
2N/A scf_iter_destroy(svc_iter);
2N/A scf_iter_destroy(inst_iter);
2N/A scf_handle_destroy(h);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/Astatic scf_property_t *
2N/Ascf_prop_get(scf_handle_t *hin, const char *instance, const char *pgname,
2N/A const char *propname)
2N/A{
2N/A char *fmri_buf, *svcfmri = NULL;
2N/A ssize_t fmri_sz;
2N/A scf_service_t *svc = NULL;
2N/A scf_property_t *prop = NULL;
2N/A scf_handle_t *h = NULL;
2N/A boolean_t local_h = B_TRUE;
2N/A boolean_t err = B_TRUE;
2N/A
2N/A /* If the user passed in a handle, use it. */
2N/A if (hin != NULL) {
2N/A h = hin;
2N/A local_h = B_FALSE;
2N/A }
2N/A
2N/A if (local_h && ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL))
2N/A return (NULL);
2N/A
2N/A if ((fmri_buf = assemble_fmri(h, instance, pgname, propname)) == NULL) {
2N/A if (local_h)
2N/A scf_handle_destroy(h);
2N/A return (NULL);
2N/A }
2N/A
2N/A if ((svc = scf_service_create(h)) == NULL ||
2N/A (prop = scf_property_create(h)) == NULL)
2N/A goto out;
2N/A
2N/A if (scf_handle_decode_fmri(h, fmri_buf, NULL, NULL, NULL, NULL, prop,
2N/A SCF_DECODE_FMRI_REQUIRE_INSTANCE) == -1) {
2N/A switch (scf_error()) {
2N/A /*
2N/A * If the property isn't found in the instance, we grab the
2N/A * underlying service, create an FMRI out of it, and then
2N/A * query the datastore again at the service level for the
2N/A * property.
2N/A */
2N/A case SCF_ERROR_NOT_FOUND:
2N/A if (scf_handle_decode_fmri(h, fmri_buf, NULL, svc,
2N/A NULL, NULL, NULL, SCF_DECODE_FMRI_TRUNCATE) == -1)
2N/A goto out;
2N/A
2N/A fmri_sz = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH) + 1;
2N/A assert(fmri_sz > 0);
2N/A
2N/A if (scf_service_to_fmri(svc, fmri_buf, fmri_sz) == -1)
2N/A goto out;
2N/A if ((svcfmri = assemble_fmri(h, fmri_buf, pgname,
2N/A propname)) == NULL)
2N/A goto out;
2N/A if (scf_handle_decode_fmri(h, svcfmri, NULL, NULL,
2N/A NULL, NULL, prop, 0) == -1) {
2N/A free(svcfmri);
2N/A goto out;
2N/A }
2N/A free(svcfmri);
2N/A break;
2N/A case SCF_ERROR_CONSTRAINT_VIOLATED:
2N/A (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
2N/A default:
2N/A goto out;
2N/A }
2N/A }
2N/A
2N/A err = B_FALSE;
2N/Aout:
2N/A if (err) {
2N/A scf_property_destroy(prop);
2N/A prop = NULL;
2N/A }
2N/A
2N/A scf_service_destroy(svc);
2N/A if (local_h)
2N/A scf_handle_destroy(h);
2N/A
2N/A free(fmri_buf);
2N/A
2N/A return (prop);
2N/A}
2N/A
2N/Ascf_simple_prop_t *
2N/Ascf_simple_prop_get(scf_handle_t *hin, const char *instance, const char *pgname,
2N/A const char *propname)
2N/A{
2N/A scf_property_t *prop = NULL;
2N/A scf_simple_prop_t *ret = NULL;
2N/A scf_handle_t *h = NULL;
2N/A boolean_t local_h = B_TRUE;
2N/A
2N/A /* If the user passed in a handle, use it. */
2N/A if (hin != NULL) {
2N/A h = hin;
2N/A local_h = B_FALSE;
2N/A }
2N/A
2N/A if (local_h && ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL))
2N/A return (NULL);
2N/A
2N/A if ((prop = scf_prop_get(h, instance, pgname, propname)) != NULL) {
2N/A ret = fill_prop(prop, pgname, propname, h);
2N/A scf_property_destroy(prop);
2N/A }
2N/A
2N/A if (local_h)
2N/A scf_handle_destroy(h);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A
2N/Avoid
2N/Ascf_simple_prop_free(scf_simple_prop_t *prop)
2N/A{
2N/A int i;
2N/A
2N/A if (prop == NULL)
2N/A return;
2N/A
2N/A free(prop->pr_propname);
2N/A free(prop->pr_pgname);
2N/A switch (prop->pr_type) {
2N/A case SCF_TYPE_OPAQUE: {
2N/A for (i = 0; i < prop->pr_numvalues; i++) {
2N/A free(prop->pr_vallist[i].pv_opaque.o_value);
2N/A }
2N/A break;
2N/A }
2N/A case SCF_TYPE_ASTRING:
2N/A case SCF_TYPE_USTRING:
2N/A case SCF_TYPE_HOST:
2N/A case SCF_TYPE_HOSTNAME:
2N/A case SCF_TYPE_NET_ADDR:
2N/A case SCF_TYPE_NET_ADDR_V4:
2N/A case SCF_TYPE_NET_ADDR_V6:
2N/A case SCF_TYPE_URI:
2N/A case SCF_TYPE_FMRI: {
2N/A for (i = 0; i < prop->pr_numvalues; i++) {
2N/A free(prop->pr_vallist[i].pv_str);
2N/A }
2N/A break;
2N/A }
2N/A default:
2N/A break;
2N/A }
2N/A free(prop->pr_vallist);
2N/A free(prop);
2N/A}
2N/A
2N/A
2N/Ascf_simple_app_props_t *
2N/Ascf_simple_app_props_get(scf_handle_t *hin, const char *inst_fmri)
2N/A{
2N/A scf_instance_t *inst = NULL;
2N/A scf_service_t *svc = NULL;
2N/A scf_propertygroup_t *pg = NULL;
2N/A scf_property_t *prop = NULL;
2N/A scf_simple_app_props_t *ret = NULL;
2N/A scf_iter_t *pgiter = NULL, *propiter = NULL;
2N/A struct scf_simple_pg *thispg = NULL, *nextpg;
2N/A scf_simple_prop_t *thisprop, *nextprop;
2N/A scf_handle_t *h = NULL;
2N/A int pgiter_ret, propiter_ret;
2N/A ssize_t namelen;
2N/A char *propname = NULL, *pgname = NULL, *sys_fmri;
2N/A uint8_t found;
2N/A boolean_t local_h = B_TRUE;
2N/A
2N/A /* If the user passed in a handle, use it. */
2N/A if (hin != NULL) {
2N/A h = hin;
2N/A local_h = B_FALSE;
2N/A }
2N/A
2N/A if (local_h && ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL))
2N/A return (NULL);
2N/A
2N/A if (inst_fmri == NULL) {
2N/A if ((namelen = scf_myname(h, NULL, 0)) == -1) {
2N/A if (local_h)
2N/A scf_handle_destroy(h);
2N/A return (NULL);
2N/A }
2N/A if ((sys_fmri = malloc(namelen + 1)) == NULL) {
2N/A if (local_h)
2N/A scf_handle_destroy(h);
2N/A (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2N/A return (NULL);
2N/A }
2N/A if (scf_myname(h, sys_fmri, namelen + 1) == -1) {
2N/A if (local_h)
2N/A scf_handle_destroy(h);
2N/A free(sys_fmri);
2N/A return (NULL);
2N/A }
2N/A } else {
2N/A if ((sys_fmri = strdup(inst_fmri)) == NULL) {
2N/A if (local_h)
2N/A scf_handle_destroy(h);
2N/A (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2N/A return (NULL);
2N/A }
2N/A }
2N/A
2N/A namelen = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) + 1;
2N/A assert(namelen > 0);
2N/A
2N/A if ((inst = scf_instance_create(h)) == NULL ||
2N/A (svc = scf_service_create(h)) == NULL ||
2N/A (pgiter = scf_iter_create(h)) == NULL ||
2N/A (propiter = scf_iter_create(h)) == NULL ||
2N/A (pg = scf_pg_create(h)) == NULL ||
2N/A (prop = scf_property_create(h)) == NULL)
2N/A goto error2;
2N/A
2N/A if (scf_handle_decode_fmri(h, sys_fmri, NULL, svc, inst, NULL, NULL,
2N/A SCF_DECODE_FMRI_REQUIRE_INSTANCE) == -1) {
2N/A if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
2N/A (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
2N/A goto error2;
2N/A }
2N/A
2N/A if ((ret = malloc(sizeof (*ret))) == NULL ||
2N/A (thispg = malloc(sizeof (*thispg))) == NULL ||
2N/A (propname = malloc(namelen)) == NULL ||
2N/A (pgname = malloc(namelen)) == NULL) {
2N/A free(thispg);
2N/A free(ret);
2N/A (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2N/A goto error2;
2N/A }
2N/A
2N/A ret->ap_fmri = sys_fmri;
2N/A thispg->pg_name = NULL;
2N/A thispg->pg_proplist = NULL;
2N/A thispg->pg_next = NULL;
2N/A ret->ap_pglist = thispg;
2N/A
2N/A if (scf_iter_service_pgs_typed(pgiter, svc, SCF_GROUP_APPLICATION) !=
2N/A 0) {
2N/A if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2N/A (void) scf_set_error(SCF_ERROR_INTERNAL);
2N/A goto error1;
2N/A }
2N/A
2N/A while ((pgiter_ret = scf_iter_next_pg(pgiter, pg)) == 1) {
2N/A if (thispg->pg_name != NULL) {
2N/A if ((nextpg = malloc(sizeof (*nextpg))) == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2N/A goto error1;
2N/A }
2N/A nextpg->pg_name = NULL;
2N/A nextpg->pg_next = NULL;
2N/A nextpg->pg_proplist = NULL;
2N/A thispg->pg_next = nextpg;
2N/A thispg = nextpg;
2N/A } else {
2N/A /* This is the first iteration */
2N/A nextpg = thispg;
2N/A }
2N/A
2N/A if ((nextpg->pg_name = malloc(namelen)) == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2N/A goto error1;
2N/A }
2N/A
2N/A if (scf_pg_get_name(pg, nextpg->pg_name, namelen) < 0) {
2N/A if (scf_error() == SCF_ERROR_NOT_SET)
2N/A (void) scf_set_error(SCF_ERROR_INTERNAL);
2N/A goto error1;
2N/A }
2N/A
2N/A thisprop = NULL;
2N/A
2N/A scf_iter_reset(propiter);
2N/A
2N/A if (scf_iter_pg_properties(propiter, pg) != 0) {
2N/A if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2N/A (void) scf_set_error(SCF_ERROR_INTERNAL);
2N/A goto error1;
2N/A }
2N/A
2N/A while ((propiter_ret = scf_iter_next_property(propiter, prop))
2N/A == 1) {
2N/A if (scf_property_get_name(prop, propname, namelen) <
2N/A 0) {
2N/A if (scf_error() == SCF_ERROR_NOT_SET)
2N/A (void) scf_set_error(
2N/A SCF_ERROR_INTERNAL);
2N/A goto error1;
2N/A }
2N/A if (thisprop != NULL) {
2N/A if ((nextprop = fill_prop(prop,
2N/A nextpg->pg_name, propname, h)) == NULL)
2N/A goto error1;
2N/A thisprop->pr_next = nextprop;
2N/A thisprop = nextprop;
2N/A } else {
2N/A /* This is the first iteration */
2N/A if ((thisprop = fill_prop(prop,
2N/A nextpg->pg_name, propname, h)) == NULL)
2N/A goto error1;
2N/A nextpg->pg_proplist = thisprop;
2N/A nextprop = thisprop;
2N/A }
2N/A nextprop->pr_pg = nextpg;
2N/A nextprop->pr_next = NULL;
2N/A }
2N/A
2N/A if (propiter_ret == -1) {
2N/A if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2N/A (void) scf_set_error(SCF_ERROR_INTERNAL);
2N/A goto error1;
2N/A }
2N/A }
2N/A
2N/A if (pgiter_ret == -1) {
2N/A if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2N/A (void) scf_set_error(SCF_ERROR_INTERNAL);
2N/A goto error1;
2N/A }
2N/A
2N/A /*
2N/A * At this point, we've filled the scf_simple_app_props_t with all the
2N/A * properties at the service level. Now we iterate over all the
2N/A * properties at the instance level, overwriting any duplicate
2N/A * properties, in order to provide service/instance composition.
2N/A */
2N/A
2N/A scf_iter_reset(pgiter);
2N/A scf_iter_reset(propiter);
2N/A
2N/A if (scf_iter_instance_pgs_typed(pgiter, inst, SCF_GROUP_APPLICATION)
2N/A != 0) {
2N/A if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2N/A (void) scf_set_error(SCF_ERROR_INTERNAL);
2N/A goto error1;
2N/A }
2N/A
2N/A while ((pgiter_ret = scf_iter_next_pg(pgiter, pg)) == 1) {
2N/A
2N/A thispg = ret->ap_pglist;
2N/A found = 0;
2N/A
2N/A /*
2N/A * Find either the end of the list, so we can append the
2N/A * property group, or an existing property group that matches
2N/A * it, so we can insert/overwrite its properties.
2N/A */
2N/A
2N/A if (scf_pg_get_name(pg, pgname, namelen) < 0) {
2N/A if (scf_error() == SCF_ERROR_NOT_SET)
2N/A (void) scf_set_error(SCF_ERROR_INTERNAL);
2N/A goto error1;
2N/A }
2N/A
2N/A while ((thispg != NULL) && (thispg->pg_name != NULL)) {
2N/A if (strcmp(thispg->pg_name, pgname) == 0) {
2N/A found = 1;
2N/A break;
2N/A }
2N/A if (thispg->pg_next == NULL)
2N/A break;
2N/A
2N/A thispg = thispg->pg_next;
2N/A }
2N/A
2N/A scf_iter_reset(propiter);
2N/A
2N/A if (scf_iter_pg_properties(propiter, pg) != 0) {
2N/A if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2N/A (void) scf_set_error(SCF_ERROR_INTERNAL);
2N/A goto error1;
2N/A }
2N/A
2N/A if (found) {
2N/A /*
2N/A * insert_app_props inserts or overwrites the
2N/A * properties in thispg.
2N/A */
2N/A
2N/A if (insert_app_props(propiter, pgname, propname,
2N/A thispg, prop, namelen, h) == -1)
2N/A goto error1;
2N/A
2N/A } else {
2N/A /*
2N/A * If the property group wasn't found, we're adding
2N/A * a newly allocated property group to the end of the
2N/A * list.
2N/A */
2N/A
2N/A if ((nextpg = malloc(sizeof (*nextpg))) == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2N/A goto error1;
2N/A }
2N/A nextpg->pg_next = NULL;
2N/A nextpg->pg_proplist = NULL;
2N/A thisprop = NULL;
2N/A
2N/A if ((nextpg->pg_name = strdup(pgname)) == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2N/A free(nextpg);
2N/A goto error1;
2N/A }
2N/A
2N/A if (thispg->pg_name == NULL) {
2N/A free(thispg);
2N/A ret->ap_pglist = nextpg;
2N/A } else {
2N/A thispg->pg_next = nextpg;
2N/A }
2N/A
2N/A while ((propiter_ret =
2N/A scf_iter_next_property(propiter, prop)) == 1) {
2N/A if (scf_property_get_name(prop, propname,
2N/A namelen) < 0) {
2N/A if (scf_error() == SCF_ERROR_NOT_SET)
2N/A (void) scf_set_error(
2N/A SCF_ERROR_INTERNAL);
2N/A goto error1;
2N/A }
2N/A if (thisprop != NULL) {
2N/A if ((nextprop = fill_prop(prop,
2N/A pgname, propname, h)) ==
2N/A NULL)
2N/A goto error1;
2N/A thisprop->pr_next = nextprop;
2N/A thisprop = nextprop;
2N/A } else {
2N/A /* This is the first iteration */
2N/A if ((thisprop = fill_prop(prop,
2N/A pgname, propname, h)) ==
2N/A NULL)
2N/A goto error1;
2N/A nextpg->pg_proplist = thisprop;
2N/A nextprop = thisprop;
2N/A }
2N/A nextprop->pr_pg = nextpg;
2N/A nextprop->pr_next = NULL;
2N/A }
2N/A
2N/A if (propiter_ret == -1) {
2N/A if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2N/A (void) scf_set_error(
2N/A SCF_ERROR_INTERNAL);
2N/A goto error1;
2N/A }
2N/A }
2N/A
2N/A }
2N/A
2N/A if (pgiter_ret == -1) {
2N/A if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2N/A (void) scf_set_error(SCF_ERROR_INTERNAL);
2N/A goto error1;
2N/A }
2N/A
2N/A scf_iter_destroy(pgiter);
2N/A scf_iter_destroy(propiter);
2N/A scf_pg_destroy(pg);
2N/A scf_property_destroy(prop);
2N/A scf_instance_destroy(inst);
2N/A scf_service_destroy(svc);
2N/A free(propname);
2N/A free(pgname);
2N/A if (local_h)
2N/A scf_handle_destroy(h);
2N/A
2N/A if (ret->ap_pglist->pg_name == NULL)
2N/A return (NULL);
2N/A
2N/A return (ret);
2N/A
2N/A /*
2N/A * Exit point for a successful call. Below this line are exit points
2N/A * for failures at various stages during the function.
2N/A */
2N/A
2N/Aerror1:
2N/A scf_simple_app_props_free(ret);
2N/A
2N/Aerror2:
2N/A scf_iter_destroy(pgiter);
2N/A scf_iter_destroy(propiter);
2N/A scf_pg_destroy(pg);
2N/A scf_property_destroy(prop);
2N/A scf_instance_destroy(inst);
2N/A scf_service_destroy(svc);
2N/A free(propname);
2N/A free(pgname);
2N/A if (local_h)
2N/A scf_handle_destroy(h);
2N/A return (NULL);
2N/A}
2N/A
2N/A
2N/Avoid
2N/Ascf_simple_app_props_free(scf_simple_app_props_t *propblock)
2N/A{
2N/A struct scf_simple_pg *pgthis, *pgnext;
2N/A scf_simple_prop_t *propthis, *propnext;
2N/A
2N/A if ((propblock == NULL) || (propblock->ap_pglist == NULL))
2N/A return;
2N/A
2N/A for (pgthis = propblock->ap_pglist; pgthis != NULL; pgthis = pgnext) {
2N/A pgnext = pgthis->pg_next;
2N/A
2N/A propthis = pgthis->pg_proplist;
2N/A
2N/A while (propthis != NULL) {
2N/A propnext = propthis->pr_next;
2N/A scf_simple_prop_free(propthis);
2N/A propthis = propnext;
2N/A }
2N/A
2N/A free(pgthis->pg_name);
2N/A free(pgthis);
2N/A }
2N/A
2N/A free(propblock->ap_fmri);
2N/A free(propblock);
2N/A}
2N/A
2N/Aconst scf_simple_prop_t *
2N/Ascf_simple_app_props_next(const scf_simple_app_props_t *propblock,
2N/A scf_simple_prop_t *last)
2N/A{
2N/A struct scf_simple_pg *this;
2N/A
2N/A if (propblock == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NOT_SET);
2N/A return (NULL);
2N/A }
2N/A
2N/A this = propblock->ap_pglist;
2N/A
2N/A /*
2N/A * We're looking for the first property in this block if last is
2N/A * NULL
2N/A */
2N/A
2N/A if (last == NULL) {
2N/A /* An empty pglist is legal, it just means no properties */
2N/A if (this == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NONE);
2N/A return (NULL);
2N/A }
2N/A /*
2N/A * Walk until we find a pg with a property in it, or we run
2N/A * out of property groups.
2N/A */
2N/A while ((this->pg_proplist == NULL) && (this->pg_next != NULL))
2N/A this = this->pg_next;
2N/A
2N/A if (this->pg_proplist == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NONE);
2N/A return (NULL);
2N/A }
2N/A
2N/A return (this->pg_proplist);
2N/A
2N/A }
2N/A /*
2N/A * If last isn't NULL, then return the next prop in the property group,
2N/A * or walk the property groups until we find another property, or
2N/A * run out of property groups.
2N/A */
2N/A if (last->pr_next != NULL)
2N/A return (last->pr_next);
2N/A
2N/A if (last->pr_pg->pg_next == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NONE);
2N/A return (NULL);
2N/A }
2N/A
2N/A this = last->pr_pg->pg_next;
2N/A
2N/A while ((this->pg_proplist == NULL) && (this->pg_next != NULL))
2N/A this = this->pg_next;
2N/A
2N/A if (this->pg_proplist == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NONE);
2N/A return (NULL);
2N/A }
2N/A
2N/A return (this->pg_proplist);
2N/A}
2N/A
2N/Aconst scf_simple_prop_t *
2N/Ascf_simple_app_props_search(const scf_simple_app_props_t *propblock,
2N/A const char *pgname, const char *propname)
2N/A{
2N/A struct scf_simple_pg *pg;
2N/A scf_simple_prop_t *prop;
2N/A
2N/A if ((propblock == NULL) || (propname == NULL)) {
2N/A (void) scf_set_error(SCF_ERROR_NOT_SET);
2N/A return (NULL);
2N/A }
2N/A
2N/A pg = propblock->ap_pglist;
2N/A
2N/A /*
2N/A * If pgname is NULL, we're searching the default application
2N/A * property group, otherwise we look for the specified group.
2N/A */
2N/A if (pgname == NULL) {
2N/A while ((pg != NULL) &&
2N/A (strcmp(SCF_PG_APP_DEFAULT, pg->pg_name) != 0))
2N/A pg = pg->pg_next;
2N/A } else {
2N/A while ((pg != NULL) && (strcmp(pgname, pg->pg_name) != 0))
2N/A pg = pg->pg_next;
2N/A }
2N/A
2N/A if (pg == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NOT_FOUND);
2N/A return (NULL);
2N/A }
2N/A
2N/A prop = pg->pg_proplist;
2N/A
2N/A while ((prop != NULL) && (strcmp(propname, prop->pr_propname) != 0))
2N/A prop = prop->pr_next;
2N/A
2N/A if (prop == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NOT_FOUND);
2N/A return (NULL);
2N/A }
2N/A
2N/A return (prop);
2N/A}
2N/A
2N/Avoid
2N/Ascf_simple_prop_next_reset(scf_simple_prop_t *prop)
2N/A{
2N/A if (prop == NULL)
2N/A return;
2N/A prop->pr_iter = 0;
2N/A}
2N/A
2N/Assize_t
2N/Ascf_simple_prop_numvalues(const scf_simple_prop_t *prop)
2N/A{
2N/A if (prop == NULL)
2N/A return (scf_set_error(SCF_ERROR_NOT_SET));
2N/A
2N/A return (prop->pr_numvalues);
2N/A}
2N/A
2N/A
2N/Ascf_type_t
2N/Ascf_simple_prop_type(const scf_simple_prop_t *prop)
2N/A{
2N/A if (prop == NULL)
2N/A return (scf_set_error(SCF_ERROR_NOT_SET));
2N/A
2N/A return (prop->pr_type);
2N/A}
2N/A
2N/A
2N/Achar *
2N/Ascf_simple_prop_name(const scf_simple_prop_t *prop)
2N/A{
2N/A if ((prop == NULL) || (prop->pr_propname == NULL)) {
2N/A (void) scf_set_error(SCF_ERROR_NOT_SET);
2N/A return (NULL);
2N/A }
2N/A
2N/A return (prop->pr_propname);
2N/A}
2N/A
2N/A
2N/Achar *
2N/Ascf_simple_prop_pgname(const scf_simple_prop_t *prop)
2N/A{
2N/A if ((prop == NULL) || (prop->pr_pgname == NULL)) {
2N/A (void) scf_set_error(SCF_ERROR_NOT_SET);
2N/A return (NULL);
2N/A }
2N/A
2N/A return (prop->pr_pgname);
2N/A}
2N/A
2N/A
2N/Astatic union scf_simple_prop_val *
2N/Ascf_next_val(scf_simple_prop_t *prop, scf_type_t type)
2N/A{
2N/A if (prop == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NOT_SET);
2N/A return (NULL);
2N/A }
2N/A
2N/A switch (prop->pr_type) {
2N/A case SCF_TYPE_USTRING:
2N/A case SCF_TYPE_HOST:
2N/A case SCF_TYPE_HOSTNAME:
2N/A case SCF_TYPE_NET_ADDR:
2N/A case SCF_TYPE_NET_ADDR_V4:
2N/A case SCF_TYPE_NET_ADDR_V6:
2N/A case SCF_TYPE_URI:
2N/A case SCF_TYPE_FMRI: {
2N/A if (type != SCF_TYPE_USTRING) {
2N/A (void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
2N/A return (NULL);
2N/A }
2N/A break;
2N/A }
2N/A default: {
2N/A if (type != prop->pr_type) {
2N/A (void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
2N/A return (NULL);
2N/A }
2N/A break;
2N/A }
2N/A }
2N/A
2N/A if (prop->pr_iter >= prop->pr_numvalues) {
2N/A (void) scf_set_error(SCF_ERROR_NONE);
2N/A return (NULL);
2N/A }
2N/A
2N/A return (&prop->pr_vallist[prop->pr_iter++]);
2N/A}
2N/A
2N/A
2N/Auint8_t *
2N/Ascf_simple_prop_next_boolean(scf_simple_prop_t *prop)
2N/A{
2N/A union scf_simple_prop_val *ret;
2N/A
2N/A ret = scf_next_val(prop, SCF_TYPE_BOOLEAN);
2N/A
2N/A if (ret == NULL)
2N/A return (NULL);
2N/A
2N/A return (&ret->pv_bool);
2N/A}
2N/A
2N/A
2N/Auint64_t *
2N/Ascf_simple_prop_next_count(scf_simple_prop_t *prop)
2N/A{
2N/A union scf_simple_prop_val *ret;
2N/A
2N/A ret = scf_next_val(prop, SCF_TYPE_COUNT);
2N/A
2N/A if (ret == NULL)
2N/A return (NULL);
2N/A
2N/A return (&ret->pv_uint);
2N/A}
2N/A
2N/A
2N/Aint64_t *
2N/Ascf_simple_prop_next_integer(scf_simple_prop_t *prop)
2N/A{
2N/A union scf_simple_prop_val *ret;
2N/A
2N/A ret = scf_next_val(prop, SCF_TYPE_INTEGER);
2N/A
2N/A if (ret == NULL)
2N/A return (NULL);
2N/A
2N/A return (&ret->pv_int);
2N/A}
2N/A
2N/Aint64_t *
2N/Ascf_simple_prop_next_time(scf_simple_prop_t *prop, int32_t *nsec)
2N/A{
2N/A union scf_simple_prop_val *ret;
2N/A
2N/A ret = scf_next_val(prop, SCF_TYPE_TIME);
2N/A
2N/A if (ret == NULL)
2N/A return (NULL);
2N/A
2N/A if (nsec != NULL)
2N/A *nsec = ret->pv_time.t_nsec;
2N/A
2N/A return (&ret->pv_time.t_sec);
2N/A}
2N/A
2N/Achar *
2N/Ascf_simple_prop_next_astring(scf_simple_prop_t *prop)
2N/A{
2N/A union scf_simple_prop_val *ret;
2N/A
2N/A ret = scf_next_val(prop, SCF_TYPE_ASTRING);
2N/A
2N/A if (ret == NULL)
2N/A return (NULL);
2N/A
2N/A return (ret->pv_str);
2N/A}
2N/A
2N/Achar *
2N/Ascf_simple_prop_next_ustring(scf_simple_prop_t *prop)
2N/A{
2N/A union scf_simple_prop_val *ret;
2N/A
2N/A ret = scf_next_val(prop, SCF_TYPE_USTRING);
2N/A
2N/A if (ret == NULL)
2N/A return (NULL);
2N/A
2N/A return (ret->pv_str);
2N/A}
2N/A
2N/Avoid *
2N/Ascf_simple_prop_next_opaque(scf_simple_prop_t *prop, size_t *length)
2N/A{
2N/A union scf_simple_prop_val *ret;
2N/A
2N/A ret = scf_next_val(prop, SCF_TYPE_OPAQUE);
2N/A
2N/A if (ret == NULL) {
2N/A *length = 0;
2N/A return (NULL);
2N/A }
2N/A
2N/A *length = ret->pv_opaque.o_size;
2N/A return (ret->pv_opaque.o_value);
2N/A}
2N/A
2N/A/*
2N/A * Generate a filename based on the fmri and the given name and return
2N/A * it in the buffer of MAXPATHLEN provided by the caller.
2N/A * If temp_filename is non-zero, also generate a temporary, unique filename
2N/A * and return it in the temp buffer of MAXPATHLEN provided by the caller.
2N/A * The path to the generated pathname is also created.
2N/A * Given fmri should begin with a scheme such as "svc:".
2N/A * Returns
2N/A * 0 on success
2N/A * -1 if filename would exceed MAXPATHLEN or
2N/A * -2 if unable to create directory to filename path
2N/A */
2N/Aint
2N/Agen_filenms_from_fmri(const char *fmri, const char *name, char *filename,
2N/A char *temp_filename)
2N/A{
2N/A int len;
2N/A
2N/A len = strlen(SMF_SPEEDY_FILES_PATH);
2N/A len += strlen(fmri);
2N/A len += 2; /* for slash and null */
2N/A len += strlen(name);
2N/A len += 6; /* For X's needed for mkstemp */
2N/A
2N/A if (len > MAXPATHLEN)
2N/A return (-1);
2N/A
2N/A /* Construct directory name first - speedy path ends in slash */
2N/A (void) strcpy(filename, SMF_SPEEDY_FILES_PATH);
2N/A (void) strcat(filename, fmri);
2N/A if (mkdirp(filename, 0755) == -1) {
2N/A /* errno is set */
2N/A if (errno != EEXIST)
2N/A return (-2);
2N/A }
2N/A
2N/A (void) strcat(filename, "/");
2N/A (void) strcat(filename, name);
2N/A
2N/A if (temp_filename) {
2N/A (void) strcpy(temp_filename, filename);
2N/A (void) strcat(temp_filename, "XXXXXX");
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Ascf_type_t
2N/Ascf_true_base_type(scf_type_t type)
2N/A{
2N/A scf_type_t base = type;
2N/A
2N/A do {
2N/A type = base;
2N/A (void) scf_type_base_type(type, &base);
2N/A } while (base != type);
2N/A
2N/A return (base);
2N/A}
2N/A
2N/A/*
2N/A * Cleanup just the linked list of scf_propvec_mval_t nodes and respective
2N/A * pv_ptr entries created by scf_read_propvec_mval().
2N/A */
2N/Astatic void
2N/Ascf_clean_propvec_mval(scf_propvec_t *prop)
2N/A{
2N/A scf_propvec_mval_t *pv_ptr_node = prop->pv_ptr;
2N/A scf_propvec_mval_t *pv_ptr_node_prev;
2N/A
2N/A assert(prop->pv_mval);
2N/A
2N/A prop->pv_ptr = NULL;
2N/A while (pv_ptr_node != NULL) {
2N/A if (prop->pv_type == SCF_TYPE_OPAQUE) {
2N/A scf_opaque_t *o = pv_ptr_node->pv_ptr;
2N/A free(o->so_addr);
2N/A }
2N/A free(pv_ptr_node->pv_ptr);
2N/A pv_ptr_node_prev = pv_ptr_node;
2N/A pv_ptr_node = pv_ptr_node->next;
2N/A free(pv_ptr_node_prev);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Convenience routine which frees all strings and opaque data
2N/A * allocated by scf_read_propvec.
2N/A *
2N/A * Like free(3C), this function preserves the value of errno.
2N/A */
2N/Avoid
2N/Ascf_clean_propvec(scf_propvec_t *propvec)
2N/A{
2N/A int saved_errno = errno;
2N/A scf_propvec_t *prop;
2N/A
2N/A for (prop = propvec; prop->pv_prop != NULL; prop++) {
2N/A assert(prop->pv_type != SCF_TYPE_INVALID);
2N/A if (prop->pv_mval) {
2N/A scf_clean_propvec_mval(prop);
2N/A continue;
2N/A }
2N/A if (prop->pv_type == SCF_TYPE_OPAQUE) {
2N/A scf_opaque_t *o = prop->pv_ptr;
2N/A
2N/A if (o->so_addr != NULL)
2N/A free(o->so_addr);
2N/A } else if (scf_true_base_type(prop->pv_type) ==
2N/A SCF_TYPE_ASTRING) {
2N/A if (*(char **)prop->pv_ptr != NULL)
2N/A free(*(char **)prop->pv_ptr);
2N/A }
2N/A }
2N/A
2N/A errno = saved_errno;
2N/A}
2N/A
2N/A/*
2N/A * Read values for given multi-valued property. The results are put to the
2N/A * linked list of scf_propvec_mval_t pointed by the pv_ptr in the original
2N/A * scf_propvec_t.
2N/A */
2N/Astatic int
2N/Ascf_read_propvec_mval(scf_handle_t *h, scf_propvec_t *prop,
2N/A scf_property_t *p)
2N/A{
2N/A
2N/A scf_iter_t *pi;
2N/A scf_value_t *piv;
2N/A scf_error_t error = SCF_ERROR_NONE;
2N/A scf_propvec_mval_t *pv_ptr_node;
2N/A scf_propvec_mval_t *pv_ptr_ll_end = NULL;
2N/A
2N/A if ((pi = scf_iter_create(h)) == NULL ||
2N/A (piv = scf_value_create(h)) == NULL) {
2N/A scf_iter_destroy(pi);
2N/A return (SCF_FAILED);
2N/A }
2N/A
2N/A if (scf_iter_property_values(pi, p) != 0) {
2N/A error = scf_error();
2N/A goto out;
2N/A }
2N/A
2N/A while (scf_iter_next_value(pi, piv) == 1) {
2N/A int ret = 0;
2N/A
2N/A if ((pv_ptr_node = calloc(1, sizeof (*pv_ptr_node))) == NULL) {
2N/A error = SCF_ERROR_NO_MEMORY;
2N/A goto out;
2N/A }
2N/A if (pv_ptr_ll_end != NULL) {
2N/A pv_ptr_ll_end->next = pv_ptr_node;
2N/A } else {
2N/A prop->pv_ptr = pv_ptr_node;
2N/A pv_ptr_ll_end = pv_ptr_node;
2N/A }
2N/A
2N/A switch (prop->pv_type) {
2N/A case SCF_TYPE_BOOLEAN: {
2N/A uint8_t b;
2N/A
2N/A if ((ret = scf_value_get_boolean(piv, &b)) == -1) {
2N/A break;
2N/A }
2N/A
2N/A pv_ptr_node->pv_ptr = malloc(sizeof (boolean_t));
2N/A if (pv_ptr_node->pv_ptr == NULL) {
2N/A error = SCF_ERROR_NO_MEMORY;
2N/A goto out;
2N/A }
2N/A *(boolean_t *)pv_ptr_node->pv_ptr =
2N/A b ? B_TRUE : B_FALSE;
2N/A break;
2N/A }
2N/A case SCF_TYPE_COUNT:
2N/A pv_ptr_node->pv_ptr = malloc(sizeof (uint64_t));
2N/A if (pv_ptr_node->pv_ptr == NULL) {
2N/A error = SCF_ERROR_NO_MEMORY;
2N/A goto out;
2N/A }
2N/A ret = scf_value_get_count(piv, pv_ptr_node->pv_ptr);
2N/A break;
2N/A case SCF_TYPE_INTEGER:
2N/A pv_ptr_node->pv_ptr = malloc(sizeof (int64_t));
2N/A if (pv_ptr_node->pv_ptr == NULL) {
2N/A error = SCF_ERROR_NO_MEMORY;
2N/A goto out;
2N/A }
2N/A ret = scf_value_get_integer(piv, pv_ptr_node->pv_ptr);
2N/A break;
2N/A case SCF_TYPE_TIME: {
2N/A pv_ptr_node->pv_ptr = malloc(sizeof (scf_time_t));
2N/A if (pv_ptr_node->pv_ptr == NULL) {
2N/A error = SCF_ERROR_NO_MEMORY;
2N/A goto out;
2N/A }
2N/A ret = scf_value_get_time(piv,
2N/A &((scf_time_t *)pv_ptr_node->pv_ptr)->t_seconds,
2N/A &((scf_time_t *)pv_ptr_node->pv_ptr)->t_ns);
2N/A break;
2N/A }
2N/A case SCF_TYPE_OPAQUE: {
2N/A scf_opaque_t *opaque;
2N/A
2N/A if (scf_value_get_opaque(piv, NULL, 0) == -1) {
2N/A error = scf_error();
2N/A goto out;
2N/A }
2N/A
2N/A pv_ptr_node->pv_ptr = malloc(sizeof (scf_opaque_t));
2N/A if (pv_ptr_node->pv_ptr == NULL) {
2N/A error = SCF_ERROR_NO_MEMORY;
2N/A goto out;
2N/A }
2N/A opaque = pv_ptr_node->pv_ptr;
2N/A
2N/A opaque->so_addr = malloc(piv->value_size);
2N/A if (opaque->so_addr == NULL) {
2N/A error = SCF_ERROR_NO_MEMORY;
2N/A goto out;
2N/A }
2N/A opaque->so_size = piv->value_size;
2N/A ret = scf_value_get_opaque(piv, opaque->so_addr,
2N/A opaque->so_size);
2N/A break;
2N/A }
2N/A default: {
2N/A ssize_t size;
2N/A
2N/A assert(scf_true_base_type(prop->pv_type) ==
2N/A SCF_TYPE_ASTRING);
2N/A
2N/A size = scf_value_get_astring(piv, NULL, 0);
2N/A if (size == -1) {
2N/A error = scf_error();
2N/A goto out;
2N/A }
2N/A if ((pv_ptr_node->pv_ptr = malloc(++size)) == NULL) {
2N/A error = SCF_ERROR_NO_MEMORY;
2N/A goto out;
2N/A }
2N/A ret = scf_value_get_astring(piv,
2N/A pv_ptr_node->pv_ptr, size);
2N/A break;
2N/A }
2N/A
2N/A }
2N/A
2N/A if (ret == -1) {
2N/A error = scf_error();
2N/A goto out;
2N/A }
2N/A }
2N/A
2N/Aout:
2N/A scf_iter_destroy(pi);
2N/A scf_value_destroy(piv);
2N/A if (error != SCF_ERROR_NONE) {
2N/A (void) scf_set_error(error);
2N/A return (SCF_FAILED);
2N/A }
2N/A
2N/A return (SCF_SUCCESS);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Reads a vector of properties from the specified fmri/property group.
2N/A * If 'running' is true, reads from the running snapshot instead of the
2N/A * editing snapshot.
2N/A *
2N/A * For string types, a buffer is allocated using malloc(3C) to hold the
2N/A * zero-terminated string, a pointer to which is stored in the
2N/A * caller-provided char **. It is the caller's responsbility to free
2N/A * this string. To simplify error handling, unread strings are
2N/A * initialized to NULL.
2N/A *
2N/A * For opaque types, a buffer is allocated using malloc(3C) to hold the
2N/A * opaque data. A pointer to this buffer and its size are stored in
2N/A * the caller-provided scf_opaque_t. It is the caller's responsibility
2N/A * to free this buffer. To simplify error handling, the address fields
2N/A * for unread opaque data are initialized to NULL.
2N/A *
2N/A * Multi-valued properties are stored in the linked list of scf_propvec_mval_t.
2N/A * The list itself and each of the pv_ptr entries holding the actual values are
2N/A * allocated using malloc(3C). It is the caller's responsibility to free the
2N/A * buffers either manually or using scf_clean_propvec().
2N/A *
2N/A * All other data is stored directly in caller-provided variables or
2N/A * structures.
2N/A *
2N/A * If this function fails to read a specific property, *badprop is set
2N/A * to point at that property's entry in the properties array.
2N/A *
2N/A * On all failures, all memory allocated by this function is freed.
2N/A */
2N/Aint
2N/Ascf_read_propvec(const char *fmri, const char *pgname, boolean_t running,
2N/A scf_propvec_t *properties, scf_propvec_t **badprop)
2N/A{
2N/A scf_handle_t *h = _scf_handle_create_and_bind(SCF_VERSION);
2N/A scf_service_t *s = scf_service_create(h);
2N/A scf_instance_t *i = scf_instance_create(h);
2N/A scf_snapshot_t *snap = running ? scf_snapshot_create(h) : NULL;
2N/A scf_propertygroup_t *pg = scf_pg_create(h);
2N/A scf_property_t *p = scf_property_create(h);
2N/A scf_value_t *v = scf_value_create(h);
2N/A boolean_t instance = B_TRUE;
2N/A scf_propvec_t *prop;
2N/A scf_error_t error = SCF_ERROR_NONE;
2N/A
2N/A for (prop = properties; prop->pv_prop != NULL; prop++) {
2N/A if (prop->pv_mval)
2N/A prop->pv_ptr = NULL;
2N/A else if (prop->pv_type == SCF_TYPE_OPAQUE)
2N/A ((scf_opaque_t *)prop->pv_ptr)->so_addr = NULL;
2N/A else if (scf_true_base_type(prop->pv_type) == SCF_TYPE_ASTRING)
2N/A *((char **)prop->pv_ptr) = NULL;
2N/A }
2N/A
2N/A if (h == NULL || s == NULL || i == NULL || (running && snap == NULL) ||
2N/A pg == NULL || p == NULL || v == NULL)
2N/A goto scferror;
2N/A
2N/A if (scf_handle_decode_fmri(h, fmri, NULL, s, i, NULL, NULL, 0) == -1)
2N/A goto scferror;
2N/A
2N/A if (scf_instance_to_fmri(i, NULL, 0) == -1) {
2N/A if (scf_error() != SCF_ERROR_NOT_SET)
2N/A goto scferror;
2N/A instance = B_FALSE;
2N/A }
2N/A
2N/A if (running) {
2N/A if (!instance) {
2N/A error = SCF_ERROR_TYPE_MISMATCH;
2N/A goto out;
2N/A }
2N/A
2N/A if (scf_instance_get_snapshot(i, "running", snap) !=
2N/A SCF_SUCCESS)
2N/A goto scferror;
2N/A }
2N/A
2N/A if ((instance ? scf_instance_get_pg_composed(i, snap, pgname, pg) :
2N/A scf_service_get_pg(s, pgname, pg)) == -1)
2N/A goto scferror;
2N/A
2N/A for (prop = properties; prop->pv_prop != NULL; prop++) {
2N/A int ret = 0;
2N/A
2N/A if (scf_pg_get_property(pg, prop->pv_prop, p) == -1) {
2N/A *badprop = prop;
2N/A goto scferror;
2N/A }
2N/A
2N/A if (prop->pv_mval) {
2N/A if (scf_read_propvec_mval(h, prop, p) != SCF_SUCCESS) {
2N/A *badprop = prop;
2N/A goto scferror;
2N/A }
2N/A continue;
2N/A }
2N/A
2N/A if (scf_property_get_value(p, v) == -1) {
2N/A *badprop = prop;
2N/A goto scferror;
2N/A }
2N/A
2N/A switch (prop->pv_type) {
2N/A case SCF_TYPE_BOOLEAN: {
2N/A uint8_t b;
2N/A
2N/A if ((ret = scf_value_get_boolean(v, &b)) == -1) {
2N/A break;
2N/A }
2N/A if (prop->pv_aux != 0) {
2N/A uint64_t *bits = prop->pv_ptr;
2N/A *bits = b ? (*bits | prop->pv_aux) :
2N/A (*bits & ~prop->pv_aux);
2N/A } else {
2N/A boolean_t *bool = prop->pv_ptr;
2N/A *bool = b ? B_TRUE : B_FALSE;
2N/A }
2N/A break;
2N/A }
2N/A case SCF_TYPE_COUNT:
2N/A ret = scf_value_get_count(v, prop->pv_ptr);
2N/A break;
2N/A case SCF_TYPE_INTEGER:
2N/A ret = scf_value_get_integer(v, prop->pv_ptr);
2N/A break;
2N/A case SCF_TYPE_TIME: {
2N/A scf_time_t *time = prop->pv_ptr;
2N/A
2N/A ret = scf_value_get_time(v, &time->t_seconds,
2N/A &time->t_ns);
2N/A break;
2N/A }
2N/A case SCF_TYPE_OPAQUE: {
2N/A scf_opaque_t *opaque = prop->pv_ptr;
2N/A
2N/A if (scf_value_get_opaque(v, NULL, 0) == -1) {
2N/A *badprop = prop;
2N/A goto scferror;
2N/A }
2N/A if ((opaque->so_addr = malloc(v->value_size)) == NULL) {
2N/A error = SCF_ERROR_NO_MEMORY;
2N/A scf_clean_propvec(properties);
2N/A goto out;
2N/A }
2N/A opaque->so_size = v->value_size;
2N/A ret = scf_value_get_opaque(v, opaque->so_addr,
2N/A opaque->so_size);
2N/A break;
2N/A }
2N/A default: {
2N/A char *s;
2N/A ssize_t size;
2N/A
2N/A assert(scf_true_base_type(prop->pv_type) ==
2N/A SCF_TYPE_ASTRING);
2N/A
2N/A size = scf_value_get_astring(v, NULL, 0);
2N/A if (size == -1) {
2N/A *badprop = prop;
2N/A goto scferror;
2N/A }
2N/A if ((s = malloc(++size)) == NULL) {
2N/A error = SCF_ERROR_NO_MEMORY;
2N/A scf_clean_propvec(properties);
2N/A goto out;
2N/A }
2N/A ret = scf_value_get_astring(v, s, size);
2N/A *(char **)prop->pv_ptr = s;
2N/A }
2N/A
2N/A }
2N/A
2N/A if (ret == -1) {
2N/A *badprop = prop;
2N/A goto scferror;
2N/A }
2N/A }
2N/A
2N/A goto out;
2N/A
2N/Ascferror:
2N/A error = scf_error();
2N/A scf_clean_propvec(properties);
2N/A
2N/Aout:
2N/A scf_value_destroy(v);
2N/A scf_property_destroy(p);
2N/A scf_pg_destroy(pg);
2N/A scf_snapshot_destroy(snap);
2N/A scf_instance_destroy(i);
2N/A scf_service_destroy(s);
2N/A scf_handle_destroy(h);
2N/A
2N/A if (error != SCF_ERROR_NONE) {
2N/A (void) scf_set_error(error);
2N/A return (SCF_FAILED);
2N/A }
2N/A
2N/A return (SCF_SUCCESS);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Adds entries for the given multi-valued property to the respective
2N/A * transaction entry of transaction initiated in scf_write_propvec().
2N/A */
2N/Aint
2N/Ascf_write_propvec_mval(scf_handle_t *h, scf_propvec_t *prop,
2N/A scf_transaction_entry_t *e)
2N/A{
2N/A int ret;
2N/A scf_value_t *v;
2N/A scf_propvec_mval_t *pv_ptr_node = prop->pv_ptr;
2N/A
2N/A assert(prop->pv_mval);
2N/A
2N/A for (; pv_ptr_node != NULL; pv_ptr_node = pv_ptr_node->next) {
2N/A if ((v = scf_value_create(h)) == NULL) {
2N/A return (SCF_FAILED);
2N/A }
2N/A
2N/A switch (prop->pv_type) {
2N/A case SCF_TYPE_BOOLEAN:
2N/A scf_value_set_boolean(v,
2N/A *(boolean_t *)pv_ptr_node->pv_ptr ? 1 : 0);
2N/A break;
2N/A case SCF_TYPE_COUNT:
2N/A scf_value_set_count(v,
2N/A *(uint64_t *)pv_ptr_node->pv_ptr);
2N/A break;
2N/A case SCF_TYPE_INTEGER:
2N/A scf_value_set_integer(v,
2N/A *(int64_t *)pv_ptr_node->pv_ptr);
2N/A break;
2N/A case SCF_TYPE_TIME: {
2N/A ret = scf_value_set_time(v,
2N/A ((scf_time_t *)pv_ptr_node->pv_ptr)->t_seconds,
2N/A ((scf_time_t *)pv_ptr_node->pv_ptr)->t_ns);
2N/A break;
2N/A }
2N/A case SCF_TYPE_OPAQUE: {
2N/A ret = scf_value_set_opaque(v,
2N/A ((scf_opaque_t *)pv_ptr_node->pv_ptr)->so_addr,
2N/A ((scf_opaque_t *)pv_ptr_node->pv_ptr)->so_size);
2N/A break;
2N/A }
2N/A case SCF_TYPE_ASTRING:
2N/A ret = scf_value_set_astring(v,
2N/A (const char *)pv_ptr_node->pv_ptr);
2N/A break;
2N/A default:
2N/A ret = scf_value_set_from_string(v, prop->pv_type,
2N/A (const char *)pv_ptr_node->pv_ptr);
2N/A }
2N/A
2N/A if (ret == -1 || scf_entry_add_value(e, v) == -1) {
2N/A scf_value_destroy(v);
2N/A return (SCF_FAILED);
2N/A }
2N/A }
2N/A
2N/A return (SCF_SUCCESS);
2N/A}
2N/A/*
2N/A * Writes a vector of properties to the specified fmri/property group.
2N/A *
2N/A * If this function fails to write a specific property, *badprop is set
2N/A * to point at that property's entry in the properties array.
2N/A *
2N/A * One significant difference between this function and the
2N/A * scf_read_propvec function is that for string types, pv_ptr is a
2N/A * char *, not a char **. This means that you can't write a propvec
2N/A * you just read, but makes other uses (hopefully the majority) simpler.
2N/A */
2N/Aint
2N/Ascf_write_propvec(const char *fmri, const char *pgname,
2N/A scf_propvec_t *properties, scf_propvec_t **badprop)
2N/A{
2N/A scf_handle_t *h = _scf_handle_create_and_bind(SCF_VERSION);
2N/A scf_service_t *s = scf_service_create(h);
2N/A scf_instance_t *inst = scf_instance_create(h);
2N/A scf_snapshot_t *snap = scf_snapshot_create(h);
2N/A scf_propertygroup_t *pg = scf_pg_create(h);
2N/A scf_property_t *p = scf_property_create(h);
2N/A scf_transaction_t *tx = scf_transaction_create(h);
2N/A scf_value_t *v;
2N/A scf_transaction_entry_t *e;
2N/A boolean_t instance = B_TRUE;
2N/A int i, ret;
2N/A scf_propvec_t *prop;
2N/A scf_error_t error = SCF_ERROR_NONE;
2N/A
2N/A if (h == NULL || s == NULL || inst == NULL || pg == NULL || p == NULL ||
2N/A tx == NULL)
2N/A goto scferror;
2N/A
2N/A if (scf_handle_decode_fmri(h, fmri, NULL, s, inst, NULL, NULL, 0)
2N/A != SCF_SUCCESS)
2N/A goto scferror;
2N/A
2N/A if (scf_instance_to_fmri(inst, NULL, 0) == -1) {
2N/A if (scf_error() != SCF_ERROR_NOT_SET)
2N/A goto scferror;
2N/A instance = B_FALSE;
2N/A }
2N/A
2N/A if ((instance ? scf_instance_get_pg(inst, pgname, pg) :
2N/A scf_service_get_pg(s, pgname, pg)) == -1)
2N/A goto scferror;
2N/A
2N/Atop:
2N/A if (scf_transaction_start(tx, pg) == -1)
2N/A goto scferror;
2N/A
2N/A for (prop = properties, i = 0; prop->pv_prop != NULL; prop++, i++) {
2N/A if ((e = scf_entry_create(h)) == NULL) {
2N/A *badprop = prop;
2N/A goto scferror;
2N/A }
2N/A
2N/A ret = scf_transaction_property_change(tx, e, prop->pv_prop,
2N/A prop->pv_type);
2N/A if (ret == -1 && scf_error() == SCF_ERROR_NOT_FOUND)
2N/A ret = scf_transaction_property_new(tx, e,
2N/A prop->pv_prop, prop->pv_type);
2N/A
2N/A if (ret == -1) {
2N/A *badprop = prop;
2N/A scf_entry_destroy(e);
2N/A goto scferror;
2N/A }
2N/A
2N/A if (prop->pv_mval) {
2N/A if (scf_write_propvec_mval(h, prop, e) != SCF_SUCCESS) {
2N/A *badprop = prop;
2N/A goto scferror;
2N/A }
2N/A continue;
2N/A }
2N/A
2N/A if ((v = scf_value_create(h)) == NULL) {
2N/A *badprop = prop;
2N/A goto scferror;
2N/A }
2N/A
2N/A switch (prop->pv_type) {
2N/A case SCF_TYPE_BOOLEAN: {
2N/A boolean_t b = (prop->pv_aux != 0) ?
2N/A (*(uint64_t *)prop->pv_ptr & prop->pv_aux) != 0 :
2N/A *(boolean_t *)prop->pv_ptr;
2N/A
2N/A scf_value_set_boolean(v, b ? 1 : 0);
2N/A break;
2N/A }
2N/A case SCF_TYPE_COUNT:
2N/A scf_value_set_count(v, *(uint64_t *)prop->pv_ptr);
2N/A break;
2N/A case SCF_TYPE_INTEGER:
2N/A scf_value_set_integer(v, *(int64_t *)prop->pv_ptr);
2N/A break;
2N/A case SCF_TYPE_TIME: {
2N/A scf_time_t *time = prop->pv_ptr;
2N/A
2N/A ret = scf_value_set_time(v, time->t_seconds,
2N/A time->t_ns);
2N/A break;
2N/A }
2N/A case SCF_TYPE_OPAQUE: {
2N/A scf_opaque_t *opaque = prop->pv_ptr;
2N/A
2N/A ret = scf_value_set_opaque(v, opaque->so_addr,
2N/A opaque->so_size);
2N/A break;
2N/A }
2N/A case SCF_TYPE_ASTRING:
2N/A ret = scf_value_set_astring(v,
2N/A (const char *)prop->pv_ptr);
2N/A break;
2N/A default:
2N/A ret = scf_value_set_from_string(v, prop->pv_type,
2N/A (const char *)prop->pv_ptr);
2N/A }
2N/A
2N/A if (ret == -1 || scf_entry_add_value(e, v) == -1) {
2N/A *badprop = prop;
2N/A scf_value_destroy(v);
2N/A goto scferror;
2N/A }
2N/A }
2N/A
2N/A ret = scf_transaction_commit(tx);
2N/A if (ret == 1)
2N/A goto out;
2N/A
2N/A if (ret == 0 && scf_pg_update(pg) != -1) {
2N/A scf_transaction_destroy_children(tx);
2N/A goto top;
2N/A }
2N/A
2N/Ascferror:
2N/A error = scf_error();
2N/A
2N/Aout:
2N/A scf_transaction_destroy_children(tx);
2N/A scf_transaction_destroy(tx);
2N/A scf_property_destroy(p);
2N/A scf_pg_destroy(pg);
2N/A scf_snapshot_destroy(snap);
2N/A scf_instance_destroy(inst);
2N/A scf_service_destroy(s);
2N/A scf_handle_destroy(h);
2N/A
2N/A if (error != SCF_ERROR_NONE) {
2N/A (void) scf_set_error(error);
2N/A return (SCF_FAILED);
2N/A }
2N/A
2N/A return (SCF_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * Returns
2N/A * 0 - success
2N/A * ECONNABORTED - repository connection broken
2N/A * ECANCELED - inst was deleted
2N/A * EPERM
2N/A * EACCES
2N/A * EROFS
2N/A * ENOMEM
2N/A */
2N/Aint
2N/Ascf_instance_delete_prop(scf_instance_t *inst, const char *pgname,
2N/A const char *pname)
2N/A{
2N/A scf_handle_t *h;
2N/A scf_propertygroup_t *pg;
2N/A scf_transaction_t *tx;
2N/A scf_transaction_entry_t *e;
2N/A scf_error_t error = SCF_ERROR_NONE;
2N/A int ret = 1, r;
2N/A
2N/A h = scf_instance_handle(inst);
2N/A
2N/A if ((pg = scf_pg_create(h)) == NULL) {
2N/A return (ENOMEM);
2N/A }
2N/A
2N/A if (scf_instance_get_pg(inst, pgname, pg) != 0) {
2N/A error = scf_error();
2N/A scf_pg_destroy(pg);
2N/A switch (error) {
2N/A case SCF_ERROR_NOT_FOUND:
2N/A return (SCF_SUCCESS);
2N/A
2N/A case SCF_ERROR_DELETED:
2N/A return (ECANCELED);
2N/A
2N/A case SCF_ERROR_CONNECTION_BROKEN:
2N/A default:
2N/A return (ECONNABORTED);
2N/A
2N/A case SCF_ERROR_NOT_SET:
2N/A bad_error("scf_instance_get_pg", scf_error());
2N/A }
2N/A }
2N/A
2N/A tx = scf_transaction_create(h);
2N/A e = scf_entry_create(h);
2N/A if (tx == NULL || e == NULL) {
2N/A ret = ENOMEM;
2N/A goto out;
2N/A }
2N/A
2N/A for (;;) {
2N/A if (scf_transaction_start(tx, pg) != 0) {
2N/A goto scferror;
2N/A }
2N/A
2N/A if (scf_transaction_property_delete(tx, e, pname) != 0) {
2N/A goto scferror;
2N/A }
2N/A
2N/A if ((r = scf_transaction_commit(tx)) == 1) {
2N/A ret = 0;
2N/A goto out;
2N/A }
2N/A
2N/A if (r == -1) {
2N/A goto scferror;
2N/A }
2N/A
2N/A scf_transaction_reset(tx);
2N/A if (scf_pg_update(pg) == -1) {
2N/A goto scferror;
2N/A }
2N/A }
2N/A
2N/Ascferror:
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_DELETED:
2N/A case SCF_ERROR_NOT_FOUND:
2N/A ret = 0;
2N/A break;
2N/A
2N/A case SCF_ERROR_PERMISSION_DENIED:
2N/A ret = EPERM;
2N/A break;
2N/A
2N/A case SCF_ERROR_BACKEND_ACCESS:
2N/A ret = EACCES;
2N/A break;
2N/A
2N/A case SCF_ERROR_BACKEND_READONLY:
2N/A ret = EROFS;
2N/A break;
2N/A
2N/A case SCF_ERROR_CONNECTION_BROKEN:
2N/A default:
2N/A ret = ECONNABORTED;
2N/A break;
2N/A
2N/A case SCF_ERROR_HANDLE_MISMATCH:
2N/A case SCF_ERROR_INVALID_ARGUMENT:
2N/A case SCF_ERROR_NOT_BOUND:
2N/A case SCF_ERROR_NOT_SET:
2N/A bad_error("scf_instance_delete_prop", scf_error());
2N/A }
2N/A
2N/Aout:
2N/A scf_transaction_destroy(tx);
2N/A scf_entry_destroy(e);
2N/A scf_pg_destroy(pg);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Check the "application/auto_enable" property for the passed FMRI.
2N/A * scf_simple_prop_get() should find the property on an instance
2N/A * or on the service FMRI. The routine returns:
2N/A * -1: inconclusive (likely no such property or FMRI)
2N/A * 0: auto_enable is false
2N/A * 1: auto_enable is true
2N/A */
2N/Astatic int
2N/Ais_auto_enabled(char *fmri)
2N/A{
2N/A scf_simple_prop_t *prop;
2N/A int retval = -1;
2N/A uint8_t *ret;
2N/A
2N/A prop = scf_simple_prop_get(NULL, fmri, SCF_GROUP_APPLICATION,
2N/A "auto_enable");
2N/A if (!prop)
2N/A return (retval);
2N/A ret = scf_simple_prop_next_boolean(prop);
2N/A retval = (*ret != 0);
2N/A scf_simple_prop_free(prop);
2N/A return (retval);
2N/A}
2N/A
2N/A/*
2N/A * Check an array of services and enable any that don't have the
2N/A * "application/auto_enable" property set to "false", which is
2N/A * the interface to turn off this behaviour (see PSARC 2004/739).
2N/A */
2N/Avoid
2N/A_check_services(char **svcs)
2N/A{
2N/A char *s;
2N/A
2N/A for (; *svcs; svcs++) {
2N/A if (is_auto_enabled(*svcs) == 0)
2N/A continue;
2N/A if ((s = smf_get_state(*svcs)) != NULL) {
2N/A if (strcmp(SCF_STATE_STRING_DISABLED, s) == 0)
2N/A (void) smf_enable_instance(*svcs,
2N/A SMF_TEMPORARY);
2N/A free(s);
2N/A }
2N/A }
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic int
2N/Astr_compare(const char *s1, const char *s2, size_t n)
2N/A{
2N/A return (strcmp(s1, s2));
2N/A}
2N/A
2N/Astatic int
2N/Astr_n_compare(const char *s1, const char *s2, size_t n)
2N/A{
2N/A return (strncmp(s1, s2, n));
2N/A}
2N/A
2N/Aint32_t
2N/Astate_from_string(const char *state, size_t l)
2N/A{
2N/A int (*str_cmp)(const char *, const char *, size_t);
2N/A
2N/A if (l == 0)
2N/A str_cmp = str_compare;
2N/A else
2N/A str_cmp = str_n_compare;
2N/A
2N/A if (str_cmp(SCF_STATE_STRING_UNINIT, state, l) == 0)
2N/A return (SCF_STATE_UNINIT);
2N/A else if (str_cmp(SCF_STATE_STRING_MAINT, state, l) == 0)
2N/A return (SCF_STATE_MAINT);
2N/A else if (str_cmp(SCF_STATE_STRING_OFFLINE, state, l) == 0)
2N/A return (SCF_STATE_OFFLINE);
2N/A else if (str_cmp(SCF_STATE_STRING_DISABLED, state, l) == 0)
2N/A return (SCF_STATE_DISABLED);
2N/A else if (str_cmp(SCF_STATE_STRING_ONLINE, state, l) == 0)
2N/A return (SCF_STATE_ONLINE);
2N/A else if (str_cmp(SCF_STATE_STRING_DEGRADED, state, l) == 0)
2N/A return (SCF_STATE_DEGRADED);
2N/A else if (str_cmp("all", state, l) == 0)
2N/A return (SCF_STATE_ALL);
2N/A else
2N/A return (-1);
2N/A}
2N/A
2N/A/*
2N/A * int32_t smf_state_from_string()
2N/A * return the value of the macro SCF_STATE_* for the corresponding state
2N/A * it returns SCF_STATE_ALL if "all" is passed. -1 if the string passed doesn't
2N/A * correspond to any valid state.
2N/A */
2N/Aint32_t
2N/Asmf_state_from_string(const char *state)
2N/A{
2N/A return (state_from_string(state, 0));
2N/A}
2N/A
2N/A/*
2N/A * smf_state_to_string()
2N/A * Takes an int32_t representing an SMF state and returns
2N/A * the corresponding string. The string is read only and need not to be
2N/A * freed.
2N/A * returns NULL on invalid input.
2N/A */
2N/Aconst char *
2N/Asmf_state_to_string(int32_t s)
2N/A{
2N/A switch (s) {
2N/A case SCF_STATE_UNINIT:
2N/A return (SCF_STATE_STRING_UNINIT);
2N/A case SCF_STATE_MAINT:
2N/A return (SCF_STATE_STRING_MAINT);
2N/A case SCF_STATE_OFFLINE:
2N/A return (SCF_STATE_STRING_OFFLINE);
2N/A case SCF_STATE_DISABLED:
2N/A return (SCF_STATE_STRING_DISABLED);
2N/A case SCF_STATE_ONLINE:
2N/A return (SCF_STATE_STRING_ONLINE);
2N/A case SCF_STATE_DEGRADED:
2N/A return (SCF_STATE_STRING_DEGRADED);
2N/A case SCF_STATE_ALL:
2N/A return ("all");
2N/A default:
2N/A return (NULL);
2N/A }
2N/A}
2N/A
2N/Astatic int
2N/Asmf_take_snapshot(scf_instance_t *inst, const char *name, scf_snapshot_t *snap)
2N/A{
2N/A scf_snapshot_t *lsnap = NULL;
2N/A scf_handle_t *h;
2N/A
2N/A if (snap == NULL) {
2N/A h = inst->rd_d.rd_handle;
2N/A
2N/A lsnap = scf_snapshot_create(h);
2N/A snap = lsnap;
2N/A }
2N/A
2N/Aagain:
2N/A if (scf_instance_get_snapshot(inst, name, snap) == 0) {
2N/A if (_scf_snapshot_take_attach(inst, snap) != 0) {
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_CONNECTION_BROKEN:
2N/A case SCF_ERROR_PERMISSION_DENIED:
2N/A case SCF_ERROR_NO_RESOURCES:
2N/A case SCF_ERROR_NOT_SET:
2N/A case SCF_ERROR_INVALID_ARGUMENT:
2N/A default:
2N/A scf_snapshot_destroy(lsnap);
2N/A return (-1);
2N/A }
2N/A }
2N/A } else {
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_NOT_FOUND:
2N/A break;
2N/A
2N/A case SCF_ERROR_DELETED:
2N/A case SCF_ERROR_CONNECTION_BROKEN:
2N/A case SCF_ERROR_HANDLE_MISMATCH:
2N/A case SCF_ERROR_NOT_BOUND:
2N/A case SCF_ERROR_INVALID_ARGUMENT:
2N/A case SCF_ERROR_NOT_SET:
2N/A default:
2N/A scf_snapshot_destroy(lsnap);
2N/A return (-1);
2N/A }
2N/A
2N/A if (_scf_snapshot_take_new(inst, name, snap) != 0) {
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_EXISTS:
2N/A goto again;
2N/A
2N/A case SCF_ERROR_CONNECTION_BROKEN:
2N/A case SCF_ERROR_NO_RESOURCES:
2N/A case SCF_ERROR_PERMISSION_DENIED:
2N/A case SCF_ERROR_NOT_SET:
2N/A case SCF_ERROR_INTERNAL:
2N/A case SCF_ERROR_INVALID_ARGUMENT:
2N/A case SCF_ERROR_HANDLE_MISMATCH:
2N/A scf_snapshot_destroy(lsnap);
2N/A return (-1);
2N/A
2N/A default:
2N/A scf_snapshot_destroy(lsnap);
2N/A return (-1);
2N/A }
2N/A }
2N/A }
2N/A
2N/A
2N/A scf_snapshot_destroy(lsnap);
2N/A return (0);
2N/A}
2N/A
2N/Astatic int
2N/Aset_restarter(const char *restarter_fmri, char *strrest, scf_handle_t *h,
2N/A scf_instance_t *inst, scf_propertygroup_t *pg, scf_value_t *v)
2N/A{
2N/A scf_transaction_t *tx = NULL;
2N/A scf_transaction_entry_t *ent = NULL;
2N/A int committed;
2N/A
2N/A ssize_t max_fmri_length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
2N/A int ret = -1;
2N/A
2N/A if ((tx = scf_transaction_create(h)) == NULL ||
2N/A (ent = scf_entry_create(h)) == NULL) {
2N/A goto out;
2N/A }
2N/A
2N/A /* Make sure that the restarter fmri is fully qualified. */
2N/A if (restarter_fmri != NULL &&
2N/A scf_canonify_fmri(restarter_fmri, strrest, max_fmri_length) <= 0) {
2N/A (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
2N/A
2N/A goto out;
2N/A }
2N/A
2N/A do {
2N/A if (scf_transaction_start(tx, pg) == -1)
2N/A goto out;
2N/A
2N/A if ((restarter_fmri == NULL &&
2N/A scf_transaction_property_delcust(tx, ent,
2N/A SCF_PROPERTY_RESTARTER)) ||
2N/A (restarter_fmri != NULL && transaction_property_set(tx, ent,
2N/A SCF_PROPERTY_RESTARTER, SCF_TYPE_FMRI) != 0)) {
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_CONNECTION_BROKEN:
2N/A case SCF_ERROR_DELETED:
2N/A default:
2N/A goto out;
2N/A
2N/A case SCF_ERROR_HANDLE_MISMATCH:
2N/A case SCF_ERROR_INVALID_ARGUMENT:
2N/A case SCF_ERROR_NOT_BOUND:
2N/A case SCF_ERROR_NOT_SET:
2N/A bad_error("transaction_property_set",
2N/A scf_error());
2N/A }
2N/A }
2N/A
2N/A if (restarter_fmri != NULL) {
2N/A if (scf_value_set_from_string(v, SCF_TYPE_FMRI,
2N/A strrest) == -1 ||
2N/A scf_entry_add_value(ent, v) == -1) {
2N/A goto out;
2N/A }
2N/A }
2N/A
2N/A committed = scf_transaction_commit(tx);
2N/A if (committed == -1)
2N/A goto out;
2N/A
2N/A scf_transaction_reset(tx);
2N/A
2N/A if (committed == 0) {
2N/A if (scf_pg_update(pg) == -1)
2N/A goto out;
2N/A }
2N/A } while (committed == 0);
2N/A
2N/A /*
2N/A * Update the running snapshot so that the new restarter is properly
2N/A * visible.
2N/A */
2N/A ret = smf_take_snapshot(inst, "running", NULL);
2N/Aout:
2N/A scf_entry_destroy(ent);
2N/A scf_transaction_destroy(tx);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * smf_set_restarter()
2N/A * Takes two char strings, each an fmri representation. The first is the
2N/A * fmri for the instance whose restarter is to be set. The second is the fmri
2N/A * for the delegated restarter.
2N/A *
2N/A * Validate that the restarter fmri is a valid fmri instance.
2N/A */
2N/Aint
2N/Asmf_set_restarter(const char *fmri, const char *restarter_fmri)
2N/A{
2N/A scf_handle_t *h = NULL;
2N/A scf_instance_t *inst = NULL;
2N/A scf_propertygroup_t *pg = NULL;
2N/A scf_property_t *prop = NULL;
2N/A scf_value_t *v = NULL;
2N/A ssize_t max_fmri_length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
2N/A char *strrest = NULL;
2N/A int ret = -1;
2N/A
2N/A assert(max_fmri_length != -1);
2N/A
2N/A if ((strrest = malloc(max_fmri_length)) == NULL) {
2N/A (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2N/A return (ret);
2N/A }
2N/A
2N/A if ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL ||
2N/A (inst = scf_instance_create(h)) == NULL ||
2N/A (pg = scf_pg_create(h)) == NULL ||
2N/A (prop = scf_property_create(h)) == NULL ||
2N/A (v = scf_value_create(h)) == NULL) {
2N/A goto out;
2N/A }
2N/A
2N/A if (restarter_fmri != NULL &&
2N/A scf_handle_decode_fmri(h, restarter_fmri, NULL, NULL, inst, NULL,
2N/A NULL,
2N/A SCF_DECODE_FMRI_EXACT|SCF_DECODE_FMRI_REQUIRE_INSTANCE) == -1) {
2N/A if (scf_error() != SCF_ERROR_NOT_FOUND)
2N/A goto out;
2N/A }
2N/A
2N/A if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL, NULL,
2N/A SCF_DECODE_FMRI_EXACT) == -1) {
2N/A if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
2N/A (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
2N/A
2N/A goto out;
2N/A }
2N/A
2N/A if (scf_instance_get_pg(inst, SCF_PG_GENERAL, pg) == -1) {
2N/A if (scf_error() != SCF_ERROR_NOT_FOUND)
2N/A goto out;
2N/A
2N/A if (scf_instance_add_pg(inst, SCF_PG_GENERAL,
2N/A SCF_GROUP_FRAMEWORK, SCF_PG_GENERAL_FLAGS, pg) == -1) {
2N/A if (scf_error() != SCF_ERROR_EXISTS)
2N/A goto out;
2N/A
2N/A }
2N/A }
2N/A
2N/A if (scf_pg_get_property(pg, SCF_PROPERTY_RESTARTER, prop) == -1) {
2N/A if (scf_error() == SCF_ERROR_NOT_FOUND ||
2N/A scf_error() == SCF_ERROR_DELETED) {
2N/A if (restarter_fmri != NULL) {
2N/A ret = set_restarter(restarter_fmri, strrest, h,
2N/A inst, pg, v);
2N/A goto out;
2N/A } else {
2N/A ret = 0;
2N/A }
2N/A }
2N/A
2N/A goto out;
2N/A }
2N/A
2N/A if (restarter_fmri == NULL) {
2N/A ret = set_restarter(restarter_fmri, strrest, h, inst, pg, v);
2N/A goto out;
2N/A }
2N/A
2N/A if (scf_property_get_value(prop, v) == -1) {
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_CONSTRAINT_VIOLATED:
2N/A case SCF_ERROR_NOT_FOUND:
2N/A ret = set_restarter(restarter_fmri, strrest, h, inst,
2N/A pg, v);
2N/A default:
2N/A goto out;
2N/A }
2N/A }
2N/A
2N/A if (scf_value_get_astring(v, strrest, max_fmri_length) == -1) {
2N/A if (scf_error() != SCF_ERROR_TYPE_MISMATCH)
2N/A goto out;
2N/A
2N/A ret = set_restarter(restarter_fmri, strrest, h, inst, pg, v);
2N/A goto out;
2N/A }
2N/A
2N/A if (strcmp(strrest, restarter_fmri) == 0) {
2N/A ret = 0;
2N/A goto out;
2N/A }
2N/A
2N/A ret = set_restarter(restarter_fmri, strrest, h, inst, pg, v);
2N/A
2N/Aout:
2N/A scf_value_destroy(v);
2N/A scf_property_destroy(prop);
2N/A scf_pg_destroy(pg);
2N/A scf_instance_destroy(inst);
2N/A scf_handle_destroy(h);
2N/A free(strrest);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * It is the responsibility of the caller to free the dfmri if it is returned.
2N/A * dfmri can be set if the rfmri is NULL, requesting a reset of the restarter.
2N/A * The dfmri will be filled in with the restarter under the admin layer for
2N/A * reporting purposes by the caller.
2N/A *
2N/A * return :
2N/A * -2 failed to get the restarter state
2N/A * -1 restarter does not exist
2N/A * 0 restarter is in a valid state (e.g online)
2N/A * restarter_state restarter state that needs to be managed
2N/A */
2N/Aint
2N/Asmf_restarter_has_potential(const char *rfmri, const char *inst,
2N/A char **dfmri)
2N/A{
2N/A size_t fmri_sz = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
2N/A char *d = NULL;
2N/A char *state_str;
2N/A int32_t state;
2N/A
2N/A assert(rfmri != NULL || inst != NULL);
2N/A
2N/A /*
2N/A * If the rfmri is NULL, then this is a reset, so we have to get the
2N/A * restarter below admin layer restarter and determine its state and
2N/A * return.
2N/A *
2N/A * If there is no admin layer then return (0) as the restarter is not
2N/A * going to be reset so we are fine.
2N/A *
2N/A * If there is an admin layer take the entry just below that.
2N/A */
2N/A if (rfmri == NULL) {
2N/A scf_decoration_layer_t hl = SCF_DECORATION_INVALID;
2N/A scf_decoration_layer_t l;
2N/A scf_handle_t *h;
2N/A scf_property_t *prop = NULL;
2N/A scf_decoration_t *dec = NULL;
2N/A scf_value_t *val = NULL;
2N/A scf_iter_t *iter = NULL;
2N/A int iter_ret;
2N/A
2N/A h = _scf_handle_create_and_bind(SCF_VERSION);
2N/A if (h == NULL)
2N/A return (-2);
2N/A
2N/A prop = scf_prop_get(h, inst, SCF_PG_GENERAL,
2N/A SCF_PROPERTY_RESTARTER);
2N/A
2N/A if (prop == NULL) {
2N/A scf_handle_destroy(h);
2N/A if (scf_error() == SCF_ERROR_NOT_FOUND)
2N/A return (0);
2N/A
2N/A return (-2);
2N/A }
2N/A
2N/A if ((iter = scf_iter_create(h)) == NULL ||
2N/A (dec = scf_decoration_create(h)) == NULL ||
2N/A (val = scf_value_create(h)) == NULL) {
2N/A scf_property_destroy(prop);
2N/A scf_iter_destroy(iter);
2N/A scf_decoration_destroy(dec);
2N/A scf_value_destroy(val);
2N/A scf_handle_destroy(h);
2N/A
2N/A return (-2);
2N/A }
2N/A
2N/A if (scf_iter_property_decorations(iter, prop, 0)) {
2N/A scf_decoration_destroy(dec);
2N/A scf_property_destroy(prop);
2N/A scf_value_destroy(val);
2N/A scf_iter_destroy(iter);
2N/A scf_handle_destroy(h);
2N/A
2N/A return (-2);
2N/A }
2N/A
2N/A if ((d = malloc(fmri_sz)) == NULL) {
2N/A scf_decoration_destroy(dec);
2N/A scf_property_destroy(prop);
2N/A scf_value_destroy(val);
2N/A scf_iter_destroy(iter);
2N/A scf_handle_destroy(h);
2N/A
2N/A return (-2);
2N/A }
2N/A
2N/A while ((iter_ret = scf_iter_next_decoration(iter, dec)) == 1) {
2N/A if (scf_decoration_get_layer(dec, &l)) {
2N/A iter_ret = -1;
2N/A break;
2N/A }
2N/A
2N/A /*
2N/A * If the layer is not an admin layer, and it's greater
2N/A * than the current higheset found layer then lets get
2N/A * the value for the restarter and set the rmfri.
2N/A */
2N/A if (l > SCF_CUSTOMIZATION_BOUNDARY || l < hl)
2N/A continue;
2N/A
2N/A hl = l;
2N/A
2N/A if (scf_decoration_get_value(dec, val) != 0 ||
2N/A scf_value_get_astring(val, d, fmri_sz) <= 0) {
2N/A iter_ret = -1;
2N/A break;
2N/A }
2N/A
2N/A }
2N/A
2N/A scf_decoration_destroy(dec);
2N/A scf_property_destroy(prop);
2N/A scf_value_destroy(val);
2N/A scf_iter_destroy(iter);
2N/A scf_handle_destroy(h);
2N/A
2N/A if (iter_ret == -1) {
2N/A free(d);
2N/A return (-2);
2N/A }
2N/A
2N/A *dfmri = d;
2N/A if (hl == SCF_DECORATION_INVALID)
2N/A return (0);
2N/A
2N/A rfmri = d;
2N/A }
2N/A
2N/A state_str = smf_get_state(rfmri);
2N/A if (state_str == NULL)
2N/A return (-1);
2N/A
2N/A state = smf_state_from_string(state_str);
2N/A
2N/A if (state == SCF_STATE_ONLINE)
2N/A return (0);
2N/A
2N/A return (state);
2N/A}
2N/A
2N/Avoid
2N/Asmf_method_exit(int semantic, const char *msg_short, const char *msg_long,
2N/A const char *textdomain)
2N/A{
2N/A char *f = NULL;
2N/A scf_handle_t *h = NULL;
2N/A scf_transaction_t *t = NULL;
2N/A scf_transaction_entry_t *t_ms = NULL, *t_ml = NULL, *t_td = NULL;
2N/A scf_value_t *v_ms = NULL, *v_ml = NULL, *v_td = NULL;
2N/A scf_instance_t *i = NULL;
2N/A scf_propertygroup_t *pg = NULL;
2N/A char pgname[] = "smf_method_exit_XXX";
2N/A int commit_retry;
2N/A
2N/A ssize_t fmrilen = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH) + 1;
2N/A
2N/A if ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL ||
2N/A (i = scf_instance_create(h)) == NULL ||
2N/A (pg = scf_pg_create(h)) == NULL ||
2N/A (t = scf_transaction_create(h)) == NULL ||
2N/A (t_ms = scf_entry_create(h)) == NULL ||
2N/A (t_ml = scf_entry_create(h)) == NULL ||
2N/A (v_ms = scf_value_create(h)) == NULL ||
2N/A (v_ml = scf_value_create(h)) == NULL ||
2N/A scf_value_set_astring(v_ms, msg_short) != 0 ||
2N/A scf_value_set_astring(v_ml, msg_long) != 0 ||
2N/A (textdomain != NULL &&
2N/A ((t_td = scf_entry_create(h)) == NULL ||
2N/A (v_td = scf_value_create(h)) == NULL ||
2N/A scf_value_set_astring(v_td, textdomain) != 0)) ||
2N/A (f = malloc(fmrilen)) == NULL ||
2N/A scf_myname(h, f, fmrilen) == -1 ||
2N/A scf_handle_decode_fmri(h, f, NULL, NULL, i,
2N/A NULL, NULL, SCF_DECODE_FMRI_EXACT) == -1) {
2N/A goto failed;
2N/A }
2N/A
2N/A (void) snprintf(pgname, sizeof (pgname),
2N/A "smf_method_exit_%-3d", semantic);
2N/A
2N/A if (scf_instance_add_pg(i, pgname, SCF_GROUP_FRAMEWORK,
2N/A SCF_PG_FLAG_NONPERSISTENT, pg) != 0) {
2N/A if (scf_error() != SCF_ERROR_EXISTS)
2N/A goto failed;
2N/A
2N/A if (scf_instance_get_pg(i, pgname, pg) != 0)
2N/A goto failed;
2N/A }
2N/A
2N/A for (commit_retry = 0; commit_retry < 10; commit_retry++) {
2N/A
2N/A if (scf_transaction_start(t, pg) != 0)
2N/A goto failed;
2N/A
2N/A if ((scf_transaction_property_new(t, t_ms,
2N/A SCF_PROPERTY_AUX_STATE_CUSTOM, SCF_TYPE_ASTRING) != 0 &&
2N/A (scf_error() != SCF_ERROR_EXISTS ||
2N/A scf_transaction_property_change_type(t, t_ms,
2N/A SCF_PROPERTY_AUX_STATE_CUSTOM, SCF_TYPE_ASTRING) != 0)) ||
2N/A scf_entry_add_value(t_ms, v_ms) != 0)
2N/A goto failed;
2N/A
2N/A if ((scf_transaction_property_new(t, t_ml,
2N/A SCF_PROPERTY_AUX_REASON, SCF_TYPE_ASTRING) != 0 &&
2N/A (scf_error() != SCF_ERROR_EXISTS ||
2N/A scf_transaction_property_change_type(t, t_ml,
2N/A SCF_PROPERTY_AUX_REASON, SCF_TYPE_ASTRING) != 0)) ||
2N/A scf_entry_add_value(t_ml, v_ml) != 0)
2N/A goto failed;
2N/A
2N/A if (textdomain != NULL &&
2N/A ((scf_transaction_property_new(t, t_td,
2N/A SCF_PROPERTY_AUX_TEXTDOMAIN,
2N/A SCF_TYPE_ASTRING) != 0 &&
2N/A (scf_error() != SCF_ERROR_EXISTS ||
2N/A scf_transaction_property_change_type(t, t_td,
2N/A SCF_PROPERTY_AUX_TEXTDOMAIN, SCF_TYPE_ASTRING) != 0)) ||
2N/A scf_entry_add_value(t_td, v_td) != 0))
2N/A goto failed;
2N/A
2N/A switch (scf_transaction_commit(t)) {
2N/A case -1:
2N/A goto failed;
2N/A
2N/A case 0:
2N/A scf_transaction_reset(t);
2N/A if (scf_pg_update(pg) != 0)
2N/A goto failed;
2N/A break;
2N/A
2N/A case 1:
2N/A exit(semantic);
2N/A }
2N/A }
2N/A
2N/Afailed:
2N/A
2N/A /*
2N/A * If semantic requires smf_method_exit(), it may be ignored by the
2N/A * restarter. If smf_method_exit() was simply used to provide custom
2N/A * messages, then semantic may still be honored, even if those messages
2N/A * were not successfully transmitted.
2N/A */
2N/A exit(semantic);
2N/A}