misc设备,内核态记录文件

简介: 1,kernel#include #include #include #include #include #include #include #include #include #include #include #define MISC_NAME ...

1,kernel

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/spinlock.h>
#include <linux/blkdev.h>
#include <linux/module.h> 
#include <linux/fs.h> 
#include <linux/errno.h> 
#include <linux/mm.h> 
#include <linux/cdev.h> 
#include <linux/miscdevice.h>
     
#define MISC_NAME   "miscdriver"

#define IO_CMD_LEN      1024  

typedef struct 
{
    int file_id; //todo
    int rw_pos;  //todo
    int cmd_type;
    int buff_len;
    char io_buff[IO_CMD_LEN - 16];
}KLOG_CMD_INFO;

#if 1

struct file *klog_fp = NULL;
loff_t klog_pos = 0;

static int user_cmd_proc(char *user_cmd, char *out_str)
{
    KLOG_CMD_INFO *klog_cmd = (KLOG_CMD_INFO *)user_cmd;
    KLOG_CMD_INFO *klog_ack = (KLOG_CMD_INFO *)out_str;
    
    if(klog_cmd->cmd_type == 1) { //open file
        if (klog_fp != NULL)  filp_close(klog_fp, NULL);
            
        klog_fp = filp_open(klog_cmd->io_buff, O_RDWR | O_CREAT | O_TRUNC, 0644);
        if (IS_ERR(klog_fp)){
            printk("filp_open error \n");
            return -1;
        }
        klog_pos = 0;
        sprintf(klog_ack->io_buff, "filp_open ok\n");
    }
    
    if(klog_cmd->cmd_type == 2) { //write file
        mm_segment_t old_fs;
        old_fs = get_fs();
        set_fs(KERNEL_DS); //to check why
        klog_pos += vfs_write(klog_fp, klog_cmd->io_buff, klog_cmd->buff_len, &klog_pos);
        set_fs(old_fs);
        sprintf(klog_ack->io_buff, "vfs_write ok\n");
    }

    if(klog_cmd->cmd_type == 3) { //close file
        filp_close(klog_fp, NULL);
        klog_fp = NULL;
        sprintf(klog_ack->io_buff, "filp_close ok\n");
    }
    
    return 0;
}
#endif

char user_cmd[IO_CMD_LEN] = {0};
char out_str[IO_CMD_LEN] = {0};
 
static int misc_open(struct inode *inode, struct file *file)
{
    printk("misc_open\n");
    return 0;
}
 
static int misc_ioctl( struct file *file, unsigned int cmd, unsigned long arg)
{    
    printk("misc_ioctl: %d \n", cmd);    
    
    if(copy_from_user(user_cmd,  (int *)arg, IO_CMD_LEN)) 
        return -EFAULT;
    
    user_cmd_proc(user_cmd, out_str);
    
    if(copy_to_user( (int *)arg, out_str, IO_CMD_LEN)) 
        return -EFAULT;

    return 0;
}

static ssize_t misc_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos)  
{
    int ret;
    
    if(IO_CMD_LEN <= size) return 0;

    if (copy_from_user(user_cmd, buf, size))  
        ret = - EFAULT;  

    user_cmd[size] = 0;
    printk(KERN_INFO "written %d bytes: %s\n", size, user_cmd);  
    return 0;
}
 
static const struct file_operations misc_fops =
{
    .owner   =  THIS_MODULE,
    .open    =  misc_open,
    .write   =  misc_write,  
    .unlocked_ioctl = misc_ioctl,
};
 
static struct miscdevice misc_dev =
{
    .minor = MISC_DYNAMIC_MINOR,
    .name = MISC_NAME,
    .fops = &misc_fops,
};
 
 
static int __init misc_init(void)
{
    int ret;
     
    ret = misc_register(&misc_dev);
    if (ret)
    {
        printk("misc_register error\n");
        return ret;
    }
 
    return 0;
}
 
static void __exit misc_exit(void)
{
    misc_deregister(&misc_dev);
}
 
