コード例 #1
0
ファイル: ct_proc.c プロジェクト: aburluka/libct
int main(int argc, char **argv)
{
	int *ct_root_pids;
	libct_session_t s;
	ct_handler_t ct;
	ct_process_desc_t p;

	ct_root_pids = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
			MAP_SHARED | MAP_ANON, 0, 0);
	ct_root_pids[0] = 0;
	ct_root_pids[1] = 0;

	s = libct_session_open_local();
	ct = libct_container_create(s, "test");
	p = libct_process_desc_create(s);
	if (libct_container_set_nsmask(ct, CLONE_NEWPID | CLONE_NEWNS))
		return err("No pid & mount NS");

	libct_container_set_option(ct, LIBCT_OPT_AUTO_PROC_MOUNT, NULL);

	libct_container_spawn_cb(ct, p, set_ct_root_pids, ct_root_pids);
	libct_container_wait(ct);
	libct_container_destroy(ct);
	libct_session_close(s);

	/* Should be init */
	if ((ct_root_pids[0] != 1) || (ct_root_pids[1] != 1))
		return fail("Pid mismatch");
	else
		return pass("Pids are OK");
}
コード例 #2
0
ファイル: ct_cgroup_basic.c プロジェクト: carriercomm/libct
int main(int argc, char **argv)
{
	int *ct_state;
	libct_session_t s;
	ct_handler_t ct;

	ct_state = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
			MAP_SHARED | MAP_ANON, 0, 0);
	ct_state[0] = 0;
	ct_state[1] = 0;

	s = libct_session_open_local();
	ct = libct_container_create(s, "test-fr");
	libct_controller_add(ct, CTL_FREEZER);
	libct_container_spawn_cb(ct, check_freezer_cg, ct_state);
	libct_container_wait(ct);
	libct_container_destroy(ct);
	libct_session_close(s);

	if (!ct_state[0])
		return fail("Container is not alive");
	if (!ct_state[1])
		return fail("Freezer cgroup is not there");

	return pass("Freezed CT is OK");
}
コード例 #3
0
ファイル: ct_enter_pidns.c プロジェクト: OpenVZ/libct
int main(int argc, char **argv)
{
	struct ct_arg cta;
	int p[2], status;
	libct_session_t s;
	ct_handler_t ct;
	ct_process_desc_t pd;
	ct_process_t pr, epr;

	test_init();

	if (pipe(p))
		return tst_perr("Unable to create pipe");
	cta.mark = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
			MAP_SHARED | MAP_ANON, 0, 0);
	cta.mark[0] = 0;
	cta.mark[1] = 0;
	cta.wait_fd = p[0];

	s = libct_session_open_local();
	ct = libct_container_create(s, "test");
	libct_container_set_nsmask(ct, CLONE_NEWPID);
	pd = libct_process_desc_create(s);
	pr = libct_container_spawn_cb(ct, pd, set_ct_alive, &cta);
	if (libct_handle_is_err(pr)) {
		return fail("Unable to start CT");
	}

	epr = libct_container_enter_cb(ct, pd, set_ct_enter, &cta);
	if (libct_handle_is_err(epr))
		return fail("Unable to enter into CT");

	if (kill(libct_process_get_pid(pr), SIGKILL))
		return fail("Unable to send a signal to the init process");

	if (libct_process_wait(epr, &status))
		return fail("Unable to wait a process");
	if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGKILL)
		return fail("Unexpected status %x\n", status);

	if (libct_process_wait(pr, &status))
		return fail("Unable to wait a process");
	if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGKILL)
		return fail("Unexpected status %x\n", status);


	libct_container_wait(ct);
	libct_container_destroy(ct);
	libct_process_desc_destroy(pd);
	libct_process_destroy(epr);
	libct_session_close(s);

	return pass("CT is created and entered");
}
コード例 #4
0
ファイル: ct_root_enter.c プロジェクト: avagin/libct
int main(int argc, char **argv)
{
	int fd, p[2], status;
	struct ct_arg cta;
	libct_session_t s;
	ct_handler_t ct;
	ct_process_desc_t pd;
	ct_process_t pr;

	pipe(p);

	mkdir(FS_ROOT, 0600);
	fd = open(FS_ROOT "/" FS_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0600);
	if (fd < 0) {
		tst_perr("Can't create file");
		return 2;
	}

	write(fd, FS_DATA, sizeof(FS_DATA));
	close(fd);

	cta.fs_data = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
			MAP_SHARED | MAP_ANON, 0, 0);
	cta.fs_data[0] = '\0';
	cta.fs_data[ENTER_DOFF] = '\0';
	cta.wait_fd = p[0];

	s = libct_session_open_local();
	ct = libct_container_create(s, "test");
	pd = libct_process_desc_create(s);
	libct_fs_set_root(ct, FS_ROOT);
	libct_container_spawn_cb(ct, pd, ct_main_fn, &cta);
	pr = libct_container_enter_cb(ct, pd, ct_enter_fn, &cta);
	if (libct_handle_is_err(pr))
		fail("Unable to enter into CT");
	libct_process_wait(pr, &status);
	write(p[1], "a", 1);
	libct_container_wait(ct);
	libct_container_destroy(ct);
	libct_session_close(s);

	unlink(FS_ROOT "/" FS_FILE);
	rmdir(FS_ROOT);

	if (strcmp(cta.fs_data, FS_DATA))
		return fail("FS not accessed");

	if (strcmp(cta.fs_data + ENTER_DOFF, FS_DATA))
		return fail("FS not entered");

	return pass("FS is created and entered");
}
コード例 #5
0
ファイル: ct_net_veth.c プロジェクト: carriercomm/libct
int main(int argc, char **argv)
{
	int p[2];
	struct ct_arg ca;
	libct_session_t s;
	ct_handler_t ct;
	struct ct_net_veth_arg va;

	ca.mark = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
			MAP_SHARED | MAP_ANON, 0, 0);
	pipe(p);

	ca.mark[0] = 0;
	ca.mark[1] = 0;
	ca.mark[2] = 0;
	ca.wait_pipe = p[0];

	va.host_name = VETH_HOST_NAME;
	va.ct_name = VETH_CT_NAME;

	s = libct_session_open_local();
	ct = libct_container_create(s, "test");
	libct_container_set_nsmask(ct, CLONE_NEWNET);

	if (libct_net_add(ct, CT_NET_VETH, &va))
		return err("Can't add hostnic");

	if (libct_container_spawn_cb(ct, check_ct_net, &ca))
		return err("Can't spawn CT");

	if (!system("ip link l " VETH_HOST_NAME ""))
		ca.mark[1] = 1;

	write(p[1], "a", 1);

	libct_container_wait(ct);
	libct_container_destroy(ct);
	libct_session_close(s);

	if (!ca.mark[0])
		return fail("CT is not alive");
	if (!ca.mark[1])
		return fail("VETH not created");
	if (!ca.mark[2])
		return fail("VETH not assigned");

	return pass("VETH works OK");
}
コード例 #6
0
ファイル: ct_root.c プロジェクト: avagin/libct
int main(int argc, char **argv)
{
	int fd;
	char *fs_data;
	libct_session_t s;
	ct_handler_t ct;
	ct_process_desc_t p;
	ct_process_t pr;

	mkdir(FS_ROOT, 0600);
	fd = open(FS_ROOT "/" FS_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0600);
	if (fd < 0) {
		tst_perr("Can't create file");
		return 2;
	}

	write(fd, FS_DATA, sizeof(FS_DATA));
	close(fd);

	fs_data = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
			MAP_SHARED | MAP_ANON, 0, 0);
	fs_data[0] = '\0';

	s = libct_session_open_local();
	ct = libct_container_create(s, "test");
	p = libct_process_desc_create(s);
	if (libct_fs_set_root(ct, FS_ROOT))
		return fail("Unable to set root");
	pr = libct_container_spawn_cb(ct, p, check_fs_data, fs_data);
	if (libct_handle_is_err(pr))
		return fail("Unable to start CT");
	if (libct_container_wait(ct))
		return fail("Unable to wait CT");
	libct_container_destroy(ct);
	libct_session_close(s);

	unlink(FS_ROOT "/" FS_FILE);
	rmdir(FS_ROOT);

	if (strcmp(fs_data, FS_DATA))
		return fail("FS not accessed");
	else
		return pass("FS is OK");
}
コード例 #7
0
ファイル: ct_service.c プロジェクト: mrunalp/libct
int main(int argc, char **argv)
{
	struct ct_arg cta;
	int pid, p[2], p2[2];
	libct_session_t s;
	ct_handler_t ct;
	int cg_ok = 0;
	char c;

	pipe(p);
	pipe(p2);
	cta.mark = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
			MAP_SHARED | MAP_ANON, 0, 0);
	cta.mark[0] = 0;
	cta.start_fd = p2[1];
	cta.wait_fd = p[0];

	s = libct_session_open_local();
	ct = libct_container_create(s, "test-s");
	if (libct_container_set_option(ct, LIBCT_OPT_KILLABLE, NULL))
		return err("can't set killable");

	if (libct_container_spawn_cb(ct, set_ct_alive, &cta))
		return err("can't start CT");

	read(p2[0], &c, 1);
	if (cta.mark[0])
		cg_ok = check_service_cg(cta.mark[0]);
	write(p[1], &c, 1);

	libct_container_wait(ct);
	libct_container_destroy(ct);
	libct_session_close(s);

	if (!cta.mark[0])
		return fail("CT is not alive");

	if (!cg_ok)
		return fail("Service CG is not there");

	return pass("service CG works OK");
}
コード例 #8
0
ファイル: ct_net_veth.c プロジェクト: avagin/libct
int main(int argc, char **argv)
{
	int p[2];
	struct ct_arg ca;
	libct_session_t s;
	ct_handler_t ct;
	ct_process_desc_t pd;
	ct_process_t pr;
	struct ct_net_veth_arg va;
	ct_net_t nd, nd_peer;

	ca.mark = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
			MAP_SHARED | MAP_ANON, 0, 0);
	pipe(p);

	ca.mark[0] = 0;
	ca.mark[1] = 0;
	ca.mark[2] = 0;
	ca.wait_pipe = p[0];

	va.host_name = VETH_HOST_NAME;
	va.ct_name = VETH_CT_NAME;

	s = libct_session_open_local();
	ct = libct_container_create(s, "test");
	pd = libct_process_desc_create(s);
	libct_container_set_nsmask(ct, CLONE_NEWNET);

	nd = libct_net_add(ct, CT_NET_VETH, &va);
	if (libct_handle_is_err(nd))
		return tst_err("Can't add hostnic");

	nd_peer = libct_net_dev_get_peer(nd);
	if (libct_handle_is_err(nd_peer))
		return tst_err("Can't get a veth peer");

	if (libct_net_dev_set_mac_addr(nd_peer, "00:11:22:33:44:66"))
		return tst_err("Can't set mac");

	if (libct_net_dev_set_mac_addr(nd, "00:11:22:33:44:55"))
		return tst_err("Can't set mac");

	if (libct_net_dev_add_ip_addr(nd, "192.168.123.123/32"))
		return tst_err("Can't set addr");

	pr = libct_container_spawn_cb(ct, pd, check_ct_net, &ca);
	if (libct_handle_is_err(pr))
		return tst_err("Can't spawn CT");

	if (!system("ip link l " VETH_HOST_NAME ""))
		ca.mark[1] = 1;

	write(p[1], "a", 1);

	libct_container_wait(ct);
	libct_container_destroy(ct);
	libct_session_close(s);

	if (!ca.mark[0])
		return fail("CT is not alive");
	if (!ca.mark[1])
		return fail("VETH not created");
	if (!ca.mark[2])
		return fail("VETH not assigned");

	return pass("VETH works OK");
}