Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.7KB

  1. /*
  2. * Carla process utils
  3. * Copyright (C) 2019-2022 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_PROCESS_UTILS_HPP_INCLUDED
  18. #define CARLA_PROCESS_UTILS_HPP_INCLUDED
  19. #include "CarlaUtils.hpp"
  20. #ifdef CARLA_OS_LINUX
  21. # include <sys/prctl.h>
  22. #endif
  23. #ifdef CARLA_OS_MAC
  24. # include <dispatch/dispatch.h>
  25. #endif
  26. #ifdef CARLA_OS_HAIKU
  27. typedef __sighandler_t sig_t;
  28. #endif
  29. #ifndef CARLA_OS_WIN
  30. # include <csignal>
  31. # include <csetjmp>
  32. #endif
  33. // --------------------------------------------------------------------------------------------------------------------
  34. // process functions
  35. /*
  36. * Set current process name.
  37. */
  38. static inline
  39. void carla_setProcessName(const char* const name) noexcept
  40. {
  41. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  42. #ifdef CARLA_OS_LINUX
  43. ::prctl(PR_SET_NAME, name, 0, 0, 0);
  44. #endif
  45. }
  46. #ifdef CARLA_OS_MAC
  47. static inline
  48. void carla_macOS_proc_exit_handler_kill(void*)
  49. {
  50. carla_stdout("Carla bridge parent has died, killing ourselves now");
  51. ::kill(::getpid(), SIGKILL);
  52. }
  53. static inline
  54. void carla_macOS_proc_exit_handler_term(void*)
  55. {
  56. carla_stdout("Carla bridge parent has died, terminating ourselves now");
  57. ::kill(::getpid(), SIGTERM);
  58. }
  59. #endif
  60. /*
  61. * Set flag to automatically terminate ourselves if parent process dies.
  62. */
  63. static inline
  64. void carla_terminateProcessOnParentExit(const bool kill) noexcept
  65. {
  66. #if defined(CARLA_OS_LINUX)
  67. ::prctl(PR_SET_PDEATHSIG, kill ? SIGKILL : SIGTERM);
  68. #elif defined(CARLA_OS_MAC)
  69. const dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC,
  70. ::getppid(),
  71. DISPATCH_PROC_EXIT,
  72. nullptr);
  73. dispatch_source_set_event_handler_f(source, kill ? carla_macOS_proc_exit_handler_kill
  74. : carla_macOS_proc_exit_handler_term);
  75. dispatch_resume(source);
  76. #endif
  77. // maybe unused
  78. return; (void)kill;
  79. }
  80. // --------------------------------------------------------------------------------------------------------------------
  81. // process utility classes
  82. #if !(defined(CARLA_OS_WASM) || defined(CARLA_OS_WIN))
  83. /*
  84. * Catches SIGABRT for a function scope.
  85. */
  86. class ScopedAbortCatcher {
  87. public:
  88. ScopedAbortCatcher();
  89. ~ScopedAbortCatcher();
  90. inline bool wasTriggered() const
  91. {
  92. return s_triggered;
  93. }
  94. private:
  95. static bool s_triggered;
  96. static jmp_buf s_env;
  97. static sig_t s_oldsig;
  98. static void sig_handler(const int signum);
  99. CARLA_DECLARE_NON_COPYABLE(ScopedAbortCatcher)
  100. CARLA_PREVENT_HEAP_ALLOCATION
  101. };
  102. #endif
  103. /*
  104. * Store and restore all signal handlers for a function scope.
  105. */
  106. class CarlaSignalRestorer {
  107. public:
  108. CarlaSignalRestorer();
  109. ~CarlaSignalRestorer();
  110. private:
  111. #if !(defined(CARLA_OS_WASM) || defined(CARLA_OS_WIN))
  112. struct ::sigaction sigs[16];
  113. #endif
  114. CARLA_DECLARE_NON_COPYABLE(CarlaSignalRestorer)
  115. CARLA_PREVENT_HEAP_ALLOCATION
  116. };
  117. // --------------------------------------------------------------------------------------------------------------------
  118. #endif // CARLA_PROCESS_UTILS_HPP_INCLUDED