xref: /titanic_52/usr/src/cmd/fm/modules/sun4/cpumem-diagnosis/cmd_fmri.c (revision 822fb41d6f36e90a1ec836b32f869e1235e7da40)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <errno.h>
30 #include <strings.h>
31 
32 #include <cmd_fmri.h>
33 #include <cmd.h>
34 
35 void
36 cmd_fmri_init(fmd_hdl_t *hdl, cmd_fmri_t *fmri, nvlist_t *nvl,
37     const char *fmt, ...)
38 {
39 	va_list ap;
40 
41 	va_start(ap, fmt);
42 	cmd_vbufname(fmri->fmri_packnm, sizeof (fmri->fmri_packnm), fmt, ap);
43 	va_end(ap);
44 
45 	if ((errno = nvlist_dup(nvl, &fmri->fmri_nvl, 0)) != 0 ||
46 	    (errno = nvlist_size(nvl, &fmri->fmri_packsz,
47 	    NV_ENCODE_NATIVE)) != 0)
48 		fmd_hdl_abort(hdl, "failed to copy fmri for fmri create");
49 
50 	fmri->fmri_packbuf = fmd_hdl_alloc(hdl, fmri->fmri_packsz, FMD_SLEEP);
51 
52 	if ((errno = nvlist_pack(nvl, &fmri->fmri_packbuf, &fmri->fmri_packsz,
53 	    NV_ENCODE_NATIVE, 0)) != 0)
54 		fmd_hdl_abort(hdl, "failed to pack fmri for fmri create");
55 
56 	cmd_fmri_write(hdl, fmri);
57 }
58 
59 void
60 cmd_fmri_fini(fmd_hdl_t *hdl, cmd_fmri_t *fmri, int destroy)
61 {
62 	if (destroy)
63 		fmd_buf_destroy(hdl, NULL, fmri->fmri_packnm);
64 
65 	fmd_hdl_free(hdl, fmri->fmri_packbuf, fmri->fmri_packsz);
66 	nvlist_free(fmri->fmri_nvl);
67 }
68 
69 void
70 cmd_fmri_restore(fmd_hdl_t *hdl, cmd_fmri_t *fmri)
71 {
72 	if (fmd_buf_size(hdl, NULL, fmri->fmri_packnm) == 0) {
73 		bzero(fmri, sizeof (cmd_fmri_t));
74 		return;
75 	}
76 
77 	if ((fmri->fmri_packbuf = cmd_buf_read(hdl, NULL, fmri->fmri_packnm,
78 	    fmri->fmri_packsz)) == NULL) {
79 		fmd_hdl_abort(hdl, "failed to read fmri buffer %s",
80 		    fmri->fmri_packnm);
81 	}
82 
83 	if (nvlist_unpack(fmri->fmri_packbuf, fmri->fmri_packsz,
84 	    &fmri->fmri_nvl, 0) != 0) {
85 		fmd_hdl_abort(hdl, "failed to unpack fmri buffer %s\n",
86 		    fmri->fmri_packnm);
87 	}
88 }
89 
90 void
91 cmd_fmri_write(fmd_hdl_t *hdl, cmd_fmri_t *fmri)
92 {
93 	size_t sz;
94 
95 	if ((sz = fmd_buf_size(hdl, NULL, fmri->fmri_packnm)) !=
96 	    fmri->fmri_packsz && sz != 0)
97 		fmd_buf_destroy(hdl, NULL, fmri->fmri_packnm);
98 
99 	fmd_buf_write(hdl, NULL, fmri->fmri_packnm, fmri->fmri_packbuf,
100 	    fmri->fmri_packsz);
101 }
102