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