-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathswitchroot.c
More file actions
232 lines (175 loc) · 5.17 KB
/
switchroot.c
File metadata and controls
232 lines (175 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <bits/magic.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/fpath.h>
#include <sys/dents.h>
#include <sys/creds.h>
#include <sys/mount.h>
#include <sys/statfs.h>
#include <format.h>
#include <string.h>
#include <errtag.h>
#include <util.h>
/* Note this *may* happen to run with empty fds 0-2, but since it never
opens anything r/w itself it's ok. At worst it will try to write its
errors into a read-only fd pointing to a directory, failing silently. */
ERRTAG("switchroot");
ERRLIST(NEACCES NEBADF NEFAULT NEINTR NEIO NELOOP NENOENT NENOMEM
NENOSYS NENOTDIR NEINVAL NENOTDIR NEBUSY NEISDIR NEPERM NEROFS);
#define DEBUFSIZE 2000
struct root {
uint64_t olddev;
uint64_t newdev;
char* newroot;
int newrlen;
};
static void delete_ent(struct root* ctx, char* dir, int dirfd, struct dirent* de);
/* Directory tree recursion is done here in (atfd, path) mode.
The actualy file ops are done via the directory fd, but proper
absolute path is also kept for error reporting and in this case
for mount() calls. */
static void delete_rec(struct root* ctx, int dirfd, char* dir)
{
char debuf[DEBUFSIZE];
int delen = sizeof(debuf);
long rd;
while((rd = sys_getdents(dirfd, debuf, delen)) > 0)
{
char* ptr = debuf;
char* end = debuf + rd;
while(ptr < end)
{
struct dirent* de = (struct dirent*) ptr;
ptr += de->reclen;
if(!de->reclen)
break;
if(dotddot(de->name))
continue;
delete_ent(ctx, dir, dirfd, de);
}
};
};
static void make_path(char* buf, int len, char* dir, char* name)
{
char* p = buf;
char* e = buf + len - 1;
p = fmtstr(p, e, dir);
p = fmtstr(p, e, "/");
p = fmtstr(p, e, name);
*p = '\0';
}
static void move_mount(struct root* ctx, char* path)
{
char newpath[ctx->newrlen + strlen(path) + 5];
char* p = newpath;
char* e = newpath + sizeof(newpath) - 1;
p = fmtstr(p, e, ctx->newroot);
p = fmtstr(p, e, path);
*p = '\0';
int ret;
if((ret = sys_mount(path, newpath, NULL, MS_MOVE, NULL)) >= 0)
return;
warn("mount", newpath, ret);
if((ret = sys_umount(path, MNT_DETACH)) >= 0)
return;
warn("umount", path, ret);
}
/* This one gets de@dirfd = "$dir/$de.name" but we don't know what kind
of direntry it is yet. There is no need to call statat() however.
We can just try to unlink it as a file, and if it happens to be a dir,
unlink() whould fail with EISDIR.
The check may be skipped if getdents promises us it's a dir. */
static void delete_ent(struct root* ctx, char* dir, int dirfd, struct dirent* de)
{
int fd, ret;
struct stat st;
char* name = de->name;
char path[strlen(dir) + strlen(name) + 5];
make_path(path, sizeof(path), dir, name);
if(de->type == DT_DIR)
goto dir;
if((ret = sys_unlinkat(dirfd, name, 0)) >= 0)
return;
if(ret != EISDIR)
goto err;
dir:
if((fd = sys_openat(dirfd, name, O_DIRECTORY)) < 0)
return;
if((ret = sys_fstatat(fd, "", &st, AT_EMPTY_PATH)) < 0)
goto out;
if(st.dev == ctx->olddev) {
delete_rec(ctx, fd, path);
ret = sys_unlinkat(dirfd, name, AT_REMOVEDIR);
} else if(st.dev == ctx->newdev) {
/* do nothing */
ret = 0;
} else {
move_mount(ctx, path);
ret = 0;
}
out:
sys_close(fd);
err:
if(ret) warn(NULL, path, ret);
}
/* Before starting directory tree recursion, we need to figure out
what the boundaries are. Only the stuff on oldroot should be unlinked,
and anything that's neither oldroot nor newroot has to be move-mounted
onto the newroot.
The tricky part: if newroot is not mounted directly under oldroot,
the move-mount code will try to move newroot's parent into newroot. */
static int stat_old_new_root(struct root* ctx, char* newroot)
{
struct stat st;
ctx->newroot = newroot;
ctx->newrlen = strlen(newroot);
xchk(sys_stat(newroot, &st), "stat", newroot);
ctx->newdev = st.dev;
int fd = xchk(sys_open("/", O_DIRECTORY), "open", "/");
xchk(sys_fstatat(fd, "", &st, AT_EMPTY_PATH), "stat", "/");
ctx->olddev = st.dev;
if(ctx->newdev == ctx->olddev)
fail("new root is on the same fs", NULL, 0);
/* . = newroot, so .. is its parent directory */
xchk(sys_stat("..", &st), "stat", "..");
if(st.dev != ctx->olddev)
fail(newroot, "is not directly under /", 0);
return fd;
}
/* Avoid deleting any actual permanent files as hard as possible. */
static int check_ramfs(void)
{
struct statfs st;
if(sys_statfs("/", &st) < 0)
return -1;
if(st.type != RAMFS_MAGIC && st.type != TMPFS_MAGIC)
return -1;
return 0;
};
static void changeroot(char* newroot)
{
struct root ctx;
if(sys_getpid() != 1)
fail("not running as pid 1", NULL, 0);
if(check_ramfs())
fail("not running on ramfs", NULL, 0);
xchk(sys_chdir(newroot), "chdir", newroot);
int rfd = stat_old_new_root(&ctx, newroot);
delete_rec(&ctx, rfd, "");
sys_close(rfd);
xchk(sys_mount(".", "/", NULL, MS_MOVE, NULL), "mount", ". to /");
xchk(sys_chroot("."), "chroot", ".");
xchk(sys_chdir("/"), "chdir", "/");
}
/* Usage: switchroot /newroot /sbin/system/start ... */
int main(int argc, char** argv)
{
if(argc < 2)
fail("no newroot to switch to", NULL, 0);
if(argc < 3)
fail("need executable to invoke on the new root", NULL, 0);
changeroot(argv[1]);
argv += 2;
long ret = sys_execve(*argv, argv, NULL);
fail("cannot exec", *argv, ret);
}