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) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/*
2N/A * This provides the basic mechanisms (str2nvl and nvl2str) for dealing with
2N/A * the service schema. The official version of a svc FMRI has the form:
2N/A *
2N/A * svc://[scope@][system-fqn]/service[:instance][@contract-id]
2N/A *
2N/A * Where 'service' is a slash-delimited list of names. Of these fields, the
2N/A * scope, constract-id, and system-fqn are rarely used, leaving the much more
2N/A * common form such as:
2N/A *
2N/A * svc:///network/ssh:default
2N/A *
2N/A * Note that the SMF software typically uses a shorthard form, where the
2N/A * authority is elided (svc:/network/ssh:default). As this module deals with
2N/A * FMA FMRIs, we only support fully specified FMRIs.
2N/A *
2N/A * This module does not support enumeration, but implements methods for FMRI
2N/A * state (service state, and presence_state).
2N/A */
2N/A
2N/A#include <fm/topo_mod.h>
2N/A#include <fm/fmd_fmri.h>
2N/A#include <sys/fm/protocol.h>
2N/A#include <topo_method.h>
2N/A#include <topo_subr.h>
2N/A#include <topo_prop.h>
2N/A#include <alloca.h>
2N/A#include <assert.h>
2N/A#include <svc.h>
2N/A#include <strings.h>
2N/A#include <libscf.h>
2N/A
2N/Astatic int svc_fmri_nvl2str(topo_mod_t *, tnode_t *, topo_version_t,
2N/A nvlist_t *, nvlist_t **);
2N/Astatic int svc_fmri_str2nvl(topo_mod_t *, tnode_t *, topo_version_t,
2N/A nvlist_t *, nvlist_t **);
2N/Astatic int svc_fmri_presence_state(topo_mod_t *, tnode_t *, topo_version_t,
2N/A nvlist_t *, nvlist_t **);
2N/Astatic int svc_fmri_service_state(topo_mod_t *, tnode_t *, topo_version_t,
2N/A nvlist_t *, nvlist_t **);
2N/Astatic int svc_fmri_prop_get(topo_mod_t *, tnode_t *, topo_version_t,
2N/A nvlist_t *, nvlist_t **);
2N/A
2N/Astatic const topo_method_t svc_methods[] = {
2N/A { TOPO_METH_PROP_GET, TOPO_METH_PROP_GET_DESC,
2N/A TOPO_METH_PROP_GET_VERSION, TOPO_STABILITY_INTERNAL,
2N/A svc_fmri_prop_get },
2N/A { TOPO_METH_NVL2STR, TOPO_METH_NVL2STR_DESC, TOPO_METH_NVL2STR_VERSION,
2N/A TOPO_STABILITY_INTERNAL, svc_fmri_nvl2str },
2N/A { TOPO_METH_STR2NVL, TOPO_METH_STR2NVL_DESC, TOPO_METH_STR2NVL_VERSION,
2N/A TOPO_STABILITY_INTERNAL, svc_fmri_str2nvl },
2N/A { TOPO_METH_PRESENCE_STATE, TOPO_METH_PRESENCE_STATE_DESC,
2N/A TOPO_METH_PRESENCE_STATE_VERSION, TOPO_STABILITY_INTERNAL,
2N/A svc_fmri_presence_state },
2N/A { TOPO_METH_SERVICE_STATE, TOPO_METH_SERVICE_STATE_DESC,
2N/A TOPO_METH_SERVICE_STATE_VERSION, TOPO_STABILITY_INTERNAL,
2N/A svc_fmri_service_state },
2N/A { NULL }
2N/A};
2N/A
2N/Astatic int svc_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
2N/A topo_instance_t, void *, void *);
2N/Astatic void svc_release(topo_mod_t *, tnode_t *);
2N/A
2N/Astatic const topo_modops_t svc_ops =
2N/A { svc_enum, svc_release };
2N/Astatic const topo_modinfo_t svc_info =
2N/A { "svc", FM_FMRI_SCHEME_SVC, SVC_VERSION, &svc_ops };
2N/A
2N/Astatic int
2N/Asvc_error(topo_mod_t *mod)
2N/A{
2N/A switch (scf_error()) {
2N/A case SCF_ERROR_NO_MEMORY:
2N/A return (topo_mod_seterrno(mod, EMOD_NOMEM));
2N/A
2N/A default:
2N/A return (topo_mod_seterrno(mod, EMOD_UNKNOWN));
2N/A }
2N/A}
2N/A
2N/Astatic scf_handle_t *
2N/Asvc_get_handle(topo_mod_t *mod)
2N/A{
2N/A scf_handle_t *hdl = topo_mod_getspecific(mod);
2N/A
2N/A if (hdl != NULL)
2N/A return (hdl);
2N/A
2N/A if ((hdl = scf_handle_create(SCF_VERSION)) == NULL) {
2N/A (void) svc_error(mod);
2N/A return (NULL);
2N/A }
2N/A
2N/A if (scf_handle_bind(hdl) != 0) {
2N/A scf_handle_destroy(hdl);
2N/A (void) svc_error(mod);
2N/A return (NULL);
2N/A }
2N/A
2N/A topo_mod_setspecific(mod, hdl);
2N/A
2N/A return (hdl);
2N/A}
2N/A
2N/Aint
2N/Asvc_init(topo_mod_t *mod, topo_version_t version)
2N/A{
2N/A if (getenv("TOPOSVCDEBUG"))
2N/A topo_mod_setdebug(mod);
2N/A
2N/A if (version != SVC_VERSION)
2N/A return (topo_mod_seterrno(mod, EMOD_VER_NEW));
2N/A
2N/A if (topo_mod_register(mod, &svc_info, TOPO_VERSION) != 0) {
2N/A topo_mod_dprintf(mod, "failed to register svc_info: "
2N/A "%s\n", topo_mod_errmsg(mod));
2N/A return (-1);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Avoid
2N/Asvc_fini(topo_mod_t *mod)
2N/A{
2N/A scf_handle_t *hdl = topo_mod_getspecific(mod);
2N/A
2N/A if (hdl != NULL)
2N/A scf_handle_destroy(hdl);
2N/A
2N/A topo_mod_unregister(mod);
2N/A}
2N/A
2N/Astatic tnode_t *
2N/Asvc_create_node(topo_mod_t *mod, tnode_t *pnode, char *fmristr)
2N/A{
2N/A nvlist_t *fmri;
2N/A tnode_t *tn;
2N/A char *fixed;
2N/A ssize_t len;
2N/A int i, j, err;
2N/A
2N/A /*
2N/A * the scf_{x}_to_fmri interfaces return short-hand svc-scheme FMRI's
2N/A * that look like:
2N/A *
2N/A * svc:/service[:instance]
2N/A *
2N/A * But all our other code assumes a proper svc-scheme FMRI, so we
2N/A * correct the fmri string before we try to convert it to an nvlist.
2N/A *
2N/A * The short-hand version is kept as the label and can be used when
2N/A * dealing with the SMF libraries and CLI's.
2N/A */
2N/A len = strlen(fmristr) + 1;
2N/A if ((fixed = topo_mod_zalloc(mod, len + 1)) == NULL) {
2N/A (void) topo_mod_seterrno(mod, EMOD_NOMEM);
2N/A topo_mod_dprintf(mod, "topo_mod_zalloc() failed: %s",
2N/A topo_mod_errmsg(mod));
2N/A return (NULL);
2N/A }
2N/A for (i = 0, j = 0; i < len; i++)
2N/A if (i == 5)
2N/A fixed[i] = '/';
2N/A else
2N/A fixed[i] = fmristr[j++];
2N/A fixed[i] = '\0';
2N/A
2N/A if (topo_mod_str2nvl(mod, fixed, &fmri) < 0) {
2N/A topo_mod_dprintf(mod, "topo_mod_str2nvl() failed: %s",
2N/A topo_mod_errmsg(mod));
2N/A topo_mod_free(mod, fixed, len + 1);
2N/A return (NULL);
2N/A }
2N/A topo_mod_free(mod, fixed, len + 1);
2N/A
2N/A if (topo_node_range_create(mod, pnode, fmristr, 0, 0) < 0) {
2N/A topo_mod_dprintf(mod, "topo_node_range_create() failed: %s",
2N/A topo_mod_errmsg(mod));
2N/A nvlist_free(fmri);
2N/A return (NULL);
2N/A }
2N/A if ((tn = topo_node_bind(mod, pnode, fmristr, 0, fmri)) == NULL) {
2N/A topo_mod_dprintf(mod, "topo_node_bind() failed: %s",
2N/A topo_mod_errmsg(mod));
2N/A nvlist_free(fmri);
2N/A return (NULL);
2N/A }
2N/A nvlist_free(fmri);
2N/A
2N/A if (topo_node_label_set(tn, fmristr, &err) != 0) {
2N/A topo_mod_dprintf(mod, "failed to set label: %s\n",
2N/A topo_strerror(err));
2N/A return (NULL);
2N/A }
2N/A (void) topo_method_register(mod, tn, svc_methods);
2N/A
2N/A return (tn);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic int
2N/Asvc_enum(topo_mod_t *mod, tnode_t *pnode, const char *name,
2N/A topo_instance_t min, topo_instance_t max, void *notused1, void *notused2)
2N/A{
2N/A scf_handle_t *hdl;
2N/A scf_scope_t *sc = NULL;
2N/A scf_iter_t *svc_iter = NULL;
2N/A scf_iter_t *inst_iter = NULL;
2N/A scf_service_t *svc = NULL;
2N/A scf_instance_t *inst = NULL;
2N/A int ret = -1;
2N/A char *sfmri, *ifmri;
2N/A ssize_t slen, ilen;
2N/A tnode_t *svc_node;
2N/A
2N/A (void) topo_method_register(mod, pnode, svc_methods);
2N/A
2N/A if ((hdl = svc_get_handle(mod)) == NULL)
2N/A goto out;
2N/A
2N/A if ((sc = scf_scope_create(hdl)) == NULL ||
2N/A (svc = scf_service_create(hdl)) == NULL ||
2N/A (inst = scf_instance_create(hdl)) == NULL ||
2N/A (svc_iter = scf_iter_create(hdl)) == NULL ||
2N/A (inst_iter = scf_iter_create(hdl)) == NULL)
2N/A goto out;
2N/A
2N/A if (scf_handle_get_scope(hdl, SCF_SCOPE_LOCAL, sc) != 0)
2N/A goto out;
2N/A
2N/A if (scf_iter_scope_services(svc_iter, sc) != 0)
2N/A goto out;
2N/A
2N/A while (scf_iter_next_service(svc_iter, svc) == 1) {
2N/A if (scf_iter_service_instances(inst_iter, svc) != 0)
2N/A continue;
2N/A
2N/A if ((slen = scf_service_to_fmri(svc, NULL, 0)) < 0)
2N/A continue;
2N/A
2N/A if ((sfmri = topo_mod_zalloc(mod, slen + 1)) == NULL) {
2N/A (void) topo_mod_seterrno(mod, EMOD_NOMEM);
2N/A goto out;
2N/A }
2N/A if (scf_service_to_fmri(svc, sfmri, slen + 1) == -1)
2N/A goto out;
2N/A
2N/A if ((svc_node = svc_create_node(mod, pnode, sfmri)) == NULL) {
2N/A topo_mod_free(mod, sfmri, slen + 1);
2N/A /* topo mod errno set */
2N/A goto out;
2N/A }
2N/A
2N/A while (scf_iter_next_instance(inst_iter, inst) == 1) {
2N/A if ((ilen = scf_instance_to_fmri(inst, NULL, 0)) < 0)
2N/A continue;
2N/A
2N/A if ((ifmri = topo_mod_zalloc(mod, ilen + 1))
2N/A == NULL) {
2N/A (void) topo_mod_seterrno(mod, EMOD_NOMEM);
2N/A topo_mod_free(mod, sfmri, slen + 1);
2N/A goto out;
2N/A }
2N/A if (scf_instance_to_fmri(inst, ifmri, ilen + 1) == -1)
2N/A goto out;
2N/A
2N/A if ((svc_node = svc_create_node(mod, svc_node, ifmri))
2N/A == NULL) {
2N/A topo_mod_free(mod, sfmri, slen + 1);
2N/A topo_mod_free(mod, ifmri, ilen + 1);
2N/A /* topo mod errno set */
2N/A goto out;
2N/A }
2N/A topo_mod_free(mod, ifmri, ilen + 1);
2N/A }
2N/A topo_mod_free(mod, sfmri, slen + 1);
2N/A }
2N/A ret = 0;
2N/Aout:
2N/A scf_scope_destroy(sc);
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
2N/A return (ret);
2N/A}
2N/A
2N/Astatic void
2N/Asvc_release(topo_mod_t *mod, tnode_t *node)
2N/A{
2N/A topo_method_unregister_all(mod, node);
2N/A}
2N/A
2N/Astatic boolean_t
2N/Asvc_component_valid(const char *str)
2N/A{
2N/A if (str == NULL)
2N/A return (B_TRUE);
2N/A
2N/A if (*str == '\0')
2N/A return (B_FALSE);
2N/A
2N/A if (strpbrk(str, "@/:") != NULL)
2N/A return (B_FALSE);
2N/A
2N/A return (B_TRUE);
2N/A}
2N/A
2N/Astatic int
2N/Asvc_fmri_prop_get(topo_mod_t *mod, tnode_t *node, topo_version_t version,
2N/A nvlist_t *in, nvlist_t **out)
2N/A{
2N/A char *svc_name, *svc_inst = NULL;
2N/A nvlist_t *rsrc, *args;
2N/A char *pgroup, *pname;
2N/A tnode_t *svc_node;
2N/A char *search;
2N/A size_t len;
2N/A int err;
2N/A
2N/A if (version > TOPO_METH_PROP_GET_VERSION)
2N/A return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW));
2N/A
2N/A err = nvlist_lookup_string(in, TOPO_PROP_GROUP, &pgroup);
2N/A err |= nvlist_lookup_string(in, TOPO_PROP_VAL_NAME, &pname);
2N/A err |= nvlist_lookup_nvlist(in, TOPO_PROP_RESOURCE, &rsrc);
2N/A if (err != 0)
2N/A return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL));
2N/A
2N/A /*
2N/A * Private args to prop method are optional
2N/A */
2N/A if ((err = nvlist_lookup_nvlist(in, TOPO_PROP_PARGS, &args)) != 0) {
2N/A if (err != ENOENT)
2N/A return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL));
2N/A else
2N/A args = NULL;
2N/A }
2N/A
2N/A /*
2N/A * Lookup a topo node named svc:/svc_name[:svc_inst]
2N/A */
2N/A if (nvlist_lookup_string(rsrc, FM_FMRI_SVC_NAME, &svc_name) != 0)
2N/A return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL));
2N/A
2N/A (void) nvlist_lookup_string(rsrc, FM_FMRI_SVC_INSTANCE, &svc_inst);
2N/A
2N/A len = 5 + strlen(svc_name) +
2N/A (svc_inst != NULL ? 1 + strlen(svc_inst) : 0) + 1;
2N/A
2N/A if ((search = topo_mod_alloc(mod, len)) == NULL)
2N/A return (topo_mod_seterrno(mod, EMOD_NOMEM));
2N/A
2N/A (void) snprintf(search, len, "svc:/%s", svc_name);
2N/A svc_node = topo_node_lookup(node, (const char *)search, 0);
2N/A
2N/A if (svc_node == NULL) {
2N/A topo_mod_free(mod, search, len);
2N/A return (topo_mod_seterrno(mod, EMOD_NODE_NOENT));
2N/A }
2N/A
2N/A if (svc_inst != NULL) {
2N/A (void) snprintf(search, len, "svc:/%s:%s", svc_name, svc_inst);
2N/A svc_node = topo_node_lookup(svc_node, (const char *)search, 0);
2N/A if (svc_node == NULL) {
2N/A topo_mod_free(mod, search, len);
2N/A return (topo_mod_seterrno(mod, EMOD_NODE_NOENT));
2N/A }
2N/A }
2N/A
2N/A topo_mod_free(mod, search, len);
2N/A
2N/A err = 0;
2N/A (void) topo_prop_getprop(svc_node, pgroup, pname, args, out, &err);
2N/A
2N/A return (err);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic int
2N/Asvc_fmri_nvl2str(topo_mod_t *mod, tnode_t *node, topo_version_t version,
2N/A nvlist_t *nvl, nvlist_t **out)
2N/A{
2N/A uint8_t scheme_version;
2N/A char *scope = NULL;
2N/A char *fqn = NULL;
2N/A char *contract = NULL;
2N/A char *instance = NULL;
2N/A char *service;
2N/A int err;
2N/A char *buf = NULL;
2N/A size_t buflen = 0;
2N/A ssize_t size = 0;
2N/A nvlist_t *fmristr;
2N/A
2N/A if (version > TOPO_METH_NVL2STR_VERSION)
2N/A return (topo_mod_seterrno(mod, EMOD_VER_NEW));
2N/A
2N/A if (nvlist_lookup_uint8(nvl, FM_VERSION, &scheme_version) != 0 ||
2N/A scheme_version > FM_SVC_SCHEME_VERSION)
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
2N/A
2N/A /*
2N/A * Check for optional members.
2N/A */
2N/A err = nvlist_lookup_string(nvl, FM_FMRI_SVC_INSTANCE, &instance);
2N/A if ((err != 0 && err != ENOENT) || !svc_component_valid(instance))
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
2N/A err = nvlist_lookup_string(nvl, FM_FMRI_SVC_AUTH_SCOPE, &scope);
2N/A if ((err != 0 && err != ENOENT) || !svc_component_valid(scope))
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
2N/A err = nvlist_lookup_string(nvl, FM_FMRI_SVC_AUTH_SYSTEM_FQN, &fqn);
2N/A if ((err != 0 && err != ENOENT) || !svc_component_valid(scope))
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
2N/A err = nvlist_lookup_string(nvl, FM_FMRI_SVC_CONTRACT_ID, &contract);
2N/A if ((err != 0 && err != ENOENT) || !svc_component_valid(contract))
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
2N/A
2N/A /*
2N/A * Get the service name.
2N/A */
2N/A if (nvlist_lookup_string(nvl, FM_FMRI_SVC_NAME, &service) != 0)
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
2N/A
2N/A /*
2N/A * We make two passes through this code. The first time through we
2N/A * calculate the size of buffer that we'll need, and the second time
2N/A * through we fill it in.
2N/A */
2N/Aagain:
2N/A /*
2N/A * svc://[scope@][system-fqn]
2N/A */
2N/A topo_fmristr_build(&size, buf, buflen, FM_FMRI_SCHEME_SVC,
2N/A NULL, "://");
2N/A topo_fmristr_build(&size, buf, buflen, scope, NULL, "@");
2N/A topo_fmristr_build(&size, buf, buflen, fqn, NULL, NULL);
2N/A
2N/A /* svc path */
2N/A if (*service == '\0')
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
2N/A
2N/A topo_fmristr_build(&size, buf, buflen, service, "/", NULL);
2N/A
2N/A /* [:instance][@contract-id] */
2N/A topo_fmristr_build(&size, buf, buflen, instance, ":", NULL);
2N/A topo_fmristr_build(&size, buf, buflen, contract, "@", NULL);
2N/A
2N/A if (buf == NULL) {
2N/A if ((buf = topo_mod_alloc(mod, size + 1)) == NULL)
2N/A return (topo_mod_seterrno(mod, EMOD_NOMEM));
2N/A
2N/A buflen = size + 1;
2N/A size = 0;
2N/A goto again;
2N/A }
2N/A
2N/A /*
2N/A * Construct the nvlist to return as the result.
2N/A */
2N/A if (topo_mod_nvalloc(mod, &fmristr, NV_UNIQUE_NAME) != 0) {
2N/A topo_mod_strfree(mod, buf);
2N/A return (topo_mod_seterrno(mod, EMOD_NOMEM));
2N/A }
2N/A
2N/A if (nvlist_add_string(fmristr, "fmri-string", buf) != 0) {
2N/A topo_mod_strfree(mod, buf);
2N/A nvlist_free(fmristr);
2N/A return (topo_mod_seterrno(mod, EMOD_NOMEM));
2N/A }
2N/A topo_mod_strfree(mod, buf);
2N/A *out = fmristr;
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic int
2N/Asvc_fmri_str2nvl(topo_mod_t *mod, tnode_t *node, topo_version_t version,
2N/A nvlist_t *in, nvlist_t **out)
2N/A{
2N/A nvlist_t *fmri;
2N/A char *str, *loc, val;
2N/A
2N/A if (version > TOPO_METH_STR2NVL_VERSION)
2N/A return (topo_mod_seterrno(mod, EMOD_VER_NEW));
2N/A
2N/A if (nvlist_lookup_string(in, "fmri-string", &str) != 0)
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
2N/A
2N/A if (strncmp(str, "svc://", 6) != 0)
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_MALFORM));
2N/A
2N/A if (topo_mod_nvalloc(mod, &fmri, NV_UNIQUE_NAME) != 0)
2N/A return (topo_mod_seterrno(mod, EMOD_NOMEM));
2N/A
2N/A str += 6;
2N/A if ((loc = strpbrk(str, "@/")) == NULL)
2N/A goto malformed;
2N/A
2N/A if (*loc == '@') {
2N/A /* scope */
2N/A *loc = '\0';
2N/A if (!svc_component_valid(str)) {
2N/A *loc = '@';
2N/A goto malformed;
2N/A }
2N/A
2N/A if (nvlist_add_string(fmri, FM_FMRI_SVC_AUTH_SCOPE, str) != 0) {
2N/A *loc = '@';
2N/A goto nomem;
2N/A }
2N/A
2N/A *loc = '@';
2N/A str = loc + 1;
2N/A if ((loc = strchr(str, '/')) == NULL)
2N/A goto malformed;
2N/A }
2N/A
2N/A if (loc != str) {
2N/A /* system-fqn */
2N/A *loc = '\0';
2N/A if (!svc_component_valid(str)) {
2N/A *loc = '/';
2N/A goto malformed;
2N/A }
2N/A
2N/A if (nvlist_add_string(fmri, FM_FMRI_SVC_AUTH_SYSTEM_FQN,
2N/A str) != 0) {
2N/A *loc = '/';
2N/A goto nomem;
2N/A }
2N/A
2N/A *loc = '/';
2N/A }
2N/A
2N/A str = loc + 1;
2N/A loc = strpbrk(str, ":@");
2N/A
2N/A if (str[0] == '\0' || loc == str)
2N/A goto malformed;
2N/A
2N/A if (loc != NULL) {
2N/A val = *loc;
2N/A *loc = '\0';
2N/A }
2N/A
2N/A /* service name */
2N/A if (nvlist_add_string(fmri, FM_FMRI_SVC_NAME, str) != 0) {
2N/A if (loc != NULL)
2N/A *loc = val;
2N/A goto nomem;
2N/A }
2N/A
2N/A if (loc != NULL)
2N/A *loc = val;
2N/A
2N/A if (loc != NULL && *loc == ':') {
2N/A /* instance */
2N/A str = loc + 1;
2N/A if (str[0] == '\0' || str[0] == '@')
2N/A goto malformed;
2N/A
2N/A loc = strchr(str, '@');
2N/A if (loc != NULL)
2N/A *loc = '\0';
2N/A
2N/A if (nvlist_add_string(fmri, FM_FMRI_SVC_INSTANCE,
2N/A str) != 0) {
2N/A if (loc != NULL)
2N/A *loc = '@';
2N/A goto nomem;
2N/A }
2N/A
2N/A if (loc != NULL)
2N/A *loc = '@';
2N/A }
2N/A
2N/A if (loc != NULL) {
2N/A /* contract-id */
2N/A assert(*loc == '@');
2N/A str = loc + 1;
2N/A if (str[0] == '\0')
2N/A goto malformed;
2N/A
2N/A if (nvlist_add_string(fmri, FM_FMRI_SVC_CONTRACT_ID,
2N/A str) != 0) {
2N/A goto nomem;
2N/A }
2N/A }
2N/A
2N/A if (nvlist_add_uint8(fmri, FM_VERSION, FM_SVC_SCHEME_VERSION) != 0 ||
2N/A nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_SVC) != 0)
2N/A goto nomem;
2N/A
2N/A *out = fmri;
2N/A return (0);
2N/A
2N/Amalformed:
2N/A nvlist_free(fmri);
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_MALFORM));
2N/A
2N/Anomem:
2N/A nvlist_free(fmri);
2N/A return (topo_mod_seterrno(mod, EMOD_NOMEM));
2N/A}
2N/A
2N/A/*
2N/A * This common function is shared by all consumers (presence_state
2N/A * and service state).
2N/A *
2N/A * svc_get_state succeeds
2N/A * Case with FMD_SERVICE_STATE_*
2N/A * ---------------------------- ------------------------
2N/A * svc name deleted UNKNOWN
2N/A * svc name not found UNKNOWN
2N/A * no fmri instance OK
2N/A * instance deleted UNKNOWN
2N/A * instance not found UNKNOWN
2N/A *
2N/A * If none of the above apply and this is a call from the
2N/A * "presence_state" method (presence_only == B_TRUE) then
2N/A * svc_get_state returns FMD_SERVICE_STATE_OK.
2N/A *
2N/A *
2N/A * The "presence_state" methods maps a return of UNKNOWN to
2N/A * FMD_OBJ_STATE_NOT_PRESENT and OK to FMD_OBJ_STATE_UNKNOWN.
2N/A *
2N/A * For the "service state" method svc_get_state goes on
2N/A * to return the instance state as below, and the method maps that
2N/A * result as in the last column of the following table:
2N/A *
2N/A * svc_get_state succeeds Service
2N/A * Instance state with FMD_SERVICE_STATE_* State
2N/A * -------------- ------------------------------- ---------------
2N/A * none OK OK
2N/A * uninitialized OK OK
2N/A * maintenance UNUSABLE UNUSABLE
2N/A * offline OK OK
2N/A * disabled OK OK
2N/A * online OK OK
2N/A * degraded DEGRADED DEGRADED
2N/A * legacy_run OK (XXX can we see this?) OK
2N/A *
2N/A * Note that *only* "maintenance" state should map to an unusable service state.
2N/A * That's because a service entering maintenance state
2N/A * is modelled as a defect fault diagnosis in FMA, but there is no
2N/A * corresponding isolation action from a response agent since the the service
2N/A * is already isolated by virtue of being in maintenance state. Any transition
2N/A * from maintenance state, even to offline, is considered a repair. If on
2N/A * repair fmd does not see the service usable again then the case hangs
2N/A * around in the "resolved but not all resources back online" state and
2N/A * further maintenance events for this service will not show up in fmd state
2N/A * because case duplicate checking code will find the old case.
2N/A */
2N/A
2N/Astatic int
2N/Asvc_get_state(topo_mod_t *mod, nvlist_t *fmri, boolean_t presence_only,
2N/A int *ret)
2N/A{
2N/A scf_handle_t *hdl;
2N/A uint8_t fmversion;
2N/A char *instance, *name;
2N/A scf_service_t *svc = NULL;
2N/A scf_scope_t *scope = NULL;
2N/A scf_instance_t *inst = NULL;
2N/A scf_property_t *prop = NULL;
2N/A scf_iter_t *iter = NULL;
2N/A scf_value_t *val = NULL;
2N/A scf_propertygroup_t *pg = NULL;
2N/A int err, retval = 0;
2N/A ssize_t len;
2N/A char *state;
2N/A
2N/A if ((hdl = svc_get_handle(mod)) == NULL)
2N/A return (-1);
2N/A
2N/A if (nvlist_lookup_uint8(fmri, FM_VERSION, &fmversion) != 0 ||
2N/A fmversion > FM_SVC_SCHEME_VERSION ||
2N/A nvlist_lookup_string(fmri, FM_FMRI_SVC_NAME, &name) != 0)
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_MALFORM));
2N/A
2N/A if ((svc = scf_service_create(hdl)) == NULL ||
2N/A (inst = scf_instance_create(hdl)) == NULL ||
2N/A (scope = scf_scope_create(hdl)) == NULL ||
2N/A (prop = scf_property_create(hdl)) == NULL ||
2N/A (iter = scf_iter_create(hdl)) == NULL ||
2N/A (pg = scf_pg_create(hdl)) == NULL ||
2N/A (val = scf_value_create(hdl)) == NULL)
2N/A goto error;
2N/A
2N/A if (scf_handle_get_scope(hdl, SCF_SCOPE_LOCAL, scope) != 0)
2N/A goto error;
2N/A
2N/A /*
2N/A * If we fail to get the service due to _DELETED or _NOT_FOUND, then we
2N/A * treat this as not present.
2N/A */
2N/A if (scf_scope_get_service(scope, name, svc) != 0) {
2N/A err = scf_error();
2N/A if (err == SCF_ERROR_NOT_FOUND || err == SCF_ERROR_DELETED) {
2N/A *ret = FMD_SERVICE_STATE_UNKNOWN;
2N/A goto out;
2N/A } else {
2N/A goto error;
2N/A }
2N/A }
2N/A
2N/A if (nvlist_lookup_string(fmri, FM_FMRI_SVC_INSTANCE, &instance) != 0) {
2N/A *ret = FMD_SERVICE_STATE_OK;
2N/A goto out;
2N/A }
2N/A
2N/A /*
2N/A * Again, check for _DELETED or _NOT_FOUND.
2N/A */
2N/A if (scf_service_get_instance(svc, instance, inst) != 0) {
2N/A err = scf_error();
2N/A if (err == SCF_ERROR_NOT_FOUND || err == SCF_ERROR_DELETED) {
2N/A *ret = FMD_SERVICE_STATE_UNKNOWN;
2N/A goto out;
2N/A } else {
2N/A goto error;
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * For presence, we are done. Otherwise, we need to get the current
2N/A * state of the instance.
2N/A */
2N/A if (presence_only) {
2N/A *ret = FMD_SERVICE_STATE_OK;
2N/A goto out;
2N/A }
2N/A
2N/A if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) != 0 ||
2N/A scf_pg_get_property(pg, SCF_PROPERTY_STATE, prop) != 0 ||
2N/A scf_iter_property_values(iter, prop) != 0 ||
2N/A scf_iter_next_value(iter, val) != 1)
2N/A goto error;
2N/A
2N/A if ((len = scf_value_get_astring(val, NULL, 0)) < 0)
2N/A goto error;
2N/A
2N/A state = alloca(len + 1);
2N/A if (scf_value_get_astring(val, state, len + 1) < 0)
2N/A goto error;
2N/A
2N/A if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
2N/A *ret = FMD_SERVICE_STATE_UNUSABLE;
2N/A } else if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 0) {
2N/A *ret = FMD_SERVICE_STATE_DEGRADED;
2N/A } else {
2N/A *ret = FMD_SERVICE_STATE_OK;
2N/A }
2N/A goto out;
2N/A
2N/Aerror:
2N/A retval = -1;
2N/Aout:
2N/A scf_value_destroy(val);
2N/A scf_pg_destroy(pg);
2N/A scf_iter_destroy(iter);
2N/A scf_property_destroy(prop);
2N/A scf_instance_destroy(inst);
2N/A scf_scope_destroy(scope);
2N/A scf_service_destroy(svc);
2N/A return (retval);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic int
2N/Asvc_fmri_presence_state(topo_mod_t *mod, tnode_t *node, topo_version_t version,
2N/A nvlist_t *in, nvlist_t **out)
2N/A{
2N/A int state;
2N/A uint8_t fmversion;
2N/A
2N/A if (nvlist_lookup_uint8(in, FM_VERSION, &fmversion) != 0 ||
2N/A fmversion > FM_SVC_SCHEME_VERSION)
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_MALFORM));
2N/A
2N/A if (version > TOPO_METH_PRESENCE_STATE_VERSION)
2N/A return (topo_mod_seterrno(mod, EMOD_VER_NEW));
2N/A
2N/A if (svc_get_state(mod, in, B_TRUE, &state) != 0)
2N/A return (-1);
2N/A
2N/A if (topo_mod_nvalloc(mod, out, NV_UNIQUE_NAME) != 0)
2N/A return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
2N/A if (nvlist_add_uint32(*out, TOPO_METH_PRESENCE_STATE_RET,
2N/A state == FMD_SERVICE_STATE_UNKNOWN ?
2N/A FMD_OBJ_STATE_NOT_PRESENT : FMD_OBJ_STATE_UNKNOWN) != 0) {
2N/A nvlist_free(*out);
2N/A return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic int
2N/Asvc_fmri_service_state(topo_mod_t *mod, tnode_t *node, topo_version_t version,
2N/A nvlist_t *in, nvlist_t **out)
2N/A{
2N/A int state;
2N/A uint8_t fmversion;
2N/A
2N/A if (nvlist_lookup_uint8(in, FM_VERSION, &fmversion) != 0 ||
2N/A fmversion > FM_SVC_SCHEME_VERSION)
2N/A return (topo_mod_seterrno(mod, EMOD_FMRI_MALFORM));
2N/A
2N/A if (version > TOPO_METH_SERVICE_STATE_VERSION)
2N/A return (topo_mod_seterrno(mod, EMOD_VER_NEW));
2N/A
2N/A if (svc_get_state(mod, in, B_FALSE, &state) != 0)
2N/A return (-1);
2N/A
2N/A if (topo_mod_nvalloc(mod, out, NV_UNIQUE_NAME) != 0)
2N/A return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
2N/A if (nvlist_add_uint32(*out, TOPO_METH_SERVICE_STATE_RET,
2N/A state) != 0) {
2N/A nvlist_free(*out);
2N/A return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
2N/A }
2N/A
2N/A return (0);
2N/A}