git.iko.soy/typeable/nixpkgs
on master help sign in
chrootenv: small improvements

* Remove unused argument from pivot_root; * Factor out tmpdir creation into a separate function; * Remove unused fstype from bind mount; * Use unlink instead of a treewalk to remove empty temporary directory.

commit7664ffbbafbe1275529fcca7936a321cb1c9e90c authorNikolay Amiantov <ab@fmap.me> dateSat 11 May 2019, 11:32 (8 years ago) parente0d3bc2
1 file changed+16−26
pkgs/build-support/build-fhs-userenv/chrootenv/chrootenv.c+16−26
pkgs/build-support/build-fhs-userenv/chrootenv/chrootenv.c+16−26
@@ -7,22 +7,16 @@
77#include <sched.h>
88#include <unistd.h>
99
10−#define fail(s, err) g_error("%s: %s: %s", __func__, s, g_strerror(err))
11−#define fail_if(expr) \
12− if (expr) \
13− fail(#expr, errno);
14−
15−#include <ftw.h>
16−
1710#include <sys/mount.h>
1811#include <sys/stat.h>
1912#include <sys/types.h>
2013#include <sys/wait.h>
2114#include <sys/syscall.h>
2215
23−int min(int a, int b) {
24− return a > b ? b : a;
25−}
16+#define fail(s, err) g_error("%s: %s: %s", __func__, s, g_strerror(err))
17+#define fail_if(expr) \
18+ if (expr) \
19+ fail(#expr, errno);
2620
2721const gchar *bind_blacklist[] = {"bin", "etc", "host", "usr", "lib", "lib64", "lib32", "sbin", NULL};
2822
@@ -36,10 +30,17 @@ void mount_tmpfs(const gchar *target) {
3630
3731void bind_mount(const gchar *source, const gchar *target) {
3832 fail_if(g_mkdir(target, 0755));
39− fail_if(mount(source, target, "bind", MS_BIND | MS_REC, NULL));
33+ fail_if(mount(source, target, NULL, MS_BIND | MS_REC, NULL));
34+}
35+
36+const gchar *create_tmpdir() {
37+ gchar *prefix =
38+ g_build_filename(g_get_tmp_dir(), "chrootenvXXXXXX", NULL);
39+ fail_if(!g_mkdtemp_full(prefix, 0755));
40+ return prefix;
4041}
4142
42−void pivot_host(const gchar *host, const gchar *guest) {
43+void pivot_host(const gchar *guest) {
4344 g_autofree gchar *point = g_build_filename(guest, "host", NULL);
4445 fail_if(g_mkdir(point, 0755));
4546 fail_if(pivot_root(guest, point));
@@ -68,7 +69,7 @@ void bind(const gchar *host, const gchar *guest) {
6869 if (!g_strv_contains(bind_blacklist, item))
6970 bind_mount_item(host, guest, item);
7071
71− pivot_host(host, guest);
72+ pivot_host(guest);
7273}
7374
7475void spit(const char *path, char *fmt, ...) {
@@ -84,11 +85,6 @@ void spit(const char *path, char *fmt, ...) {
8485 fclose(f);
8586}
8687
87−int nftw_remove(const char *path, const struct stat *sb, int type,
88− struct FTW *ftw) {
89− return remove(path);
90−}
91−
9288int main(gint argc, gchar **argv) {
9389 const gchar *self = *argv++;
9490
@@ -97,10 +93,7 @@ int main(gint argc, gchar **argv) {
9793 return 1;
9894 }
9995
100− g_autofree gchar *prefix =
101− g_build_filename(g_get_tmp_dir(), "chrootenvXXXXXX", NULL);
102−
103− fail_if(!g_mkdtemp_full(prefix, 0755));
96+ g_autofree const gchar *prefix = create_tmpdir();
10497
10598 pid_t cpid = fork();
10699
@@ -136,10 +129,7 @@ int main(gint argc, gchar **argv) {
136129 int status;
137130
138131 fail_if(waitpid(cpid, &status, 0) != cpid);
139− // glibc 2.27 (and possibly other versions) can't handle
140− // an nopenfd value larger than 2^19
141− fail_if(nftw(prefix, nftw_remove, min(getdtablesize(), 1<<19),
142− FTW_DEPTH | FTW_MOUNT | FTW_PHYS));
132+ fail_if(rmdir(prefix));
143133
144134 if (WIFEXITED(status))
145135 return WEXITSTATUS(status);