linux加载LKM简单例子

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhangpan19910604/article/details/44957093


一、写hello.c文件

#include <linux/module.h>
MODULE_LICENSE("GPL");
int init_module(void){
printk("Hello World\n");
return 0;
}
void cleanup_module(void){
printk("Bye,Bye\n");
}
二、gcc -c hello.c生成.o文件

三、编写Makefile文件
obj-m += hello.o

四、make -C /usr/src/kernels/`uname -r` SUBDIRS=$PWD modules生成.ko文件

五、insmod hello.ko加载这个模块// 输出的消息不在stdout在内核回环缓冲区中,dmesg查看缓存区的信息
六、lsmod 查看当前加载的木块
七、rmmod hello 卸载这个模块


阅读更多
换一批

LKM 中一个例子的问题

11-07

#include /* We're doing kernel work */rn#include /* Specifically, a module */rn#include /* Necessary because we use proc fs */rn#include /* For putting processes to sleep and rn waking them up */rn#include /* for get_user and put_user */rnrnrn#define MESSAGE_LENGTH 80rnstatic char Message[MESSAGE_LENGTH];rnrnstatic struct proc_dir_entry *Our_Proc_File;rn#define PROC_ENTRY_FILENAME "sleep"rnrnrnstatic ssize_t module_output(struct file *file, /* see include/linux/fs.h */rn char *buf, /* The buffer to put data to rn (in the user segment) */rn size_t len, /* The length of the buffer */rn loff_t * offset)rnrn static int finished = 0;rn int i;rn char message[MESSAGE_LENGTH + 30];rnrnrn if (finished) rn finished = 0;rn return 0;rn rnrn rn sprintf(message, "Last input:%s\n", Message);rn for (i = 0; i < len && message[i]; i++)rn put_user(message[i], buf + i);rnrn finished = 1;rn return i; /* Return the number of bytes "read" */rnrnrnrnstatic ssize_t module_input(struct file *file, /* The file itself */rn const char *buf, /* The buffer with input */rn size_t length, /* The buffer's length */rn loff_t * offset)rn /* offset to file - ignore */rn int i;rnrn rn for (i = 0; i < MESSAGE_LENGTH - 1 && i < length; i++)rn get_user(Message[i], buf + i);rn rn Message[i] = '\0';rnrn rn return i;rnrnrnrnint Already_Open = 0;rnrnrnDECLARE_WAIT_QUEUE_HEAD(WaitQ);rnrnstatic int module_open(struct inode *inode, struct file *file)rnrn rn if ((file->f_flags & O_NONBLOCK) && Already_Open)rn return -EAGAIN;rnrnrn try_module_get(THIS_MODULE);rnrnrn while (Already_Open) rn int i, is_sig = 0;rnrn rn wait_event_interruptible(WaitQ, !Already_Open);rnrnrn [color=#FF0000] for (i = 0; i < _NSIG_WORDS && !is_sig; i++)rn is_sig =rn current->pending.signal.sig[i] & ~current->rn blocked.sig[i];rnrn if (is_sig) rn rn module_put(THIS_MODULE);rn return -EINTR;rn [/color] rnrn rn Already_Open = 1;rn return 0; /* Allow the access */rnrnrnrnint module_close(struct inode *inode, struct file *file)rnrn rn Already_Open = 0;rnrn rn wake_up(&WaitQ);rnrn module_put(THIS_MODULE);rnrn return 0; /* success */rnrnrnrnstatic int module_permission(struct inode *inode, int op, struct nameidata *nd)rnrn rn if (op == 4 || (op == 2 && current->euid == 0))rn return 0;rnrn return -EACCES;rnrnrnrnstatic struct file_operations File_Ops_4_Our_Proc_File = rn .read = module_output, /* "read" from the file */rn .write = module_input, /* "write" to the file */rn .open = module_open, /* called when the /proc file is opened */rn .release = module_close, /* called when it's closed */rn;rnrnrnrnstatic struct inode_operations Inode_Ops_4_Our_Proc_File = rn .permission = module_permission, /* check for permissions */rn;rnrnrnint init_module()rnrn int rv = 0;rn Our_Proc_File = create_proc_entry(PROC_ENTRY_FILENAME, 0644, NULL);rn Our_Proc_File->owner = THIS_MODULE;rn Our_Proc_File->proc_iops = &Inode_Ops_4_Our_Proc_File;rn Our_Proc_File->proc_fops = &File_Ops_4_Our_Proc_File;rn Our_Proc_File->mode = S_IFREG | S_IRUGO | S_IWUSR;rn Our_Proc_File->uid = 0;rn Our_Proc_File->gid = 0;rn Our_Proc_File->size = 80;rnrn if (Our_Proc_File == NULL) rn rv = -ENOMEM;rn remove_proc_entry(PROC_ENTRY_FILENAME, &proc_root);rn printk(KERN_INFO "Error: Could not initialize /proc/test\n");rn rnrn return rv;rnrnrnrnvoid cleanup_module()rnrn remove_proc_entry(PROC_ENTRY_FILENAME, &proc_root);rnrnrnrn以上带颜色的部分有什么用呢,我把它注掉没发现有什么区别?rnrnrn高手解答,谢谢了

没有更多推荐了,返回首页