Skip to content

Commit 86ec9a9

Browse files
committed
Fixed getting process name for macOS
1 parent e6ff705 commit 86ec9a9

File tree

3 files changed

+93
-38
lines changed

3 files changed

+93
-38
lines changed

conf/cmake/common.cmake

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,34 @@ if ("${AREG_PROCESSOR}" STREQUAL "")
1414
set(AREG_PROCESSOR ${CMAKE_SYSTEM_PROCESSOR})
1515
endif()
1616

17+
# Detect target platform
18+
set(AREG_PLATFORM_LINUX FALSE)
19+
set(AREG_PLATFORM_MACOS FALSE)
20+
set(AREG_PLATFORM_WINDOWS FALSE)
21+
set(AREG_PLATFORM_POSIX FALSE)
22+
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
23+
set(AREG_PLATFORM_MACOS TRUE)
24+
set(AREG_PLATFORM_POSIX TRUE)
25+
message(STATUS "Areg: >>> Target platform = macOS")
26+
27+
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
28+
set(AREG_PLATFORM_LINUX TRUE)
29+
set(AREG_PLATFORM_POSIX TRUE)
30+
message(STATUS "Areg: >>> Target platform = Linux")
31+
32+
elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
33+
set(AREG_PLATFORM_WINDOWS TRUE)
34+
message(STATUS "Areg: >>> Target platform = Windows")
35+
36+
else()
37+
message(WARNING "Areg: >>> Unknown target platform '${CMAKE_SYSTEM_NAME}'")
38+
endif()
39+
40+
# Cross-compilation detection
41+
if (CMAKE_CROSSCOMPILING)
42+
message(STATUS "Areg: >>> Cross-compiling ${CMAKE_HOST_SYSTEM_NAME}(${CMAKE_HOST_SYSTEM_PROCESSOR}) -> ${CMAKE_SYSTEM_NAME}(${CMAKE_SYSTEM_PROCESSOR})")
43+
endif()
44+
1745
# Identify compiler short name
1846
if ("${AREG_COMPILER_FAMILY}" STREQUAL "")
1947

conf/cmake/user.cmake

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -91,34 +91,6 @@ set(AREG_CXX_COMPILER)
9191
set(AREG_C_COMPILER)
9292
set(AREG_COMPILER_SHORT)
9393

94-
# Detect target platform
95-
set(AREG_PLATFORM_LINUX FALSE)
96-
set(AREG_PLATFORM_MACOS FALSE)
97-
set(AREG_PLATFORM_WINDOWS FALSE)
98-
set(AREG_PLATFORM_POSIX FALSE)
99-
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
100-
set(AREG_PLATFORM_MACOS TRUE)
101-
set(AREG_PLATFORM_POSIX TRUE)
102-
message(STATUS "Areg: >>> Target platform = macOS")
103-
104-
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
105-
set(AREG_PLATFORM_LINUX TRUE)
106-
set(AREG_PLATFORM_POSIX TRUE)
107-
message(STATUS "Areg: >>> Target platform = Linux")
108-
109-
elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
110-
set(AREG_PLATFORM_WINDOWS TRUE)
111-
message(STATUS "Areg: >>> Target platform = Windows")
112-
113-
else()
114-
message(WARNING "Areg: >>> Unknown target platform '${CMAKE_SYSTEM_NAME}'")
115-
endif()
116-
117-
# Cross-compilation detection
118-
if (CMAKE_CROSSCOMPILING)
119-
message(STATUS "Areg: >>> Cross-compiling ${CMAKE_HOST_SYSTEM_NAME}(${CMAKE_HOST_SYSTEM_PROCESSOR}) -> ${CMAKE_SYSTEM_NAME}(${CMAKE_SYSTEM_PROCESSOR})")
120-
endif()
121-
12294
# Specify CPU platform here, the system CPU platform is detected in 'commmon.cmake'
12395
if (DEFINED AREG_PROCESSOR AND NOT "${AREG_PROCESSOR}" STREQUAL "")
12496
macro_get_processor(${AREG_PROCESSOR} AREG_PROCESSOR AREG_BITNESS _found_proc)

framework/areg/appbase/private/posix/ApplicationPosix.cpp

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,75 @@
1919
#include <signal.h>
2020
#include <unistd.h>
2121

