Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a system call for obtaining the uid/gid of a running process?

Tags:

c

linux

process

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?

like image 395
Craig Avatar asked Nov 02 '22 17:11

Craig


1 Answers

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);
}
like image 66
Craig Avatar answered Nov 09 '22 11:11

Craig