• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
8  *
9  * The above copyright notice and this permission notice shall be included in
10  * all copies or substantial portions of the Software.
11  *
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
18  * IN THE SOFTWARE.
19  */
20 
21 #include "uv.h"
22 #include "internal.h"
23 
24 #include <dlfcn.h>
25 #include <errno.h>
26 #include <pthread.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include <TargetConditionals.h>
31 
32 #if !TARGET_OS_IPHONE
33 # include <CoreFoundation/CoreFoundation.h>
34 # include <ApplicationServices/ApplicationServices.h>
35 #endif
36 
37 
uv__pthread_setname_np(const char * name)38 static int uv__pthread_setname_np(const char* name) {
39   char namebuf[64];  /* MAXTHREADNAMESIZE */
40   int err;
41 
42   strncpy(namebuf, name, sizeof(namebuf) - 1);
43   namebuf[sizeof(namebuf) - 1] = '\0';
44 
45   err = pthread_setname_np(namebuf);
46   if (err)
47     return UV__ERR(err);
48 
49   return 0;
50 }
51 
52 
uv__set_process_title(const char * title)53 int uv__set_process_title(const char* title) {
54 #if TARGET_OS_IPHONE
55   return uv__pthread_setname_np(title);
56 #else
57   CFStringRef (*pCFStringCreateWithCString)(CFAllocatorRef,
58                                             const char*,
59                                             CFStringEncoding);
60   CFBundleRef (*pCFBundleGetBundleWithIdentifier)(CFStringRef);
61   void *(*pCFBundleGetDataPointerForName)(CFBundleRef, CFStringRef);
62   void *(*pCFBundleGetFunctionPointerForName)(CFBundleRef, CFStringRef);
63   CFTypeRef (*pLSGetCurrentApplicationASN)(void);
64   OSStatus (*pLSSetApplicationInformationItem)(int,
65                                                CFTypeRef,
66                                                CFStringRef,
67                                                CFStringRef,
68                                                CFDictionaryRef*);
69   void* application_services_handle;
70   void* core_foundation_handle;
71   CFBundleRef launch_services_bundle;
72   CFStringRef* display_name_key;
73   CFDictionaryRef (*pCFBundleGetInfoDictionary)(CFBundleRef);
74   CFBundleRef (*pCFBundleGetMainBundle)(void);
75   CFDictionaryRef (*pLSApplicationCheckIn)(int, CFDictionaryRef);
76   void (*pLSSetApplicationLaunchServicesServerConnectionStatus)(uint64_t,
77                                                                 void*);
78   CFTypeRef asn;
79   int err;
80 
81   err = UV_ENOENT;
82   application_services_handle = dlopen("/System/Library/Frameworks/"
83                                        "ApplicationServices.framework/"
84                                        "Versions/A/ApplicationServices",
85                                        RTLD_LAZY | RTLD_LOCAL);
86   core_foundation_handle = dlopen("/System/Library/Frameworks/"
87                                   "CoreFoundation.framework/"
88                                   "Versions/A/CoreFoundation",
89                                   RTLD_LAZY | RTLD_LOCAL);
90 
91   if (application_services_handle == NULL || core_foundation_handle == NULL)
92     goto out;
93 
94   *(void **)(&pCFStringCreateWithCString) =
95       dlsym(core_foundation_handle, "CFStringCreateWithCString");
96   *(void **)(&pCFBundleGetBundleWithIdentifier) =
97       dlsym(core_foundation_handle, "CFBundleGetBundleWithIdentifier");
98   *(void **)(&pCFBundleGetDataPointerForName) =
99       dlsym(core_foundation_handle, "CFBundleGetDataPointerForName");
100   *(void **)(&pCFBundleGetFunctionPointerForName) =
101       dlsym(core_foundation_handle, "CFBundleGetFunctionPointerForName");
102 
103   if (pCFStringCreateWithCString == NULL ||
104       pCFBundleGetBundleWithIdentifier == NULL ||
105       pCFBundleGetDataPointerForName == NULL ||
106       pCFBundleGetFunctionPointerForName == NULL) {
107     goto out;
108   }
109 
110 #define S(s) pCFStringCreateWithCString(NULL, (s), kCFStringEncodingUTF8)
111 
112   launch_services_bundle =
113       pCFBundleGetBundleWithIdentifier(S("com.apple.LaunchServices"));
114 
115   if (launch_services_bundle == NULL)
116     goto out;
117 
118   *(void **)(&pLSGetCurrentApplicationASN) =
119       pCFBundleGetFunctionPointerForName(launch_services_bundle,
120                                          S("_LSGetCurrentApplicationASN"));
121 
122   if (pLSGetCurrentApplicationASN == NULL)
123     goto out;
124 
125   *(void **)(&pLSSetApplicationInformationItem) =
126       pCFBundleGetFunctionPointerForName(launch_services_bundle,
127                                          S("_LSSetApplicationInformationItem"));
128 
129   if (pLSSetApplicationInformationItem == NULL)
130     goto out;
131 
132   display_name_key = pCFBundleGetDataPointerForName(launch_services_bundle,
133                                                     S("_kLSDisplayNameKey"));
134 
135   if (display_name_key == NULL || *display_name_key == NULL)
136     goto out;
137 
138   *(void **)(&pCFBundleGetInfoDictionary) = dlsym(core_foundation_handle,
139                                      "CFBundleGetInfoDictionary");
140   *(void **)(&pCFBundleGetMainBundle) = dlsym(core_foundation_handle,
141                                  "CFBundleGetMainBundle");
142   if (pCFBundleGetInfoDictionary == NULL || pCFBundleGetMainBundle == NULL)
143     goto out;
144 
145   *(void **)(&pLSApplicationCheckIn) = pCFBundleGetFunctionPointerForName(
146       launch_services_bundle,
147       S("_LSApplicationCheckIn"));
148 
149   if (pLSApplicationCheckIn == NULL)
150     goto out;
151 
152   *(void **)(&pLSSetApplicationLaunchServicesServerConnectionStatus) =
153       pCFBundleGetFunctionPointerForName(
154           launch_services_bundle,
155           S("_LSSetApplicationLaunchServicesServerConnectionStatus"));
156 
157   if (pLSSetApplicationLaunchServicesServerConnectionStatus == NULL)
158     goto out;
159 
160   pLSSetApplicationLaunchServicesServerConnectionStatus(0, NULL);
161 
162   /* Check into process manager?! */
163   pLSApplicationCheckIn(-2,
164                         pCFBundleGetInfoDictionary(pCFBundleGetMainBundle()));
165 
166   asn = pLSGetCurrentApplicationASN();
167 
168   err = UV_EBUSY;
169   if (asn == NULL)
170     goto out;
171 
172   err = UV_EINVAL;
173   if (pLSSetApplicationInformationItem(-2,  /* Magic value. */
174                                        asn,
175                                        *display_name_key,
176                                        S(title),
177                                        NULL) != noErr) {
178     goto out;
179   }
180 
181   uv__pthread_setname_np(title);  /* Don't care if it fails. */
182   err = 0;
183 
184 out:
185   if (core_foundation_handle != NULL)
186     dlclose(core_foundation_handle);
187 
188   if (application_services_handle != NULL)
189     dlclose(application_services_handle);
190 
191   return err;
192 #endif  /* !TARGET_OS_IPHONE */
193 }
194