1 // Copyright 2013 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
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "build/build_config.h"
14
15 #if BUILDFLAG(IS_IOS)
16 #include "base/ios/sim_header_shims.h"
17 #else
18 #include <libproc.h>
19 #endif
20
21 namespace base {
22
GetParentProcessId(ProcessHandle process)23 ProcessId GetParentProcessId(ProcessHandle process) {
24 struct kinfo_proc info;
25 size_t length = sizeof(struct kinfo_proc);
26 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process };
27 if (sysctl(mib, 4, &info, &length, NULL, 0) < 0) {
28 DPLOG(ERROR) << "sysctl";
29 return -1;
30 }
31 if (length == 0)
32 return -1;
33 return info.kp_eproc.e_ppid;
34 }
35
GetProcessExecutablePath(ProcessHandle process)36 FilePath GetProcessExecutablePath(ProcessHandle process) {
37 char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
38 if (!proc_pidpath(process, pathbuf, sizeof(pathbuf)))
39 return FilePath();
40
41 return FilePath(pathbuf);
42 }
43
44 } // namespace base
45