[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 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 | |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 5 | #include "base/macros.h" |
[email protected] | dd4b5126 | 2013-07-25 21:38:23 | [diff] [blame] | 6 | #include "base/process/process_handle.h" |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 7 | |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 8 | #include <stddef.h> |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 9 | #include <sys/sysctl.h> |
[email protected] | dfa049e | 2013-02-07 02:57:22 | [diff] [blame] | 10 | #include <sys/types.h> |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 11 | #include <unistd.h> |
| 12 | |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 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, arraysize(mib), NULL, &length, NULL, 0) < 0) |
| 22 | return -1; |
| 23 | |
| 24 | mib[5] = (length / sizeof(struct kinfo_proc)); |
| 25 | |
| 26 | if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0) |
| 27 | return -1; |
| 28 | |
| 29 | return info.p_ppid; |
| 30 | } |
| 31 | |
| 32 | FilePath GetProcessExecutablePath(ProcessHandle process) { |
[email protected] | ea725b3 | 2011-10-25 17:43:05 | [diff] [blame] | 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, arraysize(mib), NULL, &len, NULL, 0) == -1) |
| 39 | return FilePath(); |
| 40 | mib[5] = (len / sizeof(struct kinfo_proc)); |
| 41 | if (sysctl(mib, arraysize(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(); |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 49 | } |
| 50 | |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 51 | } // namespace base |