kern_sysctl.c revision 1.212 1 /* $NetBSD: kern_sysctl.c,v 1.212 2008/01/02 11:48:52 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2003 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c) 1982, 1986, 1989, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * This code is derived from software contributed to Berkeley by
44 * Mike Karels at Berkeley Software Design, Inc.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)kern_sysctl.c 8.9 (Berkeley) 5/20/95
71 */
72
73 /*
74 * sysctl system call.
75 */
76
77 #include <sys/cdefs.h>
78 __KERNEL_RCSID(0, "$NetBSD: kern_sysctl.c,v 1.212 2008/01/02 11:48:52 ad Exp $");
79
80 #include "opt_defcorename.h"
81 #include "ksyms.h"
82
83 #include <sys/param.h>
84 #define __COMPAT_SYSCTL
85 #include <sys/sysctl.h>
86 #include <sys/systm.h>
87 #include <sys/buf.h>
88 #include <sys/ksyms.h>
89 #include <sys/malloc.h>
90 #include <sys/mount.h>
91 #include <sys/syscallargs.h>
92 #include <sys/kauth.h>
93 #include <sys/ktrace.h>
94 #include <machine/stdarg.h>
95
96 #define MAXDESCLEN 1024
97 MALLOC_DEFINE(M_SYSCTLNODE, "sysctlnode", "sysctl node structures");
98 MALLOC_DEFINE(M_SYSCTLDATA, "sysctldata", "misc sysctl data");
99
100 static int sysctl_mmap(SYSCTLFN_PROTO);
101 static int sysctl_alloc(struct sysctlnode *, int);
102 static int sysctl_realloc(struct sysctlnode *);
103
104 static int sysctl_cvt_in(struct lwp *, int *, const void *, size_t,
105 struct sysctlnode *);
106 static int sysctl_cvt_out(struct lwp *, int, const struct sysctlnode *,
107 void *, size_t, size_t *);
108
109 static int sysctl_log_add(struct sysctllog **, const struct sysctlnode *);
110 static int sysctl_log_realloc(struct sysctllog *);
111
112 struct sysctllog {
113 const struct sysctlnode *log_root;
114 int *log_num;
115 int log_size, log_left;
116 };
117
118 /*
119 * the "root" of the new sysctl tree
120 */
121 struct sysctlnode sysctl_root = {
122 .sysctl_flags = SYSCTL_VERSION|
123 CTLFLAG_ROOT|CTLFLAG_READWRITE|
124 CTLTYPE_NODE,
125 .sysctl_num = 0,
126 /*
127 * XXX once all ports are on gcc3, we can get rid of this
128 * ugliness and simply make it into
129 *
130 * .sysctl_size = sizeof(struct sysctlnode),
131 */
132 sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)),
133 .sysctl_name = "(root)",
134 };
135
136 /*
137 * link set of functions that add nodes at boot time (see also
138 * sysctl_buildtree())
139 */
140 __link_set_decl(sysctl_funcs, sysctl_setup_func);
141
142 /*
143 * The `sysctl_lock' is intended to serialize access to the sysctl
144 * tree. Given that it is now (a) dynamic, and (b) most consumers of
145 * sysctl are going to be copying data out, the old `sysctl_memlock'
146 * has been `upgraded' to simply guard the whole tree.
147 *
148 * The two new data here are to keep track of the locked chunk of
149 * memory, if there is one, so that it can be released more easily
150 * from anywhere.
151 */
152 krwlock_t sysctl_treelock;
153 void *sysctl_memaddr;
154 size_t sysctl_memsize;
155
156 /*
157 * Attributes stored in the kernel.
158 */
159 char hostname[MAXHOSTNAMELEN];
160 int hostnamelen;
161
162 char domainname[MAXHOSTNAMELEN];
163 int domainnamelen;
164
165 long hostid;
166
167 #ifndef DEFCORENAME
168 #define DEFCORENAME "%n.core"
169 #endif
170 char defcorename[MAXPATHLEN] = DEFCORENAME;
171
172 /*
173 * ********************************************************************
174 * Section 0: Some simple glue
175 * ********************************************************************
176 * By wrapping copyin(), copyout(), and copyinstr() like this, we can
177 * stop caring about who's calling us and simplify some code a bunch.
178 * ********************************************************************
179 */
180 static inline int
181 sysctl_copyin(struct lwp *l, const void *uaddr, void *kaddr, size_t len)
182 {
183 int error;
184
185 if (l != NULL) {
186 error = copyin(uaddr, kaddr, len);
187 ktrmibio(-1, UIO_WRITE, uaddr, len, error);
188 } else {
189 error = kcopy(uaddr, kaddr, len);
190 }
191
192 return error;
193 }
194
195 static inline int
196 sysctl_copyout(struct lwp *l, const void *kaddr, void *uaddr, size_t len)
197 {
198 int error;
199
200 if (l != NULL) {
201 error = copyout(kaddr, uaddr, len);
202 ktrmibio(-1, UIO_READ, uaddr, len, error);
203 } else {
204 error = kcopy(kaddr, uaddr, len);
205 }
206
207 return error;
208 }
209
210 static inline int
211 sysctl_copyinstr(struct lwp *l, const void *uaddr, void *kaddr,
212 size_t len, size_t *done)
213 {
214 int error;
215
216 if (l != NULL) {
217 error = copyinstr(uaddr, kaddr, len, done);
218 ktrmibio(-1, UIO_WRITE, uaddr, len, error);
219 } else {
220 error = copystr(uaddr, kaddr, len, done);
221 }
222
223 return error;
224 }
225
226 /*
227 * ********************************************************************
228 * Initialize sysctl subsystem.
229 * ********************************************************************
230 */
231 void
232 sysctl_init(void)
233 {
234 sysctl_setup_func * const *sysctl_setup, f;
235
236 rw_init(&sysctl_treelock);
237
238 /*
239 * dynamic mib numbers start here
240 */
241 sysctl_root.sysctl_num = CREATE_BASE;
242
243 __link_set_foreach(sysctl_setup, sysctl_funcs) {
244 /*
245 * XXX - why do i have to coerce the pointers like this?
246 */
247 f = (void*)*sysctl_setup;
248 (*f)(NULL);
249 }
250
251 /*
252 * setting this means no more permanent nodes can be added,
253 * trees that claim to be readonly at the root now are, and if
254 * the main tree is readonly, *everything* is.
255 */
256 sysctl_root.sysctl_flags |= CTLFLAG_PERMANENT;
257
258 }
259
260 /*
261 * ********************************************************************
262 * The main native sysctl system call itself.
263 * ********************************************************************
264 */
265 int
266 sys___sysctl(struct lwp *l, const struct sys___sysctl_args *uap, register_t *retval)
267 {
268 /* {
269 syscallarg(const int *) name;
270 syscallarg(u_int) namelen;
271 syscallarg(void *) old;
272 syscallarg(size_t *) oldlenp;
273 syscallarg(const void *) new;
274 syscallarg(size_t) newlen;
275 } */
276 int error, nerror, name[CTL_MAXNAME];
277 size_t oldlen, savelen, *oldlenp;
278
279 /*
280 * get oldlen
281 */
282 oldlen = 0;
283 oldlenp = SCARG(uap, oldlenp);
284 if (oldlenp != NULL) {
285 error = copyin(oldlenp, &oldlen, sizeof(oldlen));
286 if (error)
287 return (error);
288 }
289 savelen = oldlen;
290
291 /*
292 * top-level sysctl names may or may not be non-terminal, but
293 * we don't care
294 */
295 if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1)
296 return (EINVAL);
297 error = copyin(SCARG(uap, name), &name,
298 SCARG(uap, namelen) * sizeof(int));
299 if (error)
300 return (error);
301
302 ktrmib(name, SCARG(uap, namelen));
303
304 /*
305 * wire old so that copyout() is less likely to fail?
306 */
307 KERNEL_LOCK(1, NULL); /* XXXSMP */
308 error = sysctl_lock(l, SCARG(uap, old), savelen);
309 if (error) {
310 KERNEL_UNLOCK_ONE(NULL); /* XXXSMP */
311 return (error);
312 }
313
314 /*
315 * do sysctl work (NULL means main built-in default tree)
316 */
317 error = sysctl_dispatch(&name[0], SCARG(uap, namelen),
318 SCARG(uap, old), &oldlen,
319 SCARG(uap, new), SCARG(uap, newlen),
320 &name[0], l, NULL);
321
322 /*
323 * release the sysctl lock
324 */
325 sysctl_unlock(l);
326 KERNEL_UNLOCK_ONE(NULL); /* XXXSMP */
327
328 /*
329 * set caller's oldlen to new value even in the face of an
330 * error (if this gets an error and they didn't have one, they
331 * get this one)
332 */
333 if (oldlenp) {
334 nerror = copyout(&oldlen, oldlenp, sizeof(oldlen));
335 if (error == 0)
336 error = nerror;
337 }
338
339 /*
340 * if the only problem is that we weren't given enough space,
341 * that's an ENOMEM error
342 */
343 if (error == 0 && SCARG(uap, old) != NULL && savelen < oldlen)
344 error = ENOMEM;
345
346 return (error);
347 }
348
349 /*
350 * ********************************************************************
351 * Section 1: How the tree is used
352 * ********************************************************************
353 * Implementations of sysctl for emulations should typically need only
354 * these three functions in this order: lock the tree, dispatch
355 * request into it, unlock the tree.
356 * ********************************************************************
357 */
358 int
359 sysctl_lock(struct lwp *l, void *oldp, size_t savelen)
360 {
361 int error;
362
363 rw_enter(&sysctl_treelock, RW_WRITER); /* XXX write */
364
365 if (l != NULL && oldp != NULL && savelen) {
366
367 /*
368 * be lazy - memory is locked for short time only, so
369 * just do a basic check against system limit
370 */
371
372 if (uvmexp.wired + atop(savelen) > uvmexp.wiredmax) {
373 rw_exit(&sysctl_treelock);
374 return (ENOMEM);
375 }
376 error = uvm_vslock(l->l_proc->p_vmspace, oldp, savelen,
377 VM_PROT_WRITE);
378 if (error) {
379 rw_exit(&sysctl_treelock);
380 return (error);
381 }
382 sysctl_memaddr = oldp;
383 sysctl_memsize = savelen;
384 }
385
386 return (0);
387 }
388
389 /*
390 * ********************************************************************
391 * the main sysctl dispatch routine. scans the given tree and picks a
392 * function to call based on what it finds.
393 * ********************************************************************
394 */
395 int
396 sysctl_dispatch(SYSCTLFN_ARGS)
397 {
398 int error;
399 sysctlfn fn;
400 int ni;
401
402 if (rnode && SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
403 printf("sysctl_dispatch: rnode %p wrong version\n", rnode);
404 error = EINVAL;
405 goto out;
406 }
407
408 fn = NULL;
409 error = sysctl_locate(l, name, namelen, &rnode, &ni);
410
411 if (rnode->sysctl_func != NULL) {
412 /*
413 * the node we ended up at has a function, so call it. it can
414 * hand off to query or create if it wants to.
415 */
416 fn = rnode->sysctl_func;
417 } else if (error == 0) {
418 /*
419 * we found the node they were looking for, so do a lookup.
420 */
421 fn = (sysctlfn)sysctl_lookup; /* XXX may write to rnode */
422 } else if (error == ENOENT && (ni + 1) == namelen && name[ni] < 0) {
423 /*
424 * prospective parent node found, but the terminal node was
425 * not. generic operations associate with the parent.
426 */
427 switch (name[ni]) {
428 case CTL_QUERY:
429 fn = sysctl_query;
430 break;
431 case CTL_CREATE:
432 #if NKSYMS > 0
433 case CTL_CREATESYM:
434 #endif /* NKSYMS > 0 */
435 fn = (sysctlfn)sysctl_create; /* we own the rnode */
436 break;
437 case CTL_DESTROY:
438 fn = (sysctlfn)sysctl_destroy; /* we own the rnode */
439 break;
440 case CTL_MMAP:
441 fn = (sysctlfn)sysctl_mmap; /* we own the rnode */
442 break;
443 case CTL_DESCRIBE:
444 fn = sysctl_describe;
445 break;
446 default:
447 error = EOPNOTSUPP;
448 break;
449 }
450 }
451
452 /*
453 * after all of that, maybe we found someone who knows how to
454 * get us what we want?
455 */
456 if (fn != NULL)
457 error = (*fn)(name + ni, namelen - ni, oldp, oldlenp,
458 newp, newlen, name, l, rnode);
459 else if (error == 0)
460 error = EOPNOTSUPP;
461
462 out:
463 return (error);
464 }
465
466 /*
467 * ********************************************************************
468 * Releases the tree lock. Note that if uvm_vslock() was called when
469 * the lock was taken, we release that memory now. By keeping track
470 * of where and how much by ourselves, the lock can be released much
471 * more easily from anywhere.
472 * ********************************************************************
473 */
474 void
475 sysctl_unlock(struct lwp *l)
476 {
477
478 if (l != NULL && sysctl_memsize != 0) {
479 uvm_vsunlock(l->l_proc->p_vmspace, sysctl_memaddr,
480 sysctl_memsize);
481 sysctl_memsize = 0;
482 }
483
484 rw_exit(&sysctl_treelock); /* XXX write */
485 }
486
487 /*
488 * ********************************************************************
489 * Section 2: The main tree interfaces
490 * ********************************************************************
491 * This is how sysctl_dispatch() does its work, and you can too, by
492 * calling these routines from helpers (though typically only
493 * sysctl_lookup() will be used). The tree MUST BE LOCKED when these
494 * are called.
495 * ********************************************************************
496 */
497
498 /*
499 * sysctl_locate -- Finds the node matching the given mib under the
500 * given tree (via rv). If no tree is given, we fall back to the
501 * native tree. The current process (via l) is used for access
502 * control on the tree (some nodes may be traversable only by root) and
503 * on return, nip will show how many numbers in the mib were consumed.
504 */
505 int
506 sysctl_locate(struct lwp *l, const int *name, u_int namelen,
507 const struct sysctlnode **rnode, int *nip)
508 {
509 const struct sysctlnode *node, *pnode;
510 int tn, si, ni, error, alias;
511
512 /*
513 * basic checks and setup
514 */
515 if (*rnode == NULL)
516 *rnode = &sysctl_root;
517 if (nip)
518 *nip = 0;
519 if (namelen == 0)
520 return (0);
521
522 /*
523 * search starts from "root"
524 */
525 pnode = *rnode;
526 if (SYSCTL_VERS(pnode->sysctl_flags) != SYSCTL_VERSION) {
527 printf("sysctl_locate: pnode %p wrong version\n", pnode);
528 return (EINVAL);
529 }
530 node = pnode->sysctl_child;
531 error = 0;
532
533 /*
534 * scan for node to which new node should be attached
535 */
536 for (ni = 0; ni < namelen; ni++) {
537 /*
538 * walked off bottom of tree
539 */
540 if (node == NULL) {
541 if (SYSCTL_TYPE(pnode->sysctl_flags) == CTLTYPE_NODE)
542 error = ENOENT;
543 else
544 error = ENOTDIR;
545 break;
546 }
547 /*
548 * can anyone traverse this node or only root?
549 */
550 if (l != NULL && (pnode->sysctl_flags & CTLFLAG_PRIVATE) &&
551 (error = kauth_authorize_system(l->l_cred,
552 KAUTH_SYSTEM_SYSCTL, KAUTH_REQ_SYSTEM_SYSCTL_PRVT,
553 NULL, NULL, NULL)) != 0)
554 return (error);
555 /*
556 * find a child node with the right number
557 */
558 tn = name[ni];
559 alias = 0;
560
561 si = 0;
562 /*
563 * Note: ANYNUMBER only matches positive integers.
564 * Since ANYNUMBER is only permitted on single-node
565 * sub-trees (eg proc), check before the loop and skip
566 * it if we can.
567 */
568 if ((node[si].sysctl_flags & CTLFLAG_ANYNUMBER) && (tn >= 0))
569 goto foundit;
570 for (; si < pnode->sysctl_clen; si++) {
571 if (node[si].sysctl_num == tn) {
572 if (node[si].sysctl_flags & CTLFLAG_ALIAS) {
573 if (alias++ == 4)
574 break;
575 else {
576 tn = node[si].sysctl_alias;
577 si = -1;
578 }
579 } else
580 goto foundit;
581 }
582 }
583 /*
584 * if we ran off the end, it obviously doesn't exist
585 */
586 error = ENOENT;
587 break;
588
589 /*
590 * so far so good, move on down the line
591 */
592 foundit:
593 pnode = &node[si];
594 if (SYSCTL_TYPE(pnode->sysctl_flags) == CTLTYPE_NODE)
595 node = node[si].sysctl_child;
596 else
597 node = NULL;
598 }
599
600 *rnode = pnode;
601 if (nip)
602 *nip = ni;
603
604 return (error);
605 }
606
607 /*
608 * sysctl_query -- The auto-discovery engine. Copies out the structs
609 * describing nodes under the given node and handles overlay trees.
610 */
611 int
612 sysctl_query(SYSCTLFN_ARGS)
613 {
614 int error, ni, elim, v;
615 size_t out, left, t;
616 const struct sysctlnode *enode, *onode;
617 struct sysctlnode qnode;
618
619 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
620 printf("sysctl_query: rnode %p wrong version\n", rnode);
621 return (EINVAL);
622 }
623
624 if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE)
625 return (ENOTDIR);
626 if (namelen != 1 || name[0] != CTL_QUERY)
627 return (EINVAL);
628
629 error = 0;
630 out = 0;
631 left = *oldlenp;
632 elim = 0;
633 enode = NULL;
634
635 /*
636 * translate the given request to a current node
637 */
638 error = sysctl_cvt_in(l, &v, newp, newlen, &qnode);
639 if (error)
640 return (error);
641
642 /*
643 * if the request specifies a version, check it
644 */
645 if (qnode.sysctl_ver != 0) {
646 enode = rnode;
647 if (qnode.sysctl_ver != enode->sysctl_ver &&
648 qnode.sysctl_ver != sysctl_rootof(enode)->sysctl_ver)
649 return (EINVAL);
650 }
651
652 /*
653 * process has overlay tree
654 */
655 if (l && l->l_proc->p_emul->e_sysctlovly) {
656 enode = l->l_proc->p_emul->e_sysctlovly;
657 elim = (name - oname);
658 error = sysctl_locate(l, oname, elim, &enode, NULL);
659 if (error == 0) {
660 /* ah, found parent in overlay */
661 elim = enode->sysctl_clen;
662 enode = enode->sysctl_child;
663 } else {
664 error = 0;
665 elim = 0;
666 enode = NULL;
667 }
668 }
669
670 for (ni = 0; ni < rnode->sysctl_clen; ni++) {
671 onode = &rnode->sysctl_child[ni];
672 if (enode && enode->sysctl_num == onode->sysctl_num) {
673 if (SYSCTL_TYPE(enode->sysctl_flags) != CTLTYPE_NODE)
674 onode = enode;
675 if (--elim > 0)
676 enode++;
677 else
678 enode = NULL;
679 }
680 error = sysctl_cvt_out(l, v, onode, oldp, left, &t);
681 if (error)
682 return (error);
683 if (oldp != NULL)
684 oldp = (char*)oldp + t;
685 out += t;
686 left -= MIN(left, t);
687 }
688
689 /*
690 * overlay trees *MUST* be entirely consumed
691 */
692 KASSERT(enode == NULL);
693
694 *oldlenp = out;
695
696 return (error);
697 }
698
699 /*
700 * sysctl_create -- Adds a node (the description of which is taken
701 * from newp) to the tree, returning a copy of it in the space pointed
702 * to by oldp. In the event that the requested slot is already taken
703 * (either by name or by number), the offending node is returned
704 * instead. Yes, this is complex, but we want to make sure everything
705 * is proper.
706 */
707 #ifdef SYSCTL_DEBUG_CREATE
708 int _sysctl_create(SYSCTLFN_ARGS);
709 int
710 _sysctl_create(SYSCTLFN_ARGS)
711 #else
712 int
713 sysctl_create(SYSCTLFN_ARGS)
714 #endif
715 {
716 struct sysctlnode nnode, *node, *pnode;
717 int error, ni, at, nm, type, sz, flags, anum, v;
718 void *own;
719
720 error = 0;
721 own = NULL;
722 anum = -1;
723
724 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
725 printf("sysctl_create: rnode %p wrong version\n", rnode);
726 return (EINVAL);
727 }
728
729 if (namelen != 1 || (name[namelen - 1] != CTL_CREATE
730 #if NKSYMS > 0
731 && name[namelen - 1] != CTL_CREATESYM
732 #endif /* NKSYMS > 0 */
733 ))
734 return (EINVAL);
735
736 /*
737 * processes can only add nodes at securelevel 0, must be
738 * root, and can't add nodes to a parent that's not writeable
739 */
740 if (l != NULL) {
741 #ifndef SYSCTL_DISALLOW_CREATE
742 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL,
743 KAUTH_REQ_SYSTEM_SYSCTL_ADD, NULL, NULL, NULL);
744 if (error)
745 return (error);
746 if (!(rnode->sysctl_flags & CTLFLAG_READWRITE))
747 #endif /* SYSCTL_DISALLOW_CREATE */
748 return (EPERM);
749 }
750
751 /*
752 * nothing can add a node if:
753 * we've finished initial set up and
754 * the tree itself is not writeable or
755 * the entire sysctl system is not writeable
756 */
757 if ((sysctl_root.sysctl_flags & CTLFLAG_PERMANENT) &&
758 (!(sysctl_rootof(rnode)->sysctl_flags & CTLFLAG_READWRITE) ||
759 !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE)))
760 return (EPERM);
761
762 /*
763 * it must be a "node", not a "int" or something
764 */
765 if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE)
766 return (ENOTDIR);
767 if (rnode->sysctl_flags & CTLFLAG_ALIAS) {
768 printf("sysctl_create: attempt to add node to aliased "
769 "node %p\n", rnode);
770 return (EINVAL);
771 }
772 pnode = __UNCONST(rnode); /* we are adding children to this node */
773
774 if (newp == NULL)
775 return (EINVAL);
776 error = sysctl_cvt_in(l, &v, newp, newlen, &nnode);
777 if (error)
778 return (error);
779
780 /*
781 * nodes passed in don't *have* parents
782 */
783 if (nnode.sysctl_parent != NULL)
784 return (EINVAL);
785
786 /*
787 * if we are indeed adding it, it should be a "good" name and
788 * number
789 */
790 nm = nnode.sysctl_num;
791 #if NKSYMS > 0
792 if (nm == CTL_CREATESYM)
793 nm = CTL_CREATE;
794 #endif /* NKSYMS > 0 */
795 if (nm < 0 && nm != CTL_CREATE)
796 return (EINVAL);
797 sz = 0;
798
799 /*
800 * the name can't start with a digit
801 */
802 if (nnode.sysctl_name[sz] >= '0' &&
803 nnode.sysctl_name[sz] <= '9')
804 return (EINVAL);
805
806 /*
807 * the name must be only alphanumerics or - or _, longer than
808 * 0 bytes and less that SYSCTL_NAMELEN
809 */
810 while (sz < SYSCTL_NAMELEN && nnode.sysctl_name[sz] != '\0') {
811 if ((nnode.sysctl_name[sz] >= '0' &&
812 nnode.sysctl_name[sz] <= '9') ||
813 (nnode.sysctl_name[sz] >= 'A' &&
814 nnode.sysctl_name[sz] <= 'Z') ||
815 (nnode.sysctl_name[sz] >= 'a' &&
816 nnode.sysctl_name[sz] <= 'z') ||
817 nnode.sysctl_name[sz] == '-' ||
818 nnode.sysctl_name[sz] == '_')
819 sz++;
820 else
821 return (EINVAL);
822 }
823 if (sz == 0 || sz == SYSCTL_NAMELEN)
824 return (EINVAL);
825
826 /*
827 * various checks revolve around size vs type, etc
828 */
829 type = SYSCTL_TYPE(nnode.sysctl_flags);
830 flags = SYSCTL_FLAGS(nnode.sysctl_flags);
831 sz = nnode.sysctl_size;
832
833 /*
834 * find out if there's a collision, and if so, let the caller
835 * know what they collided with
836 */
837 node = pnode->sysctl_child;
838 at = 0;
839 if (node) {
840 if ((flags | node->sysctl_flags) & CTLFLAG_ANYNUMBER)
841 /* No siblings for a CTLFLAG_ANYNUMBER node */
842 return EINVAL;
843 for (ni = 0; ni < pnode->sysctl_clen; ni++) {
844 if (nm == node[ni].sysctl_num ||
845 strcmp(nnode.sysctl_name, node[ni].sysctl_name) == 0) {
846 /*
847 * ignore error here, since we
848 * are already fixed on EEXIST
849 */
850 (void)sysctl_cvt_out(l, v, &node[ni], oldp,
851 *oldlenp, oldlenp);
852 return (EEXIST);
853 }
854 if (nm > node[ni].sysctl_num)
855 at++;
856 }
857 }
858
859 /*
860 * use sysctl_ver to add to the tree iff it hasn't changed
861 */
862 if (nnode.sysctl_ver != 0) {
863 /*
864 * a specified value must match either the parent
865 * node's version or the root node's version
866 */
867 if (nnode.sysctl_ver != sysctl_rootof(rnode)->sysctl_ver &&
868 nnode.sysctl_ver != rnode->sysctl_ver) {
869 return (EINVAL);
870 }
871 }
872
873 /*
874 * only the kernel can assign functions to entries
875 */
876 if (l != NULL && nnode.sysctl_func != NULL)
877 return (EPERM);
878
879 /*
880 * only the kernel can create permanent entries, and only then
881 * before the kernel is finished setting itself up
882 */
883 if (l != NULL && (flags & ~SYSCTL_USERFLAGS))
884 return (EPERM);
885 if ((flags & CTLFLAG_PERMANENT) &
886 (sysctl_root.sysctl_flags & CTLFLAG_PERMANENT))
887 return (EPERM);
888 if ((flags & (CTLFLAG_OWNDATA | CTLFLAG_IMMEDIATE)) ==
889 (CTLFLAG_OWNDATA | CTLFLAG_IMMEDIATE))
890 return (EINVAL);
891 if ((flags & CTLFLAG_IMMEDIATE) &&
892 type != CTLTYPE_INT && type != CTLTYPE_QUAD)
893 return (EINVAL);
894
895 /*
896 * check size, or set it if unset and we can figure it out.
897 * kernel created nodes are allowed to have a function instead
898 * of a size (or a data pointer).
899 */
900 switch (type) {
901 case CTLTYPE_NODE:
902 /*
903 * only *i* can assert the size of a node
904 */
905 if (flags & CTLFLAG_ALIAS) {
906 anum = nnode.sysctl_alias;
907 if (anum < 0)
908 return (EINVAL);
909 nnode.sysctl_alias = 0;
910 }
911 if (sz != 0 || nnode.sysctl_data != NULL)
912 return (EINVAL);
913 if (nnode.sysctl_csize != 0 ||
914 nnode.sysctl_clen != 0 ||
915 nnode.sysctl_child != 0)
916 return (EINVAL);
917 if (flags & CTLFLAG_OWNDATA)
918 return (EINVAL);
919 sz = sizeof(struct sysctlnode);
920 break;
921 case CTLTYPE_INT:
922 /*
923 * since an int is an int, if the size is not given or
924 * is wrong, we can "int-uit" it.
925 */
926 if (sz != 0 && sz != sizeof(int))
927 return (EINVAL);
928 sz = sizeof(int);
929 break;
930 case CTLTYPE_STRING:
931 /*
932 * strings are a little more tricky
933 */
934 if (sz == 0) {
935 if (l == NULL) {
936 if (nnode.sysctl_func == NULL) {
937 if (nnode.sysctl_data == NULL)
938 return (EINVAL);
939 else
940 sz = strlen(nnode.sysctl_data) +
941 1;
942 }
943 } else if (nnode.sysctl_data == NULL &&
944 flags & CTLFLAG_OWNDATA) {
945 return (EINVAL);
946 } else {
947 char *vp, *e;
948 size_t s;
949
950 /*
951 * we want a rough idea of what the
952 * size is now
953 */
954 vp = malloc(PAGE_SIZE, M_SYSCTLDATA,
955 M_WAITOK|M_CANFAIL);
956 if (vp == NULL)
957 return (ENOMEM);
958 e = nnode.sysctl_data;
959 do {
960 error = copyinstr(e, vp, PAGE_SIZE, &s);
961 if (error) {
962 if (error != ENAMETOOLONG) {
963 free(vp, M_SYSCTLDATA);
964 return (error);
965 }
966 e += PAGE_SIZE;
967 if ((e - 32 * PAGE_SIZE) >
968 (char*)nnode.sysctl_data) {
969 free(vp, M_SYSCTLDATA);
970 return (ERANGE);
971 }
972 }
973 } while (error != 0);
974 sz = s + (e - (char*)nnode.sysctl_data);
975 free(vp, M_SYSCTLDATA);
976 }
977 }
978 break;
979 case CTLTYPE_QUAD:
980 if (sz != 0 && sz != sizeof(u_quad_t))
981 return (EINVAL);
982 sz = sizeof(u_quad_t);
983 break;
984 case CTLTYPE_STRUCT:
985 if (sz == 0) {
986 if (l != NULL || nnode.sysctl_func == NULL)
987 return (EINVAL);
988 if (flags & CTLFLAG_OWNDATA)
989 return (EINVAL);
990 }
991 break;
992 default:
993 return (EINVAL);
994 }
995
996 /*
997 * at this point, if sz is zero, we *must* have a
998 * function to go with it and we can't own it.
999 */
1000
1001 /*
1002 * l ptr own
1003 * 0 0 0 -> EINVAL (if no func)
1004 * 0 0 1 -> own
1005 * 0 1 0 -> kptr
1006 * 0 1 1 -> kptr
1007 * 1 0 0 -> EINVAL
1008 * 1 0 1 -> own
1009 * 1 1 0 -> kptr, no own (fault on lookup)
1010 * 1 1 1 -> uptr, own
1011 */
1012 if (type != CTLTYPE_NODE) {
1013 if (sz != 0) {
1014 if (flags & CTLFLAG_OWNDATA) {
1015 own = malloc(sz, M_SYSCTLDATA,
1016 M_WAITOK|M_CANFAIL);
1017 if (own == NULL)
1018 return ENOMEM;
1019 if (nnode.sysctl_data == NULL)
1020 memset(own, 0, sz);
1021 else {
1022 error = sysctl_copyin(l,
1023 nnode.sysctl_data, own, sz);
1024 if (error != 0) {
1025 free(own, M_SYSCTLDATA);
1026 return (error);
1027 }
1028 }
1029 } else if ((nnode.sysctl_data != NULL) &&
1030 !(flags & CTLFLAG_IMMEDIATE)) {
1031 #if NKSYMS > 0
1032 if (name[namelen - 1] == CTL_CREATESYM) {
1033 char symname[128]; /* XXX enough? */
1034 u_long symaddr;
1035 size_t symlen;
1036
1037 error = sysctl_copyinstr(l,
1038 nnode.sysctl_data, symname,
1039 sizeof(symname), &symlen);
1040 if (error)
1041 return (error);
1042 error = ksyms_getval(NULL, symname,
1043 &symaddr, KSYMS_EXTERN);
1044 if (error)
1045 return (error); /* EINVAL? */
1046 nnode.sysctl_data = (void*)symaddr;
1047 }
1048 #endif /* NKSYMS > 0 */
1049 /*
1050 * Ideally, we'd like to verify here
1051 * that this address is acceptable,
1052 * but...
1053 *
1054 * - it might be valid now, only to
1055 * become invalid later
1056 *
1057 * - it might be invalid only for the
1058 * moment and valid later
1059 *
1060 * - or something else.
1061 *
1062 * Since we can't get a good answer,
1063 * we'll just accept the address as
1064 * given, and fault on individual
1065 * lookups.
1066 */
1067 }
1068 } else if (nnode.sysctl_func == NULL)
1069 return (EINVAL);
1070 }
1071
1072 /*
1073 * a process can't assign a function to a node, and the kernel
1074 * can't create a node that has no function or data.
1075 * (XXX somewhat redundant check)
1076 */
1077 if (l != NULL || nnode.sysctl_func == NULL) {
1078 if (type != CTLTYPE_NODE &&
1079 nnode.sysctl_data == NULL &&
1080 !(flags & CTLFLAG_IMMEDIATE) &&
1081 own == NULL)
1082 return (EINVAL);
1083 }
1084
1085 #ifdef SYSCTL_DISALLOW_KWRITE
1086 /*
1087 * a process can't create a writable node unless it refers to
1088 * new data.
1089 */
1090 if (l != NULL && own == NULL && type != CTLTYPE_NODE &&
1091 (flags & CTLFLAG_READWRITE) != CTLFLAG_READONLY &&
1092 !(flags & CTLFLAG_IMMEDIATE))
1093 return (EPERM);
1094 #endif /* SYSCTL_DISALLOW_KWRITE */
1095
1096 /*
1097 * make sure there's somewhere to put the new stuff.
1098 */
1099 if (pnode->sysctl_child == NULL) {
1100 if (flags & CTLFLAG_ANYNUMBER)
1101 error = sysctl_alloc(pnode, 1);
1102 else
1103 error = sysctl_alloc(pnode, 0);
1104 if (error) {
1105 if (own != NULL)
1106 free(own, M_SYSCTLDATA);
1107 return (error);
1108 }
1109 }
1110 node = pnode->sysctl_child;
1111
1112 /*
1113 * no collisions, so pick a good dynamic number if we need to.
1114 */
1115 if (nm == CTL_CREATE) {
1116 nm = ++sysctl_root.sysctl_num;
1117 for (ni = 0; ni < pnode->sysctl_clen; ni++) {
1118 if (nm == node[ni].sysctl_num) {
1119 nm++;
1120 ni = -1;
1121 } else if (nm > node[ni].sysctl_num)
1122 at = ni + 1;
1123 }
1124 }
1125
1126 /*
1127 * oops...ran out of space
1128 */
1129 if (pnode->sysctl_clen == pnode->sysctl_csize) {
1130 error = sysctl_realloc(pnode);
1131 if (error) {
1132 if (own != NULL)
1133 free(own, M_SYSCTLDATA);
1134 return (error);
1135 }
1136 node = pnode->sysctl_child;
1137 }
1138
1139 /*
1140 * insert new node data
1141 */
1142 if (at < pnode->sysctl_clen) {
1143 int t;
1144
1145 /*
1146 * move the nodes that should come after the new one
1147 */
1148 memmove(&node[at + 1], &node[at],
1149 (pnode->sysctl_clen - at) * sizeof(struct sysctlnode));
1150 memset(&node[at], 0, sizeof(struct sysctlnode));
1151 node[at].sysctl_parent = pnode;
1152 /*
1153 * and...reparent any children of any moved nodes
1154 */
1155 for (ni = at; ni <= pnode->sysctl_clen; ni++)
1156 if (SYSCTL_TYPE(node[ni].sysctl_flags) == CTLTYPE_NODE)
1157 for (t = 0; t < node[ni].sysctl_clen; t++)
1158 node[ni].sysctl_child[t].sysctl_parent =
1159 &node[ni];
1160 }
1161 node = &node[at];
1162 pnode->sysctl_clen++;
1163
1164 strlcpy(node->sysctl_name, nnode.sysctl_name,
1165 sizeof(node->sysctl_name));
1166 node->sysctl_num = nm;
1167 node->sysctl_size = sz;
1168 node->sysctl_flags = SYSCTL_VERSION|type|flags; /* XXX other trees */
1169 node->sysctl_csize = 0;
1170 node->sysctl_clen = 0;
1171 if (own) {
1172 node->sysctl_data = own;
1173 node->sysctl_flags |= CTLFLAG_OWNDATA;
1174 } else if (flags & CTLFLAG_ALIAS) {
1175 node->sysctl_alias = anum;
1176 } else if (flags & CTLFLAG_IMMEDIATE) {
1177 switch (type) {
1178 case CTLTYPE_INT:
1179 node->sysctl_idata = nnode.sysctl_idata;
1180 break;
1181 case CTLTYPE_QUAD:
1182 node->sysctl_qdata = nnode.sysctl_qdata;
1183 break;
1184 }
1185 } else {
1186 node->sysctl_data = nnode.sysctl_data;
1187 node->sysctl_flags &= ~CTLFLAG_OWNDATA;
1188 }
1189 node->sysctl_func = nnode.sysctl_func;
1190 node->sysctl_child = NULL;
1191 /* node->sysctl_parent should already be done */
1192
1193 /*
1194 * update "version" on path to "root"
1195 */
1196 for (; rnode->sysctl_parent != NULL; rnode = rnode->sysctl_parent)
1197 ;
1198 pnode = node;
1199 for (nm = rnode->sysctl_ver + 1; pnode != NULL;
1200 pnode = pnode->sysctl_parent)
1201 pnode->sysctl_ver = nm;
1202
1203 /* If this fails, the node is already added - the user won't know! */
1204 error = sysctl_cvt_out(l, v, node, oldp, *oldlenp, oldlenp);
1205
1206 return (error);
1207 }
1208
1209 /*
1210 * ********************************************************************
1211 * A wrapper around sysctl_create() that prints the thing we're trying
1212 * to add.
1213 * ********************************************************************
1214 */
1215 #ifdef SYSCTL_DEBUG_CREATE
1216 int
1217 sysctl_create(SYSCTLFN_ARGS)
1218 {
1219 const struct sysctlnode *node;
1220 int k, rc, ni, nl = namelen + (name - oname);
1221
1222 node = newp;
1223
1224 printf("namelen %d (", nl);
1225 for (ni = 0; ni < nl - 1; ni++)
1226 printf(" %d", oname[ni]);
1227 printf(" %d )\t[%s]\tflags %08x (%08x %d %zu)\n",
1228 k = node->sysctl_num,
1229 node->sysctl_name,
1230 node->sysctl_flags,
1231 SYSCTL_FLAGS(node->sysctl_flags),
1232 SYSCTL_TYPE(node->sysctl_flags),
1233 node->sysctl_size);
1234
1235 node = rnode;
1236 rc = _sysctl_create(SYSCTLFN_CALL(rnode));
1237
1238 printf("sysctl_create(");
1239 for (ni = 0; ni < nl - 1; ni++)
1240 printf(" %d", oname[ni]);
1241 printf(" %d ) returned %d\n", k, rc);
1242
1243 return (rc);
1244 }
1245 #endif /* SYSCTL_DEBUG_CREATE */
1246
1247 /*
1248 * sysctl_destroy -- Removes a node (as described by newp) from the
1249 * given tree, returning (if successful) a copy of the dead node in
1250 * oldp. Since we're removing stuff, there's not much to check.
1251 */
1252 int
1253 sysctl_destroy(SYSCTLFN_ARGS)
1254 {
1255 struct sysctlnode *node, *pnode, onode, nnode;
1256 int ni, error, v;
1257
1258 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
1259 printf("sysctl_destroy: rnode %p wrong version\n", rnode);
1260 return (EINVAL);
1261 }
1262
1263 error = 0;
1264
1265 if (namelen != 1 || name[namelen - 1] != CTL_DESTROY)
1266 return (EINVAL);
1267
1268 /*
1269 * processes can only destroy nodes at securelevel 0, must be
1270 * root, and can't remove nodes from a parent that's not
1271 * writeable
1272 */
1273 if (l != NULL) {
1274 #ifndef SYSCTL_DISALLOW_CREATE
1275 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL,
1276 KAUTH_REQ_SYSTEM_SYSCTL_DELETE, NULL, NULL, NULL);
1277 if (error)
1278 return (error);
1279 if (!(rnode->sysctl_flags & CTLFLAG_READWRITE))
1280 #endif /* SYSCTL_DISALLOW_CREATE */
1281 return (EPERM);
1282 }
1283
1284 /*
1285 * nothing can remove a node if:
1286 * the node is permanent (checked later) or
1287 * the tree itself is not writeable or
1288 * the entire sysctl system is not writeable
1289 *
1290 * note that we ignore whether setup is complete or not,
1291 * because these rules always apply.
1292 */
1293 if (!(sysctl_rootof(rnode)->sysctl_flags & CTLFLAG_READWRITE) ||
1294 !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE))
1295 return (EPERM);
1296
1297 if (newp == NULL)
1298 return (EINVAL);
1299 error = sysctl_cvt_in(l, &v, newp, newlen, &nnode);
1300 if (error)
1301 return (error);
1302 memset(&onode, 0, sizeof(struct sysctlnode));
1303
1304 node = rnode->sysctl_child;
1305 for (ni = 0; ni < rnode->sysctl_clen; ni++) {
1306 if (nnode.sysctl_num == node[ni].sysctl_num) {
1307 /*
1308 * if name specified, must match
1309 */
1310 if (nnode.sysctl_name[0] != '\0' &&
1311 strcmp(nnode.sysctl_name, node[ni].sysctl_name))
1312 continue;
1313 /*
1314 * if version specified, must match
1315 */
1316 if (nnode.sysctl_ver != 0 &&
1317 nnode.sysctl_ver != node[ni].sysctl_ver)
1318 continue;
1319 /*
1320 * this must be the one
1321 */
1322 break;
1323 }
1324 }
1325 if (ni == rnode->sysctl_clen)
1326 return (ENOENT);
1327 node = &node[ni];
1328 pnode = node->sysctl_parent;
1329
1330 /*
1331 * if the kernel says permanent, it is, so there. nyah.
1332 */
1333 if (SYSCTL_FLAGS(node->sysctl_flags) & CTLFLAG_PERMANENT)
1334 return (EPERM);
1335
1336 /*
1337 * can't delete non-empty nodes
1338 */
1339 if (SYSCTL_TYPE(node->sysctl_flags) == CTLTYPE_NODE &&
1340 node->sysctl_clen != 0)
1341 return (ENOTEMPTY);
1342
1343 /*
1344 * if the node "owns" data, release it now
1345 */
1346 if (node->sysctl_flags & CTLFLAG_OWNDATA) {
1347 if (node->sysctl_data != NULL)
1348 free(node->sysctl_data, M_SYSCTLDATA);
1349 node->sysctl_data = NULL;
1350 }
1351 if (node->sysctl_flags & CTLFLAG_OWNDESC) {
1352 if (node->sysctl_desc != NULL)
1353 /*XXXUNCONST*/
1354 free(__UNCONST(node->sysctl_desc), M_SYSCTLDATA);
1355 node->sysctl_desc = NULL;
1356 }
1357
1358 /*
1359 * if the node to be removed is not the last one on the list,
1360 * move the remaining nodes up, and reparent any grandchildren
1361 */
1362 onode = *node;
1363 if (ni < pnode->sysctl_clen - 1) {
1364 int t;
1365
1366 memmove(&pnode->sysctl_child[ni], &pnode->sysctl_child[ni + 1],
1367 (pnode->sysctl_clen - ni - 1) *
1368 sizeof(struct sysctlnode));
1369 for (; ni < pnode->sysctl_clen - 1; ni++)
1370 if (SYSCTL_TYPE(pnode->sysctl_child[ni].sysctl_flags) ==
1371 CTLTYPE_NODE)
1372 for (t = 0;
1373 t < pnode->sysctl_child[ni].sysctl_clen;
1374 t++)
1375 pnode->sysctl_child[ni].sysctl_child[t].
1376 sysctl_parent =
1377 &pnode->sysctl_child[ni];
1378 ni = pnode->sysctl_clen - 1;
1379 node = &pnode->sysctl_child[ni];
1380 }
1381
1382 /*
1383 * reset the space we just vacated
1384 */
1385 memset(node, 0, sizeof(struct sysctlnode));
1386 node->sysctl_parent = pnode;
1387 pnode->sysctl_clen--;
1388
1389 /*
1390 * if this parent just lost its last child, nuke the creche
1391 */
1392 if (pnode->sysctl_clen == 0) {
1393 free(pnode->sysctl_child, M_SYSCTLNODE);
1394 pnode->sysctl_csize = 0;
1395 pnode->sysctl_child = NULL;
1396 }
1397
1398 /*
1399 * update "version" on path to "root"
1400 */
1401 for (; rnode->sysctl_parent != NULL; rnode = rnode->sysctl_parent)
1402 ;
1403 for (ni = rnode->sysctl_ver + 1; pnode != NULL;
1404 pnode = pnode->sysctl_parent)
1405 pnode->sysctl_ver = ni;
1406
1407 error = sysctl_cvt_out(l, v, &onode, oldp, *oldlenp, oldlenp);
1408
1409 return (error);
1410 }
1411
1412 /*
1413 * sysctl_lookup -- Handles copyin/copyout of new and old values.
1414 * Partial reads are globally allowed. Only root can write to things
1415 * unless the node says otherwise.
1416 */
1417 int
1418 sysctl_lookup(SYSCTLFN_ARGS)
1419 {
1420 int error, rw;
1421 size_t sz, len;
1422 void *d;
1423
1424 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
1425 printf("sysctl_lookup: rnode %p wrong version\n", rnode);
1426 return (EINVAL);
1427 }
1428
1429 error = 0;
1430
1431 /*
1432 * you can't "look up" a node. you can "query" it, but you
1433 * can't "look it up".
1434 */
1435 if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_NODE || namelen != 0)
1436 return (EINVAL);
1437
1438 /*
1439 * some nodes are private, so only root can look into them.
1440 */
1441 if (l != NULL && (rnode->sysctl_flags & CTLFLAG_PRIVATE) &&
1442 (error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL,
1443 KAUTH_REQ_SYSTEM_SYSCTL_PRVT, NULL, NULL, NULL)) != 0)
1444 return (error);
1445
1446 /*
1447 * if a node wants to be writable according to different rules
1448 * other than "only root can write to stuff unless a flag is
1449 * set", then it needs its own function which should have been
1450 * called and not us.
1451 */
1452 if (l != NULL && newp != NULL &&
1453 !(rnode->sysctl_flags & CTLFLAG_ANYWRITE) &&
1454 (error = kauth_authorize_generic(l->l_cred,
1455 KAUTH_GENERIC_ISSUSER, NULL)) != 0)
1456 return (error);
1457
1458 /*
1459 * is this node supposedly writable?
1460 */
1461 rw = (rnode->sysctl_flags & CTLFLAG_READWRITE) ? 1 : 0;
1462
1463 /*
1464 * it appears not to be writable at this time, so if someone
1465 * tried to write to it, we must tell them to go away
1466 */
1467 if (!rw && newp != NULL)
1468 return (EPERM);
1469
1470 /*
1471 * step one, copy out the stuff we have presently
1472 */
1473 if (rnode->sysctl_flags & CTLFLAG_IMMEDIATE) {
1474 /*
1475 * note that we discard const here because we are
1476 * modifying the contents of the node (which is okay
1477 * because it's ours)
1478 */
1479 switch (SYSCTL_TYPE(rnode->sysctl_flags)) {
1480 case CTLTYPE_INT:
1481 d = __UNCONST(&rnode->sysctl_idata);
1482 break;
1483 case CTLTYPE_QUAD:
1484 d = __UNCONST(&rnode->sysctl_qdata);
1485 break;
1486 default:
1487 return (EINVAL);
1488 }
1489 } else
1490 d = rnode->sysctl_data;
1491 if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_STRING)
1492 sz = strlen(d) + 1; /* XXX@@@ possible fault here */
1493 else
1494 sz = rnode->sysctl_size;
1495 if (oldp != NULL)
1496 error = sysctl_copyout(l, d, oldp, MIN(sz, *oldlenp));
1497 if (error)
1498 return (error);
1499 *oldlenp = sz;
1500
1501 /*
1502 * are we done?
1503 */
1504 if (newp == NULL || newlen == 0)
1505 return (0);
1506
1507 /*
1508 * hmm...not done. must now "copy in" new value. re-adjust
1509 * sz to maximum value (strings are "weird").
1510 */
1511 sz = rnode->sysctl_size;
1512 switch (SYSCTL_TYPE(rnode->sysctl_flags)) {
1513 case CTLTYPE_INT:
1514 case CTLTYPE_QUAD:
1515 case CTLTYPE_STRUCT:
1516 /*
1517 * these data must be *exactly* the same size coming
1518 * in.
1519 */
1520 if (newlen != sz)
1521 return (EINVAL);
1522 error = sysctl_copyin(l, newp, d, sz);
1523 break;
1524 case CTLTYPE_STRING: {
1525 /*
1526 * strings, on the other hand, can be shorter, and we
1527 * let userland be sloppy about the trailing nul.
1528 */
1529 char *newbuf;
1530
1531 /*
1532 * too much new string?
1533 */
1534 if (newlen > sz)
1535 return (EINVAL);
1536
1537 /*
1538 * temporary copy of new inbound string
1539 */
1540 len = MIN(sz, newlen);
1541 newbuf = malloc(len, M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
1542 if (newbuf == NULL)
1543 return (ENOMEM);
1544 error = sysctl_copyin(l, newp, newbuf, len);
1545 if (error) {
1546 free(newbuf, M_SYSCTLDATA);
1547 return (error);
1548 }
1549
1550 /*
1551 * did they null terminate it, or do we have space
1552 * left to do it ourselves?
1553 */
1554 if (newbuf[len - 1] != '\0' && len == sz) {
1555 free(newbuf, M_SYSCTLDATA);
1556 return (EINVAL);
1557 }
1558
1559 /*
1560 * looks good, so pop it into place and zero the rest.
1561 */
1562 if (len > 0)
1563 memcpy(d, newbuf, len);
1564 if (sz != len)
1565 memset((char*)d + len, 0, sz - len);
1566 free(newbuf, M_SYSCTLDATA);
1567 break;
1568 }
1569 default:
1570 return (EINVAL);
1571 }
1572
1573 return (error);
1574 }
1575
1576 /*
1577 * sysctl_mmap -- Dispatches sysctl mmap requests to those nodes that
1578 * purport to handle it. This interface isn't fully fleshed out yet,
1579 * unfortunately.
1580 */
1581 static int
1582 sysctl_mmap(SYSCTLFN_ARGS)
1583 {
1584 const struct sysctlnode *node;
1585 struct sysctlnode nnode;
1586 int error;
1587
1588 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
1589 printf("sysctl_mmap: rnode %p wrong version\n", rnode);
1590 return (EINVAL);
1591 }
1592
1593 /*
1594 * let's just pretend that didn't happen, m'kay?
1595 */
1596 if (l == NULL)
1597 return (EPERM);
1598
1599 /*
1600 * is this a sysctlnode description of an mmap request?
1601 */
1602 if (newp == NULL || newlen != sizeof(struct sysctlnode))
1603 return (EINVAL);
1604 error = sysctl_copyin(l, newp, &nnode, sizeof(nnode));
1605 if (error)
1606 return (error);
1607
1608 /*
1609 * does the node they asked for exist?
1610 */
1611 if (namelen != 1)
1612 return (EOPNOTSUPP);
1613 node = rnode;
1614 error = sysctl_locate(l, &nnode.sysctl_num, 1, &node, NULL);
1615 if (error)
1616 return (error);
1617
1618 /*
1619 * does this node that we have found purport to handle mmap?
1620 */
1621 if (node->sysctl_func == NULL ||
1622 !(node->sysctl_flags & CTLFLAG_MMAP))
1623 return (EOPNOTSUPP);
1624
1625 /*
1626 * well...okay, they asked for it.
1627 */
1628 return ((*node->sysctl_func)(SYSCTLFN_CALL(node)));
1629 }
1630
1631 int
1632 sysctl_describe(SYSCTLFN_ARGS)
1633 {
1634 struct sysctldesc *d;
1635 void *bf;
1636 size_t sz, left, tot;
1637 int i, error, v = -1;
1638 struct sysctlnode *node;
1639 struct sysctlnode dnode;
1640
1641 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
1642 printf("sysctl_query: rnode %p wrong version\n", rnode);
1643 return (EINVAL);
1644 }
1645
1646 if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE)
1647 return (ENOTDIR);
1648 if (namelen != 1 || name[0] != CTL_DESCRIBE)
1649 return (EINVAL);
1650
1651 /*
1652 * get ready...
1653 */
1654 error = 0;
1655 d = bf = malloc(MAXDESCLEN, M_TEMP, M_WAITOK|M_CANFAIL);
1656 if (bf == NULL)
1657 return ENOMEM;
1658 tot = 0;
1659 node = rnode->sysctl_child;
1660 left = *oldlenp;
1661
1662 /*
1663 * no request -> all descriptions at this level
1664 * request with desc unset -> just this node
1665 * request with desc set -> set descr for this node
1666 */
1667 if (newp != NULL) {
1668 error = sysctl_cvt_in(l, &v, newp, newlen, &dnode);
1669 if (error)
1670 goto out;
1671 if (dnode.sysctl_desc != NULL) {
1672 /*
1673 * processes cannot set descriptions above
1674 * securelevel 0. and must be root. blah
1675 * blah blah. a couple more checks are made
1676 * once we find the node we want.
1677 */
1678 if (l != NULL) {
1679 #ifndef SYSCTL_DISALLOW_CREATE
1680 error = kauth_authorize_system(l->l_cred,
1681 KAUTH_SYSTEM_SYSCTL,
1682 KAUTH_REQ_SYSTEM_SYSCTL_DESC, NULL,
1683 NULL, NULL);
1684 if (error)
1685 goto out;
1686 #else /* SYSCTL_DISALLOW_CREATE */
1687 error = EPERM;
1688 goto out;
1689 #endif /* SYSCTL_DISALLOW_CREATE */
1690 }
1691
1692 /*
1693 * find node and try to set the description on it
1694 */
1695 for (i = 0; i < rnode->sysctl_clen; i++)
1696 if (node[i].sysctl_num == dnode.sysctl_num)
1697 break;
1698 if (i == rnode->sysctl_clen) {
1699 error = ENOENT;
1700 goto out;
1701 }
1702 node = &node[i];
1703
1704 /*
1705 * did the caller specify a node version?
1706 */
1707 if (dnode.sysctl_ver != 0 &&
1708 dnode.sysctl_ver != node->sysctl_ver) {
1709 error = EINVAL;
1710 goto out;
1711 }
1712
1713 /*
1714 * okay...some rules:
1715 * (1) if setup is done and the tree is
1716 * read-only or the whole system is
1717 * read-only
1718 * (2) no one can set a description on a
1719 * permanent node (it must be set when
1720 * using createv)
1721 * (3) processes cannot *change* a description
1722 * (4) processes *can*, however, set a
1723 * description on a read-only node so that
1724 * one can be created and then described
1725 * in two steps
1726 * anything else come to mind?
1727 */
1728 if ((sysctl_root.sysctl_flags & CTLFLAG_PERMANENT) &&
1729 (!(sysctl_rootof(node)->sysctl_flags &
1730 CTLFLAG_READWRITE) ||
1731 !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE))) {
1732 error = EPERM;
1733 goto out;
1734 }
1735 if (node->sysctl_flags & CTLFLAG_PERMANENT) {
1736 error = EPERM;
1737 goto out;
1738 }
1739 if (l != NULL && node->sysctl_desc != NULL) {
1740 error = EPERM;
1741 goto out;
1742 }
1743
1744 /*
1745 * right, let's go ahead. the first step is
1746 * making the description into something the
1747 * node can "own", if need be.
1748 */
1749 if (l != NULL ||
1750 dnode.sysctl_flags & CTLFLAG_OWNDESC) {
1751 char *nd, *k;
1752
1753 k = malloc(MAXDESCLEN, M_TEMP,
1754 M_WAITOK|M_CANFAIL);
1755 if (k == NULL) {
1756 error = ENOMEM;
1757 goto out;
1758 }
1759 error = sysctl_copyinstr(l, dnode.sysctl_desc,
1760 k, MAXDESCLEN, &sz);
1761 if (error) {
1762 free(k, M_TEMP);
1763 goto out;
1764 }
1765 nd = malloc(sz, M_SYSCTLDATA,
1766 M_WAITOK|M_CANFAIL);
1767 if (nd == NULL) {
1768 free(k, M_TEMP);
1769 error = ENOMEM;
1770 goto out;
1771 }
1772 memcpy(nd, k, sz);
1773 dnode.sysctl_flags |= CTLFLAG_OWNDESC;
1774 dnode.sysctl_desc = nd;
1775 free(k, M_TEMP);
1776 }
1777
1778 /*
1779 * now "release" the old description and
1780 * attach the new one. ta-da.
1781 */
1782 if ((node->sysctl_flags & CTLFLAG_OWNDESC) &&
1783 node->sysctl_desc != NULL)
1784 /*XXXUNCONST*/
1785 free(__UNCONST(node->sysctl_desc), M_SYSCTLDATA);
1786 node->sysctl_desc = dnode.sysctl_desc;
1787 node->sysctl_flags |=
1788 (dnode.sysctl_flags & CTLFLAG_OWNDESC);
1789
1790 /*
1791 * now we "fall out" and into the loop which
1792 * will copy the new description back out for
1793 * those interested parties
1794 */
1795 }
1796 }
1797
1798 /*
1799 * scan for one description or just retrieve all descriptions
1800 */
1801 for (i = 0; i < rnode->sysctl_clen; i++) {
1802 /*
1803 * did they ask for the description of only one node?
1804 */
1805 if (v != -1 && node[i].sysctl_num != dnode.sysctl_num)
1806 continue;
1807
1808 /*
1809 * don't describe "private" nodes to non-suser users
1810 */
1811 if ((node[i].sysctl_flags & CTLFLAG_PRIVATE) && (l != NULL) &&
1812 !(kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL,
1813 KAUTH_REQ_SYSTEM_SYSCTL_PRVT, NULL, NULL, NULL)))
1814 continue;
1815
1816 /*
1817 * is this description "valid"?
1818 */
1819 memset(bf, 0, MAXDESCLEN);
1820 if (node[i].sysctl_desc == NULL)
1821 sz = 1;
1822 else if (copystr(node[i].sysctl_desc, &d->descr_str[0],
1823 MAXDESCLEN - sizeof(*d), &sz) != 0) {
1824 /*
1825 * erase possible partial description
1826 */
1827 memset(bf, 0, MAXDESCLEN);
1828 sz = 1;
1829 }
1830
1831 /*
1832 * we've got it, stuff it into the caller's buffer
1833 */
1834 d->descr_num = node[i].sysctl_num;
1835 d->descr_ver = node[i].sysctl_ver;
1836 d->descr_len = sz; /* includes trailing nul */
1837 sz = (char *)NEXT_DESCR(d) - (char *)d;
1838 if (oldp != NULL && left >= sz) {
1839 error = sysctl_copyout(l, d, oldp, sz);
1840 if (error)
1841 goto out;
1842 left -= sz;
1843 oldp = (void *)__sysc_desc_adv(oldp, d->descr_len);
1844 }
1845 tot += sz;
1846
1847 /*
1848 * if we get this far with v not "unset", they asked
1849 * for a specific node and we found it
1850 */
1851 if (v != -1)
1852 break;
1853 }
1854
1855 /*
1856 * did we find it after all?
1857 */
1858 if (v != -1 && tot == 0)
1859 error = ENOENT;
1860 else
1861 *oldlenp = tot;
1862
1863 out:
1864 free(bf, M_TEMP);
1865 return (error);
1866 }
1867
1868 /*
1869 * ********************************************************************
1870 * Section 3: Create and destroy from inside the kernel
1871 * ********************************************************************
1872 * sysctl_createv() and sysctl_destroyv() are simpler-to-use
1873 * interfaces for the kernel to fling new entries into the mib and rip
1874 * them out later. In the case of sysctl_createv(), the returned copy
1875 * of the node (see sysctl_create()) will be translated back into a
1876 * pointer to the actual node.
1877 *
1878 * Note that sysctl_createv() will return 0 if the create request
1879 * matches an existing node (ala mkdir -p), and that sysctl_destroyv()
1880 * will return 0 if the node to be destroyed already does not exist
1881 * (aka rm -f) or if it is a parent of other nodes.
1882 *
1883 * This allows two (or more) different subsystems to assert sub-tree
1884 * existence before populating their own nodes, and to remove their
1885 * own nodes without orphaning the others when they are done.
1886 * ********************************************************************
1887 */
1888 int
1889 sysctl_createv(struct sysctllog **log, int cflags,
1890 const struct sysctlnode **rnode, const struct sysctlnode **cnode,
1891 int flags, int type, const char *namep, const char *descr,
1892 sysctlfn func, u_quad_t qv, void *newp, size_t newlen,
1893 ...)
1894 {
1895 va_list ap;
1896 int error, ni, namelen, name[CTL_MAXNAME];
1897 const struct sysctlnode *root, *pnode;
1898 struct sysctlnode nnode, onode, *dnode;
1899 size_t sz;
1900
1901 /*
1902 * where are we putting this?
1903 */
1904 if (rnode != NULL && *rnode == NULL) {
1905 printf("sysctl_createv: rnode NULL\n");
1906 return (EINVAL);
1907 }
1908 root = rnode ? *rnode : NULL;
1909 if (cnode != NULL)
1910 *cnode = NULL;
1911 if (cflags != 0)
1912 return (EINVAL);
1913
1914 /*
1915 * what is it?
1916 */
1917 flags = SYSCTL_VERSION|SYSCTL_TYPE(type)|SYSCTL_FLAGS(flags);
1918 if (log != NULL)
1919 flags &= ~CTLFLAG_PERMANENT;
1920
1921 /*
1922 * where do we put it?
1923 */
1924 va_start(ap, newlen);
1925 namelen = 0;
1926 ni = -1;
1927 do {
1928 if (++ni == CTL_MAXNAME)
1929 return (ENAMETOOLONG);
1930 name[ni] = va_arg(ap, int);
1931 /*
1932 * sorry, this is not supported from here
1933 */
1934 if (name[ni] == CTL_CREATESYM)
1935 return (EINVAL);
1936 } while (name[ni] != CTL_EOL && name[ni] != CTL_CREATE);
1937 namelen = ni + (name[ni] == CTL_CREATE ? 1 : 0);
1938 va_end(ap);
1939
1940 /*
1941 * what's it called
1942 */
1943 if (strlcpy(nnode.sysctl_name, namep, sizeof(nnode.sysctl_name)) >=
1944 sizeof(nnode.sysctl_name))
1945 return (ENAMETOOLONG);
1946
1947 /*
1948 * cons up the description of the new node
1949 */
1950 nnode.sysctl_num = name[namelen - 1];
1951 name[namelen - 1] = CTL_CREATE;
1952 nnode.sysctl_size = newlen;
1953 nnode.sysctl_flags = flags;
1954 if (type == CTLTYPE_NODE) {
1955 nnode.sysctl_csize = 0;
1956 nnode.sysctl_clen = 0;
1957 nnode.sysctl_child = NULL;
1958 if (flags & CTLFLAG_ALIAS)
1959 nnode.sysctl_alias = qv;
1960 } else if (flags & CTLFLAG_IMMEDIATE) {
1961 switch (type) {
1962 case CTLTYPE_INT:
1963 nnode.sysctl_idata = qv;
1964 break;
1965 case CTLTYPE_QUAD:
1966 nnode.sysctl_qdata = qv;
1967 break;
1968 default:
1969 return (EINVAL);
1970 }
1971 } else {
1972 nnode.sysctl_data = newp;
1973 }
1974 nnode.sysctl_func = func;
1975 nnode.sysctl_parent = NULL;
1976 nnode.sysctl_ver = 0;
1977
1978 /*
1979 * initialize lock state -- we need locks if the main tree has
1980 * been marked as complete, but since we could be called from
1981 * either there, or from a device driver (say, at device
1982 * insertion), or from an lkm (at lkm load time, say), we
1983 * don't really want to "wait"...
1984 */
1985 error = sysctl_lock(NULL, NULL, 0);
1986 if (error)
1987 return (error);
1988
1989 /*
1990 * locate the prospective parent of the new node, and if we
1991 * find it, add the new node.
1992 */
1993 sz = sizeof(onode);
1994 pnode = root;
1995 error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni);
1996 if (error) {
1997 printf("sysctl_createv: sysctl_locate(%s) returned %d\n",
1998 nnode.sysctl_name, error);
1999 sysctl_unlock(NULL);
2000 return (error);
2001 }
2002 error = sysctl_create(&name[ni], namelen - ni, &onode, &sz,
2003 &nnode, sizeof(nnode), &name[0], NULL,
2004 pnode);
2005
2006 /*
2007 * unfortunately the node we wanted to create is already
2008 * there. if the node that's already there is a reasonable
2009 * facsimile of the node we wanted to create, just pretend
2010 * (for the caller's benefit) that we managed to create the
2011 * node they wanted.
2012 */
2013 if (error == EEXIST) {
2014 /* name is the same as requested... */
2015 if (strcmp(nnode.sysctl_name, onode.sysctl_name) == 0 &&
2016 /* they want the same function... */
2017 nnode.sysctl_func == onode.sysctl_func &&
2018 /* number is the same as requested, or... */
2019 (nnode.sysctl_num == onode.sysctl_num ||
2020 /* they didn't pick a number... */
2021 nnode.sysctl_num == CTL_CREATE)) {
2022 /*
2023 * collision here from trying to create
2024 * something that already existed; let's give
2025 * our customers a hand and tell them they got
2026 * what they wanted.
2027 */
2028 #ifdef SYSCTL_DEBUG_CREATE
2029 printf("cleared\n");
2030 #endif /* SYSCTL_DEBUG_CREATE */
2031 error = 0;
2032 }
2033 }
2034
2035 if (error == 0 &&
2036 (cnode != NULL || log != NULL || descr != NULL)) {
2037 /*
2038 * sysctl_create() gave us back a copy of the node,
2039 * but we need to know where it actually is...
2040 */
2041 pnode = root;
2042 error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni);
2043
2044 /*
2045 * manual scan of last layer so that aliased nodes
2046 * aren't followed.
2047 */
2048 if (error == 0) {
2049 for (ni = 0; ni < pnode->sysctl_clen; ni++)
2050 if (pnode->sysctl_child[ni].sysctl_num ==
2051 onode.sysctl_num)
2052 break;
2053 if (ni < pnode->sysctl_clen)
2054 pnode = &pnode->sysctl_child[ni];
2055 else
2056 error = ENOENT;
2057 }
2058
2059 /*
2060 * not expecting an error here, but...
2061 */
2062 if (error == 0) {
2063 if (log != NULL)
2064 sysctl_log_add(log, pnode);
2065 if (cnode != NULL)
2066 *cnode = pnode;
2067 if (descr != NULL) {
2068 /*
2069 * allow first caller to *set* a
2070 * description actually to set it
2071 *
2072 * discard const here so we can attach
2073 * the description
2074 */
2075 dnode = __UNCONST(pnode);
2076 if (pnode->sysctl_desc != NULL)
2077 /* skip it...we've got one */;
2078 else if (flags & CTLFLAG_OWNDESC) {
2079 size_t l = strlen(descr) + 1;
2080 char *d = malloc(l, M_SYSCTLDATA,
2081 M_WAITOK|M_CANFAIL);
2082 if (d != NULL) {
2083 memcpy(d, descr, l);
2084 dnode->sysctl_desc = d;
2085 dnode->sysctl_flags |=
2086 CTLFLAG_OWNDESC;
2087 }
2088 } else
2089 dnode->sysctl_desc = descr;
2090 }
2091 } else {
2092 printf("sysctl_create succeeded but node not found?!\n");
2093 /*
2094 * confusing, but the create said it
2095 * succeeded, so...
2096 */
2097 error = 0;
2098 }
2099 }
2100
2101 /*
2102 * now it should be safe to release the lock state. note that
2103 * the pointer to the newly created node being passed back may
2104 * not be "good" for very long.
2105 */
2106 sysctl_unlock(NULL);
2107
2108 if (error != 0) {
2109 printf("sysctl_createv: sysctl_create(%s) returned %d\n",
2110 nnode.sysctl_name, error);
2111 #if 0
2112 if (error != ENOENT)
2113 sysctl_dump(&onode);
2114 #endif
2115 }
2116
2117 return (error);
2118 }
2119
2120 int
2121 sysctl_destroyv(struct sysctlnode *rnode, ...)
2122 {
2123 va_list ap;
2124 int error, name[CTL_MAXNAME], namelen, ni;
2125 const struct sysctlnode *pnode, *node;
2126 struct sysctlnode dnode, *onode;
2127 size_t sz;
2128
2129 va_start(ap, rnode);
2130 namelen = 0;
2131 ni = 0;
2132 do {
2133 if (ni == CTL_MAXNAME)
2134 return (ENAMETOOLONG);
2135 name[ni] = va_arg(ap, int);
2136 } while (name[ni++] != CTL_EOL);
2137 namelen = ni - 1;
2138 va_end(ap);
2139
2140 /*
2141 * i can't imagine why we'd be destroying a node when the tree
2142 * wasn't complete, but who knows?
2143 */
2144 error = sysctl_lock(NULL, NULL, 0);
2145 if (error)
2146 return (error);
2147
2148 /*
2149 * where is it?
2150 */
2151 node = rnode;
2152 error = sysctl_locate(NULL, &name[0], namelen - 1, &node, &ni);
2153 if (error) {
2154 /* they want it gone and it's not there, so... */
2155 sysctl_unlock(NULL);
2156 return (error == ENOENT ? 0 : error);
2157 }
2158
2159 /*
2160 * set up the deletion
2161 */
2162 pnode = node;
2163 node = &dnode;
2164 memset(&dnode, 0, sizeof(dnode));
2165 dnode.sysctl_flags = SYSCTL_VERSION;
2166 dnode.sysctl_num = name[namelen - 1];
2167
2168 /*
2169 * we found it, now let's nuke it
2170 */
2171 name[namelen - 1] = CTL_DESTROY;
2172 sz = 0;
2173 error = sysctl_destroy(&name[namelen - 1], 1, NULL, &sz,
2174 node, sizeof(*node), &name[0], NULL,
2175 pnode);
2176 if (error == ENOTEMPTY) {
2177 /*
2178 * think of trying to delete "foo" when "foo.bar"
2179 * (which someone else put there) is still in
2180 * existence
2181 */
2182 error = 0;
2183
2184 /*
2185 * dunno who put the description there, but if this
2186 * node can ever be removed, we need to make sure the
2187 * string doesn't go out of context. that means we
2188 * need to find the node that's still there (don't use
2189 * sysctl_locate() because that follows aliasing).
2190 */
2191 node = pnode->sysctl_child;
2192 for (ni = 0; ni < pnode->sysctl_clen; ni++)
2193 if (node[ni].sysctl_num == dnode.sysctl_num)
2194 break;
2195 node = (ni < pnode->sysctl_clen) ? &node[ni] : NULL;
2196
2197 /*
2198 * if we found it, and this node has a description,
2199 * and this node can be released, and it doesn't
2200 * already own its own description...sigh. :)
2201 */
2202 if (node != NULL && node->sysctl_desc != NULL &&
2203 !(node->sysctl_flags & CTLFLAG_PERMANENT) &&
2204 !(node->sysctl_flags & CTLFLAG_OWNDESC)) {
2205 char *d;
2206
2207 sz = strlen(node->sysctl_desc) + 1;
2208 d = malloc(sz, M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
2209 if (d != NULL) {
2210 /*
2211 * discard const so that we can
2212 * re-attach the description
2213 */
2214 memcpy(d, node->sysctl_desc, sz);
2215 onode = __UNCONST(node);
2216 onode->sysctl_desc = d;
2217 onode->sysctl_flags |= CTLFLAG_OWNDESC;
2218 } else {
2219 /*
2220 * XXX drop the description? be
2221 * afraid? don't care?
2222 */
2223 }
2224 }
2225 }
2226
2227 sysctl_unlock(NULL);
2228
2229 return (error);
2230 }
2231
2232 #if 0
2233 /*
2234 * ********************************************************************
2235 * the dump routine. i haven't yet decided how (if at all) i'll call
2236 * this from userland when it's in the kernel.
2237 * ********************************************************************
2238 */
2239 static const char *
2240 sf(int f)
2241 {
2242 static char s[256];
2243 char *c;
2244
2245 s[0] = '\0';
2246 c = "";
2247
2248 #define print_flag(_f, _s, _c, _q, _x) \
2249 if (((_f) & (__CONCAT(CTLFLAG_,_x))) == (__CONCAT(CTLFLAG_,_q))) { \
2250 strlcat((_s), (_c), sizeof(_s)); \
2251 strlcat((_s), __STRING(_q), sizeof(_s)); \
2252 (_c) = ","; \
2253 (_f) &= ~__CONCAT(CTLFLAG_,_x); \
2254 }
2255
2256 print_flag(f, s, c, READONLY, READWRITE);
2257 print_flag(f, s, c, READWRITE, READWRITE);
2258 print_flag(f, s, c, ANYWRITE, ANYWRITE);
2259 print_flag(f, s, c, PRIVATE, PRIVATE);
2260 print_flag(f, s, c, PERMANENT, PERMANENT);
2261 print_flag(f, s, c, OWNDATA, OWNDATA);
2262 print_flag(f, s, c, IMMEDIATE, IMMEDIATE);
2263 print_flag(f, s, c, HEX, HEX);
2264 print_flag(f, s, c, ROOT, ROOT);
2265 print_flag(f, s, c, ANYNUMBER, ANYNUMBER);
2266 print_flag(f, s, c, HIDDEN, HIDDEN);
2267 print_flag(f, s, c, ALIAS, ALIAS);
2268 #undef print_flag
2269
2270 if (f) {
2271 char foo[9];
2272 snprintf(foo, sizeof(foo), "%x", f);
2273 strlcat(s, c, sizeof(s));
2274 strlcat(s, foo, sizeof(s));
2275 }
2276
2277 return (s);
2278 }
2279
2280 static const char *
2281 st(int t)
2282 {
2283
2284 switch (t) {
2285 case CTLTYPE_NODE:
2286 return "NODE";
2287 case CTLTYPE_INT:
2288 return "INT";
2289 case CTLTYPE_STRING:
2290 return "STRING";
2291 case CTLTYPE_QUAD:
2292 return "QUAD";
2293 case CTLTYPE_STRUCT:
2294 return "STRUCT";
2295 }
2296
2297 return "???";
2298 }
2299
2300 void
2301 sysctl_dump(const struct sysctlnode *d)
2302 {
2303 static char nmib[64], smib[256];
2304 static int indent;
2305 struct sysctlnode *n;
2306 char *np, *sp, tmp[20];
2307 int i;
2308
2309 if (d == NULL)
2310 return;
2311
2312 np = &nmib[strlen(nmib)];
2313 sp = &smib[strlen(smib)];
2314
2315 if (!(d->sysctl_flags & CTLFLAG_ROOT)) {
2316 snprintf(tmp, sizeof(tmp), "%d", d->sysctl_num);
2317 strcat(nmib, ".");
2318 strcat(smib, ".");
2319 strcat(nmib, tmp);
2320 strcat(smib, d->sysctl_name);
2321 printf("%s -> %s (%d)\n", &nmib[1], &smib[1],
2322 SYSCTL_TYPE(d->sysctl_flags));
2323 }
2324
2325 if (1) {
2326 printf("%*s%p:\tsysctl_name [%s]\n", indent, "",
2327 d, d->sysctl_name);
2328 printf("%*s\t\tsysctl_num %d\n", indent, "",
2329 d->sysctl_num);
2330 printf("%*s\t\tsysctl_flags %x (flags=%x<%s> type=%d<%s> "
2331 "size=%zu)\n",
2332 indent, "", d->sysctl_flags,
2333 SYSCTL_FLAGS(d->sysctl_flags),
2334 sf(SYSCTL_FLAGS(d->sysctl_flags)),
2335 SYSCTL_TYPE(d->sysctl_flags),
2336 st(SYSCTL_TYPE(d->sysctl_flags)),
2337 d->sysctl_size);
2338 if (SYSCTL_TYPE(d->sysctl_flags) == CTLTYPE_NODE) {
2339 printf("%*s\t\tsysctl_csize %d\n", indent, "",
2340 d->sysctl_csize);
2341 printf("%*s\t\tsysctl_clen %d\n", indent, "",
2342 d->sysctl_clen);
2343 printf("%*s\t\tsysctl_child %p\n", indent, "",
2344 d->sysctl_child);
2345 } else
2346 printf("%*s\t\tsysctl_data %p\n", indent, "",
2347 d->sysctl_data);
2348 printf("%*s\t\tsysctl_func %p\n", indent, "",
2349 d->sysctl_func);
2350 printf("%*s\t\tsysctl_parent %p\n", indent, "",
2351 d->sysctl_parent);
2352 printf("%*s\t\tsysctl_ver %d\n", indent, "",
2353 d->sysctl_ver);
2354 }
2355
2356 if (SYSCTL_TYPE(d->sysctl_flags) == CTLTYPE_NODE) {
2357 indent += 8;
2358 n = d->sysctl_child;
2359 for (i = 0; i < d->sysctl_clen; i++) {
2360 sysctl_dump(&n[i]);
2361 }
2362 indent -= 8;
2363 }
2364
2365 np[0] = '\0';
2366 sp[0] = '\0';
2367 }
2368 #endif /* 0 */
2369
2370 /*
2371 * ********************************************************************
2372 * Deletes an entire n-ary tree. Not recommended unless you know why
2373 * you're doing it. Personally, I don't know why you'd even think
2374 * about it.
2375 * ********************************************************************
2376 */
2377 void
2378 sysctl_free(struct sysctlnode *rnode)
2379 {
2380 struct sysctlnode *node, *pnode;
2381
2382 if (rnode == NULL)
2383 rnode = &sysctl_root;
2384
2385 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
2386 printf("sysctl_free: rnode %p wrong version\n", rnode);
2387 return;
2388 }
2389
2390 pnode = rnode;
2391
2392 node = pnode->sysctl_child;
2393 do {
2394 while (node != NULL && pnode->sysctl_csize > 0) {
2395 while (node <
2396 &pnode->sysctl_child[pnode->sysctl_clen] &&
2397 (SYSCTL_TYPE(node->sysctl_flags) !=
2398 CTLTYPE_NODE ||
2399 node->sysctl_csize == 0)) {
2400 if (SYSCTL_FLAGS(node->sysctl_flags) &
2401 CTLFLAG_OWNDATA) {
2402 if (node->sysctl_data != NULL) {
2403 free(node->sysctl_data,
2404 M_SYSCTLDATA);
2405 node->sysctl_data = NULL;
2406 }
2407 }
2408 if (SYSCTL_FLAGS(node->sysctl_flags) &
2409 CTLFLAG_OWNDESC) {
2410 if (node->sysctl_desc != NULL) {
2411 /*XXXUNCONST*/
2412 free(__UNCONST(node->sysctl_desc),
2413 M_SYSCTLDATA);
2414 node->sysctl_desc = NULL;
2415 }
2416 }
2417 node++;
2418 }
2419 if (node < &pnode->sysctl_child[pnode->sysctl_clen]) {
2420 pnode = node;
2421 node = node->sysctl_child;
2422 } else
2423 break;
2424 }
2425 if (pnode->sysctl_child != NULL)
2426 free(pnode->sysctl_child, M_SYSCTLNODE);
2427 pnode->sysctl_clen = 0;
2428 pnode->sysctl_csize = 0;
2429 pnode->sysctl_child = NULL;
2430 node = pnode;
2431 pnode = node->sysctl_parent;
2432 } while (pnode != NULL && node != rnode);
2433 }
2434
2435 int
2436 sysctl_log_add(struct sysctllog **logp, const struct sysctlnode *node)
2437 {
2438 int name[CTL_MAXNAME], namelen, i;
2439 const struct sysctlnode *pnode;
2440 struct sysctllog *log;
2441
2442 if (node->sysctl_flags & CTLFLAG_PERMANENT)
2443 return (0);
2444
2445 if (logp == NULL)
2446 return (0);
2447
2448 if (*logp == NULL) {
2449 log = malloc(sizeof(struct sysctllog),
2450 M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
2451 if (log == NULL) {
2452 /* XXX print error message? */
2453 return (-1);
2454 }
2455 log->log_num = malloc(16 * sizeof(int),
2456 M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
2457 if (log->log_num == NULL) {
2458 /* XXX print error message? */
2459 free(log, M_SYSCTLDATA);
2460 return (-1);
2461 }
2462 memset(log->log_num, 0, 16 * sizeof(int));
2463 log->log_root = NULL;
2464 log->log_size = 16;
2465 log->log_left = 16;
2466 *logp = log;
2467 } else
2468 log = *logp;
2469
2470 /*
2471 * check that the root is proper. it's okay to record the
2472 * address of the root of a tree. it's the only thing that's
2473 * guaranteed not to shift around as nodes come and go.
2474 */
2475 if (log->log_root == NULL)
2476 log->log_root = sysctl_rootof(node);
2477 else if (log->log_root != sysctl_rootof(node)) {
2478 printf("sysctl: log %p root mismatch (%p)\n",
2479 log->log_root, sysctl_rootof(node));
2480 return (-1);
2481 }
2482
2483 /*
2484 * we will copy out name in reverse order
2485 */
2486 for (pnode = node, namelen = 0;
2487 pnode != NULL && !(pnode->sysctl_flags & CTLFLAG_ROOT);
2488 pnode = pnode->sysctl_parent)
2489 name[namelen++] = pnode->sysctl_num;
2490
2491 /*
2492 * do we have space?
2493 */
2494 if (log->log_left < (namelen + 3))
2495 sysctl_log_realloc(log);
2496 if (log->log_left < (namelen + 3))
2497 return (-1);
2498
2499 /*
2500 * stuff name in, then namelen, then node type, and finally,
2501 * the version for non-node nodes.
2502 */
2503 for (i = 0; i < namelen; i++)
2504 log->log_num[--log->log_left] = name[i];
2505 log->log_num[--log->log_left] = namelen;
2506 log->log_num[--log->log_left] = SYSCTL_TYPE(node->sysctl_flags);
2507 if (log->log_num[log->log_left] != CTLTYPE_NODE)
2508 log->log_num[--log->log_left] = node->sysctl_ver;
2509 else
2510 log->log_num[--log->log_left] = 0;
2511
2512 return (0);
2513 }
2514
2515 void
2516 sysctl_teardown(struct sysctllog **logp)
2517 {
2518 const struct sysctlnode *rnode;
2519 struct sysctlnode node;
2520 struct sysctllog *log;
2521 uint namelen;
2522 int *name, t, v, error, ni;
2523 size_t sz;
2524
2525 if (logp == NULL || *logp == NULL)
2526 return;
2527 log = *logp;
2528
2529 error = sysctl_lock(NULL, NULL, 0);
2530 if (error)
2531 return;
2532
2533 memset(&node, 0, sizeof(node));
2534
2535 while (log->log_left < log->log_size) {
2536 KASSERT((log->log_left + 3 < log->log_size) &&
2537 (log->log_left + log->log_num[log->log_left + 2] <=
2538 log->log_size));
2539 v = log->log_num[log->log_left++];
2540 t = log->log_num[log->log_left++];
2541 namelen = log->log_num[log->log_left++];
2542 name = &log->log_num[log->log_left];
2543
2544 node.sysctl_num = name[namelen - 1];
2545 node.sysctl_flags = SYSCTL_VERSION|t;
2546 node.sysctl_ver = v;
2547
2548 rnode = log->log_root;
2549 error = sysctl_locate(NULL, &name[0], namelen, &rnode, &ni);
2550 if (error == 0) {
2551 name[namelen - 1] = CTL_DESTROY;
2552 rnode = rnode->sysctl_parent;
2553 sz = 0;
2554 (void)sysctl_destroy(&name[namelen - 1], 1, NULL,
2555 &sz, &node, sizeof(node),
2556 &name[0], NULL, rnode);
2557 }
2558
2559 log->log_left += namelen;
2560 }
2561
2562 KASSERT(log->log_size == log->log_left);
2563 free(log->log_num, M_SYSCTLDATA);
2564 free(log, M_SYSCTLDATA);
2565 *logp = NULL;
2566
2567 sysctl_unlock(NULL);
2568 }
2569
2570 /*
2571 * ********************************************************************
2572 * old_sysctl -- A routine to bridge old-style internal calls to the
2573 * new infrastructure.
2574 * ********************************************************************
2575 */
2576 int
2577 old_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
2578 void *newp, size_t newlen, struct lwp *l)
2579 {
2580 int error;
2581 size_t oldlen = 0;
2582 size_t savelen;
2583
2584 if (oldlenp) {
2585 oldlen = *oldlenp;
2586 }
2587 savelen = oldlen;
2588
2589 error = sysctl_lock(l, oldp, savelen);
2590 if (error)
2591 return (error);
2592 error = sysctl_dispatch(name, namelen, oldp, &oldlen,
2593 newp, newlen, name, l, NULL);
2594 sysctl_unlock(l);
2595 if (error == 0 && oldp != NULL && savelen < oldlen)
2596 error = ENOMEM;
2597
2598 if (oldlenp) {
2599 *oldlenp = oldlen;
2600 }
2601
2602 return (error);
2603 }
2604
2605 /*
2606 * ********************************************************************
2607 * Section 4: Generic helper routines
2608 * ********************************************************************
2609 * "helper" routines that can do more finely grained access control,
2610 * construct structures from disparate information, create the
2611 * appearance of more nodes and sub-trees, etc. for example, if
2612 * CTL_PROC wanted a helper function, it could respond to a CTL_QUERY
2613 * with a dynamically created list of nodes that represented the
2614 * currently running processes at that instant.
2615 * ********************************************************************
2616 */
2617
2618 /*
2619 * first, a few generic helpers that provide:
2620 *
2621 * sysctl_needfunc() a readonly interface that emits a warning
2622 * sysctl_notavail() returns EOPNOTSUPP (generic error)
2623 * sysctl_null() an empty return buffer with no error
2624 */
2625 int
2626 sysctl_needfunc(SYSCTLFN_ARGS)
2627 {
2628 int error;
2629
2630 printf("!!SYSCTL_NEEDFUNC!!\n");
2631
2632 if (newp != NULL || namelen != 0)
2633 return (EOPNOTSUPP);
2634
2635 error = 0;
2636 if (oldp != NULL)
2637 error = sysctl_copyout(l, rnode->sysctl_data, oldp,
2638 MIN(rnode->sysctl_size, *oldlenp));
2639 *oldlenp = rnode->sysctl_size;
2640
2641 return (error);
2642 }
2643
2644 int
2645 sysctl_notavail(SYSCTLFN_ARGS)
2646 {
2647
2648 if (namelen == 1 && name[0] == CTL_QUERY)
2649 return (sysctl_query(SYSCTLFN_CALL(rnode)));
2650
2651 return (EOPNOTSUPP);
2652 }
2653
2654 int
2655 sysctl_null(SYSCTLFN_ARGS)
2656 {
2657
2658 *oldlenp = 0;
2659
2660 return (0);
2661 }
2662
2663 /*
2664 * ********************************************************************
2665 * Section 5: The machinery that makes it all go
2666 * ********************************************************************
2667 * Memory "manglement" routines. Not much to this, eh?
2668 * ********************************************************************
2669 */
2670 static int
2671 sysctl_alloc(struct sysctlnode *p, int x)
2672 {
2673 int i;
2674 struct sysctlnode *n;
2675
2676 assert(p->sysctl_child == NULL);
2677
2678 if (x == 1)
2679 n = malloc(sizeof(struct sysctlnode),
2680 M_SYSCTLNODE, M_WAITOK|M_CANFAIL);
2681 else
2682 n = malloc(SYSCTL_DEFSIZE * sizeof(struct sysctlnode),
2683 M_SYSCTLNODE, M_WAITOK|M_CANFAIL);
2684 if (n == NULL)
2685 return (ENOMEM);
2686
2687 if (x == 1) {
2688 memset(n, 0, sizeof(struct sysctlnode));
2689 p->sysctl_csize = 1;
2690 } else {
2691 memset(n, 0, SYSCTL_DEFSIZE * sizeof(struct sysctlnode));
2692 p->sysctl_csize = SYSCTL_DEFSIZE;
2693 }
2694 p->sysctl_clen = 0;
2695
2696 for (i = 0; i < p->sysctl_csize; i++)
2697 n[i].sysctl_parent = p;
2698
2699 p->sysctl_child = n;
2700 return (0);
2701 }
2702
2703 static int
2704 sysctl_realloc(struct sysctlnode *p)
2705 {
2706 int i, j;
2707 struct sysctlnode *n;
2708
2709 assert(p->sysctl_csize == p->sysctl_clen);
2710
2711 /*
2712 * how many do we have...how many should we make?
2713 */
2714 i = p->sysctl_clen;
2715 n = malloc(2 * i * sizeof(struct sysctlnode), M_SYSCTLNODE,
2716 M_WAITOK|M_CANFAIL);
2717 if (n == NULL)
2718 return (ENOMEM);
2719
2720 /*
2721 * move old children over...initialize new children
2722 */
2723 memcpy(n, p->sysctl_child, i * sizeof(struct sysctlnode));
2724 memset(&n[i], 0, i * sizeof(struct sysctlnode));
2725 p->sysctl_csize = 2 * i;
2726
2727 /*
2728 * reattach moved (and new) children to parent; if a moved
2729 * child node has children, reattach the parent pointers of
2730 * grandchildren
2731 */
2732 for (i = 0; i < p->sysctl_csize; i++) {
2733 n[i].sysctl_parent = p;
2734 if (n[i].sysctl_child != NULL) {
2735 for (j = 0; j < n[i].sysctl_csize; j++)
2736 n[i].sysctl_child[j].sysctl_parent = &n[i];
2737 }
2738 }
2739
2740 /*
2741 * get out with the old and in with the new
2742 */
2743 free(p->sysctl_child, M_SYSCTLNODE);
2744 p->sysctl_child = n;
2745
2746 return (0);
2747 }
2748
2749 static int
2750 sysctl_log_realloc(struct sysctllog *log)
2751 {
2752 int *n, s, d;
2753
2754 s = log->log_size * 2;
2755 d = log->log_size;
2756
2757 n = malloc(s * sizeof(int), M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
2758 if (n == NULL)
2759 return (-1);
2760
2761 memset(n, 0, s * sizeof(int));
2762 memcpy(&n[d], log->log_num, d * sizeof(int));
2763 free(log->log_num, M_SYSCTLDATA);
2764 log->log_num = n;
2765 if (d)
2766 log->log_left += d;
2767 else
2768 log->log_left = s;
2769 log->log_size = s;
2770
2771 return (0);
2772 }
2773
2774 /*
2775 * ********************************************************************
2776 * Section 6: Conversion between API versions wrt the sysctlnode
2777 * ********************************************************************
2778 */
2779 static int
2780 sysctl_cvt_in(struct lwp *l, int *vp, const void *i, size_t sz,
2781 struct sysctlnode *node)
2782 {
2783 int error, flags;
2784
2785 if (i == NULL || sz < sizeof(flags))
2786 return (EINVAL);
2787
2788 error = sysctl_copyin(l, i, &flags, sizeof(flags));
2789 if (error)
2790 return (error);
2791
2792 #if (SYSCTL_VERSION != SYSCTL_VERS_1)
2793 #error sysctl_cvt_in: no support for SYSCTL_VERSION
2794 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
2795
2796 if (sz == sizeof(*node) &&
2797 SYSCTL_VERS(flags) == SYSCTL_VERSION) {
2798 error = sysctl_copyin(l, i, node, sizeof(*node));
2799 if (error)
2800 return (error);
2801 *vp = SYSCTL_VERSION;
2802 return (0);
2803 }
2804
2805 return (EINVAL);
2806 }
2807
2808 static int
2809 sysctl_cvt_out(struct lwp *l, int v, const struct sysctlnode *i,
2810 void *ovp, size_t left, size_t *szp)
2811 {
2812 size_t sz = sizeof(*i);
2813 const void *src = i;
2814 int error;
2815
2816 switch (v) {
2817 case SYSCTL_VERS_0:
2818 return (EINVAL);
2819
2820 #if (SYSCTL_VERSION != SYSCTL_VERS_1)
2821 #error sysctl_cvt_out: no support for SYSCTL_VERSION
2822 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
2823
2824 case SYSCTL_VERSION:
2825 /* nothing more to do here */
2826 break;
2827 }
2828
2829 if (ovp != NULL && left >= sz) {
2830 error = sysctl_copyout(l, src, ovp, sz);
2831 if (error)
2832 return (error);
2833 }
2834
2835 if (szp != NULL)
2836 *szp = sz;
2837
2838 return (0);
2839 }
2840