C 从Go调用SETN返回mnt命名空间的EINVAL
C代码可以正常工作并正确地进入名称空间,但Go代码似乎总是从C 从Go调用SETN返回mnt命名空间的EINVAL,c,linux,go,system-calls,cgo,C,Linux,Go,System Calls,Cgo,C代码可以正常工作并正确地进入名称空间,但Go代码似乎总是从setns调用返回EINVAL以进入mnt名称空间。我已经尝试了许多排列(包括带有cgo的嵌入式C代码和外部.so)在运行1.2,1.3和当前提示 在gdb中单步执行代码显示,两个序列在libc中以完全相同的方式调用setns 我将问题归结为下面的代码。我做错了什么 安装程序 我有一个用于启动quick busybox容器的shell别名: alias startbb='docker inspect --format "{{ .Stat
setns
调用返回EINVAL以进入mnt
名称空间。我已经尝试了许多排列(包括带有cgo的嵌入式C代码和外部.so
)在运行1.2
,1.3
和当前提示
在gdb
中单步执行代码显示,两个序列在libc
中以完全相同的方式调用setns
我将问题归结为下面的代码。我做错了什么
安装程序
我有一个用于启动quick busybox容器的shell别名:
alias startbb='docker inspect --format "{{ .State.Pid }}" $(docker run -d busybox sleep 1000000)'
运行此命令后,startbb
将启动一个容器并输出其PID
lxc checkconfig
输出:
Found kernel config file /boot/config-3.8.0-44-generic
--- Namespaces ---
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: missing
Network namespace: enabled
Multiple /dev/pts instances: enabled
--- Control groups ---
Cgroup: enabled
Cgroup clone_children flag: enabled
Cgroup device: enabled
Cgroup sched: enabled
Cgroup cpu account: enabled
Cgroup memory controller: missing
Cgroup cpuset: enabled
--- Misc ---
Veth pair device: enabled
Macvlan: enabled
Vlan: enabled
File capabilities: enabled
uname-a
产生:
Linux gecko 3.8.0-44-generic #66~precise1-Ubuntu SMP Tue Jul 15 04:01:04 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
setns on ipc namespace succeeded
setns on uts namespace succeeded
setns on net namespace succeeded
setns on pid namespace succeeded
setns on mnt namespace failed: invalid argument
工作C代码
以下C代码工作正常:
#include <errno.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
main(int argc, char* argv[]) {
int i;
char nspath[1024];
char *namespaces[] = { "ipc", "uts", "net", "pid", "mnt" };
if (geteuid()) { fprintf(stderr, "%s\n", "abort: you want to run this as root"); exit(1); }
if (argc != 2) { fprintf(stderr, "%s\n", "abort: you must provide a PID as the sole argument"); exit(2); }
for (i=0; i<5; i++) {
sprintf(nspath, "/proc/%s/ns/%s", argv[1], namespaces[i]);
int fd = open(nspath, O_RDONLY);
if (setns(fd, 0) == -1) {
fprintf(stderr, "setns on %s namespace failed: %s\n", namespaces[i], strerror(errno));
} else {
fprintf(stdout, "setns on %s namespace succeeded\n", namespaces[i]);
}
close(fd);
}
}
失败的Go代码
相反,以下Go代码(应该是相同的)也不能很好地工作:
package main
import (
"fmt"
"os"
"path/filepath"
"syscall"
)
func main() {
if syscall.Geteuid() != 0 {
fmt.Println("abort: you want to run this as root")
os.Exit(1)
}
if len(os.Args) != 2 {
fmt.Println("abort: you must provide a PID as the sole argument")
os.Exit(2)
}
namespaces := []string{"ipc", "uts", "net", "pid", "mnt"}
for i := range namespaces {
fd, _ := syscall.Open(filepath.Join("/proc", os.Args[1], "ns", namespaces[i]), syscall.O_RDONLY, 0644)
err, _, msg := syscall.RawSyscall(308, uintptr(fd), 0, 0) // 308 == setns
if err != 0 {
fmt.Println("setns on", namespaces[i], "namespace failed:", msg)
} else {
fmt.Println("setns on", namespaces[i], "namespace succeeded")
}
}
}
相反,运行sudo go run main.go
会产生:
Linux gecko 3.8.0-44-generic #66~precise1-Ubuntu SMP Tue Jul 15 04:01:04 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
setns on ipc namespace succeeded
setns on uts namespace succeeded
setns on net namespace succeeded
setns on pid namespace succeeded
setns on mnt namespace failed: invalid argument
(有)
因此,这个问题的答案是,您必须从单线程上下文调用setns
。这是有意义的,因为setns
应该将当前线程连接到名称空间。由于Go是多线程的,因此需要在Go运行时线程启动之前进行setns
调用
我认为这是因为执行对syscall.RawSyscall
调用的线程不是主线程——即使使用runtime.LockOSThread
,结果也不是您所期望的(即goroutine被“锁定”到主C线程,因此相当于下面解释的构造函数技巧)
我在提交问题后得到的答复建议使用“cgoconstructor技巧”。我找不到任何关于这个“把戏”的“适当”文档,但Docker/Michael Crosby在nsinit
中使用了它,即使我逐行检查了代码,我也没有尝试以这种方式运行它(请参见下文)
“诀窍”基本上是,在启动Go运行时之前,您可以让cgo
执行一个C函数
要执行此操作,请添加\uuuu属性\uuuu((构造函数))
宏来修饰要在Go启动前运行的函数:
/*
__attribute__((constructor)) void init() {
// this code will execute before Go starts up
// in runs in a single-threaded C context
// before Go's threads start running
}
*/
import "C"
使用此作为模板,我修改了checkns.go,如下所示:
/*
#include <sched.h>
#include <stdio.h>
#include <fcntl.h>
__attribute__((constructor)) void enter_namespace(void) {
setns(open("/proc/<PID>/ns/mnt", O_RDONLY, 0644), 0);
}
*/
import "C"
... rest of file is unchanged ...
/*
#包括
#包括
#包括
__属性(构造函数)void输入名称空间(void){
setns(open(“/proc//ns/mnt”,O_RDONLY,0644),0);
}
*/
输入“C”
... 文件的其余部分不变。。。
此代码可以工作,但需要对PID
进行硬编码,因为它不能从命令行输入中正确读取,但它说明了这一想法(如果您从如上所述启动的容器中提供PID
,则此代码可以工作)
这很令人沮丧,因为我想多次调用setns
,但是由于这个C代码在Go运行时开始之前执行,所以没有Go代码可用
更新:在内核邮件列表中四处搜索提供了一个记录此内容的对话。我似乎在任何实际发布的手册页中都找不到它,但以下是Eric Biederman确认的setns(2)
补丁中的引用:
在以下情况下,进程可能不会与新的装载命名空间重新关联:
它是多线程的。更改装载命名空间需要
调用方同时拥有CAP_SYS_CHROOT和CAP_SYS_ADMIN
在其自己的用户名称空间和CAP_SYS_ADMIN中的功能
目标装载命名空间
你试过了吗?在goroutine(main)的开头添加它没有效果。这应该足够了,不是吗?关于围棋问题的答案很有趣;如果你在这里交叉张贴,我会投你一票@IainLowe@twotwotwo据我所知,我在这里写下了答案;如果需要澄清,请告诉我包含基于init()函数解决方案的工作解决方案