From 5f9fa28251443d3295d9979da95e08df10c0c3c3 Mon Sep 17 00:00:00 2001 From: bol-van Date: Tue, 4 Feb 2025 23:28:18 +0300 Subject: [PATCH] nfqws,tpws: r/o open instead of stat() for list file check --- nfq/helpers.c | 12 ++++++++++++ nfq/helpers.h | 1 + nfq/nfqws.c | 8 ++++---- tpws/helpers.c | 12 ++++++++++++ tpws/helpers.h | 1 + tpws/tpws.c | 8 ++++---- 6 files changed, 34 insertions(+), 8 deletions(-) diff --git a/nfq/helpers.c b/nfq/helpers.c index 2e4727d..00a0ce0 100644 --- a/nfq/helpers.c +++ b/nfq/helpers.c @@ -9,6 +9,7 @@ #include #include #include +#include int unique_size_t(size_t *pu, int ct) { @@ -313,6 +314,17 @@ bool file_mod_signature(const char *filename, file_mod_sig *ms) return true; } +bool file_open_test(const char *filename, int flags) +{ + int fd = open(filename,flags); + if (fd>=0) + { + close(fd); + return true; + } + return false; +} + bool pf_in_range(uint16_t port, const port_filter *pf) { return port && (((!pf->from && !pf->to) || (port>=pf->from && port<=pf->to)) ^ pf->neg); diff --git a/nfq/helpers.h b/nfq/helpers.h index d8ede1a..6f3e5a6 100644 --- a/nfq/helpers.h +++ b/nfq/helpers.h @@ -77,6 +77,7 @@ typedef struct #define FILE_MOD_RESET(ms) memset(ms,0,sizeof(file_mod_sig)) bool file_mod_signature(const char *filename, file_mod_sig *ms); time_t file_mod_time(const char *filename); +bool file_open_test(const char *filename, int flags); typedef struct { diff --git a/nfq/nfqws.c b/nfq/nfqws.c index 5b7b56c..7ad5a7e 100644 --- a/nfq/nfqws.c +++ b/nfq/nfqws.c @@ -126,16 +126,16 @@ static bool test_list_files() struct ipset_file *ifile; LIST_FOREACH(hfile, ¶ms.hostlists, next) - if (!file_mod_time(hfile->filename)) + if (!file_open_test(hfile->filename, O_RDONLY)) { - DLOG_PERROR("file_mod_time"); + DLOG_PERROR("file_open_test"); DLOG_ERR("cannot access hostlist file '%s'\n",hfile->filename); return false; } LIST_FOREACH(ifile, ¶ms.ipsets, next) - if (!file_mod_time(ifile->filename)) + if (!file_open_test(ifile->filename, O_RDONLY)) { - DLOG_PERROR("file_mod_time"); + DLOG_PERROR("file_open_test"); DLOG_ERR("cannot access ipset file '%s'\n",ifile->filename); return false; } diff --git a/tpws/helpers.c b/tpws/helpers.c index 7db6aac..3724856 100644 --- a/tpws/helpers.c +++ b/tpws/helpers.c @@ -11,6 +11,7 @@ #include #include #include +#include #ifdef __ANDROID__ #include "andr/ifaddrs.h" @@ -327,6 +328,17 @@ bool file_mod_signature(const char *filename, file_mod_sig *ms) return true; } +bool file_open_test(const char *filename, int flags) +{ + int fd = open(filename,flags); + if (fd>=0) + { + close(fd); + return true; + } + return false; +} + bool pf_in_range(uint16_t port, const port_filter *pf) { return port && (((!pf->from && !pf->to) || (port >= pf->from && port <= pf->to)) ^ pf->neg); diff --git a/tpws/helpers.h b/tpws/helpers.h index 1b74430..cda4dcf 100644 --- a/tpws/helpers.h +++ b/tpws/helpers.h @@ -71,6 +71,7 @@ typedef struct #define FILE_MOD_RESET(ms) memset(ms,0,sizeof(file_mod_sig)) bool file_mod_signature(const char *filename, file_mod_sig *ms); time_t file_mod_time(const char *filename); +bool file_open_test(const char *filename, int flags); typedef struct { diff --git a/tpws/tpws.c b/tpws/tpws.c index ddc7b98..46e2eab 100644 --- a/tpws/tpws.c +++ b/tpws/tpws.c @@ -122,16 +122,16 @@ static bool test_list_files() struct ipset_file *ifile; LIST_FOREACH(hfile, ¶ms.hostlists, next) - if (!file_mod_time(hfile->filename)) + if (!file_open_test(hfile->filename, O_RDONLY)) { - DLOG_PERROR("file_mod_time"); + DLOG_PERROR("file_open_test"); DLOG_ERR("cannot access hostlist file '%s'\n",hfile->filename); return false; } LIST_FOREACH(ifile, ¶ms.ipsets, next) - if (!file_mod_time(ifile->filename)) + if (!file_open_test(ifile->filename, O_RDONLY)) { - DLOG_PERROR("file_mod_time"); + DLOG_PERROR("file_open_test"); DLOG_ERR("cannot access ipset file '%s'\n",ifile->filename); return false; }