xref: /onnv-gate/usr/src/common/mdesc/mdesc_scandag.c (revision 1991:f29baf5bf770)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1991Sheppo  * Common Development and Distribution License (the "License").
6*1991Sheppo  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*1991Sheppo 
220Sstevel@tonic-gate /*
23*1991Sheppo  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #ifdef _KERNEL
320Sstevel@tonic-gate #include <sys/systm.h>
330Sstevel@tonic-gate #else
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <strings.h>
360Sstevel@tonic-gate #endif
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <sys/mdesc.h>
390Sstevel@tonic-gate #include <sys/mdesc_impl.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate static int
420Sstevel@tonic-gate mdl_scan_dag(md_impl_t *mdp,
430Sstevel@tonic-gate 	int nodeidx,
440Sstevel@tonic-gate 	mde_str_cookie_t node_cookie,
450Sstevel@tonic-gate 	mde_str_cookie_t arc_cookie,
460Sstevel@tonic-gate 	uint8_t *dseenp,
470Sstevel@tonic-gate 	int *idxp,
480Sstevel@tonic-gate 	mde_cookie_t *stashp,
490Sstevel@tonic-gate 	int level);
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 
520Sstevel@tonic-gate int
md_scan_dag(md_t * ptr,mde_cookie_t startnode,mde_str_cookie_t node_name_cookie,mde_str_cookie_t arc_name_cookie,mde_cookie_t * stashp)530Sstevel@tonic-gate md_scan_dag(md_t *ptr,
540Sstevel@tonic-gate 	mde_cookie_t startnode,
550Sstevel@tonic-gate 	mde_str_cookie_t node_name_cookie,
560Sstevel@tonic-gate 	mde_str_cookie_t arc_name_cookie,
570Sstevel@tonic-gate 	mde_cookie_t *stashp)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	int	res;
600Sstevel@tonic-gate 	int	idx;
610Sstevel@tonic-gate 	uint8_t *seenp;
620Sstevel@tonic-gate 	md_impl_t *mdp;
630Sstevel@tonic-gate 	int	start;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	mdp = (md_impl_t *)ptr;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	/*
680Sstevel@tonic-gate 	 * Possible the caller was lazy and didn't check the
690Sstevel@tonic-gate 	 * validitiy of either the node name or the arc name
700Sstevel@tonic-gate 	 * on calling ... in which case fail to find any
710Sstevel@tonic-gate 	 * nodes.
720Sstevel@tonic-gate 	 * This is distinct, from a fail (-1) since we return
730Sstevel@tonic-gate 	 * that nothing was found.
740Sstevel@tonic-gate 	 */
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	if (node_name_cookie == MDE_INVAL_STR_COOKIE ||
770Sstevel@tonic-gate 		arc_name_cookie == MDE_INVAL_STR_COOKIE) return 0;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	/*
800Sstevel@tonic-gate 	 * if we want to start at the top, start at index 0
810Sstevel@tonic-gate 	 */
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	start = (int)startnode;
840Sstevel@tonic-gate 	if (start == MDE_INVAL_ELEM_COOKIE) start = 0;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	/*
870Sstevel@tonic-gate 	 * Scan from the start point until the first node.
880Sstevel@tonic-gate 	 */
890Sstevel@tonic-gate 	while (MDE_TAG(&mdp->mdep[start]) == MDET_NULL) start++;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	/*
920Sstevel@tonic-gate 	 * This was a bogus start point if no node found
930Sstevel@tonic-gate 	 */
940Sstevel@tonic-gate 	if (MDE_TAG(&mdp->mdep[start]) != MDET_NODE) {
950Sstevel@tonic-gate 		return (-1);	/* illegal start node specified */
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	/*
990Sstevel@tonic-gate 	 * Allocate a recursion mask on the local stack fail
1000Sstevel@tonic-gate 	 * if we can't allocate the recursion detection.
1010Sstevel@tonic-gate 	 */
1020Sstevel@tonic-gate 	seenp = (uint8_t *)mdp->allocp(mdp->element_count);
1030Sstevel@tonic-gate 	if (seenp == NULL)
1040Sstevel@tonic-gate 		return (-1);
1050Sstevel@tonic-gate 	(void) memset(seenp, 0, mdp->element_count);
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	/*
1080Sstevel@tonic-gate 	 * Now build the list of requested nodes.
1090Sstevel@tonic-gate 	 */
1100Sstevel@tonic-gate 	idx = 0;
1110Sstevel@tonic-gate 	res = mdl_scan_dag(mdp, start,
1120Sstevel@tonic-gate 		node_name_cookie, arc_name_cookie,
1130Sstevel@tonic-gate 		seenp, &idx, stashp, 0);
1140Sstevel@tonic-gate 
115221Sla135387 	mdp->freep(seenp, mdp->element_count);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	return (res >= 0 ? idx : res);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 
124*1991Sheppo static int
mdl_scan_dag(md_impl_t * mdp,int nodeidx,mde_str_cookie_t node_name_cookie,mde_str_cookie_t arc_name_cookie,uint8_t * seenp,int * idxp,mde_cookie_t * stashp,int level)125*1991Sheppo mdl_scan_dag(md_impl_t *mdp,
1260Sstevel@tonic-gate 	int nodeidx,
1270Sstevel@tonic-gate 	mde_str_cookie_t node_name_cookie,
1280Sstevel@tonic-gate 	mde_str_cookie_t arc_name_cookie,
1290Sstevel@tonic-gate 	uint8_t *seenp,
1300Sstevel@tonic-gate 	int *idxp,
1310Sstevel@tonic-gate 	mde_cookie_t *stashp,
1320Sstevel@tonic-gate 	int level)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	md_element_t *mdep;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	mdep = &(mdp->mdep[nodeidx]);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	/* see if cookie is infact a node */
1390Sstevel@tonic-gate 	if (MDE_TAG(mdep) != MDET_NODE)
1400Sstevel@tonic-gate 		return (-1);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	/* have we been here before ? */
1430Sstevel@tonic-gate 	if (seenp[nodeidx])
1440Sstevel@tonic-gate 		return (0);
1450Sstevel@tonic-gate 	seenp[nodeidx] = 1;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	/* is this node of the type we seek ? */
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate #ifdef	DEBUG_LIBMDESC
1500Sstevel@tonic-gate 	{
1510Sstevel@tonic-gate 	int x;
1520Sstevel@tonic-gate 	for (x = 0; x < level; x++)
1530Sstevel@tonic-gate 		printf("-");
1540Sstevel@tonic-gate 	printf("%d (%s)\n", nodeidx, (char *)(mdp->datap + MDE_NAME(mdep)));
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate #endif
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	if (MDE_NAME(mdep) == node_name_cookie) {
1590Sstevel@tonic-gate 		/* record the node in the list and keep searching */
1600Sstevel@tonic-gate 		if (stashp != NULL) {
1610Sstevel@tonic-gate 			stashp[*idxp] = (mde_cookie_t)nodeidx;
1620Sstevel@tonic-gate 		}
1630Sstevel@tonic-gate 		(*idxp)++;
1640Sstevel@tonic-gate #ifdef	DEBUG_LIBMDESC
1650Sstevel@tonic-gate 		printf("\t* %d\n", *idxp);
1660Sstevel@tonic-gate #endif
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	/*
1700Sstevel@tonic-gate 	 * Simply walk the elements in the node.
1710Sstevel@tonic-gate 	 * if we find a matching arc, then recursively call
1720Sstevel@tonic-gate 	 * the subordinate looking for a match
1730Sstevel@tonic-gate 	 */
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	for (mdep++; MDE_TAG(mdep) != MDET_NODE_END; mdep++) {
1760Sstevel@tonic-gate 		if (MDE_TAG(mdep) == MDET_PROP_ARC &&
1770Sstevel@tonic-gate 			MDE_NAME(mdep) == arc_name_cookie) {
1780Sstevel@tonic-gate 			int res;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 			res = mdl_scan_dag(mdp,
1810Sstevel@tonic-gate 			    (int)mdep->d.prop_idx,
1820Sstevel@tonic-gate 			    node_name_cookie,
1830Sstevel@tonic-gate 			    arc_name_cookie,
1840Sstevel@tonic-gate 			    seenp, idxp, stashp, level+1);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 			if (res == -1)
1870Sstevel@tonic-gate 				return (res);
1880Sstevel@tonic-gate 		}
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	return (0);
1920Sstevel@tonic-gate }
193