kernel_read和kernel_write实例
#include <linux/init.h> #include <linux/types.h> #include <linux/module.h> #include <linux/moduleparam.h> #include <linux/vmalloc.h> #include <linux/math64.h> #include <linux/slab.h> #include <linux/errno.h> #include <linux/string.h> #include <linux/delay.h> #include <linux/list.h> #include <linux/random.h> #include <linux/sched.h> #include <linux/sched/mm.h> #include <linux/fs.h> #include <linux/pagemap.h> #include <linux/seq_file.h> #include <linux/debugfs.h> 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 ", __func__, cache_file); return PTR_ERR(cfile); } if (!(cfile->f_mode & FMODE_CAN_WRITE)) { printk("%s, %s not writeable ", __func__, cache_file); err = -EINVAL; goto err_close_filp; } printk("%s, before write tmp_int: tx %lld, pos %lld ", __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 ", __func__, tx, pos); printk("%s, before write tmp_char: tx %lld, pos %lld ", __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 ", __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 ", __func__, cache_file); // return PTR_ERR(cfile); return; } if (!(cfile->f_mode & FMODE_CAN_READ)) { printk("%s, %s not readable ", __func__, cache_file); err = -EINVAL; goto err_close_filp; } printk("%s, before read tmp_int: rx %ld, pos %ld ", __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 ", __func__, rx, pos); printk("%s, read result: tmp_int[0]=%d, tmp_int[1]=%d ", __func__, tmp_int[0], tmp_int[1]); printk("%s, read result: tmp_int[2]=%d, tmp_int[3]=%d ", __func__, tmp_int[2], tmp_int[3]); printk("%s, before read tmp_char: rx %lld, pos %lld ", __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 ", __func__, rx, pos); printk("%s, read result: tmp_char=%s ", __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
上一篇:
IDEA上Java项目控制台中文乱码