Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | dd4b5126 | 2013-07-25 21:38:23 | [diff] [blame] | 5 | #include "base/process/process_handle.h" |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 6 | |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 7 | #include <stddef.h> |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 8 | #include <sys/sysctl.h> |
[email protected] | dfa049e | 2013-02-07 02:57:22 | [diff] [blame] | 9 | #include <sys/types.h> |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 10 | #include <unistd.h> |
| 11 | |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 12 | namespace base { |
| 13 | |
| 14 | ProcessId GetParentProcessId(ProcessHandle process) { |
| 15 | struct kinfo_proc info; |
| 16 | size_t length; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 17 | int mib[] = { |
| 18 | CTL_KERN, KERN_PROC, KERN_PROC_PID, process, sizeof(struct kinfo_proc), |
| 19 | 0}; |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 20 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 21 | if (sysctl(mib, std::size(mib), NULL, &length, NULL, 0) < 0) { |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 22 | return -1; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 23 | } |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 24 | |
| 25 | mib[5] = (length / sizeof(struct kinfo_proc)); |
| 26 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 27 | if (sysctl(mib, std::size(mib), &info, &length, NULL, 0) < 0) { |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 28 | return -1; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 29 | } |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 30 | |
| 31 | return info.p_ppid; |
| 32 | } |
| 33 | |
| 34 | FilePath GetProcessExecutablePath(ProcessHandle process) { |
[email protected] | ea725b3 | 2011-10-25 17:43:05 | [diff] [blame] | 35 | struct kinfo_proc kp; |
| 36 | size_t len; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [
|