/ procの名前空間番号

/ procの名前空間番号

次のように、シェルプロセスに関連する名前空間を確認しています。

# ll /proc/$$/ns
total 0
dr-x--x--x 2 root root 0 May  2 15:10 ./
dr-xr-xr-x 9 root root 0 May  1 18:39 ../
lrwxrwxrwx 1 root root 0 May  2 15:11 cgroup -> cgroup:[4026531835]
lrwxrwxrwx 1 root root 0 May  2 15:11 ipc -> ipc:[4026531839]
lrwxrwxrwx 1 root root 0 May  2 15:11 mnt -> mnt:[4026531840]
lrwxrwxrwx 1 root root 0 May  2 15:11 net -> net:[4026531957]
lrwxrwxrwx 1 root root 0 May  2 15:11 pid -> pid:[4026531836]
lrwxrwxrwx 1 root root 0 May  2 15:11 user -> user:[4026531837]
lrwxrwxrwx 1 root root 0 May  2 15:11 uts -> uts:[4026531838]

私が理解したところ、これらの項目はプロセスが関連付けられた名前空間を表します。各名前空間を識別する数字はどこから来るのでしょうか?

たとえば、上記の出力はプロセスマウントネームスペースがあることを示しますmnt:[4026531840]。マウントネームスペースを確認しました。構造:

 8  struct mnt_namespace {
 9      atomic_t        count;
10      struct ns_common    ns;
11      struct mount *  root;
12      struct list_head    list;
13      struct user_namespace   *user_ns;
14      struct ucounts      *ucounts;
15      u64         seq;    /* Sequence number to prevent loops */
16      wait_queue_head_t poll;
17      u64 event;
18      unsigned int        mounts; /* # of mounts in the namespace */
19      unsigned int        pending_mounts;
20  } __randomize_layout;

項目に使用される識別子として使用されたフィールドは表示されません/proc/PID/ns/。では、これらの識別子はどのように生成されますか?

ベストアンサー1

nsfsプロセスを名前空間に関連付けるために開いて使用できるファイルシステムによって実装されたファイルのinode番号setns(2)

あなたは見ることができますfs/nsfs.c:

int ns_get_name(char *buf, size_t size, struct task_struct *task,
                        const struct proc_ns_operations *ns_ops)
{
        struct ns_common *ns;
        int res = -ENOENT;
        const char *name;
        ns = ns_ops->get(task);
        if (ns) {
                name = ns_ops->real_ns_name ? : ns_ops->name;
                res = snprintf(buf, size, "%s:[%u]", name, ns->inum);

そしてfs/proc/namespaces.c:

static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int bufl
en)
{
        ...
        if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
                res = ns_get_name(name, sizeof(name), task, ns_ops);

おすすめ記事