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 <limits.h>
8 #include <stddef.h>
9 #include <sys/sysctl.h>
10 #include <sys/types.h>
11 #include <sys/user.h>
12 #include <unistd.h>
13
14 #include "base/files/file_path.h"
15 #include "base/posix/sysctl.h"
16 #include "third_party/abseil-cpp/absl/types/optional.h"
17
18 namespace base {
19
GetParentProcessId(ProcessHandle process)20 ProcessId GetParentProcessId(ProcessHandle process) {
21 struct kinfo_proc info;
22 size_t length;
23 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process };
24
25 if (sysctl(mib, std::size(mib), &info, &length, NULL, 0) < 0)
26 return -1;
27
28 return info.ki_ppid;
29 }
30
GetProcessExecutablePath(ProcessHandle process)31 FilePath GetProcessExecutablePath(ProcessHandle process) {
32 absl::optional<std::string> pathname =
33 base::StringSysctl({CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, process});
34
35 return FilePath(pathname.value_or(std::string{}));
36 }
37
38 } // namespace base
39