xref: /libuv/src/unix/darwin-proctitle.c (revision 61c966cf)
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 "darwin-stub.h"
34 #endif
35 
uv__set_process_title(const char * title)36 int uv__set_process_title(const char* title) {
37 #if TARGET_OS_IPHONE
38   return uv__thread_setname(title);
39 #else
40   CFStringRef (*pCFStringCreateWithCString)(CFAllocatorRef,
41                                             const char*,
42                                             CFStringEncoding);
43   CFBundleRef (*pCFBundleGetBundleWithIdentifier)(CFStringRef);
44   void *(*pCFBundleGetDataPointerForName)(CFBundleRef, CFStringRef);
45   void *(*pCFBundleGetFunctionPointerForName)(CFBundleRef, CFStringRef);
46   CFTypeRef (*pLSGetCurrentApplicationASN)(void);
47   OSStatus (*pLSSetApplicationInformationItem)(int,
48                                                CFTypeRef,
49                                                CFStringRef,
50                                                CFStringRef,
51                                                CFDictionaryRef*);
52   void* application_services_handle;
53   void* core_foundation_handle;
54   CFBundleRef launch_services_bundle;
55   CFStringRef* display_name_key;
56   CFDictionaryRef (*pCFBundleGetInfoDictionary)(CFBundleRef);
57   CFBundleRef (*pCFBundleGetMainBundle)(void);
58   CFDictionaryRef (*pLSApplicationCheckIn)(int, CFDictionaryRef);
59   void (*pLSSetApplicationLaunchServicesServerConnectionStatus)(uint64_t,
60                                                                 void*);
61   CFTypeRef asn;
62   int err;
63 
64   err = UV_ENOENT;
65   application_services_handle = dlopen("/System/Library/Frameworks/"
66                                        "ApplicationServices.framework/"
67                                        "Versions/A/ApplicationServices",
68                                        RTLD_LAZY | RTLD_LOCAL);
69   core_foundation_handle = dlopen("/System/Library/Frameworks/"
70                                   "CoreFoundation.framework/"
71                                   "Versions/A/CoreFoundation",
72                                   RTLD_LAZY | RTLD_LOCAL);
73 
74   if (application_services_handle == NULL || core_foundation_handle == NULL)
75     goto out;
76 
77   *(void **)(&pCFStringCreateWithCString) =
78       dlsym(core_foundation_handle, "CFStringCreateWithCString");
79   *(void **)(&pCFBundleGetBundleWithIdentifier) =
80       dlsym(core_foundation_handle, "CFBundleGetBundleWithIdentifier");
81   *(void **)(&pCFBundleGetDataPointerForName) =
82       dlsym(core_foundation_handle, "CFBundleGetDataPointerForName");
83   *(void **)(&pCFBundleGetFunctionPointerForName) =
84       dlsym(core_foundation_handle, "CFBundleGetFunctionPointerForName");
85 
86   if (pCFStringCreateWithCString == NULL ||
87       pCFBundleGetBundleWithIdentifier == NULL ||
88       pCFBundleGetDataPointerForName == NULL ||
89       pCFBundleGetFunctionPointerForName == NULL) {
90     goto out;
91   }
92 
93 #define S(s) pCFStringCreateWithCString(NULL, (s), kCFStringEncodingUTF8)
94 
95   launch_services_bundle =
96       pCFBundleGetBundleWithIdentifier(S("com.apple.LaunchServices"));
97 
98   if (launch_services_bundle == NULL)
99     goto out;
100 
101   *(void **)(&pLSGetCurrentApplicationASN) =
102       pCFBundleGetFunctionPointerForName(launch_services_bundle,
103                                          S("_LSGetCurrentApplicationASN"));
104 
105   if (pLSGetCurrentApplicationASN == NULL)
106     goto out;
107 
108   *(void **)(&pLSSetApplicationInformationItem) =
109       pCFBundleGetFunctionPointerForName(launch_services_bundle,
110                                          S("_LSSetApplicationInformationItem"));
111 
112   if (pLSSetApplicationInformationItem == NULL)
113     goto out;
114 
115   display_name_key = pCFBundleGetDataPointerForName(launch_services_bundle,
116                                                     S("_kLSDisplayNameKey"));
117 
118   if (display_name_key == NULL || *display_name_key == NULL)
119     goto out;
120 
121   *(void **)(&pCFBundleGetInfoDictionary) = dlsym(core_foundation_handle,
122                                      "CFBundleGetInfoDictionary");
123   *(void **)(&pCFBundleGetMainBundle) = dlsym(core_foundation_handle,
124                                  "CFBundleGetMainBundle");
125   if (pCFBundleGetInfoDictionary == NULL || pCFBundleGetMainBundle == NULL)
126     goto out;
127 
128   *(void **)(&pLSApplicationCheckIn) = pCFBundleGetFunctionPointerForName(
129       launch_services_bundle,
130       S("_LSApplicationCheckIn"));
131 
132   if (pLSApplicationCheckIn == NULL)
133     goto out;
134 
135   *(void **)(&pLSSetApplicationLaunchServicesServerConnectionStatus) =
136       pCFBundleGetFunctionPointerForName(
137           launch_services_bundle,
138           S("_LSSetApplicationLaunchServicesServerConnectionStatus"));
139 
140   if (pLSSetApplicationLaunchServicesServerConnectionStatus == NULL)
141     goto out;
142 
143   pLSSetApplicationLaunchServicesServerConnectionStatus(0, NULL);
144 
145   /* Check into process manager?! */
146   pLSApplicationCheckIn(-2,
147                         pCFBundleGetInfoDictionary(pCFBundleGetMainBundle()));
148 
149   asn = pLSGetCurrentApplicationASN();
150 
151   err = UV_EBUSY;
152   if (asn == NULL)
153     goto out;
154 
155   err = UV_EINVAL;
156   if (pLSSetApplicationInformationItem(-2,  /* Magic value. */
157                                        asn,
158                                        *display_name_key,
159                                        S(title),
160                                        NULL) != noErr) {
161     goto out;
162   }
163 
164   uv__thread_setname(title);  /* Don't care if it fails. */
165   err = 0;
166 
167 out:
168   if (core_foundation_handle != NULL)
169     dlclose(core_foundation_handle);
170 
171   if (application_services_handle != NULL)
172     dlclose(application_services_handle);
173 
174   return err;
175 #endif  /* !TARGET_OS_IPHONE */
176 }
177