Reputation: 169
I have been using proc_info.h
and similar files to get information about my processes like paths, ppids, names, etc. Now I need to get the user's who own those processes using C/C++. How can I do this? in proc_info.h
I see the struct called proc_regioninfo
which has a member pri_user_tag
which might be what I am looking for but I'm cant figure out how to populate a struct of ````proc_regioninfo```. I've looked everywhere online and can't find an answer.
I've been digging around in this part of my file system trying to find stuff: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift-migrator/sdks/MacOSX.sdk/usr/include
so maybe that will help you find things.
this question How to programatically get uid from pid in osx using c++? has an answer but I can't figure out how to install libproc-dev
like the person suggests, nor can my system figure out what to do with #include <proc/readproc.h>
.
If you have any idea I would greatly appreciate it!
Upvotes: 0
Views: 1634
Reputation: 169
So as it turns out you don't really get usernames through pid's at all. Here is my solution and it should work for anyone else with in this situation.
If you have PIDs from a sysctl call then you already have kinfostructs in which case you can just use these lines of code to access the information in that struct:
#include <pwd.h>
#include <sysctl.h>
struct passwd *pwd = getpwuid(kinfostructs[i].kp_eproc.e_ucred.cr_uid);
printf("username: %s\n",pwd->pw_name);
I can't format the actual source code in a way that they (stackoverflow) will let me post it. If you want the actual source code I used just ask me for it.
Upvotes: 1
Reputation: 35154
If you are using XCode (to what your digging in the file systems hints at) and if it is an option to write a mixed Objective-C / C++ - app, you could use Objective-C's NSProcessInfo
-object, which provides features like userName
of fullUserName
. A way how to use NSProcessInfo
is described in this SO answer. Hope it helps.
Upvotes: 0