1*f6e214c7SGavin Maltby /*
2*f6e214c7SGavin Maltby  * CDDL HEADER START
3*f6e214c7SGavin Maltby  *
4*f6e214c7SGavin Maltby  * The contents of this file are subject to the terms of the
5*f6e214c7SGavin Maltby  * Common Development and Distribution License (the "License").
6*f6e214c7SGavin Maltby  * You may not use this file except in compliance with the License.
7*f6e214c7SGavin Maltby  *
8*f6e214c7SGavin Maltby  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*f6e214c7SGavin Maltby  * or http://www.opensolaris.org/os/licensing.
10*f6e214c7SGavin Maltby  * See the License for the specific language governing permissions
11*f6e214c7SGavin Maltby  * and limitations under the License.
12*f6e214c7SGavin Maltby  *
13*f6e214c7SGavin Maltby  * When distributing Covered Code, include this CDDL HEADER in each
14*f6e214c7SGavin Maltby  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*f6e214c7SGavin Maltby  * If applicable, add the following below this CDDL HEADER, with the
16*f6e214c7SGavin Maltby  * fields enclosed by brackets "[]" replaced with your own identifying
17*f6e214c7SGavin Maltby  * information: Portions Copyright [yyyy] [name of copyright owner]
18*f6e214c7SGavin Maltby  *
19*f6e214c7SGavin Maltby  * CDDL HEADER END
20*f6e214c7SGavin Maltby  */
21*f6e214c7SGavin Maltby 
22*f6e214c7SGavin Maltby /*
23*f6e214c7SGavin Maltby  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24*f6e214c7SGavin Maltby  */
25*f6e214c7SGavin Maltby 
26*f6e214c7SGavin Maltby /*
27*f6e214c7SGavin Maltby  * SMF software diagnosis engine components.
28*f6e214c7SGavin Maltby  */
29*f6e214c7SGavin Maltby 
30*f6e214c7SGavin Maltby #include <fm/libtopo.h>
31*f6e214c7SGavin Maltby #include <strings.h>
32*f6e214c7SGavin Maltby 
33*f6e214c7SGavin Maltby #include "../../common/sw.h"
34*f6e214c7SGavin Maltby #include "smf.h"
35*f6e214c7SGavin Maltby 
36*f6e214c7SGavin Maltby /*
37*f6e214c7SGavin Maltby  * Given a "svc' scheme FMRI in nvlist form, produce a string form
38*f6e214c7SGavin Maltby  * of the FMRI (with no short-hand).
39*f6e214c7SGavin Maltby  */
40*f6e214c7SGavin Maltby char *
sw_smf_svcfmri2str(fmd_hdl_t * hdl,nvlist_t * fmri)41*f6e214c7SGavin Maltby sw_smf_svcfmri2str(fmd_hdl_t *hdl, nvlist_t *fmri)
42*f6e214c7SGavin Maltby {
43*f6e214c7SGavin Maltby 	char *fmristr = NULL;
44*f6e214c7SGavin Maltby 	topo_hdl_t *thp;
45*f6e214c7SGavin Maltby 	char *topostr;
46*f6e214c7SGavin Maltby 	int err;
47*f6e214c7SGavin Maltby 
48*f6e214c7SGavin Maltby 	thp = fmd_hdl_topo_hold(hdl, TOPO_VERSION);
49*f6e214c7SGavin Maltby 	if (topo_fmri_nvl2str(thp, fmri, &topostr, &err) == 0) {
50*f6e214c7SGavin Maltby 		fmristr = fmd_hdl_strdup(hdl, (const char *)topostr, FMD_SLEEP);
51*f6e214c7SGavin Maltby 		topo_hdl_strfree(thp, topostr);
52*f6e214c7SGavin Maltby 	}
53*f6e214c7SGavin Maltby 	fmd_hdl_topo_rele(hdl, thp);
54*f6e214c7SGavin Maltby 
55*f6e214c7SGavin Maltby 	return (fmristr);	/* caller must fmd_hdl_strfree */
56*f6e214c7SGavin Maltby }
57*f6e214c7SGavin Maltby 
58*f6e214c7SGavin Maltby /*
59*f6e214c7SGavin Maltby  * Given a "svc" scheme FMRI in nvlist form, produce a short-hand form
60*f6e214c7SGavin Maltby  * string FMRI "svc:/..." as generally used in SMF cmdline output.
61*f6e214c7SGavin Maltby  */
62*f6e214c7SGavin Maltby char *
sw_smf_svcfmri2shortstr(fmd_hdl_t * hdl,nvlist_t * fmri)63*f6e214c7SGavin Maltby sw_smf_svcfmri2shortstr(fmd_hdl_t *hdl, nvlist_t *fmri)
64*f6e214c7SGavin Maltby {
65*f6e214c7SGavin Maltby 	char *name, *inst, *bufp, *fullname;
66*f6e214c7SGavin Maltby 	size_t len;
67*f6e214c7SGavin Maltby 
68*f6e214c7SGavin Maltby 	if (nvlist_lookup_string(fmri, FM_FMRI_SVC_NAME, &name) != 0 ||
69*f6e214c7SGavin Maltby 	    nvlist_lookup_string(fmri, FM_FMRI_SVC_INSTANCE, &inst) != 0)
70*f6e214c7SGavin Maltby 		return (NULL);
71*f6e214c7SGavin Maltby 
72*f6e214c7SGavin Maltby 	len = strlen(name) + strlen(inst) + 8;
73*f6e214c7SGavin Maltby 	bufp = fmd_hdl_alloc(hdl, len, FMD_SLEEP);
74*f6e214c7SGavin Maltby 	(void) snprintf(bufp, len, "svc:/%s:%s", name, inst);
75*f6e214c7SGavin Maltby 
76*f6e214c7SGavin Maltby 	fullname = fmd_hdl_strdup(hdl, bufp, FMD_SLEEP);
77*f6e214c7SGavin Maltby 	fmd_hdl_free(hdl, bufp, len);
78*f6e214c7SGavin Maltby 
79*f6e214c7SGavin Maltby 	return (fullname);	/* caller must fmd_hdl_strfree */
80*f6e214c7SGavin Maltby }
81