This patch implements a small finite state machine on the cmd side to be
able to run a command, wait for all of its output, and then exit.
As the daemon can send those message in any order, we need to wait them
properly, in particular the closing of stdout/stderr before exiting.
This patch also fixes the read_until_close callback creation to ensure
that the stream EOF is handled as a distinct message.