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.

106 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. #include "CarlaProcessUtils.hpp"
  18. #ifdef CARLA_OS_LINUX
  19. # include <sys/prctl.h>
  20. #endif
  21. // --------------------------------------------------------------------------------------------------------------------
  22. // process functions
  23. void carla_setProcessName(const char* const name) noexcept
  24. {
  25. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  26. #ifdef CARLA_OS_LINUX
  27. ::prctl(PR_SET_NAME, name, 0, 0, 0);
  28. #endif
  29. }
  30. void carla_terminateProcessOnParentExit(const bool kill) noexcept
  31. {
  32. #ifdef CARLA_OS_LINUX
  33. //
  34. ::prctl(PR_SET_PDEATHSIG, kill ? SIGKILL : SIGTERM);
  35. // TODO, osx version too, see https://stackoverflow.com/questions/284325/how-to-make-child-process-die-after-parent-exits
  36. #endif
  37. // maybe unused
  38. return; (void)kill;
  39. }
  40. // --------------------------------------------------------------------------------------------------------------------
  41. // process utility classes
  42. ScopedAbortCatcher::ScopedAbortCatcher()
  43. {
  44. s_triggered = false;
  45. #ifndef CARLA_OS_WIN
  46. s_oldsig = ::setjmp(s_env) == 0
  47. ? std::signal(SIGABRT, sig_handler)
  48. : nullptr;
  49. #endif
  50. }
  51. ScopedAbortCatcher::~ScopedAbortCatcher()
  52. {
  53. #ifndef CARLA_OS_WIN
  54. if (! s_triggered)
  55. std::signal(SIGABRT, s_oldsig);
  56. #endif
  57. }
  58. bool ScopedAbortCatcher::s_triggered = false;
  59. #ifndef CARLA_OS_WIN
  60. jmp_buf ScopedAbortCatcher::s_env;
  61. sig_t ScopedAbortCatcher::s_oldsig;
  62. void ScopedAbortCatcher::sig_handler(const int signum)
  63. {
  64. CARLA_SAFE_ASSERT_INT2_RETURN(signum == SIGABRT, signum, SIGABRT,);
  65. s_triggered = true;
  66. std::signal(signum, s_oldsig);
  67. std::longjmp(s_env, 1);
  68. }
  69. #endif
  70. // --------------------------------------------------------------------------------------------------------------------
  71. CarlaSignalRestorer::CarlaSignalRestorer()
  72. {
  73. #ifndef CARLA_OS_WIN
  74. carla_zeroStructs(sigs, 16);
  75. for (int i=0; i < 16; ++i)
  76. ::sigaction(i+1, nullptr, &sigs[i]);
  77. #endif
  78. }
  79. CarlaSignalRestorer::~CarlaSignalRestorer()
  80. {
  81. #ifndef CARLA_OS_WIN
  82. for (int i=0; i < 16; ++i)
  83. ::sigaction(i+1, &sigs[i], nullptr);
  84. #endif
  85. }
  86. // --------------------------------------------------------------------------------------------------------------------