Browse Source

Split CarlaProcessUtils in hpp/cpp, add ScopedAbortCatcher

Signed-off-by: falkTX <falktx@falktx.com>
tags/v2.2.0-RC1
falkTX 5 years ago
parent
commit
90d69c02dd
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
7 changed files with 146 additions and 41 deletions
  1. +1
    -0
      source/backend/CarlaStandalone.cpp
  2. +1
    -0
      source/backend/engine/CarlaEngineNative.cpp
  3. +4
    -0
      source/backend/utils/System.cpp
  4. +1
    -0
      source/bridges-ui/CarlaBridgeFormat.cpp
  5. +1
    -0
      source/libjack/libjack.cpp
  6. +105
    -0
      source/utils/CarlaProcessUtils.cpp
  7. +33
    -41
      source/utils/CarlaProcessUtils.hpp

+ 1
- 0
source/backend/CarlaStandalone.cpp View File

@@ -2295,6 +2295,7 @@ const char* carla_get_host_osc_url_udp(CarlaHostHandle handle)
#include "CarlaMacUtils.cpp"
#include "CarlaPatchbayUtils.cpp"
#include "CarlaPipeUtils.cpp"
#include "CarlaProcessUtils.cpp"
#include "CarlaStateUtils.cpp"

// --------------------------------------------------------------------------------------------------------------------

+ 1
- 0
source/backend/engine/CarlaEngineNative.cpp View File

@@ -2751,6 +2751,7 @@ CARLA_BACKEND_END_NAMESPACE
#include "CarlaMacUtils.cpp"
#include "CarlaPatchbayUtils.cpp"
#include "CarlaPipeUtils.cpp"
#include "CarlaProcessUtils.cpp"
#include "CarlaStateUtils.cpp"

#endif


+ 4
- 0
source/backend/utils/System.cpp View File

@@ -39,3 +39,7 @@ void carla_set_process_name(const char* name)
}

// -------------------------------------------------------------------------------------------------------------------

#include "CarlaProcessUtils.cpp"

// -------------------------------------------------------------------------------------------------------------------

+ 1
- 0
source/bridges-ui/CarlaBridgeFormat.cpp View File

@@ -344,5 +344,6 @@ void CarlaBridgeFormat::exec(const bool showUI)
CARLA_BRIDGE_UI_END_NAMESPACE

#include "CarlaPipeUtils.cpp"
#include "CarlaProcessUtils.cpp"

// ---------------------------------------------------------------------

+ 1
- 0
source/libjack/libjack.cpp View File

@@ -1395,6 +1395,7 @@ pthread_t jack_client_thread_id(jack_client_t* client)

#include "jackbridge/JackBridge2.cpp"
#include "CarlaBridgeUtils.cpp"
#include "CarlaProcessUtils.cpp"

// ---------------------------------------------------------------------------------------------------------------------
// TODO


+ 105
- 0
source/utils/CarlaProcessUtils.cpp View File

@@ -0,0 +1,105 @@
/*
* Carla process utils
* Copyright (C) 2019-2020 Filipe Coelho <falktx@falktx.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* For a full copy of the GNU General Public License see the doc/GPL.txt file.
*/

#include "CarlaProcessUtils.hpp"

#ifdef CARLA_OS_LINUX
# include <sys/prctl.h>
#endif

// --------------------------------------------------------------------------------------------------------------------
// process functions

void carla_setProcessName(const char* const name) noexcept
{
CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);

#ifdef CARLA_OS_LINUX
::prctl(PR_SET_NAME, name, 0, 0, 0);
#endif
}

void carla_terminateProcessOnParentExit(const bool kill) noexcept
{
#ifdef CARLA_OS_LINUX
//
::prctl(PR_SET_PDEATHSIG, kill ? SIGKILL : SIGTERM);
// TODO, osx version too, see https://stackoverflow.com/questions/284325/how-to-make-child-process-die-after-parent-exits
#endif

// maybe unused
return; (void)kill;
}

// --------------------------------------------------------------------------------------------------------------------
// process utility classes

ScopedAbortCatcher::ScopedAbortCatcher()
{
s_triggered = false;
#ifndef CARLA_OS_WIN
s_oldsig = ::setjmp(s_env) == 0
? std::signal(SIGABRT, sig_handler)
: nullptr;
#endif
}

