📄 kern_sysctl.c
字号:
static intsysctl_sysctl_name(SYSCTL_HANDLER_ARGS){ int *name = (int *) arg1; u_int namelen = arg2; int error = 0; struct sysctl_oid *oid; struct sysctl_oid_list *lsp = &sysctl__children, *lsp2; char buf[10]; while (namelen) { if (!lsp) { snprintf(buf,sizeof(buf),"%d",*name); if (req->oldidx) error = SYSCTL_OUT(req, ".", 1); if (!error) error = SYSCTL_OUT(req, buf, strlen(buf)); if (error) return (error); namelen--; name++; continue; } lsp2 = 0; SLIST_FOREACH(oid, lsp, oid_link) { if (oid->oid_number != *name) continue; if (req->oldidx) error = SYSCTL_OUT(req, ".", 1); if (!error) error = SYSCTL_OUT(req, oid->oid_name, strlen(oid->oid_name)); if (error) return (error); namelen--; name++; if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) break; if (oid->oid_handler) break; lsp2 = (struct sysctl_oid_list *)oid->oid_arg1; break; } lsp = lsp2; } return (SYSCTL_OUT(req, "", 1));}SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");static intsysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, int *next, int *len, int level, struct sysctl_oid **oidpp){ struct sysctl_oid *oidp; *len = level; SLIST_FOREACH(oidp, lsp, oid_link) { *next = oidp->oid_number; *oidpp = oidp; if (oidp->oid_kind & CTLFLAG_SKIP) continue; if (!namelen) { if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) return 0; if (oidp->oid_handler) /* We really should call the handler here...*/ return 0; lsp = (struct sysctl_oid_list *)oidp->oid_arg1; if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, len, level+1, oidpp)) return 0; goto next; } if (oidp->oid_number < *name) continue; if (oidp->oid_number > *name) { if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) return 0; if (oidp->oid_handler) return 0; lsp = (struct sysctl_oid_list *)oidp->oid_arg1; if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, len, level+1, oidpp)) return (0); goto next; } if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) continue; if (oidp->oid_handler) continue; lsp = (struct sysctl_oid_list *)oidp->oid_arg1; if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, len, level+1, oidpp)) return (0); next: namelen = 1; *len = level; } return 1;}static intsysctl_sysctl_next(SYSCTL_HANDLER_ARGS){ int *name = (int *) arg1; u_int namelen = arg2; int i, j, error; struct sysctl_oid *oid; struct sysctl_oid_list *lsp = &sysctl__children; int newoid[CTL_MAXNAME]; i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid); if (i) return ENOENT; error = SYSCTL_OUT(req, newoid, j * sizeof (int)); return (error);}SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");static intname2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp){ int i; struct sysctl_oid *oidp; struct sysctl_oid_list *lsp = &sysctl__children; char *p; if (!*name) return ENOENT; p = name + strlen(name) - 1 ; if (*p == '.') *p = '\0'; *len = 0; for (p = name; *p && *p != '.'; p++) ; i = *p; if (i == '.') *p = '\0'; oidp = SLIST_FIRST(lsp); while (oidp && *len < CTL_MAXNAME) { if (strcmp(name, oidp->oid_name)) { oidp = SLIST_NEXT(oidp, oid_link); continue; } *oid++ = oidp->oid_number; (*len)++; if (!i) { if (oidpp) *oidpp = oidp; return (0); } if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) break; if (oidp->oid_handler) break; lsp = (struct sysctl_oid_list *)oidp->oid_arg1; oidp = SLIST_FIRST(lsp); name = p+1; for (p = name; *p && *p != '.'; p++) ; i = *p; if (i == '.') *p = '\0'; } return ENOENT;}static intsysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS){ char *p; int error, oid[CTL_MAXNAME], len; struct sysctl_oid *op = 0; if (!req->newlen) return ENOENT; if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */ return (ENAMETOOLONG); p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK); error = SYSCTL_IN(req, p, req->newlen); if (error) { free(p, M_SYSCTL); return (error); } p [req->newlen] = '\0'; error = name2oid(p, oid, &len, &op); free(p, M_SYSCTL); if (error) return (error); error = SYSCTL_OUT(req, oid, len * sizeof *oid); return (error);}SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0, sysctl_sysctl_name2oid, "I", "");static intsysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS){ struct sysctl_oid *oid; int error; error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); if (error) return (error); if (!oid->oid_fmt) return (ENOENT); error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind)); if (error) return (error); error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1); return (error);}SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");static intsysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS){ struct sysctl_oid *oid; int error; error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); if (error) return (error); if (!oid->descr) return (ENOENT); error = SYSCTL_OUT(req, oid->descr, strlen(oid->descr) + 1); return (error);}SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");/* * Default "handler" functions. *//* * Handle an int, signed or unsigned. * Two cases: * a variable: point arg1 at it. * a constant: pass it in arg2. */intsysctl_handle_int(SYSCTL_HANDLER_ARGS){ int tmpout, error = 0; /* * Attempt to get a coherent snapshot by making a copy of the data. */ if (arg1) tmpout = *(int *)arg1; else tmpout = arg2; error = SYSCTL_OUT(req, &tmpout, sizeof(int)); if (error || !req->newptr) return (error); if (!arg1) error = EPERM; else error = SYSCTL_IN(req, arg1, sizeof(int)); return (error);}/* * Handle a long, signed or unsigned. arg1 points to it. */intsysctl_handle_long(SYSCTL_HANDLER_ARGS){ int error = 0; long tmpout; /* * Attempt to get a coherent snapshot by making a copy of the data. */ if (!arg1) return (EINVAL); tmpout = *(long *)arg1; error = SYSCTL_OUT(req, &tmpout, sizeof(long)); if (error || !req->newptr) return (error); error = SYSCTL_IN(req, arg1, sizeof(long)); return (error);}/* * Handle our generic '\0' terminated 'C' string. * Two cases: * a variable string: point arg1 at it, arg2 is max length. * a constant string: point arg1 at it, arg2 is zero. */intsysctl_handle_string(SYSCTL_HANDLER_ARGS){ int error=0; char *tmparg; size_t outlen; /* * Attempt to get a coherent snapshot by copying to a * temporary kernel buffer. */retry: outlen = strlen((char *)arg1)+1; tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK); if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) { free(tmparg, M_SYSCTLTMP); goto retry; } error = SYSCTL_OUT(req, tmparg, outlen); free(tmparg, M_SYSCTLTMP); if (error || !req->newptr) return (error); if ((req->newlen - req->newidx) >= arg2) { error = EINVAL; } else { arg2 = (req->newlen - req->newidx); error = SYSCTL_IN(req, arg1, arg2); ((char *)arg1)[arg2] = '\0'; } return (error);}/* * Handle any kind of opaque data. * arg1 points to it, arg2 is the size. */intsysctl_handle_opaque(SYSCTL_HANDLER_ARGS){ int error; void *tmparg; /* * Attempt to get a coherent snapshot, either by wiring the * user space buffer or copying to a temporary kernel buffer * depending on the size of the data. */ if (arg2 > PAGE_SIZE) { sysctl_wire_old_buffer(req, arg2); error = SYSCTL_OUT(req, arg1, arg2); } else { tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK); bcopy(arg1, tmparg, arg2); error = SYSCTL_OUT(req, tmparg, arg2); free(tmparg, M_SYSCTLTMP); } if (error || !req->newptr) return (error); error = SYSCTL_IN(req, arg1, arg2); return (error);}/* * Transfer functions to/from kernel space. * XXX: rather untested at this point */static intsysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l){ size_t i = 0; if (req->oldptr) { i = l; if (req->oldlen <= req->oldidx) i = 0; else if (i > req->oldlen - req->oldidx) i = req->oldlen - req->oldidx; if (i > 0) bcopy(p, (char *)req->oldptr + req->oldidx, i); } req->oldidx += l; if (req->oldptr && i != l) return (ENOMEM); return (0);}static intsysctl_new_kernel(struct sysctl_req *req, void *p, size_t l){ if (!req->newptr) return 0; if (req->newlen - req->newidx < l) return (EINVAL); bcopy((char *)req->newptr + req->newidx, p, l); req->newidx += l; return (0);}intkernel_sysctl(struct thread *td, int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval){ int error = 0; struct sysctl_req req; bzero(&req, sizeof req); req.td = td; if (oldlenp) { req.oldlen = *oldlenp; } if (old) { req.oldptr= old; } if (new != NULL) { req.newlen = newlen; req.newptr = new; } req.oldfunc = sysctl_old_kernel; req.newfunc = sysctl_new_kernel; req.lock = 1; SYSCTL_LOCK(); error = sysctl_root(0, name, namelen, &req); if (req.lock == 2)#ifdef __rtems__ printf ("kern_sysctl: vsunlock needs to be called!\n");#else vsunlock(req.oldptr, req.oldlen);#endif SYSCTL_UNLOCK(); if (error && error != ENOMEM) return (error); if (retval) { if (req.oldptr && req.oldidx > req.oldlen) *retval = req.oldlen; else *retval = req.oldidx; } return (error);}intkernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval){ int oid[CTL_MAXNAME]; size_t oidlen, plen; int error; oid[0] = 0; /* sysctl internal magic */ oid[1] = 3; /* name2oid */ oidlen = sizeof(oid); error = kernel_sysctl(td, oid, 2, oid, &oidlen, (void *)name, strlen(name), &plen); if (error) return (error); error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp, new, newlen, retval); return (error);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -