如何创建自己的sysctl参数

问题描述 投票:1回答:1

在版本4.10.0-38-generic,ctl_table结构中没有ctl_name字段我找到了教程https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=2ahUKEwie4Zz_5ZrdAhVKiaYKHRqsDiwQFjABegQICRAC&url=https%3A%2F%2Fsar.informatik.hu-berlin.de%2Fteaching%2F2012-s%2F2012-s%2520Operating%2520Systems%2520Principles%2Flab%2Flab-1%2Fsysctl_.pdf&usg=AOvVaw0mJdbT9E3lP2k3AQOGgzQz

但是有这个领域的用法

能否请您给我一个4.10.0-38-generic版本的ctl_table使用示例

我尝试实现:

    #include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sysctl.h>

#define SUCCESS (0)


MODULE_LICENSE("GPL");
MODULE_AUTHOR("Kasparyants George");
MODULE_DESCRIPTION("A simple Linux driver");
MODULE_VERSION("0.1");


static int global_var1 = 1;
static int global_var2 = 1;
static int min_val = 0;
static int max_val = 5;
static struct ctl_table_header* header;


static struct ctl_table child_ctl_table[] = {
  {
    .procname = "sample_value1",
    .data = &global_var1,
    .maxlen = sizeof(int),
    .mode = 0644,
    .proc_handler = &proc_dointvec_minmax,
    .extra1 = &min_val,
    .extra2 = &max_val
  }, 
  {
    .procname = "sample_value2",
    .data = &global_var2,
    .maxlen = sizeof(int),
    .mode = 0644,
    .proc_handler = &proc_dointvec_minmax,
    .extra1 = &min_val,
    .extra2 = &max_val
  },
  {}
};


static struct ctl_table parent_ctl_table[] = {
  {
    .procname = "mykernel",
    .mode = 0555,
    .child = child_ctl_table
  }, 
  {}
};


static int __init sysctl_module_init(void) {
  if (!(header = register_sysctl_table(parent_ctl_table))) {
    printk(KERN_ALERT "Error: Failed to register parent_ctl_table\n");
    return -EFAULT;
  }
  printk(KERN_INFO "Start global_var1 = %d, global_var2 = %d\n", global_var1, global_var2);
  return SUCCESS;
}


static void __exit sysctl_module_exit(void) {
  printk(KERN_INFO "End global_var1 = %d, global_var2 = %d\n", global_var1, global_var2);
}


module_init(sysctl_module_init);
module_exit(sysctl_module_exit);

但是不时有缺点。

另外,我有另一个问题:在内核源代码,有评论,这个参数已被弃用...为什么? 如何在没有此字段的情况下使用参数层次结构?请帮忙!

c linux-kernel
1个回答
1
投票

修正了我的错误 我们应该退出函数unregister_ctl_table但是还有一个问题与字段“孩子”... enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.