The long answer to my own question, having Googled it and not found anything useful, is to sift through the source of '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. Is this pstatus_t type available in newer kernels? If so, does that mean newer kernels provide a binary interface to /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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.