|
procfs.c
[code:1]
/* procfs.c - create a "file" in /proc
*/
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
/* Deal with CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include <linux/modversions.h>
#endif
/* Necessary because we use the proc fs */
#include <linux/proc_fs.h>
/* 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
//read_proc_t procfile_read;
int procfile_read(char *buffer,
char **buffer_location,
off_t offset,
int buffer_length,
int zero,
void *data)
{
int len; /* The number of bytes actually used */
/* This is static so it will still be in memory
* when we leave this function */
static char my_buffer[80];
static int count = 1;
if (offset > 0)
return 0;
/* Fill the buffer and get its length */
len = sprintf(my_buffer,
"For the %d%s time, go away!\n", count,
(count % 100 > 10 && count % 100 < 14) ? "th" :
(count % 10 == 1) ? "st" :
(count % 10 == 2) ? "nd" :
(count % 10 == 3) ? "rd" : "th" );
count++;
/* Tell the function which called us where the
* buffer is */
*buffer_location = my_buffer;
/* Return the length */
return len;
}
struct proc_dir_entry Our_Proc_File =
{
0, /* Inode number - ignore, it will be filled by
* proc_register[_dynamic] */
4, /* Length of the file name */
"test", /* The file name */
S_IFREG | S_IRUGO, /* File mode - this is a regular
* file which can be read by its
* owner, its group, and everybody
* else */
1, /* Number of links (directories where the
* file is referenced) */
0, 0, /* The uid and gid for the file - we give it
* to root */
80, /* The size of the file reported by ls. */
NULL, /* functions which can be done on the inode
* (linking, removing, etc.) - we don't
* support any. */
procfile_read, /* The read function for this file,
* the function called when somebody
* tries to read something from it. */
NULL /* We could have here a function to fill the
* file's inode, to enable us to play with
* permissions, ownership, etc. */
};
/* Initialize the module - register the proc file */
int init_module()
{
/* Success if proc_register[_dynamic] is a success,
* failure otherwise. */
if (!( create_proc_read_entry("test", 0 /* default mode */,
NULL /* parent dir */, procfile_read,
NULL /* client data */)))
{
printk("error!!!!!!!!!!!!\n\n");
}
}
/* Cleanup - unregister our file from /proc */
void cleanup_module()
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,0)
remove_proc_entry("test", NULL /* parent dir */);
#else
proc_unregister(&proc_root, Our_Proc_File.low_ino);
#endif
}
MODULE_LICENSE("GPL");
[/code:1]
然后编译
gcc -c -DMODULE -D__KERNEL__ -I $(KDIR)/include -o procfs.o
procfs.c
我用的2.4.21的内核
编译后的警告如下:
procfs.c: In function 'init_module':
warning: passing arg 4 of 'create_proc_read_entry' form incompatible pointer type
但是.o文件已经生成了
然后加载模块:
insmod procfs.o
出现如下信息:
procfs.o: init_module: Success
Hint: insmod errors can be caused by incorrect module parameters, including invalid IO or IRQ parameters.
You may find more information in syslog or the output from dmesg
模块没有被加载上,请问应该怎么修改?
我曾去掉了struct proc_dir_entry Our_Proc_File 的定义,因为没有用到
但是还是不能加载。
谢谢了! |
|