Example #1
0
} END_TEST

START_TEST(test_cc_pod_is_sandbox) {
	struct cc_oci_config *config = NULL;

	ck_assert(!cc_pod_is_sandbox(config));

	config = cc_oci_config_create ();
	ck_assert(config);
	ck_assert(!cc_pod_is_sandbox(config));

	config->pod = g_malloc0 (sizeof (struct cc_pod));
	ck_assert(config->pod);

	config->pod->sandbox = false;
	ck_assert(!cc_pod_is_sandbox(config));

	config->pod->sandbox = true;
	ck_assert(cc_pod_is_sandbox(config));

	/* clean up */
	cc_oci_config_free (config);
} END_TEST
Example #2
0
/*!
 * Forcibly stop the Hypervisor.
 *
 * \param config \ref cc_oci_config.
 * \param state \ref oci_state.
 * \param signum Signal number to send to hypervisor.
 * \param all_processes \c true to send a signal to all container's processes,
 *  \c false to send a signal to container's workload.
 *
 * \return \c true on success, else \c false.
 */
gboolean
cc_oci_kill (struct cc_oci_config *config,
		struct oci_state *state,
		int signum,
		gboolean all_processes)
{
	enum oci_status last_status;

	if (! (config && state)) {
		return false;
	}

	if (! cc_oci_get_signame (signum)) {
		g_critical ("signal %d is not supported", signum);
		return false;
	}

	/* save current status */
	last_status = config->state.status;

	/* A sandbox is not a running container, nothing to kill here */
	if (cc_pod_is_sandbox(config)) {
		config->state.status = OCI_STATUS_STOPPED;

		/* update state file */
		if (! cc_oci_state_file_create (config, state->create_time)) {
			g_critical ("failed to recreate state file");
			goto error;
		}

		return true;
	}

	/* stopping container */
	config->state.status = OCI_STATUS_STOPPING;

	/* update state file */
	if (! cc_oci_state_file_create (config, state->create_time)) {
		g_critical ("failed to recreate state file");
		goto error;
	}

#ifndef UNIT_TESTING
	/* kill container's processes, cc-shim will catch exit code
	 * and will end
	 */
	if (! cc_proxy_hyper_kill_container (config, signum, all_processes)) {
		g_critical ("failed to kill container %s: %s",
			config->optarg_container_id,
			strerror (errno));
		/* revert container status */
		config->state.status = last_status;
		if (! cc_oci_state_file_create (config, state->create_time)) {
			g_critical ("failed to recreate state file");
		}
		return false;
	}
#endif //UNIT_TESTING

	if (signum == SIGKILL) {
		/* cc-shim is not able to catch the exit code of a process
		 * finished with SIGKILL signal hence cc-shim MUST receive
		 * this signal too
		 */
		if (kill (state->pid, signum) < 0 && errno != ESRCH) {
			g_critical ("failed to stop cc-shim %u: %s",
					(unsigned)state->pid,
					strerror (errno));
			return false;
		}
	}

	last_status = config->state.status;

	config->state.status = OCI_STATUS_STOPPED;

	/* update state file */
	if (! cc_oci_state_file_create (config, state->create_time)) {
		g_critical ("failed to recreate state file");
		goto error;
	}

	return true;

error:
	/* revert container status */
	config->state.status = last_status;

	return false;
}
Example #3
0
/*!
 * Forcibly stop the Hypervisor.
 *
 * \param config \ref cc_oci_config.
 * \param state \ref oci_state.
 * \param signum Signal number to send to hypervisor.
 *
 * \return \c true on success, else \c false.
 */
gboolean
cc_oci_kill (struct cc_oci_config *config,
		struct oci_state *state,
		int signum)
{
	enum oci_status last_status;

	if (! (config && state)) {
		return false;
	}

	/* save current status */
	last_status = config->state.status;

	/* A sandbox is not a running container, nothing to kill here */
	if (cc_pod_is_sandbox(config)) {
		config->state.status = OCI_STATUS_STOPPED;

		/* update state file */
		if (! cc_oci_state_file_create (config, state->create_time)) {
			g_critical ("failed to recreate state file");
			goto error;
		}

		return true;
	}

	/* stopping container */
	config->state.status = OCI_STATUS_STOPPING;

	/* update state file */
	if (! cc_oci_state_file_create (config, state->create_time)) {
		g_critical ("failed to recreate state file");
		goto error;
	}

	/* send signal to cc-shim because it
	 * will send killcontainer to hyperstart
	 */
	if (kill (state->pid, signum) < 0) {
		g_critical ("failed to stop container %s "
				"running with pid %u: %s",
				config->optarg_container_id,
				(unsigned)state->pid,
				strerror (errno));
		/* revert container status */
		config->state.status = last_status;
		if (! cc_oci_state_file_create (config, state->create_time)) {
			g_critical ("failed to recreate state file");
		}
		return false;
	}

#ifndef UNIT_TESTING
	/* cc-shim is not able to catch SIGKILL and SIGSTOP
	 * signals for this reason we must kill the container
	 * using hyperstart's API
	 */
	if (signum == SIGKILL || signum == SIGSTOP) {
		if (! cc_proxy_hyper_kill_container (config, signum)) {
			g_critical ("failed to kill container");
			return false;
		}
	}
#endif //UNIT_TESTING

	last_status = config->state.status;

	config->state.status = OCI_STATUS_STOPPED;

	/* update state file */
	if (! cc_oci_state_file_create (config, state->create_time)) {
		g_critical ("failed to recreate state file");
		goto error;
	}

	return true;

error:
	/* revert container status */
	config->state.status = last_status;

	return false;
}