长时间写入linux char设备驱动程序? [英] Write long to linux char device driver?
问题描述
我正在尝试在Linux中编写字符设备驱动程序。不幸的是,它不适用于任何大于255的数字。
I'm trying to write a character device driver in linux. Unfortunately it's not working for any numbers greater than 255.
我希望该驱动程序专门用于处理 long
。每当我输入一个大于255的值时,数字都是错误的。 256变为0,等等。
I want this driver specifically to work with value of type long
. Anytime I input a value greater than 255, the numbers wrong. 256 goes to 0 etc.
我写了一个简单的字符设备驱动程序来显示问题,当我复制完整的驱动程序时,可能有很多未使用的include语句并删除了几乎所有内容:
I've written a simple character device driver that shows the problem, there might be a lot of unused include statements as I copied my full driver and deleted almost everything:
chartest.c
chartest.c
#include <linux/init.h>
#include <linux/module.h> /* I mean this is a module after all! */
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/sched.h> /* For current task information */
#include <linux/fs.h> /* For file operations */
#include <linux/types.h> /* dev_t: device number data type */
#include <linux/cdev.h> /* cdev is the module data type that the kernel sees */
#include <asm/uaccess.h> /* For routines to copy data to/from user space */
#include <linux/uaccess.h>
#include <linux/slab.h> /* kmalloc/kfree */
MODULE_LICENSE("GPL");
#define DRIVER_NAME "chartest"
#define MAJOR_NUM 230
#define MINOR_NUM 0
struct cdev *cdev;
int test_device_open(struct inode *inode, struct file *fp) {
return 0;
}
int test_device_release(struct inode *inode, struct file *fp) {
return 0;
}
ssize_t test_read(struct file *fp, char __user *buffer, size_t count, loff_t *f_pos) {
return count;
}
ssize_t test_write(struct file *fp, const char __user *buffer, size_t count, loff_t *fpos) {
// We must validate the user's buffer and convert it to a long long
long userOperand;
unsigned char *userInput = NULL;
printk(KERN_NOTICE "Write Function Entered.\n");
printk(KERN_ALERT "Write count: %ld, Write fp: %lld\n", count, *fpos);
userInput = kmalloc(count, GFP_KERNEL);
get_user(*userInput, buffer);
printk(KERN_NOTICE "Value before cast: %ld\n", (long) *userInput);
userOperand = (long) *userInput;
printk(KERN_NOTICE "Value after cast: %ld\n", userOperand);
// Increment the file position pointer (in our case, always by 8)
*fpos += count;
kfree(userInput);
return count;
}
/*
* Declaration of function for open file operations
*/
static struct file_operations test_fops = {
.owner = THIS_MODULE,
.read = test_read,
.write = test_write,
.open = test_device_open,
.release = test_device_release,
};
// Initialization function
static int __init test_init(void)
{
// Register device number:
int err = 0;
dev_t device_number = MKDEV(MAJOR_NUM, MINOR_NUM);
err = register_chrdev_region(device_number, 1, DRIVER_NAME);
if (err < 0) {
printk(KERN_ALERT "Could not allocate device number.\n");
return err;
}
cdev = cdev_alloc();
cdev->owner = THIS_MODULE;
cdev->ops = &test_fops;
err = cdev_add(cdev, device_number, 1);
if (err) {
printk("Error allocating cdev.\n");
}
printk(KERN_ALERT "Test Initialized. Major Number: %d\n", MAJOR_NUM);
return 0;
}
// Exit function:
static void __exit test_exit(void)
{
dev_t device_number = MKDEV(MAJOR_NUM, MINOR_NUM);
// Remove char device */
cdev_del(cdev);
/* Unregister Device Number: */
unregister_chrdev_region(device_number, 1);
printk(KERN_ALERT "TestDriver %d destroyed.\n", MAJOR_NUM);
}
module_init(test_init);
module_exit(test_exit);
小型测试程序:
maintest。 c:
maintest.c:
#include <unistd.h>
#include <fcntl.h>
int main(void) {
long input = 256;
int fd = open("/dev/chartest0", O_RDWR);
write(fd, &input, sizeof(long));
close(fd);
return 0;
}
printk
语句以给定的输入256给出以下输出:
The printk
statements gives the following output with the given input of 256:
Write Eunction Entered.
Write count: 8, Write fp: 0
Value before cast: 0
Value after cast: 0
这同样会失败,因为 copy_from_user
的放置大小为8个字节。当一次遍历缓冲区一个字节并复制数据时,它也会失败。我已经尝试了一切。
This also fails with copy_from_user
given an in put size of 8 bytes. It also fails when iterating through the buffer one byte at a time and copying the data. I've tried everything.
如果您愿意提供帮助,请使用以下命令进行编译:
MakeFile
If you are graciously willing to help, compile with: MakeFile
ifeq ($(KERNELRELEASE),)
# Assume the source tree is where the running kernel was built
# You should set KERNELDIR in the environment if it's elsewhere
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
# The current directory is passed to sub-makes as argument
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
else
# called from kernel build system: just declare what our modules are
obj-m := chartest.o
endif
然后在同一目录中:
sudo insmod chartest.ko
最后:
sudo mknod -m 777 /dev/chartest0 c 230 0
然后您可以编译 maindriver.c
并运行它进行测试。
Then you can compile maindriver.c
and run it to test.
有人可以帮我解决此问题吗?
Can someone please help me fix this issue?
推荐答案
您不能使用 get_user
的方式:
来自 get_user文档
此宏将单个简单变量从用户空间复制到内核空间。它支持char和int之类的简单类型,但不支持结构或数组之类的较大数据类型。
ptr必须具有指向简单变量的指针类型,并且取消引用ptr的结果必须可分配到x而无需强制转换。
This macro copies a single simple variable from user space to kernel space. It supports simple types like char and int, but not larger data types like structures or arrays.
ptr must have pointer-to-simple-variable type, and the result of dereferencing ptr must be assignable to x without a cast.
使用 get_user
,您将仅复制第一个字符。
With get_user
, you will only copy the first character.
您需要使用 copy_from_user
,此函数不仅可以复制简单的类型,还可以复制数组和结构:
You need to use copy_from_user
, this function can copy array and structure, not only simple types:
ssize_t test_write(struct file *fp, const char __user *buffer, size_t count, loff_t *fpos) {
// We must validate the user's buffer and convert it to a long long
long userOperand;
unsigned char *userInput = NULL;
userInput = kmalloc(count, GFP_KERNEL);
printk(KERN_NOTICE "Write Function Entered.\n");
printk(KERN_ALERT "Write count: %ld, Write fp: %lld\n", count, *fpos);
/* warning, here you should test that count is exactly sizeof userInput */
copy_from_user(userInput, buffer, count);
userOperand = *(long*)userInput;
printk(KERN_NOTICE "Value after cast: %ld\n", userOperand);
// Increment the file position pointer (in our case, always by 8)
*fpos += count;
kfree(userInput);
return count;
}
您也可以从 char * $复制c $ c>到
copy_from_user
中的 long
(在这种情况下,没有内存分配):
You can also copy from char *
to long
in copy_from_user
(no memory alloc in that case):
ssize_t test_write(struct file *fp, const char __user *buffer, size_t count, loff_t *fpos) {
// We must validate the user's buffer and convert it to a long long
long userOperand;
printk(KERN_NOTICE "Write Function Entered.\n");
printk(KERN_ALERT "Write count: %ld, Write fp: %lld\n", count, *fpos);
/* warning, here you should test that count is exactly sizeof userOperand */
copy_from_user(&userOperand, buffer, sizeof userOperand);
printk(KERN_NOTICE "Value after reading: %ld\n", userOperand);
// Increment the file position pointer (in our case, always by 8)
*fpos += count;
return count;
}
这篇关于长时间写入linux char设备驱动程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!