我想在go中更改 namespace 。当我在C中编译和运行代码时,它可以正常工作,但是在运行netns syscall时,出现了errno 22。任何想法为什么会发生这种情况?
去)
$ 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)
}
}
最佳答案
您可以使用类似这样的方法一起跳过cgo,但现在无法测试:
const (
netNS = "/run/netns/"
mntNS = "/run/mntns/"
)
func ChangeNamespace(name string) error {
fd, err := syscall.Open(netNS+name, syscall.O_RDONLY, 0666)
if err != nil {
return err
}
defer syscall.Close(fd)
if _, _, err := syscall.RawSyscall(syscall.SYS_SETNS, uintptr(fd), 0, 0); err != nil {
return err
}
fd1, err := syscall.Open(mntNS+name, syscall.O_RDONLY, 0666)
if err != nil {
return err
}
defer syscall.Close(fd1)
if _, _, err := syscall.RawSyscall(syscall.SYS_SETNS, uintptr(fd1), 0, 0); err != nil {
return err
}
return nil
}