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.

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