Browse files

darwin: implement uv_set_process_title, part 1

Apply undocumented Carbon magic to change the process title in a way
that's visible in the Activity Monitor.
  • Loading branch information...
1 parent 0761fb5 commit 14eb8b03919db9004503691c63c937fef9c405b1 @bnoordhuis bnoordhuis committed Feb 24, 2013
Showing with 107 additions and 4 deletions.
  1. +8 −1 config-unix.mk
  2. +78 −0 src/unix/darwin-proctitle.m
  3. +14 −2 src/unix/darwin.c
  4. +7 −1 uv.gyp
View
9 config-unix.mk
@@ -70,11 +70,15 @@ endif
ifeq (darwin,$(OS))
CPPFLAGS += -D_DARWIN_USE_64_BIT_INODE=1
-LDFLAGS+=-framework CoreServices -dynamiclib -install_name "@rpath/libuv.dylib"
+LDFLAGS += -framework Foundation \
+ -framework CoreServices \
+ -framework ApplicationServices \
+ -dynamiclib -install_name "@rpath/libuv.dylib"
SOEXT = dylib
OBJS += src/unix/darwin.o
OBJS += src/unix/kqueue.o
OBJS += src/unix/fsevents.o
+OBJS += src/unix/darwin-proctitle.o
endif
ifeq (linux,$(OS))
@@ -156,3 +160,6 @@ clean-platform:
distclean-platform:
-rm -f libuv.a libuv.$(SOEXT) test/run-{tests,benchmarks}.dSYM
+
+%.pic.o %.o: %.m
+ $(CC) $(CPPFLAGS) $(CFLAGS) -c $^ -o $@
View
78 src/unix/darwin-proctitle.m
@@ -0,0 +1,78 @@
+/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <Cocoa/Cocoa.h>
+
+
+int uv__set_process_title(const char* title) {
+ typedef CFTypeRef (*LSGetCurrentApplicationASNType)(void);
+ typedef OSStatus (*LSSetApplicationInformationItemType)(int,
+ CFTypeRef,
+ CFStringRef,
+ CFStringRef,
+ CFDictionaryRef*);
+ CFBundleRef launch_services_bundle;
+ LSGetCurrentApplicationASNType ls_get_current_application_asn;
+ LSSetApplicationInformationItemType ls_set_application_information_item;
+ CFStringRef* display_name_key;
+ ProcessSerialNumber psn;
+ CFTypeRef asn;
+ CFStringRef display_name;
+ OSStatus err;
+
+ launch_services_bundle =
+ CFBundleGetBundleWithIdentifier(CFSTR("com.apple.LaunchServices"));
+
+ if (launch_services_bundle == NULL)
+ return -1;
+
+ ls_get_current_application_asn =
+ CFBundleGetFunctionPointerForName(launch_services_bundle,
+ CFSTR("_LSGetCurrentApplicationASN"));
+
+ if (ls_get_current_application_asn == NULL)
+ return -1;
+
+ ls_set_application_information_item =
+ CFBundleGetFunctionPointerForName(launch_services_bundle,
+ CFSTR("_LSSetApplicationInformationItem"));
+
+ if (ls_set_application_information_item == NULL)
+ return -1;
+
+ display_name_key = CFBundleGetDataPointerForName(launch_services_bundle,
+ CFSTR("_kLSDisplayNameKey"));
+
+ if (display_name_key == NULL || *display_name_key == NULL)
+ return -1;
+
+ /* Force the process manager to initialize. */
+ GetCurrentProcess(&psn);
+
+ display_name = CFStringCreateWithCString(NULL, title, kCFStringEncodingUTF8);
+ asn = ls_get_current_application_asn();
+ err = ls_set_application_information_item(-2, /* Magic value. */
+ asn,
+ *display_name_key,
+ display_name,
+ NULL);
+
+ return (err == noErr) ? 0 : -1;
+}
View
16 src/unix/darwin.c
@@ -261,8 +261,20 @@ char** uv_setup_args(int argc, char** argv) {
uv_err_t uv_set_process_title(const char* title) {
- /* TODO implement me */
- return uv__new_artificial_error(UV_ENOSYS);
+ int uv__set_process_title(const char*);
+
+ if (process_title != NULL)
+ free(process_title);
+
+ process_title = strdup(title);
+
+ if (process_title == NULL)
+ return uv__new_artificial_error(UV_ENOMEM);
+
+ if (uv__set_process_title(title))
+ return uv__new_artificial_error(UV_ENOSYS);
+
+ return uv_ok_;
}
View
8 uv.gyp
@@ -158,10 +158,16 @@
],
}],
[ 'OS=="mac"', {
- 'sources': [ 'src/unix/darwin.c', 'src/unix/fsevents.c' ],
+ 'sources': [
+ 'src/unix/darwin.c',
+ 'src/unix/fsevents.c',
+ 'src/unix/darwin-proctitle.m',
+ ],
'link_settings': {
'libraries': [
+ '$(SDKROOT)/System/Library/Frameworks/Foundation.framework',
'$(SDKROOT)/System/Library/Frameworks/CoreServices.framework',
+ '$(SDKROOT)/System/Library/Frameworks/ApplicationServices.framework',
],
},
'defines': [

0 comments on commit 14eb8b0

Please sign in to comment.