sapling/eden/scm/lib/procinfo/darwin.c
Durham Goode d2e3736f67 procinfo: fix buck build on OSX
Summary:
As part of getting buck build to work on OSX, we need procinfo to
include it's OSX specific library.

Reviewed By: sfilipco

Differential Revision: D24513234

fbshipit-source-id: 69d8dd546e28b4403718351ff7984ee6b2ed3d1d
2020-11-03 17:40:19 -08:00

47 lines
1.3 KiB
C

// Exporting a small subset of libproc (macOS-specific) features.
//
// Ideally the "libproc" crate can be used. At the time of writing,
// libproc does not expose the proc_bsdshortinfo struct, therefore
// cannot provide the "parent process" information.
//
// See:
// https://github.com/osquery/osquery/blob/4.0.0/osquery/tables/system/darwin/processes.cpp
// The build system is disappointing and can't exclude this target from Linux
// builds. So let's ifdef out the entire file.
#ifdef __APPLE__
#include <assert.h>
#include <libproc.h> // @manual
#include <mach-o/dyld_images.h> // @manual
#include <mach/mach.h> // @manual
/// Return pid's parent process id.
/// Return 0 on error or if pid does not have a parent.
pid_t darwin_ppid(pid_t pid) {
struct proc_bsdshortinfo proc;
proc.pbsi_ppid = 0;
if (proc_pidinfo(
pid, PROC_PIDT_SHORTBSDINFO, 1, &proc, PROC_PIDT_SHORTBSDINFO_SIZE) ==
PROC_PIDT_SHORTBSDINFO_SIZE) {
return proc.pbsi_ppid;
}
return 0;
}
/// Return the executable path. Not thread-safe. Not reentrant.
const char* darwin_exepath(pid_t pid) {
static char path[PROC_PIDPATHINFO_MAXSIZE + 1];
int len = proc_pidpath(pid, path, PROC_PIDPATHINFO_MAXSIZE);
if (len <= 0) {
path[0] = 0;
} else {
assert(len < (int)sizeof(path));
path[len] = 0;
}
return path;
}
#endif