我想要更改命名空間。當我在C編譯和運行代碼時,它工作正常,但是我在netns系統調用中得到了errno 22。任何想法爲什麼會發生這種情況更改linux命名空間in
GO)
$ go build main.go ; ./main
setns mnt: Invalid argument
panic: -1
goroutine 1 [running]:
runtime.panic(0x423b80, 0xffffffffffffffff)
/usr/local/go/src/pkg/runtime/panic.c:266 +0xb6
main.main()
main.go:81 +0x86
$
C)
$ grep ^// main.go | sed 's/\/\///' | sed 's/__main/main/' > main.c; gcc main.c -o main; ./main
$
下面的代碼:
package main
//
// #define _GNU_SOURCE
// #include <fcntl.h>
// #include <sched.h>
// #include <sys/syscall.h>
// #include <sys/param.h>
// #include <sys/mount.h>
// #include <stdio.h>
// #include <unistd.h>
//
// #define NETNS_RUN_DIR "/run/netns"
// #define MNTNS_RUN_DIR "/run/mntns"
//
// #ifndef HAVE_SETNS
//
// int
// setns(int fd, int nstype) {
// #ifdef __NR_setns
// return syscall(__NR_setns, fd, nstype);
// #else
// errno = ENOSYS;
// return -1;
// #endif
// }
//
// #endif /* HAVE_SETNS */
//
//
// int
// ChangeNamespace(char *name)
// {
// char net_path[MAXPATHLEN];
// char mnt_path[MAXPATHLEN];
// int fd;
//
// snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
// snprintf(mnt_path, sizeof(mnt_path), "%s/%s", MNTNS_RUN_DIR, name);
//
// fd = open(net_path, O_RDONLY);
// if (fd < 0) {
// perror("open net");
// return -1;
// }
//
// if (setns(fd, 0) < 0) {
// perror("setns net");
// return -1;
// }
//
// fd = open(mnt_path, O_RDONLY);
// if (fd < 0) {
// perror("open mnt");
// return -1;
// }
//
// if (setns(fd, 0) < 0) {
// perror("setns mnt");
// return -1;
// }
//
// return 0;
// }
//
// int
// __main(int argc, char *argv[]) {
// ChangeNamespace("ns");
// return 0;
// }
//
import "C"
import "unsafe"
func main() {
name := C.CString("ns")
defer C.free(unsafe.Pointer(name))
i := int(C.ChangeNamespace(name))
if i < 0 {
panic(i)
}
}
你可以改變你的'perror()'這些字符串來表示哪個調用失敗嗎?只是「無效的論點」不足以解釋發生了什麼。 (我有一種感覺,這是一個移動的堆棧問題...) – andlabs 2014-09-04 15:23:55
您還需要調用'runtime.LockOSThread'來確保從預期的線程調用setns。 – JimB 2014-09-04 15:32:41
你可能想看看docker如何在[libcontainer/namespaces](https://github.com/libcontainer/namespaces)中處理這個問題。還有至少1個關於爲即將推出的'go.sys'軟件包添加更多支持的未解決問題:[#8447](https://code.google.com/p/go/issues/detail?id=8447) – JimB 2014-09-04 15:34:58