Refactoring of daemon/* code to support exec and related features

This PR should not affect any existing functionality.

@crosbymichael : As per your request, I have extracted out the refactoring part of #7409 into this PR.