Skip to content

Commit

Permalink
Reduce code-duplication by using an array of shared memory pointers w…
Browse files Browse the repository at this point in the history
…e can iterate on when chown-ing or deleteing.

Signed-off-by: DL6ER <dl6er@dl6er.de>
  • Loading branch information
DL6ER committed Feb 21, 2021
1 parent 6512290 commit 0463544
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions src/shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ static SharedMemory shm_settings = { 0 };
static SharedMemory shm_dns_cache = { 0 };
static SharedMemory shm_per_client_regex = { 0 };

static SharedMemory *sharedMemories[] = { &shm_lock,
&shm_strings,
&shm_counters,
&shm_domains,
&shm_clients,
&shm_queries,
&shm_upstreams,
&shm_overTime,
&shm_settings,
&shm_dns_cache,
&shm_per_client_regex };
#define NUM_SHMEM (sizeof(sharedMemories)/sizeof(SharedMemory*))

// Variable size array structs
static queriesData *queries = NULL;
static clientsData *clients = NULL;
Expand Down Expand Up @@ -154,21 +167,6 @@ static bool chown_shmem(SharedMemory *sharedMemory, struct passwd *ent_pw)
return true;
}

void chown_all_shmem(struct passwd *ent_pw)
{
chown_shmem(&shm_lock, ent_pw);
chown_shmem(&shm_strings, ent_pw);
chown_shmem(&shm_counters, ent_pw);
chown_shmem(&shm_domains, ent_pw);
chown_shmem(&shm_clients, ent_pw);
chown_shmem(&shm_queries, ent_pw);
chown_shmem(&shm_upstreams, ent_pw);
chown_shmem(&shm_overTime, ent_pw);
chown_shmem(&shm_settings, ent_pw);
chown_shmem(&shm_dns_cache, ent_pw);
chown_shmem(&shm_per_client_regex, ent_pw);
}

// A function that duplicates a string and replaces all characters "s" by "r"
static char *__attribute__ ((malloc)) str_replace(const char *input,
const char s,
Expand Down Expand Up @@ -565,26 +563,27 @@ bool init_shmem(bool create_new)
return true;
}

// CHOWN all shared memory objects to suppplied user/group
void chown_all_shmem(struct passwd *ent_pw)
{
for(unsigned int i = 0; i < NUM_SHMEM; i++)
chown_shmem(sharedMemories[i], ent_pw);
}

// Destory mutex and, subsequently, delete all shared memory objects
void destroy_shmem(void)
{
// First, we destroy the mutex
if(shmLock != NULL)
{
pthread_mutex_destroy(&shmLock->shmem.lock);
pthread_mutex_destroy(&shmLock->logfile.lock);
}
shmLock = NULL;

delete_shm(&shm_lock);
delete_shm(&shm_strings);
delete_shm(&shm_counters);
delete_shm(&shm_domains);
delete_shm(&shm_clients);
delete_shm(&shm_queries);
delete_shm(&shm_upstreams);
delete_shm(&shm_overTime);
delete_shm(&shm_settings);
delete_shm(&shm_dns_cache);
delete_shm(&shm_per_client_regex);
// Then, we delete the shared memory objects
for(unsigned int i = 0; i < NUM_SHMEM; i++)
delete_shm(sharedMemories[i]);
}

/// Create shared memory
Expand Down

0 comments on commit 0463544

Please sign in to comment.