struct pid *find_pid_ns(int nr, struct pid_namespace *ns) 用于从namespace 下的pid找到对应的pid结构体
其中pid结构体定义如下:
struct pid
{
atomic_t count;
unsigned int level;
/* lists of tasks that use this pid */
struct hlist_head tasks[PIDTYPE_MAX];
struct rcu_head rcu;
struct upid numbers[1];
};
其使用的例子如下:
static int convert_fuse_file_lock(struct fuse_conn *fc,
const struct fuse_file_lock *ffl,
struct file_lock *fl)
{
rcu_read_lock();
fl->fl_pid = pid_vnr(find_pid_ns(ffl->pid, fc->pid_ns));
rcu_read_unlock();
}
其源码分析如下:
首先根据ns和nr在pid_hash中找到对应的项,即pid_hash[pid_hashfn(nr, ns)],然后遍历pid_hash[pid_hashfn(nr, ns)]
找到其中的pid_chain,并复制给pnr,如果pnr的成员变量nr和ns分别等于形参中的nr和nr,则pnr就是我们我们要找到
最后通过pid中的成员变量upid返回pid结构体的指针,如果没有找到的话则返回null.
struct pid *find_pid_ns(int nr, struct pid_namespace *ns)
{
struct upid *pnr;
hlist_for_each_entry_rcu(pnr,
&pid_hash[pid_hashfn(nr, ns)], pid_chain)
if (pnr->nr == nr && pnr->ns == ns)
return container_of(pnr, struct pid,
numbers[ns->level]);
return NULL;
}
进程管理API之find_pid_ns
最新推荐文章于 2022-11-03 10:47:16 发布