Linux中与命名空间相关的内核数据结构

news/2024/10/18 23:25:24/

【摘要】本文详细讲述了在Linux内核中与命名空间概念相关的内核数据结构及其内在联系。

十、命名空间(namespace)相关数据结构

Linux内核通过数据结构之间互相的连接关系,形成了一套虚拟的命名空间的虚拟化概念。

10.1 struct pid_namespace

  • \linux-2.6.32.63\include\linux\pid_namespace.h
struct pid_namespace 
{struct kref kref;struct pidmap pidmap[PIDMAP_ENTRIES];int last_pid;
/*每个PID命名空间都具有一个进程,其发挥的作用相当于全局的init进程,init的一个目的是对孤儿调用wait4,命名空间局部的init变体也必须完成该工作。child_reaper保存了指向该进程的task_struct的指针
*/
struct task_struct *child_reaper;
struct kmem_cache *pid_cachep;/*
2. level表示当前命名空间在命名空间层次结构中的深度。初始命名空间的level为0,下一层为1,逐层递增。level较高的命名空间中的ID,对level较低的命名空间来说是可见的(即子命名空间对父命名空间可见)。从给定的level位置,内核即可推断进程会关联到多少个ID(即子命名空间中的进程需要关联从当前命名空间一直到最顶层的所有命名空间)
*/
unsigned int level;/*
3. parent是指向父命名空间的指针
*/struct pid_namespace *parent;
#ifdef CONFIG_PROC_FSstruct vfsmount *proc_mnt;
#endif
#ifdef CONFIG_BSD_PROCESS_ACCTstruct bsd_acct_struct *bacct;
#endif
};

10.2 struct pid、struct upid

  • PID的管理围绕着两个数据结构展开,struct pid是内核对PID的内部表示、struct upid则表示特定的命名空间中可见的信息。
  • \linux-2.6.32.63\include\linux\pid.h
/*
struct upid is used to get the id of the struct pid, as it is seen in particular namespace. 
Later the struct pid is found with find_pid_ns() using the int nr and struct pid_namespace *ns.
*/
struct upid {/* Try to keep pid_chain in the same cacheline as nr for find_vpid *///1. 表示ID的数值int nr;//2. 指向该ID所属的命名空间的指针struct pid_namespace *ns;/*3. 所有的upid实例都保存在一个散列表中,pid_chain用内核的标准方法实现了散列溢出链表*/struct hlist_node pid_chain;
};struct pid
{//1. 引用计数器atomic_t count;unsigned int level;/* lists of tasks that use this pid task是一个数组,每个数组项都是一个散列表头,对应于一个ID类型,因为一个ID可能用于几个进程,所有共享同一个给定ID的task_struct实例,都通过该列表连接起来,PIDTYPE_MAX表示ID类型的数目enum pid_type{PIDTYPE_PID,PIDTYPE_PGID,PIDTYPE_SID,PIDTYPE_MAX};*/struct hlist_head tasks[PIDTYPE_MAX];struct rcu_head rcu;struct upid numbers[1];
};

下载

  • 另一幅图如下:

image-20230505235815294

  • 可以看到,内核用数据结构中的这种N:N的关系,实现了一个虚拟的层次命名空间结构。

10.3 struct nsproxy

  • \linux-2.6.32.63\include\linux\nsproxy.h
/*
A structure to contain pointers to all per-process namespaces
1. fs (mount)
2. uts
3. network
4. sysvipc
5. etc
'count' is the number of tasks holding a reference. The count for each namespace, then, will be the number of nsproxies pointing to it, not the number of tasks.
The nsproxy is shared by tasks which share all namespaces. As soon as a single namespace is cloned or unshared, the nsproxy is copied.
*/
struct nsproxy 
{atomic_t count;/*1. UTS(UNIX Timesharing System)命名空间包含了运行内核的名称、版本、底层体系结构类型等信息*/struct uts_namespace *uts_ns;/*2. 保存在struct ipc_namespace中的所有与进程间通信(IPC)有关的信息*/struct ipc_namespace *ipc_ns;/*3. 已经装载的文件系统的视图,在struct mnt_namespace中给出*/struct mnt_namespace *mnt_ns;/*4. 有关进程ID的信息,由struct pid_namespace提供*/struct pid_namespace *pid_ns;/*5. struct net包含所有网络相关的命名空间参数*/struct net          *net_ns;
};
extern struct nsproxy init_nsproxy;

