c3171a6176e52e51dd6db34a2a459849d2827dd6
[platform/upstream/nodejs.git] / deps / uv / src / unix / darwin-proctitle.c
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 <TargetConditionals.h>
22
23 #if !TARGET_OS_IPHONE
24 # include <CoreFoundation/CoreFoundation.h>
25 # include <ApplicationServices/ApplicationServices.h>
26 #endif
27
28
29 int uv__set_process_title(const char* title) {
30 #if TARGET_OS_IPHONE
31   return -1;
32 #else
33   typedef CFTypeRef (*LSGetCurrentApplicationASNType)(void);
34   typedef OSStatus (*LSSetApplicationInformationItemType)(int,
35                                                           CFTypeRef,
36                                                           CFStringRef,
37                                                           CFStringRef,
38                                                           CFDictionaryRef*);
39   CFBundleRef launch_services_bundle;
40   LSGetCurrentApplicationASNType ls_get_current_application_asn;
41   LSSetApplicationInformationItemType ls_set_application_information_item;
42   CFStringRef* display_name_key;
43   ProcessSerialNumber psn;
44   CFTypeRef asn;
45   CFStringRef display_name;
46   OSStatus err;
47
48   launch_services_bundle =
49       CFBundleGetBundleWithIdentifier(CFSTR("com.apple.LaunchServices"));
50
51   if (launch_services_bundle == NULL)
52     return -1;
53
54   ls_get_current_application_asn = (LSGetCurrentApplicationASNType)
55       CFBundleGetFunctionPointerForName(launch_services_bundle,
56                                         CFSTR("_LSGetCurrentApplicationASN"));
57
58   if (ls_get_current_application_asn == NULL)
59     return -1;
60
61   ls_set_application_information_item = (LSSetApplicationInformationItemType)
62       CFBundleGetFunctionPointerForName(launch_services_bundle,
63                                         CFSTR("_LSSetApplicationInformationItem"));
64
65   if (ls_set_application_information_item == NULL)
66     return -1;
67
68   display_name_key = CFBundleGetDataPointerForName(launch_services_bundle,
69                                                    CFSTR("_kLSDisplayNameKey"));
70
71   if (display_name_key == NULL || *display_name_key == NULL)
72     return -1;
73
74   /* Force the process manager to initialize. */
75   GetCurrentProcess(&psn);
76
77   display_name = CFStringCreateWithCString(NULL, title, kCFStringEncodingUTF8);
78   asn = ls_get_current_application_asn();
79   err = ls_set_application_information_item(-2,  /* Magic value. */
80                                             asn,
81                                             *display_name_key,
82                                             display_name,
83                                             NULL);
84
85   return (err == noErr) ? 0 : -1;
86 #endif  /* !TARGET_OS_IPHONE */
87 }