From 79f0873e9bd88d9c1157373a71c2e58fcca21290 Mon Sep 17 00:00:00 2001 From: Vincent Le Date: Wed, 20 Oct 2021 20:17:44 -0700 Subject: [PATCH 1/2] [CLI] Auto attach SHM_UI iff it exists - Removes need to specify "attach" arg --- tests/cli/shm_ui.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/tests/cli/shm_ui.c b/tests/cli/shm_ui.c index a57d0418..d4623e2b 100644 --- a/tests/cli/shm_ui.c +++ b/tests/cli/shm_ui.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "../../logger/logger.h" #include "../../runtime_util/runtime_util.h" @@ -104,10 +105,11 @@ const int DATA_VAL_COL = COMMAND_VAL_COL + VALUE_WIDTH; // */ int DEVICE_WIN_IS_BLANK = 0; -// This is a global flag that is TRUE (1) when shm_ui is attaching to -// existing shared memory, and FALSE (0) when shm_ui is creating/destroying -// to shared memory and attaching to that newly created shared memory. -uint8_t attach = 0; +// Initalized to FALSE -- This process will create shm +// and destroy it when this process exits. +// Set to TRUE at the start of main() if shm already exists when shm_ui starts +// in which case we should not create/destroy shm +bool shm_already_exists = false; // The header of DEVICE_WIN; Useful for clearing and redrawing DEVICE_WIN #define DEVICE_WIN_HEADER "~~~~~~~~~~~~~~~~~~~~~~~~~~~~DEVICE INFORMATION~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" @@ -521,8 +523,7 @@ void display_device(uint32_t catalog, dev_id_t dev_ids[MAX_DEVICES], int shm_idx // Sending SIGINT to the process will stop shared memory void clean_up(int signum) { - // only stop shared memory if we did not attach - if (!attach) { + if (!shm_already_exists) { stop_shm(); } endwin(); @@ -534,17 +535,13 @@ int main(int argc, char** argv) { signal(SIGINT, clean_up); logger_init(TEST); - // If the argument "attach" is specified, then set the global variable - if (argc == 2 && strcmp(argv[1], "attach") == 0) { - attach = 1; - } - - // Start shm if we aren't attaching to existing shared memory - if (!attach) { - start_shm(); - sleep(1); // Allow shm to initialize + // If SHM exists, simply attach to it. Otherwise, create SHM + if (shm_exists()) { + shm_init(); + shm_already_exists = true; } else { - shm_init(); // If just attaching, init shm to have access to sems and shm blocks + start_shm(); + sleep(1); // Allow shm to initialize } // Start UI @@ -629,7 +626,7 @@ int main(int argc, char** argv) { } // Properly clean up - if (!attach) { + if (!shm_already_exists) { stop_shm(); } endwin(); From b96e4ef8a35d19d8fac9dbe90d545497043e9ab9 Mon Sep 17 00:00:00 2001 From: Vincent Le Date: Wed, 20 Oct 2021 21:47:29 -0700 Subject: [PATCH 2/2] [FORMAT] Format shm_ui.c --- tests/cli/shm_ui.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/cli/shm_ui.c b/tests/cli/shm_ui.c index d4623e2b..29beab7c 100644 --- a/tests/cli/shm_ui.c +++ b/tests/cli/shm_ui.c @@ -24,9 +24,9 @@ * sudo apt-get install libncurses5-dev libncursesw5-dev */ #include +#include #include #include -#include #include "../../logger/logger.h" #include "../../runtime_util/runtime_util.h" @@ -541,7 +541,7 @@ int main(int argc, char** argv) { shm_already_exists = true; } else { start_shm(); - sleep(1); // Allow shm to initialize + sleep(1); // Allow shm to initialize } // Start UI