1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/process/process_handle.h"
6
7 #include <stddef.h>
8 #include <sys/sysctl.h>
9 #include <sys/types.h>
10 #include <unistd.h>
11
12 namespace base {
13
GetParentProcessId(ProcessHandle process)14 ProcessId GetParentProcessId(ProcessHandle process) {
15 struct kinfo_proc info;
16 size_t length;
17 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process,
18 sizeof(struct kinfo_proc), 0 };
19
20 if (sysctl(mib, std::size(mib), NULL, &length, NULL, 0) < 0)
21 return -1;
22
23 mib[5] = (length / sizeof(struct kinfo_proc));
24
25 if (sysctl(mib, std::size(mib), &info, &length, NULL, 0) < 0)
26 return -1;
27
28 return info.p_ppid;
29 }
30
GetProcessExecutablePath(ProcessHandle process)31 FilePath GetProcessExecutablePath(ProcessHandle process) {
32 struct kinfo_proc kp;
33 size_t len;
34 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process,
35 sizeof(struct kinfo_proc), 0 };
36
37 if (sysctl(mib, std::size(mib), NULL, &len, NULL, 0) == -1)
38 return FilePath();
39 mib[5] = (len / sizeof(struct kinfo_proc));
40 if (sysctl(mib, std::size(mib), &kp, &len, NULL, 0) < 0)
41 return FilePath();
42 if ((kp.p_flag & P_SYSTEM) != 0)
43 return FilePath();
44 if (strcmp(kp.p_comm, "chrome") == 0)
45 return FilePath(kp.p_comm);
46
47 return FilePath();
48 }
49
50 } // namespace base
51