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"
24 # include <sys/prctl.h>
28 # include <dispatch/dispatch.h>
32 typedef __sighandler_t sig_t
;
40 // --------------------------------------------------------------------------------------------------------------------
44 * Set current process name.
47 void carla_setProcessName(const char* const name
) noexcept
49 CARLA_SAFE_ASSERT_RETURN(name
!= nullptr && name
[0] != '\0',);
52 ::prctl(PR_SET_NAME
, name
, 0, 0, 0);
58 void carla_macOS_proc_exit_handler_kill(void*)
60 carla_stdout("Carla bridge parent has died, killing ourselves now");
61 ::kill(::getpid(), SIGKILL
);
64 void carla_macOS_proc_exit_handler_term(void*)
66 carla_stdout("Carla bridge parent has died, terminating ourselves now");
67 ::kill(::getpid(), SIGTERM
);
72 * Set flag to automatically terminate ourselves if parent process dies.
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
,
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
);
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
{
104 ScopedAbortCatcher();
105 ~ScopedAbortCatcher();
107 inline bool wasTriggered() const
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
124 * Store and restore all signal handlers for a function scope.
126 class CarlaSignalRestorer
{
128 CarlaSignalRestorer();
129 ~CarlaSignalRestorer();
132 #if !(defined(CARLA_OS_WASM) || defined(CARLA_OS_WIN))
133 struct ::sigaction sigs
[16];
136 CARLA_DECLARE_NON_COPYABLE(CarlaSignalRestorer
)
137 CARLA_PREVENT_HEAP_ALLOCATION
140 // --------------------------------------------------------------------------------------------------------------------
142 #endif // CARLA_PROCESS_UTILS_HPP_INCLUDED