1
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2
* Permission is hereby granted, free of charge, to any person obtaining a copy
3
* of this software and associated documentation files (the "Software"), to
4
* deal in the Software without restriction, including without limitation the
5
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6
* sell copies of the Software, and to permit persons to whom the Software is
7
* furnished to do so, subject to the following conditions:
9
* The above copyright notice and this permission notice shall be included in
10
* all copies or substantial portions of the Software.
12
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26
#include <TargetConditionals.h>
29
# include <CoreFoundation/CoreFoundation.h>
30
# include <ApplicationServices/ApplicationServices.h>
34
static int uv__pthread_setname_np(const char* name) {
35
int (*dynamic_pthread_setname_np)(const char* name);
36
char namebuf[64]; /* MAXTHREADNAMESIZE */
39
/* pthread_setname_np() first appeared in OS X 10.6 and iOS 3.2. */
40
*(void **)(&dynamic_pthread_setname_np) =
41
dlsym(RTLD_DEFAULT, "pthread_setname_np");
43
if (dynamic_pthread_setname_np == NULL)
46
strncpy(namebuf, name, sizeof(namebuf) - 1);
47
namebuf[sizeof(namebuf) - 1] = '\0';
49
err = dynamic_pthread_setname_np(namebuf);
57
int uv__set_process_title(const char* title) {
59
return uv__pthread_setname_np(title);
61
CFStringRef (*pCFStringCreateWithCString)(CFAllocatorRef,
64
CFBundleRef (*pCFBundleGetBundleWithIdentifier)(CFStringRef);
65
void *(*pCFBundleGetDataPointerForName)(CFBundleRef, CFStringRef);
66
void *(*pCFBundleGetFunctionPointerForName)(CFBundleRef, CFStringRef);
67
CFTypeRef (*pLSGetCurrentApplicationASN)(void);
68
OSStatus (*pLSSetApplicationInformationItem)(int,
73
void* application_services_handle;
74
void* core_foundation_handle;
75
CFBundleRef launch_services_bundle;
76
CFStringRef* display_name_key;
77
CFDictionaryRef (*pCFBundleGetInfoDictionary)(CFBundleRef);
78
CFBundleRef (*pCFBundleGetMainBundle)(void);
79
CFBundleRef hi_services_bundle;
80
OSStatus (*pSetApplicationIsDaemon)(int);
81
CFDictionaryRef (*pLSApplicationCheckIn)(int, CFDictionaryRef);
82
void (*pLSSetApplicationLaunchServicesServerConnectionStatus)(uint64_t,
88
application_services_handle = dlopen("/System/Library/Frameworks/"
89
"ApplicationServices.framework/"
90
"Versions/A/ApplicationServices",
91
RTLD_LAZY | RTLD_LOCAL);
92
core_foundation_handle = dlopen("/System/Library/Frameworks/"
93
"CoreFoundation.framework/"
94
"Versions/A/CoreFoundation",
95
RTLD_LAZY | RTLD_LOCAL);
97
if (application_services_handle == NULL || core_foundation_handle == NULL)
100
*(void **)(&pCFStringCreateWithCString) =
101
dlsym(core_foundation_handle, "CFStringCreateWithCString");
102
*(void **)(&pCFBundleGetBundleWithIdentifier) =
103
dlsym(core_foundation_handle, "CFBundleGetBundleWithIdentifier");
104
*(void **)(&pCFBundleGetDataPointerForName) =
105
dlsym(core_foundation_handle, "CFBundleGetDataPointerForName");
106
*(void **)(&pCFBundleGetFunctionPointerForName) =
107
dlsym(core_foundation_handle, "CFBundleGetFunctionPointerForName");
109
if (pCFStringCreateWithCString == NULL ||
110
pCFBundleGetBundleWithIdentifier == NULL ||
111
pCFBundleGetDataPointerForName == NULL ||
112
pCFBundleGetFunctionPointerForName == NULL) {
116
#define S(s) pCFStringCreateWithCString(NULL, (s), kCFStringEncodingUTF8)
118
launch_services_bundle =
119
pCFBundleGetBundleWithIdentifier(S("com.apple.LaunchServices"));
121
if (launch_services_bundle == NULL)
124
*(void **)(&pLSGetCurrentApplicationASN) =
125
pCFBundleGetFunctionPointerForName(launch_services_bundle,
126
S("_LSGetCurrentApplicationASN"));
128
if (pLSGetCurrentApplicationASN == NULL)
131
*(void **)(&pLSSetApplicationInformationItem) =
132
pCFBundleGetFunctionPointerForName(launch_services_bundle,
133
S("_LSSetApplicationInformationItem"));
135
if (pLSSetApplicationInformationItem == NULL)
138
display_name_key = pCFBundleGetDataPointerForName(launch_services_bundle,
139
S("_kLSDisplayNameKey"));
141
if (display_name_key == NULL || *display_name_key == NULL)
144
*(void **)(&pCFBundleGetInfoDictionary) = dlsym(core_foundation_handle,
145
"CFBundleGetInfoDictionary");
146
*(void **)(&pCFBundleGetMainBundle) = dlsym(core_foundation_handle,
147
"CFBundleGetMainBundle");
148
if (pCFBundleGetInfoDictionary == NULL || pCFBundleGetMainBundle == NULL)
151
/* Black 10.9 magic, to remove (Not responding) mark in Activity Monitor */
153
pCFBundleGetBundleWithIdentifier(S("com.apple.HIServices"));
155
if (hi_services_bundle == NULL)
158
*(void **)(&pSetApplicationIsDaemon) = pCFBundleGetFunctionPointerForName(
160
S("SetApplicationIsDaemon"));
161
*(void **)(&pLSApplicationCheckIn) = pCFBundleGetFunctionPointerForName(
162
launch_services_bundle,
163
S("_LSApplicationCheckIn"));
164
*(void **)(&pLSSetApplicationLaunchServicesServerConnectionStatus) =
165
pCFBundleGetFunctionPointerForName(
166
launch_services_bundle,
167
S("_LSSetApplicationLaunchServicesServerConnectionStatus"));
168
if (pSetApplicationIsDaemon == NULL ||
169
pLSApplicationCheckIn == NULL ||
170
pLSSetApplicationLaunchServicesServerConnectionStatus == NULL) {
174
if (pSetApplicationIsDaemon(1) != noErr)
177
pLSSetApplicationLaunchServicesServerConnectionStatus(0, NULL);
179
/* Check into process manager?! */
180
pLSApplicationCheckIn(-2,
181
pCFBundleGetInfoDictionary(pCFBundleGetMainBundle()));
183
asn = pLSGetCurrentApplicationASN();
186
if (pLSSetApplicationInformationItem(-2, /* Magic value. */
194
uv__pthread_setname_np(title); /* Don't care if it fails. */
198
if (core_foundation_handle != NULL)
199
dlclose(core_foundation_handle);
201
if (application_services_handle != NULL)
202
dlclose(application_services_handle);
205
#endif /* !TARGET_OS_IPHONE */