Unix to Windows Porting Dictionary for HPC

Links

Function List

getgid


Unix

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

uid_t getgid(void);
uid_t getegid(void);

Windows

header file: Winbase.h

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 group identity (GID) 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 GID.

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 GID of the user that started the process (the real GID) and the GID of the group that the process is running as (the effective GID). Windows does not maintain this distinction. The value of a SID will be the equivalent of the Unix effective GID. Since the equivalent of the Unix real GID cannot be obtained, using the SID is recommended for Windows when the real or effective GID is needed.

On Unix systems a process runs with a single effective group identity. On Windows systems all of the group identities are available with a process because this information is treated like an ACL.

Example of Use in Windows

#include <Winbase.h>

HANDLE Thandle;
DWORD DAmask, gsize, size;
PTOKEN_GROUPS GInfo = NULL;


DAmask = TOKEN_READ;

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

/* We will get this twice to get enough space of the groups */
gsize = 0;
if (GetTokenInformation(Thandle, TokenGroups, &SidInfo, sizeof(SidInfo), &size) == 0) {
        printf("error: get token information failed\n");
}
else {
        gsize = size;
        GInfo = malloc(gsize * sizeof(TOKEN_GROUPS);
        if (GetTokenInformation(Thandle, TokenGroups, &SidInfo, sizeof(SidInfo), &size) =
= 0) {
                printf("success: the group SID's were obtained from the token\n");
        }
}
    
blog comments powered by Disqus