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 <unistd.h>
30 #include <netdb.h>
31 #include <errno.h>
32 
33 #include "util.h"
34 #include "cimKeys.h"
35 #include "providerNames.h"
36 #include "messageStrings.h"
37 #include "mediapresent_descriptors.h"
38 #include "drive_descriptors.h"
39 #include "logicaldisk_descriptors.h"
40 #include "Solaris_MediaPresent.h"
41 
42 #define	MEDIA_GETINSTANCE		"MEDIA_PRESENT,GET_INSTANCE"
43 #define	MEDIA_ENUMINSTANCES		"MEDIA_PRESENT,ENUM_INSTANCES"
44 #define	MEDIA_ENUMINSTANCENAMES		"MEDIA_PRESENT,ENUM_INSTANCENAMES"
45 #define	MEDIA_CREATEINSTANCE		"MEDIA_PRESENT,CREATE_INSTANCE"
46 #define	MEDIA_DELETEINSTANCE		"MEDIA_PRESENT,DELETE_INSTANCE"
47 #define	MEDIA_SETINSTANCE		"MEDIA_PRESENT,SET_INSTANCE"
48 #define	MEDIA_GETPROPERTY		"MEDIA_PRESENT,GET_PROPERTY"
49 #define	MEDIA_SETPROPERTY		"MEDIA_PRESENT,SET_PROPERTY"
50 #define	MEDIA_INVOKEMETHOD		"MEDIA_PRESENT,INVOKE_METHOD"
51 #define	MEDIA_EXECQUERY			"MEDIA_PRESENT,EXEC_QUERY"
52 #define	MEDIA_ASSOCIATORS		"MEDIA_PRESENT,ASSOCIATORS"
53 #define	MEDIA_ASSOCIATORNAMES		"MEDIA_PRESENT,ASSOCIATOR_NAMES"
54 #define	MEDIA_REFERENCES		"MEDIA_PRESENT,REFERENCES"
55 #define	MEDIA_REFERENCENAMES		"MEDIA_PRESENT,REFERENCE_NAMES"
56 
57 
58 static CCIMInstanceList  *createMediaPresRefList(
59     CCIMObjectPath *pObjectName, cimchar *pObjectNameRole,
60 	CCIMObjectPathList *objList, cimchar *objRole, int *error);
61 
62 static CCIMInstance  *createMediaPresRefInst(
63     CCIMObjectPath *pObjectName, cimchar *pObjectNameRole,
64 	CCIMObjectPath *objName, cimchar *objRole, int *error);
65 /*
66  * Solaris_MediaPresent provider
67  *
68  * It is important to note that all memory allocated by these functions
69  * and passed to the CIMOM, is freed by the door process prior to
70  * sending a copy of the data to the CIMOM.
71  */
72 
73 /*
74  * Name: cp_getInstance_Solaris_MediaPresent
75  *
76  * Description: Returns an instance which matches the passed in object path
77  * if found.
78  *
79  * Parameters:
80  *	pOP - An CCIMObjectPath * which contains the information on
81  *	the class for which to find the instance.
82  * Returns:
83  *	CCIMInstance * if matched instance is found. Otherwise, NULL.
84  */
85 
86 CCIMInstance*
cp_getInstance_Solaris_MediaPresent(CCIMObjectPath * pOP)87 cp_getInstance_Solaris_MediaPresent(CCIMObjectPath* pOP)
88 {
89 	CCIMInstance* 		inst = NULL;
90 	CCIMPropertyList* 	pCurPropList;
91 	CCIMObjectPath		*antOp = NULL;
92 	CCIMObjectPath		*depOp = NULL;
93 	dm_descriptor_t		dd_descriptor;
94 	dm_descriptor_t		ld_descriptor;
95 	char			*name;
96 	int			error;
97 
98 	if (pOP == NULL ||
99 	    (pCurPropList = pOP->mKeyProperties) == NULL) {
100 	    util_handleError(MEDIA_GETINSTANCE, CIM_ERR_INVALID_PARAMETER, NULL,
101 		NULL, &error);
102 	    return ((CCIMInstance *)NULL);
103 	}
104 
105 	antOp = (CCIMObjectPath *)util_getKeyValue(pCurPropList, reference,
106 	    ANTECEDENT, &error);
107 
108 	if (error == 0) {
109 	    depOp = (CCIMObjectPath *)util_getKeyValue(pCurPropList, reference,
110 		DEPENDENT, &error);
111 	}
112 	/*
113 	 * Make sure we have both keys. If not, this is an error.
114 	 */
115 
116 	if (error != 0) {
117 	    util_handleError(MEDIA_GETINSTANCE, CIM_ERR_INVALID_PARAMETER, NULL,
118 		NULL, &error);
119 	    return ((CCIMInstance*)NULL);
120 	}
121 
122 	/*
123 	 * Now, get the name of the antecedent from the object path.
124 	 */
125 
126 	if ((pCurPropList = antOp->mKeyProperties) == NULL ||
127 	    (pCurPropList = depOp->mKeyProperties) == NULL) {
128 	    util_handleError(MEDIA_GETINSTANCE, CIM_ERR_INVALID_PARAMETER, NULL,
129 		NULL, &error);
130 	    return ((CCIMInstance *)NULL);
131 	}
132 
133 	/*
134 	 * both of the objects have the deviceid as the key value.
135 	 */
136 	pCurPropList = antOp->mKeyProperties;
137 	name = (cimchar *)util_getKeyValue(pCurPropList, string, DEVICEID,
138 	    &error);
139 	if (error != 0) {
140 	    util_handleError(MEDIA_GETINSTANCE, CIM_ERR_INVALID_PARAMETER, NULL,
141 		NULL, &error);
142 	    return ((CCIMInstance *)NULL);
143 	}
144 	dd_descriptor = dm_get_descriptor_by_name(DM_DRIVE, name, &error);
145 	/*
146 	 * Not found. Return a null instance.
147 	 */
148 	if (error == ENODEV || dd_descriptor) {
149 	    return ((CCIMInstance *)NULL);
150 	}
151 
152 	if (error != 0) {
153 	    util_handleError(MEDIA_GETINSTANCE, CIM_ERR_FAILED,
154 		DM_GET_DESC_BYNAME_FAILURE, NULL, &error);
155 	    return ((CCIMInstance*)NULL);
156 	}
157 
158 	/*
159 	 * Only need the descriptor to verify the device still exists.
160 	 */
161 	dm_free_descriptor(dd_descriptor);
162 
163 	/*
164 	 * Now, get the name of the dependent from the object path.
165 	 */
166 
167 	pCurPropList = depOp->mKeyProperties;
168 	name = (cimchar *)util_getKeyValue(pCurPropList, string, DEVICEID,
169 	    &error);
170 	if (error != 0) {
171 	    util_handleError(MEDIA_GETINSTANCE, CIM_ERR_INVALID_PARAMETER,
172 		NULL, NULL, &error);
173 	    return ((CCIMInstance *)NULL);
174 	}
175 
176 	ld_descriptor = dm_get_descriptor_by_name(DM_MEDIA, name, &error);
177 	/*
178 	 * Not found. Return a null instance.
179 	 */
180 
181 	if (error == ENODEV || ld_descriptor == NULL) {
182 	    return ((CCIMInstance *)NULL);
183 	}
184 
185 	if (error != 0) {
186 	    util_handleError(MEDIA_GETINSTANCE, CIM_ERR_FAILED,
187 		DM_GET_DESC_BYNAME_FAILURE, NULL, &error);
188 	    return ((CCIMInstance *)NULL);
189 	}
190 	dm_free_descriptor(ld_descriptor);
191 
192 	/* Turn these descriptors in to a media present instance */
193 	inst = createMediaPresRefInst(antOp, ANTECEDENT, depOp, DEPENDENT,
194 	    &error);
195 	if (error != 0) {
196 	    util_handleError(MEDIA_GETINSTANCE, CIM_ERR_FAILED,
197 		MEDIAPRES_ASSOC_TO_INSTANCE_FAILURE, NULL, &error);
198 	    return ((CCIMInstance *)NULL);
199 	}
200 	return (inst);
201 }
202 
203 /*
204  * Name: cp_enumInstances_Solaris_MediaPresent
205  *
206  * Description: Returns a linked list of instances of associated objects
207  * if found.
208  *
209  * Parameters:
210  *	pOP - An CCIMObjectPath * which contains the information on
211  *	the class for which to find the instances.
212  * Returns:
213  *	CCIMInstanceList * if istances are found. Otherwise NULL,
214  */
215 
216 /* ARGSUSED */
217 CCIMInstanceList*
cp_enumInstances_Solaris_MediaPresent(CCIMObjectPath * pOP)218 cp_enumInstances_Solaris_MediaPresent(CCIMObjectPath* pOP)
219 {
220 	CCIMInstanceList	*instList = NULL;
221 	CCIMInstance		*inst;
222 	CCIMException*		ex;
223 	dm_descriptor_t		*dd_descriptorp = NULL;
224 	dm_descriptor_t		*ld_descriptorp = NULL;
225 	int			error = 0;
226 	int			filter[2];
227 	int			i = 0;
228 	int			j = 0;
229 
230 	filter[0] = DM_DT_FIXED;
231 	filter[1] = DM_FILTER_END;
232 
233 	if (pOP == NULL) {
234 	    util_handleError(MEDIA_ENUMINSTANCES, CIM_ERR_INVALID_PARAMETER,
235 		NULL, NULL, &error);
236 	    return ((CCIMInstanceList *)NULL);
237 	}
238 	dd_descriptorp = dm_get_descriptors(DM_DRIVE, filter, &error);
239 	if (dd_descriptorp == NULL) {
240 	    return ((CCIMInstanceList *)NULL);
241 	}
242 
243 	if (dd_descriptorp[0] == NULL) {
244 	    dm_free_descriptors(dd_descriptorp);
245 	    return ((CCIMInstanceList *)NULL);
246 	}
247 
248 	if (error != 0) {
249 	    util_handleError(MEDIA_ENUMINSTANCES, CIM_ERR_FAILED,
250 		DM_GET_DESCRIPTORS, NULL, &error);
251 	    return ((CCIMInstanceList *)NULL);
252 	}
253 
254 	/*
255 	 * For each one of the drives found, get the associated media, if
256 	 * any.
257 	 */
258 
259 	instList = cim_createInstanceList();
260 	if (instList == NULL) {
261 	    ex = cim_getLastError();
262 	    util_handleError(MEDIA_ENUMINSTANCES, CIM_ERR_FAILED,
263 		CREATE_INSTANCE_LIST_FAILURE, ex, &error);
264 	    dm_free_descriptors(dd_descriptorp);
265 	    return ((CCIMInstanceList *)NULL);
266 	}
267 
268 	for (i = 0; dd_descriptorp[i] != NULL; i ++) {
269 	    ld_descriptorp = dm_get_associated_descriptors(
270 		dd_descriptorp[i], DM_MEDIA, &error);
271 
272 	    /* If no media associated with this disk, continue */
273 
274 	    if (ld_descriptorp == NULL) {
275 		continue;
276 	    }
277 
278 	    if (error != 0) {
279 		util_handleError(MEDIA_ENUMINSTANCES, CIM_ERR_FAILED,
280 		    DM_GET_ASSOC_FAILURE, NULL, &error);
281 		dm_free_descriptors(dd_descriptorp);
282 		cim_freeInstanceList(instList);
283 		return ((CCIMInstanceList *)NULL);
284 	    }
285 
286 	    for (j = 0; ld_descriptorp[j] != NULL; j ++) {
287 		inst = mediapresent_descriptor_toCCIMInstance(hostName,
288 		    dd_descriptorp[i], ld_descriptorp[j],
289 			MEDIA_PRESENT, &error);
290 		if (error != 0) {
291 		    util_handleError(MEDIA_ENUMINSTANCES, CIM_ERR_FAILED,
292 			MEDIAPRES_DESC_TO_INSTANCE_FAILURE, NULL, &error);
293 		    dm_free_descriptors(dd_descriptorp);
294 		    dm_free_descriptors(ld_descriptorp);
295 		    cim_freeInstanceList(instList);
296 		    return ((CCIMInstanceList *)NULL);
297 		}
298 
299 		instList = cim_addInstance(instList, inst);
300 		if (instList == NULL) {
301 		    ex = cim_getLastError();
302 		    util_handleError(MEDIA_ENUMINSTANCES, CIM_ERR_FAILED,
303 			ADD_INSTANCE_FAILURE, ex, NULL);
304 		    dm_free_descriptors(dd_descriptorp);
305 		    dm_free_descriptors(ld_descriptorp);
306 		    return ((CCIMInstanceList *)NULL);
307 		}
308 	    }
309 
310 	    dm_free_descriptors(ld_descriptorp);
311 	}
312 	dm_free_descriptors(dd_descriptorp);
313 
314 	if (instList->mDataObject == NULL) {
315 	    cim_freeInstanceList(instList);
316 	    instList = NULL;
317 	}
318 
319 	return (instList);
320 }
321 
322 /*
323  * Name: cp_enumInstanceNames_Solaris_MediaPresent
324  *
325  * Description: Returns a linked list of CCIMObjectPath *
326  *      of Solaris_MediaPresent if found.
327  *
328  * Parameters:
329  *	pOP - An CCIMObjectPath * which contains the information on
330  *	the class for which to find the instances.
331  * Returns:
332  *	CCIMObjectPathList * if objects are found. Otherwise NULL.
333  */
334 
335 CCIMObjectPathList*
cp_enumInstanceNames_Solaris_MediaPresent(CCIMObjectPath * pOP)336 cp_enumInstanceNames_Solaris_MediaPresent(CCIMObjectPath * pOP) {
337 
338 	CCIMInstanceList	*instList;
339 	CCIMObjectPathList	*objList = NULL;
340 	int			error;
341 
342 	if (pOP == NULL) {
343 	    util_handleError(MEDIA_ENUMINSTANCENAMES,
344 		CIM_ERR_INVALID_PARAMETER, NULL, NULL, &error);
345 	    return ((CCIMObjectPathList *)NULL);
346 	}
347 
348 	/*
349 	 * Call in to enumInstances and then convert the instance list in
350 	 * to an object list.
351 	 */
352 
353 	instList = cp_enumInstances_Solaris_MediaPresent(pOP);
354 
355 	if (instList != NULL) {
356 	    objList = cim_createObjectPathList(instList);
357 	    cim_freeInstanceList(instList);
358 	}
359 	return (objList);
360 }
361 
362 /*
363  * Creating an instance of a Solaris_MediaPresent is not supported.
364  */
365 
366 /* ARGSUSED */
367 CCIMObjectPath*
cp_createInstance_Solaris_MediaPresent(CCIMObjectPath * pOP,CCIMInstance * pInst)368 cp_createInstance_Solaris_MediaPresent(
369     CCIMObjectPath* pOP, CCIMInstance* pInst)
370 {
371 	int	error;
372 
373 	util_handleError(MEDIA_CREATEINSTANCE, CIM_ERR_NOT_SUPPORTED,
374 	    NULL, NULL, &error);
375 	return ((CCIMObjectPath *)NULL);
376 }
377 
378 /*
379  * Deleting an instance of a Solaris_MediaPresent is not supported.
380  */
381 
382 /* ARGSUSED */
383 CIMBool
cp_deleteInstance_Solaris_MediaPresent(CCIMObjectPath * pInst)384 cp_deleteInstance_Solaris_MediaPresent(CCIMObjectPath* pInst)
385 {
386 	int	error;
387 
388 	util_handleError(MEDIA_DELETEINSTANCE, CIM_ERR_NOT_SUPPORTED,
389 	    NULL, NULL, &error);
390 	return (cim_false);
391 }
392 
393 /*
394  * Name: cp_getProperty_Solaris_MediaPresent
395  *
396  * Description: Returns the property requested, if found.
397  *
398  * Parameters:
399  *	pOP - An CCIMObjectPath * which contains the information on
400  *	the class for which to find the instances.
401  * Returns:
402  *	CCIMProperty * if found.
403  */
404 
405 /* ARGSUSED */
406 CCIMProperty	*
cp_getProperty_Solaris_MediaPresent(CCIMObjectPath * pOP,char * pPropName)407 cp_getProperty_Solaris_MediaPresent(CCIMObjectPath *pOP,
408     char *pPropName)
409 {
410 
411 	CCIMProperty	*prop = NULL;
412 	CCIMInstance	*inst = NULL;
413 	int		error = 0;
414 
415 	if (pOP == NULL) {
416 	    util_handleError(MEDIA_GETPROPERTY, CIM_ERR_INVALID_PARAMETER,
417 		NULL, NULL, &error);
418 	    return ((CCIMProperty *)NULL);
419 	}
420 
421 	inst = cp_getInstance_Solaris_MediaPresent(pOP);
422 	if (inst == NULL) {
423 	    return ((CCIMProperty *)NULL);
424 	}
425 
426 	prop = cim_getProperty(inst, pPropName);
427 	cim_freeInstance(inst);
428 	return (prop);
429 }
430 
431 /*
432  * Setting an instance of a Solaris_MediaPresent is not supported.
433  */
434 
435 /* ARGSUSED */
436 CIMBool
cp_setInstance_Solaris_MediaPresent(CCIMObjectPath * pOP,CCIMInstance * pInst)437 cp_setInstance_Solaris_MediaPresent(CCIMObjectPath* pOP, CCIMInstance* pInst)
438 {
439 	int	error;
440 
441 	util_handleError(MEDIA_SETINSTANCE, CIM_ERR_NOT_SUPPORTED,
442 	    NULL, NULL, &error);
443 	return (cim_false);
444 }
445 
446 /*
447  * Setting a property on a Solaris_MediaPresent is not supported.
448  */
449 
450 /* ARGSUSED */
451 CIMBool
cp_setProperty_Solaris_MediaPresent(CCIMObjectPath * pOP,CCIMProperty * pProp)452 cp_setProperty_Solaris_MediaPresent(CCIMObjectPath* pOP, CCIMProperty* pProp)
453 {
454 	int	error;
455 
456 	util_handleError(MEDIA_SETPROPERTY, CIM_ERR_NOT_SUPPORTED,
457 	    NULL, NULL, &error);
458 	return (cim_false);
459 }
460 
461 /*
462  * No Methods for Solaris_MediaPresent.
463  */
464 
465 /* ARGSUSED */
466 CCIMProperty*
cp_invokeMethod_Solaris_MediaPresent(CCIMObjectPath * op,cimchar * methodName,CCIMPropertyList * inParams,CCIMPropertyList * outParams)467 cp_invokeMethod_Solaris_MediaPresent(CCIMObjectPath* op, cimchar* methodName,
468 	CCIMPropertyList* inParams, CCIMPropertyList* outParams)
469 {
470 	CCIMProperty	*retVal = (CCIMProperty	*)NULL;
471 	return (retVal);
472 }
473 
474 /*
475  * Name: cp_execQuery_Solaris_MediaPresent
476  *
477  * Description:
478  * Returns an instance list which matches the query if any are found.
479  *
480  * Parameters:
481  *	CCIMObjectPath *op - An CCIMObjectPath * which contains the
482  *	information on the class for which to find the instances.
483  *
484  * 	selectList - Not used
485  *	nonJoinExp - Not used
486  *
487  * Returns:
488  *	CCIMInstance * if matched instance is found. Otherwise, NULL.
489  */
490 /*
491  * Currently, there is no WQL parser for the C providers. As a result,
492  * what is returned to the CIMOM is a list of instances with
493  * a NULL value at the beginning of the list. This NULL value indicates
494  * to the CIMOM that it must do the filtering for the client.
495  */
496 
497 /* ARGSUSED */
498 CCIMInstanceList*
cp_execQuery_Solaris_MediaPresent(CCIMObjectPath * op,cimchar * selectList,cimchar * nonJoinExp,cimchar * queryExp,int queryType)499 cp_execQuery_Solaris_MediaPresent(CCIMObjectPath *op, cimchar *selectList,
500     cimchar *nonJoinExp, cimchar *queryExp, int queryType)
501 {
502 	CCIMInstanceList	*instList = NULL;
503 	CCIMInstanceList	*result;
504 	CCIMInstance		*emptyInst;
505 	CCIMException		*ex;
506 	int			error;
507 
508 	if (op == NULL) {
509 	    util_handleError(MEDIA_EXECQUERY, CIM_ERR_INVALID_PARAMETER, NULL,
510 		NULL, &error);
511 	    return ((CCIMInstanceList *)NULL);
512 	}
513 
514 	instList = cp_enumInstances_Solaris_MediaPresent(op);
515 
516 	if (instList == NULL) {
517 	    return ((CCIMInstanceList *)NULL);
518 	}
519 
520 	/*
521 	 * Create a null instance and add it to the beginning
522 	 * of the list to indicate to the CIMOM that no filtering
523 	 * was done.
524 	 */
525 
526 	emptyInst = cim_createInstance("");
527 	if (emptyInst == NULL) {
528 	    ex = cim_getLastError();
529 	    util_handleError(MEDIA_EXECQUERY, CIM_ERR_FAILED,
530 		CREATE_INSTANCE_FAILURE, ex, &error);
531 	    return ((CCIMInstanceList *)NULL);
532 	}
533 
534 	result = cim_createInstanceList();
535 	if (result == NULL) {
536 	    ex = cim_getLastError();
537 	    util_handleError(MEDIA_EXECQUERY, CIM_ERR_FAILED,
538 		CREATE_INSTANCE_LIST_FAILURE, ex, &error);
539 	    cim_freeInstance(emptyInst);
540 	    cim_freeInstanceList(instList);
541 	    return ((CCIMInstanceList *)NULL);
542 	}
543 
544 	result = cim_addInstance(result, emptyInst);
545 	if (result == NULL) {
546 	    ex = cim_getLastError();
547 	    util_handleError(MEDIA_EXECQUERY, CIM_ERR_FAILED,
548 		ADD_INSTANCE_FAILURE, ex, &error);
549 	    cim_freeInstance(emptyInst);
550 	    cim_freeInstanceList(instList);
551 	    return ((CCIMInstanceList *)NULL);
552 	}
553 
554 	/*
555 	 * Since copying the original list to the new list will
556 	 * leave no way to free the original list, manually
557 	 * concatenate the original list to the new one.
558 	 */
559 
560 	result->mNext = instList;
561 	return (result);
562 }
563 
564 /*
565  * Name: cp_associators_Solaris_MediaPresent
566  *
567  * Description:
568  * Returns a instances of objects associated with the passed in
569  * object if there are any.
570  *
571  * Parameters:
572  *
573  *	CCIMObjectPath *pAssocName - The name of the association that
574  *	the client wants information about.
575  *
576  *	CCIMObjectPath *pObjectName - An CCIMObjectPath * which contains the
577  *	information on the class for which to find the associated instances.
578  *
579  *	cimchar *pResultClass - If specified, only return instances that
580  *	are of this class type.
581  *
582  *      cimchar *pRole - If specified, must be valid for the object path
583  *	passed in requesting the associated instances.
584  *
585  *	cimchar *pResultRole - If specified, only return instances that
586  *	are playing this role in the association.
587  *
588  *
589  * Returns:
590  *	CCIMInstanceList * if associated objects are found.
591  *	NULL is returned on error and for an empty list.
592  */
593 /* ARGSUSED */
594 CCIMInstanceList *
cp_associators_Solaris_MediaPresent(CCIMObjectPath * pAssocName,CCIMObjectPath * pObjectName,cimchar * pResultClass,cimchar * pRole,cimchar * pResultRole)595 cp_associators_Solaris_MediaPresent(CCIMObjectPath *pAssocName,
596     CCIMObjectPath *pObjectName, cimchar *pResultClass, cimchar *pRole,
597 	cimchar *pResultRole)
598 {
599 	CCIMPropertyList	*pCurPropList;
600 	CCIMInstanceList	*instList = NULL;
601 	CCIMInstance		*inst;
602 	CCIMException		*ex;
603 	dm_descriptor_t		*assoc_descriptors;
604 	dm_descriptor_t		obj_desc;
605 	char			*name;
606 	int			error = 0;
607 	int			isAntecedent = 0;
608 	int			i;
609 
610 
611 	if (pObjectName == NULL || pObjectName->mName == NULL ||
612 		pObjectName->mKeyProperties == NULL) {
613 	    util_handleError(MEDIA_ASSOCIATORS, CIM_ERR_INVALID_PARAMETER,
614 		NULL, NULL, &error);
615 	    return ((CCIMInstanceList *)NULL);
616 	}
617 	if (strcasecmp(pObjectName->mName, DISK_DRIVE) == 0) {
618 	    isAntecedent = 1;
619 	}
620 
621 	if (pRole != NULL) {
622 	    if (strcasecmp(pRole, ANTECEDENT) == 0) {
623 		if (isAntecedent != 1) {
624 		    util_handleError(MEDIA_ASSOCIATORS,
625 			CIM_ERR_INVALID_PARAMETER, NULL, NULL, &error);
626 		    return ((CCIMInstanceList *)NULL);
627 		}
628 	    } else if (strcasecmp(pRole, DEPENDENT) == 0) {
629 		if (isAntecedent == 1) {
630 		    util_handleError(MEDIA_ASSOCIATORS,
631 			CIM_ERR_INVALID_PARAMETER, NULL, NULL, &error);
632 		    return ((CCIMInstanceList *)NULL);
633 		}
634 	    }
635 	}
636 
637 	/*
638 	 * Both logical disk and disk drive have deviceid as the
639 	 * key.
640 	 */
641 
642 	pCurPropList = pObjectName->mKeyProperties;
643 	name = (cimchar *)util_getKeyValue(pCurPropList, string, DEVICEID,
644 	    &error);
645 	if (error != 0) {
646 	    util_handleError(MEDIA_ASSOCIATORS, CIM_ERR_INVALID_PARAMETER, NULL,
647 		NULL, &error);
648 	    return ((CCIMInstanceList *)NULL);
649 	}
650 
651 	if (isAntecedent) {
652 	    obj_desc = dm_get_descriptor_by_name(DM_DRIVE, name, &error);
653 	} else {
654 	    obj_desc = dm_get_descriptor_by_name(DM_MEDIA, name, &error);
655 	}
656 	if (error == ENODEV || obj_desc == NULL) {
657 	    return ((CCIMInstanceList *)NULL);
658 	}
659 	if (error != 0) {
660 	    util_handleError(MEDIA_ASSOCIATORS, CIM_ERR_FAILED,
661 		    DM_GET_DESC_BYNAME_FAILURE, NULL, &error);
662 	    return ((CCIMInstanceList *)NULL);
663 	}
664 
665 	if (isAntecedent) {
666 
667 	    /* Get associated descriptors. */
668 
669 	    assoc_descriptors = dm_get_associated_descriptors(obj_desc,
670 		DM_MEDIA, &error);
671 	    dm_free_descriptor(obj_desc);
672 
673 	    if (assoc_descriptors == NULL) {
674 		return ((CCIMInstanceList *)NULL);
675 	    }
676 
677 	    if (assoc_descriptors[0] == NULL) {
678 		dm_free_descriptors(assoc_descriptors);
679 		return ((CCIMInstanceList *)NULL);
680 	    }
681 
682 	    if (error != 0) {
683 		util_handleError(MEDIA_ASSOCIATORS,  CIM_ERR_FAILED,
684 		    DM_GET_ASSOC_FAILURE, NULL, &error);
685 		return ((CCIMInstanceList *)NULL);
686 	    }
687 
688 	    instList = cim_createInstanceList();
689 	    if (instList == NULL) {
690 		ex = cim_getLastError();
691 		util_handleError(MEDIA_ASSOCIATORS, CIM_ERR_FAILED,
692 		    CREATE_INSTANCE_LIST_FAILURE, ex, &error);
693 		dm_free_descriptors(assoc_descriptors);
694 		return ((CCIMInstanceList *)NULL);
695 	    }
696 	    /* Traverse the list and create instances of associated objects. */
697 
698 	    for (i = 0; assoc_descriptors[i] != NULL; i ++) {
699 		inst = logicaldisk_descriptor_toCCIMInstance(hostName,
700 		    assoc_descriptors[i], LOGICAL_DISK, &error);
701 
702 		if (error != 0) {
703 		    ex = cim_getLastError();
704 		    util_handleError(MEDIA_ASSOCIATORS, CIM_ERR_FAILED,
705 			LOGICALDISK_DESC_TO_INSTANCE_FAILURE, ex, &error);
706 		    dm_free_descriptors(assoc_descriptors);
707 		    cim_freeInstanceList(instList);
708 		    return ((CCIMInstanceList *)NULL);
709 		}
710 		instList = cim_addInstance(instList, inst);
711 		if (instList == NULL) {
712 		    ex = cim_getLastError();
713 		    util_handleError(MEDIA_ASSOCIATORS, CIM_ERR_FAILED,
714 			ADD_INSTANCE_FAILURE, ex, &error);
715 		    dm_free_descriptors(assoc_descriptors);
716 		    cim_freeInstance(inst);
717 		    return ((CCIMInstanceList *)NULL);
718 		}
719 	    } /* End for */
720 	} else {
721 		/*
722 		 * This is the logical disk calling this function. Return the
723 		 * logical disk that this belongs to.
724 		 */
725 
726 	    assoc_descriptors = dm_get_associated_descriptors(obj_desc,
727 		DM_DRIVE, &error);
728 	    dm_free_descriptor(obj_desc);
729 
730 	    if (assoc_descriptors == NULL) {
731 		return ((CCIMInstanceList *)NULL);
732 	    }
733 
734 	    if (assoc_descriptors[0] == NULL) {
735 		dm_free_descriptors(assoc_descriptors);
736 		return ((CCIMInstanceList *)NULL);
737 	    }
738 
739 	    if (error != 0) {
740 		util_handleError(MEDIA_ASSOCIATORS,  CIM_ERR_FAILED,
741 		    DM_GET_ASSOC_FAILURE, NULL, &error);
742 		return ((CCIMInstanceList *)NULL);
743 	    }
744 
745 	    instList = cim_createInstanceList();
746 	    if (instList == NULL) {
747 		ex = cim_getLastError();
748 		util_handleError(MEDIA_ASSOCIATORS, CIM_ERR_FAILED,
749 		    CREATE_INSTANCE_LIST_FAILURE, ex, &error);
750 		dm_free_descriptors(assoc_descriptors);
751 		return ((CCIMInstanceList *)NULL);
752 	    }
753 	    for (i = 0; assoc_descriptors[i] != NULL; i ++) {
754 		inst = drive_descriptor_toCCIMInstance(hostName,
755 		    assoc_descriptors[i], DISK_DRIVE, &error);
756 
757 		if (error != 0) {
758 		    ex = cim_getLastError();
759 		    util_handleError(MEDIA_ASSOCIATORS, CIM_ERR_FAILED,
760 			DRIVE_DESC_TO_INSTANCE_FAILURE, ex, &error);
761 		    dm_free_descriptors(assoc_descriptors);
762 		    cim_freeInstanceList(instList);
763 		    return ((CCIMInstanceList *)NULL);
764 		}
765 
766 		instList = cim_addInstance(instList, inst);
767 		if (instList == NULL) {
768 		    ex = cim_getLastError();
769 		    util_handleError(MEDIA_ASSOCIATORS, CIM_ERR_FAILED,
770 			ADD_INSTANCE_FAILURE, ex, &error);
771 		    dm_free_descriptors(assoc_descriptors);
772 		    cim_freeInstance(inst);
773 		    return ((CCIMInstanceList *)NULL);
774 		}
775 	    } /* End for */
776 	}
777 	dm_free_descriptors(assoc_descriptors);
778 	return (instList);
779 }
780 
781 /*
782  * Name: cp_associatorNames_Solaris_MediaPresent
783  *
784  * Description:
785  * Returns a list of objects associated with the passed in
786  * object if there are any via the object CCIMObjectPath.
787  *
788  * Parameters:
789  *
790  *	CCIMObjectPath *pAssocName - The name of the association that
791  *	the client wants information about.
792  *
793  *	CCIMObjectPath *pObjectName - An CCIMObjectPath * which contains the
794  *	information on the class for which to find the associated instances.
795  *
796  *	cimchar *pResultClass - If specified, only return instances that
797  *	are of this class type.
798  *
799  *      cimchar *pRole - If specified, must be valid for the object path
800  *	passed in requesting the associated instances.
801  *
802  *	cimchar *pResultRole - If specified, only return instances that
803  *	are playing this role in the association.
804  *
805  *
806  * Returns:
807  *	CCIMObjectPathList * if associated objects are found. NULL otherwise.
808  */
809 
810 /* ARGSUSED */
811 CCIMObjectPathList *
cp_associatorNames_Solaris_MediaPresent(CCIMObjectPath * pAssocName,CCIMObjectPath * pObjectName,cimchar * pResultClass,cimchar * pRole,cimchar * pResultRole)812 cp_associatorNames_Solaris_MediaPresent(CCIMObjectPath *pAssocName,
813     CCIMObjectPath *pObjectName, cimchar *pResultClass, cimchar *pRole,
814 	cimchar *pResultRole)
815 {
816 
817 	CCIMInstanceList	*instList;
818 	CCIMObjectPathList	*objList = NULL;
819 	int			error;
820 
821 	if (pObjectName == NULL) {
822 	    util_handleError(MEDIA_ASSOCIATORNAMES,
823 		CIM_ERR_INVALID_PARAMETER, NULL, NULL, &error);
824 	    return ((CCIMObjectPathList *)NULL);
825 	}
826 
827 	instList =
828 	    cp_associators_Solaris_MediaPresent(
829 		pAssocName, pObjectName, pResultClass, pRole, pResultRole);
830 
831 	if (instList != NULL) {
832 	    objList = cim_createObjectPathList(instList);
833 	    cim_freeInstanceList(instList);
834 	}
835 
836 	return (objList);
837 }
838 
839 /*
840  * Name: cp_references_Solaris_MediaPresent
841  *
842  * Description:
843  * Returns a instances of objects that have references to the passed in
844  * object if there are any.
845  *
846  * Parameters:
847  *
848  *	CCIMObjectPath *pAssocName - The name of the association that
849  *	the client wants information about.
850  *
851  *	CCIMObjectPath *pObjectName - An CCIMObjectPath * which contains the
852  *	information on the class for which to find the associated instances.
853  *
854  *      cimchar *pRole - If specified, must be valid for the object path
855  *	passed in requesting the associated instances.
856  *
857  * Returns:
858  *	CCIMObjectPathList * if associated objects are found. NULL otherwise.
859  */
860 
861 /* ARGSUSED */
862 CCIMInstanceList *
cp_references_Solaris_MediaPresent(CCIMObjectPath * pAssocName,CCIMObjectPath * pObjectName,char * pRole)863 cp_references_Solaris_MediaPresent(CCIMObjectPath *pAssocName,
864 CCIMObjectPath *pObjectName, char *pRole)
865 {
866 
867 	CCIMInstanceList	*instList;
868 	CCIMObjectPathList	*objList = NULL;
869 	int			error;
870 
871 	if (pObjectName == NULL) {
872 	    util_handleError(MEDIA_REFERENCES, CIM_ERR_INVALID_PARAMETER,
873 		NULL, NULL, &error);
874 	    return ((CCIMInstanceList *)NULL);
875 	}
876 	/*
877 	 * Get the list of those objects that are referred to by
878 	 * the calling object.
879 	 */
880 
881 	objList =
882 	    cp_associatorNames_Solaris_MediaPresent(
883 		pAssocName, pObjectName, NULL, NULL, NULL);
884 	/*
885 	 * Now generate the list of instances to return.
886 	 */
887 
888 	if (strcasecmp(pObjectName->mName, DISK_DRIVE) == 0) {
889 	    instList = createMediaPresRefList(pObjectName,
890 		ANTECEDENT, objList, DEPENDENT, &error);
891 	} else {
892 	    instList = createMediaPresRefList(pObjectName,
893 		DEPENDENT, objList, ANTECEDENT, &error);
894 	}
895 
896 	cim_freeObjectPathList(objList);
897 	return (instList);
898 }
899 
900 /*
901  * Name: cp_referenceNames_Solaris_MediaPresent
902  *
903  * Description:
904  * Returns a instances of objects that have references to the passed in
905  * object if there are any.
906  *
907  * Parameters:
908  *
909  *	CCIMObjectPath *pAssocName - The name of the association that
910  *	the client wants information about.
911  *
912  *	CCIMObjectPath *pObjectName - An CCIMObjectPath * which contains the
913  *	information on the class for which to find the associated instances.
914  *
915  *      cimchar *pRole - If specified, must be valid for the object path
916  *	passed in requesting the associated instances.
917  *
918  *
919  * Returns:
920  *	CCIMInstanceList * if associated objects are found. Thist list
921  *	may be empty. NULL is returned on error.
922  *
923  */
924 /* ARGSUSED */
925 CCIMObjectPathList *
cp_referenceNames_Solaris_MediaPresent(CCIMObjectPath * pAssocName,CCIMObjectPath * pObjectName,cimchar * pRole)926 cp_referenceNames_Solaris_MediaPresent(CCIMObjectPath *pAssocName,
927     CCIMObjectPath *pObjectName, cimchar *pRole)
928 {
929 
930 	CCIMInstanceList	*instList;
931 	CCIMObjectPathList	*objList = NULL;
932 	int			error;
933 
934 	if (pObjectName == NULL) {
935 	    util_handleError(MEDIA_REFERENCENAMES,
936 		CIM_ERR_INVALID_PARAMETER, NULL, NULL, &error);
937 	    return ((CCIMObjectPathList *)NULL);
938 	}
939 
940 	instList =
941 	    cp_references_Solaris_MediaPresent(pAssocName, pObjectName, pRole);
942 
943 	if (instList != NULL) {
944 	    objList = cim_createObjectPathList(instList);
945 	    cim_freeInstanceList(instList);
946 	}
947 
948 	return (objList);
949 }
950 
951 /*
952  * Create the association class with the passed in attributes.
953  */
954 static
955 CCIMInstanceList  *
createMediaPresRefList(CCIMObjectPath * pObjectName,cimchar * pObjectNameRole,CCIMObjectPathList * objList,cimchar * objRole,int * error)956 createMediaPresRefList(CCIMObjectPath *pObjectName, cimchar *pObjectNameRole,
957 	CCIMObjectPathList *objList, cimchar *objRole, int *error)
958 {
959 
960 	CCIMObjectPathList	*tmpList;
961 	CCIMInstanceList	*instList = NULL;
962 	CCIMInstance		*inst;
963 	CCIMObjectPath		*obj1;
964 	CCIMObjectPath		*obj2;
965 	CCIMException		*ex;
966 
967 	*error = 0;
968 
969 
970 	/*
971 	 * If no objects associated with this one, return NULL.
972 	 */
973 	if (objList == NULL) {
974 	    return ((CCIMInstanceList *)NULL);
975 	}
976 
977 	instList = cim_createInstanceList();
978 	if (instList == NULL) {
979 	    ex = cim_getLastError();
980 	    util_handleError(MEDIA_PRESENT, CIM_ERR_FAILED,
981 		CREATE_INSTANCE_FAILURE, ex, error);
982 	    return ((CCIMInstanceList *)NULL);
983 	}
984 
985 	tmpList = objList;
986 	while (tmpList != NULL) {
987 	    obj1 = tmpList->mDataObject;
988 	    obj2 = cim_copyObjectPath(pObjectName);
989 	    if (obj2 == NULL) {
990 		ex = cim_getLastError();
991 		util_handleError(MEDIA_PRESENT, CIM_ERR_FAILED,
992 		    COPY_OBJPATH_FAILURE, ex, error);
993 		cim_freeInstanceList(instList);
994 		return ((CCIMInstanceList *)NULL);
995 	    }
996 
997 	    inst = createMediaPresRefInst(obj2, pObjectNameRole, obj1,
998 		objRole, error);
999 	    cim_freeObjectPath(obj2);
1000 
1001 	    if (*error != 0) {
1002 		cim_freeInstanceList(instList);
1003 		return ((CCIMInstanceList *)NULL);
1004 	    }
1005 
1006 	    instList = cim_addInstance(instList, inst);
1007 	    if (instList == NULL) {
1008 		ex = cim_getLastError();
1009 		util_handleError(MEDIA_PRESENT, CIM_ERR_FAILED,
1010 		    CREATE_INSTANCE_FAILURE, ex, NULL);
1011 		cim_freeInstance(inst);
1012 		return ((CCIMInstanceList *)NULL);
1013 	    }
1014 
1015 	    tmpList = tmpList->mNext;
1016 	}
1017 	return (instList);
1018 }
1019 static
1020 CCIMInstance  *
createMediaPresRefInst(CCIMObjectPath * obj2,cimchar * pObjectNameRole,CCIMObjectPath * obj1,cimchar * objRole,int * error)1021 createMediaPresRefInst(CCIMObjectPath *obj2,
1022     cimchar *pObjectNameRole, CCIMObjectPath *obj1, cimchar *objRole,
1023 	int *error)
1024 {
1025 
1026 	CCIMInstance	*inst = NULL;
1027 	CCIMException	*ex;
1028 
1029 	*error	= 0;
1030 
1031 	inst  = cim_createInstance(MEDIA_PRESENT);
1032 	if (inst == NULL) {
1033 	    ex = cim_getLastError();
1034 	    util_handleError(MEDIA_PRESENT, CIM_ERR_FAILED, NULL, NULL, error);
1035 	    return ((CCIMInstance *)NULL);
1036 	}
1037 	util_doReferenceProperty(pObjectNameRole, obj2, cim_true, inst, error);
1038 	if (*error != 0) {
1039 	    ex = cim_getLastError();
1040 	    util_handleError(MEDIA_PRESENT, CIM_ERR_FAILED,
1041 		CREATE_REFPROP_FAILURE, ex, error);
1042 	    cim_freeInstance(inst);
1043 	    return ((CCIMInstance *)NULL);
1044 	}
1045 
1046 	util_doReferenceProperty(objRole, obj1, cim_true, inst, error);
1047 	if (*error != 0) {
1048 	    ex = cim_getLastError();
1049 	    util_handleError(MEDIA_PRESENT, CIM_ERR_FAILED,
1050 		CREATE_REFPROP_FAILURE, ex, error);
1051 	    cim_freeInstance(inst);
1052 	    return ((CCIMInstance *)NULL);
1053 	}
1054 
1055 	util_doProperty("FixedMedia", boolean, "1", cim_false, inst, error);
1056 	if (*error != 0) {
1057 	    ex = cim_getLastError();
1058 	    util_handleError(MEDIA_PRESENT, CIM_ERR_FAILED,
1059 		CREATE_REFPROP_FAILURE, ex, error);
1060 	    cim_freeInstance(inst);
1061 	    return ((CCIMInstance *)NULL);
1062 	}
1063 
1064 	return (inst);
1065 }
1066