Home | History | Annotate | Line # | Download | only in gen
sysctlgetmibinfo.c revision 1.9.6.1
      1  1.9.6.1    yamt /*	$NetBSD: sysctlgetmibinfo.c,v 1.9.6.1 2012/04/17 00:05:19 yamt Exp $ */
      2      1.1  atatat 
      3      1.1  atatat /*-
      4      1.1  atatat  * Copyright (c) 2003,2004 The NetBSD Foundation, Inc.
      5      1.1  atatat  *	All rights reserved.
      6      1.1  atatat  *
      7      1.1  atatat  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1  atatat  * by Andrew Brown.
      9      1.1  atatat  *
     10      1.1  atatat  * Redistribution and use in source and binary forms, with or without
     11      1.1  atatat  * modification, are permitted provided that the following conditions
     12      1.1  atatat  * are met:
     13      1.1  atatat  * 1. Redistributions of source code must retain the above copyright
     14      1.1  atatat  *    notice, this list of conditions and the following disclaimer.
     15      1.1  atatat  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  atatat  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  atatat  *    documentation and/or other materials provided with the distribution.
     18      1.1  atatat  *
     19      1.1  atatat  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1  atatat  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1  atatat  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1  atatat  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1  atatat  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1  atatat  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1  atatat  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1  atatat  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1  atatat  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1  atatat  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1  atatat  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1  atatat  */
     31      1.1  atatat 
     32      1.5   lukem #include <sys/cdefs.h>
     33      1.5   lukem #if defined(LIBC_SCCS) && !defined(lint)
     34  1.9.6.1    yamt __RCSID("$NetBSD: sysctlgetmibinfo.c,v 1.9.6.1 2012/04/17 00:05:19 yamt Exp $");
     35      1.5   lukem #endif /* LIBC_SCCS and not lint */
     36      1.5   lukem 
     37      1.8   pooka #ifndef RUMP_ACTION
     38      1.3  atatat #include "namespace.h"
     39      1.3  atatat #ifdef _REENTRANT
     40      1.3  atatat #include "reentrant.h"
     41      1.3  atatat #endif /* _REENTRANT */
     42      1.8   pooka #endif /* RUMP_ACTION */
     43      1.1  atatat #include <sys/param.h>
     44      1.1  atatat #include <sys/sysctl.h>
     45      1.1  atatat 
     46  1.9.6.1    yamt #include <assert.h>
     47      1.1  atatat #include <errno.h>
     48      1.4  kleink #include <inttypes.h>
     49      1.1  atatat #include <stdlib.h>
     50      1.1  atatat #include <string.h>
     51      1.1  atatat 
     52      1.9   pooka #ifdef RUMP_ACTION
     53      1.9   pooka #include <rump/rump_syscalls.h>
     54      1.9   pooka #define sysctl(a,b,c,d,e,f) rump_sys___sysctl(a,b,c,d,e,f)
     55      1.9   pooka #else
     56      1.3  atatat #ifdef __weak_alias
     57      1.3  atatat __weak_alias(__learn_tree,___learn_tree)
     58      1.3  atatat __weak_alias(sysctlgetmibinfo,_sysctlgetmibinfo)
     59      1.3  atatat #endif
     60      1.8   pooka #endif
     61      1.8   pooka 
     62      1.1  atatat /*
     63      1.1  atatat  * the place where we attach stuff we learn on the fly, not
     64      1.1  atatat  * necessarily used.
     65      1.1  atatat  */
     66      1.1  atatat static struct sysctlnode sysctl_mibroot = {
     67      1.1  atatat #if defined(lint)
     68      1.1  atatat 	/*
     69      1.1  atatat 	 * lint doesn't like my initializers
     70      1.1  atatat 	 */
     71      1.1  atatat 	0
     72      1.1  atatat #else /* !lint */
     73      1.1  atatat 	.sysctl_flags = SYSCTL_VERSION|CTLFLAG_ROOT|CTLTYPE_NODE,
     74      1.2      he 	sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)),
     75      1.1  atatat 	.sysctl_name = "(root)",
     76      1.1  atatat #endif /* !lint */
     77      1.1  atatat };
     78      1.1  atatat 
     79      1.1  atatat /*
     80      1.1  atatat  * routines to handle learning and cleanup
     81      1.1  atatat  */
     82      1.1  atatat static int compar(const void *, const void *);
     83      1.1  atatat static void free_children(struct sysctlnode *);
     84      1.1  atatat static void relearnhead(void);
     85      1.1  atatat 
     86      1.1  atatat /*
     87      1.1  atatat  * specifically not static since sysctl(8) "borrows" it.
     88      1.1  atatat  */
     89      1.1  atatat int __learn_tree(int *, u_int, struct sysctlnode *);
     90      1.1  atatat 
     91      1.1  atatat /*
     92      1.1  atatat  * for ordering nodes -- a query may or may not be given them in
     93      1.1  atatat  * numeric order
     94      1.1  atatat  */
     95      1.1  atatat static int
     96      1.1  atatat compar(const void *a, const void *b)
     97      1.1  atatat {
     98      1.1  atatat 
     99      1.1  atatat 	return (((const struct sysctlnode *)a)->sysctl_num -
    100      1.1  atatat 		((const struct sysctlnode *)b)->sysctl_num);
    101      1.1  atatat }
    102      1.1  atatat 
    103      1.1  atatat /*
    104      1.1  atatat  * recursively nukes a branch or an entire tree from the given node
    105      1.1  atatat  */
    106      1.1  atatat static void
    107      1.1  atatat free_children(struct sysctlnode *rnode)
    108      1.1  atatat {
    109      1.1  atatat 	struct sysctlnode *node;
    110      1.1  atatat 
    111      1.1  atatat 	if (rnode == NULL ||
    112      1.1  atatat 	    SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE ||
    113      1.1  atatat 	    rnode->sysctl_child == NULL)
    114      1.1  atatat 		return;
    115      1.1  atatat 
    116      1.1  atatat 	for (node = rnode->sysctl_child;
    117      1.1  atatat 	     node < &rnode->sysctl_child[rnode->sysctl_clen];
    118      1.1  atatat 	     node++) {
    119      1.1  atatat 		free_children(node);
    120      1.1  atatat 	}
    121      1.1  atatat 	free(rnode->sysctl_child);
    122      1.1  atatat 	rnode->sysctl_child = NULL;
    123      1.1  atatat }
    124      1.1  atatat 
    125      1.1  atatat /*
    126      1.1  atatat  * verifies that the head of the tree in the kernel is the same as the
    127      1.1  atatat  * head of the tree we already got, integrating new stuff and removing
    128      1.1  atatat  * old stuff, if it's not.
    129      1.1  atatat  */
    130      1.1  atatat static void
    131      1.1  atatat relearnhead(void)
    132      1.1  atatat {
    133      1.1  atatat 	struct sysctlnode *h, *i, *o, qnode;
    134      1.1  atatat 	size_t si, so;
    135  1.9.6.1    yamt 	int rc, name;
    136  1.9.6.1    yamt 	size_t nlen, olen, ni, oi;
    137      1.7   lukem 	uint32_t t;
    138      1.1  atatat 
    139      1.1  atatat 	/*
    140      1.1  atatat 	 * if there's nothing there, there's no need to expend any
    141      1.1  atatat 	 * effort
    142      1.1  atatat 	 */
    143      1.1  atatat 	if (sysctl_mibroot.sysctl_child == NULL)
    144      1.1  atatat 		return;
    145      1.1  atatat 
    146      1.1  atatat 	/*
    147      1.1  atatat 	 * attempt to pull out the head of the tree, starting with the
    148      1.1  atatat 	 * size we have now, and looping if we need more (or less)
    149      1.1  atatat 	 * space
    150      1.1  atatat 	 */
    151      1.1  atatat 	si = 0;
    152      1.1  atatat 	so = sysctl_mibroot.sysctl_clen * sizeof(struct sysctlnode);
    153      1.1  atatat 	name = CTL_QUERY;
    154      1.1  atatat 	memset(&qnode, 0, sizeof(qnode));
    155      1.1  atatat 	qnode.sysctl_flags = SYSCTL_VERSION;
    156      1.1  atatat 	do {
    157      1.1  atatat 		si = so;
    158      1.1  atatat 		h = malloc(si);
    159      1.1  atatat 		rc = sysctl(&name, 1, h, &so, &qnode, sizeof(qnode));
    160      1.1  atatat 		if (rc == -1 && errno != ENOMEM)
    161      1.1  atatat 			return;
    162      1.1  atatat 		if (si < so)
    163      1.1  atatat 			free(h);
    164      1.1  atatat 	} while (si < so);
    165      1.1  atatat 
    166      1.1  atatat 	/*
    167      1.1  atatat 	 * order the new copy of the head
    168      1.1  atatat 	 */
    169      1.1  atatat 	nlen = so / sizeof(struct sysctlnode);
    170  1.9.6.1    yamt 	qsort(h, nlen, sizeof(struct sysctlnode), compar);
    171      1.1  atatat 
    172      1.1  atatat 	/*
    173      1.1  atatat 	 * verify that everything is the same.  if it is, we don't
    174      1.1  atatat 	 * need to do any more work here.
    175      1.1  atatat 	 */
    176      1.1  atatat 	olen = sysctl_mibroot.sysctl_clen;
    177      1.1  atatat 	rc = (nlen == olen) ? 0 : 1;
    178      1.1  atatat 	o = sysctl_mibroot.sysctl_child;
    179      1.1  atatat 	for (ni = 0; rc == 0 && ni < nlen; ni++) {
    180      1.1  atatat 		if (h[ni].sysctl_num != o[ni].sysctl_num ||
    181      1.1  atatat 		    h[ni].sysctl_ver != o[ni].sysctl_ver)
    182      1.1  atatat 			rc = 1;
    183      1.1  atatat 	}
    184      1.1  atatat 	if (rc == 0) {
    185      1.1  atatat 		free(h);
    186      1.1  atatat 		return;
    187      1.1  atatat 	}
    188      1.1  atatat 
    189      1.1  atatat 	/*
    190      1.1  atatat 	 * something changed.  h will become the new head, and we need
    191      1.1  atatat 	 * pull over any subtrees we already have if they're the same
    192      1.1  atatat 	 * version.
    193      1.1  atatat 	 */
    194      1.1  atatat 	i = h;
    195      1.1  atatat 	ni = oi = 0;
    196      1.1  atatat 	while (ni < nlen && oi < olen) {
    197      1.1  atatat 		/*
    198      1.1  atatat 		 * something was inserted or deleted
    199      1.1  atatat 		 */
    200      1.1  atatat 		if (SYSCTL_TYPE(i[ni].sysctl_flags) == CTLTYPE_NODE)
    201      1.1  atatat 			i[ni].sysctl_child = NULL;
    202      1.1  atatat 		if (i[ni].sysctl_num != o[oi].sysctl_num) {
    203      1.1  atatat 			if (i[ni].sysctl_num < o[oi].sysctl_num) {
    204      1.1  atatat 				ni++;
    205      1.1  atatat 			}
    206      1.1  atatat 			else {
    207      1.1  atatat 				free_children(&o[oi]);
    208      1.1  atatat 				oi++;
    209      1.1  atatat 			}
    210      1.1  atatat 			continue;
    211      1.1  atatat 		}
    212      1.1  atatat 
    213      1.1  atatat 		/*
    214      1.1  atatat 		 * same number, but different version, so throw away
    215      1.1  atatat 		 * any accumulated children
    216      1.1  atatat 		 */
    217      1.1  atatat 		if (i[ni].sysctl_ver != o[oi].sysctl_ver)
    218      1.1  atatat 			free_children(&o[oi]);
    219      1.1  atatat 
    220      1.1  atatat 		/*
    221      1.1  atatat 		 * this node is the same, but we only need to
    222      1.1  atatat 		 * move subtrees.
    223      1.1  atatat 		 */
    224      1.1  atatat 		else if (SYSCTL_TYPE(i[ni].sysctl_flags) == CTLTYPE_NODE) {
    225      1.1  atatat 			/*
    226      1.1  atatat 			 * move subtree to new parent
    227      1.1  atatat 			 */
    228      1.1  atatat 			i[ni].sysctl_clen = o[oi].sysctl_clen;
    229      1.1  atatat 			i[ni].sysctl_csize = o[oi].sysctl_csize;
    230      1.1  atatat 			i[ni].sysctl_child = o[oi].sysctl_child;
    231      1.1  atatat 			/*
    232      1.1  atatat 			 * reparent inherited subtree
    233      1.1  atatat 			 */
    234      1.1  atatat 			for (t = 0;
    235      1.1  atatat 			     i[ni].sysctl_child != NULL &&
    236      1.1  atatat 				     t < i[ni].sysctl_clen;
    237      1.1  atatat 			     t++)
    238      1.1  atatat 				i[ni].sysctl_child[t].sysctl_parent = &i[ni];
    239      1.1  atatat 		}
    240      1.1  atatat 		ni++;
    241      1.1  atatat 		oi++;
    242      1.1  atatat 	}
    243      1.1  atatat 
    244      1.1  atatat 	/*
    245      1.1  atatat 	 * left over new nodes need to have empty subtrees cleared
    246      1.1  atatat 	 */
    247      1.1  atatat 	while (ni < nlen) {
    248      1.1  atatat 		if (SYSCTL_TYPE(i[ni].sysctl_flags) == CTLTYPE_NODE)
    249      1.1  atatat 			i[ni].sysctl_child = NULL;
    250      1.1  atatat 		ni++;
    251      1.1  atatat 	}
    252      1.1  atatat 
    253      1.1  atatat 	/*
    254      1.1  atatat 	 * left over old nodes need to be cleaned out
    255      1.1  atatat 	 */
    256      1.1  atatat 	while (oi < olen) {
    257      1.1  atatat 		free_children(&o[oi]);
    258      1.1  atatat 		oi++;
    259      1.1  atatat 	}
    260      1.1  atatat 
    261      1.1  atatat 	/*
    262      1.1  atatat 	 * pop new head in
    263      1.1  atatat 	 */
    264  1.9.6.1    yamt 	_DIAGASSERT(__type_fit(uint32_t, nlen));
    265  1.9.6.1    yamt 	sysctl_mibroot.sysctl_csize =
    266  1.9.6.1    yamt 	    sysctl_mibroot.sysctl_clen = (uint32_t)nlen;
    267      1.1  atatat 	sysctl_mibroot.sysctl_child = h;
    268      1.1  atatat 	free(o);
    269      1.1  atatat }
    270      1.1  atatat 
    271      1.1  atatat /*
    272      1.1  atatat  * sucks in the children at a given level and attaches it to the tree.
    273      1.1  atatat  */
    274      1.1  atatat int
    275      1.1  atatat __learn_tree(int *name, u_int namelen, struct sysctlnode *pnode)
    276      1.1  atatat {
    277      1.1  atatat 	struct sysctlnode qnode;
    278      1.7   lukem 	uint32_t rc;
    279      1.1  atatat 	size_t sz;
    280      1.1  atatat 
    281      1.1  atatat 	if (pnode == NULL)
    282      1.1  atatat 		pnode = &sysctl_mibroot;
    283      1.1  atatat 	if (SYSCTL_TYPE(pnode->sysctl_flags) != CTLTYPE_NODE) {
    284      1.1  atatat 		errno = EINVAL;
    285      1.1  atatat 		return (-1);
    286      1.1  atatat 	}
    287      1.1  atatat 	if (pnode->sysctl_child != NULL)
    288      1.1  atatat 		return (0);
    289      1.1  atatat 
    290      1.1  atatat 	if (pnode->sysctl_clen == 0)
    291      1.1  atatat 		sz = SYSCTL_DEFSIZE * sizeof(struct sysctlnode);
    292      1.1  atatat 	else
    293      1.1  atatat 		sz = pnode->sysctl_clen * sizeof(struct sysctlnode);
    294      1.1  atatat 	pnode->sysctl_child = malloc(sz);
    295      1.1  atatat 	if (pnode->sysctl_child == NULL)
    296      1.1  atatat 		return (-1);
    297      1.1  atatat 
    298      1.1  atatat 	name[namelen] = CTL_QUERY;
    299      1.1  atatat 	pnode->sysctl_clen = 0;
    300      1.1  atatat 	pnode->sysctl_csize = 0;
    301      1.1  atatat 	memset(&qnode, 0, sizeof(qnode));
    302      1.1  atatat 	qnode.sysctl_flags = SYSCTL_VERSION;
    303      1.1  atatat 	rc = sysctl(name, namelen + 1, pnode->sysctl_child, &sz,
    304      1.1  atatat 		    &qnode, sizeof(qnode));
    305      1.1  atatat 	if (sz == 0) {
    306      1.1  atatat 		free(pnode->sysctl_child);
    307      1.1  atatat 		pnode->sysctl_child = NULL;
    308      1.1  atatat 		return (rc);
    309      1.1  atatat 	}
    310      1.1  atatat 	if (rc) {
    311      1.1  atatat 		free(pnode->sysctl_child);
    312      1.1  atatat 		pnode->sysctl_child = NULL;
    313      1.1  atatat 		if ((sz % sizeof(struct sysctlnode)) != 0)
    314      1.1  atatat 			errno = EINVAL;
    315      1.1  atatat 		if (errno != ENOMEM)
    316      1.1  atatat 			return (rc);
    317      1.1  atatat 	}
    318      1.1  atatat 
    319      1.1  atatat 	if (pnode->sysctl_child == NULL) {
    320      1.1  atatat 		pnode->sysctl_child = malloc(sz);
    321      1.1  atatat 		if (pnode->sysctl_child == NULL)
    322      1.1  atatat 			return (-1);
    323      1.1  atatat 
    324      1.1  atatat 		rc = sysctl(name, namelen + 1, pnode->sysctl_child, &sz,
    325      1.1  atatat 			    &qnode, sizeof(qnode));
    326      1.1  atatat 		if (rc) {
    327      1.1  atatat 			free(pnode->sysctl_child);
    328      1.1  atatat 			pnode->sysctl_child = NULL;
    329      1.1  atatat 			return (rc);
    330      1.1  atatat 		}
    331      1.1  atatat 	}
    332      1.1  atatat 
    333      1.1  atatat 	/*
    334      1.1  atatat 	 * how many did we get?
    335      1.1  atatat 	 */
    336  1.9.6.1    yamt 	sz /= sizeof(struct sysctlnode);
    337  1.9.6.1    yamt 	pnode->sysctl_csize = pnode->sysctl_clen = (uint32_t)sz;
    338  1.9.6.1    yamt 	if (pnode->sysctl_clen != sz) {
    339      1.1  atatat 		free(pnode->sysctl_child);
    340      1.1  atatat 		pnode->sysctl_child = NULL;
    341      1.1  atatat 		errno = EINVAL;
    342      1.1  atatat 		return (-1);
    343      1.1  atatat 	}
    344      1.1  atatat 
    345      1.1  atatat 	/*
    346      1.1  atatat 	 * you know, the kernel doesn't really keep them in any
    347      1.1  atatat 	 * particular order...just like entries in a directory
    348      1.1  atatat 	 */
    349      1.1  atatat 	qsort(pnode->sysctl_child, pnode->sysctl_clen,
    350      1.1  atatat 	    sizeof(struct sysctlnode), compar);
    351      1.1  atatat 
    352      1.1  atatat 	/*
    353      1.1  atatat 	 * rearrange parent<->child linkage
    354      1.1  atatat 	 */
    355      1.1  atatat 	for (rc = 0; rc < pnode->sysctl_clen; rc++) {
    356      1.1  atatat 		pnode->sysctl_child[rc].sysctl_parent = pnode;
    357      1.1  atatat 		if (SYSCTL_TYPE(pnode->sysctl_child[rc].sysctl_flags) ==
    358      1.1  atatat 		    CTLTYPE_NODE) {
    359      1.1  atatat 			/*
    360      1.1  atatat 			 * these nodes may have children, but we
    361      1.1  atatat 			 * haven't discovered that yet.
    362      1.1  atatat 			 */
    363      1.1  atatat 			pnode->sysctl_child[rc].sysctl_child = NULL;
    364      1.1  atatat 		}
    365      1.1  atatat 		pnode->sysctl_child[rc].sysctl_desc = NULL;
    366      1.1  atatat 	}
    367      1.1  atatat 
    368      1.1  atatat 	return (0);
    369      1.1  atatat }
    370      1.1  atatat 
    371      1.1  atatat /*
    372      1.1  atatat  * that's "given name" as a string, the integer form of the name fit
    373      1.1  atatat  * to be passed to sysctl(), "canonicalized name" (optional), and a
    374      1.1  atatat  * pointer to the length of the integer form.  oh, and then a pointer
    375      1.1  atatat  * to the node, in case you (the caller) care.  you can leave them all
    376      1.1  atatat  * NULL except for gname, though that might be rather pointless,
    377      1.1  atatat  * unless all you wanna do is verify that a given name is acceptable.
    378      1.1  atatat  *
    379      1.1  atatat  * returns either 0 (everything was fine) or -1 and sets errno
    380      1.1  atatat  * accordingly.  if errno is set to EAGAIN, we detected a change to
    381      1.1  atatat  * the mib while parsing, and you should try again.  in the case of an
    382      1.1  atatat  * invalid node name, cname will be set to contain the offending name.
    383      1.1  atatat  */
    384      1.1  atatat #ifdef _REENTRANT
    385      1.1  atatat static mutex_t sysctl_mutex = MUTEX_INITIALIZER;
    386      1.1  atatat static int sysctlgetmibinfo_unlocked(const char *, int *, u_int *, char *,
    387      1.1  atatat 				     size_t *, struct sysctlnode **, int);
    388      1.1  atatat #endif /* __REENTRANT */
    389      1.1  atatat 
    390      1.1  atatat int
    391      1.1  atatat sysctlgetmibinfo(const char *gname, int *iname, u_int *namelenp,
    392      1.1  atatat 		 char *cname, size_t *csz, struct sysctlnode **rnode, int v)
    393      1.1  atatat #ifdef _REENTRANT
    394      1.1  atatat {
    395      1.1  atatat 	int rc;
    396      1.1  atatat 
    397      1.1  atatat 	mutex_lock(&sysctl_mutex);
    398      1.1  atatat 	rc = sysctlgetmibinfo_unlocked(gname, iname, namelenp, cname, csz,
    399      1.1  atatat 				       rnode, v);
    400      1.1  atatat 	mutex_unlock(&sysctl_mutex);
    401      1.1  atatat 
    402      1.1  atatat 	return (rc);
    403      1.1  atatat }
    404      1.1  atatat 
    405      1.1  atatat static int
    406      1.1  atatat sysctlgetmibinfo_unlocked(const char *gname, int *iname, u_int *namelenp,
    407      1.1  atatat 			  char *cname, size_t *csz, struct sysctlnode **rnode,
    408      1.1  atatat 			  int v)
    409      1.1  atatat #endif /* _REENTRANT */
    410      1.1  atatat {
    411      1.1  atatat 	struct sysctlnode *pnode, *node;
    412      1.7   lukem 	int name[CTL_MAXNAME], n, haven;
    413      1.7   lukem 	u_int ni, nl;
    414      1.4  kleink 	intmax_t q;
    415      1.1  atatat 	char sep[2], token[SYSCTL_NAMELEN],
    416      1.1  atatat 		pname[SYSCTL_NAMELEN * CTL_MAXNAME + CTL_MAXNAME];
    417      1.1  atatat 	const char *piece, *dot;
    418      1.1  atatat 	char *t;
    419      1.1  atatat 	size_t l;
    420      1.1  atatat 
    421      1.1  atatat 	if (rnode != NULL) {
    422      1.1  atatat 		if (*rnode == NULL) {
    423      1.1  atatat 			/* XXX later deal with dealing back a sub version */
    424      1.1  atatat 			if (v != SYSCTL_VERSION)
    425      1.1  atatat 				return (EINVAL);
    426      1.1  atatat 
    427      1.1  atatat 			pnode = &sysctl_mibroot;
    428      1.1  atatat 		}
    429      1.1  atatat 		else {
    430      1.1  atatat 			/* this is just someone being silly */
    431      1.7   lukem 			if (SYSCTL_VERS((*rnode)->sysctl_flags) != (uint32_t)v)
    432      1.1  atatat 				return (EINVAL);
    433      1.1  atatat 
    434      1.1  atatat 			/* XXX later deal with other people's trees */
    435      1.1  atatat 			if (SYSCTL_VERS((*rnode)->sysctl_flags) !=
    436      1.1  atatat 			    SYSCTL_VERSION)
    437      1.1  atatat 				return (EINVAL);
    438      1.1  atatat 
    439      1.1  atatat 			pnode = *rnode;
    440      1.1  atatat 		}
    441      1.1  atatat 	}
    442      1.1  atatat 	else
    443      1.1  atatat 		pnode = &sysctl_mibroot;
    444      1.1  atatat 
    445      1.1  atatat 	if (pnode == &sysctl_mibroot)
    446      1.1  atatat 		relearnhead();
    447      1.1  atatat 
    448      1.1  atatat 	nl = ni = 0;
    449      1.1  atatat 	token[0] = '\0';
    450      1.1  atatat 	pname[0] = '\0';
    451      1.1  atatat 	node = NULL;
    452      1.1  atatat 
    453      1.1  atatat 	/*
    454      1.1  atatat 	 * default to using '.' as the separator, but allow '/' as
    455      1.1  atatat 	 * well, and then allow a leading separator
    456      1.1  atatat 	 */
    457      1.1  atatat 	if ((dot = strpbrk(gname, "./")) == NULL)
    458      1.1  atatat 		sep[0] = '.';
    459      1.1  atatat 	else
    460      1.1  atatat 		sep[0] = dot[0];
    461      1.1  atatat 	sep[1] = '\0';
    462      1.1  atatat 	if (gname[0] == sep[0]) {
    463      1.1  atatat 		strlcat(pname, sep, sizeof(pname));
    464      1.1  atatat 		gname++;
    465      1.1  atatat 	}
    466      1.1  atatat 
    467      1.1  atatat #define COPY_OUT_DATA(t, c, cs, nlp, l) do {			\
    468      1.1  atatat 		if ((c) != NULL && (cs) != NULL)		\
    469      1.1  atatat 			*(cs) = strlcpy((c), (t), *(cs));	\
    470      1.1  atatat 		else if ((cs) != NULL)				\
    471      1.1  atatat 			*(cs) = strlen(t) + 1;			\
    472      1.1  atatat 		if ((nlp) != NULL)				\
    473      1.1  atatat 			*(nlp) = (l);				\
    474      1.1  atatat 	} while (/*CONSTCOND*/0)
    475      1.1  atatat 
    476      1.1  atatat 	piece = gname;
    477      1.1  atatat 	while (piece != NULL && *piece != '\0') {
    478      1.1  atatat 		/*
    479      1.1  atatat 		 * what was i looking for?
    480      1.1  atatat 		 */
    481      1.1  atatat 		dot = strchr(piece, sep[0]);
    482      1.1  atatat 		if (dot == NULL) {
    483      1.1  atatat 			l = strlcpy(token, piece, sizeof(token));
    484      1.1  atatat 			if (l > sizeof(token)) {
    485      1.1  atatat 				COPY_OUT_DATA(piece, cname, csz, namelenp, nl);
    486      1.1  atatat 				errno = ENAMETOOLONG;
    487      1.1  atatat 				return (-1);
    488      1.1  atatat 			}
    489      1.1  atatat 		}
    490      1.7   lukem 		else if (dot - piece > (intptr_t)(sizeof(token) - 1)) {
    491      1.1  atatat 			COPY_OUT_DATA(token, cname, csz, namelenp, nl);
    492      1.1  atatat 			errno = ENAMETOOLONG;
    493      1.1  atatat 			return (-1);
    494      1.1  atatat 		}
    495      1.1  atatat 		else {
    496      1.1  atatat 			strncpy(token, piece, (size_t)(dot - piece));
    497      1.1  atatat 			token[dot - piece] = '\0';
    498      1.1  atatat 		}
    499      1.1  atatat 
    500      1.1  atatat 		/*
    501      1.1  atatat 		 * i wonder if this "token" is an integer?
    502      1.1  atatat 		 */
    503      1.1  atatat 		errno = 0;
    504      1.4  kleink 		q = strtoimax(token, &t, 0);
    505      1.1  atatat 		n = (int)q;
    506      1.1  atatat 		if (errno != 0 || *t != '\0')
    507      1.1  atatat 			haven = 0;
    508      1.1  atatat 		else if (q < INT_MIN || q > UINT_MAX)
    509      1.1  atatat 			haven = 0;
    510      1.1  atatat 		else
    511      1.1  atatat 			haven = 1;
    512      1.1  atatat 
    513      1.1  atatat 		/*
    514      1.1  atatat 		 * make sure i have something to look at
    515      1.1  atatat 		 */
    516      1.1  atatat 		if (SYSCTL_TYPE(pnode->sysctl_flags) != CTLTYPE_NODE) {
    517      1.1  atatat 			if (haven && nl > 0) {
    518      1.1  atatat 				strlcat(pname, sep, sizeof(pname));
    519      1.1  atatat 				goto just_numbers;
    520      1.1  atatat 			}
    521      1.1  atatat 			COPY_OUT_DATA(token, cname, csz, namelenp, nl);
    522      1.1  atatat 			errno = ENOTDIR;
    523      1.1  atatat 			return (-1);
    524      1.1  atatat 		}
    525      1.1  atatat 		if (pnode->sysctl_child == NULL) {
    526      1.1  atatat 			if (__learn_tree(name, nl, pnode) == -1) {
    527      1.1  atatat 				COPY_OUT_DATA(token, cname, csz, namelenp, nl);
    528      1.1  atatat 				return (-1);
    529      1.1  atatat 			}
    530      1.1  atatat 		}
    531      1.1  atatat 		node = pnode->sysctl_child;
    532      1.1  atatat 		if (node == NULL) {
    533      1.1  atatat 			COPY_OUT_DATA(token, cname, csz, namelenp, nl);
    534      1.1  atatat 			errno = ENOENT;
    535      1.1  atatat 			return (-1);
    536      1.1  atatat 		}
    537      1.1  atatat 
    538      1.1  atatat 		/*
    539      1.1  atatat 		 * now...is it there?
    540      1.1  atatat 		 */
    541      1.1  atatat 		for (ni = 0; ni < pnode->sysctl_clen; ni++)
    542      1.1  atatat 			if ((haven && ((n == node[ni].sysctl_num) ||
    543      1.1  atatat 			    (node[ni].sysctl_flags & CTLFLAG_ANYNUMBER))) ||
    544      1.1  atatat 			    strcmp(token, node[ni].sysctl_name) == 0)
    545      1.1  atatat 				break;
    546      1.1  atatat 		if (ni >= pnode->sysctl_clen) {
    547      1.1  atatat 			COPY_OUT_DATA(token, cname, csz, namelenp, nl);
    548      1.1  atatat 			errno = ENOENT;
    549      1.1  atatat 			return (-1);
    550      1.1  atatat 		}
    551      1.1  atatat 
    552      1.1  atatat 		/*
    553      1.1  atatat 		 * ah...it is.
    554      1.1  atatat 		 */
    555      1.1  atatat 		pnode = &node[ni];
    556      1.1  atatat 		if (nl > 0)
    557      1.1  atatat 			strlcat(pname, sep, sizeof(pname));
    558      1.1  atatat 		if (haven && n != pnode->sysctl_num) {
    559      1.1  atatat  just_numbers:
    560      1.1  atatat 			strlcat(pname, token, sizeof(pname));
    561      1.1  atatat 			name[nl] = n;
    562      1.1  atatat 		}
    563      1.1  atatat 		else {
    564      1.1  atatat 			strlcat(pname, pnode->sysctl_name, sizeof(pname));
    565      1.1  atatat 			name[nl] = pnode->sysctl_num;
    566      1.1  atatat 		}
    567      1.1  atatat 		piece = (dot != NULL) ? dot + 1 : NULL;
    568      1.1  atatat 		nl++;
    569      1.1  atatat 		if (nl == CTL_MAXNAME) {
    570      1.1  atatat 			COPY_OUT_DATA(token, cname, csz, namelenp, nl);
    571      1.1  atatat 			errno = ERANGE;
    572      1.1  atatat 			return (-1);
    573      1.1  atatat 		}
    574      1.1  atatat 	}
    575      1.1  atatat 
    576      1.1  atatat 	if (nl == 0) {
    577      1.1  atatat 		if (namelenp != NULL)
    578      1.1  atatat 			*namelenp = 0;
    579      1.1  atatat 		errno = EINVAL;
    580      1.1  atatat 		return (-1);
    581      1.1  atatat 	}
    582      1.1  atatat 
    583      1.1  atatat 	COPY_OUT_DATA(pname, cname, csz, namelenp, nl);
    584      1.1  atatat 	if (iname != NULL && namelenp != NULL)
    585      1.1  atatat 		memcpy(iname, &name[0], MIN(nl, *namelenp) * sizeof(int));
    586      1.1  atatat 	if (namelenp != NULL)
    587      1.1  atatat 		*namelenp = nl;
    588      1.1  atatat 	if (rnode != NULL) {
    589      1.1  atatat 		if (*rnode != NULL)
    590      1.1  atatat 			/*
    591      1.1  atatat 			 * they gave us a private tree to work in, so
    592      1.1  atatat 			 * we give back a pointer into that private
    593      1.1  atatat 			 * tree
    594      1.1  atatat 			 */
    595      1.1  atatat 			*rnode = pnode;
    596      1.1  atatat 		else {
    597      1.1  atatat 			/*
    598      1.1  atatat 			 * they gave us a place to put the node data,
    599      1.1  atatat 			 * so give them a copy
    600      1.1  atatat 			 */
    601      1.1  atatat 			*rnode = malloc(sizeof(struct sysctlnode));
    602      1.1  atatat 			if (*rnode != NULL) {
    603      1.1  atatat 				**rnode = *pnode;
    604      1.1  atatat 				(*rnode)->sysctl_child = NULL;
    605      1.1  atatat 				(*rnode)->sysctl_parent = NULL;
    606      1.1  atatat 			}
    607      1.1  atatat 		}
    608      1.1  atatat 	}
    609      1.1  atatat 
    610      1.1  atatat 	return (0);
    611      1.1  atatat }
    612