1 /*-
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Mike Karels at Berkeley Software Design, Inc.
7 *
8 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
9 * project, to make these variables more userfriendly.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94
36 */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include "opt_compat.h"
42 #include "opt_mac.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/sysctl.h>
48 #include <sys/malloc.h>
49 #include <sys/priv.h>
50 #include <sys/proc.h>
51 #include <sys/lock.h>
52 #include <sys/mutex.h>
53 #include <sys/sx.h>
54 #include <sys/sysproto.h>
55 #include <sys/uio.h>
56
57 #include <security/mac/mac_framework.h>
58
59 #include <vm/vm.h>
60 #include <vm/vm_extern.h>
61
62 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
63 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
64 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
65
66 /*
67 * Locking - this locks the sysctl tree in memory.
68 */
69 static struct sx sysctllock;
70
71 #define SYSCTL_LOCK() sx_xlock(&sysctllock)
72 #define SYSCTL_UNLOCK() sx_xunlock(&sysctllock)
73 #define SYSCTL_INIT() sx_init(&sysctllock, "sysctl lock")
74
75 static int sysctl_root(SYSCTL_HANDLER_ARGS);
76
77 struct sysctl_oid_list sysctl__children; /* root list */
78
79 static struct sysctl_oid *
80 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
81 {
82 struct sysctl_oid *oidp;
83
84 SLIST_FOREACH(oidp, list, oid_link) {
85 if (strcmp(oidp->oid_name, name) == 0) {
86 return (oidp);
87 }
88 }
89 return (NULL);
90 }
91
92 /*
93 * Initialization of the MIB tree.
94 *
95 * Order by number in each list.
96 */
97
98 void
99 sysctl_register_oid(struct sysctl_oid *oidp)
100 {
101 struct sysctl_oid_list *parent = oidp->oid_parent;
102 struct sysctl_oid *p;
103 struct sysctl_oid *q;
104
105 /*
106 * First check if another oid with the same name already
107 * exists in the parent's list.
108 */
109 p = sysctl_find_oidname(oidp->oid_name, parent);
110 if (p != NULL) {
111 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
112 p->oid_refcnt++;
113 return;
114 } else {
115 printf("can't re-use a leaf (%s)!\n", p->oid_name);
116 return;
117 }
118 }
119 /*
120 * If this oid has a number OID_AUTO, give it a number which
121 * is greater than any current oid.
122 * NOTE: DO NOT change the starting value here, change it in
123 * <sys/sysctl.h>, and make sure it is at least 256 to
124 * accomodate e.g. net.inet.raw as a static sysctl node.
125 */
126 if (oidp->oid_number == OID_AUTO) {
127 static int newoid = CTL_AUTO_START;
128
129 oidp->oid_number = newoid++;
130 if (newoid == 0x7fffffff)
131 panic("out of oids");
132 }
133 #if 0
134 else if (oidp->oid_number >= CTL_AUTO_START) {
135 /* do not panic; this happens when unregistering sysctl sets */
136 printf("static sysctl oid too high: %d", oidp->oid_number);
137 }
138 #endif
139
140 /*
141 * Insert the oid into the parent's list in order.
142 */
143 q = NULL;
144 SLIST_FOREACH(p, parent, oid_link) {
145 if (oidp->oid_number < p->oid_number)
146 break;
147 q = p;
148 }
149 if (q)
150 SLIST_INSERT_AFTER(q, oidp, oid_link);
151 else
152 SLIST_INSERT_HEAD(parent, oidp, oid_link);
153 }
154
155 void
156 sysctl_unregister_oid(struct sysctl_oid *oidp)
157 {
158 struct sysctl_oid *p;
159 int error;
160
161 error = ENOENT;
162 if (oidp->oid_number == OID_AUTO) {
163 error = EINVAL;
164 } else {
165 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
166 if (p == oidp) {
167 SLIST_REMOVE(oidp->oid_parent, oidp,
168 sysctl_oid, oid_link);
169 error = 0;
170 break;
171 }
172 }
173 }
174
175 /*
176 * This can happen when a module fails to register and is
177 * being unloaded afterwards. It should not be a panic()
178 * for normal use.
179 */
180 if (error)
181 printf("%s: failed to unregister sysctl\n", __func__);
182 }
183
184 /* Initialize a new context to keep track of dynamically added sysctls. */
185 int
186 sysctl_ctx_init(struct sysctl_ctx_list *c)
187 {
188
189 if (c == NULL) {
190 return (EINVAL);
191 }
192 TAILQ_INIT(c);
193 return (0);
194 }
195
196 /* Free the context, and destroy all dynamic oids registered in this context */
197 int
198 sysctl_ctx_free(struct sysctl_ctx_list *clist)
199 {
200 struct sysctl_ctx_entry *e, *e1;
201 int error;
202
203 error = 0;
204 /*
205 * First perform a "dry run" to check if it's ok to remove oids.
206 * XXX FIXME
207 * XXX This algorithm is a hack. But I don't know any
208 * XXX better solution for now...
209 */
210 TAILQ_FOREACH(e, clist, link) {
211 error = sysctl_remove_oid(e->entry, 0, 0);
212 if (error)
213 break;
214 }
215 /*
216 * Restore deregistered entries, either from the end,
217 * or from the place where error occured.
218 * e contains the entry that was not unregistered
219 */
220 if (error)
221 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
222 else
223 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
224 while (e1 != NULL) {
225 sysctl_register_oid(e1->entry);
226 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
227 }
228 if (error)
229 return(EBUSY);
230 /* Now really delete the entries */
231 e = TAILQ_FIRST(clist);
232 while (e != NULL) {
233 e1 = TAILQ_NEXT(e, link);
234 error = sysctl_remove_oid(e->entry, 1, 0);
235 if (error)
236 panic("sysctl_remove_oid: corrupt tree, entry: %s",
237 e->entry->oid_name);
238 free(e, M_SYSCTLOID);
239 e = e1;
240 }
241 return (error);
242 }
243
244 /* Add an entry to the context */
245 struct sysctl_ctx_entry *
246 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
247 {
248 struct sysctl_ctx_entry *e;
249
250 if (clist == NULL || oidp == NULL)
251 return(NULL);
252 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
253 e->entry = oidp;
254 TAILQ_INSERT_HEAD(clist, e, link);
255 return (e);
256 }
257
258 /* Find an entry in the context */
259 struct sysctl_ctx_entry *
260 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
261 {
262 struct sysctl_ctx_entry *e;
263
264 if (clist == NULL || oidp == NULL)
265 return(NULL);
266 TAILQ_FOREACH(e, clist, link) {
267 if(e->entry == oidp)
268 return(e);
269 }
270 return (e);
271 }
272
273 /*
274 * Delete an entry from the context.
275 * NOTE: this function doesn't free oidp! You have to remove it
276 * with sysctl_remove_oid().
277 */
278 int
279 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
280 {
281 struct sysctl_ctx_entry *e;
282
283 if (clist == NULL || oidp == NULL)
284 return (EINVAL);
285 e = sysctl_ctx_entry_find(clist, oidp);
286 if (e != NULL) {
287 TAILQ_REMOVE(clist, e, link);
288 free(e, M_SYSCTLOID);
289 return (0);
290 } else
291 return (ENOENT);
292 }
293
294 /*
295 * Remove dynamically created sysctl trees.
296 * oidp - top of the tree to be removed
297 * del - if 0 - just deregister, otherwise free up entries as well
298 * recurse - if != 0 traverse the subtree to be deleted
299 */
300 int
301 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
302 {
303 struct sysctl_oid *p;
304 int error;
305
306 if (oidp == NULL)
307 return(EINVAL);
308 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
309 printf("can't remove non-dynamic nodes!\n");
310 return (EINVAL);
311 }
312 /*
313 * WARNING: normal method to do this should be through
314 * sysctl_ctx_free(). Use recursing as the last resort
315 * method to purge your sysctl tree of leftovers...
316 * However, if some other code still references these nodes,
317 * it will panic.
318 */
319 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
320 if (oidp->oid_refcnt == 1) {
321 SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
322 if (!recurse)
323 return (ENOTEMPTY);
324 error = sysctl_remove_oid(p, del, recurse);
325 if (error)
326 return (error);
327 }
328 if (del)
329 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
330 }
331 }
332 if (oidp->oid_refcnt > 1 ) {
333 oidp->oid_refcnt--;
334 } else {
335 if (oidp->oid_refcnt == 0) {
336 printf("Warning: bad oid_refcnt=%u (%s)!\n",
337 oidp->oid_refcnt, oidp->oid_name);
338 return (EINVAL);
339 }
340 sysctl_unregister_oid(oidp);
341 if (del) {
342 if (oidp->oid_descr)
343 free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
344 free((void *)(uintptr_t)(const void *)oidp->oid_name,
345 M_SYSCTLOID);
346 free(oidp, M_SYSCTLOID);
347 }
348 }
349 return (0);
350 }
351
352 /*
353 * Create new sysctls at run time.
354 * clist may point to a valid context initialized with sysctl_ctx_init().
355 */
356 struct sysctl_oid *
357 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
358 int number, const char *name, int kind, void *arg1, int arg2,
359 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
360 {
361 struct sysctl_oid *oidp;
362 ssize_t len;
363 char *newname;
364
365 /* You have to hook up somewhere.. */
366 if (parent == NULL)
367 return(NULL);
368 /* Check if the node already exists, otherwise create it */
369 oidp = sysctl_find_oidname(name, parent);
370 if (oidp != NULL) {
371 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
372 oidp->oid_refcnt++;
373 /* Update the context */
374 if (clist != NULL)
375 sysctl_ctx_entry_add(clist, oidp);
376 return (oidp);
377 } else {
378 printf("can't re-use a leaf (%s)!\n", name);
379 return (NULL);
380 }
381 }
382 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
383 oidp->oid_parent = parent;
384 SLIST_NEXT(oidp, oid_link) = NULL;
385 oidp->oid_number = number;
386 oidp->oid_refcnt = 1;
387 len = strlen(name);
388 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
389 bcopy(name, newname, len + 1);
390 newname[len] = '\0';
391 oidp->oid_name = newname;
392 oidp->oid_handler = handler;
393 oidp->oid_kind = CTLFLAG_DYN | kind;
394 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
395 /* Allocate space for children */
396 SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
397 M_SYSCTLOID, M_WAITOK));
398 SLIST_INIT(SYSCTL_CHILDREN(oidp));
399 } else {
400 oidp->oid_arg1 = arg1;
401 oidp->oid_arg2 = arg2;
402 }
403 oidp->oid_fmt = fmt;
404 if (descr) {
405 int len = strlen(descr) + 1;
406 oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
407 if (oidp->oid_descr)
408 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
409 }
410 /* Update the context, if used */
411 if (clist != NULL)
412 sysctl_ctx_entry_add(clist, oidp);
413 /* Register this oid */
414 sysctl_register_oid(oidp);
415 return (oidp);
416 }
417
418 /*
419 * Reparent an existing oid.
420 */
421 int
422 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
423 {
424 struct sysctl_oid *oidp;
425
426 if (oid->oid_parent == parent)
427 return (0);
428 oidp = sysctl_find_oidname(oid->oid_name, parent);
429 if (oidp != NULL)
430 return (EEXIST);
431 sysctl_unregister_oid(oid);
432 oid->oid_parent = parent;
433 oid->oid_number = OID_AUTO;
434 sysctl_register_oid(oid);
435 return (0);
436 }
437
438 /*
439 * Register the kernel's oids on startup.
440 */
441 SET_DECLARE(sysctl_set, struct sysctl_oid);
442
443 static void
444 sysctl_register_all(void *arg)
445 {
446 struct sysctl_oid **oidp;
447
448 SYSCTL_INIT();
449 SET_FOREACH(oidp, sysctl_set)
450 sysctl_register_oid(*oidp);
451 }
452 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
453
454 /*
455 * "Staff-functions"
456 *
457 * These functions implement a presently undocumented interface
458 * used by the sysctl program to walk the tree, and get the type
459 * so it can print the value.
460 * This interface is under work and consideration, and should probably
461 * be killed with a big axe by the first person who can find the time.
462 * (be aware though, that the proper interface isn't as obvious as it
463 * may seem, there are various conflicting requirements.
464 *
465 * {0,0} printf the entire MIB-tree.
466 * {0,1,...} return the name of the "..." OID.
467 * {0,2,...} return the next OID.
468 * {0,3} return the OID of the name in "new"
469 * {0,4,...} return the kind & format info for the "..." OID.
470 * {0,5,...} return the description the "..." OID.
471 */
472
473 #ifdef SYSCTL_DEBUG
474 static void
475 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
476 {
477 int k;
478 struct sysctl_oid *oidp;
479
480 SLIST_FOREACH(oidp, l, oid_link) {
481
482 for (k=0; k<i; k++)
483 printf(" ");
484
485 printf("%d %s ", oidp->oid_number, oidp->oid_name);
486
487 printf("%c%c",
488 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
489 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
490
491 if (oidp->oid_handler)
492 printf(" *Handler");
493
494 switch (oidp->oid_kind & CTLTYPE) {
495 case CTLTYPE_NODE:
496 printf(" Node\n");
497 if (!oidp->oid_handler) {
498 sysctl_sysctl_debug_dump_node(
499 oidp->oid_arg1, i+2);
500 }
501 break;
502 case CTLTYPE_INT: printf(" Int\n"); break;
503 case CTLTYPE_STRING: printf(" String\n"); break;
504 case CTLTYPE_QUAD: printf(" Quad\n"); break;
505 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
506 default: printf("\n");
507 }
508
509 }
510 }
511
512 static int
513 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
514 {
515 int error;
516
517 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
518 if (error)
519 return (error);
520 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
521 return (ENOENT);
522 }
523
524 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
525 0, 0, sysctl_sysctl_debug, "-", "");
526 #endif
527
528 static int
529 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
530 {
531 int *name = (int *) arg1;
532 u_int namelen = arg2;
533 int error = 0;
534 struct sysctl_oid *oid;
535 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
536 char buf[10];
537
538 while (namelen) {
539 if (!lsp) {
540 snprintf(buf,sizeof(buf),"%d",*name);
541 if (req->oldidx)
542 error = SYSCTL_OUT(req, ".", 1);
543 if (!error)
544 error = SYSCTL_OUT(req, buf, strlen(buf));
545 if (error)
546 return (error);
547 namelen--;
548 name++;
549 continue;
550 }
551 lsp2 = 0;
552 SLIST_FOREACH(oid, lsp, oid_link) {
553 if (oid->oid_number != *name)
554 continue;
555
556 if (req->oldidx)
557 error = SYSCTL_OUT(req, ".", 1);
558 if (!error)
559 error = SYSCTL_OUT(req, oid->oid_name,
560 strlen(oid->oid_name));
561 if (error)
562 return (error);
563
564 namelen--;
565 name++;
566
567 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
568 break;
569
570 if (oid->oid_handler)
571 break;
572
573 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
574 break;
575 }
576 lsp = lsp2;
577 }
578 return (SYSCTL_OUT(req, "", 1));
579 }
580
581 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
582
583 static int
584 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
585 int *next, int *len, int level, struct sysctl_oid **oidpp)
586 {
587 struct sysctl_oid *oidp;
588
589 *len = level;
590 SLIST_FOREACH(oidp, lsp, oid_link) {
591 *next = oidp->oid_number;
592 *oidpp = oidp;
593
594 if (oidp->oid_kind & CTLFLAG_SKIP)
595 continue;
596
597 if (!namelen) {
598 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
599 return (0);
600 if (oidp->oid_handler)
601 /* We really should call the handler here...*/
602 return (0);
603 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
604 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
605 len, level+1, oidpp))
606 return (0);
607 goto emptynode;
608 }
609
610 if (oidp->oid_number < *name)
611 continue;
612
613 if (oidp->oid_number > *name) {
614 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
615 return (0);
616 if (oidp->oid_handler)
617 return (0);
618 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
619 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
620 next+1, len, level+1, oidpp))
621 return (0);
622 goto next;
623 }
624 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
625 continue;
626
627 if (oidp->oid_handler)
628 continue;
629
630 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
631 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
632 len, level+1, oidpp))
633 return (0);
634 next:
635 namelen = 1;
636 emptynode:
637 *len = level;
638 }
639 return (1);
640 }
641
642 static int
643 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
644 {
645 int *name = (int *) arg1;
646 u_int namelen = arg2;
647 int i, j, error;
648 struct sysctl_oid *oid;
649 struct sysctl_oid_list *lsp = &sysctl__children;
650 int newoid[CTL_MAXNAME];
651
652 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
653 if (i)
654 return (ENOENT);
655 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
656 return (error);
657 }
658
659 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
660
661 static int
662 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
663 {
664 int i;
665 struct sysctl_oid *oidp;
666 struct sysctl_oid_list *lsp = &sysctl__children;
667 char *p;
668
669 if (!*name)
670 return (ENOENT);
671
672 p = name + strlen(name) - 1 ;
673 if (*p == '.')
674 *p = '\0';
675
676 *len = 0;
677
678 for (p = name; *p && *p != '.'; p++)
679 ;
680 i = *p;
681 if (i == '.')
682 *p = '\0';
683
684 oidp = SLIST_FIRST(lsp);
685
686 while (oidp && *len < CTL_MAXNAME) {
687 if (strcmp(name, oidp->oid_name)) {
688 oidp = SLIST_NEXT(oidp, oid_link);
689 continue;
690 }
691 *oid++ = oidp->oid_number;
692 (*len)++;
693
694 if (!i) {
695 if (oidpp)
696 *oidpp = oidp;
697 return (0);
698 }
699
700 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
701 break;
702
703 if (oidp->oid_handler)
704 break;
705
706 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
707 oidp = SLIST_FIRST(lsp);
708 name = p+1;
709 for (p = name; *p && *p != '.'; p++)
710 ;
711 i = *p;
712 if (i == '.')
713 *p = '\0';
714 }
715 return (ENOENT);
716 }
717
718 static int
719 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
720 {
721 char *p;
722 int error, oid[CTL_MAXNAME], len;
723 struct sysctl_oid *op = 0;
724
725 if (!req->newlen)
726 return (ENOENT);
727 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
728 return (ENAMETOOLONG);
729
730 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
731
732 error = SYSCTL_IN(req, p, req->newlen);
733 if (error) {
734 free(p, M_SYSCTL);
735 return (error);
736 }
737
738 p [req->newlen] = '\0';
739
740 error = name2oid(p, oid, &len, &op);
741
742 free(p, M_SYSCTL);
743
744 if (error)
745 return (error);
746
747 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
748 return (error);
749 }
750
751 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
752 sysctl_sysctl_name2oid, "I", "");
753
754 static int
755 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
756 {
757 struct sysctl_oid *oid;
758 int error;
759
760 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
761 if (error)
762 return (error);
763
764 if (!oid->oid_fmt)
765 return (ENOENT);
766 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
767 if (error)
768 return (error);
769 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
770 return (error);
771 }
772
773
774 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
775
776 static int
777 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
778 {
779 struct sysctl_oid *oid;
780 int error;
781
782 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
783 if (error)
784 return (error);
785
786 if (!oid->oid_descr)
787 return (ENOENT);
788 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
789 return (error);
790 }
791
792 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
793
794 /*
795 * Default "handler" functions.
796 */
797
798 /*
799 * Handle an int, signed or unsigned.
800 * Two cases:
801 * a variable: point arg1 at it.
802 * a constant: pass it in arg2.
803 */
804
805 int
806 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
807 {
808 int tmpout, error = 0;
809
810 /*
811 * Attempt to get a coherent snapshot by making a copy of the data.
812 */
813 if (arg1)
814 tmpout = *(int *)arg1;
815 else
816 tmpout = arg2;
817 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
818
819 if (error || !req->newptr)
820 return (error);
821
822 if (!arg1)
823 error = EPERM;
824 else
825 error = SYSCTL_IN(req, arg1, sizeof(int));
826 return (error);
827 }
828
829
830 /*
831 * Based on on sysctl_handle_int() convert milliseconds into ticks.
832 */
833
834 int
835 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
836 {
837 int error, s, tt;
838
839 tt = *(int *)oidp->oid_arg1;
840 s = (int)((int64_t)tt * 1000 / hz);
841
842 error = sysctl_handle_int(oidp, &s, 0, req);
843 if (error || !req->newptr)
844 return (error);
845
846 tt = (int)((int64_t)s * hz / 1000);
847 if (tt < 1)
848 return (EINVAL);
849
850 *(int *)oidp->oid_arg1 = tt;
851 return (0);
852 }
853
854
855 /*
856 * Handle a long, signed or unsigned. arg1 points to it.
857 */
858
859 int
860 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
861 {
862 int error = 0;
863 long tmplong;
864 #ifdef SCTL_MASK32
865 int tmpint;
866 #endif
867
868 /*
869 * Attempt to get a coherent snapshot by making a copy of the data.
870 */
871 if (!arg1)
872 return (EINVAL);
873 tmplong = *(long *)arg1;
874 #ifdef SCTL_MASK32
875 if (req->flags & SCTL_MASK32) {
876 tmpint = tmplong;
877 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
878 } else
879 #endif
880 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
881
882 if (error || !req->newptr)
883 return (error);
884
885 #ifdef SCTL_MASK32
886 if (req->flags & SCTL_MASK32) {
887 error = SYSCTL_IN(req, &tmpint, sizeof(int));
888 *(long *)arg1 = (long)tmpint;
889 } else
890 #endif
891 error = SYSCTL_IN(req, arg1, sizeof(long));
892 return (error);
893 }
894
895 /*
896 * Handle a 64 bit int, signed or unsigned. arg1 points to it.
897 */
898
899 int
900 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
901 {
902 int error = 0;
903 uint64_t tmpout;
904
905 /*
906 * Attempt to get a coherent snapshot by making a copy of the data.
907 */
908 if (!arg1)
909 return (EINVAL);
910 tmpout = *(uint64_t *)arg1;
911 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
912
913 if (error || !req->newptr)
914 return (error);
915
916 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
917 return (error);
918 }
919
920 /*
921 * Handle our generic '\0' terminated 'C' string.
922 * Two cases:
923 * a variable string: point arg1 at it, arg2 is max length.
924 * a constant string: point arg1 at it, arg2 is zero.
925 */
926
927 int
928 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
929 {
930 int error=0;
931 char *tmparg;
932 size_t outlen;
933
934 /*
935 * Attempt to get a coherent snapshot by copying to a
936 * temporary kernel buffer.
937 */
938 retry:
939 outlen = strlen((char *)arg1)+1;
940 tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
941
942 if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
943 free(tmparg, M_SYSCTLTMP);
944 goto retry;
945 }
946
947 error = SYSCTL_OUT(req, tmparg, outlen);
948 free(tmparg, M_SYSCTLTMP);
949
950 if (error || !req->newptr)
951 return (error);
952
953 if ((req->newlen - req->newidx) >= arg2) {
954 error = EINVAL;
955 } else {
956 arg2 = (req->newlen - req->newidx);
957 error = SYSCTL_IN(req, arg1, arg2);
958 ((char *)arg1)[arg2] = '\0';
959 }
960
961 return (error);
962 }
963
964 /*
965 * Handle any kind of opaque data.
966 * arg1 points to it, arg2 is the size.
967 */
968
969 int
970 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
971 {
972 int error, tries;
973 u_int generation;
974 struct sysctl_req req2;
975
976 /*
977 * Attempt to get a coherent snapshot, by using the thread
978 * pre-emption counter updated from within mi_switch() to
979 * determine if we were pre-empted during a bcopy() or
980 * copyout(). Make 3 attempts at doing this before giving up.
981 * If we encounter an error, stop immediately.
982 */
983 tries = 0;
984 req2 = *req;
985 retry:
986 generation = curthread->td_generation;
987 error = SYSCTL_OUT(req, arg1, arg2);
988 if (error)
989 return (error);
990 tries++;
991 if (generation != curthread->td_generation && tries < 3) {
992 *req = req2;
993 goto retry;
994 }
995
996 error = SYSCTL_IN(req, arg1, arg2);
997
998 return (error);
999 }
1000
1001 /*
1002 * Transfer functions to/from kernel space.
1003 * XXX: rather untested at this point
1004 */
1005 static int
1006 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1007 {
1008 size_t i = 0;
1009
1010 if (req->oldptr) {
1011 i = l;
1012 if (req->oldlen <= req->oldidx)
1013 i = 0;
1014 else
1015 if (i > req->oldlen - req->oldidx)
1016 i = req->oldlen - req->oldidx;
1017 if (i > 0)
1018 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1019 }
1020 req->oldidx += l;
1021 if (req->oldptr && i != l)
1022 return (ENOMEM);
1023 return (0);
1024 }
1025
1026 static int
1027 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1028 {
1029 if (!req->newptr)
1030 return (0);
1031 if (req->newlen - req->newidx < l)
1032 return (EINVAL);
1033 bcopy((char *)req->newptr + req->newidx, p, l);
1034 req->newidx += l;
1035 return (0);
1036 }
1037
1038 int
1039 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1040 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1041 {
1042 int error = 0;
1043 struct sysctl_req req;
1044
1045 bzero(&req, sizeof req);
1046
1047 req.td = td;
1048 req.flags = flags;
1049
1050 if (oldlenp) {
1051 req.oldlen = *oldlenp;
1052 }
1053 req.validlen = req.oldlen;
1054
1055 if (old) {
1056 req.oldptr= old;
1057 }
1058
1059 if (new != NULL) {
1060 req.newlen = newlen;
1061 req.newptr = new;
1062 }
1063
1064 req.oldfunc = sysctl_old_kernel;
1065 req.newfunc = sysctl_new_kernel;
1066 req.lock = REQ_LOCKED;
1067
1068 SYSCTL_LOCK();
1069
1070 error = sysctl_root(0, name, namelen, &req);
1071
1072 if (req.lock == REQ_WIRED && req.validlen > 0)
1073 vsunlock(req.oldptr, req.validlen);
1074
1075 SYSCTL_UNLOCK();
1076
1077 if (error && error != ENOMEM)
1078 return (error);
1079
1080 if (retval) {
1081 if (req.oldptr && req.oldidx > req.validlen)
1082 *retval = req.validlen;
1083 else
1084 *retval = req.oldidx;
1085 }
1086 return (error);
1087 }
1088
1089 int
1090 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1091 void *new, size_t newlen, size_t *retval, int flags)
1092 {
1093 int oid[CTL_MAXNAME];
1094 size_t oidlen, plen;
1095 int error;
1096
1097 oid[0] = 0; /* sysctl internal magic */
1098 oid[1] = 3; /* name2oid */
1099 oidlen = sizeof(oid);
1100
1101 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1102 (void *)name, strlen(name), &plen, flags);
1103 if (error)
1104 return (error);
1105
1106 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1107 new, newlen, retval, flags);
1108 return (error);
1109 }
1110
1111 /*
1112 * Transfer function to/from user space.
1113 */
1114 static int
1115 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1116 {
1117 int error = 0;
1118 size_t i, len, origidx;
1119
1120 origidx = req->oldidx;
1121 req->oldidx += l;
1122 if (req->oldptr == NULL)
1123 return (0);
1124 /*
1125 * If we have not wired the user supplied buffer and we are currently
1126 * holding locks, drop a witness warning, as it's possible that
1127 * write operations to the user page can sleep.
1128 */
1129 if (req->lock != REQ_WIRED)
1130 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1131 "sysctl_old_user()");
1132 i = l;
1133 len = req->validlen;
1134 if (len <= origidx)
1135 i = 0;
1136 else {
1137 if (i > len - origidx)
1138 i = len - origidx;
1139 error = copyout(p, (char *)req->oldptr + origidx, i);
1140 }
1141 if (error)
1142 return (error);
1143 if (i < l)
1144 return (ENOMEM);
1145 return (0);
1146 }
1147
1148 static int
1149 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1150 {
1151 int error;
1152
1153 if (!req->newptr)
1154 return (0);
1155 if (req->newlen - req->newidx < l)
1156 return (EINVAL);
1157 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1158 "sysctl_new_user()");
1159 error = copyin((char *)req->newptr + req->newidx, p, l);
1160 req->newidx += l;
1161 return (error);
1162 }
1163
1164 /*
1165 * Wire the user space destination buffer. If set to a value greater than
1166 * zero, the len parameter limits the maximum amount of wired memory.
1167 */
1168 int
1169 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1170 {
1171 int ret;
1172 size_t i, wiredlen;
1173 char *cp, dummy;
1174
1175 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1176 ret = 0;
1177 if (req->lock == REQ_LOCKED && req->oldptr &&
1178 req->oldfunc == sysctl_old_user) {
1179 if (wiredlen != 0) {
1180 ret = vslock(req->oldptr, wiredlen);
1181 if (ret != 0) {
1182 if (ret != ENOMEM)
1183 return (ret);
1184 wiredlen = 0;
1185 }
1186 /*
1187 * Touch all the wired pages to avoid PTE modified
1188 * bit emulation traps on Alpha while holding locks
1189 * in the sysctl handler.
1190 */
1191 for (i = (wiredlen + PAGE_SIZE - 1) / PAGE_SIZE,
1192 cp = req->oldptr; i > 0; i--, cp += PAGE_SIZE) {
1193 copyin(cp, &dummy, 1);
1194 copyout(&dummy, cp, 1);
1195 }
1196 }
1197 req->lock = REQ_WIRED;
1198 req->validlen = wiredlen;
1199 }
1200 return (0);
1201 }
1202
1203 int
1204 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1205 int *nindx, struct sysctl_req *req)
1206 {
1207 struct sysctl_oid *oid;
1208 int indx;
1209
1210 oid = SLIST_FIRST(&sysctl__children);
1211 indx = 0;
1212 while (oid && indx < CTL_MAXNAME) {
1213 if (oid->oid_number == name[indx]) {
1214 indx++;
1215 if (oid->oid_kind & CTLFLAG_NOLOCK)
1216 req->lock = REQ_UNLOCKED;
1217 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1218 if (oid->oid_handler != NULL ||
1219 indx == namelen) {
1220 *noid = oid;
1221 if (nindx != NULL)
1222 *nindx = indx;
1223 return (0);
1224 }
1225 oid = SLIST_FIRST(
1226 (struct sysctl_oid_list *)oid->oid_arg1);
1227 } else if (indx == namelen) {
1228 *noid = oid;
1229 if (nindx != NULL)
1230 *nindx = indx;
1231 return (0);
1232 } else {
1233 return (ENOTDIR);
1234 }
1235 } else {
1236 oid = SLIST_NEXT(oid, oid_link);
1237 }
1238 }
1239 return (ENOENT);
1240 }
1241
1242 /*
1243 * Traverse our tree, and find the right node, execute whatever it points
1244 * to, and return the resulting error code.
1245 */
1246
1247 static int
1248 sysctl_root(SYSCTL_HANDLER_ARGS)
1249 {
1250 struct sysctl_oid *oid;
1251 int error, indx, lvl;
1252
1253 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1254 if (error)
1255 return (error);
1256
1257 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1258 /*
1259 * You can't call a sysctl when it's a node, but has
1260 * no handler. Inform the user that it's a node.
1261 * The indx may or may not be the same as namelen.
1262 */
1263 if (oid->oid_handler == NULL)
1264 return (EISDIR);
1265 }
1266
1267 /* Is this sysctl writable? */
1268 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1269 return (EPERM);
1270
1271 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1272
1273 /* Is this sysctl sensitive to securelevels? */
1274 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1275 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1276 error = securelevel_gt(req->td->td_ucred, lvl);
1277 if (error)
1278 return (error);
1279 }
1280
1281 /* Is this sysctl writable by only privileged users? */
1282 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1283 if (oid->oid_kind & CTLFLAG_PRISON)
1284 error = priv_check(req->td, PRIV_SYSCTL_WRITEJAIL);
1285 else
1286 error = priv_check(req->td, PRIV_SYSCTL_WRITE);
1287 if (error)
1288 return (error);
1289 }
1290
1291 if (!oid->oid_handler)
1292 return (EINVAL);
1293
1294 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1295 arg1 = (int *)arg1 + indx;
1296 arg2 -= indx;
1297 } else {
1298 arg1 = oid->oid_arg1;
1299 arg2 = oid->oid_arg2;
1300 }
1301 #ifdef MAC
1302 error = mac_check_system_sysctl(req->td->td_ucred, oid, arg1, arg2,
1303 req);
1304 if (error != 0)
1305 return (error);
1306 #endif
1307 error = oid->oid_handler(oid, arg1, arg2, req);
1308
1309 return (error);
1310 }
1311
1312 #ifndef _SYS_SYSPROTO_H_
1313 struct sysctl_args {
1314 int *name;
1315 u_int namelen;
1316 void *old;
1317 size_t *oldlenp;
1318 void *new;
1319 size_t newlen;
1320 };
1321 #endif
1322 int
1323 __sysctl(struct thread *td, struct sysctl_args *uap)
1324 {
1325 int error, name[CTL_MAXNAME];
1326 size_t j;
1327
1328 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1329 return (EINVAL);
1330
1331 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1332 if (error)
1333 return (error);
1334
1335 mtx_lock(&Giant);
1336
1337 error = userland_sysctl(td, name, uap->namelen,
1338 uap->old, uap->oldlenp, 0,
1339 uap->new, uap->newlen, &j, 0);
1340 if (error && error != ENOMEM)
1341 goto done2;
1342 if (uap->oldlenp) {
1343 int i = copyout(&j, uap->oldlenp, sizeof(j));
1344 if (i)
1345 error = i;
1346 }
1347 done2:
1348 mtx_unlock(&Giant);
1349 return (error);
1350 }
1351
1352 /*
1353 * This is used from various compatibility syscalls too. That's why name
1354 * must be in kernel space.
1355 */
1356 int
1357 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1358 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1359 int flags)
1360 {
1361 int error = 0;
1362 struct sysctl_req req;
1363
1364 bzero(&req, sizeof req);
1365
1366 req.td = td;
1367 req.flags = flags;
1368
1369 if (oldlenp) {
1370 if (inkernel) {
1371 req.oldlen = *oldlenp;
1372 } else {
1373 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1374 if (error)
1375 return (error);
1376 }
1377 }
1378 req.validlen = req.oldlen;
1379
1380 if (old) {
1381 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1382 return (EFAULT);
1383 req.oldptr= old;
1384 }
1385
1386 if (new != NULL) {
1387 if (!useracc(new, newlen, VM_PROT_READ))
1388 return (EFAULT);
1389 req.newlen = newlen;
1390 req.newptr = new;
1391 }
1392
1393 req.oldfunc = sysctl_old_user;
1394 req.newfunc = sysctl_new_user;
1395 req.lock = REQ_LOCKED;
1396
1397 SYSCTL_LOCK();
1398
1399 for (;;) {
1400 req.oldidx = 0;
1401 req.newidx = 0;
1402 error = sysctl_root(0, name, namelen, &req);
1403 if (error != EAGAIN)
1404 break;
1405 uio_yield();
1406 }
1407
1408 if (req.lock == REQ_WIRED && req.validlen > 0)
1409 vsunlock(req.oldptr, req.validlen);
1410
1411 SYSCTL_UNLOCK();
1412
1413 if (error && error != ENOMEM)
1414 return (error);
1415
1416 if (retval) {
1417 if (req.oldptr && req.oldidx > req.validlen)
1418 *retval = req.validlen;
1419 else
1420 *retval = req.oldidx;
1421 }
1422 return (error);
1423 }
1424
1425 #ifdef COMPAT_43
1426 #include <sys/socket.h>
1427 #include <vm/vm_param.h>
1428
1429 #define KINFO_PROC (0<<8)
1430 #define KINFO_RT (1<<8)
1431 #define KINFO_VNODE (2<<8)
1432 #define KINFO_FILE (3<<8)
1433 #define KINFO_METER (4<<8)
1434 #define KINFO_LOADAVG (5<<8)
1435 #define KINFO_CLOCKRATE (6<<8)
1436
1437 /* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
1438 #define KINFO_BSDI_SYSINFO (101<<8)
1439
1440 /*
1441 * XXX this is bloat, but I hope it's better here than on the potentially
1442 * limited kernel stack... -Peter
1443 */
1444
1445 static struct {
1446 int bsdi_machine; /* "i386" on BSD/386 */
1447 /* ^^^ this is an offset to the string, relative to the struct start */
1448 char *pad0;
1449 long pad1;
1450 long pad2;
1451 long pad3;
1452 u_long pad4;
1453 u_long pad5;
1454 u_long pad6;
1455
1456 int bsdi_ostype; /* "BSD/386" on BSD/386 */
1457 int bsdi_osrelease; /* "1.1" on BSD/386 */
1458 long pad7;
1459 long pad8;
1460 char *pad9;
1461
1462 long pad10;
1463 long pad11;
1464 int pad12;
1465 long pad13;
1466 quad_t pad14;
1467 long pad15;
1468
1469 struct timeval pad16;
1470 /* we dont set this, because BSDI's uname used gethostname() instead */
1471 int bsdi_hostname; /* hostname on BSD/386 */
1472
1473 /* the actual string data is appended here */
1474
1475 } bsdi_si;
1476
1477 /*
1478 * this data is appended to the end of the bsdi_si structure during copyout.
1479 * The "char *" offsets are relative to the base of the bsdi_si struct.
1480 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
1481 * should not exceed the length of the buffer here... (or else!! :-)
1482 */
1483 static char bsdi_strings[80]; /* It had better be less than this! */
1484
1485 #ifndef _SYS_SYSPROTO_H_
1486 struct getkerninfo_args {
1487 int op;
1488 char *where;
1489 size_t *size;
1490 int arg;
1491 };
1492 #endif
1493 int
1494 ogetkerninfo(struct thread *td, struct getkerninfo_args *uap)
1495 {
1496 int error, name[6];
1497 size_t size;
1498 u_int needed = 0;
1499
1500 mtx_lock(&Giant);
1501
1502 switch (uap->op & 0xff00) {
1503
1504 case KINFO_RT:
1505 name[0] = CTL_NET;
1506 name[1] = PF_ROUTE;
1507 name[2] = 0;
1508 name[3] = (uap->op & 0xff0000) >> 16;
1509 name[4] = uap->op & 0xff;
1510 name[5] = uap->arg;
1511 error = userland_sysctl(td, name, 6, uap->where, uap->size,
1512 0, 0, 0, &size, 0);
1513 break;
1514
1515 case KINFO_VNODE:
1516 name[0] = CTL_KERN;
1517 name[1] = KERN_VNODE;
1518 error = userland_sysctl(td, name, 2, uap->where, uap->size,
1519 0, 0, 0, &size, 0);
1520 break;
1521
1522 case KINFO_PROC:
1523 name[0] = CTL_KERN;
1524 name[1] = KERN_PROC;
1525 name[2] = uap->op & 0xff;
1526 name[3] = uap->arg;
1527 error = userland_sysctl(td, name, 4, uap->where, uap->size,
1528 0, 0, 0, &size, 0);
1529 break;
1530
1531 case KINFO_FILE:
1532 name[0] = CTL_KERN;
1533 name[1] = KERN_FILE;
1534 error = userland_sysctl(td, name, 2, uap->where, uap->size,
1535 0, 0, 0, &size, 0);
1536 break;
1537
1538 case KINFO_METER:
1539 name[0] = CTL_VM;
1540 name[1] = VM_TOTAL;
1541 error = userland_sysctl(td, name, 2, uap->where, uap->size,
1542 0, 0, 0, &size, 0);
1543 break;
1544
1545 case KINFO_LOADAVG:
1546 name[0] = CTL_VM;
1547 name[1] = VM_LOADAVG;
1548 error = userland_sysctl(td, name, 2, uap->where, uap->size,
1549 0, 0, 0, &size, 0);
1550 break;
1551
1552 case KINFO_CLOCKRATE:
1553 name[0] = CTL_KERN;
1554 name[1] = KERN_CLOCKRATE;
1555 error = userland_sysctl(td, name, 2, uap->where, uap->size,
1556 0, 0, 0, &size, 0);
1557 break;
1558
1559 case KINFO_BSDI_SYSINFO: {
1560 /*
1561 * this is pretty crude, but it's just enough for uname()
1562 * from BSDI's 1.x libc to work.
1563 *
1564 * *size gives the size of the buffer before the call, and
1565 * the amount of data copied after a successful call.
1566 * If successful, the return value is the amount of data
1567 * available, which can be larger than *size.
1568 *
1569 * BSDI's 2.x product apparently fails with ENOMEM if *size
1570 * is too small.
1571 */
1572
1573 u_int left;
1574 char *s;
1575
1576 bzero((char *)&bsdi_si, sizeof(bsdi_si));
1577 bzero(bsdi_strings, sizeof(bsdi_strings));
1578
1579 s = bsdi_strings;
1580
1581 bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
1582 strcpy(s, ostype);
1583 s += strlen(s) + 1;
1584
1585 bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
1586 strcpy(s, osrelease);
1587 s += strlen(s) + 1;
1588
1589 bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
1590 strcpy(s, machine);
1591 s += strlen(s) + 1;
1592
1593 needed = sizeof(bsdi_si) + (s - bsdi_strings);
1594
1595 if ((uap->where == NULL) || (uap->size == NULL)) {
1596 /* process is asking how much buffer to supply.. */
1597 size = needed;
1598 error = 0;
1599 break;
1600 }
1601
1602 if ((error = copyin(uap->size, &size, sizeof(size))) != 0)
1603 break;
1604
1605 /* if too much buffer supplied, trim it down */
1606 if (size > needed)
1607 size = needed;
1608
1609 /* how much of the buffer is remaining */
1610 left = size;
1611
1612 if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
1613 break;
1614
1615 /* is there any point in continuing? */
1616 if (left > sizeof(bsdi_si)) {
1617 left -= sizeof(bsdi_si);
1618 error = copyout(&bsdi_strings,
1619 uap->where + sizeof(bsdi_si), left);
1620 }
1621 break;
1622 }
1623
1624 default:
1625 error = EOPNOTSUPP;
1626 break;
1627 }
1628 if (error == 0) {
1629 td->td_retval[0] = needed ? needed : size;
1630 if (uap->size) {
1631 error = copyout(&size, uap->size, sizeof(size));
1632 }
1633 }
1634 mtx_unlock(&Giant);
1635 return (error);
1636 }
1637 #endif /* COMPAT_43 */
Cache object: e9743fa2d932d494f6e7ffea8141f660
|