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