xref: /illumos-gate/usr/src/cmd/fm/schemes/mod/scheme.c (revision 4703203d)
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 <fm/fmd_fmri.h>
30 
31 /*
32  * buf_append -- Append str to buf (if it's non-NULL).  Place prepend
33  * in buf in front of str and append behind it (if they're non-NULL).
34  * Continue to update size even if we run out of space to actually
35  * stuff characters in the buffer.
36  */
37 static void
38 buf_append(ssize_t *sz, char *buf, size_t buflen, char *str,
39     char *prepend, char *append)
40 {
41 	ssize_t left;
42 
43 	if (str == NULL)
44 		return;
45 
46 	if (buflen == 0 || (left = buflen - *sz) < 0)
47 		left = 0;
48 
49 	if (buf != NULL && left != 0)
50 		buf += *sz;
51 
52 	if (prepend == NULL && append == NULL)
53 		*sz += snprintf(buf, left, "%s", str);
54 	else if (append == NULL)
55 		*sz += snprintf(buf, left, "%s%s", prepend, str);
56 	else if (prepend == NULL)
57 		*sz += snprintf(buf, left, "%s%s", str, append);
58 	else
59 		*sz += snprintf(buf, left, "%s%s%s", prepend, str, append);
60 }
61 
62 /*
63  * Maximum 32 bit integer is 2147483647, which is 10 digits.  A buffer
64  * of 11 bytes can therefore contain the null-terminated ascii
65  * representation of any integer module id.
66  */
67 #define	MAXINTSTR	11
68 
69 ssize_t
70 fmd_fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen)
71 {
72 	nvlist_t *anvl = NULL;
73 	uint8_t version;
74 	ssize_t size = 0;
75 	int32_t modid;
76 	char *achas = NULL;
77 	char *adom = NULL;
78 	char *aprod = NULL;
79 	char *asrvr = NULL;
80 	char *ahost = NULL;
81 	char *modname = NULL;
82 	char numbuf[MAXINTSTR];
83 	int more_auth = 0;
84 	int err;
85 
86 	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
87 	    version > FM_MOD_SCHEME_VERSION)
88 		return (fmd_fmri_set_errno(EINVAL));
89 
90 	/* Get authority, if present */
91 	err = nvlist_lookup_nvlist(nvl, FM_FMRI_AUTHORITY, &anvl);
92 	if (err != 0 && err != ENOENT)
93 		return (fmd_fmri_set_errno(err));
94 
95 	/*
96 	 *  For brevity, we only include the module name and id
97 	 *  present in the FMRI in our output string.  The FMRI
98 	 *  also has data on the package containing the module.
99 	 */
100 
101 	/* There must be a module name */
102 	err = nvlist_lookup_string(nvl, FM_FMRI_MOD_NAME, &modname);
103 	if (err != 0 || modname == NULL)
104 		return (fmd_fmri_set_errno(EINVAL));
105 
106 	/* There must be a module id */
107 	err = nvlist_lookup_int32(nvl, FM_FMRI_MOD_ID, &modid);
108 	if (err != 0)
109 		return (fmd_fmri_set_errno(EINVAL));
110 
111 	if (anvl != NULL) {
112 		(void) nvlist_lookup_string(anvl,
113 		    FM_FMRI_AUTH_PRODUCT, &aprod);
114 		(void) nvlist_lookup_string(anvl,
115 		    FM_FMRI_AUTH_CHASSIS, &achas);
116 		(void) nvlist_lookup_string(anvl,
117 		    FM_FMRI_AUTH_DOMAIN, &adom);
118 		(void) nvlist_lookup_string(anvl,
119 		    FM_FMRI_AUTH_SERVER, &asrvr);
120 		(void) nvlist_lookup_string(anvl,
121 		    FM_FMRI_AUTH_HOST, &ahost);
122 		if (aprod != NULL)
123 			more_auth++;
124 		if (achas != NULL)
125 			more_auth++;
126 		if (adom != NULL)
127 			more_auth++;
128 		if (asrvr != NULL)
129 			more_auth++;
130 		if (ahost != NULL)
131 			more_auth++;
132 	}
133 
134 	/* mod:// */
135 	buf_append(&size, buf, buflen, FM_FMRI_SCHEME_MOD, NULL, "://");
136 
137 	/* authority, if any */
138 	if (aprod != NULL)
139 		buf_append(&size, buf, buflen, aprod, FM_FMRI_AUTH_PRODUCT "=",
140 		    --more_auth > 0 ? "," : NULL);
141 	if (achas != NULL)
142 		buf_append(&size, buf, buflen, achas, FM_FMRI_AUTH_CHASSIS "=",
143 		    --more_auth > 0 ? "," : NULL);
144 	if (adom != NULL)
145 		buf_append(&size, buf, buflen, adom, FM_FMRI_AUTH_DOMAIN "=",
146 		    --more_auth > 0 ? "," : NULL);
147 	if (asrvr != NULL)
148 		buf_append(&size, buf, buflen, asrvr, FM_FMRI_AUTH_SERVER "=",
149 		    --more_auth > 0 ? "," : NULL);
150 	if (ahost != NULL)
151 		buf_append(&size, buf, buflen, ahost, FM_FMRI_AUTH_HOST "=",
152 		    NULL);
153 
154 	/* module parts */
155 	buf_append(&size, buf, buflen, modname, "/" FM_FMRI_MOD_NAME "=", "/");
156 
157 	(void) snprintf(numbuf, MAXINTSTR, "%d", modid);
158 	buf_append(&size, buf, buflen, numbuf, FM_FMRI_MOD_ID "=", NULL);
159 
160 	return (size);
161 }
162 
163 /*
164  * fmd_fmri_present() is called by fmadm to determine if a faulty ASRU
165  * is still present in the system.  In general we don't expect to get
166  * ASRUs in this scheme, so it's unlikely this routine will get called.
167  * In case it does, though, we just return true by default, as we have no
168  * real way to look up the component in the system configuration.
169  */
170 /*ARGSUSED*/
171 int
172 fmd_fmri_present(nvlist_t *nvl)
173 {
174 	return (1);
175 }
176 
177 /*
178  * fmd_fmri_unusable() is called by fmadm to determine if a faulty ASRU
179  * is usable.  In general we don't expect to get ASRUs in this scheme,
180  * so it's unlikely this routine will get called.  In case it does,
181  * though, we just return false by default, as we have no real way to
182  * find the component or determine the component's usability.
183  */
184 /*ARGSUSED*/
185 int
186 fmd_fmri_unusable(nvlist_t *nvl)
187 {
188 	return (0);
189 }
190