免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1132 | 回复: 0
打印 上一主题 下一主题

How to access the kernel variables from user space [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-14 14:04 |只看该作者 |倒序浏览
In linux operating system there are many methods to access the kernel variables from user space. You can access the kernel variables through psuedo-device drivers, procfs, etc. In this article the procfs method is discussed.
Procfs is a pseudo filesystem which no disk storage is required. It resides in RAM. It just provide an interface. The variables of kernel is not required additional space to store. They are just kernel variables which are allocated memory space when system intialized(not considering module inserting). In short procfs provides a view of kernel's state which is described by kernel's variables. So obviously many kernel variables are only readable but writable through profs interface.
[lsq@localhost proc]$ ls -al
...
-r--r--r--   1 root      root              0 2009-08-14 11:18 partitions
-r--r--r--   1 root      root              0 2009-08-14 11:18 schedstat
dr-xr-xr-x   4 root      root              0 2009-08-14 11:18 scsi
lrwxrwxrwx   1 root      root             64 2009-08-14 07:52 self -> 10872
-rw-r--r--   1 root      root              0 2009-08-14 11:18 slabinfo
-r--r--r--   1 root      root              0 2009-08-14 11:18 stat
-r--r--r--   1 root      root              0 2009-08-14 11:18 swaps
dr-xr-xr-x   1 root      root              0 2009-08-14 07:52 sys
--w-------   1 root      root              0 2009-08-14 11:18 sysrq-trigger
dr-xr-xr-x   2 root      root              0 2009-08-14 11:18 sysvipc
-rw-r--r--   1 root      root              0 2009-08-14 11:18 timer_list
-rw-r--r--   1 root      root              0 2009-08-14 11:18 timer_stats
dr-xr-xr-x   4 root      root              0 2009-08-14 11:18 tty
-r--r--r--   1 root      root              0 2009-08-14 11:18 uptime
-r--r--r--   1 root      root              0 2009-08-14 11:18 version
-r--------   1 root      root              0 2009-08-14 11:18 vmcore
-r--r--r--   1 root      root              0 2009-08-14 11:18 vmmemctl
-r--r--r--   1 root      root              0 2009-08-14 11:18 vmstat
-r--r--r--   1 root      root              0 2009-08-14 11:18 zoneinfo
Now let's look at codes in include/linux/proc-fs.h.
struct proc_dir_entry is a main structure of procfs. It represents a procfs entry which defines mode, authorities of user, and operations etc. In this structure there are several fields which we should take into account.
struct proc_dir_entry {
  ...
  mode_t  mode;
  ...
  read_proc_t  *read_proc;
  write_proc_t  *write_proc;
  ...
};
And then how to create a proc_dir_entry? The Linux kernel provides the corresponding function:
  struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
          struct proc_dir_entry *parent);
When the entry is removed. The function
  void remove_proc_entry(const char *name, struct proc_dir_entry *parent);
if you want to create an entry named "myentry" in /proc with mode 0644, you can do as follows:
  ...
  struct proc_dir_entry  *entry;
  ...
  entry = create_proc_entry("myprocentry", 0644, NULL);
  ...
when system runs again, you can see the following entry in /proc
[lsq@localhost proc]$ ls -al | grep myprocentry
-rw-r--r--   1 root      root              0 2009-08-14 11:18 myprocentry
Now lets add the operations to the entry.
  ...
  entry->read_proc = proc_myprocentry_read;
  entry->write_proc = proc_myprocentry_write;
  ...
  static int
  proc_myprocentry_read(char *page, char **start, off_t off, int count, int *eof,
        void *data)
  {
      ...
  }
  static int
  proc_myprocentry_write(struct file *file, const char __user *buffer,
        unsigned long count, void *data)
  {
      ...
  }
And then you can read myprocentry with following commands:
[lsq@localhost proc]$ cat /proc/myprocentry
...
[lsq@localhost proc]$ od -x /proc/myprocentry
If you want to change the kernel variable, you can use:
[lsq@localhost proc]$ echo value > /proc/myprocentry
The following codes gives details of above procedure. Assume that kernel variable
"kernel_var" is an unsigned integer. It is declared as:
   unsigned int kernel_var;
----------------------------------------------------------------------------------
#include
#include
#include
static int
proc_myprocentry_read(char *page, char **start, off_t off, int count, int *eof,
   void *data)
{
   int  len = 0;
   len += snprintf(page + len, PAGE_SIZE - len, "%ud\n", kernel_var);
   *eof = 0;
   return  len;
}
static int
proc_myprocentry_write(struct file *file, const char __user *buffer, unsigned long count,
   void *data)
{
   /* if using 'echo val > /proc/myprocentry', it should change string to int */
   if (unlikely(count == sizeof(unsigned int))) {
      copy_from_user((char *)&kernel_var, buffer, sizeof(unsigned int));
   }
   return  count;
}
int
myprocentry_init(void)
{
   struct proc_dir_entry  *entry;
   entry = create_proc_entry("myprocentry", 0644, NULL);
   if (entry == NULL)
       return  -1;
   entry->read_proc = proc_myprocentry_read;
   entry->write_proc = proc_myprocentry_write;
   return  0;
}
void
myprocentry_exit(void)
{
   remove_proc_entry("myprocentry", NULL);
}
module_init(myprocentry_init);
module_exit(myprocentry_exit);
MODULE_LICENSE("GPL");
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/103132/showart_2027256.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP