Path: blob/master/Utilities/cmlibuv/src/unix/darwin-proctitle.c
3156 views
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.1* Permission is hereby granted, free of charge, to any person obtaining a copy2* of this software and associated documentation files (the "Software"), to3* deal in the Software without restriction, including without limitation the4* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or5* sell copies of the Software, and to permit persons to whom the Software is6* furnished to do so, subject to the following conditions:7*8* The above copyright notice and this permission notice shall be included in9* all copies or substantial portions of the Software.10*11* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR12* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,13* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE14* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER15* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING16* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS17* IN THE SOFTWARE.18*/1920#include "uv.h"21#include "internal.h"2223#include <dlfcn.h>24#include <errno.h>25#include <pthread.h>26#include <stdlib.h>27#include <string.h>2829#include <TargetConditionals.h>3031#if !TARGET_OS_IPHONE32#include "darwin-stub.h"33#endif343536static int uv__pthread_setname_np(const char* name) {37char namebuf[64]; /* MAXTHREADNAMESIZE */38int err;3940strncpy(namebuf, name, sizeof(namebuf) - 1);41namebuf[sizeof(namebuf) - 1] = '\0';4243err = pthread_setname_np(namebuf);44if (err)45return UV__ERR(err);4647return 0;48}495051int uv__set_process_title(const char* title) {52#if TARGET_OS_IPHONE53return uv__pthread_setname_np(title);54#else55CFStringRef (*pCFStringCreateWithCString)(CFAllocatorRef,56const char*,57CFStringEncoding);58CFBundleRef (*pCFBundleGetBundleWithIdentifier)(CFStringRef);59void *(*pCFBundleGetDataPointerForName)(CFBundleRef, CFStringRef);60void *(*pCFBundleGetFunctionPointerForName)(CFBundleRef, CFStringRef);61CFTypeRef (*pLSGetCurrentApplicationASN)(void);62OSStatus (*pLSSetApplicationInformationItem)(int,63CFTypeRef,64CFStringRef,65CFStringRef,66CFDictionaryRef*);67void* application_services_handle;68void* core_foundation_handle;69CFBundleRef launch_services_bundle;70CFStringRef* display_name_key;71CFDictionaryRef (*pCFBundleGetInfoDictionary)(CFBundleRef);72CFBundleRef (*pCFBundleGetMainBundle)(void);73CFDictionaryRef (*pLSApplicationCheckIn)(int, CFDictionaryRef);74void (*pLSSetApplicationLaunchServicesServerConnectionStatus)(uint64_t,75void*);76CFTypeRef asn;77int err;7879err = UV_ENOENT;80application_services_handle = dlopen("/System/Library/Frameworks/"81"ApplicationServices.framework/"82"Versions/A/ApplicationServices",83RTLD_LAZY | RTLD_LOCAL);84core_foundation_handle = dlopen("/System/Library/Frameworks/"85"CoreFoundation.framework/"86"Versions/A/CoreFoundation",87RTLD_LAZY | RTLD_LOCAL);8889if (application_services_handle == NULL || core_foundation_handle == NULL)90goto out;9192*(void **)(&pCFStringCreateWithCString) =93dlsym(core_foundation_handle, "CFStringCreateWithCString");94*(void **)(&pCFBundleGetBundleWithIdentifier) =95dlsym(core_foundation_handle, "CFBundleGetBundleWithIdentifier");96*(void **)(&pCFBundleGetDataPointerForName) =97dlsym(core_foundation_handle, "CFBundleGetDataPointerForName");98*(void **)(&pCFBundleGetFunctionPointerForName) =99dlsym(core_foundation_handle, "CFBundleGetFunctionPointerForName");100101if (pCFStringCreateWithCString == NULL ||102pCFBundleGetBundleWithIdentifier == NULL ||103pCFBundleGetDataPointerForName == NULL ||104pCFBundleGetFunctionPointerForName == NULL) {105goto out;106}107108#define S(s) pCFStringCreateWithCString(NULL, (s), kCFStringEncodingUTF8)109110launch_services_bundle =111pCFBundleGetBundleWithIdentifier(S("com.apple.LaunchServices"));112113if (launch_services_bundle == NULL)114goto out;115116*(void **)(&pLSGetCurrentApplicationASN) =117pCFBundleGetFunctionPointerForName(launch_services_bundle,118S("_LSGetCurrentApplicationASN"));119120if (pLSGetCurrentApplicationASN == NULL)121goto out;122123*(void **)(&pLSSetApplicationInformationItem) =124pCFBundleGetFunctionPointerForName(launch_services_bundle,125S("_LSSetApplicationInformationItem"));126127if (pLSSetApplicationInformationItem == NULL)128goto out;129130display_name_key = pCFBundleGetDataPointerForName(launch_services_bundle,131S("_kLSDisplayNameKey"));132133if (display_name_key == NULL || *display_name_key == NULL)134goto out;135136*(void **)(&pCFBundleGetInfoDictionary) = dlsym(core_foundation_handle,137"CFBundleGetInfoDictionary");138*(void **)(&pCFBundleGetMainBundle) = dlsym(core_foundation_handle,139"CFBundleGetMainBundle");140if (pCFBundleGetInfoDictionary == NULL || pCFBundleGetMainBundle == NULL)141goto out;142143*(void **)(&pLSApplicationCheckIn) = pCFBundleGetFunctionPointerForName(144launch_services_bundle,145S("_LSApplicationCheckIn"));146147if (pLSApplicationCheckIn == NULL)148goto out;149150*(void **)(&pLSSetApplicationLaunchServicesServerConnectionStatus) =151pCFBundleGetFunctionPointerForName(152launch_services_bundle,153S("_LSSetApplicationLaunchServicesServerConnectionStatus"));154155if (pLSSetApplicationLaunchServicesServerConnectionStatus == NULL)156goto out;157158pLSSetApplicationLaunchServicesServerConnectionStatus(0, NULL);159160/* Check into process manager?! */161pLSApplicationCheckIn(-2,162pCFBundleGetInfoDictionary(pCFBundleGetMainBundle()));163164asn = pLSGetCurrentApplicationASN();165166err = UV_EBUSY;167if (asn == NULL)168goto out;169170err = UV_EINVAL;171if (pLSSetApplicationInformationItem(-2, /* Magic value. */172asn,173*display_name_key,174S(title),175NULL) != noErr) {176goto out;177}178179uv__pthread_setname_np(title); /* Don't care if it fails. */180err = 0;181182out:183if (core_foundation_handle != NULL)184dlclose(core_foundation_handle);185186if (application_services_handle != NULL)187dlclose(application_services_handle);188189return err;190#endif /* !TARGET_OS_IPHONE */191}192193194