add an exit_main_fail() function and use it.

This should clear shm->mainpid in all the places that matter before the
main process exits.