Advertisement
franklinglzhou

OS X Processs name changer

Apr 24th, 2023
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. void SetProcessName(CFStringRef process_name) {
  5. if (!process_name || CFStringGetLength(process_name) == 0) {
  6. NOTREACHED() << "SetProcessName given bad name.";
  7. return;
  8. }
  9.  
  10. if (![NSThread isMainThread]) {
  11. NOTREACHED() << "Should only set process name from main thread.";
  12. return;
  13. }
  14.  
  15. // Warning: here be dragons! This is SPI reverse-engineered from WebKit's
  16. // plugin host, and could break at any time (although realistically it's only
  17. // likely to break in a new major release).
  18. // When 10.7 is available, check that this still works, and update this
  19. // comment for 10.8.
  20.  
  21. // Private CFType used in these LaunchServices calls.
  22. typedef CFTypeRef PrivateLSASN;
  23. typedef PrivateLSASN (*LSGetCurrentApplicationASNType)();
  24. typedef OSStatus (*LSSetApplicationInformationItemType)(int, PrivateLSASN,
  25. CFStringRef,
  26. CFStringRef,
  27. CFDictionaryRef*);
  28.  
  29. static LSGetCurrentApplicationASNType ls_get_current_application_asn_func =
  30. NULL;
  31. static LSSetApplicationInformationItemType
  32. ls_set_application_information_item_func = NULL;
  33. static CFStringRef ls_display_name_key = NULL;
  34.  
  35. static bool did_symbol_lookup = false;
  36. if (!did_symbol_lookup) {
  37. did_symbol_lookup = true;
  38. CFBundleRef launch_services_bundle =
  39. CFBundleGetBundleWithIdentifier(CFSTR("com.apple.LaunchServices"));
  40. if (!launch_services_bundle) {
  41. LOG(ERROR) << "Failed to look up LaunchServices bundle";
  42. return;
  43. }
  44.  
  45. ls_get_current_application_asn_func =
  46. reinterpret_cast<LSGetCurrentApplicationASNType>(
  47. CFBundleGetFunctionPointerForName(
  48. launch_services_bundle, CFSTR("_LSGetCurrentApplicationASN")));
  49. if (!ls_get_current_application_asn_func)
  50. LOG(ERROR) << "Could not find _LSGetCurrentApplicationASN";
  51.  
  52. ls_set_application_information_item_func =
  53. reinterpret_cast<LSSetApplicationInformationItemType>(
  54. CFBundleGetFunctionPointerForName(
  55. launch_services_bundle,
  56. CFSTR("_LSSetApplicationInformationItem")));
  57. if (!ls_set_application_information_item_func)
  58. LOG(ERROR) << "Could not find _LSSetApplicationInformationItem";
  59.  
  60. CFStringRef* key_pointer = reinterpret_cast<CFStringRef*>(
  61. CFBundleGetDataPointerForName(launch_services_bundle,
  62. CFSTR("_kLSDisplayNameKey")));
  63. ls_display_name_key = key_pointer ? *key_pointer : NULL;
  64. if (!ls_display_name_key)
  65. LOG(ERROR) << "Could not find _kLSDisplayNameKey";
  66.  
  67. // Internally, this call relies on the Mach ports that are started up by the
  68. // Carbon Process Manager. In debug builds this usually happens due to how
  69. // the logging layers are started up; but in release, it isn't started in as
  70. // much of a defined order. So if the symbols had to be loaded, go ahead
  71. // and force a call to make sure the manager has been initialized and hence
  72. // the ports are opened.
  73. ProcessSerialNumber psn;
  74. GetCurrentProcess(&psn);
  75. }
  76. if (!ls_get_current_application_asn_func ||
  77. !ls_set_application_information_item_func ||
  78. !ls_display_name_key) {
  79. return;
  80. }
  81.  
  82. PrivateLSASN asn = ls_get_current_application_asn_func();
  83. // Constant used by WebKit; what exactly it means is unknown.
  84. const int magic_session_constant = -2;
  85. OSErr err =
  86. ls_set_application_information_item_func(magic_session_constant, asn,
  87. ls_display_name_key,
  88. process_name,
  89. NULL /* optional out param */);
  90. LOG_IF(ERROR, err) << "Call to set process name failed, err " << err;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement