I need to write some C code to check the effective GID of a running process whose PID I know. I've not found any direct way to do this. There are a number of heavyweight kludges I could use, such as invoking popen("/bin/ps ARGS", "r")
and parsing its output, or fopen("/proc/PID/status", "r")
and parsing that output, but I'm looking for a cleaner, more direct approach. If only getegid()
took a PID argument...
2 Answers
I don't know of any portable way to do this. I thought maybe ptrace()
, but I can't see how from the manpage. Even if that works, "tracing" the other process in any way is probably unnecessarily invasive
For Linux, your suggestion to use fopen("/proc/PID/status", "r")
is about as clean and direct as you're going to get. It seems to be what gdb does.
-
It seems crazy to parse multiple lines of a text file to do a simple thing like this, but this is what I was afraid of. Thanks. Jul 3, 2014 at 13:39
-
2You can also do
stat("/proc/nnn", &stb)
; the process's effective gid will be instb.st_gid
. This is simultaneously more portable (it works on Linux and FreeBSD) and less portable (it's not documented). Jul 6, 2014 at 4:41 -
1
-
@MarkPlotnick That is a great idea! Somehow I never saw it when you made this comment. Can you make it an actual answer so I can mark it as such for others? Jan 1, 2017 at 20:08
This appears to be a very popular question, so I want to make the answer very clear. @MarkPlotnick gave the best answer, as a comment to the previous (pretty good) answer, but we haven't gotten his attention to repost it so I can accept it as a proper answer and help people out. So, with apologies to Mark:
Mark Plotnick says: You can also do stat("/proc/nnn", &stb); the process's effective gid will be in stb.st_gid. This is simultaneously more portable (it works on Linux and FreeBSD) and less portable (it's not documented).
Thanks Mark!
-
stat
is good. Still doesn't give additional groups. Or I can't see that. Dec 8, 2020 at 20:32 -
There are two separate issues here. A process has only one GID. Perhaps you are thinking of how a user can type "groups" and see multiple group memberships. That mechanism is unrelated. Dec 15, 2020 at 23:28
-
I was looking for a way to see all group permissions a process has, when I found this question. So it depends on lurker's needs whether it is related or not ;) btw I have upvoted your answer which is good. I am merely pointing out a limitation of this method. Dec 17, 2020 at 8:01
-
@akostadinov It is a common confusion that people expect a process can belong to multiple groups. It can't. A process belongs to a single group. Users can belong to multiple groups, and files can have permissions for multiple groups, but processes can't. Jan 7, 2021 at 23:30
-
1Here's a reasonable description of how groups work: jvns.ca/blog/2017/11/20/groups Ignore the first section. For some reason the blogger decided to start describing groups incorrectly and then correct it. Not helpful. Jan 7, 2021 at 23:46
getpgid
concerns the PGID = process group ID, which is a parameter related to job control and not related to the security credentials UID/EUID/GID/EGIDgetegid()
and that system call is only able to give you your own GID, not that of another process. Too bad about that :-(