是否有系统调用来获取正在运行的进程的uid / gid?

时间:2013-08-06 11:52:54

标签: c linux process

对我自己的问题的长期回答,用谷歌搜索并没有发现任何有用的东西,就是筛选出来的' ps。但在我这样做之前,是否有人愿意提供懒惰的解决方案? : - )

我发现了这个问题:Knowing the process status using procf/<pid>/status但是,解决方案似乎并没有在3.2内核上提供。这个pstatus_t类型是否在较新的内核中可用?如果是这样,这是否意味着较新的内核为/ proc // status提供二进制接口?

3 个答案:

答案 0 :(得分:1)

目前,我能想出的唯一可行解决方案就是这样。显然,没有去努力看看这是否真的像我期望的那样......:

int len, pid, n, fd = open("/proc/12345/status", O_RDONLY | O_NOATIME);
char buf[4096], whitespace[50];

if (0 < (len = read(fd, buf, 4096)))
{
    n = sscanf(buf, "Uid:%s%d ", whitespace, &pid);
}

答案 1 :(得分:0)

我没有系统调用,但由于我需要相同的系统调用,我编写了这个小程序。享受。

df = (pd.lreshape(df, {'Value':['Value1','Value2'], 'Field':['Field1','Field2']})
        .groupby(['id', 'Field'], as_index=False)['Value'].sum())

print (df)
   id Field  Value
0   1     A    1.0
1   1     B    0.0
2   2     A    1.0
3   2     D    1.0
4   3     A    1.0
5   3     C    2.0
6   4     E    0.0
7   4     F    1.0

答案 2 :(得分:0)

如果我没记错的话,可以使用某些系统调用来解决这种情况:

#include <unistd.h>
#include <sys/types.h>

geteuid() //returns the effective user ID of the calling process.
getuid() //returns the real user ID of the calling process.
getgid() //returns the real group ID of the calling process.

getegid() //returns the effective group ID of the calling process.

有关更多信息,请参见以下链接:

Unix manual page for getgid()

unix manual page for getuid()