pid로 uid 알아내기

익명 사용자의 이미지

어떤 프로세스의 pid를 알고 있을때 그 pid를 이용해서 uid를 얻는 방법이 있을까요?
예를들면, getuid(pid) 이런식으로요. 물론 이런건 없지만...
아니면 /proc/pid/status로 들어가서 얻어봐야 하나요?

해당 파일에서 uid를 얻어야 한다면
UID : 0001 0001 0001 0001 이런 식으로 저장 되어있던데 어떻게 변수에 저장을 할까요?

김정균의 이미지

C 를 원하시는 것이라면 stat을 이용하셔야 겠지요. /proc 의 pid directory 에 있는 파일의 stat 정보를 읽으시면 됩니다.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
 
int main (void) {
    int pid = getpid ();
    char path[64] = { 0, };
    struct stat st;
 
    sprintf (path, "/proc/%d/stat", pid);
 
    if ( stat (path, &st) == -1 ) {
        fprintf (stderr, "path is something worng (%s)\n", path);
        exit (1);
    }
 
    printf ("The UID of %s is %d\n", path, st.st_uid);
    return 0;
}