8000 minix/minix/lib/libsys/rmib.c at master · 1170300812/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
1089 lines (919 loc) · 29.9 KB

rmib.c

File metadata and controls

1089 lines (919 loc) · 29.9 KB
call.call_endpt = m_in->m_mib_lsys_call.user_endpt;
call.call_oname = name;
call.call_name = &name[prefixlen];
call.call_namelen = namelen;
call.call_flags = m_in->m_mib_lsys_call.flags;
call.call_rootver = m_in->m_mib_lsys_call.root_ver;
call.call_treever = m_in->m_mib_lsys_call.tree_ver;
/*
* Dispatch the call.
*/
for (rparent = rnode; call.call_namelen > 0; rparent = rnode) {
id = call.call_name[0];
call.call_name++;
call.call_namelen--;
assert(SYSCTL_TYPE(rparent->rnode_flags) == CTLTYPE_NODE);
/* Check for meta-identifiers. */
if (id < 0) {
/*
* A meta-identifier must always be the last name
* component.
*/
if (call.call_namelen > 0)
return EINVAL;
switch (id) {
case CTL_QUERY:
return rmib_query(&call, rparent, oldp, newp);
case CTL_DESCRIBE:
return rmib_describe(&call, rparent, oldp,
newp);
case CTL_CREATE:
case CTL_DESTROY:
/* We support fully static subtrees only. */
return EPERM;
default:
return EOPNOTSUPP;
}
}
/* Locate the child node. */
if ((rnode = rmib_lookup(rparent, id)) == NULL)
return ENOENT;
/* Check if access is permitted at this level. */
if ((rnode->rnode_flags & CTLFLAG_PRIVATE) &&
!(call.call_flags & RMIB_FLAG_AUTH))
return EPERM;
/*
* Is this a leaf node, and/or is this node handled by a
* function? If either is true, resolution ends at this level.
*/
is_leaf = (SYSCTL_TYPE(rnode->rnode_flags) != CTLTYPE_NODE);
has_func = (rnode->rnode_func != NULL);
/*
* The name may be longer only if the node is not a leaf. That
* also applies to leaves with functions, so check this first.
*/
if (is_leaf && call.call_namelen > 0)
return ENOTDIR;
/*
* If resolution indeed ends here, and the user supplied new
* data, check if writing is allowed.
*/
if ((is_leaf || has_func) && newp != NULL) {
if (!(rnode->rnode_flags & CTLFLAG_READWRITE))
return EPERM;
if (!(rnode->rnode_flags & CTLFLAG_ANYWRITE) &&
!(call.call_flags & RMIB_FLAG_AUTH))
return EPERM;
}
/* If this node has a handler function, let it do the work. */
if (has_func)
return rnode->rnode_func(&call, rnode, oldp, newp);
/* For regular data leaf nodes, handle generic access. */
if (is_leaf)
return rmib_readwrite(&call, rnode, oldp, newp);
/* No function and not a leaf? Descend further. */
}
/* If we get here, the name refers to a node array. */
return EISDIR;
}
/*
* Initialize the given node and recursively all its node-type children,
* assigning the proper child length value to each of them.
*/
static void
rmib_init(struct rmib_node * rparent)
{
struct rmib_node *rnode;
unsigned int i;
for (i = 0; i < rparent->rnode_size; i++) {
if (rparent->rnode_flags & CTLFLAG_SPARSE) {
/* Indirect lists must be sorted ascending by ID. */
assert(i == 0 || rparent->rnode_icptr[i].rindir_id >
rparent->rnode_icptr[i - 1].rindir_id);
rnode = rparent->rnode_icptr[i].rindir_node;
} else {
rnode = &rparent->rnode_cptr[i];
if (rnode->rnode_flags == 0)
continue;
}
rparent->rnode_clen++;
if (SYSCTL_TYPE(rnode->rnode_flags) == CTLTYPE_NODE)
rmib_init(rnode); /* recurse */
}
}
/*
* Request that the MIB service (re)mount the subtree identified by the given
* identifier. This is a one-way request, so we never hear whether mounting
* succeeds. There is not that much we can do if it fails anyway though.
*/
static void
rmib_send_reg(int id)
{
message m;
int r;
memset(&m, 0, sizeof(m));
m.m_type = MIB_REGISTER;
m.m_lsys_mib_register.root_id = id;
m.m_lsys_mib_register.flags = SYSCTL_VERSION |
(rnodes[id].rno_node->rnode_flags & ~CTLFLAG_SPARSE);
m.m_lsys_mib_register.csize = rnodes[id].rno_node->rnode_size;
m.m_lsys_mib_register.clen = rnodes[id].rno_node->rnode_clen;
m.m_lsys_mib_register.miblen = rnodes[id].rno_namelen;
memcpy(m.m_lsys_mib_register.mib, rnodes[id].rno_name,
sizeof(rnodes[id].rno_name[0]) * rnodes[id].rno_namelen);
if ((r = asynsend3(MIB_PROC_NR, &m, AMF_NOREPLY)) != OK)
panic("asynsend3 call to MIB service failed: %d", r);
}
/*
* Register a MIB subtree. Initialize the subtree, add it to the local set,
* and send a registration request for it to the MIB service.
*/
int
rmib_register(const int * name, unsigned int namelen, struct rmib_node * rnode)
{
unsigned int id, free_id;
/* A few basic sanity checks. */
if (namelen == 0 || namelen >= __arraycount(rnodes[0].rno_name))
return EINVAL;
if (SYSCTL_TYPE(rnode->rnode_flags) != CTLTYPE_NODE)
return EINVAL;
/* Make sure this is a new subtree, and find a free slot for it. */
for (id = free_id = 0; id < __arraycount(rnodes); id++) {
if (rnodes[id].rno_node == rnode)
return EEXIST;
else if (rnodes[id].rno_node == NULL &&
rnodes[free_id].rno_node != NULL)
free_id = id;
}
if (rnodes[free_id].rno_node != NULL)
return ENOMEM;
rnodes[free_id].rno_node = rnode;
rnodes[free_id].rno_namelen = namelen;
memcpy(rnodes[free_id].rno_name, name, sizeof(name[0]) * namelen);
/*
* Initialize the entire subtree. This will also compute rnode_clen
* for the given rnode, so do this before sending the message.
*/
rmib_init(rnode);
/* Send the registration request to the MIB service. */
rmib_send_reg(free_id);
return OK;
}
/*
* Deregister a previously registered subtree, both internally and with the MIB
* service. Return OK if the deregistration procedure has been started, in
* which case the given subtree is guaranteed to no longer be accessed. Return
* a negative error code on failure.
*/
int
rmib_deregister(struct rmib_node * rnode)
{
message m;
unsigned int id;
for (id = 0; id < __arraycount(rnodes); id++)
if (rnodes[id].rno_node == rnode)
break;
if (id == __arraycount(rnodes))
return ENOENT;
rnodes[id].rno_node = NULL;
/*
* Request that the MIB service unmount the subtree. We completely
* ignore failure here, because the caller would not be able to do
* anything about it anyway. We may also still receive sysctl call
* requests for the node we just deregistered, but this is caught
* during request processing. Reuse of the rnodes[] slot could be a
* potential problem though. We could use sequence numbers in the root
* identifiers to resolve that problem if it ever occurs in reality.
*/
memset(&m, 0, sizeof(m));
m.m_type = MIB_DEREGISTER;
m.m_lsys_mib_register.root_id = id;
(void)asynsend3(MIB_PROC_NR, &m, AMF_NOREPLY);
return OK;
}
/*
* Reregister all previously registered subtrees. This routine should be
* called after the main program has determined that the MIB service has been
* restarted.
*/
void
rmib_reregister(void)
{
unsigned int id;
for (id = 0; id < __arraycount(rnodes); id++)
if (rnodes[id].rno_node != NULL)
rmib_send_reg(id);
}
/*
* Reset all registrations, without involving MIB communication. This routine
* exists for testing purposes only, and may disappear in the future.
*/
void
rmib_reset(void)
{
memset(rnodes, 0, sizeof(rnodes));
}
/*
* Process a request from the MIB service for information about the root node
* of a subtree, specifically its name and description.
*/
static int
rmib_info(const message * m_in)
{
struct rmib_node *rnode;
0