第二步是从struct pid找到task struct,代码如下:
struct task_struct *pid_task(struct pid *pid, enum pid_type type)
{
struct task_struct *result = NULL;
if (pid) {
struct hlist_node *first;
first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]),
lockdep_tasklist_lock_is_held());
if (first)
result = hlist_entry(first, struct task_struct, pids[(type)].node);
}
return result;
}
5、getpgid是如何实现的?
SYSCALL_DEFINE1(getpgid, pid_t, pid)
{
struct task_struct *p;
struct pid *grp;
int retval;
rcu_read_lock();
if (!pid)
grp = task_pgrp(current);
else {
retval = -ESRCH;
p = find_task_by_vpid(pid);
if (!p)
goto out;
grp = task_pgrp(p);
if (!grp)
goto out;
retval = security_task_getpgid(p);
if (retval)
goto out;
}
retval = pid_vnr(grp);
out:
rcu_read_unlock();
return retval;
}
当传入的pid number等于0的时候,getpgid实际上是获取当前进程的process groud ID number,通过task_pgrp可以获取该进程的使用的表示progress group ID对应的那个pid对象。如果调用getpgid的时候给出了非0的process ID number,那么getpgid实际上是想要获取指定pid number的gpid。这时候,我们需要调用find_task_by_vpid找到该pid number对应的task struct。一旦找到task struct结构,那么很容易得到其使用的pgid(该实体是struct pid类型)。至此,无论哪一种参数情况(传入的参数pid number等于0或者非0),我们都找到了该pid number对应的struct pid数据对象(pgid)。当然,最终用户空间需要的是pgid number,因此我们需要调用pid_vnr找到该pid在当前namespace中的pgid number。
getsid的代码逻辑和getpid是类似的,不再赘述。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-76987-6.html
还特么学法律的