From 70fff27c46d85bc9984494158bebae3a8bd6e732 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 15 Jan 2023 20:43:38 +0100 Subject: [PATCH] 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 --- src/api/ftl.c | 15 ++++++++--- src/config/dnsmasq_config.c | 4 +-- src/files.c | 52 +++++++++++++++++++++++-------------- src/files.h | 2 +- 4 files changed, 47 insertions(+), 26 deletions(-) diff --git a/src/api/ftl.c b/src/api/ftl.c index 88f09151..47bc89a9 100644 --- a/src/api/ftl.c +++ b/src/api/ftl.c @@ -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); diff --git a/src/config/dnsmasq_config.c b/src/config/dnsmasq_config.c index 65e685f4..45a02c06 100644 --- a/src/config/dnsmasq_config.c +++ b/src/config/dnsmasq_config.c @@ -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) diff --git a/src/files.c b/src/files.c index 92c5f8c0..abb3e3f9 100644 --- a/src/files.c +++ b/src/files.c @@ -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) { diff --git a/src/files.h b/src/files.h index c636ba79..e1c332a8 100644 --- a/src/files.h +++ b/src/files.h @@ -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);