From 7664ffbbafbe1275529fcca7936a321cb1c9e90c Mon Sep 17 00:00:00 2001 From: Nikolay Amiantov Date: Sat, 11 May 2019 11:32:47 +0300 Subject: 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. --- .../build-fhs-userenv/chrootenv/chrootenv.c | 42 +++++++++------------- 1 file changed, 16 insertions(+), 26 deletions(-) (limited to 'pkgs/build-support/build-fhs-userenv') diff --git a/pkgs/build-support/build-fhs-userenv/chrootenv/chrootenv.c b/pkgs/build-support/build-fhs-userenv/chrootenv/chrootenv.c index e57949cc44c..9a7c76b1c07 100644 --- a/pkgs/build-support/build-fhs-userenv/chrootenv/chrootenv.c +++ b/pkgs/build-support/build-fhs-userenv/chrootenv/chrootenv.c @@ -7,22 +7,16 @@ #include #include -#define fail(s, err) g_error("%s: %s: %s", __func__, s, g_strerror(err)) -#define fail_if(expr) \ - if (expr) \ - fail(#expr, errno); - -#include - #include #include #include #include #include -int min(int a, int b) { - return a > b ? b : a; -} +#define fail(s, err) g_error("%s: %s: %s", __func__, s, g_strerror(err)) +#define fail_if(expr) \ + if (expr) \ + fail(#expr, errno); const gchar *bind_blacklist[] = {"bin", "etc", "host", "usr", "lib", "lib64", "lib32", "sbin", NULL}; @@ -36,10 +30,17 @@ void mount_tmpfs(const gchar *target) { void bind_mount(const gchar *source, const gchar *target) { fail_if(g_mkdir(target, 0755)); - fail_if(mount(source, target, "bind", MS_BIND | MS_REC, NULL)); + fail_if(mount(source, target, NULL, MS_BIND | MS_REC, NULL)); +} + +const gchar *create_tmpdir() { + gchar *prefix = + g_build_filename(g_get_tmp_dir(), "chrootenvXXXXXX", NULL); + fail_if(!g_mkdtemp_full(prefix, 0755)); + return prefix; } -void pivot_host(const gchar *host, const gchar *guest) { +void pivot_host(const gchar *guest) { g_autofree gchar *point = g_build_filename(guest, "host", NULL); fail_if(g_mkdir(point, 0755)); fail_if(pivot_root(guest, point)); @@ -68,7 +69,7 @@ void bind(const gchar *host, const gchar *guest) { if (!g_strv_contains(bind_blacklist, item)) bind_mount_item(host, guest, item); - pivot_host(host, guest); + pivot_host(guest); } void spit(const char *path, char *fmt, ...) { @@ -84,11 +85,6 @@ void spit(const char *path, char *fmt, ...) { fclose(f); } -int nftw_remove(const char *path, const struct stat *sb, int type, - struct FTW *ftw) { - return remove(path); -} - int main(gint argc, gchar **argv) { const gchar *self = *argv++; @@ -97,10 +93,7 @@ int main(gint argc, gchar **argv) { return 1; } - g_autofree gchar *prefix = - g_build_filename(g_get_tmp_dir(), "chrootenvXXXXXX", NULL); - - fail_if(!g_mkdtemp_full(prefix, 0755)); + g_autofree const gchar *prefix = create_tmpdir(); pid_t cpid = fork(); @@ -136,10 +129,7 @@ int main(gint argc, gchar **argv) { int status; fail_if(waitpid(cpid, &status, 0) != cpid); - // glibc 2.27 (and possibly other versions) can't handle - // an nopenfd value larger than 2^19 - fail_if(nftw(prefix, nftw_remove, min(getdtablesize(), 1<<19), - FTW_DEPTH | FTW_MOUNT | FTW_PHYS)); + fail_if(rmdir(prefix)); if (WIFEXITED(status)) return WEXITSTATUS(status); -- cgit 1.4.1