diff --git a/demo-content/demo.py b/demo-content/demo.py index 4dcaa66..ac3c1cc 100644 --- a/demo-content/demo.py +++ b/demo-content/demo.py @@ -70,6 +70,8 @@ def stop_injection_process(folder): injector = open(folder + "/pid", "r") pid = injector.read() os.kill(int(pid), signal.SIGTERM) + else: + print("The PID file has already been removed, app is not running") def parse_injection_input(): ''' diff --git a/src/linux/daemonize.cc b/src/linux/daemonize.cc index 362d251..603503d 100644 --- a/src/linux/daemonize.cc +++ b/src/linux/daemonize.cc @@ -45,7 +45,8 @@ daemonize::daemonize(const std::string &log_dir) { open("/dev/null", O_WRONLY); pid_ = getpid(); - std::ofstream pid_file(log_dir + "/pid"); + pid_file_ = log_dir + "/pid"; + std::ofstream pid_file(pid_file_); pid_file << std::to_string(pid_); } @@ -53,6 +54,7 @@ daemonize::~daemonize() { close(0); close(1); close(2); + remove(pid_file_.c_str()); } void daemonize::wait_indefinitely() { diff --git a/src/linux/daemonize.h b/src/linux/daemonize.h index 8848139..e14d0f4 100644 --- a/src/linux/daemonize.h +++ b/src/linux/daemonize.h @@ -34,6 +34,7 @@ class daemonize { private: void parent_exit(pid_t pid); pid_t pid_{-1}; + std::string pid_file_{}; sem_t semaphore_{}; }; diff --git a/src/main.cc b/src/main.cc index 0cce33f..fb7ee54 100755 --- a/src/main.cc +++ b/src/main.cc @@ -13,6 +13,7 @@ #include "utils/async_accumulator.h" #include "utils/commands_handler.h" +#include #include using namespace dolbyio::comms::sample; @@ -23,9 +24,8 @@ using namespace dolbyio::comms::sample; #include #include -// Global unique_ptr for linux daemonization, because I was too lazy -// to read sigaction and setting context for handler ;) -std::unique_ptr daemonize_ptr{nullptr}; +// Global pointer for linux daemonization +std::shared_ptr daemonize_ptr{nullptr}; void signal_handler(int sig) { if (sig == SIGTERM && daemonize_ptr) @@ -120,7 +120,21 @@ int main(int argc, char** argv) { // Run blocking loop #if defined(__linux__) + // If the conference has ended then we should unblock the loop. + // Not ideal because you could techinically race here with signal + // handler, but for our purposes it should be ok. + std::weak_ptr weak_daemoize_ptr{daemonize_ptr}; + sdk->conference() + .add_event_handler( + [weak_daemoize_ptr]( + const dolbyio::comms::conference_status_updated& status) { + if (status.is_ended() && !weak_daemoize_ptr.expired()) { + weak_daemoize_ptr.lock()->unblock_indefinte_wait(); + } + }) + .on_error([](auto&&) {}); daemonize_ptr->wait_indefinitely(); + daemonize_ptr.reset(); #else while (!quit) { command_handler.print_interactive_options();