-
Notifications
You must be signed in to change notification settings - Fork 4
os::get_process_uid and os::rss for BSD #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: bsd-port
Are you sure you want to change the base?
Changes from 4 commits
56e6475
9e7292f
d217060
a87298d
376cedf
bd8bdcd
b3e450f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,7 +76,6 @@ | |
| # include <fcntl.h> | ||
| # include <fenv.h> | ||
| # include <inttypes.h> | ||
| # include <mach/mach.h> | ||
| # include <poll.h> | ||
| # include <pthread.h> | ||
| # include <pwd.h> | ||
|
|
@@ -112,6 +111,7 @@ | |
|
|
||
| #ifdef __APPLE__ | ||
| #include <libproc.h> | ||
| #include <mach/mach.h> | ||
| #include <mach/task_info.h> | ||
| #include <mach-o/dyld.h> | ||
|
|
||
|
|
@@ -124,6 +124,32 @@ | |
| #include <pthread_np.h> | ||
| #endif | ||
|
|
||
| #if defined(__APPLE__) | ||
| #define KERN_PROC_MIB KERN_PROC | ||
| #define KINFO_PROC_T kinfo_proc | ||
| #define KI_UID kp_eproc.e_ucred.cr_uid | ||
| #define KI_PID kp_proc.p_pid | ||
| #elif defined(__OpenBSD__) | ||
| #define KERN_PROC_MIB KERN_PROC | ||
| #define KINFO_PROC_T kinfo_proc | ||
| #define KI_RSS p_vm_rssize | ||
| #define KI_UID p_uid | ||
| #define KI_PID p_pid | ||
| #elif defined(__FreeBSD__) | ||
| #include <sys/user.h> | ||
| #define KERN_PROC_MIB KERN_PROC | ||
| #define KINFO_PROC_T kinfo_proc | ||
| #define KI_RSS ki_rssize | ||
| #define KI_UID ki_uid | ||
| #define KI_PID ki_pid | ||
| #elif defined(__NetBSD__) | ||
| #define KERN_PROC_MIB KERN_PROC2 | ||
| #define KINFO_PROC_T kinfo_proc2 | ||
| #define KI_RSS p_vm_rssize | ||
| #define KI_UID p_uid | ||
| #define KI_PID p_pid | ||
| #endif | ||
|
|
||
| #ifndef MAP_ANONYMOUS | ||
| #define MAP_ANONYMOUS MAP_ANON | ||
| #endif | ||
|
|
@@ -261,6 +287,20 @@ size_t os::rss() { | |
| if (ret == KERN_SUCCESS) { | ||
| rss = info.resident_size; | ||
| } | ||
| #else | ||
| pid_t pid = getpid(); | ||
| struct KINFO_PROC_T kp; | ||
| size_t bufSize = sizeof kp; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we use brackets with sizeof
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in latest commit. |
||
| #ifndef __FreeBSD__ | ||
| u_int namelen = 6; | ||
| int mib[6] = {CTL_KERN, KERN_PROC_MIB, KERN_PROC_PID, pid, bufSize, 1}; | ||
| #else | ||
| u_int namelen = 4; | ||
| int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid}; | ||
| #endif | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make namelen const, move it out of the ifdefs, and calculated it based on the array like this also mib can probably be constexpr as well.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated, but a few comments: I could not use the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. haha right, sorry, my bad |
||
| if (sysctl(mib, namelen, &kp, &bufSize, nullptr, 0) != -1) { | ||
| return kp.KI_RSS * getpagesize(); | ||
| } | ||
| #endif // __APPLE__ | ||
|
|
||
| return rss; | ||
|
|
@@ -899,12 +939,19 @@ pid_t os::Bsd::gettid() { | |
|
|
||
| // Returns the uid of a process or -1 on error. | ||
| uid_t os::Bsd::get_process_uid(pid_t pid) { | ||
| struct kinfo_proc kp; | ||
| struct KINFO_PROC_T kp; | ||
| size_t size = sizeof kp; | ||
| int mib_kern[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid}; | ||
| if (sysctl(mib_kern, 4, &kp, &size, nullptr, 0) == 0) { | ||
| if (size > 0 && kp.kp_proc.p_pid == pid) { | ||
| return kp.kp_eproc.e_ucred.cr_uid; | ||
| #if defined(__FreeBSD__) || defined(__APPLE__) | ||
| u_int namelen = 4; | ||
| int mib_kern[4] = {CTL_KERN, KERN_PROC_MIB, KERN_PROC_PID, pid}; | ||
| #else | ||
| u_int namelen = 6; | ||
| int mib_kern[6] = {CTL_KERN, KERN_PROC_MIB, KERN_PROC_PID, pid, | ||
| static_cast<int>(size), 1}; | ||
| #endif | ||
| if (sysctl(mib_kern, namelen, &kp, &size, nullptr, 0) == 0) { | ||
| if (size > 0 && kp.KI_PID == pid) { | ||
| return kp.KI_UID; | ||
| } | ||
| } | ||
| return (uid_t)-1; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an eyesore. Can this be shortened, and/or moved into a helper include?