Fix last commit
[carla.git] / source / utils / CarlaProcessUtils.hpp
blobdec2fdfe874424ba875805bdc7f1f0a1c8a08422
1 /*
2 * Carla process utils
3 * Copyright (C) 2019-2022 Filipe Coelho <falktx@falktx.com>
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.
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.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #ifndef CARLA_PROCESS_UTILS_HPP_INCLUDED
19 #define CARLA_PROCESS_UTILS_HPP_INCLUDED
21 #include "CarlaUtils.hpp"
23 #ifdef CARLA_OS_LINUX
24 # include <sys/prctl.h>
25 #endif
27 #ifdef CARLA_OS_MAC
28 # include <dispatch/dispatch.h>
29 #endif
31 #ifdef CARLA_OS_HAIKU
32 typedef __sighandler_t sig_t;
33 #endif
35 #ifndef CARLA_OS_WIN
36 # include <csignal>
37 # include <csetjmp>
38 #endif
40 // --------------------------------------------------------------------------------------------------------------------
41 // process functions
44 * Set current process name.
46 static inline
47 void carla_setProcessName(const char* const name) noexcept
49 CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
51 #ifdef CARLA_OS_LINUX
52 ::prctl(PR_SET_NAME, name, 0, 0, 0);
53 #endif
56 #ifdef CARLA_OS_MAC
57 static inline
58 void carla_macOS_proc_exit_handler_kill(void*)
60 carla_stdout("Carla bridge parent has died, killing ourselves now");
61 ::kill(::getpid(), SIGKILL);
63 static inline
64 void carla_macOS_proc_exit_handler_term(void*)
66 carla_stdout("Carla bridge parent has died, terminating ourselves now");
67 ::kill(::getpid(), SIGTERM);
69 #endif
72 * Set flag to automatically terminate ourselves if parent process dies.
74 static inline
75 void carla_terminateProcessOnParentExit(const bool kill) noexcept
77 #if defined(CARLA_OS_LINUX)
78 ::prctl(PR_SET_PDEATHSIG, kill ? SIGKILL : SIGTERM);
79 #elif defined(CARLA_OS_MAC)
80 const dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC,
81 ::getppid(),
82 DISPATCH_PROC_EXIT,
83 nullptr);
85 dispatch_source_set_event_handler_f(source, kill ? carla_macOS_proc_exit_handler_kill
86 : carla_macOS_proc_exit_handler_term);
88 dispatch_resume(source);
89 #endif
91 // maybe unused
92 return; (void)kill;
95 // --------------------------------------------------------------------------------------------------------------------
96 // process utility classes
98 #if !(defined(CARLA_OS_WASM) || defined(CARLA_OS_WIN))
100 * Catches SIGABRT for a function scope.
102 class ScopedAbortCatcher {
103 public:
104 ScopedAbortCatcher();
105 ~ScopedAbortCatcher();
107 inline bool wasTriggered() const
109 return s_triggered;
112 private:
113 static bool s_triggered;
114 static jmp_buf s_env;
115 static sig_t s_oldsig;
116 static void sig_handler(const int signum);
118 CARLA_DECLARE_NON_COPYABLE(ScopedAbortCatcher)
119 CARLA_PREVENT_HEAP_ALLOCATION
121 #endif
124 * Store and restore all signal handlers for a function scope.
126 class CarlaSignalRestorer {
127 public:
128 CarlaSignalRestorer();
129 ~CarlaSignalRestorer();
131 private:
132 #if !(defined(CARLA_OS_WASM) || defined(CARLA_OS_WIN))
133 struct ::sigaction sigs[16];
134 #endif
136 CARLA_DECLARE_NON_COPYABLE(CarlaSignalRestorer)
137 CARLA_PREVENT_HEAP_ALLOCATION
140 // --------------------------------------------------------------------------------------------------------------------
142 #endif // CARLA_PROCESS_UTILS_HPP_INCLUDED