daemon: Move some common code to daemon.c