Use hostsdir for custom.list to avoid cache flushing on changes

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER 2023-11-13 09:52:45 +01:00
parent 33639beaf1
commit af4214d8f9
No known key found for this signature in database
GPG Key ID: 00135ACBD90B28DD
4 changed files with 18 additions and 18 deletions

View File

@ -778,11 +778,7 @@ static int api_config_patch(struct ftl_conn *api)
// Rewrite HOSTS file if required
if(rewrite_hosts)
{
write_custom_list();
// Reload HOSTS file
kill(main_pid(), SIGHUP);
}
}
else
{
@ -976,11 +972,7 @@ static int api_config_put_delete(struct ftl_conn *api)
// Rewrite HOSTS file if required
if(rewrite_hosts)
{
write_custom_list();
// Reload HOSTS file
kill(main_pid(), SIGHUP);
}
// Send empty reply with matching HTTP status code
// 201 - Created or 204 - No content

View File

@ -441,10 +441,8 @@ int set_config_from_CLI(const char *key, const char *value)
}
else if(conf_item == &config.dns.hosts)
{
// We need to rewrite the custom.list file but do not need to
// restart dnsmasq. If dnsmasq is going to be restarted anyway,
// this is not necessary as the file will be rewritten during
// the restart
// We need to rewrite the custom.list file but do not
// need to restart dnsmasq
write_custom_list();
}

View File

@ -241,7 +241,7 @@ bool __attribute__((const)) write_dnsmasq_config(struct config *conf, bool test_
write_config_header(pihole_conf, "Dnsmasq config for Pi-hole's FTLDNS");
fputs("addn-hosts=/etc/pihole/local.list\n", pihole_conf);
fputs("addn-hosts="DNSMASQ_CUSTOM_LIST"\n", pihole_conf);
fputs("hostsdir="DNSMASQ_HOSTSDIR"\n", pihole_conf);
fputs("\n", pihole_conf);
fputs("# Don't read /etc/resolv.conf. Get upstream servers only from the configuration\n", pihole_conf);
fputs("no-resolv\n", pihole_conf);
@ -727,8 +727,8 @@ bool read_legacy_cnames_config(void)
bool read_legacy_custom_hosts_config(void)
{
// Check if file exists, if not, there is nothing to do
const char *path = DNSMASQ_CUSTOM_LIST;
const char *target = DNSMASQ_CUSTOM_LIST".bck";
const char *path = DNSMASQ_CUSTOM_LIST_LEGACY;
const char *target = DNSMASQ_CUSTOM_LIST_LEGACY".bck";
if(!file_exists(path))
return true;
@ -790,8 +790,16 @@ bool read_legacy_custom_hosts_config(void)
bool write_custom_list(void)
{
// Rotate old hosts files
rotate_files(DNSMASQ_CUSTOM_LIST, NULL);
// Ensure that the directory exists
if(!directory_exists(DNSMASQ_HOSTSDIR))
{
log_debug(DEBUG_CONFIG, "Creating directory "DNSMASQ_HOSTSDIR);
if(mkdir(DNSMASQ_HOSTSDIR, 0755) != 0)
{
log_err("Cannot create directory "DNSMASQ_HOSTSDIR": %s", strerror(errno));
return false;
}
}
log_debug(DEBUG_CONFIG, "Opening "DNSMASQ_CUSTOM_LIST" for writing");
FILE *custom_list = fopen(DNSMASQ_CUSTOM_LIST, "w");

View File

@ -26,7 +26,9 @@ bool write_custom_list(void);
#define DNSMASQ_TEMP_CONF "/etc/pihole/dnsmasq.conf.temp"
#define DNSMASQ_STATIC_LEASES "/etc/pihole/04-pihole-static-dhcp.conf"
#define DNSMASQ_CNAMES "/etc/pihole/05-pihole-custom-cname.conf"
#define DNSMASQ_CUSTOM_LIST "/etc/pihole/custom.list"
#define DNSMASQ_HOSTSDIR "/etc/pihole/hosts"
#define DNSMASQ_CUSTOM_LIST DNSMASQ_HOSTSDIR"/custom.list"
#define DNSMASQ_CUSTOM_LIST_LEGACY "/etc/pihole/custom.list"
#define DHCPLEASESFILE "/etc/pihole/dhcp.leases"
#endif //DNSMASQ_CONFIG_H