第10章 无持久存储的文件系统 (2)

目录

10.1 proc文件系统

10.1.2 数据结构

10.1.3 初始化

10.1.4 装载 proc 文件系统

10.1.5 管理 /proc 数据项

10.1.6 读取和写入信息

10.1.7 进程相关信息

10.1.8 系统控制机制


本专栏文章将有70篇左右,欢迎+关注,查看后续文章。

 

10.1 proc文件系统

10.1.2 数据结构

1. proc数据项的表示

struct   proc_dir_entry   {                 // 表示proc中一个文件。

        unsigned int        low_ino;         // inode索引节。

        umode_t             mode;            // 文件类型,权限等。

        nlink_t                 nlink;

        kuid_t                  uid;               // 通常为0,即root。

        kgid_t                  gid;               // 通常为0,即root。

        loff_t                    size;

        struct inode_operations         *proc_iops;

        struct file_operations              *proc_fops;

        struct proc_dir_entry              *next, *parent, *subdir;       //   目录层级关系。

        atomic_t                                 count;                                 // 引用计数。

        u8                                           namelen;                            // 文件名长度。

        char                                         name[  ];

}

2. proc  inode

struct   proc_inode   {

        struct pid                          *pid;         // cat /proc/pid/*** 时,有意义。

        unsigned int                      fd;           // cat /proc/ <pid> fd/ 时,有意义。

        union proc_op                   op;

        struct proc_dir_entry         *pde;

        struct inode                       vfs_inode;

};

union   proc_op {

        int         (*proc_get_link) (struct  dentry  *,   struct  path  *);

        int         (*proc_read) (struct  task_struct   *task,    char  *page);

        int         (*proc_show) (struct  seq_file   *m,    struct  pid_namespace   *ns,    struct  pid   *pid,    struct task_struct   *task);

};

10.1.3 初始化

proc_root_init 函数

        作用:初始化proc文件系统。

10.1.4 装载 proc 文件系统

mount   -t proc   proc   /proc

mount   -t ext4   /dev/sdb1   /mnt/data

struct   file_system_type    proc_fs_type   =   {

        .name   =   "proc",

        .mount   =   proc_mount,         // 调用 proc_fill_super。

        .kill_sb   =   proc_kill_sb,

        .fs_flags   =   FS_USERNS_MOUNT,

};

proc_mount

        -> proc_fill_super(struct   super_block   *s)

                -> s->s_op   =   &proc_sops;

10.1.5 管理 /proc 数据项

1. 创建和注册一个数据项

创建函数:

        struct   proc_dir_entry   *proc_create( char  *name,   umode_t  mode,

                struct proc_dir_entry   *parent,    struct file_operations   *proc_fops)

举例:

创建 /proc/ vmallocinfo 文件:

        proc_create("vmallocinfo",   S_IRUSR,   NULL,   &proc_vmalloc_operations);

struct   file_operations    proc_vmalloc_operations   =   {

        .open   =   vmalloc_open,

        .read   =   seq_read,         //读 /proc/vmallocinfo 时调用。

        .llseek   =   seq_lseek,

        .release   =   seq_release_private,

};

辅助函数:

        proc_mkdir:创建新目录。

        proc_symlink:创建符号链接。

2. 查找 proc 数据项

从 /proc 根节点开始查找。

struct   inode_operations    proc_root_inode_operations   =   {

        .lookup   =   proc_root_lookup;

};

10.1.6 读取和写入信息

每个 /proc 文件可自定义 struct   file_operations,自己实现read,write等函数指针。

举例 cat /proc/cpuinfo(不可写)

struct   file_operations    proc_cpuinfo_operations   =   {

        .open       =     cpuinfo_open,

        .read        =     seq_read,

        .llseek      =     seq_lseek,

        .release    =     seq_release,

};

10.1.7 进程相关信息

proc_pid_lookup:负责打开 /proc/<pid>/ 中文件。

10.1.8 系统控制机制

sysctl 系统调用已过时。已移到/proc/sys

注册/proc/sys文件:

        register_sysctl_table(sd_ctl_root);

struct  ctl_table sd_ctl_root[  ]   =   {

{

        .procname   =    "kernel",

        .child            =    sd_ctl_dir,

},

};

struct   ctl_table    sd_ctl_dir[ ]   =   {

        {

                .procname    =    "sched_domain",   //即 /proc/ sys/ kernel/ sched_domain文件。

        },

};

struct   file_operations    proc_sys_file_operations   =   {

        .open   =   proc_sys_open,

        .read   =   proc_sys_read,

        .write   =   proc_sys_write,

};

  • 15
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山下小童

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值