George Kasparyants
George Kasparyants

Reputation: 41

How to create own sysctl parameter

At version 4.10.0-38-generic there are no ctl_name field at ctl_table struct I have found the tutorial 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

But there are the usage of this field

Could you please give me an example of the usage of ctl_table at version 4.10.0-38-generic

I try to implement:

    #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);

But there are faults from time to time.

Also, i have another question: At kernel sources, there is the comment, that this parameter is deprecated... Why?
How can I work with the hierarchy of parameters without this field? Please help!

Upvotes: 1

Views: 663

Answers (1)

George Kasparyants
George Kasparyants

Reputation: 41

Fixed my bug
We should unregister_ctl_table in exit function But also there is a question with the field "child"...
enter image description here

Upvotes: 1

Related Questions