From 565cdd16bb822a0f02edffcd241a6f9c9e70e78b Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 16 Dec 2019 15:21:05 +0100 Subject: [PATCH] Revert "darwin: speed up uv_set_process_title()" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 038eacfbf466f0b51e80b6f5fa9026c7ed767dfb. It was reported that this change causes a regression when trying to obtain the screen resolution with `CGDisplayPixelsWide()` or `CGDisplayPixelsHigh()` after changing the process title. This is the second time this change had to be reverted due to regressions and, although third time is allegedly the charm, leaving well enough alone is the proverb I plan to adhere to... Fixes: https://github.com/libuv/libuv/issues/2566 PR-URL: https://github.com/libuv/libuv/pull/2568 Reviewed-By: Michael Dawson Reviewed-By: Richard Lau Reviewed-By: Saúl Ibarra Corretgé --- src/unix/darwin-proctitle.c | 154 ++++++++++++++------------- src/unix/proctitle.c | 4 - test/test-process-title-threadsafe.c | 6 +- 3 files changed, 86 insertions(+), 78 deletions(-) diff --git a/src/unix/darwin-proctitle.c b/src/unix/darwin-proctitle.c index 8d074b349..dabde2239 100644 --- a/src/unix/darwin-proctitle.c +++ b/src/unix/darwin-proctitle.c @@ -33,57 +33,61 @@ # include #endif -#define S(s) pCFStringCreateWithCString(NULL, (s), kCFStringEncodingUTF8) - -static int (*dynamic_pthread_setname_np)(const char* name); -#if !TARGET_OS_IPHONE -static CFStringRef (*pCFStringCreateWithCString)(CFAllocatorRef, - const char*, - CFStringEncoding); -static CFBundleRef (*pCFBundleGetBundleWithIdentifier)(CFStringRef); -static void *(*pCFBundleGetDataPointerForName)(CFBundleRef, CFStringRef); -static void *(*pCFBundleGetFunctionPointerForName)(CFBundleRef, CFStringRef); -static CFTypeRef (*pLSGetCurrentApplicationASN)(void); -static OSStatus (*pLSSetApplicationInformationItem)(int, - CFTypeRef, - CFStringRef, - CFStringRef, - CFDictionaryRef*); -static void* application_services_handle; -static void* core_foundation_handle; -static CFBundleRef launch_services_bundle; -static CFStringRef* display_name_key; -static CFDictionaryRef (*pCFBundleGetInfoDictionary)(CFBundleRef); -static CFBundleRef (*pCFBundleGetMainBundle)(void); -static CFBundleRef hi_services_bundle; -static CFDictionaryRef (*pLSApplicationCheckIn)(int, CFDictionaryRef); -static void (*pLSSetApplicationLaunchServicesServerConnectionStatus)(uint64_t, - void*); - - -UV_DESTRUCTOR(static void uv__set_process_title_platform_fini(void)) { - if (core_foundation_handle != NULL) { - dlclose(core_foundation_handle); - core_foundation_handle = NULL; - } - - if (application_services_handle != NULL) { - dlclose(application_services_handle); - application_services_handle = NULL; - } -} -#endif /* !TARGET_OS_IPHONE */ - - -void uv__set_process_title_platform_init(void) { - OSStatus (*pSetApplicationIsDaemon)(int); +static int uv__pthread_setname_np(const char* name) { + int (*dynamic_pthread_setname_np)(const char* name); + char namebuf[64]; /* MAXTHREADNAMESIZE */ + int err; /* pthread_setname_np() first appeared in OS X 10.6 and iOS 3.2. */ *(void **)(&dynamic_pthread_setname_np) = dlsym(RTLD_DEFAULT, "pthread_setname_np"); -#if !TARGET_OS_IPHONE + if (dynamic_pthread_setname_np == NULL) + return UV_ENOSYS; + + strncpy(namebuf, name, sizeof(namebuf) - 1); + namebuf[sizeof(namebuf) - 1] = '\0'; + + err = dynamic_pthread_setname_np(namebuf); + if (err) + return UV__ERR(err); + + return 0; +} + + +int uv__set_process_title(const char* title) { +#if TARGET_OS_IPHONE + return uv__pthread_setname_np(title); +#else + CFStringRef (*pCFStringCreateWithCString)(CFAllocatorRef, + const char*, + CFStringEncoding); + CFBundleRef (*pCFBundleGetBundleWithIdentifier)(CFStringRef); + void *(*pCFBundleGetDataPointerForName)(CFBundleRef, CFStringRef); + void *(*pCFBundleGetFunctionPointerForName)(CFBundleRef, CFStringRef); + CFTypeRef (*pLSGetCurrentApplicationASN)(void); + OSStatus (*pLSSetApplicationInformationItem)(int, + CFTypeRef, + CFStringRef, + CFStringRef, + CFDictionaryRef*); + void* application_services_handle; + void* core_foundation_handle; + CFBundleRef launch_services_bundle; + CFStringRef* display_name_key; + CFDictionaryRef (*pCFBundleGetInfoDictionary)(CFBundleRef); + CFBundleRef (*pCFBundleGetMainBundle)(void); + CFBundleRef hi_services_bundle; + OSStatus (*pSetApplicationIsDaemon)(int); + CFDictionaryRef (*pLSApplicationCheckIn)(int, CFDictionaryRef); + void (*pLSSetApplicationLaunchServicesServerConnectionStatus)(uint64_t, + void*); + CFTypeRef asn; + int err; + + err = UV_ENOENT; application_services_handle = dlopen("/System/Library/Frameworks/" "ApplicationServices.framework/" "Versions/A/ApplicationServices", @@ -112,6 +116,8 @@ void uv__set_process_title_platform_init(void) { goto out; } +#define S(s) pCFStringCreateWithCString(NULL, (s), kCFStringEncodingUTF8) + launch_services_bundle = pCFBundleGetBundleWithIdentifier(S("com.apple.LaunchServices")); @@ -142,14 +148,13 @@ void uv__set_process_title_platform_init(void) { "CFBundleGetInfoDictionary"); *(void **)(&pCFBundleGetMainBundle) = dlsym(core_foundation_handle, "CFBundleGetMainBundle"); - if (pCFBundleGetInfoDictionary == NULL || pCFBundleGetMainBundle == NULL) goto out; /* Black 10.9 magic, to remove (Not responding) mark in Activity Monitor */ hi_services_bundle = pCFBundleGetBundleWithIdentifier(S("com.apple.HIServices")); - + err = UV_ENOENT; if (hi_services_bundle == NULL) goto out; @@ -163,39 +168,42 @@ void uv__set_process_title_platform_init(void) { pCFBundleGetFunctionPointerForName( launch_services_bundle, S("_LSSetApplicationLaunchServicesServerConnectionStatus")); - if (pSetApplicationIsDaemon == NULL || pLSApplicationCheckIn == NULL || pLSSetApplicationLaunchServicesServerConnectionStatus == NULL) { goto out; } - /* Prevent crash when LaunchServices cannot be connected to. */ - pSetApplicationIsDaemon(1); - return; + if (pSetApplicationIsDaemon(1) != noErr) + goto out; + + pLSSetApplicationLaunchServicesServerConnectionStatus(0, NULL); + + /* Check into process manager?! */ + pLSApplicationCheckIn(-2, + pCFBundleGetInfoDictionary(pCFBundleGetMainBundle())); + + asn = pLSGetCurrentApplicationASN(); + + err = UV_EINVAL; + if (pLSSetApplicationInformationItem(-2, /* Magic value. */ + asn, + *display_name_key, + S(title), + NULL) != noErr) { + goto out; + } + + uv__pthread_setname_np(title); /* Don't care if it fails. */ + err = 0; out: - uv__set_process_title_platform_fini(); + if (core_foundation_handle != NULL) + dlclose(core_foundation_handle); + + if (application_services_handle != NULL) + dlclose(application_services_handle); + + return err; #endif /* !TARGET_OS_IPHONE */ } - - -void uv__set_process_title(const char* title) { -#if !TARGET_OS_IPHONE - if (core_foundation_handle != NULL) { - CFTypeRef asn; - pLSSetApplicationLaunchServicesServerConnectionStatus(0, NULL); - pLSApplicationCheckIn(/* Magic value */ -2, - pCFBundleGetInfoDictionary(pCFBundleGetMainBundle())); - asn = pLSGetCurrentApplicationASN(); - pLSSetApplicationInformationItem(/* Magic value */ -2, asn, - *display_name_key, S(title), NULL); - } -#endif /* !TARGET_OS_IPHONE */ - - if (dynamic_pthread_setname_np != NULL) { - char namebuf[64]; /* MAXTHREADNAMESIZE */ - uv__strscpy(namebuf, title, sizeof(namebuf)); - dynamic_pthread_setname_np(namebuf); - } -} diff --git a/src/unix/proctitle.c b/src/unix/proctitle.c index a5ce2030c..1a8c7a709 100644 --- a/src/unix/proctitle.c +++ b/src/unix/proctitle.c @@ -24,7 +24,6 @@ #include #include -extern void uv__set_process_title_platform_init(void); extern void uv__set_process_title(const char* title); static uv_mutex_t process_title_mutex; @@ -39,9 +38,6 @@ static struct { static void init_process_title_mutex_once(void) { uv_mutex_init(&process_title_mutex); -#ifdef __APPLE__ - uv__set_process_title_platform_init(); -#endif } diff --git a/test/test-process-title-threadsafe.c b/test/test-process-title-threadsafe.c index 19098eda0..f35053bda 100644 --- a/test/test-process-title-threadsafe.c +++ b/test/test-process-title-threadsafe.c @@ -25,7 +25,11 @@ #include -#define NUM_ITERATIONS 50 +#ifdef __APPLE__ +# define NUM_ITERATIONS 5 +#else +# define NUM_ITERATIONS 50 +#endif static const char* titles[] = { "8L2NY0Kdj0XyNFZnmUZigIOfcWjyNr0SkMmUhKw99VLUsZFrvCQQC3XIRfNR8pjyMjXObllled",