get uid from pid(process id) from kernel level cod

2019-08-12 22:35发布

问题:

I would like to get uid of a pid. Please note that the uid that I want to get is NOT of the current process. Please let me know if you have an idea. Maybe can I get it by reuse the function of /proc/[pid]/status read function in kernel? //Bruce

回答1:

  1. Search process descriptor (struct task_struct) with given pid.

  2. Use task->cred->uid.

Struct struct task_struct is defined in include/linux/sched.h. Struct struct cred is defined in include/linux/cred.h.

Note, that iterating through processes and reading process's credentials should be done under RCU-critical section.