forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetppid.cpp
More file actions
46 lines (36 loc) · 940 Bytes
/
getppid.cpp
File metadata and controls
46 lines (36 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "pal_config.h"
#include "getppid.h"
#include <sys/user.h>
#include <sys/param.h>
#if HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
//! @brief GetPPid returns the parent process id for a process
//!
//! GetPPid
//!
//! @param[in] pid
//! @parblock
//! The process id to query for it's parent.
//! @endparblock
//!
//! @retval the parent process id, or UINT_MAX if unsuccessful
//!
pid_t GetPPid(pid_t pid)
{
#if defined(__APPLE__) && defined(__MACH__)
const pid_t PIDUnknown = UINT_MAX;
struct kinfo_proc info;
size_t length = sizeof(struct kinfo_proc);
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
if (sysctl(mib, 4, &info, &length, NULL, 0) < 0)
return PIDUnknown;
if (length == 0)
return PIDUnknown;
return info.kp_eproc.e_ppid;
#else
return UINT_MAX;
#endif
}