22+
#ifdef __APPLE__
23+
#include <sys/sysctl.h>
24+
#include <libproc.h>
25+
#endif
26+
2227
namespace
2328
{
24-
int _getProcIdByName(const char * procName)
29+
30+
31+
#ifdef __APPLE__ //macOS
32+
33+
int _getProcIdByName(const char* procName)
2534
{
26-
constexpr char const fmt[] { "/proc/%s/cmdline" };
27-
constexpr char const dirProc[] { "/proc" };
28-
int pid = -1;
35+
if (NEString::isEmpty<char>(procName))
36+
return -1;
37+
38+
// macOS implementation using sysctl
39+
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
40+
size_t size {0};
41+
42+
if (sysctl(mib, 4, nullptr, &size, nullptr, 0) < 0)
43+
return -1;
44+
45+
uint8_t* buffer = size != 0 ? DEBUG_NEW uint8_t[size] : nullptr;
46+
struct kinfo_proc* procs = reinterpret_cast<struct kinfo_proc*>(buffer);
47+
if (procs == nullptr)
48+
return -1;
49+
50+
if (sysctl(mib, 4, procs, &size, nullptr, 0) < 0)
51+
{
52+
delete [] buffer;
53+
return -1;
54+
}
55+
56+
int count = size / sizeof(struct kinfo_proc);
57+
int pid {-1};
58+
59+
for (int i = 0; i < count && pid < 0; ++i)
60+
{
61+
if (NEString::compareIgnoreCase<char, char>(procName, procs[i].kp_proc.p_comm) == NEMath::eCompare::Equal)
62+
{
63+
pid = procs[i].kp_proc.p_pid;
64+
}
65+
}
66+
67+
delete[] buffer;
68+
return pid;
69+
}
70+
71+
#else // Linux
72+
73+
int _getProcIdByName(const char* procName)
74+
{
75+
if (NEString::isEmpty<char>(procName))
76+
return -1;
77+
78+
// Linux implementation using /proc
79+
constexpr char const fmt[]{ "/proc/%s/cmdline" };
80+
constexpr char const dirProc[]{ "/proc" };
81+
int pid {-1};
2982

3083
DIR* dir = opendir(dirProc);
3184
char* buffer = dir != nullptr ? DEBUG_NEW char[File::MAXIMUM_PATH + 1] : nullptr;
32-
if ((buffer == nullptr) || NEString::isEmpty<char>(procName))
85+
if (buffer == nullptr)
3386
return pid;
3487

3588
for (struct dirent* dirEntry = readdir(dir); (pid < 0) && (dirEntry != nullptr); dirEntry = readdir(dir))
3689
{
37-
// skip non-numeric directories.
38-
if ((dirEntry->d_type == DT_REG) && (NEString::isNumeric<char>(dirEntry->d_name[0])))
90+
if (NEString::isNumeric<char>(dirEntry->d_name[0]))
3991
{
4092
String name;
4193
name.format(fmt, dirEntry->d_name);
@@ -44,11 +96,11 @@ namespace
4496
{
4597
if (fgets(buffer, File::MAXIMUM_PATH + 1, file) != nullptr)
4698
{
47-
NEString::CharPos pos = NEString::findLast<char>( File::PATH_SEPARATOR, buffer);
99+
NEString::CharPos pos = NEString::findLast<char>(File::PATH_SEPARATOR, buffer);
48100
if (NEString::isPositionValid(pos))
49101
{
50-
char* name = buffer + pos + 1;
51-
if (NEString::compareIgnoreCase<char, char>(procName, name) == NEMath::eCompare::Equal)
102+
char* procPath = buffer + pos + 1;
103+
if (NEString::compareIgnoreCase<char, char>(procName, procPath) == NEMath::eCompare::Equal)
52104
{
53105
pid = NEString::makeInteger<char>(dirEntry->d_name, nullptr);
54106
}
@@ -60,8 +112,11 @@ namespace
60112
}
61113
}
62114

115+
delete[] buffer;
116+
closedir(dir);
63117
return pid;
64118
}
119+
#endif // Linux
65120

66121
DEF_LOG_SCOPE(areg_appbase_ApplicationPosix__handleSignalBrokenPipe);
67122
void _handleSignalBrokenPipe(int s)

0 commit comments

Comments
 (0)