zoukankan      html  css  js  c++  java
  • proc文件

    参考代码:

    #include <linux/init.h>  
    #include <linux/version.h>  
    #include <linux/module.h>  
    #include <linux/sched.h>  
    #include <linux/uaccess.h>  
    #include <linux/proc_fs.h>  
    #include <linux/fs.h>  
    #include <linux/seq_file.h>   
    
    #include <asm/uaccess.h>  
     
    #define STRING_LEN 1024  
     
    char global_buffer[STRING_LEN] = {0};
    
    static int my_proc_show(struct seq_file *seq, void *v)
    {
        printk("my_proc_show called
    ");
        seq_printf(seq, "current kernel time is %ld
    ", jiffies);  
        seq_printf(seq, "global_buffer: %s", global_buffer);
    
        return 0;        
    }
    
    static int my_proc_open(struct inode *inode, struct file *file)
    {
        return single_open(file, my_proc_show, inode->i_private);
    } 
    
    static ssize_t my_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos)
    {
        if (count > 0) {
            printk("my_proc_write called
    ");
            copy_from_user(global_buffer, buffer, count);  
        }
    
        return count;
    } 
    
    struct file_operations proc_fops =
    {
        .open  = my_proc_open,
        .read  = seq_read,
        .write  = my_proc_write,
        .llseek  = seq_lseek,
        .release = single_release,
    };
    
    static struct proc_dir_entry *proc_dir = NULL;
    static struct proc_dir_entry *proc_file = NULL; 
    
    static int __init proc_test_init(void) 
    {  
        proc_dir = proc_mkdir("my_proc", NULL);
        if (!proc_dir) {
             printk(KERN_DEBUG"proc_mkdir failed");
             return 0;
        }
        
        proc_file = proc_create("buffer", 0666, proc_dir, &proc_fops);
        if (!proc_file) {
             printk(KERN_DEBUG"proc_create failed");
             return 0;
        }
    
        strcpy(global_buffer, "hello"); 
        return 0;  
    }  
     
    static void __exit proc_test_exit(void) 
    {  
        remove_proc_entry("buffer", proc_dir);  
        remove_proc_entry("my_proc", NULL);  
    }  
     
    module_init(proc_test_init);  
    module_exit(proc_test_exit);
    MODULE_AUTHOR("derek.yi");  
    MODULE_LICENSE("GPL");  
    
    
    /*
    echo "Hello from kernel" > /proc/my_proc/buffer
    cat /proc/my_proc/buffer
    */

    测试:

    derek@ubox:~/share/ldd5$ cat /proc/my_proc/buffer
    current kernel time is 4295327951
    global_buffer: hello
    derek@ubox:
    ~/share/ldd5$ echo "Hello from kernel" > /proc/my_proc/buffer derek@ubox:~/share/ldd5$ cat /proc/my_proc/buffer current kernel time is 4295330282 global_buffer: Hello from kernel
  • 相关阅读:
    Java注解入门
    两种求素数
    几个经典的递归小程序
    Java8新特性——接口的默认方法和类方法
    SSH框架总结
    初识SSH框架
    Mybatis中DAO层接口没有写实现类,Mapper中的方法和DAO接口方法是怎么绑定到一起的
    使用SQL查询所有数据库名和表名
    mybatis中#{}和${}的区别
    SOCKET, TCP/UDP, HTTP, FTP 浅析
  • 原文地址:https://www.cnblogs.com/soul-stone/p/6715788.html
Copyright © 2011-2022 走看看