Skip to content

[CF] Unify implementations of _CFProcessPath. #2679

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

Merged
merged 1 commit into from
Aug 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 44 additions & 40 deletions CoreFoundation/Base.subproj/CFPlatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,44 +113,36 @@ const char **_CFGetProcessPath(void) {
return &__CFProcessPath;
}

#if TARGET_OS_WIN32
static inline void _CFSetProgramNameFromPath(const char *path) {
__CFProcessPath = strdup(path);
__CFprogname = strrchr(__CFProcessPath, PATH_SEP);
__CFprogname = (__CFprogname ? __CFprogname + 1 : __CFProcessPath);
}

const char *_CFProcessPath(void) {
if (__CFProcessPath) return __CFProcessPath;

#if TARGET_OS_WIN32
wchar_t buf[CFMaxPathSize] = {0};
DWORD rlen = GetModuleFileNameW(NULL, buf, sizeof(buf) / sizeof(buf[0]));
if (0 < rlen) {
char asciiBuf[CFMaxPathSize] = {0};
int res = WideCharToMultiByte(CP_UTF8, 0, buf, rlen, asciiBuf, sizeof(asciiBuf) / sizeof(asciiBuf[0]), NULL, NULL);
if (0 < res) {
__CFProcessPath = strdup(asciiBuf);
__CFprogname = strrchr(__CFProcessPath, PATH_SEP);
__CFprogname = (__CFprogname ? __CFprogname + 1 : __CFProcessPath);
_CFSetProgramNameFromPath(asciiBuf);
}
}
if (!__CFProcessPath) {
__CFProcessPath = "";
__CFprogname = __CFProcessPath;
}
return __CFProcessPath;
}
#endif

#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_BSD
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void) {
return pthread_main_np() == 1;
}
#endif

#if TARGET_OS_MAC
const char *_CFProcessPath(void) {
if (__CFProcessPath) return __CFProcessPath;
#elif TARGET_OS_MAC
#if TARGET_OS_OSX
if (!__CFProcessIsRestricted()) {
const char *path = (char *)__CFgetenv("CFProcessPath");
if (path) {
__CFProcessPath = strdup(path);
__CFprogname = strrchr(__CFProcessPath, PATH_SEP);
__CFprogname = (__CFprogname ? __CFprogname + 1 : __CFProcessPath);
_CFSetProgramNameFromPath(path);
return __CFProcessPath;
}
}
Expand All @@ -160,9 +152,7 @@ const char *_CFProcessPath(void) {
uint32_t size = CFMaxPathSize;
char buffer[size];
if (0 == _NSGetExecutablePath(buffer, &size)) {
__CFProcessPath = strdup(buffer);
__CFprogname = strrchr(__CFProcessPath, PATH_SEP);
__CFprogname = (__CFprogname ? __CFprogname + 1 : __CFProcessPath);
_CFSetProgramNameFromPath(buffer);
}
}

Expand All @@ -171,6 +161,38 @@ const char *_CFProcessPath(void) {
__CFprogname = __CFProcessPath;
}
return __CFProcessPath;
#elif TARGET_OS_LINUX
char buf[CFMaxPathSize + 1];

ssize_t res = readlink("/proc/self/exe", buf, CFMaxPathSize);
if (res > 0) {
// null terminate, readlink does not
buf[res] = 0;
_CFSetProgramNameFromPath(buf);
} else {
__CFProcessPath = "";
__CFprogname = __CFProcessPath;
}
return __CFProcessPath;
#else // TARGET_OS_BSD
if (!__CFProcessIsRestricted()) {
char *path = getenv("_");
if (path != NULL) {
_CFSetProgramNameFromPath(path);
return __CFProcessPath;
}
}

// We don't yet have anything left to try.
__CFProcessPath = "";
__CFprogname = __CFProcessPath;
return __CFProcessPath;
#endif
}

#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_BSD
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void) {
return pthread_main_np() == 1;
}
#endif

Expand All @@ -185,24 +207,6 @@ const char *_CFProcessPath(void) {
Boolean _CFIsMainThread(void) {
return syscall(SYS_gettid) == getpid();
}

const char *_CFProcessPath(void) {
if (__CFProcessPath) return __CFProcessPath;
char buf[CFMaxPathSize + 1];

ssize_t res = readlink("/proc/self/exe", buf, CFMaxPathSize);
if (res > 0) {
// null terminate, readlink does not
buf[res] = 0;
__CFProcessPath = strdup(buf);
__CFprogname = strrchr(__CFProcessPath, PATH_SEP);
__CFprogname = (__CFprogname ? __CFprogname + 1 : __CFProcessPath);
} else {
__CFProcessPath = "";
__CFprogname = __CFProcessPath;
}
return __CFProcessPath;
}
#endif

CF_PRIVATE CFStringRef _CFProcessNameString(void) {
Expand Down