forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetuserfrompid.cpp
More file actions
54 lines (39 loc) · 1.04 KB
/
getuserfrompid.cpp
File metadata and controls
54 lines (39 loc) · 1.04 KB
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
47
48
49
50
51
52
53
54
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "pal.h"
#include "pal_config.h"
#include "getfileowner.h"
#include "getpwuid.h"
#include "getuserfrompid.h"
#include <string>
#include <sstream>
#include <errno.h>
#if HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
char* GetUserFromPid(pid_t pid)
{
#if defined(__linux__)
// Get effective owner of pid from procfs
std::stringstream ss;
ss << "/proc/" << pid;
std::string path;
ss >> path;
return GetFileOwner(path.c_str());
#elif defined(__APPLE__) && defined(__MACH__)
// Get effective owner of pid from sysctl
struct kinfo_proc oldp;
size_t oldlenp = sizeof(oldp);
int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
u_int namelen = sizeof(name)/sizeof(int);
// Read-only query
int ret = sysctl(name, namelen, &oldp, &oldlenp, NULL, 0);
if (ret != 0 || oldlenp == 0)
{
return NULL;
}
return GetPwUid(oldp.kp_eproc.e_ucred.cr_uid);
#else
return NULL;
#endif
}