使用ctl_table从用户空间向内核传递参数

本文深入探讨了Linux系统中PTY (Pseudo-Terminal Multiplexer) 的控制机制,包括PTY计数的增减过程,以及通过Sysctl表对PTY参数进行配置的方法。同时,展示了如何注册和注销Sysctl表,并通过示例代码说明了如何创建自定义的Sysctl条目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

root:/proc/sys/kernel/pty# ls
max      nr       reserve
 

static int pty_limit_min;
static int pty_limit_max = NR_UNIX98_PTY_MAX;
static int tty_count;
static int pty_count;

static inline void pty_inc_count(void)
{
    pty_count = (++tty_count) / 2;
}

static inline void pty_dec_count(void)
{
    pty_count = (--tty_count) / 2;
}

 

static struct ctl_table pty_table[] = {
    {
        .procname    = "max",
        .maxlen        = sizeof(int),
        .mode        = 0644,
        .data        = &pty_limit,
        .proc_handler    = proc_dointvec_minmax,
        .extra1        = &pty_limit_min,
        .extra2        = &pty_limit_max,
    }, {
        .procname    = "nr",
        .maxlen        = sizeof(int),
        .mode        = 0444,
        .data        = &pty_count,
        .proc_handler    = proc_dointvec,
    }, 
    {}
};

static struct ctl_table pty_kern_table[] = {
    {
        .procname    = "pty",
        .mode        = 0555,
        .child        = pty_table,
    },
    {}
};

 

static struct ctl_table_header *ctl_table_header;

ctl_table_header=register_sysctl_table(pty_root_table);

unregister_sysctl_table(ctl_table_header);

 

/////////////////////////////////////////////////////////////////////////

 

#include <linux/module.h>
 
static struct ctl_table_header *ctl_table_test_header;
 
unsigned int sysctl_test_int;
char sysctl_test_string[256];
static struct ctl_table ctl_table_test[] = {
    {
     .procname = "test_int",
     .data = &sysctl_test_int,
     .maxlen = sizeof(unsigned int),
     .mode = 0777,
     .proc_handler = proc_dointvec,
     },
    {
     .procname = "test_string",
     .data = sysctl_test_string,
     .maxlen = 255,
     .mode = 0777,
     .proc_handler = proc_dostring,
     },
    {}
};
 
static struct ctl_table ctl_table_test_root[] = {
    {
     .procname = "test_root",
     .mode = 0555,
     .child = ctl_table_test,
     },
    {}
};
 
static int __init test_init(void)
{
    ctl_table_test_header = register_sysctl_table(ctl_table_test_root);
    if (!ctl_table_test_header) {
        printk("ctl_table_test_header error\n");
    }
    return 0;
}
 
static void __exit test_exit(void)
{
    if (ctl_table_test_header)
        unregister_sysctl_table(ctl_table_test_header);
}
 
module_init(test_init);
module_exit(test_exit);
MODULE_AUTHOR("tony");
MODULE_DESCRIPTION("test");
MODULE_LICENSE("GPL");
MODULE_ALIAS("test");
 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值