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