I have a 'dummy' network interface setup with systemd-networkd; by default the interface is left 'down'.
There is a service unit which runs an application that binds to the address on that interface (using a non-local-bind so that it can bind even when the interface is down). If, and only if, the service is operating, the network interface should be 'up'.
The result I'm hoping for is that if the service is restarted, or crashes and can't be brought back up, the interface will be left 'down', and the routing daemon on the machine (bird2 using OSPF) will stop broadcasting a route to that address, and thus requests for the service will be routed to other available instances.
I could use
ExecStopPost to run 'ip link set up/down' or 'networkctl up/down' commands (and in fact that was my first attempt), but the unit file for this service runs it in a very tightly sandboxed environment as an unprivileged user, so those commands can't be executed because they will be run in an environment without CAP_NET_ADMIN. An example of the service unit file is here.
I've also tried creating a separate service to just run these commands, which will run normally as 'root' and not be restricted in this way. However, I haven't found a combination of dependency specs that will cause the second service to follow the state of the first service.
If I use
BindsTo= in service B, pointing to service A, then service B is properly started after service A during system startup. If service A is stopped, service B is stopped. If service A is restarted by systemd, then service B is also restarted.
However, if service A is stopped, and then started again, service B is not started.
The only other option I can think of is to make a new target which combines both services, and somehow restrict service A so that it cannot be manually started and in that way remind the admin that they need start the target and not the service. It's not clear to me how I could make this new target be automatically depended-on by