process_handle_openbsd.cc 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/process/process_handle.h"
  5. #include <stddef.h>
  6. #include <sys/sysctl.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. namespace base {
  10. ProcessId GetParentProcessId(ProcessHandle process) {
  11. struct kinfo_proc info;
  12. size_t length;
  13. int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process,
  14. sizeof(struct kinfo_proc), 0 };
  15. if (sysctl(mib, std::size(mib), NULL, &length, NULL, 0) < 0)
  16. return -1;
  17. mib[5] = (length / sizeof(struct kinfo_proc));
  18. if (sysctl(mib, std::size(mib), &info, &length, NULL, 0) < 0)
  19. return -1;
  20. return info.p_ppid;
  21. }
  22. FilePath GetProcessExecutablePath(ProcessHandle process) {
  23. struct kinfo_proc kp;
  24. size_t len;
  25. int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process,
  26. sizeof(struct kinfo_proc), 0 };
  27. if (sysctl(mib, std::size(mib), NULL, &len, NULL, 0) == -1)
  28. return FilePath();
  29. mib[5] = (len / sizeof(struct kinfo_proc));
  30. if (sysctl(mib, std::size(mib), &kp, &len, NULL, 0) < 0)
  31. return FilePath();
  32. if ((kp.p_flag & P_SYSTEM) != 0)
  33. return FilePath();
  34. if (strcmp(kp.p_comm, "chrome") == 0)
  35. return FilePath(kp.p_comm);
  36. return FilePath();
  37. }
  38. } // namespace base