当前位置: 技术问答>linux和unix
在/proc目录中添加文件并写入内容
来源: 互联网 发布时间:2017-01-08
本文导语: 驱动中如何在/proc目录中添加文件和添加文件内容,,最好能给出代码,急用,不胜感激!!! 分不够可加!! | #define __KERNEL__ #define MODULE #include #include #include #include /* Necessary b...
驱动中如何在/proc目录中添加文件和添加文件内容,,最好能给出代码,急用,不胜感激!!!
分不够可加!!
分不够可加!!
|
#define __KERNEL__
#define MODULE
#include
#include
#include
#include /* Necessary because we use the proc fs */
#define procfs_name "proctest"
struct proc_dir_entry *Our_Proc_File;
int procfile_read(char *buffer,
char **buffer_location,
off_t offset, int buffer_length, int *eof, void *data)
{
int ret;
ret = sprintf(buffer, "HelloWorld!n");
return ret;
}
int proc_init()
{
Our_Proc_File = create_proc_entry(procfs_name, 0644, NULL);
if (Our_Proc_File == NULL) {
remove_proc_entry(procfs_name, NULL);
printk(KERN_ALERT "Error: Could not initialize /proc/%sn",
procfs_name);
return -ENOMEM;
}
Our_Proc_File->read_proc = procfile_read;
Our_Proc_File->owner = THIS_MODULE;
Our_Proc_File->mode = S_IFREG | S_IRUGO;
Our_Proc_File->uid = 0;
Our_Proc_File->gid = 0;
Our_Proc_File->size = 37;
printk("/proc/%s createdn", procfs_name);
return 0;
}
void proc_exit()
{
remove_proc_entry(procfs_name, NULL);
printk(KERN_INFO "/proc/%s removedn", procfs_name);
}
module_init(proc_init);
module_exit(proc_exit);
#define MODULE
#include
#include
#include
#include /* Necessary because we use the proc fs */
#define procfs_name "proctest"
struct proc_dir_entry *Our_Proc_File;
int procfile_read(char *buffer,
char **buffer_location,
off_t offset, int buffer_length, int *eof, void *data)
{
int ret;
ret = sprintf(buffer, "HelloWorld!n");
return ret;
}
int proc_init()
{
Our_Proc_File = create_proc_entry(procfs_name, 0644, NULL);
if (Our_Proc_File == NULL) {
remove_proc_entry(procfs_name, NULL);
printk(KERN_ALERT "Error: Could not initialize /proc/%sn",
procfs_name);
return -ENOMEM;
}
Our_Proc_File->read_proc = procfile_read;
Our_Proc_File->owner = THIS_MODULE;
Our_Proc_File->mode = S_IFREG | S_IRUGO;
Our_Proc_File->uid = 0;
Our_Proc_File->gid = 0;
Our_Proc_File->size = 37;
printk("/proc/%s createdn", procfs_name);
return 0;
}
void proc_exit()
{
remove_proc_entry(procfs_name, NULL);
printk(KERN_INFO "/proc/%s removedn", procfs_name);
}
module_init(proc_init);
module_exit(proc_exit);
|
用create_proc_entry(const char* name, mode_t mode, struct proc_dir_entry *parent) 创建proc文件
用seq_file接口来实现对/proc的读写
用seq_file接口来实现对/proc的读写