From 3d8e22a56c767ad1eb36df68c735bdfcd9e8c474 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Sat, 19 Jan 2019 10:57:36 -0800 Subject: [PATCH 1/9] Add an "/FTL-settings" shared memory block Signed-off-by: Mcat12 Signed-off-by: DL6ER Conflicts: shmem.c --- FTL.h | 4 ++++ shmem.c | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/FTL.h b/FTL.h index 5623e0c2d..6d174e9bd 100644 --- a/FTL.h +++ b/FTL.h @@ -221,6 +221,10 @@ typedef struct { char **domains; } whitelistStruct; +typedef struct { + int version; +} ShmSettings; + // Prepare timers, used mainly for debugging purposes #define NUMTIMERS 5 diff --git a/shmem.c b/shmem.c index c49e057a9..e2bb33b5f 100644 --- a/shmem.c +++ b/shmem.c @@ -11,6 +11,9 @@ #include "FTL.h" #include "shmem.h" +/// The version of shared memory used +#define SHARED_MEMORY_VERSION 1 + /// The name of the shared memory. Use this when connecting to the shared memory. #define SHARED_LOCK_NAME "/FTL-lock" #define SHARED_STRINGS_NAME "/FTL-strings" @@ -20,6 +23,7 @@ #define SHARED_QUERIES_NAME "/FTL-queries" #define SHARED_FORWARDED_NAME "/FTL-forwarded" #define SHARED_OVERTIME_NAME "/FTL-overTime" +#define SHARED_SETTINGS_NAME "/FTL-settings" /// The pointer in shared memory to the shared string buffer static SharedMemory shm_lock = { 0 }; @@ -30,6 +34,7 @@ static SharedMemory shm_clients = { 0 }; static SharedMemory shm_queries = { 0 }; static SharedMemory shm_forwarded = { 0 }; static SharedMemory shm_overTime = { 0 }; +static SharedMemory shm_settings = { 0 }; typedef struct { pthread_mutex_t lock; @@ -189,6 +194,14 @@ bool init_shmem(void) counters->overTime_MAX = (int) size; initOverTime(); + /****************************** shared settings struct ******************************/ + // Try to create shared memory object + shm_settings = create_shm(SHARED_SETTINGS_NAME, sizeof(ShmSettings)); + if(shm_settings.ptr == NULL) + return false; + ShmSettings *settings = (ShmSettings*)shm_settings.ptr; + settings->version = SHARED_MEMORY_VERSION; + return true; } @@ -205,6 +218,7 @@ void destroy_shmem(void) delete_shm(&shm_queries); delete_shm(&shm_forwarded); delete_shm(&shm_overTime); + delete_shm(&shm_settings); } SharedMemory create_shm(char *name, size_t size) From fda4027bf55e1ac0ea8d8ba0c6ecf8d1b3e38428 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 21 Feb 2019 17:34:23 +0100 Subject: [PATCH 2/9] Remap dynamically sized shared memory objects in locker when we detect that at least one of the objects has changed Signed-off-by: DL6ER --- FTL.h | 1 + shmem.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/FTL.h b/FTL.h index 6d174e9bd..af70f33b5 100644 --- a/FTL.h +++ b/FTL.h @@ -223,6 +223,7 @@ typedef struct { typedef struct { int version; + unsigned int global_shm_counter; } ShmSettings; // Prepare timers, used mainly for debugging purposes diff --git a/shmem.c b/shmem.c index e2bb33b5f..cd098f07f 100644 --- a/shmem.c +++ b/shmem.c @@ -12,7 +12,7 @@ #include "shmem.h" /// The version of shared memory used -#define SHARED_MEMORY_VERSION 1 +#define SHARED_MEMORY_VERSION 5 /// The name of the shared memory. Use this when connecting to the shared memory. #define SHARED_LOCK_NAME "/FTL-lock" @@ -41,9 +41,11 @@ typedef struct { bool waitingForLock; } ShmLock; static ShmLock *shmLock = NULL; +static ShmSettings *shmSettings = NULL; static int pagesize; static unsigned int next_pos = 0; +static unsigned int local_shm_counter = 0; unsigned long long addstr(const char *str) { @@ -104,12 +106,32 @@ pthread_mutex_t create_mutex() { return lock; } +void remap_shm(void) +{ + // Remap shared object pointers which might have changed + realloc_shm(&shm_queries, 0); + realloc_shm(&shm_domains, 0); + realloc_shm(&shm_clients, 0); + realloc_shm(&shm_forwarded, 0); + + // Update local counter to reflect that we absorbed this change + local_shm_counter = shmSettings->global_shm_counter; +} + void _lock_shm(const char* function, const int line, const char * file) { // Signal that FTL is waiting for a lock shmLock->waitingForLock = true; if(debug) logg("Waiting for lock in %s() (%s:%i)", function, file, line); + // Check if this process needs to remap the shared memory objects + if(shmSettings != NULL && + local_shm_counter != shmSettings->global_shm_counter) + { + logg("Remapping shared memory for current process %u %u", local_shm_counter, shmSettings->global_shm_counter); + remap_shm(); + } + int result = pthread_mutex_lock(&shmLock->lock); if(debug) logg("Obtained lock for %s() (%s:%i)", function, file, line); @@ -199,8 +221,9 @@ bool init_shmem(void) shm_settings = create_shm(SHARED_SETTINGS_NAME, sizeof(ShmSettings)); if(shm_settings.ptr == NULL) return false; - ShmSettings *settings = (ShmSettings*)shm_settings.ptr; - settings->version = SHARED_MEMORY_VERSION; + shmSettings = (ShmSettings*)shm_settings.ptr; + shmSettings->version = SHARED_MEMORY_VERSION; + shmSettings->global_shm_counter = 0; return true; } @@ -325,8 +348,10 @@ void *enlarge_shmem_struct(char type) return sharedMemory->ptr; } -bool realloc_shm(SharedMemory *sharedMemory, size_t size) { - logg("Resizing \"%s\" from %zu to %zu", sharedMemory->name, sharedMemory->size, size); +bool realloc_shm(SharedMemory *sharedMemory, size_t size) +{ + if(size > 0) + logg("Resizing \"%s\" from %zu to %zu", sharedMemory->name, sharedMemory->size, size); int result = munmap(sharedMemory->ptr, sharedMemory->size); if(result != 0) @@ -341,12 +366,27 @@ bool realloc_shm(SharedMemory *sharedMemory, size_t size) { exit(EXIT_FAILURE); } - // Resize shard memory object to requested size - result = ftruncate(fd, size); - if(result == -1) { - logg("FATAL: realloc_shm(): ftruncate(%i, %zu): Failed to resize \"%s\": %s", - fd, size, sharedMemory->name, strerror(errno)); - exit(EXIT_FAILURE); + // Resize shard memory object if requested + // If not, we only remap a shared memory object which might have changed + // in another process. This happens when pihole-FTL forks due to incoming + // TCP requests. + if(size > 0) + { + result = ftruncate(fd, size); + if(result == -1) { + logg("FATAL: realloc_shm(): ftruncate(%i, %zu): Failed to resize \"%s\": %s", + fd, size, sharedMemory->name, strerror(errno)); + exit(EXIT_FAILURE); + } + + // Update shm counters to indicate that at least one shared memory object changed + shmSettings->global_shm_counter++; + local_shm_counter++; + } + else + { + // If we are not resizing, we copy sharedMemory->size to pass this to mmap() in the next step + size = sharedMemory->size; } // void *new_ptr = mremap(sharedMemory->ptr, sharedMemory->size, size, MREMAP_MAYMOVE); From e33b9bcf27324d04302efaa4f5009999bb690f07 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 21 Feb 2019 17:48:16 +0100 Subject: [PATCH 3/9] Also remap strings object Signed-off-by: DL6ER --- shmem.c | 1 + 1 file changed, 1 insertion(+) diff --git a/shmem.c b/shmem.c index cd098f07f..f7a8688c2 100644 --- a/shmem.c +++ b/shmem.c @@ -110,6 +110,7 @@ void remap_shm(void) { // Remap shared object pointers which might have changed realloc_shm(&shm_queries, 0); + realloc_shm(&shm_strings, 0); realloc_shm(&shm_domains, 0); realloc_shm(&shm_clients, 0); realloc_shm(&shm_forwarded, 0); From 8f82ef3041006746a085c681a2fe2c2f0c92e30e Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 21 Feb 2019 18:06:42 +0100 Subject: [PATCH 4/9] Use counters to transport information about actual size of shared memory objects into all running processes, even if they are forks Signed-off-by: DL6ER --- FTL.h | 2 +- shmem.c | 35 +++++++++++++++-------------------- shmem.h | 3 ++- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/FTL.h b/FTL.h index af70f33b5..60b9f8f0b 100644 --- a/FTL.h +++ b/FTL.h @@ -130,7 +130,7 @@ typedef struct { int forwarded_MAX; int clients_MAX; int domains_MAX; - int overTime_MAX; + int strings_MAX; int gravity; int gravity_conf; int querytype[TYPE_MAX-1]; diff --git a/shmem.c b/shmem.c index f7a8688c2..f8a403ef2 100644 --- a/shmem.c +++ b/shmem.c @@ -64,7 +64,7 @@ unsigned long long addstr(const char *str) size_t required_size = next_pos + len + 1; // Need to cast to long long because size_t calculations cannot be negative if((long long)required_size-(long long)shm_strings.size > 0 && - !realloc_shm(&shm_strings, shm_strings.size + pagesize)) + !realloc_shm(&shm_strings, shm_strings.size + pagesize, true)) return 0; // Copy the C string pointed by str into the shared string buffer @@ -109,11 +109,11 @@ pthread_mutex_t create_mutex() { void remap_shm(void) { // Remap shared object pointers which might have changed - realloc_shm(&shm_queries, 0); - realloc_shm(&shm_strings, 0); - realloc_shm(&shm_domains, 0); - realloc_shm(&shm_clients, 0); - realloc_shm(&shm_forwarded, 0); + realloc_shm(&shm_queries, counters->queries_MAX*sizeof(queriesDataStruct), false); + realloc_shm(&shm_domains, counters->domains_MAX*sizeof(domainsDataStruct), false); + realloc_shm(&shm_clients, counters->clients_MAX*sizeof(clientsDataStruct), false); + realloc_shm(&shm_forwarded, counters->forwarded_MAX*sizeof(forwardedDataStruct), false); + realloc_shm(&shm_strings, counters->strings_MAX*sizeof(char), false); // Update local counter to reflect that we absorbed this change local_shm_counter = shmSettings->global_shm_counter; @@ -172,19 +172,20 @@ bool init_shmem(void) shmLock->lock = create_mutex(); shmLock->waitingForLock = false; + /****************************** shared counters struct ******************************/ + // Try to create shared memory object + shm_counters = create_shm(SHARED_COUNTERS_NAME, sizeof(countersStruct)); + counters = (countersStruct*)shm_counters.ptr; + /****************************** shared strings buffer ******************************/ // Try to create shared memory object shm_strings = create_shm(SHARED_STRINGS_NAME, pagesize); + counters->strings_MAX = pagesize; // Initialize shared string object with an empty string at position zero ((char*)shm_strings.ptr)[0] = '\0'; next_pos = 1; - /****************************** shared counters struct ******************************/ - // Try to create shared memory object - shm_counters = create_shm(SHARED_COUNTERS_NAME, sizeof(countersStruct)); - counters = (countersStruct*)shm_counters.ptr; - /****************************** shared domains struct ******************************/ // Try to create shared memory object shm_domains = create_shm(SHARED_DOMAINS_NAME, pagesize*sizeof(domainsDataStruct)); @@ -214,7 +215,6 @@ bool init_shmem(void) // Try to create shared memory object shm_overTime = create_shm(SHARED_OVERTIME_NAME, size); overTime = (overTimeDataStruct*)shm_overTime.ptr; - counters->overTime_MAX = (int) size; initOverTime(); /****************************** shared settings struct ******************************/ @@ -341,7 +341,7 @@ void *enlarge_shmem_struct(char type) } // Reallocate enough space for 4096 instances of requested object - realloc_shm(sharedMemory, sharedMemory->size + pagesize*sizeofobj); + realloc_shm(sharedMemory, sharedMemory->size + pagesize*sizeofobj, true); // Add allocated memory to corresponding counter *counter += pagesize; @@ -349,7 +349,7 @@ void *enlarge_shmem_struct(char type) return sharedMemory->ptr; } -bool realloc_shm(SharedMemory *sharedMemory, size_t size) +bool realloc_shm(SharedMemory *sharedMemory, size_t size, bool resize) { if(size > 0) logg("Resizing \"%s\" from %zu to %zu", sharedMemory->name, sharedMemory->size, size); @@ -371,7 +371,7 @@ bool realloc_shm(SharedMemory *sharedMemory, size_t size) // If not, we only remap a shared memory object which might have changed // in another process. This happens when pihole-FTL forks due to incoming // TCP requests. - if(size > 0) + if(resize) { result = ftruncate(fd, size); if(result == -1) { @@ -384,11 +384,6 @@ bool realloc_shm(SharedMemory *sharedMemory, size_t size) shmSettings->global_shm_counter++; local_shm_counter++; } - else - { - // If we are not resizing, we copy sharedMemory->size to pass this to mmap() in the next step - size = sharedMemory->size; - } // void *new_ptr = mremap(sharedMemory->ptr, sharedMemory->size, size, MREMAP_MAYMOVE); void *new_ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); diff --git a/shmem.h b/shmem.h index 9f9b1f902..d6d793f8a 100644 --- a/shmem.h +++ b/shmem.h @@ -33,8 +33,9 @@ SharedMemory create_shm(char *name, size_t size); /// /// \param sharedMemory the shared memory struct /// \param size the new size +/// \param resize whether the object should be resized or only remapped /// \return if reallocation was successful -bool realloc_shm(SharedMemory *sharedMemory, size_t size); +bool realloc_shm(SharedMemory *sharedMemory, size_t size, bool resize); /// Disconnect from shared memory. If there are no other connections to shared memory, it will be deleted. /// From 6ca1c5d0d479817247ffe2d3d9a507aa14aa7503 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 21 Feb 2019 22:40:42 +0100 Subject: [PATCH 5/9] Store string_MAX value when resizing + use mremap instead of munmap/mmap Signed-off-by: DL6ER --- shmem.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/shmem.c b/shmem.c index f8a403ef2..8446030fd 100644 --- a/shmem.c +++ b/shmem.c @@ -67,6 +67,10 @@ unsigned long long addstr(const char *str) !realloc_shm(&shm_strings, shm_strings.size + pagesize, true)) return 0; + // Store new string buffer size in corresponding counters entry + // for re-using when we need to re-map shared memory objects + counters->strings_MAX = shm_strings.size; + // Copy the C string pointed by str into the shared string buffer strncpy(&((char*)shm_strings.ptr)[next_pos], str, len); ((char*)shm_strings.ptr)[next_pos + len] = '\0'; @@ -113,7 +117,7 @@ void remap_shm(void) realloc_shm(&shm_domains, counters->domains_MAX*sizeof(domainsDataStruct), false); realloc_shm(&shm_clients, counters->clients_MAX*sizeof(clientsDataStruct), false); realloc_shm(&shm_forwarded, counters->forwarded_MAX*sizeof(forwardedDataStruct), false); - realloc_shm(&shm_strings, counters->strings_MAX*sizeof(char), false); + realloc_shm(&shm_strings, counters->strings_MAX, false); // Update local counter to reflect that we absorbed this change local_shm_counter = shmSettings->global_shm_counter; @@ -125,6 +129,10 @@ void _lock_shm(const char* function, const int line, const char * file) { if(debug) logg("Waiting for lock in %s() (%s:%i)", function, file, line); + int result = pthread_mutex_lock(&shmLock->lock); + + if(debug) logg("Obtained lock for %s() (%s:%i)", function, file, line); + // Check if this process needs to remap the shared memory objects if(shmSettings != NULL && local_shm_counter != shmSettings->global_shm_counter) @@ -133,10 +141,6 @@ void _lock_shm(const char* function, const int line, const char * file) { remap_shm(); } - int result = pthread_mutex_lock(&shmLock->lock); - - if(debug) logg("Obtained lock for %s() (%s:%i)", function, file, line); - // Turn off the waiting for lock signal to notify everyone who was // deferring to FTL that they can jump in the lock queue. shmLock->waitingForLock = false; @@ -351,12 +355,7 @@ void *enlarge_shmem_struct(char type) bool realloc_shm(SharedMemory *sharedMemory, size_t size, bool resize) { - if(size > 0) - logg("Resizing \"%s\" from %zu to %zu", sharedMemory->name, sharedMemory->size, size); - - int result = munmap(sharedMemory->ptr, sharedMemory->size); - if(result != 0) - logg("realloc_shm(): munmap(%p, %zu) failed: %s", sharedMemory->ptr, sharedMemory->size, strerror(errno)); + logg("%s \"%s\" from %zu to %zu", resize ? "Remapping" : "Resizing", sharedMemory->name, sharedMemory->size, size); // Open shared memory object int fd = shm_open(sharedMemory->name, O_RDWR, S_IRUSR | S_IWUSR); @@ -373,7 +372,7 @@ bool realloc_shm(SharedMemory *sharedMemory, size_t size, bool resize) // TCP requests. if(resize) { - result = ftruncate(fd, size); + int result = ftruncate(fd, size); if(result == -1) { logg("FATAL: realloc_shm(): ftruncate(%i, %zu): Failed to resize \"%s\": %s", fd, size, sharedMemory->name, strerror(errno)); @@ -385,8 +384,7 @@ bool realloc_shm(SharedMemory *sharedMemory, size_t size, bool resize) local_shm_counter++; } -// void *new_ptr = mremap(sharedMemory->ptr, sharedMemory->size, size, MREMAP_MAYMOVE); - void *new_ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + void *new_ptr = mremap(sharedMemory->ptr, sharedMemory->size, size, MREMAP_MAYMOVE); if(new_ptr == MAP_FAILED) { logg("FATAL: realloc_shm(): mremap(%p, %zu, %zu, MREMAP_MAYMOVE): Failed to reallocate \"%s\" (%i): %s", From fabfd38edbf57e1fc83a8885c46ea2e47aadc476 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 21 Feb 2019 22:57:04 +0100 Subject: [PATCH 6/9] Update global pointers after mremap() might have set the pointers to a new virtual address Signed-off-by: DL6ER --- shmem.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/shmem.c b/shmem.c index 8446030fd..3a49c4896 100644 --- a/shmem.c +++ b/shmem.c @@ -114,10 +114,15 @@ void remap_shm(void) { // Remap shared object pointers which might have changed realloc_shm(&shm_queries, counters->queries_MAX*sizeof(queriesDataStruct), false); + queries = (queriesDataStruct*)shm_queries.ptr; realloc_shm(&shm_domains, counters->domains_MAX*sizeof(domainsDataStruct), false); + domains = (domainsDataStruct*)shm_domains.ptr; realloc_shm(&shm_clients, counters->clients_MAX*sizeof(clientsDataStruct), false); + clients = (clientsDataStruct*)shm_clients.ptr; realloc_shm(&shm_forwarded, counters->forwarded_MAX*sizeof(forwardedDataStruct), false); + forwarded = (forwardedDataStruct*)shm_forwarded.ptr; realloc_shm(&shm_strings, counters->strings_MAX, false); + // strings are not exposed by a global pointer // Update local counter to reflect that we absorbed this change local_shm_counter = shmSettings->global_shm_counter; @@ -137,7 +142,8 @@ void _lock_shm(const char* function, const int line, const char * file) { if(shmSettings != NULL && local_shm_counter != shmSettings->global_shm_counter) { - logg("Remapping shared memory for current process %u %u", local_shm_counter, shmSettings->global_shm_counter); + if(debug) logg("Remapping shared memory for current process %u %u", + local_shm_counter, shmSettings->global_shm_counter); remap_shm(); } @@ -355,7 +361,14 @@ void *enlarge_shmem_struct(char type) bool realloc_shm(SharedMemory *sharedMemory, size_t size, bool resize) { - logg("%s \"%s\" from %zu to %zu", resize ? "Remapping" : "Resizing", sharedMemory->name, sharedMemory->size, size); + // Check if we can skip this routine as nothing is to be done + // when an object is not to be resized and its size didn't + // change elsewhere + if(!resize && size == sharedMemory->size) + return true; + + // Log that we are doing something here + logg("%s \"%s\" from %zu to %zu", resize ? "Resizing" : "Remapping", sharedMemory->name, sharedMemory->size, size); // Open shared memory object int fd = shm_open(sharedMemory->name, O_RDWR, S_IRUSR | S_IWUSR); @@ -394,7 +407,7 @@ bool realloc_shm(SharedMemory *sharedMemory, size_t size, bool resize) } // Close shared memory object file descriptor as it is no longer - // needed after having called mmap() + // needed after having called ftruncate() close(fd); sharedMemory->ptr = new_ptr; From cefcbe61ed801d02d3820571bb58635d73977ec8 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 21 Feb 2019 23:19:35 +0100 Subject: [PATCH 7/9] Only open shared memory object if we also want to truncate it Signed-off-by: DL6ER --- shmem.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/shmem.c b/shmem.c index 3a49c4896..59a4b580d 100644 --- a/shmem.c +++ b/shmem.c @@ -370,21 +370,23 @@ bool realloc_shm(SharedMemory *sharedMemory, size_t size, bool resize) // Log that we are doing something here logg("%s \"%s\" from %zu to %zu", resize ? "Resizing" : "Remapping", sharedMemory->name, sharedMemory->size, size); - // Open shared memory object - int fd = shm_open(sharedMemory->name, O_RDWR, S_IRUSR | S_IWUSR); - if(fd == -1) - { - logg("FATAL: realloc_shm(): Failed to open shared memory object \"%s\": %s", - sharedMemory->name, strerror(errno)); - exit(EXIT_FAILURE); - } - // Resize shard memory object if requested // If not, we only remap a shared memory object which might have changed // in another process. This happens when pihole-FTL forks due to incoming // TCP requests. + int fd = -1; if(resize) { + // Open shared memory object + fd = shm_open(sharedMemory->name, O_RDWR, S_IRUSR | S_IWUSR); + if(fd == -1) + { + logg("FATAL: realloc_shm(): Failed to open shared memory object \"%s\": %s", + sharedMemory->name, strerror(errno)); + exit(EXIT_FAILURE); + } + + // Truncate shared memory object to specified size int result = ftruncate(fd, size); if(result == -1) { logg("FATAL: realloc_shm(): ftruncate(%i, %zu): Failed to resize \"%s\": %s", @@ -408,7 +410,8 @@ bool realloc_shm(SharedMemory *sharedMemory, size_t size, bool resize) // Close shared memory object file descriptor as it is no longer // needed after having called ftruncate() - close(fd); + if(resize) + close(fd); sharedMemory->ptr = new_ptr; sharedMemory->size = size; From a0b01dde36a47ad34e813bb86927483dbb74d8ec Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Thu, 21 Feb 2019 18:11:54 -0800 Subject: [PATCH 8/9] Store next string position in shared memory settings This was causing an issue because each process had a different value for the next string position, and caused corruption in the string shared memory. Signed-off-by: Mcat12 --- FTL.h | 1 + shmem.c | 31 ++++++++++++++----------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/FTL.h b/FTL.h index 60b9f8f0b..3cdc064ec 100644 --- a/FTL.h +++ b/FTL.h @@ -224,6 +224,7 @@ typedef struct { typedef struct { int version; unsigned int global_shm_counter; + unsigned int next_str_pos; } ShmSettings; // Prepare timers, used mainly for debugging purposes diff --git a/shmem.c b/shmem.c index 59a4b580d..ee44af4ee 100644 --- a/shmem.c +++ b/shmem.c @@ -44,7 +44,6 @@ static ShmLock *shmLock = NULL; static ShmSettings *shmSettings = NULL; static int pagesize; -static unsigned int next_pos = 0; static unsigned int local_shm_counter = 0; unsigned long long addstr(const char *str) @@ -58,10 +57,10 @@ unsigned long long addstr(const char *str) // Get string length size_t len = strlen(str); - if(debug) logg("Adding \"%s\" (len %i) to buffer. next_pos is %i", str, len, next_pos); + if(debug) logg("Adding \"%s\" (len %i) to buffer. next_str_pos is %i", str, len, shmSettings->next_str_pos); // Reserve additional memory if necessary - size_t required_size = next_pos + len + 1; + size_t required_size = shmSettings->next_str_pos + len + 1; // Need to cast to long long because size_t calculations cannot be negative if((long long)required_size-(long long)shm_strings.size > 0 && !realloc_shm(&shm_strings, shm_strings.size + pagesize, true)) @@ -72,14 +71,14 @@ unsigned long long addstr(const char *str) counters->strings_MAX = shm_strings.size; // Copy the C string pointed by str into the shared string buffer - strncpy(&((char*)shm_strings.ptr)[next_pos], str, len); - ((char*)shm_strings.ptr)[next_pos + len] = '\0'; + strncpy(&((char*)shm_strings.ptr)[shmSettings->next_str_pos], str, len); + ((char*)shm_strings.ptr)[shmSettings->next_str_pos + len] = '\0'; // Increment string length counter - next_pos += len+1; + shmSettings->next_str_pos += len+1; // Return start of stored string - return (next_pos - (len + 1)); + return (shmSettings->next_str_pos - (len + 1)); } char *getstr(unsigned long long pos) @@ -187,6 +186,13 @@ bool init_shmem(void) shm_counters = create_shm(SHARED_COUNTERS_NAME, sizeof(countersStruct)); counters = (countersStruct*)shm_counters.ptr; + /****************************** shared settings struct ******************************/ + // Try to create shared memory object + shm_settings = create_shm(SHARED_SETTINGS_NAME, sizeof(ShmSettings)); + shmSettings = (ShmSettings*)shm_settings.ptr; + shmSettings->version = SHARED_MEMORY_VERSION; + shmSettings->global_shm_counter = 0; + /****************************** shared strings buffer ******************************/ // Try to create shared memory object shm_strings = create_shm(SHARED_STRINGS_NAME, pagesize); @@ -194,7 +200,7 @@ bool init_shmem(void) // Initialize shared string object with an empty string at position zero ((char*)shm_strings.ptr)[0] = '\0'; - next_pos = 1; + shmSettings->next_str_pos = 1; /****************************** shared domains struct ******************************/ // Try to create shared memory object @@ -227,15 +233,6 @@ bool init_shmem(void) overTime = (overTimeDataStruct*)shm_overTime.ptr; initOverTime(); - /****************************** shared settings struct ******************************/ - // Try to create shared memory object - shm_settings = create_shm(SHARED_SETTINGS_NAME, sizeof(ShmSettings)); - if(shm_settings.ptr == NULL) - return false; - shmSettings = (ShmSettings*)shm_settings.ptr; - shmSettings->version = SHARED_MEMORY_VERSION; - shmSettings->global_shm_counter = 0; - return true; } From a11d3dd55f8e401fb10d2f9e263c8023971dc4d6 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Thu, 21 Feb 2019 18:20:51 -0800 Subject: [PATCH 9/9] Lower shared memory version to 4 The version on development is 3, so this version should be 4. Signed-off-by: Mcat12 --- shmem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shmem.c b/shmem.c index ee44af4ee..61f5ce119 100644 --- a/shmem.c +++ b/shmem.c @@ -12,7 +12,7 @@ #include "shmem.h" /// The version of shared memory used -#define SHARED_MEMORY_VERSION 5 +#define SHARED_MEMORY_VERSION 4 /// The name of the shared memory. Use this when connecting to the shared memory. #define SHARED_LOCK_NAME "/FTL-lock"