Registering Sysctls

In addition to statically initiated sysctls, the kernel features an interface for dynamically registering and unregistering new system control functions. register_sysctl_table is used to register controls and its counterpart, unregister_sysctl_table, to remove sysctl tables, typically when modules are unloaded.

The register_sysctl_table function requires one parameter — a pointer to an array of ctl_table entries in which the new sysctl hierarchy is defined. The function also comprises just a few steps. First, a new ctl_table_header is instantiated and associated with the sysctl table. The resulting construct is then added to the existing list of sysctl hierarchies.

The auxiliary function sysct_check_table is used to check that the new entry contains proper information. Basically, it ensures that no nonsense combinations are specified (i.e., directories that contain data directories that are writable) and that regular files have a valid strategy routine.

Registering a sysctl entry does not automatically create inode instances that connect the sysctl entries with proc entries. Since most sysctls are never used via proc, this wastes memory. Instead, the connection with proc files is created dynamically. Only the directory /proc/sys is created when procfs is initialized:

fs/proc/proc_sysctl.c int proc_sys_init(void) {

proc_sys_root = proc_mkdir("sys", NULL); proc_sys_root->proc_iops = &proc_sys_inode_operations; proc_sys_root->proc_fops = &proc_sys_file_operations; proc_sys_root->nlink = 0; return 0;

The inode operations specified in proc_sys_inode_operations ensure that files and directories below /proc/sys are dynamically generated when they are needed. The contents of the structure are as follows:

fs/proc/proc_sysctl.c static struct inode_operations proc_sys_inode_operations = { .lookup = proc_sys_lookup,

.permission = proc_sys_permission, .setattr = proc_sys_setattr,

Lookup operations are handled by proc_sys_lookup. The following approach is used to dynamically construct inodes for proc entries:

□ do_proc_sys_lookup takes the parent dentry and the name of the file or directory to find the desired sysctl table entry. This involves mainly iterating over the data structures presented before.

□ Given the inode of the parent directory and the sysctl table, proc_sys_make_inode is employed to construct the required inode instance. Since the new inode's inode operations are also implemented by proc_sys_inode_operations, it is ensured that the described method also works for new subdirectories.

The file operations for /proc/sys entries are given as follows:

kernel/sysctl.c static const struct file_operations proc_sys_file_operations = { .read = proc_sys_read,

.write = proc_sys_write,

.readdir = proc_sys_readdir,

Read and write file operations for all entries are implemented by means of standard operations.

Continue reading here: Procsys File Operations

Was this article helpful?

0 0