Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shared memory crashes in TCP forks #1485

Merged
merged 2 commits into from
Nov 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 14 additions & 29 deletions src/shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,47 +135,32 @@ static int get_dev_shm_usage(char buffer[64])
// (while we initialized the shared memory objects)
static void verify_shmem_pid(void)
{
const int fd = shm_open(SHARED_SETTINGS_NAME, O_RDWR, S_IRUSR | S_IWUSR);
if(fd == -1)
// Open shared memory settings object
const int settingsfd = shm_open(SHARED_SETTINGS_NAME, O_RDONLY, S_IRUSR | S_IWUSR);
if(settingsfd == -1)
{
logg("FATAL: verify_shmem_pid(): Failed to open shared memory object \"%s\": %s",
SHARED_SETTINGS_NAME, strerror(errno));
exit(EXIT_FAILURE);
}

// Get new pointer
void *shm = mmap(NULL, shm_settings.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

// Check for `mmap` error
if(shm == MAP_FAILED)
{
logg("FATAL: verify_shmem_pid(): Failed to map shared memory object \"%s\" (%i): %s",
SHARED_SETTINGS_NAME, fd, strerror(errno));
SHARED_SETTINGS_NAME, strerror(errno));
exit(EXIT_FAILURE);
}

// Close file descriptor after call to mmap
close(fd);

// Unmap old memory region
if(munmap(shm_settings.ptr, shm_settings.size) < 0)
ShmSettings shms = { 0 };
if(read(settingsfd, &shms, sizeof(shms)) != sizeof(shms))
{
logg("FATAL: verify_shmem_pid(): Failed to unmap shared memory object \"%s\" (%i): %s",
SHARED_SETTINGS_NAME, fd, strerror(errno));
logg("FATAL: verify_shmem_pid(): Failed to read %zu bytes from shared memory object \"%s\": %s",
sizeof(shms), SHARED_SETTINGS_NAME, strerror(errno));
exit(EXIT_FAILURE);
}

// Update pointer
shmSettings = shm_settings.ptr = shm;
close(settingsfd);

// Compare the SHM's PID to the one we had when creating the SHM objects
if(shmSettings->pid == shmem_pid)
if(shms.pid == shmem_pid)
return;

// If we reach here, we are in serious trouble. Terminating with error
// code is the most sensible thing we can do at this point
logg("FATAL: Shared memory is owned by a different process (PID %d)",
shmSettings->pid);
logg("FATAL: Shared memory is owned by a different process (PID %d)", shms.pid);
check_running_FTL();
logg("Exiting now!");
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -782,6 +767,9 @@ static bool realloc_shm(SharedMemory *sharedMemory, const size_t size1, const si
// TCP requests.
if(resize)
{
// Verify shared memory ownership
verify_shmem_pid();

// Open shared memory object
const int fd = shm_open(sharedMemory->name, O_RDWR, S_IRUSR | S_IWUSR);
if(fd == -1)
Expand Down Expand Up @@ -921,9 +909,6 @@ static size_t get_optimal_object_size(const size_t objsize, const size_t minsize
// Enlarge shared memory to be able to hold at least one new record
void shm_ensure_size(void)
{
// Verify shared memory ownership
verify_shmem_pid();

if(counters->queries >= counters->queries_MAX-1)
{
// Have to reallocate shared memory
Expand Down