Skip to content

Commit

Permalink
Panic (crash) if we are unable to create or resize shared memory
Browse files Browse the repository at this point in the history
If we are unable to create or resize shared memory then there is no
point in continuing execution. Further interactions with shared memory
may result in crashes which are hard to debug. As a result of this
change, there is no need to check if the returned shared memory object
pointer is null, because if the function succeeded, the operation was
successful.

Signed-off-by: Mcat12 <newtoncat12@yahoo.com>
Signed-off-by: DL6ER <dl6er@dl6er.de>

Conflicts:
	shmem.c
  • Loading branch information
AzureMarker authored and DL6ER committed Feb 10, 2019
1 parent f4fa764 commit b9166d4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 30 deletions.
42 changes: 13 additions & 29 deletions shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ char *getstr(unsigned long long pos)
return &((char*)shm_strings.ptr)[pos];
}

/// Create a mutex for shared memory
// Create a mutex for shared memory

This comment has been minimized.

Copy link
@AzureMarker

AzureMarker Feb 10, 2019

Author Contributor

This is actually intentional, as /// signifies a one-line documentation comment.

pthread_mutex_t create_mutex() {
pthread_mutexattr_t lock_attr = {};
pthread_mutex_t lock = {};
Expand Down Expand Up @@ -141,17 +141,13 @@ bool init_shmem(void)
/****************************** shared memory lock ******************************/
// Try to create shared memory object
shm_lock = create_shm(SHARED_LOCK_NAME, sizeof(ShmLock));
if(shm_lock.ptr == NULL)
return false;
shmLock = (ShmLock*) shm_lock.ptr;
shmLock->lock = create_mutex();
shmLock->waitingForLock = false;

/****************************** shared strings buffer ******************************/
// Try to create shared memory object
shm_strings = create_shm(SHARED_STRINGS_NAME, pagesize);
if(shm_strings.ptr == NULL)
return false;

// Initialize shared string object with an empty string at position zero
((char*)shm_strings.ptr)[0] = '\0';
Expand All @@ -160,48 +156,36 @@ bool init_shmem(void)
/****************************** shared counters struct ******************************/
// Try to create shared memory object
shm_counters = create_shm(SHARED_COUNTERS_NAME, sizeof(countersStruct));
if(shm_counters.ptr == NULL)
return false;
counters = (countersStruct*)shm_counters.ptr;

/****************************** shared domains struct ******************************/
// Try to create shared memory object
shm_domains = create_shm(SHARED_DOMAINS_NAME, pagesize*sizeof(domainsDataStruct));
if(shm_domains.ptr == NULL)
return false;
domains = (domainsDataStruct*)shm_domains.ptr;
counters->domains_MAX = pagesize;

/****************************** shared clients struct ******************************/
// Try to create shared memory object
shm_clients = create_shm(SHARED_CLIENTS_NAME, pagesize*sizeof(clientsDataStruct));
if(shm_clients.ptr == NULL)
return false;
clients = (clientsDataStruct*)shm_clients.ptr;
counters->clients_MAX = pagesize;

/****************************** shared forwarded struct ******************************/
// Try to create shared memory object
shm_forwarded = create_shm(SHARED_FORWARDED_NAME, pagesize*sizeof(forwardedDataStruct));
if(shm_forwarded.ptr == NULL)
return false;
forwarded = (forwardedDataStruct*)shm_forwarded.ptr;
counters->forwarded_MAX = pagesize;

/****************************** shared queries struct ******************************/
// Try to create shared memory object
shm_queries = create_shm(SHARED_QUERIES_NAME, pagesize*sizeof(queriesDataStruct));
if(shm_queries.ptr == NULL)
return false;
queries = (queriesDataStruct*)shm_queries.ptr;
counters->queries_MAX = pagesize;

/****************************** shared overTime struct ******************************/
size_t size = (OVERTIME_SLOTS*sizeof(overTimeDataStruct)/pagesize + 1)*pagesize;
// Try to create shared memory object
shm_overTime = create_shm(SHARED_OVERTIME_NAME, size);
if(shm_overTime.ptr == NULL)
return false;
overTime = (overTimeDataStruct*)shm_overTime.ptr;
counters->overTime_MAX = size;
initOverTime();
Expand Down Expand Up @@ -250,9 +234,9 @@ SharedMemory create_shm(char *name, size_t size)
// Check for `shm_open` error
if(fd == -1)
{
logg("create_shm(): Failed to create_shm shared memory object \"%s\": %s",
logg("FATAL: create_shm(): Failed to create_shm shared memory object \"%s\": %s",
name, strerror(errno));
return sharedMemory;
exit(EXIT_FAILURE);
}

// Resize shared memory file
Expand All @@ -261,9 +245,9 @@ SharedMemory create_shm(char *name, size_t size)
// Check for `ftruncate` error
if(result == -1)
{
logg("create_shm(): ftruncate(%i, %zu): Failed to resize shared memory object \"%s\": %s",
logg("FATAL: create_shm(): ftruncate(%i, %zu): Failed to resize shared memory object \"%s\": %s",
fd, size, sharedMemory.name, strerror(errno));
return sharedMemory;
exit(EXIT_FAILURE);
}

// Create shared memory mapping
Expand All @@ -272,9 +256,9 @@ SharedMemory create_shm(char *name, size_t size)
// Check for `mmap` error
if(shm == MAP_FAILED)
{
logg("create_shm(): Failed to map shared memory object \"%s\" (%i): %s",
logg("FATAL: create_shm(): Failed to map shared memory object \"%s\" (%i): %s",
sharedMemory.name, fd, strerror(errno));
return sharedMemory;
exit(EXIT_FAILURE);
}

// Close shared memory object file descriptor as it is no longer
Expand Down Expand Up @@ -339,27 +323,27 @@ bool realloc_shm(SharedMemory *sharedMemory, size_t size) {
int fd = shm_open(sharedMemory->name, O_RDWR, S_IRUSR | S_IWUSR);
if(fd == -1)
{
logg("realloc_shm(): Failed to open shared memory object \"%s\": %s",
logg("FATAL: realloc_shm(): Failed to open shared memory object \"%s\": %s",
sharedMemory->name, strerror(errno));
return false;
exit(EXIT_FAILURE);
}

// Resize shard memory object to requested size
result = ftruncate(fd, size);
if(result == -1) {
logg("realloc_shm(): ftruncate(%i, %zu): Failed to resize \"%s\": %s",
logg("FATAL: realloc_shm(): ftruncate(%i, %zu): Failed to resize \"%s\": %s",
fd, size, sharedMemory->name, strerror(errno));
return false;
exit(EXIT_FAILURE);
}

// 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);
if(new_ptr == MAP_FAILED)
{
logg("realloc_shm(): mremap(%p, %zu, %zu, MREMAP_MAYMOVE): Failed to reallocate \"%s\" (%i): %s",
logg("FATAL: realloc_shm(): mremap(%p, %zu, %zu, MREMAP_MAYMOVE): Failed to reallocate \"%s\" (%i): %s",
sharedMemory->ptr, sharedMemory->size, size, sharedMemory->name, fd,
strerror(errno));
return false;
exit(EXIT_FAILURE);
}

// Close shared memory object file descriptor as it is no longer
Expand Down
3 changes: 2 additions & 1 deletion shmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ typedef struct {
///
/// \param name the name of the shared memory
/// \param size the size to allocate
/// \return a structure with a pointer to the mounted shared memory. The pointer will be NULL if it failed
/// \return a structure with a pointer to the mounted shared memory. The pointer
/// will always be valid, because if it failed FTL will have exited.
SharedMemory create_shm(char *name, size_t size);

/// Reallocate shared memory
Expand Down

0 comments on commit b9166d4

Please sign in to comment.