我正在製作一個簡單的字符驅動程序,它假定寫入到我的字符設備「/ dev/coffee_bean」中,讀取時應顯示字符串「Hi There!」。在控制檯。我通過「cat/dev/coffee_bean」從設備讀取,而是我的系統崩潰並重置。貝婁是我的源代碼。感謝幫助。簡單字符驅動程序崩潰
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kdev_t.h>
#include <linux/types.h>
#include <linux/completion.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
#include <linux/semaphore.h>
MODULE_LICENSE("Dual BSD/GPL");
#define DEVICE_NAME "coffee_grinds"
#define COUNT 4
#define FIRST_MINOR 0
#define CONST_QUANTUM 4000
#define CONST_QSET 4000
int test;
module_param(test, int, S_IRUGO);
struct my_char_structure{
struct cdev my_cdev;
struct semaphore sem;
unsigned int access_key;
unsigned long size;
};
static dev_t dev_num;
int dev_open(struct inode *in_node, struct file *filp){
struct my_char_structure *my_dev;
my_dev = container_of(in_node->i_cdev, struct my_char_structure, my_cdev);
filp->private_data = my_dev;
return 0;
}
int dev_release(struct inode *inode, struct file *filp){
return 0;
}
ssize_t dev_write(struct file *filp, const char __user *buff, size_t count, loff_t *offp){
struct my_char_structure *my_dev = filp->private_data;
ssize_t retval = -ENOMEM; /* value used in "goto out" statements */
char *my_string;
int counting;
printk(KERN_ALERT "Write was accessed, Lol");
if (down_interruptible(&my_dev->sem))
return -ERESTARTSYS;
my_string = kmalloc(count,GFP_KERNEL);
counting = copy_from_user(my_string,buff,count);
printk(KERN_ALERT "You wrote %s",my_string);
kfree(my_string);
up(&my_dev->sem);
printk(KERN_ALERT "We wrote %d bytes",counting);
return retval;
// Here is some experimental code
}
ssize_t dev_read(struct file *filp, char __user *buff, size_t count, loff_t *offp){
struct my_char_structure *my_dev = filp->private_data;
ssize_t retval = 0;
char *my_string;
printk(KERN_ALERT "Read was accessed Lol");
if (down_interruptible(&my_dev->sem))
return -ERESTARTSYS;
my_string = "Hi there!";
copy_to_user(buff,my_string,10);
up(&my_dev->sem);
return retval;
}
struct file_operations fops = {
.owner = THIS_MODULE,
.read = dev_read,
.write = dev_write,
.open = dev_open,
.release= dev_release,
};
int start_mod(void){
//Because we are dealing with a fictitious device, I want
//the driver to create my two devices with arbitrarly
//assigned major numbers.
static struct my_char_structure Dev;
static struct my_char_structure *my_dev = &Dev;
int err;
alloc_chrdev_region(&dev_num, FIRST_MINOR, COUNT, DEVICE_NAME);
sema_init(&(my_dev->sem),1);
cdev_init(&(my_dev->my_cdev), &fops);
my_dev->my_cdev.owner = THIS_MODULE;
my_dev->my_cdev.ops = &fops;// fops is my file operations struct
err = cdev_add(&my_dev->my_cdev, dev_num, COUNT);
if(err)
printk(KERN_ALERT "There was an error %d.",err);
printk(KERN_ALERT " insmod to major number %d",MAJOR(dev_num));
return 0;
}
void end_mod(void){
unregister_chrdev_region(dev_num, COUNT);
}
module_init(start_mod);
module_exit(end_mod);
感謝Kaz,我調試了我的程序,崩潰是由於我爲我的字符串分配內存的方式。設備不再崩潰,但是我得到了來自copy_to_user()函數的段錯誤。我懷疑我得到這個錯誤,因爲我沒有使用傳遞的count變量。感謝捆綁花時間看我的代碼! – 2012-03-13 04:53:50
但你在寫程序中有分配;你說它在閱讀時崩潰了。寫例程中的主要問題是'kmalloc'在它可以返回的塊的大小方面非常有限。它返回物理上連續的頁面(這是一個寶貴的資源)。但是,對「讀」的爭論可能非常大。即一般來說,使用用戶空間中的值作爲'kmalloc'大小(除了知道它在做什麼的根特權服務器程序之外)並不是一個好主意。即使有很多內存,也要做好空回的準備。 – Kaz 2012-03-13 07:16:26
嗯,我只想打印出一些像「Hi There」這樣的小東西,並用kmalloc分配字節,這樣可以防止它崩潰。現在我不想尋找一個功能齊全的驅動程序來實現寫入。我只是想先閱讀才能工作。這是我寫的第一個實現讀/寫的驅動程序。我在2周前纔開始學習。 – 2012-03-13 20:11:34