Example #1
0
File: ProcFS.cpp Project: cosql/ds2
bool ProcFS::ReadProcessInfo(pid_t pid, ProcessInfo &info) {
  pid_t ppid;
  uid_t uid, euid;
  gid_t gid, egid;
  ELFInfo elf;
  std::string path;

  info.clear();

  if (!ReadProcessIds(pid, ppid, uid, euid, gid, egid) ||
      !GetProcessELFInfo(pid, elf) ||
      (path = GetProcessExecutablePath(pid)).empty())
    return false;

  info.pid = pid;
  info.parentPid = ppid;

  info.name.swap(path);

  info.realUid = uid;
  info.effectiveUid = euid;
  info.realGid = gid;
  info.effectiveGid = egid;

  if (!ELFSupport::MachineTypeToCPUType(elf.machine, elf.is64Bit, info.cpuType,
                                        info.cpuSubType)) {
    info.cpuType = kCPUTypeAny;
    info.cpuSubType = kCPUSubTypeInvalid;
  }

  info.nativeCPUType = elf.machine;
  info.nativeCPUSubType = kInvalidCPUType;

  info.endian = elf.endian;
  info.pointerSize = elf.is64Bit ? 8 : 4;

  info.osType = Platform::GetOSTypeName();
  info.osVendor = Platform::GetOSVendorName();

  return true;
}
Example #2
0
bool Platform::GetProcessInfo(ProcessId pid, ProcessInfo &info) {
  HANDLE processHandle;
  BOOL rc;

  info.clear();

  info.pid = pid;

  processHandle =
      OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
  if (processHandle == nullptr) {
    goto error;
  }

  // Get process name.
  {
    HMODULE firstModuleHandle;
    DWORD bytesNeeded;
    WCHAR processName[MAX_PATH];

    rc = EnumProcessModules(processHandle, &firstModuleHandle,
                            sizeof(firstModuleHandle), &bytesNeeded);
    if (!rc)
      goto error;

    rc = GetModuleBaseNameW(processHandle, firstModuleHandle, processName,
                            sizeof(processName));
    if (!rc)
      goto error;

    info.name = WideToNarrowString(processName);
  }

  // Get process user ID.
  {
    HANDLE processToken;
    std::vector<char> userInfoBuffer;
    PTOKEN_USER userInfo;
    DWORD bytesNeeded;

    rc = OpenProcessToken(processHandle, TOKEN_QUERY, &processToken);
    if (!rc)
      goto error;

    GetTokenInformation(processToken, TokenUser, userInfoBuffer.data(),
                        userInfoBuffer.size(), &bytesNeeded);

    userInfoBuffer.resize(bytesNeeded);

    rc = GetTokenInformation(processToken, TokenUser, userInfoBuffer.data(),
                             userInfoBuffer.size(), &bytesNeeded);
    if (!rc) {
      CloseHandle(processToken);
      goto error;
    }

    userInfo = reinterpret_cast<PTOKEN_USER>(userInfoBuffer.data());

    DWORD size = GetLengthSid(userInfo->User.Sid);
    info.realUid = malloc(size);
    CopySid(size, info.realUid, userInfo->User.Sid);

    CloseHandle(processToken);
  }

  // TODO(sas): Fetch the process group ID. This looks like it's gonna
  // require some additional work as a process on Windows doesn't have
  // a single group but a list of group tokens instead.

  CloseHandle(processHandle);

  return true;

error:
  if (processHandle != nullptr)
    CloseHandle(processHandle);
  return false;
}