Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2548423
  • 博文数量: 351
  • 博客积分: 76
  • 博客等级: 上将
  • 技术积分: 3555
  • 用 户 组: 普通用户
  • 注册时间: 2004-11-13 21:27
文章分类

全部博文(351)

文章存档

2013年(1)

2012年(4)

2011年(7)

2010年(16)

2009年(34)

2008年(34)

2007年(34)

2006年(68)

2005年(82)

2004年(71)

分类:

2005-09-28 15:27:15

The Linux Kernel Module Programming Guide中,
讲/proc的那一章节的源代码编译没有问题,insmod时,提示:
procfs.o: unresolved symbol proc_register
procfs.o: unresolved symbol proc_unregister
最后google了一下,最后发现新的内核没export出这两个接口,
使用create_proc_entry()和remove_proc_entry()就ok了。

修改代码之后编译时,总报错:
couldn't find the kernel version the module was compiled for
查看源代码,添加了
#include linux/module.h
#include linux/version.h
问题就解决了。
代码如下:

#include
#include
#include
#include
#include
#include

/* Deal with CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include
#endif

/* In 2.2.3 /usr/include/linux/version.h includes a
 * macro for this, but 2.0.35 doesn't - so I add it
 * here if necessary. */
#ifndef KERNEL_VERSION
#define KERNEL_VERSION(a,b,c) ((a)*65536+(b)*256+(c))
#endif

#define MY_PROC_ENTRY_NAME "my_proc_entry"
struct proc_dir_entry *my_proc_entry=NULL;

static ssize_t ip_prof_output(
             struct file *file,
             char *buf,
             size_t len,
             loff_t *offset)
{
/* ... implement output from proc entry here ... */
return 0; /* returns number of output bytes */
}
static ssize_t ip_prof_input(
               struct file *file,
               const char *buf,
               size_t len,
               loff_t *offset)
{
/* ... implemetnt input into proc entry here ... */
return 0; /* returns number of input bytes */
}

int my_proc_entry_open(struct inode *inode, struct file *file) {
  return 0;
}

int my_proc_entry_close(struct inode *inode, struct file *file){
  return 0;
}

static int my_proc_entry_permission( struct inode *inode, int op) {
/* allow supeuser access only. doesn't really matter. */
  if(op==4 || (op==2 && current->euid==0))
    return 0;
  return -EACCES;
}

static struct file_operations my_proc_entry_fops =
{
        read: ip_prof_output,
        write: ip_prof_input,
        open: my_proc_entry_open,
        release:my_proc_entry_close,
};


static struct inode_operations my_proc_entry_iops =
{
       permission: my_proc_entry_permission
};

int start_my_proc_entry(void) {
  my_proc_entry = create_proc_entry(MY_PROC_ENTRY_NAME, S_IFREG | S_IRUGO | S_IWUSR, &proc_root);
  if ( !my_proc_entry ) return -ENOMEM;
  my_proc_entry->proc_fops = &my_proc_entry_fops;
  my_proc_entry->proc_iops = &my_proc_entry_iops;
  return 0;
}

int stop_my_proc_entry(void) {
  printk("my_proc_entry: terminating. ");
  remove_proc_entry(MY_PROC_ENTRY_NAME, &proc_root);
  return 0;
}

module_init(start_my_proc_entry);
module_exit(stop_my_proc_entry);

阅读(3585) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~