diff --git a/src/database/message-table.c b/src/database/message-table.c index ef94e22a..ede35e2e 100644 --- a/src/database/message-table.c +++ b/src/database/message-table.c @@ -685,11 +685,11 @@ void log_resource_shortage(const double load, const int nprocs, const int shmem, else if(disk > -1) { // Get filesystem details for this path - struct fstab *fsdetails = get_filesystem_details(path); + struct mntent *fsdetails = get_filesystem_details(path); // Create plain message if(fsdetails != NULL) - ret = snprintf(buf, sizeof(buf), "Disk shortage ahead: %d%% is used (%s) on filesystem mounted at %s", disk, msg, fsdetails->fs_file); + ret = snprintf(buf, sizeof(buf), "Disk shortage ahead: %d%% is used (%s) on filesystem mounted at %s", disk, msg, fsdetails->mnt_dir); else ret = snprintf(buf, sizeof(buf), "Disk shortage ahead: %d%% is used (%s) on partition containing the file %s", disk, msg, path); @@ -708,16 +708,13 @@ void log_resource_shortage(const double load, const int nprocs, const int shmem, // Create HTML message if(fsdetails != NULL) ret = snprintf(buf, sizeof(buf), "Disk shortage ahead: %d%% is used (%s) on filesystem mounted at %s", - disk, msg, fsdetails->fs_file); + disk, msg, fsdetails->mnt_dir); else ret = snprintf(buf, sizeof(buf), "Disk shortage ahead: %d%% is used (%s) on partition containing the file %s", disk, msg, path); if(ret > (int)sizeof(buf)) log_warn("log_resource_shortage(): Buffer too small to hold HTML message, warning truncated"); - // Rewind fsdetails to the beginning of the list - endfsent(); - add_html_message(item, buf); } } diff --git a/src/files.c b/src/files.c index f3f65993..e9fce9fe 100644 --- a/src/files.c +++ b/src/files.c @@ -250,26 +250,29 @@ unsigned int get_path_usage(const char *path, char buffer[64]) } // Get the filesystem where the given path is located -// Credits: https://stackoverflow.com/a/40660348/2087442 -struct fstab *get_filesystem_details(const char *path) { +struct mntent *get_filesystem_details(const char *path) +{ /* stat the file in question */ struct stat path_stat; stat(path, &path_stat); /* iterate through the list of devices */ - struct fstab *fs = NULL; - while( (fs = getfsent()) ) + FILE *aFile = setmntent("/proc/mounts", "r"); + struct mntent *ent; + while((ent = getmntent(aFile)) != NULL) { /* stat the mount point */ struct stat dev_stat; - stat(fs->fs_file, &dev_stat); + stat(ent->mnt_dir, &dev_stat); /* check if our file and the mount point are on the same device */ if( dev_stat.st_dev == path_stat.st_dev ) break; } - return fs; + endmntent(aFile); + + return ent; } // Credits: https://stackoverflow.com/a/55410469 diff --git a/src/files.h b/src/files.h index b53305b6..d30e7f80 100644 --- a/src/files.h +++ b/src/files.h @@ -12,8 +12,8 @@ #include #include -// getfsent() -#include +// setmntent() +#include #define ZIP_ROTATIONS 3 #define MAX_ROTATIONS 15 @@ -25,7 +25,7 @@ unsigned long long get_FTL_db_filesize(void); void get_permission_string(char permissions[10], struct stat *st); void ls_dir(const char* path); unsigned int get_path_usage(const char *path, char buffer[64]); -struct fstab *get_filesystem_details(const char *path); +struct mntent *get_filesystem_details(const char *path); bool directory_exists(const char *path); void rotate_files(const char *path, char **first_file); diff --git a/src/gc.c b/src/gc.c index c211543a..52c360af 100644 --- a/src/gc.c +++ b/src/gc.c @@ -276,6 +276,18 @@ static void runGC(const time_t now, time_t *lastGCrun) DBdeleteoldqueries = true; } +static bool check_files_on_same_device(const char *path1, const char *path2) +{ + struct stat s1, s2; + if(stat(path1, &s1) != 0 || stat(path2, &s2) != 0) + { + log_warn("check_files_on_same_device(): stat() failed: %s", strerror(errno)); + return false; + } + + return s1.st_dev == s2.st_dev; +} + void *GC_thread(void *val) { // Set thread name @@ -292,6 +304,9 @@ void *GC_thread(void *val) unsigned int LastLogStorageUsage = 0; unsigned int LastDBStorageUsage = 0; + bool db_and_log_on_same_dev = false; + db_and_log_on_same_dev = check_files_on_same_device(config.files.database.v.s, config.files.log.ftl.v.s); + // Create inotify watcher for pihole.toml config file watch_config(true); @@ -319,9 +334,17 @@ void *GC_thread(void *val) // Check available resources if(now - lastResourceCheck >= RCinterval) { + // Check load averages check_load(); + + // Check disk space of database file LastDBStorageUsage = check_space(config.files.database.v.s, LastDBStorageUsage); - LastLogStorageUsage = check_space(config.files.log.ftl.v.s, LastLogStorageUsage); + + // Check disk space of log file only if they are not on + // the same file system + if(!db_and_log_on_same_dev) + LastLogStorageUsage = check_space(config.files.log.ftl.v.s, LastLogStorageUsage); + lastResourceCheck = now; }