1e66f31c5Sopenharmony_ci/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2e66f31c5Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
3e66f31c5Sopenharmony_ci * of this software and associated documentation files (the "Software"), to
4e66f31c5Sopenharmony_ci * deal in the Software without restriction, including without limitation the
5e66f31c5Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6e66f31c5Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
7e66f31c5Sopenharmony_ci * furnished to do so, subject to the following conditions:
8e66f31c5Sopenharmony_ci *
9e66f31c5Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
10e66f31c5Sopenharmony_ci * all copies or substantial portions of the Software.
11e66f31c5Sopenharmony_ci *
12e66f31c5Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13e66f31c5Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14e66f31c5Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15e66f31c5Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16e66f31c5Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17e66f31c5Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18e66f31c5Sopenharmony_ci * IN THE SOFTWARE.
19e66f31c5Sopenharmony_ci */
20e66f31c5Sopenharmony_ci
21e66f31c5Sopenharmony_ci#include "uv.h"
22e66f31c5Sopenharmony_ci#include "internal.h"
23e66f31c5Sopenharmony_ci
24e66f31c5Sopenharmony_ci#include <dlfcn.h>
25e66f31c5Sopenharmony_ci#include <errno.h>
26e66f31c5Sopenharmony_ci#include <pthread.h>
27e66f31c5Sopenharmony_ci#include <stdlib.h>
28e66f31c5Sopenharmony_ci#include <string.h>
29e66f31c5Sopenharmony_ci
30e66f31c5Sopenharmony_ci#include <TargetConditionals.h>
31e66f31c5Sopenharmony_ci
32e66f31c5Sopenharmony_ci#if !TARGET_OS_IPHONE
33e66f31c5Sopenharmony_ci#include "darwin-stub.h"
34e66f31c5Sopenharmony_ci#endif
35e66f31c5Sopenharmony_ci
36e66f31c5Sopenharmony_ci
37e66f31c5Sopenharmony_cistatic int uv__pthread_setname_np(const char* name) {
38e66f31c5Sopenharmony_ci  char namebuf[64];  /* MAXTHREADNAMESIZE */
39e66f31c5Sopenharmony_ci  int err;
40e66f31c5Sopenharmony_ci
41e66f31c5Sopenharmony_ci  strncpy(namebuf, name, sizeof(namebuf) - 1);
42e66f31c5Sopenharmony_ci  namebuf[sizeof(namebuf) - 1] = '\0';
43e66f31c5Sopenharmony_ci
44e66f31c5Sopenharmony_ci  err = pthread_setname_np(namebuf);
45e66f31c5Sopenharmony_ci  if (err)
46e66f31c5Sopenharmony_ci    return UV__ERR(err);
47e66f31c5Sopenharmony_ci
48e66f31c5Sopenharmony_ci  return 0;
49e66f31c5Sopenharmony_ci}
50e66f31c5Sopenharmony_ci
51e66f31c5Sopenharmony_ci
52e66f31c5Sopenharmony_ciint uv__set_process_title(const char* title) {
53e66f31c5Sopenharmony_ci#if TARGET_OS_IPHONE
54e66f31c5Sopenharmony_ci  return uv__pthread_setname_np(title);
55e66f31c5Sopenharmony_ci#else
56e66f31c5Sopenharmony_ci  CFStringRef (*pCFStringCreateWithCString)(CFAllocatorRef,
57e66f31c5Sopenharmony_ci                                            const char*,
58e66f31c5Sopenharmony_ci                                            CFStringEncoding);
59e66f31c5Sopenharmony_ci  CFBundleRef (*pCFBundleGetBundleWithIdentifier)(CFStringRef);
60e66f31c5Sopenharmony_ci  void *(*pCFBundleGetDataPointerForName)(CFBundleRef, CFStringRef);
61e66f31c5Sopenharmony_ci  void *(*pCFBundleGetFunctionPointerForName)(CFBundleRef, CFStringRef);
62e66f31c5Sopenharmony_ci  CFTypeRef (*pLSGetCurrentApplicationASN)(void);
63e66f31c5Sopenharmony_ci  OSStatus (*pLSSetApplicationInformationItem)(int,
64e66f31c5Sopenharmony_ci                                               CFTypeRef,
65e66f31c5Sopenharmony_ci                                               CFStringRef,
66e66f31c5Sopenharmony_ci                                               CFStringRef,
67e66f31c5Sopenharmony_ci                                               CFDictionaryRef*);
68e66f31c5Sopenharmony_ci  void* application_services_handle;
69e66f31c5Sopenharmony_ci  void* core_foundation_handle;
70e66f31c5Sopenharmony_ci  CFBundleRef launch_services_bundle;
71e66f31c5Sopenharmony_ci  CFStringRef* display_name_key;
72e66f31c5Sopenharmony_ci  CFDictionaryRef (*pCFBundleGetInfoDictionary)(CFBundleRef);
73e66f31c5Sopenharmony_ci  CFBundleRef (*pCFBundleGetMainBundle)(void);
74e66f31c5Sopenharmony_ci  CFDictionaryRef (*pLSApplicationCheckIn)(int, CFDictionaryRef);
75e66f31c5Sopenharmony_ci  void (*pLSSetApplicationLaunchServicesServerConnectionStatus)(uint64_t,
76e66f31c5Sopenharmony_ci                                                                void*);
77e66f31c5Sopenharmony_ci  CFTypeRef asn;
78e66f31c5Sopenharmony_ci  int err;
79e66f31c5Sopenharmony_ci
80e66f31c5Sopenharmony_ci  err = UV_ENOENT;
81e66f31c5Sopenharmony_ci  application_services_handle = dlopen("/System/Library/Frameworks/"
82e66f31c5Sopenharmony_ci                                       "ApplicationServices.framework/"
83e66f31c5Sopenharmony_ci                                       "Versions/A/ApplicationServices",
84e66f31c5Sopenharmony_ci                                       RTLD_LAZY | RTLD_LOCAL);
85e66f31c5Sopenharmony_ci  core_foundation_handle = dlopen("/System/Library/Frameworks/"
86e66f31c5Sopenharmony_ci                                  "CoreFoundation.framework/"
87e66f31c5Sopenharmony_ci                                  "Versions/A/CoreFoundation",
88e66f31c5Sopenharmony_ci                                  RTLD_LAZY | RTLD_LOCAL);
89e66f31c5Sopenharmony_ci
90e66f31c5Sopenharmony_ci  if (application_services_handle == NULL || core_foundation_handle == NULL)
91e66f31c5Sopenharmony_ci    goto out;
92e66f31c5Sopenharmony_ci
93e66f31c5Sopenharmony_ci  *(void **)(&pCFStringCreateWithCString) =
94e66f31c5Sopenharmony_ci      dlsym(core_foundation_handle, "CFStringCreateWithCString");
95e66f31c5Sopenharmony_ci  *(void **)(&pCFBundleGetBundleWithIdentifier) =
96e66f31c5Sopenharmony_ci      dlsym(core_foundation_handle, "CFBundleGetBundleWithIdentifier");
97e66f31c5Sopenharmony_ci  *(void **)(&pCFBundleGetDataPointerForName) =
98e66f31c5Sopenharmony_ci      dlsym(core_foundation_handle, "CFBundleGetDataPointerForName");
99e66f31c5Sopenharmony_ci  *(void **)(&pCFBundleGetFunctionPointerForName) =
100e66f31c5Sopenharmony_ci      dlsym(core_foundation_handle, "CFBundleGetFunctionPointerForName");
101e66f31c5Sopenharmony_ci
102e66f31c5Sopenharmony_ci  if (pCFStringCreateWithCString == NULL ||
103e66f31c5Sopenharmony_ci      pCFBundleGetBundleWithIdentifier == NULL ||
104e66f31c5Sopenharmony_ci      pCFBundleGetDataPointerForName == NULL ||
105e66f31c5Sopenharmony_ci      pCFBundleGetFunctionPointerForName == NULL) {
106e66f31c5Sopenharmony_ci    goto out;
107e66f31c5Sopenharmony_ci  }
108e66f31c5Sopenharmony_ci
109e66f31c5Sopenharmony_ci#define S(s) pCFStringCreateWithCString(NULL, (s), kCFStringEncodingUTF8)
110e66f31c5Sopenharmony_ci
111e66f31c5Sopenharmony_ci  launch_services_bundle =
112e66f31c5Sopenharmony_ci      pCFBundleGetBundleWithIdentifier(S("com.apple.LaunchServices"));
113e66f31c5Sopenharmony_ci
114e66f31c5Sopenharmony_ci  if (launch_services_bundle == NULL)
115e66f31c5Sopenharmony_ci    goto out;
116e66f31c5Sopenharmony_ci
117e66f31c5Sopenharmony_ci  *(void **)(&pLSGetCurrentApplicationASN) =
118e66f31c5Sopenharmony_ci      pCFBundleGetFunctionPointerForName(launch_services_bundle,
119e66f31c5Sopenharmony_ci                                         S("_LSGetCurrentApplicationASN"));
120e66f31c5Sopenharmony_ci
121e66f31c5Sopenharmony_ci  if (pLSGetCurrentApplicationASN == NULL)
122e66f31c5Sopenharmony_ci    goto out;
123e66f31c5Sopenharmony_ci
124e66f31c5Sopenharmony_ci  *(void **)(&pLSSetApplicationInformationItem) =
125e66f31c5Sopenharmony_ci      pCFBundleGetFunctionPointerForName(launch_services_bundle,
126e66f31c5Sopenharmony_ci                                         S("_LSSetApplicationInformationItem"));
127e66f31c5Sopenharmony_ci
128e66f31c5Sopenharmony_ci  if (pLSSetApplicationInformationItem == NULL)
129e66f31c5Sopenharmony_ci    goto out;
130e66f31c5Sopenharmony_ci
131e66f31c5Sopenharmony_ci  display_name_key = pCFBundleGetDataPointerForName(launch_services_bundle,
132e66f31c5Sopenharmony_ci                                                    S("_kLSDisplayNameKey"));
133e66f31c5Sopenharmony_ci
134e66f31c5Sopenharmony_ci  if (display_name_key == NULL || *display_name_key == NULL)
135e66f31c5Sopenharmony_ci    goto out;
136e66f31c5Sopenharmony_ci
137e66f31c5Sopenharmony_ci  *(void **)(&pCFBundleGetInfoDictionary) = dlsym(core_foundation_handle,
138e66f31c5Sopenharmony_ci                                     "CFBundleGetInfoDictionary");
139e66f31c5Sopenharmony_ci  *(void **)(&pCFBundleGetMainBundle) = dlsym(core_foundation_handle,
140e66f31c5Sopenharmony_ci                                 "CFBundleGetMainBundle");
141e66f31c5Sopenharmony_ci  if (pCFBundleGetInfoDictionary == NULL || pCFBundleGetMainBundle == NULL)
142e66f31c5Sopenharmony_ci    goto out;
143e66f31c5Sopenharmony_ci
144e66f31c5Sopenharmony_ci  *(void **)(&pLSApplicationCheckIn) = pCFBundleGetFunctionPointerForName(
145e66f31c5Sopenharmony_ci      launch_services_bundle,
146e66f31c5Sopenharmony_ci      S("_LSApplicationCheckIn"));
147e66f31c5Sopenharmony_ci
148e66f31c5Sopenharmony_ci  if (pLSApplicationCheckIn == NULL)
149e66f31c5Sopenharmony_ci    goto out;
150e66f31c5Sopenharmony_ci
151e66f31c5Sopenharmony_ci  *(void **)(&pLSSetApplicationLaunchServicesServerConnectionStatus) =
152e66f31c5Sopenharmony_ci      pCFBundleGetFunctionPointerForName(
153e66f31c5Sopenharmony_ci          launch_services_bundle,
154e66f31c5Sopenharmony_ci          S("_LSSetApplicationLaunchServicesServerConnectionStatus"));
155e66f31c5Sopenharmony_ci
156e66f31c5Sopenharmony_ci  if (pLSSetApplicationLaunchServicesServerConnectionStatus == NULL)
157e66f31c5Sopenharmony_ci    goto out;
158e66f31c5Sopenharmony_ci
159e66f31c5Sopenharmony_ci  pLSSetApplicationLaunchServicesServerConnectionStatus(0, NULL);
160e66f31c5Sopenharmony_ci
161e66f31c5Sopenharmony_ci  /* Check into process manager?! */
162e66f31c5Sopenharmony_ci  pLSApplicationCheckIn(-2,
163e66f31c5Sopenharmony_ci                        pCFBundleGetInfoDictionary(pCFBundleGetMainBundle()));
164e66f31c5Sopenharmony_ci
165e66f31c5Sopenharmony_ci  asn = pLSGetCurrentApplicationASN();
166e66f31c5Sopenharmony_ci
167e66f31c5Sopenharmony_ci  err = UV_EBUSY;
168e66f31c5Sopenharmony_ci  if (asn == NULL)
169e66f31c5Sopenharmony_ci    goto out;
170e66f31c5Sopenharmony_ci
171e66f31c5Sopenharmony_ci  err = UV_EINVAL;
172e66f31c5Sopenharmony_ci  if (pLSSetApplicationInformationItem(-2,  /* Magic value. */
173e66f31c5Sopenharmony_ci                                       asn,
174e66f31c5Sopenharmony_ci                                       *display_name_key,
175e66f31c5Sopenharmony_ci                                       S(title),
176e66f31c5Sopenharmony_ci                                       NULL) != noErr) {
177e66f31c5Sopenharmony_ci    goto out;
178e66f31c5Sopenharmony_ci  }
179e66f31c5Sopenharmony_ci
180e66f31c5Sopenharmony_ci  uv__pthread_setname_np(title);  /* Don't care if it fails. */
181e66f31c5Sopenharmony_ci  err = 0;
182e66f31c5Sopenharmony_ci
183e66f31c5Sopenharmony_ciout:
184e66f31c5Sopenharmony_ci  if (core_foundation_handle != NULL)
185e66f31c5Sopenharmony_ci    dlclose(core_foundation_handle);
186e66f31c5Sopenharmony_ci
187e66f31c5Sopenharmony_ci  if (application_services_handle != NULL)
188e66f31c5Sopenharmony_ci    dlclose(application_services_handle);
189e66f31c5Sopenharmony_ci
190e66f31c5Sopenharmony_ci  return err;
191e66f31c5Sopenharmony_ci#endif  /* !TARGET_OS_IPHONE */
192e66f31c5Sopenharmony_ci}
193