AdGuardHome icon indicating copy to clipboard operation
AdGuardHome copied to clipboard

/etc/hosts does not reload when changed

Open hwilson-dev opened this issue 1 year ago • 7 comments

Prerequisites

Platform (OS and CPU architecture)

Darwin (aka macOS), AMD64 (aka x86_64)

Installation

GitHub releases or script from README

Setup

On one machine

AdGuard Home version

0.107.44

Action

When I make a change the /etc/hosts, I expect it to show in the 'runtime clients' table and I expect the configured IP address to be returned when querying it.

Expected result

I expect the newly configured data to be show and used.

Actual result

The change in /etc/hosts does not become active until I restart the AdguardHome process.

By looking at the code and turning on debug, I see that an FSWatcher is instantiated for the file. But it never seem to recive any change notifications from the OS. I tried both macOS and Windows.

I cloned the fsnotify repo and ran their test application. It works as expected. So I suspect the issue is with the AdguardHome use of that code.

Please take a look.

Additional information and/or screenshots

No response

hwilson-dev avatar Feb 06 '24 20:02 hwilson-dev

I was able to get it to work on macOS by using a Darwin specific defaultHostsPaths():

//go:build darwin

package aghnet

func defaultHostsPaths() (paths []string) {
        return []string{"private/etc/hosts"}
}

So I needed to use "private/" in front of "etc/hosts".

hwilson-dev avatar Feb 06 '24 21:02 hwilson-dev

I also needed this change in fswatcher.go:

-       return w.w.Add(filepath.Join("/", name))
+       return w.w.Add(filepath.Dir(filepath.Join("/", name)))

See the "Watching a file doesn't work well" comment in the README of the fsnotify repo.

hwilson-dev avatar Feb 06 '24 22:02 hwilson-dev

@hwilson-dev, hello and thanks for the report. What version of macOS do you use? It seems, the /etc directory is actually a symlink to the /private/etc directory. However, I can't reproduce the issue on my macOS 12.4 machine, trying to edit both /etc/hosts and /private/etc/hosts files. Did previous versions of AdGuard Home receive these notifications?

EugeneOne1 avatar Feb 07 '24 10:02 EugeneOne1

@EugeneOne1 I'm on macOS 13.5.1. I started with v0.107.36 and am now on v0.107.44.

Without the changes mentioned above, it never worked for me on Windows or macOS.

hwilson-dev avatar Feb 07 '24 15:02 hwilson-dev

@hwilson-dev, do you happen to find out, which of these changes are required on Windows to fix the issue? AFAIK, at least, the file location isn't that tricky there.

EugeneOne1 avatar Feb 07 '24 17:02 EugeneOne1

@EugeneOne1 The fswatcher.go change above:

-       return w.w.Add(filepath.Join("/", name))
+       return w.w.Add(filepath.Dir(filepath.Join("/", name)))

Made it work on Windows for me.

hwilson-dev avatar Feb 07 '24 17:02 hwilson-dev

@hwilson-dev, hello again. We've pushed the edge release that implements the suggested fixes, could you please check if it now tracks changes to the hosts file? Thanks for your participation.

EugeneOne1 avatar Feb 13 '24 10:02 EugeneOne1

@hwilson-dev, we're going to close the issue for now, feel free to reopen it if the problem reoccurs.

EugeneOne1 avatar Feb 16 '24 10:02 EugeneOne1