Unix to Windows Porting Dictionary for HPC

RSS

Links

Function List

getuid


Unix

header files: unistd.h, sys/types.h

uid_t getuid();
uid_t geteuid();

Windows

BOOL WINAPI OpenProcessToken(
 __in   HANDLE ProcessHandle,
 __in   DWORD DesiredAccess,
 __out  PHANDLE TokenHandle
);
      
BOOL GetTokenInformation(
 __in       HANDLE TokenHandle,
 __in       TOKEN_INFORMATION_CLASS TokenInformationClass,
 __out_opt  LPVOID TokenInformation,
 __in       DWORD TokenInformationLength,
 __out      PDWORD ReturnLength
);
      

Purpose

Obtains the user identity that the process is running as.

Discussion

Unix systems maintain a user identity (UID) as a numeric value that is guaranteed unique only for the system the process is running on. In contrast, on Windows, a security identifier (SID) that is unique across all Windows systems is used to identify a user. A SID necessarily consumes more storage space than a UID.

The information about the SID for a running process is stored in the security token for a process. The SID is obtained indirectly by performing an information query on the security token of a process.

A Unix process maintains a distinction between the UID of the user that started the process (the real UID) and the UID of the user that the process is running as (the effective UID). Windows does not maintain this distinction. The value of a SID will be the equivalent of the Unix effective UID. Since the equivalent of the Unix real UID cannot be obtained using the SID is recommended for Windows when the real or effective UID is needed.

Example of Use in Windows

#include <Winbase.h>

HANDLE Thandle;
DWORD DAmask, size;
SID SidInfo;

DAmask = TOKEN_READ;

if (OpenProcessToken(GetCurrentProcess(), DAmask, &Thandle)) {
        printf("error: could not open token\n");
        return(1);
}

if (GetTokenInformation(Thandle, TokenLogonSid, &SidInfo, sizeof(SidInfo), &size) == 0) {
        printf("error: get token information failed\n");
}
else {
        printf("success: the SID was obtained from the token\n");
}
    
blog comments powered by Disqus
Valid HTML 4.01 Transitional Valid CSS!