📄 skull.c
字号:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/malloc.h>
#include <asm/uaccess.h>
#include <linux/errno.h>
unsigned int fs_major=0;
static char *data;
static struct file_operations chr_fops=
{
read: test_read,
write: test_write,
open: test_open,
release: test_release,
};
static ssize_t test_read(struct file *file,char *buf,size_t count,loff_t *f_pos);
static ssize_t test_write(struct file *file,char *buf,size_t count,loff_t *f_pos);
static int test_open(struct inode *inode,struct file *file);
static int test_release(struct inode *inode,struct file *file);
int init_module(void);
void cleanup_module(void);
/**/
static ssize_t test_read(struct file *file,char *buf,size_t count,loff_t *f_pos);
{
int len;
if(count<0)
return -EINVAL;
len=strlen(data);
if(len<count)
count=len;
copy_to_user(buf,data,count+1);
return count;
}
static ssize_t test_write(struct file *file,char *buf,size_t count,loff_t *f_pos);
{
if(count<0)
return -EINVAL;
kfree(data);
data=(char *)kmalloc(sizeof(char)*(count+1),GFP_KERNEL);
if(!data)
return -ENOMEM;
copy_from_user(data,buffer,count+1);
return count;
}
static ssize_t test_open(struct inode *inode,struct file *file);
{
MOD_INC_USE_COUNT;
printk("This is open\n");
return 0;
}
static ssize_t test_release(struct inode *inode,struct file *file);
{
MOD_INC_USE_COUNT;
printk("This is released\n");
return 0;
}
int init_module(void)
{
int res;
res=register_chrdev(0,"fs",&chr_fops);
if(res<0)
{
printk("can't get major name!\n");
return res;
}
if(fs_major==0)
fs_major=res;
return 0;
}
void cleanup_module(void)
{
unregister_chrdev(fs_major,"fs");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -