1 module symmetry.linux.pivot_root;
2 
3 version(PivotRoot):
4 
5 version(Posix):
6 import symmetry.sildoc;
7 	
8 	
9 	
10 @SILdoc(`
11  pivot_root() moves the root filesystem of the calling process to the
12        directory put_old and makes new_root the new root filesystem of the
13        calling process.
14 
15        The typical use of pivot_root() is during system startup, when the
16        system mounts a temporary root filesystem (e.g., an initrd), then
17        mounts the real root filesystem, and eventually turns the latter into
18        the current root of all relevant processes or threads.
19 
20        pivot_root() may or may not change the current root and the current
21        working directory of any processes or threads which use the old root
22        directory.  The caller of pivot_root() must ensure that processes
23        with root or current working directory at the old root operate
24        correctly in either case.  An easy way to ensure this is to change
25        their root and current working directory to new_root before invoking
26        pivot_root().
27 
28        The paragraph above is intentionally vague because the implementation
29        of pivot_root() may change in the future.  At the time of writing,
30        pivot_root() changes root and current working directory of each
31        process or thread to new_root if they point to the old root
32        directory.  This is necessary in order to prevent kernel threads from
33        keeping the old root directory busy with their root and current
34        working directory, even if they never access the filesystem in any
35        way.  In the future, there may be a mechanism for kernel threads to
36        explicitly relinquish any access to the filesystem, such that this
37        fairly intrusive mechanism can be removed from pivot_root().
38 
39        Note that this also applies to the calling process: pivot_root() may
40        or may not affect its current working directory.  It is therefore
41        recommended to call chdir("/") immediately after pivot_root().
42 
43        The following restrictions apply to new_root and put_old:
44 
45        -  They must be directories.
46 
47        -  new_root and put_old must not be on the same filesystem as the
48           current root.
49 
50        -  put_old must be underneath new_root, that is, adding a nonzero
51           number of /.. to the string pointed to by put_old must yield the
52           same directory as new_root.
53 
54        -  No other filesystem may be mounted on put_old.
55 
56        See also pivot_root(8) for additional usage examples.
57 
58        If the current root is not a mount point (e.g., after chroot(2) or
59        pivot_root(), see also below), not the old root directory, but the
60        mount point of that filesystem is mounted on put_old.
61 
62        new_root must be a mount point.  (If it is not otherwise a mount
63        point, it suffices to bind mount new_root on top of itself.)
64 
65        The propagation type of new_root and its parent mount must not be
66        MS_SHARED; similarly, if put_old is an existing mount point, its
67        propagation type must not be MS_SHARED.
68 RETURN VALUE         top
69        On success, zero is returned.  On error, -1 is returned, and errno is
70        set appropriately.
71 ERRORS         top
72        pivot_root() may return (in errno) any of the errors returned by
73        stat(2).  Additionally, it may return:
74 
75        EBUSY  new_root or put_old are on the current root filesystem, or a
76               filesystem is already mounted on put_old.
77 
78        EINVAL new_root is not a mount point.
79 
80        EINVAL put_old is not underneath new_root.
81 
82        EINVAL The current root is on the rootfs (initial ramfs) filesystem.
83 
84        EINVAL Either the mount point at new_root, or the parent mount of
85               that mount point, has propagation type MS_SHARED.
86 
87        EINVAL put_old is a mount point and has the propagation type
88               MS_SHARED.
89 
90        ENOTDIR
91               new_root or put_old is not a directory.
92 
93        EPERM  The calling process does not have the CAP_SYS_ADMIN
94               capability.
95 VERSIONS         top
96        pivot_root() was introduced in Linux 2.3.41.
97 CONFORMING TO         top
98        pivot_root() is Linux-specific and hence is not portable.
99 NOTES         top
100        Glibc does not provide a wrapper for this system call; call it using
101        syscall(2).
102 
103        The rootfs (initial ramfs) cannot be pivot_root()ed.  The recommended
104        method of changing the root filesystem in this case is to delete
105        everything in rootfs, overmount rootfs with the new root, attach
106        stdin/stdout/stderr to the new /dev/console, and exec the new
107        init(1).  Helper programs for this process exist; see switch_root(8).
108 BUGS         top
109        pivot_root() should not have to change root and current working
110        directory of all other processes in the system.
111 
112        Some of the more obscure uses of pivot_root() may quickly lead to
113        insanity.
114 `)
115 void pivotRoot(string newRoot, string putOld)
116 {
117 	import std.string : toStringz, fromStringz;
118 	import syscalld;
119 
120 	
121 	// nano vendor - original should be a public import!
122 	version(D_InlineAsm_X86_64)
123 	{
124 		version(linux) import syscalld.os.linux_x86_64;
125 		else version(OSX) import syscalld.os.osx_x86_64;
126 		else version(FreeBSD) import syscalld.os.freebsd_x86_64;
127 		else static assert(false, "Not supoorted your platform/architecuture.");
128 
129 		import syscalld.arch.syscall_x86_64;
130 	}
131 	else version(D_InlineAsm_X86)
132 	{
133 		version(linux) import syscalld.os.linux_x86;
134 		else static assert(false, "Not supoorted your platform/architecuture.");
135 		import syscalld.arch.syscall_x86;
136 	}
137 	syscall(PIVOT_ROOT, cast(ulong)newRoot.toStringz, cast(ulong)putOld.toStringz);
138 }
139