我已经开发了一个简单的linux内核模块:
#include#include #include ssize_t exer_open(struct inode *pinode, struct file *pfile) { return 0; } ssize_t exer_read(struct file *pfile, char __user *buffer, size_t length, loff_t *offset) { return 0; } ssize_t exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset) { return length; } ssize_t exer_close(struct inode *pinode, struct file *pfile) { return 0; } struct file_operations exer_file_operatiOns= { .owner = THIS_MODULE, .open = exer_open, .read = exer_read, .write = exer_write, .release = exer_close, }; int exer_simple_module_init(void) { printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__); register_chrdev(240, "Simple Char Drv", &exer_file_operations); return 0; } void exer_simple_module_exit(void) { unregister_chrdev(240, "Simple Char Drv"); } module_init(exer_simple_module_init); module_exit(exer_simple_module_exit);
我使用insmod
命令将这个模块插入内核没有任何问题。
我想使用此模块来打印由我也开发的用户空间程序发送给它的消息:
#include#include #include #include #include #include int main() { int ret, fd; char stringToSend[] = "Hello World !"; fd = open("/dev/char_device", O_RDWR); // Open the device with read/write access if (fd <0) { perror("Failed to open the device..."); return errno; } ret = write(fd, stringToSend, strlen(stringToSend)); // Send the string to the LKM if (ret <0) { perror("Failed to write the message to the device."); return errno; } return 0; }
当我执行该程序并使用tail -f /var/log/messages
命令检查内核日志时,我可以看到:user.alert kernel: Inside the exer_read function
但是我看不到消息“ Hello World!”。
我不知道我在这里缺少什么,特别是我仍然是开发模块和使用它的初学者。请帮帮我!