Mercurial > minori
diff dep/animia/src/fd/xnu.cc @ 163:44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Sat, 18 Nov 2023 00:47:40 -0500 |
parents | 61b76c7b656a |
children | 54c5d80a737e |
line wrap: on
line diff
--- a/dep/animia/src/fd/xnu.cc Fri Nov 17 16:49:57 2023 -0500 +++ b/dep/animia/src/fd/xnu.cc Sat Nov 18 00:47:40 2023 -0500 @@ -24,88 +24,6 @@ namespace animia::internal::xnu { -static bool GetProcessNameFromArgs(pid_t pid, std::string& result) { - /* sysctl shouldn't touch these, so we define them as const */ - const int mib[3] = {CTL_KERN, KERN_PROCARGS2, static_cast<int>(pid)}; - const size_t mib_size = sizeof(mib)/sizeof(*mib); - - /* Get the initial size of the array */ - size_t size; - { - int ret = sysctl((int*)mib, mib_size, nullptr, &size, nullptr, 0); - if (ret) - return false; - } - - /* Reserve the space for it in a std::string */ - std::string args; - args.resize(size); - - /* Get the contents of argc and argv */ - { - int ret = sysctl((int*)mib, mib_size, &args.front(), &size, NULL, 0); - if (ret) - return false; - } - - /* Is the size big enough to hold at least argc? */ - if (size < sizeof(int)) - return false; - - args.resize(size); - - /* Get argc using memcpy */ - int argc; - memcpy(&argc, &args.front(), sizeof(argc)); - - /* Do we even have argv[0]? */ - if (argc < 1) - return false; - - /* Find the first null character */ - size_t null_pos = args.find('\0', sizeof(argc)); - if (null_pos == std::string::npos) - return false; - - /* Find the last slash */ - size_t last_slash = args.rfind('/', null_pos); - if (last_slash == std::string::npos) - return false; - - /* Return our result */ - result = args.substr(last_slash + 1, null_pos - last_slash - 1); - return true; -} - -static bool GetProcessNameFromKernel(pid_t pid, std::string& result) { - result.reserve(2*MAXCOMLEN); - if (!proc_name(pid, &result.front(), result.length())) - return false; - - result.shrink_to_fit(); - return true; -} - -static bool GetProcessName(pid_t pid, std::string& result) { - /* Use LaunchServices */ -#ifdef HAVE_COREFOUNDATION - if (osx::util::LaunchServicesGetProcessName(pid, result)) - return true; -#endif - - /* Try parsing the arguments, this prevents the process name being - cut off to 2*MAXCOMLEN (32 chars) */ - if (GetProcessNameFromArgs(pid, result)) - return true; - - /* Then attempt getting it from the kernel, which results in the process name - being cut to 32 chars (16 chars if p_name is unavailable) */ - if (GetProcessNameFromKernel(pid, result)) - return true; - - return false; -} - bool XnuFdTools::EnumerateOpenProcesses(process_proc_t process_proc) { size_t pids_size = 512; std::unique_ptr<pid_t[]> pids; @@ -120,7 +38,7 @@ for (int i = 0; i < pids_size; i++) { std::string result; - GetProcessName(pids[i], result); + osx::util::GetProcessName(pids[i], result); if (!process_proc({pids[i], result})) return false; }