~webapps/unity-js-scopes/node.js

« back to all changes in this revision

Viewing changes to deps/uv/src/unix/darwin-proctitle.c

  • Committer: Marcus Tomlinson
  • Date: 2015-11-13 07:59:04 UTC
  • Revision ID: marcus.tomlinson@canonical.com-20151113075904-h0swczmoq1rvstfc
Node v4 (stable)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <dlfcn.h>
 
22
#include <errno.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
 
 
26
#include <TargetConditionals.h>
 
27
 
 
28
#if !TARGET_OS_IPHONE
 
29
# include <CoreFoundation/CoreFoundation.h>
 
30
# include <ApplicationServices/ApplicationServices.h>
 
31
#endif
 
32
 
 
33
 
 
34
static int uv__pthread_setname_np(const char* name) {
 
35
  int (*dynamic_pthread_setname_np)(const char* name);
 
36
  char namebuf[64];  /* MAXTHREADNAMESIZE */
 
37
  int err;
 
38
 
 
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");
 
42
 
 
43
  if (dynamic_pthread_setname_np == NULL)
 
44
    return -ENOSYS;
 
45
 
 
46
  strncpy(namebuf, name, sizeof(namebuf) - 1);
 
47
  namebuf[sizeof(namebuf) - 1] = '\0';
 
48
 
 
49
  err = dynamic_pthread_setname_np(namebuf);
 
50
  if (err)
 
51
    return -err;
 
52
 
 
53
  return 0;
 
54
}
 
55
 
 
56
 
 
57
int uv__set_process_title(const char* title) {
 
58
#if TARGET_OS_IPHONE
 
59
  return uv__pthread_setname_np(title);
 
60
#else
 
61
  CFStringRef (*pCFStringCreateWithCString)(CFAllocatorRef,
 
62
                                            const char*,
 
63
                                            CFStringEncoding);
 
64
  CFBundleRef (*pCFBundleGetBundleWithIdentifier)(CFStringRef);
 
65
  void *(*pCFBundleGetDataPointerForName)(CFBundleRef, CFStringRef);
 
66
  void *(*pCFBundleGetFunctionPointerForName)(CFBundleRef, CFStringRef);
 
67
  CFTypeRef (*pLSGetCurrentApplicationASN)(void);
 
68
  OSStatus (*pLSSetApplicationInformationItem)(int,
 
69
                                               CFTypeRef,
 
70
                                               CFStringRef,
 
71
                                               CFStringRef,
 
72
                                               CFDictionaryRef*);
 
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,
 
83
                                                                void*);
 
84
  CFTypeRef asn;
 
85
  int err;
 
86
 
 
87
  err = -ENOENT;
 
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);
 
96
 
 
97
  if (application_services_handle == NULL || core_foundation_handle == NULL)
 
98
    goto out;
 
99
 
 
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");
 
108
 
 
109
  if (pCFStringCreateWithCString == NULL ||
 
110
      pCFBundleGetBundleWithIdentifier == NULL ||
 
111
      pCFBundleGetDataPointerForName == NULL ||
 
112
      pCFBundleGetFunctionPointerForName == NULL) {
 
113
    goto out;
 
114
  }
 
115
 
 
116
#define S(s) pCFStringCreateWithCString(NULL, (s), kCFStringEncodingUTF8)
 
117
 
 
118
  launch_services_bundle =
 
119
      pCFBundleGetBundleWithIdentifier(S("com.apple.LaunchServices"));
 
120
 
 
121
  if (launch_services_bundle == NULL)
 
122
    goto out;
 
123
 
 
124
  *(void **)(&pLSGetCurrentApplicationASN) =
 
125
      pCFBundleGetFunctionPointerForName(launch_services_bundle,
 
126
                                         S("_LSGetCurrentApplicationASN"));
 
127
 
 
128
  if (pLSGetCurrentApplicationASN == NULL)
 
129
    goto out;
 
130
 
 
131
  *(void **)(&pLSSetApplicationInformationItem) =
 
132
      pCFBundleGetFunctionPointerForName(launch_services_bundle,
 
133
                                         S("_LSSetApplicationInformationItem"));
 
134
 
 
135
  if (pLSSetApplicationInformationItem == NULL)
 
136
    goto out;
 
137
 
 
138
  display_name_key = pCFBundleGetDataPointerForName(launch_services_bundle,
 
139
                                                    S("_kLSDisplayNameKey"));
 
140
 
 
141
  if (display_name_key == NULL || *display_name_key == NULL)
 
142
    goto out;
 
143
 
 
144
  *(void **)(&pCFBundleGetInfoDictionary) = dlsym(core_foundation_handle,
 
145
                                     "CFBundleGetInfoDictionary");
 
146
  *(void **)(&pCFBundleGetMainBundle) = dlsym(core_foundation_handle,
 
147
                                 "CFBundleGetMainBundle");
 
148
  if (pCFBundleGetInfoDictionary == NULL || pCFBundleGetMainBundle == NULL)
 
149
    goto out;
 
150
 
 
151
  /* Black 10.9 magic, to remove (Not responding) mark in Activity Monitor */
 
152
  hi_services_bundle =
 
153
      pCFBundleGetBundleWithIdentifier(S("com.apple.HIServices"));
 
154
  err = -ENOENT;
 
155
  if (hi_services_bundle == NULL)
 
156
    goto out;
 
157
 
 
158
  *(void **)(&pSetApplicationIsDaemon) = pCFBundleGetFunctionPointerForName(
 
159
      hi_services_bundle,
 
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) {
 
171
    goto out;
 
172
  }
 
173
 
 
174
  if (pSetApplicationIsDaemon(1) != noErr)
 
175
    goto out;
 
176
 
 
177
  pLSSetApplicationLaunchServicesServerConnectionStatus(0, NULL);
 
178
 
 
179
  /* Check into process manager?! */
 
180
  pLSApplicationCheckIn(-2,
 
181
                        pCFBundleGetInfoDictionary(pCFBundleGetMainBundle()));
 
182
 
 
183
  asn = pLSGetCurrentApplicationASN();
 
184
 
 
185
  err = -EINVAL;
 
186
  if (pLSSetApplicationInformationItem(-2,  /* Magic value. */
 
187
                                       asn,
 
188
                                       *display_name_key,
 
189
                                       S(title),
 
190
                                       NULL) != noErr) {
 
191
    goto out;
 
192
  }
 
193
 
 
194
  uv__pthread_setname_np(title);  /* Don't care if it fails. */
 
195
  err = 0;
 
196
 
 
197
out:
 
198
  if (core_foundation_handle != NULL)
 
199
    dlclose(core_foundation_handle);
 
200
 
 
201
  if (application_services_handle != NULL)
 
202
    dlclose(application_services_handle);
 
203
 
 
204
  return err;
 
205
#endif  /* !TARGET_OS_IPHONE */
 
206
}