blob: 045e7205dc627a9d84115efe3ccfa96a1fc98800 [file] [log] [blame]
[email protected]817f0f142011-10-13 04:23:221// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avibeced7c2015-12-24 06:47:595#include "base/macros.h"
[email protected]dd4b51262013-07-25 21:38:236#include "base/process/process_handle.h"
[email protected]817f0f142011-10-13 04:23:227
avibeced7c2015-12-24 06:47:598#include <stddef.h>
[email protected]817f0f142011-10-13 04:23:229#include <sys/sysctl.h>
[email protected]dfa049e2013-02-07 02:57:2210#include <sys/types.h>
[email protected]817f0f142011-10-13 04:23:2211#include <unistd.h>
12
[email protected]817f0f142011-10-13 04:23:2213namespace base {
14
15ProcessId GetParentProcessId(ProcessHandle process) {
16 struct kinfo_proc info;
17 size_t length;
18 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process,
19 sizeof(struct kinfo_proc), 0 };
20
21 if (sysctl(mib, arraysize(mib), NULL, &length, NULL, 0) < 0)
22 return -1;
23
24 mib[5] = (length / sizeof(struct kinfo_proc));
25
26 if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0)
27 return -1;
28
29 return info.p_ppid;
30}
31
32FilePath GetProcessExecutablePath(ProcessHandle process) {
[email protected]ea725b32011-10-25 17:43:0533 struct kinfo_proc kp;
34 size_t len;
35 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process,
36 sizeof(struct kinfo_proc), 0 };
37
38 if (sysctl(mib, arraysize(mib), NULL, &len, NULL, 0) == -1)
39 return FilePath();
40 mib[5] = (len / sizeof(struct kinfo_proc));
41 if (sysctl(mib, arraysize(mib), &kp, &len, NULL, 0) < 0)
42 return FilePath();
43 if ((kp.p_flag & P_SYSTEM) != 0)
44 return FilePath();
45 if (strcmp(kp.p_comm, "chrome") == 0)
46 return FilePath(kp.p_comm);
47
48 return FilePath();
[email protected]817f0f142011-10-13 04:23:2249}
50
[email protected]817f0f142011-10-13 04:23:2251} // namespace base