ScopedAbortCatcher::~ScopedAbortCatcher()
{
#ifndef CARLA_OS_WIN
if (! s_triggered)
std::signal(SIGABRT, s_oldsig);
#endif
}

bool ScopedAbortCatcher::s_triggered = false;

#ifndef CARLA_OS_WIN
jmp_buf ScopedAbortCatcher::s_env;
sig_t ScopedAbortCatcher::s_oldsig;

void ScopedAbortCatcher::sig_handler(const int signum)
{
CARLA_SAFE_ASSERT_INT2_RETURN(signum == SIGABRT, signum, SIGABRT,);

s_triggered = true;
std::signal(signum, s_oldsig);
std::longjmp(s_env, 1);
}
#endif

// --------------------------------------------------------------------------------------------------------------------

CarlaSignalRestorer::CarlaSignalRestorer()
{
#ifndef CARLA_OS_WIN
carla_zeroStructs(sigs, 16);

for (int i=0; i < 16; ++i)
::sigaction(i+1, nullptr, &sigs[i]);
#endif
}

CarlaSignalRestorer::~CarlaSignalRestorer()
{
#ifndef CARLA_OS_WIN
for (int i=0; i < 16; ++i)
::sigaction(i+1, &sigs[i], nullptr);
#endif
}

// --------------------------------------------------------------------------------------------------------------------

+ 33
- 41
source/utils/CarlaProcessUtils.hpp View File

@@ -1,6 +1,6 @@
/*
* Carla process utils
* Copyright (C) 2019 Filipe Coelho <falktx@falktx.com>
* Copyright (C) 2019-2020 Filipe Coelho <falktx@falktx.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@@ -21,10 +21,8 @@
#include "CarlaUtils.hpp"

#ifndef CARLA_OS_WIN
# include <signal.h>
#endif
#ifdef CARLA_OS_LINUX
# include <sys/prctl.h>
# include <csignal>
# include <csetjmp>
#endif

// --------------------------------------------------------------------------------------------------------------------
@@ -33,54 +31,48 @@
/*
* Set current process name.
*/
static inline
void carla_setProcessName(const char* const name) noexcept
{
CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);

#ifdef CARLA_OS_LINUX
::prctl(PR_SET_NAME, name, 0, 0, 0);
#endif
}
void carla_setProcessName(const char* const name) noexcept;

/*
* Set flag to automatically terminate ourselves if parent process dies.
*/
static inline
void carla_terminateProcessOnParentExit(const bool kill) noexcept
{
#ifdef CARLA_OS_LINUX
//
::prctl(PR_SET_PDEATHSIG, kill ? SIGKILL : SIGTERM);
// TODO, osx version too, see https://stackoverflow.com/questions/284325/how-to-make-child-process-die-after-parent-exits
#endif

// maybe unused
return; (void)kill;
}
void carla_terminateProcessOnParentExit(const bool kill) noexcept;

// --------------------------------------------------------------------------------------------------------------------
// process functions
// process utility classes

class CarlaSignalRestorer {
/*
* Catches SIGABRT for a function scope.
*/
class ScopedAbortCatcher {
public:
CarlaSignalRestorer()
{
#ifndef CARLA_OS_WIN
carla_zeroStructs(sigs, 16);
ScopedAbortCatcher();
~ScopedAbortCatcher();

for (int i=0; i < 16; ++i)
::sigaction(i+1, nullptr, &sigs[i]);
#endif
}
inline bool wasTriggered() const
{
return s_triggered;
}

~CarlaSignalRestorer()
{
private:
static bool s_triggered;
#ifndef CARLA_OS_WIN
for (int i=0; i < 16; ++i)
::sigaction(i+1, &sigs[i], nullptr);
static jmp_buf s_env;
static sig_t s_oldsig;
static void sig_handler(const int signum);
#endif
}

CARLA_DECLARE_NON_COPY_CLASS(ScopedAbortCatcher)
CARLA_PREVENT_HEAP_ALLOCATION
};

/*
* Store and restore all signal handlers for a function scope.
*/
class CarlaSignalRestorer {
public:
CarlaSignalRestorer();
~CarlaSignalRestorer();

private:
#ifndef CARLA_OS_WIN


Loading…
Cancel
Save