resolve: reload /etc/hosts on inode change
authorGiedrius Statkevičius <giedriuswork@gmail.com>
Sun, 22 Mar 2020 20:40:18 +0000 (22:40 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 17 Apr 2020 10:27:10 +0000 (12:27 +0200)
commit9c61b53c748f024add63fa78b940449f48d965d9
tree5f99b4d31d131c6d1e2afa78b328f9688c2cd370
parentd540b947f41d6690dd99aaab533846ac1c673da7
resolve: reload /etc/hosts on inode change

On certain distributions such as NixOS the mtime of `/etc/hosts` is
locked to a fixed value. In such cases, only checking the last mtime of
`/etc/hosts` is not enough - we also need to check if the st_ino/st_dev
match up. Thus, let's make sure make sure that systemd-resolved also
rereads `/etc/hosts` if the inode or the device containing `/etc/hosts` changes.

Test script:

```bash
hosts="/etc/hosts"
echo "127.0.0.1 testpr" > "hosts_new"
mv "hosts_new" "$hosts"
resolvectl query testpr || exit 1
mtime="$(stat -c %y "$hosts")"
echo "127.0.0.1 newhost" > "hosts_tmp"
touch -d "$mtime" "hosts_tmp"
install -p "hosts_tmp" "$hosts"
sleep 10
resolvectl query newhost || exit 1
rm -f "hosts_tmp"
```

Closes #14456.

(cherry picked from commit aa5408e2e8a66b71f38531f1dcb91252c586a23c)
src/resolve/resolved-etc-hosts.c
src/resolve/resolved-manager.c
src/resolve/resolved-manager.h