1

我正在使用内核为 3.11.6-200.fc19.x86_64 的 Fedora 19

我在学习 proc_fs.h 时遇到了一些错误,这些代码似乎可以在 Ubuntu 12.04 上正常编译,我只是无法弄清楚它在 Fedora 上出了什么问题。

[root@frederick-pc Test]# make
make -C /lib/modules/3.11.6-200.fc19.x86_64/build M=/home/frederick/Documents/HW_2nd/Test modules
make[1]: Entering directory `/usr/src/kernels/3.11.6-200.fc19.x86_64'
  CC [M]  /home/frederick/Documents/HW_2nd/Test/rw_proc.o
/home/frederick/Documents/HW_2nd/Test/rw_proc.c: In function ‘rw_proc_init’:
/home/frederick/Documents/HW_2nd/Test/rw_proc.c:43:2: error: implicit declaration of function ‘proc_create_entry’ [-Werror=implicit-function-declaration]
  p = proc_create_entry(PROCFS_NAME, 0644, NULL);
  ^
/home/frederick/Documents/HW_2nd/Test/rw_proc.c:43:4: warning: assignment makes pointer from integer without a cast [enabled by default]
  p = proc_create_entry(PROCFS_NAME, 0644, NULL);
    ^
/home/frederick/Documents/HW_2nd/Test/rw_proc.c:51:3: error: dereferencing pointer to incomplete type
  p->read_proc = procfile_read;
   ^
/home/frederick/Documents/HW_2nd/Test/rw_proc.c:52:3: error: dereferencing pointer to incomplete type
  p->write_proc = procfile_write;
   ^
/home/frederick/Documents/HW_2nd/Test/rw_proc.c:54:3: error: dereferencing pointer to incomplete type
  p->mode = S_IFREG | S_IRUGO;
   ^
/home/frederick/Documents/HW_2nd/Test/rw_proc.c:55:3: error: dereferencing pointer to incomplete type
  p->uid = 0;
   ^
/home/frederick/Documents/HW_2nd/Test/rw_proc.c:56:3: error: dereferencing pointer to incomplete type
  p->gid = 0;
   ^
/home/frederick/Documents/HW_2nd/Test/rw_proc.c:57:3: error: dereferencing pointer to incomplete type
  p->size = 37;
   ^
cc1: some warnings being treated as errors
make[2]: *** [/home/frederick/Documents/HW_2nd/Test/rw_proc.o] Error 1
make[1]: *** [_module_/home/frederick/Documents/HW_2nd/Test] Error 2
make[1]: Leaving directory `/usr/src/kernels/3.11.6-200.fc19.x86_64'
make: *** [default] Error 2

这是我的代码

rw_proc.c

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/proc_fs.h>
#include<asm/uaccess.h>

#define PROCFS_MAX_SIZE 1024
#define PROCFS_NAME "my_procfile_1k"

MODULE_LICENSE("GPL");

static struct proc_dir_entry *p = NULL;
static char procfs_buffer[PROCFS_MAX_SIZE];
static unsigned long procfs_buffer_size = 0;
static int procfile_read(char *buffer,char **buffer_location,off_t offset, int buffer_length, int *eof,void *data)
{
    int ret;
    printk(KERN_INFO "procfile_read (/proc/%s) called\n", PROCFS_NAME);
    if (offset > 0) 
    {
        ret = 0;
    } 
    else 
    {
        memcpy(buffer, procfs_buffer, procfs_buffer_size);
        ret = procfs_buffer_size;
    }
    return ret;
}
static int procfile_write(struct file *file, const char *buffer, unsigned long count,void *data)
{
    procfs_buffer_size = count;
    if (procfs_buffer_size > PROCFS_MAX_SIZE ) {
    procfs_buffer_size = PROCFS_MAX_SIZE;
    }
        if ( copy_from_user(procfs_buffer, buffer, procfs_buffer_size) ) {
        return EFAULT;
    }
    return procfs_buffer_size;
}

static int rw_proc_init(void)
{
    p = proc_create_entry(PROCFS_NAME, 0644, NULL);
    if (p == NULL) 
    {
        remove_proc_entry(PROCFS_NAME, NULL);
        printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
        PROCFS_NAME);
        return ENOMEM;
    }
    p->read_proc = procfile_read;
    p->write_proc = procfile_write;
    //p->owner = THIS_MODULE;
    p->mode = S_IFREG | S_IRUGO;
    p->uid = 0;
    p->gid = 0;
    p->size = 37;
    printk(KERN_INFO "/proc/%s created\n", PROCFS_NAME);
    return 0;
}

static void rw_proc_exit(void)
{
    remove_proc_entry(PROCFS_NAME, NULL);
    printk(KERN_INFO "/proc/%s removed\n", PROCFS_NAME);
}

module_init(rw_proc_init);
module_exit(rw_proc_exit);

谁能帮我?谢谢

4

2 回答 2

1

最后我自己解决了这个问题

好像从 3.10.0 版本开始, 的功能proc_create_entry()就被删掉了,取而代之的是proc_create()

因此,如果您使用比 3.10.0 更新的内核进行编译,只需在代码中替换它们

于 2013-11-24T12:07:30.163 回答
0

您的变量p未声明,这是编译器试图告诉您的。您只需要在函数的开头在本地声明变量,如下所示:

struct proc_dir_entry *p = proc_create_entry(PROCFS_NAME, 0644, NULL);

(如果struct proc_dir_entry*真的是函数返回的类型,我没有检查。)

于 2013-11-14T12:28:48.043 回答