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.

CarlaProcessUtils.hpp 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Carla process utils
  3. * Copyright (C) 2019 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 <signal.h>
  22. #endif
  23. #ifdef CARLA_OS_LINUX
  24. # include <sys/prctl.h>
  25. #endif
  26. // --------------------------------------------------------------------------------------------------------------------
  27. // process functions
  28. /*
  29. * Set current process name.
  30. */
  31. static inline
  32. void carla_setProcessName(const char* const name) noexcept
  33. {
  34. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  35. #ifdef CARLA_OS_LINUX
  36. ::prctl(PR_SET_NAME, name, 0, 0, 0);
  37. #endif
  38. }
  39. /*
  40. * Set flag to automatically terminate ourselves if parent process dies.
  41. */
  42. static inline
  43. void carla_terminateProcessOnParentExit(const bool kill) noexcept
  44. {
  45. #ifdef CARLA_OS_LINUX
  46. //
  47. ::prctl(PR_SET_PDEATHSIG, kill ? SIGKILL : SIGTERM);
  48. // TODO, osx version too, see https://stackoverflow.com/questions/284325/how-to-make-child-process-die-after-parent-exits
  49. #endif
  50. // maybe unused
  51. return; (void)kill;
  52. }
  53. // --------------------------------------------------------------------------------------------------------------------
  54. // process functions
  55. class CarlaSignalRestorer {
  56. public:
  57. CarlaSignalRestorer()
  58. {
  59. #ifndef CARLA_OS_WIN
  60. carla_zeroStructs(sigs, 16);
  61. for (int i=0; i < 16; ++i)
  62. ::sigaction(i+1, nullptr, &sigs[i]);
  63. #endif
  64. }
  65. ~CarlaSignalRestorer()
  66. {
  67. #ifndef CARLA_OS_WIN
  68. for (int i=0; i < 16; ++i)
  69. ::sigaction(i+1, &sigs[i], nullptr);
  70. #endif
  71. }
  72. private:
  73. #ifndef CARLA_OS_WIN
  74. struct ::sigaction sigs[16];
  75. #endif
  76. CARLA_DECLARE_NON_COPY_CLASS(CarlaSignalRestorer)
  77. CARLA_PREVENT_HEAP_ALLOCATION
  78. };
  79. // --------------------------------------------------------------------------------------------------------------------
  80. #endif // CARLA_PROCESS_UTILS_HPP_INCLUDED