module_init(misc_init);
module_exit(misc_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("derek");

 

2,app

#include <stdio.h>  
#include <fcntl.h>  
#include <stdlib.h>  
#include <string.h>  
#include <sys/types.h>  
#include <sys/stat.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sys/syscall.h>

#define IO_CMD_LEN      1024  

typedef struct 
{
    int file_id; //todo
    int rw_pos;  //todo
    int cmd_type;
    int buff_len;
    char io_buff[IO_CMD_LEN - 16];
}KLOG_CMD_INFO;

KLOG_CMD_INFO cmd_info;

#if 0
int main()
{
    int fd;
    int ret = 0;

    fd = open("/dev/miscdriver", O_RDWR);
    if( fd < 0 ) {
        printf("open miscdriver WRONG!\n");
        return 0;
    }

    sprintf(cmd_info.io_buff, "/home/derek/klog.txt");
    cmd_info.cmd_type = 1;
    ret = ioctl(fd, 0, &cmd_info);
    printf("open: ret=%d rdata:%s\n", ret, cmd_info.io_buff);

    for(int i = 0; i < 10; i++) {
        sprintf(cmd_info.io_buff, "%d: ==============================\n", i);
        cmd_info.buff_len = strlen(cmd_info.io_buff);
        cmd_info.cmd_type = 2;
        ret = ioctl(fd, 0, &cmd_info);
        printf("read: ret=%d rdata:%s\n", ret, cmd_info.io_buff);
    }

    cmd_info.cmd_type = 3;
    ret = ioctl(fd, 0, &cmd_info);
    printf("close: ret=%d rdata:%s\n", ret, cmd_info.io_buff);
   
    close(fd);
    return 0;
}
#else

int main()
{
    int fd;
    int ret = 0;
    char buf[]="this is a example for character devices driver by derek!";

    fd = open("/dev/miscdriver", O_RDWR);
    if( fd < 0 ) {
        printf("open miscdriver WRONG!\n");
        return 0;
    }

    ret = write(fd, buf, sizeof(buf));
    printf("write: ret=%d \n", ret);
   
    close(fd);
    return 0;
}

#endif

 

相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务&nbsp;ACK 容器服务&nbsp;Kubernetes&nbsp;版(简称&nbsp;ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情:&nbsp;https://www.aliyun.com/product/kubernetes
目录
相关文章
|
9月前
|
Ubuntu Linux 编译器
字符驱动设备原理及其相关函数(一)
字符驱动设备原理及其相关函数(一)
76 0
|
4月前
|
存储 Windows
4.6 Windows驱动开发:内核遍历进程VAD结构体
在上一篇文章`《内核中实现Dump进程转储》`中我们实现了ARK工具的转存功能,本篇文章继续以内存为出发点介绍`VAD`结构,该结构的全程是`Virtual Address Descriptor`即`虚拟地址描述符`,VAD是一个`AVL`自`平衡二叉树`,树的每一个节点代表一段虚拟地址空间。程序中的代码段,数据段,堆段都会各种占用一个或多个`VAD`节点,由一个`MMVAD`结构完整描述。
38 0
4.6 Windows驱动开发:内核遍历进程VAD结构体
|
5月前
|
存储 NoSQL Linux
【Linux】进程信号中的 core dump 标记位
【Linux】进程信号中的 core dump 标记位
|
7月前
|
Linux Shell
内核调试之devmem直接读写寄存器
内核调试之devmem直接读写寄存器
|
9月前
|
Linux
内核是如何运行ko文件的--系统调用
内核是如何运行ko文件的--系统调用
183 0
|
9月前
|
Linux
字符驱动设备原理及其相关函数(二)
字符驱动设备原理及其相关函数(二)
36 0
|
10月前
|
存储 Linux 编译器
【Linux】基础IO(二)--- 理解内核级和用户级缓冲区、磁盘与ext系列文件系统、inode与软硬连接(上)
【Linux】基础IO(二)--- 理解内核级和用户级缓冲区、磁盘与ext系列文件系统、inode与软硬连接(上)
148 0
|
10月前
|
存储 缓存 算法
【Linux】基础IO(二)--- 理解内核级和用户级缓冲区、磁盘与ext系列文件系统、inode与软硬连接(下)
【Linux】基础IO(二)--- 理解内核级和用户级缓冲区、磁盘与ext系列文件系统、inode与软硬连接(下)
139 1
|
存储 固态存储 Linux
【Linux】基础IO —— 系统文件IO | 文件描述符fd | inode | 重定向原理 | 缓冲区 | 软硬链接
如果学习文件操作,只停留在语言层面,很难对文件有深刻理解。这也是一定程度导致我对它印象不深刻,每次写都要回看文档,现在要站在系统角度重新理解。的确,学了这儿我写文件操作自信多了。 本文重点:深入理解文件描述符`fd`;理解文件系统中`inode`的概念;软硬链接。
355 0
【Linux】基础IO —— 系统文件IO | 文件描述符fd | inode | 重定向原理 | 缓冲区 | 软硬链接
|
网络协议 Unix Linux
嵌入式 uboot以及kernel添加看门狗临时记录(个人记录未整理乱)
Uboot_Kernerl_Add_Watch_Dog:   U-Boot 2010.06 (Nov 01 2013 - 15:28:44) DRAM:  128 MiBCheck spi flash controller v350.
5026 0