示例#1
0
文件: libct.go 项目: hallyn/libct
func (route *NetRoute) AddNextHop() (*NetRouteNextHop, error) {
	nh := C.libct_net_route_add_nh(route.route)
	if C.libct_handle_is_err(unsafe.Pointer(nh)) != 0 {
		return nil, LibctError{int(C.libct_handle_to_err(unsafe.Pointer(nh)))}
	}

	return &NetRouteNextHop{nh}, nil
}
示例#2
0
文件: libct.go 项目: hallyn/libct
func (s *Session) ProcessCreateDesc() (*ProcessDesc, error) {
	p := C.libct_process_desc_create(s.s)
	if C.libct_handle_is_err(unsafe.Pointer(p)) != 0 {
		return nil, LibctError{int(C.libct_handle_to_err(unsafe.Pointer(p)))}
	}

	return &ProcessDesc{desc: p}, nil
}
示例#3
0
文件: libct.go 项目: hallyn/libct
func (ct *Container) AddRoute() (*NetRoute, error) {
	r := C.libct_net_route_add(ct.ct)

	if C.libct_handle_is_err(unsafe.Pointer(r)) != 0 {
		return nil, LibctError{int(C.libct_handle_to_err(unsafe.Pointer(r)))}
	}

	return &NetRoute{r}, nil
}
示例#4
0
文件: libct.go 项目: hallyn/libct
func (nd *NetDev) GetPeer() (*NetDev, error) {

	dev := C.libct_net_dev_get_peer(nd.dev)

	if C.libct_handle_is_err(unsafe.Pointer(dev)) != 0 {
		return nil, LibctError{int(C.libct_handle_to_err(unsafe.Pointer(dev)))}
	}

	return &NetDev{dev}, nil
}
示例#5
0
文件: libct.go 项目: hallyn/libct
func (s *Session) OpenLocal() error {
	h := C.libct_session_open_local()

	if C.libct_handle_is_err(unsafe.Pointer(h)) != 0 {
		return LibctError{int(C.libct_handle_to_err(unsafe.Pointer(h)))}
	}

	s.s = h

	return nil
}
示例#6
0
文件: libct.go 项目: hallyn/libct
func (s *Session) ContainerCreate(name string) (*Container, error) {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	ct := C.libct_container_create(s.s, cname)

	if C.libct_handle_is_err(unsafe.Pointer(ct)) != 0 {
		return nil, LibctError{int(C.libct_handle_to_err(unsafe.Pointer(ct)))}
	}

	return &Container{ct}, nil
}
示例#7
0
文件: libct.go 项目: hallyn/libct
func (ct *Container) Load(p *ProcessDesc, pid int) error {
	h := C.libct_container_load(ct.ct, C.pid_t(pid))

	if C.libct_handle_is_err(unsafe.Pointer(h)) != 0 {
		p.closeDescriptors(p.closeAfterStart)
		p.closeDescriptors(p.closeAfterWait)
		return LibctError{int(C.libct_handle_to_err(unsafe.Pointer(h)))}
	}

	p.handle = h

	return nil
}
示例#8
0
文件: libct.go 项目: hallyn/libct
func (ct *Container) Processes() ([]int, error) {
	ctasks := C.libct_container_processes(ct.ct)
	if C.libct_handle_is_err(unsafe.Pointer(ctasks)) != 0 {
		return nil, LibctError{int(C.libct_handle_to_err(unsafe.Pointer(ctasks)))}
	}
	defer C.libct_processes_free(ctasks)

	tasks := make([]int, int(ctasks.nproc))
	for i := 0; i < int(ctasks.nproc); i++ {
		tasks[i] = int(C.libct_processes_get(ctasks, C.int(i)))
	}

	return tasks, nil
}
示例#9
0
文件: libct.go 项目: hallyn/libct
func (ct *Container) AddNetVeth(host_name string, ct_name string) (*NetDev, error) {

	var args C.struct_ct_net_veth_arg

	chost_name := C.CString(host_name)
	defer C.free(unsafe.Pointer(chost_name))
	cct_name := C.CString(ct_name)
	defer C.free(unsafe.Pointer(cct_name))

	args.host_name = chost_name
	args.ct_name = cct_name

	dev := C.libct_net_add(ct.ct, C.CT_NET_VETH, unsafe.Pointer(&args))

	if C.libct_handle_is_err(unsafe.Pointer(dev)) != 0 {
		return nil, LibctError{int(C.libct_handle_to_err(unsafe.Pointer(dev)))}
	}

	return &NetDev{dev}, nil
}
示例#10
0
文件: libct.go 项目: hallyn/libct
func (ct *Container) execve(p *ProcessDesc, path string, argv []string, env []string, spawn bool) error {
	var (
		h C.ct_process_t
		i int = 0
	)

	type F func(*ProcessDesc) (file, error)
	for _, setupFd := range []F{(*ProcessDesc).stdin, (*ProcessDesc).stdout, (*ProcessDesc).stderr} {
		fd, err := setupFd(p)
		if err != nil {
			p.closeDescriptors(p.closeAfterStart)
			p.closeDescriptors(p.closeAfterWait)
			return err
		}
		p.childFiles = append(p.childFiles, fd)
		i = i + 1
	}

	freeStrings := func(array []*C.char) {
		for _, item := range array {
			if item != nil {
				C.free(unsafe.Pointer(item))
			}
		}
	}

	p.childFiles = append(p.childFiles, p.ExtraFiles...)

	cargv := make([]*C.char, len(argv)+1)
	defer freeStrings(cargv)

	for i, arg := range argv {
		cargv[i] = C.CString(arg)
	}

	var penv **C.char
	if env == nil {
		penv = nil
	} else {
		cenv := make([]*C.char, len(env)+1)
		defer freeStrings(cenv)

		for i, e := range env {
			cenv[i] = C.CString(e)
		}
		penv = &cenv[0]
	}

	cfds := make([]C.int, len(p.childFiles))
	for i, fd := range p.childFiles {
		cfds[i] = C.int(getFd(fd))
	}

	C.libct_process_desc_set_fds(p.desc, &cfds[0], C.int(len(p.childFiles)))

	cpath := C.CString(path)
	defer C.free(unsafe.Pointer(cpath))

	if spawn {
		h = C.libct_container_spawn_execve(ct.ct, p.desc, cpath, &cargv[0], penv)
	} else {
		h = C.libct_container_enter_execve(ct.ct, p.desc, cpath, &cargv[0], penv)
	}

	if C.libct_handle_is_err(unsafe.Pointer(h)) != 0 {
		p.closeDescriptors(p.closeAfterStart)
		p.closeDescriptors(p.closeAfterWait)
		return LibctError{int(C.libct_handle_to_err(unsafe.Pointer(h)))}
	}

	p.closeDescriptors(p.closeAfterStart)

	p.errch = make(chan error, len(p.goroutine))
	for _, fn := range p.goroutine {
		go func(fn func() error) {
			p.errch <- fn()
		}(fn)
	}

	p.handle = h

	return nil
}