execv(“/bin/mount”,mount_args)) { 佩罗尔(“execv:”); 出口(1); } //这条线永远不会到达。 返回0; } else if(0,linux,mount,umount,Linux,Mount,Umount" /> execv(“/bin/mount”,mount_args)) { 佩罗尔(“execv:”); 出口(1); } //这条线永远不会到达。 返回0; } else if(0,linux,mount,umount,Linux,Mount,Umount" />

Linux 取消共享系统调用不';行不通 下面的简单C++程序试图取消安装空间,安装USB存储设备(位于 /DEV/SDD < /强>),等待输入,然后对该设备进行注销。 #include <iostream> #include <stdio.h> #include <exception> #include <algorithm> #include <vector> #include <limits> #include <stdio.h> #include <sys/wait.h> #include <unistd.h> int main() { unshare(CLONE_NEWNS); pid_t pid = fork(); if (0 == pid) { char * mount_args[] = {"/bin/mount", "--make-rprivate", "/dev/sdd", "/mnt", "-o,ro", "-o,noexec", NULL}; if (0 > execv("/bin/mount", mount_args)) { perror("execv: "); exit(1); } //this line will never be reached. return 0; } else if (0 < pid) { //parent process! int status = -1; wait(&status); if (0 == status) { std::cout << "press ENTER to continue...."; std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); char * umount_args[] = {"/bin/umount", "/mnt", NULL}; if (0 > execv("/bin/umount", umount_args)) { perror("execv: "); exit(1); } } return status; } else { //fork error! perror("fork!\n"); exit(1); } return 0; } #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 int main() { 取消共享(克隆和更新); pid_t pid=fork(); 如果(0==pid) { char*mount_args[]={/bin/mount“,“-make rprivate”,“/dev/sdd”,“/mnt”,“-o,ro”,“-o,noexec”,NULL}; 如果(0>execv(“/bin/mount”,mount_args)) { 佩罗尔(“execv:”); 出口(1); } //这条线永远不会到达。 返回0; } else if(0

Linux 取消共享系统调用不';行不通 下面的简单C++程序试图取消安装空间,安装USB存储设备(位于 /DEV/SDD < /强>),等待输入,然后对该设备进行注销。 #include <iostream> #include <stdio.h> #include <exception> #include <algorithm> #include <vector> #include <limits> #include <stdio.h> #include <sys/wait.h> #include <unistd.h> int main() { unshare(CLONE_NEWNS); pid_t pid = fork(); if (0 == pid) { char * mount_args[] = {"/bin/mount", "--make-rprivate", "/dev/sdd", "/mnt", "-o,ro", "-o,noexec", NULL}; if (0 > execv("/bin/mount", mount_args)) { perror("execv: "); exit(1); } //this line will never be reached. return 0; } else if (0 < pid) { //parent process! int status = -1; wait(&status); if (0 == status) { std::cout << "press ENTER to continue...."; std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); char * umount_args[] = {"/bin/umount", "/mnt", NULL}; if (0 > execv("/bin/umount", umount_args)) { perror("execv: "); exit(1); } } return status; } else { //fork error! perror("fork!\n"); exit(1); } return 0; } #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 int main() { 取消共享(克隆和更新); pid_t pid=fork(); 如果(0==pid) { char*mount_args[]={/bin/mount“,“-make rprivate”,“/dev/sdd”,“/mnt”,“-o,ro”,“-o,noexec”,NULL}; 如果(0>execv(“/bin/mount”,mount_args)) { 佩罗尔(“execv:”); 出口(1); } //这条线永远不会到达。 返回0; } else if(0,linux,mount,umount,Linux,Mount,Umount,但是,当我运行它时(使用-fpermissive编译后),从系统上的每个其他进程都可以看到装载 我的目标,即我的坐骑对其他用户空间进程不可见,显然没有实现 我做错了什么 编辑:这段代码在Ubuntu 16.04(内核版本4.4)上不起作用。它在Ubuntu14.04(内核版本4.2)上运行-这可能与此有关吗?结果表明,Ubuntu16中的操作系统默认装载选项已更改。为了使取消共享(2)工作,您需要在代码中添加以下行(在取消共享之前): 也许您有权挂载它(根据/etc/fstab),但不可以卸载它

但是,当我运行它时(使用-fpermissive编译后),从系统上的每个其他进程都可以看到装载

我的目标,即我的坐骑对其他用户空间进程不可见,显然没有实现

我做错了什么


编辑:这段代码在Ubuntu 16.04(内核版本4.4)上不起作用。它在Ubuntu14.04(内核版本4.2)上运行-这可能与此有关吗?

结果表明,Ubuntu16中的操作系统默认装载选项已更改。为了使取消共享(2)工作,您需要在代码中添加以下行(在取消共享之前):


也许您有权挂载它(根据
/etc/fstab
),但不可以卸载它?您没有检查
unshare()
的返回值,也没有检查
wait()
…为什么要使用
CLONE\u NEWNS
?另外,为什么不调用
umount()
?我确实有权挂载(我以root身份运行可执行文件)。我确实检查unshare()和wait()的返回值,我只是没有在这里添加它以简化代码。谢谢!在命令行上,使用
mount(8)
unshare(1)
的等效命令是:
mount--make rprivate none/&&unshare--mount程序[args]
mount("none", "/", NULL, MS_PRIVATE | MS_REC, NULL);