0

I am writing a VPN utility, which adds new entries to /etc/hosts. In order to make things more managable, I try to do a bind-mount:

mount -o ro,bind myhosts /etc/hosts

While the VPN connection is disconnected, I remove myhosts, but the content still exists in /etc/hosts...

Is it possible to make myhosts transient, so that if it vanishes, the mount point is automatically removed and the original /etc/hosts is restored?

0

1 Answer 1

2

Is it possible to make myhosts transient

not on a VPN connection, without you writing a program that checks whether the connection works and deletes the file if it doesn't.

and if it vanishes the mount point is automatically removed and original /etc/hosts is restored?

this sounds like a very questionable approach. Your VPN client should probably implement Name Service Switch (NSS), that's what it's for.

8
  • I checked NSS, it is either too complicated, or not meet my request. Let's forget about VPN. What I want is: can umount be automatic if the underlying resource disappears?
    – xrfang
    Jun 8, 2023 at 6:48
  • As said in the beginning of my answer: no. Whatever resource you mean, you need to write a program to check that resource's availability and do what you want (unmount) accordingly. Files are no different here: deleting a file doesn't actually make it unavailable to someone still holding an open for handle of it, so you need to watch its path for changes. You have to solve such problems with userland programs. Jun 8, 2023 at 8:18
  • And honestly, having had a look at the NSS API you'd need to implement: it's really not that bad? Jun 8, 2023 at 8:21
  • Could you please explain, if using NSS API what should I do, and will it be automatic reverted if the VPN connection vanishes. The reason I am reluctant is that I program in Go, not in C
    – xrfang
    Jun 8, 2023 at 9:17
  • I've never written Go, but I think Go can export C symbols, so that's no inherent problem.You need to implement a NSS Service (which means shared library), which means you need to implement a setup and teardown function, and the one important function that looks up hosts, _nss_files_gethostbyname_r. In that function you check whether you're currently on VPN, and if so, you do one thing (e.g. read the host they asked for from a file), and if not, Jun 8, 2023 at 9:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .