BSc*_*ker 5 c linux kernel pid linux-namespaces
今天在Linux内核命名空间方面遇到一些困难,特别是将唯一PID命名空间内的PID与全局PID命名空间内的PID相关联
我需要能够执行以下操作之一:
a)使用命名空间分配的PID从全局范围中终止进程
要么
b)将特定于命名空间的PID转换为全局PID,因此我可以从全局范围中删除PID
要么
c)在PID命名空间内启用进程以向我报告其全局PID,因此我可以从全局范围中终止PID
有在包含在命名空间场景的PID信息的流程结构进行一些讨论在这里.我不确定如何/如果我可以从用户态应用程序访问这些结构,或者我是否需要通过内核hack添加支持.
为什么? 我有一个当前使用网络命名空间的应用程序.我正在添加对PID命名空间的支持.以下是它目前的工作原理:
在引入PID命名空间之前: 主应用程序当前在另一个网络命名空间中启动bash控制台.然后它使用该bash控制台启动程序,并让这些程序报告其当前的PID.当主应用程序想要杀死该网络命名空间中的子进程时,它只是告诉操作系统杀死报告的PID.
使用PID命名空间(损坏状态): 主应用程序当前在另一个网络和PID命名空间中启动bash控制台.然后它使用该bash控制台启动程序,并让这些程序报告其当前的PID.但是,报告的当前PID在全局PID命名空间中无效(当全局命名空间中的PID为56000时,它可能为10).因此,主应用程序无法终止该网络+ PID命名空间中的子进程
与往常一样,任何指导表示赞赏
一种方法可以在 pid 队列中搜索与目标 pid 匹配的进程描述符,如果报告 shell 位于同一工作空间上,它可以进行系统调用来获取其他进程的“进程描述符”并进行某种 for 循环在 /proc/< pid> 中查找进程描述
您可能还想看看这里:http://lkml.indiana.edu/hypermail/linux/kernel/0707.0/1701.html 特别是这部分:
/*
* the helpers to get the pid's id seen from different namespaces
*
* pid_nr() : global id, i.e. the id seen from the init namespace;
* pid_vnr() : virtual id, i.e. the id seen from the namespace this pid
* belongs to. this only makes sence when called in the
* context of the task that belongs to the same namespace;
* pid_nr_ns() : id seen from the ns specified.
*
* see also task_xid_nr() etc in include/linux/sched.h
*/
static inline pid_t pid_nr(struct pid *pid)
{
pid_t nr = 0;
if (pid)
- nr = pid->nr;
+ nr = pid->numbers[0].nr;
return nr;
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你!
此致!
| 归档时间: |
|
| 查看次数: |
1001 次 |
| 最近记录: |