Skip to content

Commit

Permalink
Merge pull request #1072 from pi-hole/tweak/shmem_resize_in_lock
Browse files Browse the repository at this point in the history
Resize shared memory only when locking
  • Loading branch information
DL6ER committed Mar 2, 2021
2 parents 6d5c118 + 06c7216 commit 2485ac9
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 134 deletions.
8 changes: 5 additions & 3 deletions src/database/query-table.c
Expand Up @@ -396,6 +396,9 @@ void DB_read_queries(void)
continue;
}

// Lock shared memory
lock_shm();

const char *buffer = NULL;
int upstreamID = -1; // Default if not forwarded
// Try to extract the upstream from the "forward" column if non-empty
Expand All @@ -418,9 +421,6 @@ void DB_read_queries(void)
const int domainID = findDomainID(domainname, true);
const int clientID = findClientID(clientIP, true, false);

// Ensure we have enough space in the queries struct
memory_check(QUERIES);

// Set index for this query
const int queryIndex = counters->queries;

Expand Down Expand Up @@ -563,6 +563,8 @@ void DB_read_queries(void)
logg("Warning: Found unknown status %i in long term database!", status);
break;
}

unlock_shm();
}
logg("Imported %i queries from the long-term database", counters->queries);

Expand Down
12 changes: 0 additions & 12 deletions src/datastructure.c
Expand Up @@ -89,9 +89,6 @@ int findUpstreamID(const char * upstreamString, const in_port_t port)
const int upstreamID = counters->upstreams;
logg("New upstream server: %s:%u (%i/%u)", upstreamString, port, upstreamID, counters->upstreams_MAX);

// Check struct size
memory_check(UPSTREAMS);

// Get upstream pointer
upstreamsData* upstream = getUpstream(upstreamID, false);
if(upstream == NULL)
Expand Down Expand Up @@ -152,9 +149,6 @@ int findDomainID(const char *domainString, const bool count)
// Store ID
const int domainID = counters->domains;

// Check struct size
memory_check(DOMAINS);

// Get domain pointer
domainsData* domain = getDomain(domainID, false);
if(domain == NULL)
Expand Down Expand Up @@ -212,9 +206,6 @@ int findClientID(const char *clientIP, const bool count, const bool aliasclient)
// Store ID
const int clientID = counters->clients;

// Check struct size
memory_check(CLIENTS);

// Get client pointer
clientsData* client = getClient(clientID, false);
if(client == NULL)
Expand Down Expand Up @@ -332,9 +323,6 @@ int findCacheID(int domainID, int clientID, enum query_types query_type)
// Get ID of new cache entry
const int cacheID = counters->dns_cache_size;

// Check struct size
memory_check(DNS_CACHE);

// Get client pointer
DNSCacheData* dns_cache = getDNSCache(cacheID, false);

Expand Down
5 changes: 1 addition & 4 deletions src/dnsmasq_interface.c
Expand Up @@ -569,6 +569,7 @@ bool _FTL_new_query(const unsigned int flags, const char *name,

// Lock shared memory
lock_shm();
const int queryID = counters->queries;

// Find client IP
const int clientID = findClientID(clientIP, true, false);
Expand Down Expand Up @@ -604,10 +605,6 @@ bool _FTL_new_query(const unsigned int flags, const char *name,
return true;
}

// Ensure we have enough space in the queries struct
memory_check(QUERIES);
const int queryID = counters->queries;

// Log new query if in debug mode
if(config.debug & DEBUG_QUERIES)
{
Expand Down
10 changes: 5 additions & 5 deletions src/files.c
Expand Up @@ -83,7 +83,7 @@ void ls_dir(const char* path)
char full_path[strlen(path)+NAME_MAX+2];

logg("------ Listing content of directory %s ------", path);
logg("File Mode User:Group Filesize Filename");
logg("File Mode User:Group Size Filename");

struct dirent *dircontent = NULL;
// Walk directory file by file
Expand Down Expand Up @@ -112,12 +112,12 @@ void ls_dir(const char* path)
snprintf(user, sizeof(user), "%d", st.st_uid);

struct group *grp;
char group[256];
char usergroup[256];
// Get out group name
if ((grp = getgrgid(st.st_gid)) != NULL)
snprintf(group, sizeof(group), "%s", grp->gr_name);
snprintf(usergroup, sizeof(usergroup), "%s:%s", user, grp->gr_name);
else
snprintf(group, sizeof(group), "%d", st.st_gid);
snprintf(usergroup, sizeof(usergroup), "%s:%d", user, st.st_gid);

char permissions[10];
// Get human-readable format of permissions as known from ls
Expand All @@ -138,7 +138,7 @@ void ls_dir(const char* path)
format_memory_size(prefix, (unsigned long long)st.st_size, &formated);

// Log output for this file
logg("%s %s:%s %.0f%s %s", permissions, user, group, formated, prefix, filename);
logg("%s %-15s %3.0f%s %s", permissions, usergroup, formated, prefix, filename);
}

logg("---------------------------------------------------");
Expand Down
2 changes: 1 addition & 1 deletion src/log.c
Expand Up @@ -226,7 +226,7 @@ void format_memory_size(char * const prefix, const unsigned long long int bytes,
break;
*formated /= 1e3;
}
const char* prefixes[8] = { "", "K", "M", "G", "T", "P", "E", "?" };
const char* prefixes[8] = { " ", "K", "M", "G", "T", "P", "E", "?" };
// Chose matching SI prefix
strcpy(prefix, prefixes[i]);
}
Expand Down

0 comments on commit 2485ac9

Please sign in to comment.