process_handle_freebsd.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
1// 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
5#include "base/process/process_handle.h"
6
7#include <sys/sysctl.h>
8#include <sys/types.h>
9#include <unistd.h>
10
11namespace base {
12
13ProcessId GetParentProcessId(ProcessHandle process) {
14  struct kinfo_proc info;
15  size_t length;
16  int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process };
17
18  if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0)
19    return -1;
20
21  return info.ki_ppid;
22}
23
24FilePath GetProcessExecutablePath(ProcessHandle process) {
25  char pathname[PATH_MAX];
26  size_t length;
27  int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, process };
28
29  length = sizeof(pathname);
30
31  if (sysctl(mib, arraysize(mib), pathname, &length, NULL, 0) < 0 ||
32      length == 0) {
33    return FilePath();
34  }
35
36  return FilePath(std::string(pathname));
37}
38
39}  // namespace base
40