blob: 88c9aec081232f522b60b54dbe24723224c011d5 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2011 The Chromium Authors
[email protected]817f0f142011-10-13 04:23:222// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]dd4b51262013-07-25 21:38:235#include "base/process/process_handle.h"
[email protected]817f0f142011-10-13 04:23:226
avibeced7c2015-12-24 06:47:597#include <stddef.h>
[email protected]817f0f142011-10-13 04:23:228#include <sys/sysctl.h>
[email protected]dfa049e2013-02-07 02:57:229#include <sys/types.h>
[email protected]817f0f142011-10-13 04:23:2210#include <unistd.h>
11
[email protected]817f0f142011-10-13 04:23:2212namespace base {
13
14ProcessId GetParentProcessId(ProcessHandle process) {
15 struct kinfo_proc info;
16 size_t length;
Peter Kasting134ef9af2024-12-28 02:30:0917 int mib[] = {
18 CTL_KERN, KERN_PROC, KERN_PROC_PID, process, sizeof(struct kinfo_proc),
19 0};
[email protected]817f0f142011-10-13 04:23:2220
Peter Kasting134ef9af2024-12-28 02:30:0921 if (sysctl(mib, std::size(mib), NULL, &length, NULL, 0) < 0) {
[email protected]817f0f142011-10-13 04:23:2222 return -1;
Peter Kasting134ef9af2024-12-28 02:30:0923 }
[email protected]817f0f142011-10-13 04:23:2224
25 mib[5] = (length / sizeof(struct kinfo_proc));
26
Peter Kasting134ef9af2024-12-28 02:30:0927 if (sysctl(mib, std::size(mib), &info, &length, NULL, 0) < 0) {
[email protected]817f0f142011-10-13 04:23:2228 return -1;
Peter Kasting134ef9af2024-12-28 02:30:0929 }
[email protected]817f0f142011-10-13 04:23:2230
31 return info.p_ppid;
32}
33
34FilePath GetProcessExecutablePath(ProcessHandle process) {
[email protected]ea725b32011-10-25 17:43:0535 struct kinfo_proc kp;
36 size_t len;
Peter Kasting134ef9af2024-12-28 02:30:09