From 15e5b9d17a34d7ea72e356e9db061b370ba763e6 Mon Sep 17 00:00:00 2001 From: falkTX Date: Sat, 21 Aug 2021 23:14:22 +0100 Subject: [PATCH] Automatically close bridges if main Carla dies, macOS edition --- source/utils/CarlaMacUtils.cpp | 2 +- source/utils/CarlaProcessUtils.hpp | 35 ++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/source/utils/CarlaMacUtils.cpp b/source/utils/CarlaMacUtils.cpp index ecba554e2..c7391b0ee 100644 --- a/source/utils/CarlaMacUtils.cpp +++ b/source/utils/CarlaMacUtils.cpp @@ -31,7 +31,7 @@ CARLA_BACKEND_START_NAMESPACE void initStandaloneApplication() { - [NSApplication sharedApplication]; + [[NSApplication sharedApplication] retain]; [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; [NSApp activateIgnoringOtherApps:YES]; } diff --git a/source/utils/CarlaProcessUtils.hpp b/source/utils/CarlaProcessUtils.hpp index 1d8f9160f..1c09c480e 100644 --- a/source/utils/CarlaProcessUtils.hpp +++ b/source/utils/CarlaProcessUtils.hpp @@ -1,6 +1,6 @@ /* * Carla process utils - * Copyright (C) 2019-2020 Filipe Coelho + * Copyright (C) 2019-2021 Filipe Coelho * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -24,6 +24,10 @@ # include #endif +#ifdef CARLA_OS_MAC +# include +#endif + #ifdef CARLA_OS_HAIKU typedef __sighandler_t sig_t; #endif @@ -49,16 +53,39 @@ void carla_setProcessName(const char* const name) noexcept #endif } +#ifdef CARLA_OS_MAC +static inline +void carla_macOS_proc_exit_handler_kill(void*) +{ + carla_stdout("Carla bridge parent has died, killing ourselves now"); + ::kill(::getpid(), SIGKILL); +} +static inline +void carla_macOS_proc_exit_handler_term(void*) +{ + carla_stdout("Carla bridge parent has died, terminating ourselves now"); + ::kill(::getpid(), SIGTERM); +} +#endif + /* * Set flag to automatically terminate ourselves if parent process dies. */ static inline void carla_terminateProcessOnParentExit(const bool kill) noexcept { -#ifdef CARLA_OS_LINUX - // +#if defined(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 +#elif defined(CARLA_OS_MAC) + const dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, + ::getppid(), + DISPATCH_PROC_EXIT, + nullptr); + + dispatch_source_set_event_handler_f(source, kill ? carla_macOS_proc_exit_handler_kill + : carla_macOS_proc_exit_handler_term); + + dispatch_resume(source); #endif // maybe unused