Proc file

Proc File

Proc FS is called the process file system, and in the /proc folder, detailed information of each process is stored. At the same time, proc also serves as a means of interaction between the application layer and the kernel.

Proc function

In addition to storing detailed information about each process, proc FS also stores kernel state data in the proc virtual file system for debugging.

Proc usage

From the perspective of use, the files under proc are usually readable, that is, we read some state information of the kernel through proc, and the reading method: cat /proc/cpuinfo.
It is not excluded that some proc files are writable , that is, write some configuration information from the user mode to the proc file, thereby changing the operation of the kernel. Configuration method: echo /proc/xxx
Note: The original purpose of proc fs is the process file system, so the information that is not related to the process placed in the proc folder is gradually being migrated to the sys file system.

Common proc information

  • cat /proc/info
  • cat /proc/meminfo
  • cat /proc/mounts
  • cat /proc/cmdline
  • cat /proc/devices
  • cat /proc/filesystems
  • cat /proc/modules
  • cat /proc/partitions

Design one Proc File.

Note: The usage here is based on the Linux 4.1 version, and the usage will be different as the version changes.

    #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 <linux/slab.h>

    struct proc_dir_entry *proc_dir;
    static char * str = NULL;

    static int my_proc_show(struct seq_file *m, void * v)
    {
        seq_printf(m,"current kernel time is %ld\n", jiffies);
        seq_printf(m,"str is %s\n", str);
        return 0;
    }

    static int my_proc_open(struct inode * inode, struct file *file)
    {
        return single_open(file, my_proc_show, NULL);
    }

    static struct file_operations my_fops =
    {
        .owner = THIS_MODULE,
        .open = my_proc_open,
        .release = single_release,
        .read = seq_read,
        .llseek = seq_lseek,
    };

    static int __init my_init(void)
    {
        struct proc_dri_entry *file;
        
        proc_dir = proc_mkdir("myproc", NULL);
        if (!proc_dir)
        {
            return -ENOMEM;
        }
        
        file = proc_create("abc_proc", 0644, proc_dir, &my_fops);
        if (!file)
            return -ENOMEM;
        return 0;    
    }

    static void __exit my_exit(void)
    {
        
        remove_proc_entry("abc_proc", proc_dir);
        remove_proc_entry("myproc", NULL);
        kfree(str);
    }

    module_init(my_init);
    module_exit(my_exit);
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("Johnson");

Makefile

    obj-m :=proc_test.o
    KERNEL := /lib/modules/`uname -r`/build

    all:
    make -C $(KERNEL) M=`pwd` modules

    install:
    make -C $(KERNEL) M=`pwd` modules_install

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325271607&siteId=291194637