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 2002 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 <stdio.h>
30 #include <unistd.h>
31 #include <strings.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <netdb.h>
35 #include <cimapi.h>
36 #include <libnvpair.h>
37 #include <md5.h>
38
39 #include "libdiskmgt.h"
40 #include "providerNames.h"
41 #include "messageStrings.h"
42 #include "cimKeys.h"
43 #include "util.h"
44
45 /*
46 * Convert a single descriptor in to a Solaris_LogicalDisk instance
47 */
48
49 CCIMInstance *
logicaldisk_descriptor_toCCIMInstance(char * hostname,dm_descriptor_t desc,char * provider,int * errp)50 logicaldisk_descriptor_toCCIMInstance(char *hostname, dm_descriptor_t desc,
51 char *provider, int *errp)
52 {
53
54 nvlist_t *nvlp;
55 nvpair_t *nvp;
56 CCIMInstance *inst = NULL;
57 CCIMException *ex;
58 dm_descriptor_t *dlist;
59 dm_descriptor_t *alist;
60 char *str;
61 char *drive;
62 int error;
63 char buf[100];
64
65 *errp = 0;
66
67 /* Create instance of disk. */
68
69 if ((inst = cim_createInstance(provider)) == NULL) {
70 ex = cim_getLastError();
71 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC, CIM_ERR_FAILED,
72 CREATE_INSTANCE_FAILURE, ex, errp);
73 return ((CCIMInstance *)NULL);
74 }
75
76 /*
77 * Get the alias name to display as the common name as well as
78 * the deviceid if for some reason this media does not have
79 * a name.
80 */
81 dlist = dm_get_associated_descriptors(desc, DM_DRIVE, &error);
82
83 if (error != 0) {
84 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC, CIM_ERR_FAILED,
85 DM_GET_ASSOC_FAILURE, NULL, errp);
86 cim_freeInstance(inst);
87 return ((CCIMInstance *)NULL);
88 }
89
90 alist = dm_get_associated_descriptors(dlist[0], DM_ALIAS, &error);
91 dm_free_descriptors(dlist);
92
93 if (error != 0) {
94 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC, CIM_ERR_FAILED,
95 DM_GET_ASSOC_FAILURE, NULL, errp);
96 cim_freeInstance(inst);
97 return ((CCIMInstance *)NULL);
98 }
99
100 drive = dm_get_name(alist[0], &error);
101 dm_free_descriptors(alist);
102
103 if (error != 0) {
104 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC, CIM_ERR_FAILED,
105 DM_GET_NAME_FAILURE, NULL, errp);
106 cim_freeInstance(inst);
107 return ((CCIMInstance *)NULL);
108 }
109
110 util_doProperty("Name", string, drive, cim_true, inst, errp);
111 if (*errp != 0) {
112 ex = cim_getLastError();
113 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC, CIM_ERR_FAILED,
114 ADD_PROPERTY_FAILURE, ex, errp);
115 cim_freeInstance(inst);
116 dm_free_name(drive);
117 return ((CCIMInstance *)NULL);
118 }
119
120 /* Now, assign the deviceID */
121 str = dm_get_name(desc, &error);
122
123 if (error != 0) {
124 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC,
125 CIM_ERR_FAILED, DM_GET_NAME_FAILURE, NULL, errp);
126 cim_freeInstance(inst);
127 dm_free_name(drive);
128 return ((CCIMInstance *)NULL);
129 }
130
131 if (str != NULL) {
132 util_doProperty(DEVICEID, string, str, cim_true, inst, errp);
133 dm_free_name(str);
134
135 if (*errp != 0) {
136 ex = cim_getLastError();
137 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC, CIM_ERR_FAILED,
138 ADD_PROPERTY_FAILURE, ex, errp);
139 cim_freeInstance(inst);
140 dm_free_name(drive);
141 return ((CCIMInstance *)NULL);
142 }
143
144 } else {
145
146 util_doProperty(DEVICEID, string, drive, cim_true, inst, errp);
147
148 if (*errp != 0) {
149 ex = cim_getLastError();
150 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC, CIM_ERR_FAILED,
151 ADD_PROPERTY_FAILURE, ex, errp);
152 cim_freeInstance(inst);
153 dm_free_name(drive);
154 return ((CCIMInstance *)NULL);
155 }
156 }
157 dm_free_name(drive);
158
159
160 /* add keys */
161
162 util_doProperty(CREATION_CLASS, string, LOGICAL_DISK, cim_true,
163 inst, errp);
164
165 if (*errp == 0) {
166 util_doProperty(SYS_CREATION_CLASS, string, COMPUTER_SYSTEM,
167 cim_true, inst, errp);
168 }
169
170 if (*errp == 0) {
171 util_doProperty(SYSTEM, string, hostname, cim_true, inst, errp);
172 }
173
174 if (!errp != 0) {
175 ex = cim_getLastError();
176 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC, CIM_ERR_FAILED,
177 ADD_PROPERTY_FAILURE, ex, errp);
178 cim_freeInstance(inst);
179 return ((CCIMInstance *)NULL);
180 }
181
182 nvlp = dm_get_attributes(desc, &error);
183 if (error != 0) {
184 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC, CIM_ERR_FAILED,
185 DM_GET_ATTR_FAILURE, NULL, errp);
186 cim_freeInstance(inst);
187 return ((CCIMInstance *)NULL);
188 }
189
190 if (nvlp == NULL) {
191 return (inst);
192 }
193
194 for (nvp = nvlist_next_nvpair(nvlp, NULL); nvp != NULL;
195 nvp = nvlist_next_nvpair(nvlp, nvp)) {
196
197 char *attrname;
198 uint32_t ui32;
199 uint64_t ui64;
200
201 attrname = nvpair_name(nvp);
202
203 /* If the attrname for this nvp is null, try the next one. */
204
205 if (attrname == NULL) {
206 continue;
207 }
208
209 /* Loop through nvpair list and assign attrs to the CIMInstace. */
210
211 if (strcasecmp(attrname, DM_BLOCKSIZE) == 0) {
212 error = nvpair_value_uint32(nvp, &ui32);
213 if (error != 0) {
214 cim_freeInstance(inst);
215 nvlist_free(nvlp);
216 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC,
217 CIM_ERR_FAILED, DM_GET_ATTR_FAILURE, NULL, errp);
218 return ((CCIMInstance *)NULL);
219 }
220 error = snprintf(buf, sizeof (buf), "%u", ui32);
221 if (error < 0) {
222 cim_freeInstance(inst);
223 nvlist_free(nvlp);
224 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC,
225 CIM_ERR_FAILED, DM_GET_ATTR_FAILURE, NULL, errp);
226 return ((CCIMInstance *)NULL);
227 }
228
229 util_doProperty("BlockSize", uint64, buf, cim_false, inst,
230 errp);
231
232 if (*errp != 0) {
233 ex = cim_getLastError();
234 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC,
235 CIM_ERR_FAILED, ADD_PROPERTY_FAILURE, ex, errp);
236 cim_freeInstance(inst);
237 nvlist_free(nvlp);
238 return ((CCIMInstance *)NULL);
239 }
240 } else if (strcasecmp(attrname, DM_SIZE) == 0) {
241 error = nvpair_value_uint64(nvp, &ui64);
242 if (error < 0) {
243 cim_freeInstance(inst);
244 nvlist_free(nvlp);
245 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC,
246 CIM_ERR_FAILED, DM_GET_ATTR_FAILURE, NULL, errp);
247 return ((CCIMInstance *)NULL);
248 }
249 error = snprintf(buf, sizeof (buf), "%llu", ui64);
250 if (error < 0) {
251 cim_freeInstance(inst);
252 nvlist_free(nvlp);
253 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC,
254 CIM_ERR_FAILED, DM_GET_ATTR_FAILURE, NULL, errp);
255 return ((CCIMInstance *)NULL);
256 }
257
258 util_doProperty("NumberOfBlocks", uint64, buf, cim_false,
259 inst, errp);
260
261 if (*errp != 0) {
262 ex = cim_getLastError();
263 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC,
264 CIM_ERR_FAILED, ADD_PROPERTY_FAILURE, ex, errp);
265 cim_freeInstance(inst);
266 nvlist_free(nvlp);
267 return ((CCIMInstance *)NULL);
268 }
269 }
270 } /* End for */
271 nvlist_free(nvlp);
272
273 /*
274 * Get the associated drive descriptor to get the status information.
275 * about this media.
276 */
277
278 dlist = dm_get_associated_descriptors(desc, DM_DRIVE, &error);
279 if (error != 0) {
280 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC, CIM_ERR_FAILED,
281 DM_GET_ASSOC_FAILURE, NULL, errp);
282 cim_freeInstance(inst);
283 return ((CCIMInstance *)NULL);
284 }
285
286
287 nvlp = dm_get_attributes(dlist[0], &error);
288 dm_free_descriptors(dlist);
289 if (error != 0) {
290 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC, CIM_ERR_FAILED,
291 DM_GET_ATTR_FAILURE, NULL, errp);
292 cim_freeInstance(inst);
293 return ((CCIMInstance *)NULL);
294 }
295
296 if (nvlp == NULL) {
297 return (inst);
298 }
299
300 for (nvp = nvlist_next_nvpair(nvlp, NULL); nvp != NULL;
301 nvp = nvlist_next_nvpair(nvlp, nvp)) {
302
303 char *attrname;
304 uint32_t ui32;
305 char *status;
306 char *statusinfo;
307
308 attrname = nvpair_name(nvp);
309 /* If the attrname for this nvp is null, try the next one. */
310
311 if (attrname == NULL) {
312 continue;
313 }
314
315 if (strcasecmp(attrname, DM_STATUS) == 0) {
316 error = nvpair_value_uint32(nvp, &ui32);
317 if (error < 0) {
318 cim_freeInstance(inst);
319 nvlist_free(nvlp);
320 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC,
321 CIM_ERR_FAILED, DM_GET_ATTR_FAILURE, NULL, errp);
322 return ((CCIMInstance *)NULL);
323 }
324 if (ui32 == 0) {
325 status = "Error";
326 statusinfo = "4";
327 } else {
328 status = "OK";
329 statusinfo = "3";
330 }
331 util_doProperty("Status", string, status, cim_false,
332 inst, errp);
333 util_doProperty("StatusInfo", uint16, statusinfo, cim_false,
334 inst, errp);
335
336 if (*errp != 0) {
337 ex = cim_getLastError();
338 util_handleError(DISK_DESCRIPTOR_FUNC,
339 CIM_ERR_FAILED, ADD_PROPERTY_FAILURE, ex, errp);
340 cim_freeInstance(inst);
341 nvlist_free(nvlp);
342 return ((CCIMInstance *)NULL);
343 }
344 } /* End ifelse */
345 } /* end for */
346 nvlist_free(nvlp);
347 return (inst);
348 }
349
350 /* Convert the descriptor list to a CIMInstance List */
351
352 CCIMInstanceList*
logicaldisk_descriptors_toCCIMInstanceList(char * providerName,dm_descriptor_t * dp,int * errp)353 logicaldisk_descriptors_toCCIMInstanceList(char *providerName,
354 dm_descriptor_t *dp, int *errp)
355 {
356 CCIMInstance *inst;
357 CCIMInstanceList *instList = NULL;
358 CCIMException *ex;
359 dm_descriptor_t desc;
360 int error;
361 int i;
362
363 *errp = 0;
364
365
366 /* If not descriptpr list, return an empty instance list. */
367 if (dp == NULL) {
368 return ((CCIMInstanceList *)NULL);
369 }
370
371 /* Create the instance list which will store the instances */
372 instList = cim_createInstanceList();
373 if (instList == NULL) {
374 ex = cim_getLastError();
375 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC,
376 CIM_ERR_FAILED, CREATE_INSTANCE_LIST_FAILURE, ex, errp);
377 return ((CCIMInstanceList *)NULL);
378 }
379 for (i = 0; dp[i] != NULL; i ++) {
380 desc = dp[i];
381 inst = logicaldisk_descriptor_toCCIMInstance(hostName, desc,
382 providerName, &error);
383 if (error != 0) {
384 /* Error handling is done in the subfunction. */
385 cim_freeInstanceList(instList);
386 return ((CCIMInstanceList *)NULL);
387 }
388
389 if (inst == NULL) {
390 continue;
391 }
392
393 /* add the instance to the instance list */
394 instList = cim_addInstance(instList, inst);
395 if (instList == NULL) {
396 ex = cim_getLastError();
397 util_handleError(LOGICALDISK_DESCRIPTOR_FUNC,
398 CIM_ERR_FAILED, ADD_INSTANCE_FAILURE, ex, errp);
399 cim_freeInstance(inst);
400 return ((CCIMInstanceList *)NULL);
401 }
402 }
403 return (instList);
404 }
405