chroot是一个系统调用,能改变一个进程的根目录。linux
一个进程有两个目录:工做目录和根目录。工做目录(working directory)也叫当前目录, 随cd命令常常发生变化,而通常状况下,全部进程看到的根路径都是/
。不考虑权限问题,整个的文件系统树对全部进程都是可见的。假如,管理员要把一个进程限制某个目录下面,这个进程能够是一个测试程序,就要用到chroot。数据结构关系以下:数据结构
include/linux/sched.h: struct task_struct { ... 771 /* Filesystem information: */ 772 struct fs_struct *fs; ... }
且测试
include/linux/fs_struct.h: struct fs_struct { ... struct path root, pwd; }
注意,使用chroot并非件容易的事情,由于执行chroot后,原来系统中的命令和动态库等都不可用了,因此要预先准备好环境再切换。chroot系统调用只要改变进程根目录就能够了,很简单。code
fs/open.c: 482 SYSCALL_DEFINE1(chroot, const char __user *, filename) 483 { ... 503 set_fs_root(current->fs, &path); ...