Changeset 1917


Ignore:
Timestamp:
Apr 26, 2005, 2:48:42 AM (21 years ago)
Author:
bird
Message:

Heavy hacking.

Location:
trunk/src/emx/src/lib/misc
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/emx/src/lib/misc/sysctl.c

    • Property cvs2svn:cvs-rev changed from 1.1 to 1.2
    r1916 r1917  
    3636 */
    3737
     38
    3839#include <sys/cdefs.h>
    39 __FBSDID("$FreeBSD: src/sys/kern/kern_sysctl.c,v 1.159 2004/07/28 06:42:41 kan Exp $");
    40 
    41 #include "opt_compat.h"
    42 #include "opt_mac.h"
     40//__FBSDID("$FreeBSD: src/sys/kern/kern_sysctl.c,v 1.159 2004/07/28 06:42:41 kan Exp $");
    4341
    4442#include <sys/param.h>
    45 #include <sys/systm.h>
    46 #include <sys/kernel.h>
     43#define _KERNEL
    4744#include <sys/sysctl.h>
    48 #include <sys/mac.h>
    49 #include <sys/malloc.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 <vm/vm.h>
    56 #include <vm/vm_extern.h>
    57 
    58 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
    59 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
    60 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
     45#include <386/builtin.h>
     46#include <sys/fmutex.h>
     47#include <sys/smutex.h>
     48#include <errno.h>
     49#include <string.h>
     50#include <stdio.h>
     51
     52#define __LIBC_LOG_GROUP __LIBC_LOG_GRP_MISC
     53#include <InnoTekLIBC/logstrict.h>
     54#undef printf
     55#define printf LIBCLOG_MSG2
     56#undef panic
     57#define panic LIBC_ASSERTM_FAILED
     58#undef copyout
     59#define copyout(x,y,z)  !memcpy((y),(x),(z))
     60#undef copyin
     61#define copyin(x,y,z)   !memcpy((y),(x),(z))
     62#undef securelevel_gt
     63#define securelevel_gt(cr, level) 0
    6164
    6265/*
    6366 * Locking - this locks the sysctl tree in memory.
    6467 */
    65 static struct sx sysctllock;
    66 
    67 #define SYSCTL_LOCK()           sx_xlock(&sysctllock)
    68 #define SYSCTL_UNLOCK()         sx_xunlock(&sysctllock)
    69 #define SYSCTL_INIT()           sx_init(&sysctllock, "sysctl lock")
     68static x sysctllock;
     69
     70#define SYSCTL_LOCK()           )
     71#define SYSCTL_UNLOCK()         (&sysctllock)
     72#define SYSCTL_INIT()           , "sysctl lock")
    7073
    7174static int sysctl_root(SYSCTL_HANDLER_ARGS);
     
    9295 */
    9396
    94 void
     97void
    9598sysctl_register_oid(struct sysctl_oid *oidp)
    9699{
     
    149152}
    150153
     154
    151155void
    152156sysctl_unregister_oid(struct sysctl_oid *oidp)
     
    169173        }
    170174
    171         /* 
     175        /*
    172176         * This can happen when a module fails to register and is
    173177         * being unloaded afterwards.  It should not be a panic()
     
    177181                printf("%s: failed to unregister sysctl\n", __func__);
    178182}
     183
     184
     185
     186
    179187
    180188/* Initialize a new context to keep track of dynamically added sysctls. */
     
    288296}
    289297
     298
     299
     300
    290301/*
    291302 * Remove dynamically created sysctl trees.
     
    431442        return (0);
    432443}
     444
     445
    433446
    434447/*
    435448 * Register the kernel's oids on startup.
    436449 */
    437 SET_DECLARE(sysctl_set, struct sysctl_oid);
    438 
    439450static void
    440451sysctl_register_all(void *arg)
     
    443454
    444455        SYSCTL_INIT();
    445         SET_FOREACH(oidp, sysctl_set)
    446                 sysctl_register_oid(*oidp);
    447 }
    448 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
    449 
     456        extern int __sysctl_set__;
     457        int *ptr = &__sysctl_set__;
     458        if (*ptr == -2)
     459            for (oidp = (struct sysctl_oid **)(ptr + 1); *oidp != NULL; oidp++)
     460                sysctl_register_oid(*oidp);
     461        else
     462        {
     463            if (*ptr == -1) --ptr;      /* Fix GNU ld bug */
     464            int n = *ptr++;              /* Get size of vector */
     465            if (*ptr == -1)             /* First element must be -1, see crt0.s */
     466                for (oidp = (struct sysctl_oid **)(ptr + 1); n > 1; oidp++, n--)
     467                    sysctl_register_oid(*oidp);
     468        }
     469
     470}
     471//SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
     472
     473#if 0 /* no debug stuff just yet */
    450474/*
    451475 * "Staff-functions"
    452476 *
    453  * These functions implement a presently undocumented interface 
     477 * These functions implement a presently undocumented interface
    454478 * used by the sysctl program to walk the tree, and get the type
    455479 * so it can print the value.
     
    519543SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
    520544        0, 0, sysctl_sysctl_debug, "-", "");
     545
    521546
    522547static int
     
    559584                        name++;
    560585
    561                         if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
     586                        if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
    562587                                break;
    563588
     
    576601
    577602static int
    578 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 
     603sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
    579604        int *next, int *len, int level, struct sysctl_oid **oidpp)
    580605{
     
    590615
    591616                if (!namelen) {
    592                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
     617                        if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
    593618                                return 0;
    594                         if (oidp->oid_handler) 
     619                        if (oidp->oid_handler)
    595620                                /* We really should call the handler here...*/
    596621                                return 0;
    597622                        lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
    598                         if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 
     623                        if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
    599624                                len, level+1, oidpp))
    600625                                return 0;
     
    611636                                return 0;
    612637                        lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
    613                         if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 
     638                        if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
    614639                                next+1, len, level+1, oidpp))
    615640                                return (0);
     
    623648
    624649                lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
    625                 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 
     650                if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
    626651                        len, level+1, oidpp))
    627652                        return (0);
     
    670695        *len = 0;
    671696
    672         for (p = name; *p && *p != '.'; p++) 
     697        for (p = name; *p && *p != '.'; p++)
    673698                ;
    674699        i = *p;
     
    701726                oidp = SLIST_FIRST(lsp);
    702727                name = p+1;
    703                 for (p = name; *p && *p != '.'; p++) 
     728                for (p = name; *p && *p != '.'; p++)
    704729                                ;
    705730                i = *p;
     
    717742        struct sysctl_oid *op = 0;
    718743
    719         if (!req->newlen) 
     744        if (!req->newlen)
    720745                return ENOENT;
    721746        if (req->newlen >= MAXPATHLEN)  /* XXX arbitrary, undocumented */
    722747                return (ENAMETOOLONG);
    723748
    724         p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
     749        p = malloc(req->newlen+1);
    725750
    726751        error = SYSCTL_IN(req, p, req->newlen);
    727752        if (error) {
    728                 free(p, M_SYSCTL);
     753                free(p);
    729754                return (error);
    730755        }
     
    734759        error = name2oid(p, oid, &len, &op);
    735760
    736         free(p, M_SYSCTL);
     761        free(p);
    737762
    738763        if (error)
     
    743768}
    744769
    745 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0, 
     770SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
    746771        sysctl_sysctl_name2oid, "I", "");
     772
     773
     774
     775
    747776
    748777static int
     
    866895retry:
    867896        outlen = strlen((char *)arg1)+1;
    868         tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
     897        tmparg = malloc(outlen);
    869898
    870899        if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
    871                 free(tmparg, M_SYSCTLTMP);
     900                free(tmparg);
    872901                goto retry;
    873902        }
    874903
    875904        error = SYSCTL_OUT(req, tmparg, outlen);
    876         free(tmparg, M_SYSCTLTMP);
     905        free(tmparg);
    877906
    878907        if (error || !req->newptr)
     
    898927sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
    899928{
    900         int error, tries;
    901         u_int generation;
     929        int error;
     930      u_int generation;
    902931        struct sysctl_req req2;
    903932
     
    909938         * If we encounter an error, stop immediately.
    910939         */
    911         tries = 0;
     940      tries = 0;
    912941        req2 = *req;
    913 retry:
    914         generation = curthread->td_generation;
     942retry:
     943        generation = curthread->td_generation;
    915944        error = SYSCTL_OUT(req, arg1, arg2);
    916945        if (error)
    917946                return (error);
    918         tries++;
    919         if (generation != curthread->td_generation && tries < 3) {
    920                 *req = req2;
    921                 goto retry;
    922         }
     947        tries++;
     948        if (generation != curthread->td_generation && tries < 3) {
     949              *req = req2;
     950              goto retry;
     951        }
    923952
    924953        error = SYSCTL_IN(req, arg1, arg2);
     
    927956}
    928957
     958
    929959/*
    930960 * Transfer functions to/from kernel space.
     
    9971027        error = sysctl_root(0, name, namelen, &req);
    9981028
    999         if (req.lock == REQ_WIRED && req.validlen > 0)
    1000                 vsunlock(req.oldptr, req.validlen);
     1029        if (req.lock == REQ_WIRED && req.validlen > 0)
     1030              vsunlock(req.oldptr, req.validlen);
    10011031
    10021032        SYSCTL_UNLOCK();
     
    10351065        return (error);
    10361066}
     1067
    10371068
    10381069/*
     
    10491080        if (req->oldptr == NULL)
    10501081                return (0);
    1051         if (req->lock == REQ_LOCKED)
    1052                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
    1053                     "sysctl_old_user()");
     1082        if (req->lock == REQ_LOCKED)
     1083              WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
     1084                  "sysctl_old_user()");
    10541085        i = l;
    10551086        len = req->validlen;
     
    10821113}
    10831114
     1115
    10841116/*
    10851117 * Wire the user space destination buffer.  If set to a value greater than
     
    11131145        return (0);
    11141146}
    1115 
    1116 int
     1147#endif /* don't need you.. */
     1148
     1149static int
    11171150sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
    11181151    int *nindx, struct sysctl_req *req)
     
    11821215                return (EPERM);
    11831216
    1184         KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
     1217        KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
    11851218
    11861219        /* Is this sysctl sensitive to securelevels? */
     
    11941227        /* Is this sysctl writable by only privileged users? */
    11951228        if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
     1229
    11961230                int flags;
    11971231
     
    12031237                if (error)
    12041238                        return (error);
     1239
    12051240        }
    12061241
     
    12251260        return (error);
    12261261}
     1262
     1263
     1264
     1265
    12271266
    12281267#ifndef _SYS_SYSPROTO_H_
     
    12371276#endif
    12381277
     1278
    12391279/*
    12401280 * MPSAFE
    12411281 */
    1242 int
     1282int
    12431283__sysctl(struct thread *td, struct sysctl_args *uap)
    12441284{
     
    12691309        return (error);
    12701310}
     1311
    12711312
    12721313/*
     
    12741315 * must be in kernel space.
    12751316 */
    1276 int
     1317int
    12771318userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
    12781319    size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
     
    12971338
    12981339        if (old) {
    1299                 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
    1300                         return (EFAULT);
     1340                if (!useracc(old, req.oldlen, VM_PROT_WRITE))
     1341                      return (EFAULT);
    13011342                req.oldptr= old;
    13021343        }
    13031344
    13041345        if (new != NULL) {
    1305                 if (!useracc(new, req.newlen, VM_PROT_READ))
    1306                         return (EFAULT);
     1346                if (!useracc(new, req.newlen, VM_PROT_READ))
     1347                      return (EFAULT);
    13071348                req.newlen = newlen;
    13081349                req.newptr = new;
     
    13211362        } while (error == EAGAIN);
    13221363
    1323         if (req.lock == REQ_WIRED && req.validlen > 0)
    1324                 vsunlock(req.oldptr, req.validlen);
     1364        if (req.lock == REQ_WIRED && req.validlen > 0)
     1365              vsunlock(req.oldptr, req.validlen);
    13251366
    13261367        SYSCTL_UNLOCK();
     
    15541595}
    15551596#endif /* COMPAT_43 */
     1597
     1598
     1599
     1600
     1601
     1602
     1603
     1604
     1605
     1606
     1607
     1608
     1609
     1610
     1611
     1612
     1613
     1614
     1615
     1616
     1617
     1618
     1619
     1620
     1621
     1622
     1623
     1624
     1625
     1626
     1627
     1628
     1629
     1630
     1631
     1632
     1633
     1634
     1635
     1636
     1637
     1638
     1639