comparison dep/animia/src/fd/xnu.cc @ 162:61b76c7b656a

dep/animia: add os x launchservices method
author Paper <mrpapersonic@gmail.com>
date Fri, 17 Nov 2023 16:49:57 -0500
parents 900b5b530883
children 44c5e6dd9488
comparison
equal deleted inserted replaced
161:71752dcbb49f 162:61b76c7b656a
1 /* 1 /*
2 ** fd/xnu.cpp 2 ** fd/xnu.cc
3 ** - provides support for XNU (part of Darwin) 3 ** - provides support for XNU (part of Darwin)
4 */ 4 */
5 #include "animia/fd/xnu.h" 5 #include "animia/fd/xnu.h"
6 #ifdef HAVE_COREFOUNDATION
7 # include "animia/util/osx.h"
8 #endif
6 #include "animia.h" 9 #include "animia.h"
7 10
8 #include <cassert> 11 #include <cassert>
9 #include <string> 12 #include <string>
10 #include <unordered_map> 13 #include <unordered_map>
26 const int mib[3] = {CTL_KERN, KERN_PROCARGS2, static_cast<int>(pid)}; 29 const int mib[3] = {CTL_KERN, KERN_PROCARGS2, static_cast<int>(pid)};
27 const size_t mib_size = sizeof(mib)/sizeof(*mib); 30 const size_t mib_size = sizeof(mib)/sizeof(*mib);
28 31
29 /* Get the initial size of the array */ 32 /* Get the initial size of the array */
30 size_t size; 33 size_t size;
31 int ret = sysctl((int*)mib, mib_size, nullptr, &size, nullptr, 0); 34 {
32 if (ret) 35 int ret = sysctl((int*)mib, mib_size, nullptr, &size, nullptr, 0);
33 return false; 36 if (ret)
37 return false;
38 }
34 39
35 /* Reserve the space for it in a std::string */ 40 /* Reserve the space for it in a std::string */
36 std::string args; 41 std::string args;
37 args.resize(size); 42 args.resize(size);
38 43
39 /* Get the contents of argc and argv */ 44 /* Get the contents of argc and argv */
40 ret = sysctl((int*)mib, mib_size, &args.front(), &size, NULL, 0); 45 {
41 if (ret) 46 int ret = sysctl((int*)mib, mib_size, &args.front(), &size, NULL, 0);
42 return false; 47 if (ret)
48 return false;
49 }
43 50
44 /* Is the size big enough to hold at least argc? */ 51 /* Is the size big enough to hold at least argc? */
45 if (size < sizeof(int)) 52 if (size < sizeof(int))
46 return false; 53 return false;
47 54
49 56
50 /* Get argc using memcpy */ 57 /* Get argc using memcpy */
51 int argc; 58 int argc;
52 memcpy(&argc, &args.front(), sizeof(argc)); 59 memcpy(&argc, &args.front(), sizeof(argc));
53 60
54 /* Check for a condition that, realistically, would never happen, 61 /* Do we even have argv[0]? */
55 but better to be safe than sorry */
56 if (argc < 1) 62 if (argc < 1)
57 return false; 63 return false;
58 64
59 /* Find the first null character */ 65 /* Find the first null character */
60 size_t null_pos = args.find('\0', sizeof(argc)); 66 size_t null_pos = args.find('\0', sizeof(argc));
79 result.shrink_to_fit(); 85 result.shrink_to_fit();
80 return true; 86 return true;
81 } 87 }
82 88
83 static bool GetProcessName(pid_t pid, std::string& result) { 89 static bool GetProcessName(pid_t pid, std::string& result) {
84 /* First try parsing the arguments, this prevents the process name being 90 /* Use LaunchServices */
91 #ifdef HAVE_COREFOUNDATION
92 if (osx::util::LaunchServicesGetProcessName(pid, result))
93 return true;
94 #endif
95
96 /* Try parsing the arguments, this prevents the process name being
85 cut off to 2*MAXCOMLEN (32 chars) */ 97 cut off to 2*MAXCOMLEN (32 chars) */
86 if (GetProcessNameFromArgs(pid, result)) 98 if (GetProcessNameFromArgs(pid, result))
87 return true; 99 return true;
88 100
89 /* Then attempt getting it from the kernel, which results in the process name 101 /* Then attempt getting it from the kernel, which results in the process name