Implementing System Control Nodes (Sysctl)

Implementing System Control Nodes (Sysctl)

BY JOHN BALDWIN IMPLEMENTING SYSTEM CONTROL NODES • l The FreeBSD( kernel pro- ) sysctTHE NODES are organized as a tree. Each vides a set of system control node is assigned a unique number within its nodes that can be used to level. Nodes are identified internally by an array of node numbers that traverses the tree from query and set state infor- the root down to the node being requested. mation. These nodes Most nodes in the tree have a name, and named nodes may be identified by a string of can be used to names separated by dots. For example, there is a top-level node named “kern” that is assigned obtain a wide variety the number one (1), and it contains a child node named “ostype” that is assigned the number of statistics and con- one (1). This node’s address is 1.1, but it may figure parameters. The also be referred to by its full name: “kern.ostype”. Users and applications generally information accepted and use node names rather than node numbers. provided by each node can Accessing System Control Nodes use one of several types- The standard C library on FreeBSD provides sev- eral routines for accessing system control nodes. including integers, strings, The sysctl(3) and sysctlbyname(3) functions access a single node. The sysctl(3) function uses and structures. the internal array of numbers to identify a node while sysctlbyname(3) accepts a string contain- ing the node’s full name. Each sysctl access can 10 FreeBSD Journal • query the current state of the node, set the struct kinfo_proc kp; state of the node, or perform both operations. int i, mib[4]; The sysctlnametomib(3) function maps a size_t len; node’s full name to its internal address. This operation uses an internal sysctl node and is a /* Fetch the address of the "kern.proc.pid" bit expensive, so a program that queries a prefix. */ control node frequently can use this routine len = 3; to cache the address of a node. It can then sysctlnametomib("kern.proc.pid", mib, &len); query the node using sysctl(3) rather than sysctlbyname(3). /* Fetch the process information for the Some control nodes have a named prefix with current process. */ unnamed leaves. An example of this is the len = sizeof(kp); “kern.proc.pid” node. It contains a child node mib[3] = getpid(); for each process. The internal address of a given sysctl(mib, 4, &kp, &len, NULL, 0); process’s node consists of the address of “kern.proc.pid” and a fourth number which cor- Example 1 responds to the pid of the process. Example 1 demonstrates using this to fetch information about the current process. SYSCTL_INT(_kern, OID_AUTO, one, CTLFLAG_RD, NULL, 1, "Always returns one"); Simple Control Nodes In the kernel, the <sys/sysctl.h> header provides int frob = 500; several macros to declare control nodes. Each SYSCTL_INT(_kern, OID_AUTO, frob, CTLFLAG_RW, declaration includes the name of the parent &frob, 0, "The \"frob\" variable"); node, a number to assign to this node, a name for the node, flags to control the node’s behav- Example 2 ior, and a description of the node. Some decla- rations require additional arguments. The parent Example 2 defines two integer sysctl nodes: node is identified by its full name, but with a • “kern.one” is a read-only node that always single underscore as a prefix and dots replaced returns the value one and “kern.frob” is a read- by underscores. For example, the “foo.bar” par- write node that reads and writes the value of ent node would be identified by “_foo_bar”. To the global “frob” integer. declare a top-level node, use an empty parent Additional macros are available for several name. The number should use the macro integer types including: SYSCTL_UINT() for OID_AUTO to request that the system assign a unsigned integers, SYSCTL_LONG() for signed unique number. (Some nodes use hardcoded long integers, SYSCTL_ULONG() for unsigned numbers for legacy reasons, but all new nodes long integers, SYSCTL_QUAD() for signed 64- should use system-assigned numbers.) The flags bit integers, SYSCTL_UQUAD() for 64-bit un- argument must indicate which types of access signed integers, and SYSCTL_COUNTER_U64() the node supports (read, write, or both) and can for 64-bit unsigned integers managed by the also include other optional flags. The description counter(9) API. Only the SYSCTL_INT() and should be a short string describing the node. It SYSCTL_UINT() macros may be used with a is displayed instead of the value when the “-d” NULL pointer. The other macros require a non- flag is passed to sysctl(8). NULL pointer and ignore the value parameter. Integer Nodes Other Node Types The simplest and most common control node is The SYSCTL_STRING() macro is used to a leaf node that controls a single integer. This define a leaf node with a string value. This type of node is defined by the SYSCTL_INT() macro accepts two additional arguments: a macro. It accepts two additional arguments; a pointer and a length. The pointer should point pointer and a value. If the pointer is non-NULL, to the start of the string. If the length is zero, it should point to an integer variable which will the string is assumed to be a constant string and be read and written by the control node (as attempts to write to the string will fail (even if specified in the flags argument). If the pointer is the node allows write access). If the length is NULL, then the node must be a read-only node non-zero, it specifies the maximum length of the that returns the value argument when read. string buffer (including a terminating null char- Jan/Feb 2014 11 IMPLEMENTING CONTROL NODES The SYSCTL_NODE() macro is used to static SYSCTL_NODE(, OID_AUTO, demo, 0, NULL, define a branch node. This macro accepts one "Demonstration tree"); additional argument which is a pointer to a function handler. For a branch node with explicit static char name_buffer[64] = "initial name"; leaf nodes (declared by other SYSCTL_*() SYSCTL_STRING(_demo, OID_AUTO, name, macros) the pointer should be NULL. The macro CTLFLAG_RW, name_buffer, may be prefixed with static to declare a sizeof(name_buffer), "Demo name"); branch node private to the current file. A public node can be forward declared in a header for static struct demo_stats { use by other files via the SYSCTL_DECL() int demo_reads; macro. This macro accepts a single argument int demo_writes; which is the full name of the node specified in } stats; the format used for a parent node in the other SYSCTL_STRUCT(_demo, OID_AUTO, status, macro invocations. Example 3 defines a top level CTLFLAG_RW, &stats, demo_stats, node with three leaf nodes describing a string "Demo statistics"); buffer, a structure, and an opaque data buffer. SYSCTL_OPAQUE(_demo, OID_AUTO, mi_switch, Node Flags CTLFLAG_RD, &mi_switch, 64, "Code", "First 64 bytes of mi_switch()"); Each node definition requires a flags argument. All leaf nodes and branch nodes with a non- NULL function handler must specify the permit- Example 3 ted access (read and/or write) in the flags field. The flags field can also include zero or more of the flags listed in Table 1. • acter) and attempts to write a string longer than the buffer’s size will fail. Complex Control Nodes The SYSCTL_STRUCT() macro is used to define a leaf node whose value is a single C System control nodes are not limited to simply struct. This macro accepts one additional pointer reading and writing existing variables. Each leaf argument which should point to the structure to node includes a pointer to a handler function be controlled. The size of the structure is that is invoked when the node is accessed. This inferred from the type. function is responsible for returning the “old” The SYSCTL_OPAQUE() macro is used to value of a node as well as accepting “new” val- define a leaf node whose value is a data buffer ues assigned to a node. The standard node of unspecified type. The macro accepts three macros such as SYSCTL_INT() use predefined additional arguments: a pointer to the start of handlers in sys/kern/kern_sysctl.c. the data buffer, the length of the data buffer, A leaf node with a custom handler function is and a string describing the format of the data defined via the SYSCTL_PROC() macro. In buffer. addition to the standard arguments accepted by the other macros, SYSCTL_PROC()accepts a FLAG ............................PURPOSE Table 1 CTLFLAG_ANYBODY .........All users can write to this node. Normally only the superuser can ............................................write to a node. CTLFLAG_SECURE ..............Can only be written if securelevel is less than or equal to zero. CTLFLAG_PRISON ..............Can be written to by a superuser inside of a prison created by jail(2). CTLFLAG_SKIP....................Hides this node from iterative walks of the tree such as when ............................................sysctl(8) lists nodes. CTLFLAG_MPSAFE .............Handler routine does not require Giant. All of the simple node types ............................................set this flag already. It is only required explicitly for nodes that use a ............................................custom handler. CTLFLAG_VNET ..................Can be written to by a superuser inside of a prison if that prison ............................................contains its own virtual network stack. 12 FreeBSD Journal FLAG .........................................MEANING CTLTYPE_NODE ........................This node is a branch node and does not have an associated value. • CTLTYPE_INT.............................This node describes one or more signed integers. CTLTYPE_UINT ..........................This node describes one or more unsigned integers. CTLTYPE_LONG.........................This node describes one or more signed long integers. CTLTYPE_ULONG......................This node describes one or more unsigned long integers. CTLTYPE_S64 ............................This node describes one or more signed 64-bit integers. Table 2 CTLTYPE_U64............................This node describes one or more unsigned 64-bit integers.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us