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"); }
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"); }
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"); }
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"); }