util: do_exec: fix error handling and make function global