![/proc의 네임스페이스 번호](https://linux55.com/image/152915/%2Fproc%EC%9D%98%20%EB%84%A4%EC%9E%84%EC%8A%A4%ED%8E%98%EC%9D%B4%EC%8A%A4%20%EB%B2%88%ED%98%B8.png)
다음과 같이 셸 프로세스와 관련된 네임스페이스를 확인하고 있습니다.
# 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);
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);