process_handle_freebsd.cc 1013 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 <limits.h>
  6. #include <stddef.h>
  7. #include <sys/sysctl.h>
  8. #include <sys/types.h>
  9. #include <sys/user.h>
  10. #include <unistd.h>
  11. namespace base {
  12. ProcessId GetParentProcessId(ProcessHandle process) {
  13. struct kinfo_proc info;
  14. size_t length;
  15. int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process };
  16. if (sysctl(mib, std::size(mib), &info, &length, NULL, 0) < 0)
  17. return -1;
  18. return info.ki_ppid;
  19. }
  20. FilePath GetProcessExecutablePath(ProcessHandle process) {
  21. char pathname[PATH_MAX];
  22. size_t length;
  23. int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, process };
  24. length = sizeof(pathname);
  25. if (sysctl(mib, std::size(mib), pathname, &length, NULL, 0) < 0 ||
  26. length == 0) {
  27. return FilePath();
  28. }
  29. return FilePath(std::string(pathname));
  30. }
  31. } // namespace base