Skip to content

[wasm] Port ProcessInfo for WASI platform #4891

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
Show file tree
Hide file tree
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
46 changes: 40 additions & 6 deletions CoreFoundation/Base.subproj/CFPlatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ CF_PRIVATE const wchar_t *_CFDLLPath(void) {
}
#endif // TARGET_OS_WIN32

#if !TARGET_OS_WASI
static const char *__CFProcessPath = NULL;
static const char *__CFprogname = NULL;

Expand Down Expand Up @@ -186,6 +185,31 @@ const char *_CFProcessPath(void) {
__CFprogname = __CFProcessPath;
}
return __CFProcessPath;
#elif TARGET_OS_WASI
__wasi_errno_t err;
size_t argc;
size_t argv_buf_size;
err = __wasi_args_sizes_get(&argc, &argv_buf_size);
if (err != 0) {
__CFProcessPath = "";
__CFprogname = __CFProcessPath;
return __CFProcessPath;
}
char *argv_buf = malloc(argv_buf_size);
char **argv = calloc(argc, sizeof(char *));
err = __wasi_args_get((uint8_t **)argv, (uint8_t *)argv_buf);
if (err != 0) {
__CFProcessPath = "";
__CFprogname = __CFProcessPath;
free(argv_buf);
free(argv);
return __CFProcessPath;
}
_CFSetProgramNameFromPath(argv[0]);
free(argv_buf);
free(argv);
return __CFProcessPath;

#else // TARGET_OS_BSD
char *argv0 = NULL;

Expand Down Expand Up @@ -248,7 +272,6 @@ const char *_CFProcessPath(void) {
return __CFProcessPath;
#endif
}
#endif // TARGET_OS_WASI

#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_BSD
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void) {
Expand All @@ -273,7 +296,6 @@ Boolean _CFIsMainThread(void) {
}
#endif // TARGET_OS_LINUX

#if !TARGET_OS_WASI
CF_PRIVATE CFStringRef _CFProcessNameString(void) {
static CFStringRef __CFProcessNameString = NULL;
if (!__CFProcessNameString) {
Expand All @@ -292,7 +314,6 @@ CF_PRIVATE CFStringRef _CFProcessNameString(void) {
}
return __CFProcessNameString;
}
#endif // !TARGET_OS_WASI

#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD

Expand Down Expand Up @@ -387,16 +408,20 @@ static CFURLRef _CFCopyHomeDirURLForUser(const char *username, bool fallBackToHo

#endif

#if !TARGET_OS_WASI
#define CFMaxHostNameLength 256
#define CFMaxHostNameSize (CFMaxHostNameLength+1)

CF_PRIVATE CFStringRef _CFStringCreateHostName(void) {
#if TARGET_OS_WASI
// WASI doesn't have a concept of a hostname
return CFSTR("");
#else
char myName[CFMaxHostNameSize];

// return @"" instead of nil a la CFUserName() and Ali Ozer
if (0 != gethostname(myName, CFMaxHostNameSize)) return CFSTR("");
return CFStringCreateWithCString(kCFAllocatorSystemDefault, myName, kCFPlatformInterfaceStringEncoding);
#endif
}

/* These are sanitized versions of the above functions. We might want to eliminate the above ones someday.
Expand Down Expand Up @@ -433,6 +458,8 @@ CF_EXPORT CFStringRef CFCopyUserName(void) {
result = CFStringCreateWithCString(kCFAllocatorSystemDefault, cname, kCFPlatformInterfaceStringEncoding);
}
}
#elif TARGET_OS_WASI
// WASI does not have user concept
#else
#error "Please add an implementation for CFCopyUserName() that copies the account username"
#endif
Expand Down Expand Up @@ -462,6 +489,8 @@ CF_CROSS_PLATFORM_EXPORT CFStringRef CFCopyFullUserName(void) {
GetUserNameExW(NameDisplay, (LPWSTR)wszBuffer, &ulLength);

result = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (UniChar *)wszBuffer, ulLength);
#elif TARGET_OS_WASI
// WASI does not have user concept
#else
#error "Please add an implementation for CFCopyFullUserName() that copies the full (display) user name"
#endif
Expand Down Expand Up @@ -528,6 +557,9 @@ CFURLRef CFCopyHomeDirectoryURL(void) {
if (testPath) CFRelease(testPath);

return retVal;
#elif TARGET_OS_WASI
// WASI does not have user concept
return NULL;
#else
#error Dont know how to compute users home directories on this platform
#endif
Expand Down Expand Up @@ -659,6 +691,9 @@ CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName) {
CFAllocatorDeallocate(kCFAllocatorSystemDefault, pwszUserName);

return url;
#elif TARGET_OS_WASI
// WASI does not have user concept
return NULL;
#else
#error Dont know how to compute users home directories on this platform
#endif
Expand All @@ -667,7 +702,6 @@ CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName) {

#undef CFMaxHostNameLength
#undef CFMaxHostNameSize
#endif // !TARGET_OS_WASI

#if TARGET_OS_WIN32
CF_INLINE CFIndex strlen_UniChar(const UniChar* p) {
Expand Down
6 changes: 1 addition & 5 deletions CoreFoundation/Base.subproj/CFPriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,17 @@ CF_EXTERN_C_BEGIN

CF_EXPORT void _CFRuntimeSetCFMPresent(void *a);

#if !TARGET_OS_WASI
CF_EXPORT const char *_CFProcessPath(void);
CF_EXPORT const char **_CFGetProcessPath(void);
CF_EXPORT const char **_CFGetProgname(void);

#if !TARGET_OS_WIN32
#if !TARGET_OS_WIN32 && !TARGET_OS_WASI
#include <sys/types.h>

CF_EXPORT void _CFGetUGIDs(uid_t *euid, gid_t *egid);
CF_EXPORT uid_t _CFGetEUID(void);
CF_EXPORT uid_t _CFGetEGID(void);
#endif
#endif

#if (TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_LINUX))
CF_EXPORT void _CFRunLoopSetCurrent(CFRunLoopRef rl);
Expand Down Expand Up @@ -166,7 +164,6 @@ CF_EXPORT Boolean _CFStringGetFileSystemRepresentation(CFStringRef string, UInt8
/* If this is publicized, we might need to create a GetBytesPtr type function as well. */
CF_EXPORT CFStringRef _CFStringCreateWithBytesNoCopy(CFAllocatorRef alloc, const UInt8 *bytes, CFIndex numBytes, CFStringEncoding encoding, Boolean externalFormat, CFAllocatorRef contentsDeallocator);

#if !TARGET_OS_WASI
/* These return NULL on MacOS 8 */
// This one leaks the returned string in order to be thread-safe.
// CF cannot help you in this matter if you continue to use this SPI.
Expand All @@ -178,7 +175,6 @@ CFStringRef CFCopyUserName(void);

CF_EXPORT
CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName); /* Pass NULL for the current user's home directory */
#endif


/*
Expand Down