Blame src/unix/darwin-proctitle.c

Packit b5b901
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
Packit b5b901
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit b5b901
 * of this software and associated documentation files (the "Software"), to
Packit b5b901
 * deal in the Software without restriction, including without limitation the
Packit b5b901
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit b5b901
 * sell copies of the Software, and to permit persons to whom the Software is
Packit b5b901
 * furnished to do so, subject to the following conditions:
Packit b5b901
 *
Packit b5b901
 * The above copyright notice and this permission notice shall be included in
Packit b5b901
 * all copies or substantial portions of the Software.
Packit b5b901
 *
Packit b5b901
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit b5b901
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit b5b901
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit b5b901
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit b5b901
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit b5b901
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit b5b901
 * IN THE SOFTWARE.
Packit b5b901
 */
Packit b5b901
Packit b5b901
#include "uv.h"
Packit b5b901
#include "internal.h"
Packit b5b901
Packit b5b901
#include <dlfcn.h>
Packit b5b901
#include <errno.h>
Packit Service e08953
#include <pthread.h>
Packit b5b901
#include <stdlib.h>
Packit b5b901
#include <string.h>
Packit b5b901
Packit b5b901
#include <TargetConditionals.h>
Packit b5b901
Packit b5b901
#if !TARGET_OS_IPHONE
Packit Service e08953
#include "darwin-stub.h"
Packit b5b901
#endif
Packit b5b901
Packit b5b901
Packit b5b901
static int uv__pthread_setname_np(const char* name) {
Packit b5b901
  char namebuf[64];  /* MAXTHREADNAMESIZE */
Packit b5b901
  int err;
Packit b5b901
Packit b5b901
  strncpy(namebuf, name, sizeof(namebuf) - 1);
Packit b5b901
  namebuf[sizeof(namebuf) - 1] = '\0';
Packit b5b901
Packit Service e08953
  err = pthread_setname_np(namebuf);
Packit b5b901
  if (err)
Packit b5b901
    return UV__ERR(err);
Packit b5b901
Packit b5b901
  return 0;
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
int uv__set_process_title(const char* title) {
Packit b5b901
#if TARGET_OS_IPHONE
Packit b5b901
  return uv__pthread_setname_np(title);
Packit b5b901
#else
Packit b5b901
  CFStringRef (*pCFStringCreateWithCString)(CFAllocatorRef,
Packit b5b901
                                            const char*,
Packit b5b901
                                            CFStringEncoding);
Packit b5b901
  CFBundleRef (*pCFBundleGetBundleWithIdentifier)(CFStringRef);
Packit b5b901
  void *(*pCFBundleGetDataPointerForName)(CFBundleRef, CFStringRef);
Packit b5b901
  void *(*pCFBundleGetFunctionPointerForName)(CFBundleRef, CFStringRef);
Packit b5b901
  CFTypeRef (*pLSGetCurrentApplicationASN)(void);
Packit b5b901
  OSStatus (*pLSSetApplicationInformationItem)(int,
Packit b5b901
                                               CFTypeRef,
Packit b5b901
                                               CFStringRef,
Packit b5b901
                                               CFStringRef,
Packit b5b901
                                               CFDictionaryRef*);
Packit b5b901
  void* application_services_handle;
Packit b5b901
  void* core_foundation_handle;
Packit b5b901
  CFBundleRef launch_services_bundle;
Packit b5b901
  CFStringRef* display_name_key;
Packit b5b901
  CFDictionaryRef (*pCFBundleGetInfoDictionary)(CFBundleRef);
Packit b5b901
  CFBundleRef (*pCFBundleGetMainBundle)(void);
Packit b5b901
  CFDictionaryRef (*pLSApplicationCheckIn)(int, CFDictionaryRef);
Packit b5b901
  void (*pLSSetApplicationLaunchServicesServerConnectionStatus)(uint64_t,
Packit b5b901
                                                                void*);
Packit b5b901
  CFTypeRef asn;
Packit b5b901
  int err;
Packit b5b901
Packit b5b901
  err = UV_ENOENT;
Packit b5b901
  application_services_handle = dlopen("/System/Library/Frameworks/"
Packit b5b901
                                       "ApplicationServices.framework/"
Packit b5b901
                                       "Versions/A/ApplicationServices",
Packit b5b901
                                       RTLD_LAZY | RTLD_LOCAL);
Packit b5b901
  core_foundation_handle = dlopen("/System/Library/Frameworks/"
Packit b5b901
                                  "CoreFoundation.framework/"
Packit b5b901
                                  "Versions/A/CoreFoundation",
Packit b5b901
                                  RTLD_LAZY | RTLD_LOCAL);
Packit b5b901
Packit b5b901
  if (application_services_handle == NULL || core_foundation_handle == NULL)
Packit b5b901
    goto out;
Packit b5b901
Packit b5b901
  *(void **)(&pCFStringCreateWithCString) =
Packit b5b901
      dlsym(core_foundation_handle, "CFStringCreateWithCString");
Packit b5b901
  *(void **)(&pCFBundleGetBundleWithIdentifier) =
Packit b5b901
      dlsym(core_foundation_handle, "CFBundleGetBundleWithIdentifier");
Packit b5b901
  *(void **)(&pCFBundleGetDataPointerForName) =
Packit b5b901
      dlsym(core_foundation_handle, "CFBundleGetDataPointerForName");
Packit b5b901
  *(void **)(&pCFBundleGetFunctionPointerForName) =
Packit b5b901
      dlsym(core_foundation_handle, "CFBundleGetFunctionPointerForName");
Packit b5b901
Packit b5b901
  if (pCFStringCreateWithCString == NULL ||
Packit b5b901
      pCFBundleGetBundleWithIdentifier == NULL ||
Packit b5b901
      pCFBundleGetDataPointerForName == NULL ||
Packit b5b901
      pCFBundleGetFunctionPointerForName == NULL) {
Packit b5b901
    goto out;
Packit b5b901
  }
Packit b5b901
Packit b5b901
#define S(s) pCFStringCreateWithCString(NULL, (s), kCFStringEncodingUTF8)
Packit b5b901
Packit b5b901
  launch_services_bundle =
Packit b5b901
      pCFBundleGetBundleWithIdentifier(S("com.apple.LaunchServices"));
Packit b5b901
Packit b5b901
  if (launch_services_bundle == NULL)
Packit b5b901
    goto out;
Packit b5b901
Packit b5b901
  *(void **)(&pLSGetCurrentApplicationASN) =
Packit b5b901
      pCFBundleGetFunctionPointerForName(launch_services_bundle,
Packit b5b901
                                         S("_LSGetCurrentApplicationASN"));
Packit b5b901
Packit b5b901
  if (pLSGetCurrentApplicationASN == NULL)
Packit b5b901
    goto out;
Packit b5b901
Packit b5b901
  *(void **)(&pLSSetApplicationInformationItem) =
Packit b5b901
      pCFBundleGetFunctionPointerForName(launch_services_bundle,
Packit b5b901
                                         S("_LSSetApplicationInformationItem"));
Packit b5b901
Packit b5b901
  if (pLSSetApplicationInformationItem == NULL)
Packit b5b901
    goto out;
Packit b5b901
Packit b5b901
  display_name_key = pCFBundleGetDataPointerForName(launch_services_bundle,
Packit b5b901
                                                    S("_kLSDisplayNameKey"));
Packit b5b901
Packit b5b901
  if (display_name_key == NULL || *display_name_key == NULL)
Packit b5b901
    goto out;
Packit b5b901
Packit b5b901
  *(void **)(&pCFBundleGetInfoDictionary) = dlsym(core_foundation_handle,
Packit b5b901
                                     "CFBundleGetInfoDictionary");
Packit b5b901
  *(void **)(&pCFBundleGetMainBundle) = dlsym(core_foundation_handle,
Packit b5b901
                                 "CFBundleGetMainBundle");
Packit b5b901
  if (pCFBundleGetInfoDictionary == NULL || pCFBundleGetMainBundle == NULL)
Packit b5b901
    goto out;
Packit b5b901
Packit b5b901
  *(void **)(&pLSApplicationCheckIn) = pCFBundleGetFunctionPointerForName(
Packit b5b901
      launch_services_bundle,
Packit b5b901
      S("_LSApplicationCheckIn"));
Packit Service e08953
Packit Service e08953
  if (pLSApplicationCheckIn == NULL)
Packit Service e08953
    goto out;
Packit Service e08953
Packit b5b901
  *(void **)(&pLSSetApplicationLaunchServicesServerConnectionStatus) =
Packit b5b901
      pCFBundleGetFunctionPointerForName(
Packit b5b901
          launch_services_bundle,
Packit b5b901
          S("_LSSetApplicationLaunchServicesServerConnectionStatus"));
Packit b5b901
Packit Service e08953
  if (pLSSetApplicationLaunchServicesServerConnectionStatus == NULL)
Packit b5b901
    goto out;
Packit b5b901
Packit b5b901
  pLSSetApplicationLaunchServicesServerConnectionStatus(0, NULL);
Packit b5b901
Packit b5b901
  /* Check into process manager?! */
Packit b5b901
  pLSApplicationCheckIn(-2,
Packit b5b901
                        pCFBundleGetInfoDictionary(pCFBundleGetMainBundle()));
Packit b5b901
Packit b5b901
  asn = pLSGetCurrentApplicationASN();
Packit b5b901
Packit Service e08953
  err = UV_EBUSY;
Packit Service e08953
  if (asn == NULL)
Packit Service e08953
    goto out;
Packit Service e08953
Packit b5b901
  err = UV_EINVAL;
Packit b5b901
  if (pLSSetApplicationInformationItem(-2,  /* Magic value. */
Packit b5b901
                                       asn,
Packit b5b901
                                       *display_name_key,
Packit b5b901
                                       S(title),
Packit b5b901
                                       NULL) != noErr) {
Packit b5b901
    goto out;
Packit b5b901
  }
Packit b5b901
Packit b5b901
  uv__pthread_setname_np(title);  /* Don't care if it fails. */
Packit b5b901
  err = 0;
Packit b5b901
Packit b5b901
out:
Packit b5b901
  if (core_foundation_handle != NULL)
Packit b5b901
    dlclose(core_foundation_handle);
Packit b5b901
Packit b5b901
  if (application_services_handle != NULL)
Packit b5b901
    dlclose(application_services_handle);
Packit b5b901
Packit b5b901
  return err;
Packit b5b901
#endif  /* !TARGET_OS_IPHONE */
Packit b5b901
}