Search code examples
clinuxlinux-kernelkernel-modulesysctl

Dynamically adding entries to sysctl


Consider this code:

int procmon_state = 0;
static struct ctl_table_header *procmon_table_header;

static ctl_table state_table[] = {
    {
        .procname = "state", .mode = 0666,
        .proc_handler = &proc_dointvec_minmax,
        .data = &procmon_state, .maxlen = sizeof(int),
        .extra1 = "\x00\x00\x00\x00" /*0*/, .extra2 = "\x01\x00\x00\x00" /*1*/
    },
    { 0 }
};

static ctl_table procmon_table[] = {
    {
        .procname = "procmon", .mode = 0555,
        .child = state_table
    },
    { 0 }
};

procmon_table_header = register_sysctl_table(procmon_table);

This will create an entry in /proc/sys (so I could then just sysctl procmon.state=1).

My question is: Once that entry is created, how can I add more entries?

EDIT: More entries inside procmon, that is. For example, procmon.another_state


Solution

  • There are no functions for changing sysctl tables in sysctl.h.

    You have to list all entries that you might need before calling register_sysctl_table.

    If you really need to change the table afterwards, you have to call unregister_sysctl_table before doing your modifications, and then register it again.