ctld: fix several process setup/teardown bugs
All of the below bugs could result in a system where ctld is not
running, but LUNs and targets still exist in the kernel; a difficult
situation to recover from.
- open the pidfile earlier. Open the pidfile before reading the kernel's current state, so two racing ctld processes won't step on each others' toes.
- close the pidfile later. Close it after tearing down the configuration, for the same reason.
- If the configured pidfile changes, then rename it on SIGHUP rather than remove and recreate it.
- When running in debug mode, don't close the pidfile while handling a new connection. Only do that in non-debug mode, in the child of the fork.
- Register signal handlers earlier. Otherwise a SIGTERM signal received during startup could kill ctld without tearing down the configuration.
PR: 271460
Sponsored by: Axcient
Reviewed by: mav
Pull Request: https://github.com/freebsd/freebsd-src/pull/1370
(cherry picked from commit 5f89aea7b74aa4605b25af62e31303097a4a48cc)