Querying the process credentials

How do you programmatically (in a C program) query the real and effective UIDs /GIDs? Here are the system calls to do so:

#include <unistd.h>#include <sys/types.h>uid_t getuid(void);uid_t geteuid(void);gid_t getgid(void);gid_t getegid(void);

This is pretty straightforward:

  • getuid(2) returns the real UID; geteuid(2) returns the effective UID
  • getgid(2) returns the real GID; getegid(2) returns the effective GID
  • uid_t and gid_t are glibc typedefs for an unsigned integer
Here is a neat tip to figure out the typedef for any given data type: you will need to know the header file that contains the definition. Just do this: $ echo | gcc -E -xc -include 'sys/types.h' - | grep uid_t typedef unsigned int __uid_t; ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.