| 6 | #include "base/stl_util.h" |
| 7 |
| 8 | #include <stddef.h> |
| 9 | #include <sys/sysctl.h> |
| 10 | #include <sys/types.h> |
| 11 | #include <unistd.h> |
| 12 |
| 13 | namespace base { |
| 14 |
| 15 | ProcessId GetParentProcessId(ProcessHandle process) { |
| 16 | struct kinfo_proc info; |
| 17 | size_t length; |
| 18 | int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process, |
| 19 | sizeof(struct kinfo_proc), 0 }; |
| 20 |
| 21 | if (sysctl(mib, base::size(mib), NULL, &length, NULL, 0) < 0) |
| 22 | return -1; |
| 23 |
| 24 | mib[5] = (length / sizeof(struct kinfo_proc)); |
| 25 |
| 26 | if (sysctl(mib, base::size(mib), &info, &length, NULL, 0) < 0) |
| 27 | return -1; |
| 28 |
| 29 | return info.p_ppid; |
| 30 | } |
| 31 |
| 32 | FilePath GetProcessExecutablePath(ProcessHandle process) { |
| 33 | struct kinfo_proc kp; |
| 34 | size_t len; |
| 35 | int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process, |
| 36 | sizeof(struct kinfo_proc), 0 }; |
| 37 |
| 38 | if (sysctl(mib, base::size(mib), NULL, &len, NULL, 0) == -1) |
| 39 | return FilePath(); |
| 40 | mib[5] = (len / sizeof(struct kinfo_proc)); |
| 41 | if (sysctl(mib, base::size(mib), &kp, &len, NULL, 0) < 0) |
| 42 | return FilePath(); |
| 43 | if ((kp.p_flag & P_SYSTEM) != 0) |
| 44 | return FilePath(); |
| 45 | if (strcmp(kp.p_comm, "chrome") == 0) |
| 46 | return FilePath(kp.p_comm); |
| 47 |
| 48 | return FilePath(); |
| 49 | } |
| 50 |
| 51 | } // namespace base |
| 52 |