[英]Is there a system call for obtaining the uid/gid of a running process?
The long answer to my own question, having Googled it and not found anything useful, is to sift through the source of 'ps'. 对我自己的问题的长期回答,用Google搜索并没有发现任何有用的东西,就是筛选'ps'的来源。 But before I do that, is there anyone willing to provide the lazy man's solution?
但在我这样做之前,是否有人愿意提供懒人的解决方案? :-)
:-)
I found this question: Knowing the process status using procf/<pid>/status However, the solution doesn't seem to be available on the 3.2 kernel. 我发现了这个问题: 使用procf / <pid> / status了解进程状态但是,3.2内核似乎没有提供解决方案。 Is this pstatus_t type available in newer kernels?
这个pstatus_t类型是否在较新的内核中可用? If so, does that mean newer kernels provide a binary interface to /proc//status?
如果是这样,这是否意味着较新的内核为/ proc // status提供二进制接口?
At the moment, the only viable solution I can come up with is something along the lines of this. 目前,我能想出的唯一可行的解决方案就是这样。 Obviously, not gone to the effort to see if this actually works as I would expect it to yet...:
显然,没有去努力看看这是否真的像我期望的那样......:
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);
}
There is not a system call that I know of but since I needed the same, I wrote this small program. 没有我知道的系统调用,但由于我需要相同的,我编写了这个小程序。 Enjoy.
请享用。
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;
}
If I'm not mistaken there are some system calls you can use for this situation: 如果我没弄错的话,可以使用一些系统调用来解决这种情况:
#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.
See these links for more information: 有关更多信息,请参阅以下链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.