Check if temperature sensors exist before reading them to avoid spamming the log with "no such file or directory" messages for sensors without labels

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2023-01-15 20:43:38 +01:00
parent 64ea7ff5b2
commit 70fff27c46
4 changed files with 47 additions and 26 deletions
+12 -3
View File
@@ -210,7 +210,11 @@ static int read_temp_sensor(struct ftl_conn *api,
const char *short_path,
cJSON *object)
{
FILE *f_label = fopen(label_path, "r");
// Check if sensor is available
if(file_exists(value_path) == false)
return 0;
// Open files
FILE *f_value = fopen(value_path, "r");
if(f_value != NULL)
{
@@ -218,6 +222,10 @@ static int read_temp_sensor(struct ftl_conn *api,
char label[1024];
if(fscanf(f_value, "%d", &raw_temp) == 1)
{
FILE *f_label = NULL;
if(file_exists(label_path))
f_label = fopen(label_path, "r");
cJSON *item = JSON_NEW_OBJECT();
if(f_label && fgets(label, sizeof(label)-1, f_label))
{
@@ -232,6 +240,9 @@ static int read_temp_sensor(struct ftl_conn *api,
}
JSON_COPY_STR_TO_OBJECT(item, "path", short_path);
if(f_label != NULL)
fclose(f_label);
// Compute actual temperature
double temp = raw_temp < 1000 ? raw_temp : 1e-3*raw_temp;
const char *unit = "C";
@@ -251,8 +262,6 @@ static int read_temp_sensor(struct ftl_conn *api,
JSON_ADD_ITEM_TO_ARRAY(object, item);
}
}
if(f_label != NULL)
fclose(f_label);
if(f_value != NULL)
fclose(f_value);
+2 -2
View File
@@ -20,7 +20,7 @@
#include "config/config.h"
// JSON array functions
#include "cJSON/cJSON.h"
// directoryExists()
// directory_exists()
#include "files.h"
#define DNSMASQ_PH_CONFIG "/etc/pihole/dnsmasq.conf"
@@ -290,7 +290,7 @@ bool __attribute__((const)) write_dnsmasq_config(bool test_config)
fputs("server=/bind/\n", pihole_conf);
fputs("server=/onion/\n", pihole_conf);
if(directoryExists("/etc/dnsmasq.d"))
if(directory_exists("/etc/dnsmasq.d"))
{
// Load possible additional user scripts from /etc/dnsmasq.d if
// the directory exists (it may not, e.g., in a container)
+32 -20
View File
@@ -59,10 +59,40 @@ bool chmod_file(const char *filename, const mode_t mode)
return true;
}
/**
* Function to check whether a file exists or not.
* It returns true if given path is a file and exists
* otherwise returns false.
*/
bool file_exists(const char *filename)
{
struct stat st;
return stat(filename, &st) == 0;
struct stat stats = { 0 };
if(stat(filename, &stats) != 0)
{
// Directory does not exist
return false;
}
// Check if this is a directory
return S_ISREG(stats.st_mode);
}
/**
* Function to check whether a directory exists or not.
* It returns true if given path is a directory and exists
* otherwise returns false.
*/
bool directory_exists(const char *path)
{
struct stat stats = { 0 };
if(stat(path, &stats) != 0)
{
// Directory does not exist
return false;
}
// Check if this is a directory
return S_ISDIR(stats.st_mode);
}
bool get_database_stat(struct stat *st)
@@ -234,24 +264,6 @@ static char *trim(char *str)
return start;
}
/**
* Function to check whether a directory exists or not.
* It returns 1 if given path is directory and exists
* otherwise returns 0.
*/
bool directoryExists(const char *path)
{
struct stat stats = { 0 };
if(stat(path, &stats) != 0)
{
// Directory does not exist
return false;
}
// Check if this is a directory
return S_ISDIR(stats.st_mode);
}
// Credits: https://stackoverflow.com/a/55410469
int parse_line(char *line, char **key, char **value)
{
+1 -1
View File
@@ -18,7 +18,7 @@ void get_permission_string(char permissions[10], struct stat *st);
void ls_dir(const char* path);
int get_path_usage(const char *path, char buffer[64]);
int get_filepath_usage(const char *file, char buffer[64]);
bool directoryExists(const char *path);
bool directory_exists(const char *path);
int parse_line(char *line, char **key, char **value);