như tôi biết, để thông báo cho không gian người dùng từ không gian hạt nhân, một cách là sử dụng cuộc thăm dò ý kiến. Điều đó có nghĩa là trình điều khiển nhân nên cung cấp phương thức thăm dò ý kiến trước. Dưới đây là mã được tìm thấy từ internet, và nó thực sự hoạt động!Làm thế nào để thêm chức năng thăm dò vào mã mô-đun hạt nhân?
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/string.h>
#include <linux/vmalloc.h>
#include <asm/uaccess.h>
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Fortune Cookie Kernel Module");
MODULE_AUTHOR("M. Tim Jones");
#define MAX_COOKIE_LENGTH PAGE_SIZE
static struct proc_dir_entry *proc_entry;
static char *cookie_buf; // Space for fortune strings
static int write_index; // Index to write next fortune
static int read_index; // Index to read next fortune
ssize_t fortune_write(struct file *filp, const char __user *buff,
unsigned long len, void *data)
// Refer to: ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
{
int space_available = (MAX_COOKIE_LENGTH-write_index);
if (len > space_available) {
printk(KERN_INFO "fortune: cookie buffer is full!\n");
return -ENOSPC;
}
if (copy_from_user(&cookie_buf[write_index], buff, len)) {
return -EFAULT;
}
write_index += len;
cookie_buf[write_index-1] = 0;
return len;
}
ssize_t fortune_read(struct file *file, char *buf, size_t count, loff_t *f_pos){
// Refer to: ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
int len;
//there's no fortune or a fortune has already been read
//the *f_pos > 0 hack is needed because `cat /proc/fortune` would otherwise
//display every thing in the cookie_buf
if(write_index == 0 || *f_pos > 0){
return 0;
}
// cicle through fortunes
if(read_index >= write_index){
read_index = 0;
}
len = sprintf(buf, "%s\n", &cookie_buf[read_index]);
read_index += len;
*f_pos += len;
return len;
}
static const struct file_operations proc_test_fops = {
.owner = THIS_MODULE,
// .open = led_proc_open,
.read = fortune_read,
// .llseek = seq_lseek,
// .release = single_release,
.write = fortune_write,
// unsigned int (*poll) (struct file *, struct poll_table_struct *);
// int (*fasync) (int, struct file *, int);
};
int __init init_fortune_module(void)
{
int ret = 0;
cookie_buf = (char *)vmalloc(MAX_COOKIE_LENGTH);
if (!cookie_buf) {
ret = -ENOMEM;
} else {
memset(cookie_buf, 0, MAX_COOKIE_LENGTH);
// proc_entry = create_proc_entry("fortune", 0644, NULL);
proc_entry = proc_create("fortune", 0644, NULL, &proc_test_fops);
if (proc_entry == NULL) {
ret = -ENOMEM;
vfree(cookie_buf);
printk(KERN_INFO "fortune: Couldn't create proc entry\n");
} else {
write_index = 0;
read_index = 0;
printk(KERN_INFO "fortune: Module loaded.\n");
}
}
return ret;
}
void __exit exit_fortune_module(void)
{
// remove_proc_entry("fortune", &proc_entry);
proc_remove(proc_entry);
vfree(cookie_buf);
printk(KERN_INFO "fortune: Module unloaded.\n");
}
module_init(init_fortune_module);
module_exit(exit_fortune_module);
tôi có thể làm như thế này để làm cho nó hoạt:
echo "hello" > /proc/fortune
Và sau đó
cat /proc/fortune
để xem kết quả.
Nhưng cách thêm phương thức thăm dò ý kiến vào nó? Tôi đã thử một số lần, nhưng vẫn thất bại. Có ai giúp được không? Cảm ơn!
Câu hỏi này rất hữu ích cho tôi, nhưng tôi vẫn còn lúng túng. Tôi có một câu hỏi liên quan ở đây: https://stackoverflow.com/questions/34027366/implementing-poll-in-a-linux-kernel-module – zmb