Reminder for later. Put a file-descriptor in non-blocking mode with fcntl:

static int
set_non_blocking(int fd)
{
        return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
}