Dynamically adding entries to sysctl

谁说我不能喝 提交于 2019-12-23 12:25:50

问题


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


回答1:


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.




回答2:


Yes, you can, just look into the linux kernel's drivers directory for many examples. Essentially, you just need to call register_sysctl_table() multiple times, for for each call you make, you will be creating a branch off an existing branch.

The details are covered here:

https://tthtlc.wordpress.com/2016/05/26/how-to-add-new-entries-to-sysctl-with-the-same-root/



来源:https://stackoverflow.com/questions/20164041/dynamically-adding-entries-to-sysctl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!