Refactor code in pgsql_src for server monitoring

Most of those things are taken from pg_ctl.c, but were somewhat not
really in a place corresponding to their role.