static int xen_detach_helper(struct nc_state_t *nc, char *instanceId, char *localDevReal, char *xml)
{
    int err = EUCA_ERROR;
    int rc = -1;

    pid_t pid = fork();
    if (!pid) {
        char tmpfile[MAX_PATH];
        snprintf(tmpfile, 32, "/tmp/detachxml.XXXXXX");
        int fd = safe_mkstemp(tmpfile);

        char devReal[32];
        char *tmp = strstr(xml, "<target");
        if (tmp == NULL) {
            LOGERROR("[%s] '<target' not found in the device xml\n", instanceId);
            return -1;
        }
        tmp = strstr(tmp, "dev=\"");
        if (tmp == NULL) {
            LOGERROR("[%s] '<target dev' not found in the device xml\n", instanceId);
            return -1;
        }
        snprintf(devReal, 32, "%s", tmp + strlen("dev=\""));
        for (int i = 0; i < 32; i++) {
            if (devReal[i] == '\"') {
                for (; i < 32; i++)
                    devReal[i] = '\0';
            }
        }

        if (fd > 0) {
            if (write(fd, xml, strlen(xml)) != strlen(xml)) {
                LOGERROR("[%s] fail to write %ld bytes in temp file.\n", instanceId, strlen(xml));
            }
            close(fd);

            char cmd[MAX_PATH];
            //! @todo does this work?
            snprintf(cmd, MAX_PATH, "[%s] executing '%s %s `which virsh` %s %s %s'", instanceId, nc->detach_cmd_path, nc->rootwrap_cmd_path, instanceId, devReal, tmpfile);
            LOGDEBUG("%s\n", cmd);
            rc = system(cmd);
            rc = rc >> 8;
            unlink(tmpfile);
        } else {
            LOGERROR("[%s] could not write to tmpfile for detach XML: %s\n", instanceId, tmpfile);
            rc = 1;
        }
        exit(rc);

    } else {
        // parent or failed to fork
        int status;
        rc = timewait(pid, &status, 15);
        if (WEXITSTATUS(status)) {
            LOGERROR("[%s] failed to sucessfully run detach helper\n", instanceId);
            err = EUCA_ERROR;
        } else {
            err = EUCA_OK;
        }
    }
    return (err);
}
static int xen_detach_helper(struct nc_state_t *nc, char *instanceId, char *localDevReal, char *xml)
{
    int err = EUCA_ERROR;
    int rc = -1;

    pid_t pid = fork();
    if (!pid) {
        char tmpfile[MAX_PATH];
        snprintf(tmpfile, 32, "/tmp/detachxml.XXXXXX");
        int fd = safe_mkstemp(tmpfile);

        char devReal[32];
        char *tmp = strstr(xml, "<target");
        if (tmp == NULL) {
            LOGERROR("[%s] '<target' not found in the device xml\n", instanceId);
            return -1;
        }
        tmp = strstr(tmp, "dev=\"");
        if (tmp == NULL) {
            LOGERROR("[%s] '<target dev' not found in the device xml\n", instanceId);
            return -1;
        }
        snprintf(devReal, 32, "%s", tmp + strlen("dev=\""));
        for (int i = 0; i < 32; i++) {
            if (devReal[i] == '\"') {
                for (; i < 32; i++)
                    devReal[i] = '\0';
            }
        }

        if (fd > 0) {
            if (write(fd, xml, strlen(xml)) != strlen(xml)) {
                LOGERROR("[%s] fail to write %ld bytes in temp file.\n", instanceId, strlen(xml));
            }
            close(fd);

            char cmd[MAX_PATH];
             
            snprintf(cmd, MAX_PATH, "[%s] executing '%s %s `which virsh` %s %s %s'", instanceId, nc->detach_cmd_path, nc->rootwrap_cmd_path, instanceId, devReal, tmpfile);
            LOGDEBUG("%s\n", cmd);
            rc = system(cmd);
            rc = rc >> 8;
            unlink(tmpfile);
        } else {
            LOGERROR("[%s] could not write to tmpfile for detach XML: %s\n", instanceId, tmpfile);
            rc = 1;
        }
        exit(rc);

    } else {
         
        int status;
        rc = timewait(pid, &status, 15);
        if (WEXITSTATUS(status)) {
            LOGERROR("[%s] failed to sucessfully run detach helper\n", instanceId);
            err = EUCA_ERROR;
        } else {
            err = EUCA_OK;
        }
    }
    return (err);
}