#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
static struct Proc_dir_entry *proc_root;
static struct Proc_dir_entry *proc_entry;
#define USER_ROOT_DIR "Fellow_root"
#define User_entry "Fellow_entry"
#define Info_len 16
Char *info;
static int proc_fellow_show (struct seq_file *m, void *v)
{
seq_printf (M, "%s\n", info);
return 0;
}
static int Proc_fellow_open (struct inode *inode, struct file *file)
{
return Single_open (file, proc_fellow_show, NULL);
}
Static ssize_t proc_fellow_write (struct file *file, const char __user *buffer, size_t count, loff_t *f_pos)
{
if (Count > Info_len)
Return-efault;
if (Copy_from_user (info, buffer, count))
{
Return-efault;
}
return count;
}
static const struct File_operations Fellow_proc_fops = {
. owner= This_module,
. open= Proc_fellow_open,
. read= Seq_read,
. write = Proc_fellow_write,
. llseek= Seq_lseek,
. release= Single_release,
};
static int fellow_create_proc_entry (void)
{
int error = 0;
Proc_root = Proc_mkdir (User_root_dir, NULL);
if (null==proc_root)
{
PRINTK (kern_alert "Create dir/proc/%s error!\n", User_root_dir);
Return-enomem;
}
PRINTK (kern_info "Create dir/proc/%s\n", User_root_dir);
Proc_entry =create_proc_entry (User_entry, 0666, proc_root);
Proc_entry = Proc_create (User_entry, 0666, Proc_root, &fellow_proc_fops);
if (NULL ==proc_entry)
{
PRINTK (Kern_alert "Create entry%s under/proc/%s error!\n", User_entry,user_root_dir);
Error =-enomem;
Goto Err_out;
}
Proc_entry->write_proc= Fellow_writeproc;
Proc_entry->read_proc = Fellow_readproc;
PRINTK (kern_info "create/proc/%s/%s\n", user_root_dir,user_entry);
return 0;
Err_out:
Proc_entry->read_proc = NULL;
Proc_entry->write_proc= NULL;
Remove_proc_entry (User_entry, proc_root);
Remove_proc_entry (User_root_dir, NULL);
return error;
}
static int fellowproc_init (void)
{
int ret = 0;
PRINTK ("fellowproc_init\n");
info = kmalloc (Info_len * sizeof (char), gfp_kernel);
if (!info)
{
ret =-enomem;
Goto fail;
}
memset (info, 0, Info_len);
Fellow_create_proc_entry ();
return 0;
Fail
return ret;
}
static void Fellowproc_exit (void)
{
Kfree (info);
Remove_proc_entry (User_entry, proc_root);
Remove_proc_entry (User_root_dir, NULL);
}
Module_author ("Fellow");
Module_license ("GPL");
Module_init (Fellowproc_init);
Module_exit (Fellowproc_exit);
The results of the operation are as follows:
Device-driven basic learning--/proc adding nodes