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