Is there a system call to get the uid / gid of the current process?

The long answer to my own question, having Googled and not found anything useful, is to sift the source of "ps". But before I do this, are there anyone willing to provide a lazy solution ?:-)

I found this question: Knowing the status of a process using procf / <pid> / status However, the solution does not seem to be available in kernel 3.2. Is this type of pstatus_t available for new kernels? If so, does this mean that newer kernels provide a binary interface for / proc // status?

+4
source share
3 answers

Right now, the only viable solution I can come up with is something like this. Obviously, I didn’t make an attempt to see if this really works, as I would expect this ...:

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
source

There is no system call that I know of, but since I needed the same thing, I wrote this little program. Enjoy it.

 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; } 
0
source

If I'm not mistaken, there are some system calls that 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:

Unix man page for getgid ()

Unix man page for getuid ()

0
source

Source: https://habr.com/ru/post/1495521/


All Articles