Chinaunix首页 | 论坛 | 博客
  • 博客访问: 416937
  • 博文数量: 124
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 872
  • 用 户 组: 普通用户
  • 注册时间: 2018-03-29 14:38
个人简介

默默的一块石头

文章分类

全部博文(124)

文章存档

2022年(26)

2021年(10)

2020年(28)

2019年(60)

我的朋友

分类: LINUX

2019-08-06 16:05:55

struct fs_struct {
int users;
spinlock_t lock;
seqcount_t seq;
int umask;
int in_exec;
struct path root, pwd;
};

struct path {
struct vfsmount *mnt;
struct dentry *dentry;
};

//创建进程过程
static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
{
struct fs_struct *fs = current->fs;//current进程如何维护的呢?
if (clone_flags & CLONE_FS) {
/* tsk->fs is already what we want */
spin_lock(&fs->lock);
if (fs->in_exec) {
spin_unlock(&fs->lock);
return -EAGAIN;
}
fs->users++;
spin_unlock(&fs->lock);
return 0;
}
tsk->fs = copy_fs_struct(fs);
if (!tsk->fs)
return -ENOMEM;
return 0;
}

打印相关信息代码:
#include linux/init.h
#include linux/module.h
#include linux/sched.h
#include linux/fs_struct.h
#include linux/dcache.h
#include linux/path.h
#include linux/mount.h
#include linux/fs.h

static int hello_init(void)
{
struct task_struct *task = current;
struct fs_struct *fs = task->fs;


printk( "Info\npwd %s\nroot %s\npwd d_iname %s\nroot d_iname %s\n",fs->pwd.dentry->d_name.name,fs->root.dentry->d_name.name,fs->pwd.dentry->d_iname,fs->root.dentry->d_iname);

printk( "Parent\npwd %s\nroot %s\npwd d_iname %s\nroot d_iname %s\n",fs->pwd.dentry->d_parent->d_name.name,fs->root.dentry->d_parent->d_name.name,fs->pwd.dentry->d_parent->d_iname,fs->root.dentry->d_parent->d_iname);

 printk( "vfsmount info\npwd %s\nroot %s\npwd d_iname %s\nroot d_iname %s\n",fs->pwd.mnt->mnt_root->d_name.name,fs->root.mnt->mnt_root->d_name.name,fs->pwd.mnt->mnt_root->d_iname,fs->root.mnt->mnt_root->d_iname);

printk( "vfsmount super_block filesystem name is %s\n",fs->pwd.mnt->mnt_sb->s_type->name);
return 0;
}


static void hello_exit(void)
{
printk("bye bye\n");
}


module_init(hello_init);
module_exit(hello_exit);

测试:
在路径/home/kwa/testcode/hello下执行sudo insmod hello.ko
打印信息如下:
[ 1713.390021] Info
[ 1713.390021] pwd hello
[ 1713.390021] root /
[ 1713.390021] pwd d_iname hello
[ 1713.390021] root d_iname /
[ 1713.390023] Parent
[ 1713.390023] pwd testcode
[ 1713.390023] root /
[ 1713.390023] pwd d_iname testcode
[ 1713.390023] root d_iname /
[ 2650.260153] vfsmount info
[ 2650.260153] pwd /
[ 2650.260153] root /
[ 2650.260153] pwd d_iname /
[ 2650.260153] root d_iname /
[ 3358.699346] vfsmount super_block filesystem name is ext4

阅读(1368) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~