Unix time sharing system namespace

定义在include/linux/utsname.h,实现在kernel/utsname.c中。

新utsnamespace的创建。

struct uts_namespace *copy_utsname(unsigned long flags, struct uts_namespace *old_ns)
{
        struct uts_namespace *new_ns;
        //BUG_ON (定义在asm-generic/bug.h )
        //如果old_ns为空则直接,报错。
        BUG_ON(!old_ns);
        //kref加一
        get_uts_ns(old_ns);
        //flgs标记位的CLONE_NEWUTS没有置一
        //也就是父子进程使用同一个uts namespace
        if (!(flags & CLONE_NEWUTS))
                return old_ns;

        new_ns = clone_uts_ns(old_ns);

        put_uts_ns(old_ns);
        return new_ns;
}

clone_uts_ns的实现如下:

static struct uts_namespace *clone_uts_ns(struct uts_namespace *old_ns)
{
        struct uts_namespace *ns;
        //分配内存
        ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
        //分配不成功,报错
        if (!ns)
                return ERR_PTR(-ENOMEM);
        //P(uts_sem) uts_sem是信号量,定义在include/linux/utsname.h
        down_read(&uts_sem);
        memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
        //V
        up_read(&uts_sem);
        //kref初始化为1
        kref_init(&ns->kref);
        return ns;
}

results matching ""

    No results matching ""