static int doTerminateInstance(struct nc_state_t *nc, ncMetadata * pMeta, char *instanceId, int force, int *shutdownState, int *previousState)
{
    ncInstance *instance = NULL;
    int err = EUCA_ERROR;
    char resourceName[1][MAX_SENSOR_NAME_LEN] = { {0} };
    char resourceAlias[1][MAX_SENSOR_NAME_LEN] = { {0} };

    {                                  // find the instance to ensure we know about it
        sem_p(inst_sem);
        instance = find_instance(&global_instances, instanceId);
        sem_v(inst_sem);
    }

    if (instance == NULL)
        return EUCA_NOT_FOUND_ERROR;

    // refresh stats so latest instance measurements are captured before it disappears
    euca_strncpy(resourceName[0], instanceId, MAX_SENSOR_NAME_LEN);
    sem_p(hyp_sem);                    // we serialize all hypervisor calls and sensor_refresh_resources() may ultimately call the hypervisor
    sensor_refresh_resources(resourceName, resourceAlias, 1);
    sem_v(hyp_sem);

    // do the shutdown in a thread
    pthread_attr_t tattr;
    pthread_t tid;
    pthread_attr_init(&tattr);
    pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
    void *param = (void *)strdup(instanceId);
    if (pthread_create(&tid, &tattr, terminating_thread, (void *)param) != 0) {
        LOGERROR("[%s] failed to start VM termination thread\n", instanceId);
    } else {
        // previous and shutdown state are ignored by CC anyway
        *previousState = 0;
        *shutdownState = 0;
        err = EUCA_OK;
    }

    return err;
}
static int doTerminateInstance(struct nc_state_t *nc, ncMetadata * pMeta, char *instanceId, int force, int *shutdownState, int *previousState)
{
    ncInstance *instance = NULL;
    int err = EUCA_ERROR;
    char resourceName[1][MAX_SENSOR_NAME_LEN] = { {0} };
    char resourceAlias[1][MAX_SENSOR_NAME_LEN] = { {0} };

    {                                   
        sem_p(inst_sem);
        instance = find_instance(&global_instances, instanceId);
        sem_v(inst_sem);
    }

    if (instance == NULL)
        return EUCA_NOT_FOUND_ERROR;

     
    euca_strncpy(resourceName[0], instanceId, MAX_SENSOR_NAME_LEN);
    sem_p(hyp_sem);                     
    sensor_refresh_resources(resourceName, resourceAlias, 1);
    sem_v(hyp_sem);

     
    pthread_attr_t tattr;
    pthread_t tid;
    pthread_attr_init(&tattr);
    pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
    void *param = (void *)strdup(instanceId);
    if (pthread_create(&tid, &tattr, terminating_thread, (void *)param) != 0) {
        LOGERROR("[%s] failed to start VM termination thread\n", instanceId);
    } else {
         
        *previousState = 0;
        *shutdownState = 0;
        err = EUCA_OK;
    }

    return err;
}