10.4 struct mnt_namespace

  • \linux-2.6.32.63\include\linux\mnt_namespace.h
struct mnt_namespace 
{//使用计数器,指定了使用该命名空间的进程数目atomic_t        count;//指向根目录的vfsmount实例struct vfsmount *    root;//双链表表头,保存了VFS命名空间中所有文件系统的vfsmount实例,链表元素是vfsmount的成员mnt_liststruct list_head    list;wait_queue_head_t poll;int event;
};

http://www.ppmy.cn/news/107192.html

相关文章

为kong网关添加key-auth插件实现安全认证

官方指导文档:https://docs.konghq.com/gateway/latest/get-started/key-authentication/ 一、新建一个用户 这里我们新建一个usernameluka的用户 [rootmin ~]# curl -i -X POST http://localhost:8001/consumers/ \ > --data usernameluka HTTP/1.1 201 Cr…

年营收超1700亿元 中国机器人行业走向更多场景

文 | BFT机器人 中国已经连续九年成为全球最大的工业机器人应用国,同时还是全球机器人第一生产大国,2022年机器人全行业营收超过1700亿元(人民币,下同)。随着《“机器人”应用行动实施方案》的落地,机器人应用的深度和广度加速扩…

moment获取指定日期的周x,某月最后一天

安装了moment插件的情况下,使用moment处理时间,原生的Date对象是另一回事。 非官方中文网-文档 1 当前时间 moment() 2 格式化时间 YYYY/yyyy 四位数年份 MM 两位数月份 DD 两位数天 moment().format("YYYY MM DD") 2023 05 26 moment().…

【操作系统】04.设备管理

IO软件层次 IO设备 机械部件 分类 设备控制器 寄存器编址 IO控制方式 程序直接控制方式 中断驱动方式 DMA方式 通道控制方式 中断处理程序 设备驱动程序 设备独立性软件 IO调度 设备保护 设备分配与回收 静态分配和动态分配 数据结构 设备分配步骤 改进 缓冲区管理 缓冲区的…

前端面试:iframe 的优缺点

优点 作为一个完全独立的窗口去运行另一个页面,而不用去担心污染。解决加载缓慢的第三方内容如图标和广告等的加载问题并行的加载资源文件 缺点 占用同源连接数,对于每个浏览器,都会去控制并发的同源的连接数。比如说chrome的连接数是8&#…

c语言编程练习题:7-76 计算分段函数[3]

#include <stdio.h> int main(){double x;double f;if (scanf("%lf",&x)!EOF){if (x10.0){f0.1;}else{fx;}printf("f(%.1f) %.1f",x,f);}else{printf("intput x wrong");}return 0; }代码来自&#xff1a;https://yunjinqi.top/artic…

融合PWA技术的小程序:为用户带来原生应用体验

PWA代表“渐进式网络应用”&#xff08;Progressive Web Application&#xff09;。它是一种结合了网页和移动应用程序功能的技术概念。PWA旨在提供类似于原生应用程序的用户体验&#xff0c;包括离线访问、推送通知、后台同步等功能&#xff0c;同时又具有网页的优势&#xff…

02 Redis经典五种数据类型介绍及落地运用

命令大全9大类型 String(字符类型)Hash(散列类型)List(列表类型)Set(集合类型)SortedSet(有序集合类型&#xff0c;简称zset)Bitmap(位图)HyperLogLog(统计)GEO(地理)Stream&#xff08;了解&#xff09; string 常用命令 最常用 set key valueget key 同时设置/获取多个键…