Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 是否存在用于获取正在运行的进程的uid/gid的系统调用?_C_Linux_Process - Fatal编程技术网

C 是否存在用于获取正在运行的进程的uid/gid的系统调用?

C 是否存在用于获取正在运行的进程的uid/gid的系统调用?,c,linux,process,C,Linux,Process,对于我自己的问题,谷歌搜索了一下,没有发现任何有用的东西,我的答案很长,就是筛选“ps”的来源。但在我这么做之前,有没有人愿意提供懒汉的解决方案呢?:-) 我发现了这个问题:然而,解决方案似乎在3.2内核上不可用。这种状态类型在较新的内核中可用吗?如果是这样的话,这是否意味着较新的内核提供了一个到/proc//status的二进制接口?目前,我能想到的唯一可行的解决方案就是类似的东西。显然,我还没有尽力去看看这是否真的像我所期望的那样有效…: int len, pid, n, fd = open

对于我自己的问题,谷歌搜索了一下,没有发现任何有用的东西,我的答案很长,就是筛选“ps”的来源。但在我这么做之前,有没有人愿意提供懒汉的解决方案呢?:-)


我发现了这个问题:然而,解决方案似乎在3.2内核上不可用。这种状态类型在较新的内核中可用吗?如果是这样的话,这是否意味着较新的内核提供了一个到/proc//status的二进制接口?

目前,我能想到的唯一可行的解决方案就是类似的东西。显然,我还没有尽力去看看这是否真的像我所期望的那样有效…:

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);
}
int len,pid,n,fd=open(“/proc/12345/status”,O_RDONLY | O_NOATIME);
char buf[4096],空格[50];
如果(0<(len=读取(fd,buf,4096)))
{
n=sscanf(buf,“Uid:%s%d”、空格和pid);
}

我知道没有系统调用,但因为我需要相同的调用,所以我编写了这个小程序。享受

static int getPuid (int gpid) 
{ // by Zibri http://www.zibri.org
    char fname[256];
    char buf[256];
    int pid=8;
    sprintf(fname,"/proc/%d/status",gpid);
    FILE *proc; 
    proc = fopen(fname,"r");    
    if (proc) { 
        while(pid--) fgets(buf,256,proc); // skip first 8 lines
        sscanf(buf,"Uid:\t%lu\t",&pid); 
    } else return -1;   
    fclose(proc);   
    return pid;
}

如果我没有弄错的话,在这种情况下可以使用一些系统调用:

#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.
#包括
#包括
geteuid()//返回调用进程的有效用户ID。
getuid()//返回调用进程的实际用户ID。
getgid()//返回调用进程的实际组ID。
getegid()//返回调用进程的有效组ID。
有关更多信息,请参见以下链接:


/proc/self/status似乎是一个很好的候选者,但是如果没有安装/proc怎么办?您希望它用于当前进程,还是用于给定pid的任何其他进程?不是当前进程,而是基于pid的进程。忽略/proc/self,这只是我将命令行思想转换为漫无边际的想法。实际上我指的是/proc//status。那么
/proc//status
可能是您最好的选择。较旧的Unix系统(例如SunOS 4.x)没有
/proc
,但是
ps
命令工作正常。在给定进程PID的情况下,必须有另一种机制来获取有关进程的信息。我不知道该机制是如何特定于系统的,也不知道它是否仍然存在于Linux(或Solaris,或…)中。只需
stat
the
/proc/pid
目录并读取st_uid和st_gid值不是更容易吗?这对于获取“真实”uid和gid是可以的。它不会为您提供有效或有效的fs,当然也不会为您提供辅助组。这些仅适用于呼叫过程。OP需要给定pid的任何进程的uid/gid。