mirror of
https://github.com/pi-hole/FTL.git
synced 2024-10-26 16:52:18 +02:00
Move locking/unlocking to the place where the functions that need it are called. This fixes a bug where socket.c tried to release a lock that was already released in request.c. Furthermore, this prevents obtaining a lock for both, API calls that do not need a lock and also for unknown API calls.
Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
@@ -26,100 +26,134 @@ void process_request(char *client_message, int *sock)
|
||||
if(command(client_message, ">stats"))
|
||||
{
|
||||
processed = true;
|
||||
lock_shm();
|
||||
getStats(sock);
|
||||
unlock_shm();
|
||||
}
|
||||
else if(command(client_message, ">overTime"))
|
||||
{
|
||||
processed = true;
|
||||
lock_shm();
|
||||
getOverTime(sock);
|
||||
unlock_shm();
|
||||
}
|
||||
else if(command(client_message, ">top-domains") || command(client_message, ">top-ads"))
|
||||
{
|
||||
processed = true;
|
||||
lock_shm();
|
||||
getTopDomains(client_message, sock);
|
||||
unlock_shm();
|
||||
}
|
||||
else if(command(client_message, ">top-clients"))
|
||||
{
|
||||
processed = true;
|
||||
lock_shm();
|
||||
getTopClients(client_message, sock);
|
||||
unlock_shm();
|
||||
}
|
||||
else if(command(client_message, ">forward-dest"))
|
||||
{
|
||||
processed = true;
|
||||
lock_shm();
|
||||
getForwardDestinations(client_message, sock);
|
||||
unlock_shm();
|
||||
}
|
||||
else if(command(client_message, ">forward-names"))
|
||||
{
|
||||
processed = true;
|
||||
lock_shm();
|
||||
getForwardDestinations(">forward-dest unsorted", sock);
|
||||
unlock_shm();
|
||||
}
|
||||
else if(command(client_message, ">querytypes"))
|
||||
{
|
||||
processed = true;
|
||||
lock_shm();
|
||||
getQueryTypes(sock);
|
||||
unlock_shm();
|
||||
}
|
||||
else if(command(client_message, ">getallqueries"))
|
||||
{
|
||||
processed = true;
|
||||
lock_shm();
|
||||
getAllQueries(client_message, sock);
|
||||
unlock_shm();
|
||||
}
|
||||
else if(command(client_message, ">recentBlocked"))
|
||||
{
|
||||
processed = true;
|
||||
lock_shm();
|
||||
getRecentBlocked(client_message, sock);
|
||||
unlock_shm();
|
||||
}
|
||||
else if(command(client_message, ">clientID"))
|
||||
{
|
||||
processed = true;
|
||||
lock_shm();
|
||||
getClientID(sock);
|
||||
unlock_shm();
|
||||
}
|
||||
else if(command(client_message, ">QueryTypesoverTime"))
|
||||
{
|
||||
processed = true;
|
||||
lock_shm();
|
||||
getQueryTypesOverTime(sock);
|
||||
unlock_shm();
|
||||
}
|
||||
else if(command(client_message, ">version"))
|
||||
{
|
||||
processed = true;
|
||||
// No lock required
|
||||
getVersion(sock);
|
||||
}
|
||||
else if(command(client_message, ">dbstats"))
|
||||
{
|
||||
processed = true;
|
||||
// No lock required. Access to the database
|
||||
// is guaranteed to be atomic
|
||||
getDBstats(sock);
|
||||
}
|
||||
else if(command(client_message, ">ClientsoverTime"))
|
||||
{
|
||||
processed = true;
|
||||
lock_shm();
|
||||
getClientsOverTime(sock);
|
||||
unlock_shm();
|
||||
}
|
||||
else if(command(client_message, ">client-names"))
|
||||
{
|
||||
processed = true;
|
||||
lock_shm();
|
||||
getClientNames(sock);
|
||||
unlock_shm();
|
||||
}
|
||||
else if(command(client_message, ">unknown"))
|
||||
{
|
||||
processed = true;
|
||||
lock_shm();
|
||||
getUnknownQueries(sock);
|
||||
unlock_shm();
|
||||
}
|
||||
else if(command(client_message, ">domain"))
|
||||
{
|
||||
processed = true;
|
||||
lock_shm();
|
||||
getDomainDetails(client_message, sock);
|
||||
unlock_shm();
|
||||
}
|
||||
else if(command(client_message, ">cacheinfo"))
|
||||
{
|
||||
processed = true;
|
||||
lock_shm();
|
||||
getCacheInformation(sock);
|
||||
unlock_shm();
|
||||
}
|
||||
else if(command(client_message, ">reresolve"))
|
||||
{
|
||||
processed = true;
|
||||
logg("Received API request to re-resolve host names");
|
||||
// Need to release the thread lock already here to allow
|
||||
// the resolver to process the incoming PTR requests
|
||||
unlock_shm();
|
||||
// Important: Don't obtain a lock for this request
|
||||
// Locking will be done internally when needed
|
||||
// onlynew=false -> reresolve all host names
|
||||
resolveClients(false);
|
||||
resolveForwardDestinations(false);
|
||||
@@ -129,8 +163,10 @@ void process_request(char *client_message, int *sock)
|
||||
{
|
||||
processed = true;
|
||||
logg("Received API request to recompile regex");
|
||||
lock_shm();
|
||||
free_regex();
|
||||
read_regex_from_file();
|
||||
unlock_shm();
|
||||
}
|
||||
|
||||
// Test only at the end if we want to quit or kill
|
||||
|
||||
@@ -320,16 +320,10 @@ void *telnet_connection_handler_thread(void *socket_desc)
|
||||
// Clear client message receive buffer
|
||||
memset(client_message, 0, sizeof client_message);
|
||||
|
||||
// Lock FTL data structure, since it is likely that it will be changed here
|
||||
// Requests should not be processed/answered when data is about to change
|
||||
lock_shm();
|
||||
|
||||
// Process received message
|
||||
process_request(message, &sock);
|
||||
free(message);
|
||||
|
||||
// Release thread lock
|
||||
unlock_shm();
|
||||
|
||||
if(sock == 0)
|
||||
{
|
||||
// Client disconnected by sending EOT or ">quit"
|
||||
@@ -377,16 +371,10 @@ void *socket_connection_handler_thread(void *socket_desc)
|
||||
// Clear client message receive buffer
|
||||
memset(client_message, 0, sizeof client_message);
|
||||
|
||||
// Lock FTL data structure, since it is likely that it will be changed here
|
||||
// Requests should not be processed/answered when data is about to change
|
||||
lock_shm();
|
||||
|
||||
// Process received message
|
||||
process_request(message, &sock);
|
||||
free(message);
|
||||
|
||||
// Release thread lock
|
||||
unlock_shm();
|
||||
|
||||
if(sock == 0)
|
||||
{
|
||||
// Socket connection interrupted by sending EOT or ">quit"
|
||||
|
||||
Reference in New Issue
Block a user