kernel_read和kernel_write实例

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

static char *cache_file = "/home/kiss/demo/test";

module_param(cache_file, charp, 0664);

MODULE_PARM_DESC(cache_file, "File to use to cache pages instead of memory");

static int __init kernel_read_write_init(void)
{
	struct file *cfile;
	int err;
	ssize_t tx;
	loff_t pos = 0;
	int tmp_int[4] = {1,3,5,7};
	char *tmp_char = "hello world";
	size_t count;
	void *buf;

	if (cache_file) {
		cfile = filp_open(cache_file, O_CREAT | O_RDWR, 0664);
		if (IS_ERR(cfile)) {
			printk("%s, %s open failed\n", __func__, cache_file);
			return PTR_ERR(cfile);
		}

		if (!(cfile->f_mode & FMODE_CAN_WRITE)) {
			printk("%s, %s not writeable\n", __func__, cache_file);
			err = -EINVAL;
			goto err_close_filp;
		}

		printk("%s, before write tmp_int: tx %lld, pos %lld\n", __func__, tx, pos);
		buf = (void *)(&tmp_int);
		count = sizeof(tmp_int);
		tx = kernel_write(cfile, buf, count, &pos);
		printk("%s, after write tmp_int: tx %lld, pos %lld\n", __func__, tx, pos);

		printk("%s, before write tmp_char: tx %lld, pos %lld\n", __func__, tx, pos);
		buf = (void *)tmp_char;
		count = strlen(tmp_char)+1;
		tx = kernel_write(cfile, buf, count, &pos);
		printk("%s, after write tmp_char: tx %lld, pos %lld\n", __func__, tx, pos);

err_close_filp:
		filp_close(cfile, NULL);
	}

	return 0;
}

static void kernel_read_write_exit(void)
{
	struct file *cfile;
	int err;
	ssize_t rx;
	loff_t pos = 0;
	int tmp_int[4] = {0};
	char tmp_char[32];
	size_t count;
	void *buf;

	memset(tmp_char, 0, sizeof(tmp_char));

	if (cache_file) {
		cfile = filp_open(cache_file, O_CREAT | O_RDWR, 0664);
		if (IS_ERR(cfile)) {
			printk("%s, %s open failed\n", __func__, cache_file);
			// return PTR_ERR(cfile);
			return;
		}

		if (!(cfile->f_mode & FMODE_CAN_READ)) {
			printk("%s, %s not readable\n", __func__, cache_file);
			err = -EINVAL;
			goto err_close_filp;
		}

		printk("%s, before read tmp_int: rx %ld, pos %ld\n", __func__, rx, pos);
		count = sizeof(tmp_int);
		buf = (void *)(&tmp_int);
		rx = kernel_read(cfile, buf, count, &pos);
		printk("%s, after read tmp_int: rx %lld, pos %lld\n", __func__, rx, pos);
		printk("%s, read result: tmp_int[0]=%d, tmp_int[1]=%d\n", __func__, tmp_int[0], tmp_int[1]);
		printk("%s, read result: tmp_int[2]=%d, tmp_int[3]=%d\n", __func__, tmp_int[2], tmp_int[3]);

		printk("%s, before read tmp_char: rx %lld, pos %lld\n", __func__, rx, pos);
		count = sizeof(tmp_char);
		buf = (void *)tmp_char;
		rx = kernel_read(cfile, buf, count, &pos);
		printk("%s, after read tmp_char: rx %lld, pos %lld\n", __func__, rx, pos);		
		printk("%s, read result: tmp_char=%s\n", __func__, tmp_char);

err_close_filp:
		filp_close(cfile, NULL);
	}
}

module_init(kernel_read_write_init);
module_exit(kernel_read_write_exit);

MODULE_LICENSE ("GPL");
MODULE_AUTHOR ("kiss1994");
MODULE_DESCRIPTION ("kernel read write op");
root@ubuntu:/home/kiss/demo# ls
test
root@ubuntu:/home/kiss/demo# dmesg | grep -iE "kernel_read_write"
[31002.722518] kernel_read_write_init, before write tmp_int: tx 0, pos 0
[31002.722616] kernel_read_write_init, after write tmp_int: tx 16, pos 16

[31002.722619] kernel_read_write_init, before write tmp_char: tx 16, pos 16
[31002.722630] kernel_read_write_init, after write tmp_char: tx 12, pos 28
root@ubuntu:/home/kiss/demo# dmesg | grep -iE "kernel_read_write"
[31089.764381] kernel_read_write_exit, before read tmp_int: rx 0, pos 0
[31089.764394] kernel_read_write_exit, after read tmp_int: rx 16, pos 16
[31089.764397] kernel_read_write_exit, read result: tmp_int[0]=1, tmp_int[1]=3
[31089.764398] kernel_read_write_exit, read result: tmp_int[2]=5, tmp_int[3]=7

[31089.764400] kernel_read_write_exit, before read tmp_char: rx 16, pos 16
[31089.764404] kernel_read_write_exit, after read tmp_char: rx 12, pos 28
[31089.764406] kernel_read_write_exit, read result: tmp_char=hello world

你可能感兴趣的:(驱动程序,linux)