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.

116 lines
2.8KB

  1. /*
  2. * Carla process utils
  3. * Copyright (C) 2019-2020 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_HAIKU
  24. typedef __sighandler_t sig_t;
  25. #endif
  26. #ifndef CARLA_OS_WIN
  27. # include <csignal>
  28. # include <csetjmp>
  29. #endif
  30. // --------------------------------------------------------------------------------------------------------------------
  31. // process functions
  32. /*
  33. * Set current process name.
  34. */
  35. static inline
  36. void carla_setProcessName(const char* const name) noexcept
  37. {
  38. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  39. #ifdef CARLA_OS_LINUX
  40. ::prctl(PR_SET_NAME, name, 0, 0, 0);
  41. #endif
  42. }
  43. /*
  44. * Set flag to automatically terminate ourselves if parent process dies.
  45. */
  46. static inline
  47. void carla_terminateProcessOnParentExit(const bool kill) noexcept
  48. {
  49. #ifdef CARLA_OS_LINUX
  50. //
  51. ::prctl(PR_SET_PDEATHSIG, kill ? SIGKILL : SIGTERM);
  52. // TODO, osx version too, see https://stackoverflow.com/questions/284325/how-to-make-child-process-die-after-parent-exits
  53. #endif
  54. // maybe unused
  55. return; (void)kill;
  56. }
  57. // --------------------------------------------------------------------------------------------------------------------
  58. // process utility classes
  59. /*
  60. * Catches SIGABRT for a function scope.
  61. */
  62. class ScopedAbortCatcher {
  63. public:
  64. ScopedAbortCatcher();
  65. ~ScopedAbortCatcher();
  66. inline bool wasTriggered() const
  67. {
  68. return s_triggered;
  69. }
  70. private:
  71. static bool s_triggered;
  72. #ifndef CARLA_OS_WIN
  73. static jmp_buf s_env;
  74. static sig_t s_oldsig;
  75. static void sig_handler(const int signum);
  76. #endif
  77. CARLA_DECLARE_NON_COPY_CLASS(ScopedAbortCatcher)
  78. CARLA_PREVENT_HEAP_ALLOCATION
  79. };
  80. /*
  81. * Store and restore all signal handlers for a function scope.
  82. */
  83. class CarlaSignalRestorer {
  84. public:
  85. CarlaSignalRestorer();
  86. ~CarlaSignalRestorer();
  87. private:
  88. #ifndef CARLA_OS_WIN
  89. struct ::sigaction sigs[16];
  90. #endif
  91. CARLA_DECLARE_NON_COPY_CLASS(CarlaSignalRestorer)
  92. CARLA_PREVENT_HEAP_ALLOCATION
  93. };
  94. // --------------------------------------------------------------------------------------------------------------------
  95. #endif // CARLA_PROCESS_UTILS_HPP_INCLUDED