IPC namespace nsenter

nsenter

https://www.cnblogs.com/sparkdev/p/9400673.html

 

与 namespace 相关的工具

unshare 命令
unshare 命令把当前进程加入到一个新建的 namespace 中,然后运行指定的程序(不指定目标程序则运行系统的默认 shell)。在前文《Linux Namespace: UTS》中我们介绍了一些与 namespace 相关的 API,比如 unshare 函数。unshare 函数的功能是把当前进程加入到一个新建的 namespace 中。比起我们自己写的小 demo,系统工具中已经内置了 unshare 命令行工具,本文将使用系统中的 unshare 命令进行相关的演示。对 unshare 命令的实现感兴趣的朋友可以参考其源代码,它也是通过调用 unshare 函数实现的。
下面的例子就是通过 unshare 命令让新建的 bash 进程属于新的 IPC namespace:

$ sudo unshare -i

nsenter 命令
nsenter 命令把当前进程加入到指定进程的 namespace 中,然后运行指定的程序(不指定目标程序则运行系统的默认 shell)。其实这个命令的核心功能也是通过我们在前文《Linux Namespace: UTS》中介绍的 setns 函数实现的。这个命令和 unshare 命令一样,也属于 linux 的 sys-utiles 工具,对其实现感兴趣的朋友可以参考其源代码。
我们接上面的例子,使用 nsenter 命令把一个 bash 进程加入到 4956 号进程的 IPC namespace 中

此时当前 bash 进程的 IPC namespace 已经和 4956 号进程的 IPC namespace 是同一个了。

演示 IPC namespace 隔离

接下来让我们通过 IPC 信号量的隔离来了解如何隔离 IPC namespace。

首先我们打开两个 bash shell,为了方便区分,分别把它们称为为 shell1 和 shell2。先在 shell2 中执行 sudo unshare -i,然后分别执行 readlink /proc/$$/ns/ipc 命令:

图中左侧为 shell1,右侧为 shell2。可以看出它们的 IPC namespace 是不同的。

然后我们在 shell2 中创建 IPC 信号量集,并分别在两个 shell 中进行查看:

结果显示,shell1 中不能观察到 shell2 中创建的 IPC 信号量集,这是因为 shell1 和 shell2 此时分别在不同的 IPC namespace 中。

接下来我们在 shell1 中启动一个新的 bash 进程,并通过 nsenter 命令把它加入到 shell2 的 IPC namespace 中,然后再次查看 IPC 信号量信息:

这次 shell1 中显示的信号量信息和 shell2 中是一样的。

最后让我们看看此时 shell1 和 shell2 中当前进程的 IPC namespace:

此时这两个进程属于同一个 IPC namespace,这才是他们可以看到相同的 IPC 资源的根本原因。

 

 

用一个用户登陆的ipc一样

shell1

 

 

 shell2

 

 

 

shell2 执行unshare -i

[root@bogon ~]# readlink /proc/$$/ns/ipc
ipc:[4026531839]
[root@bogon ~]# unshare -i
[root@bogon ~]# readlink /proc/$$/ns/ipc
ipc:[4026535466]
[root@bogon ~]# 

 

shell1 

 

 

 

shell2

 

 

shell2 创建一个ipc

 

 

[root@bogon ~]# ipcs -s

------ Semaphore Arrays --------
key        semid      owner      perms      nsems     

You have new mail in /var/spool/mail/root
[root@bogon ~]# ipcmk -S 10
Semaphore id: 0
[root@bogon ~]# ipcs -s

------ Semaphore Arrays --------
key        semid      owner      perms      nsems     
0x7e8ef94b 0          root       644        10        

[root@bogon ~]# 

 

[root@bogon ~]# ipcs -s

------ Semaphore Arrays --------
key        semid      owner      perms      nsems     

You have new mail in /var/spool/mail/root
[root@bogon ~]# ipcmk -S 10
Semaphore id: 0
[root@bogon ~]# ipcs -s

------ Semaphore Arrays --------
key        semid      owner      perms      nsems     
0x7e8ef94b 0          root       644        10        

[root@bogon ~]# 

 

 

 

shell1看不到0x7e8ef94b

 

 

 

 

 打印$$可以看到shell的PID,退出后如果再次执行可以看到效果如下

 shell2 echo $$

[root@bogon ~]# echo $$
54308
[root@bogon ~]# 

 

 

shell1 加入 54308进程

[root@bogon ~]# nsenter -t 54308 -i
[root@bogon ~]# ipcs -s | grep 0x7e8ef94b
0x7e8ef94b 0          root       644        10        
[root@bogon ~]# ipcs -s 

------ Semaphore Arrays --------
key        semid      owner      perms      nsems     
0x7e8ef94b 0          root       644        10        

[root@bogon ~]# 

 

shell1 和shell2 的IPC namespace一样

shell2

[root@bogon ~]# readlink /proc/$$/ns/ipc
ipc:[4026535466]
[root@bogon ~]# 

 

shell1

[root@bogon ~]# readlink /proc/$$/ns/ipc
ipc:[4026535466]
[root@bogon ~]# 

 

posted on 2020-11-20 14:47  tycoon3  阅读(268)  评论(0编辑  收藏  举报

导航