From 10b26851a4ab933d5af41f6e8836fed6bf62c267 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Tue, 28 Dec 2021 16:50:25 -0600 Subject: [PATCH] database: refactor mounting and unmounting /proc --- src/database.c | 63 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/src/database.c b/src/database.c index a71cd9f..f5d3f0f 100644 --- a/src/database.c +++ b/src/database.c @@ -1544,6 +1544,37 @@ static void remount_cache(struct apk_database *db) db->cache_remount_dir = NULL; } } + +static int mount_proc(struct apk_database *db) +{ + struct statfs stfs; + + /* mount /proc */ + if (asprintf(&db->root_proc_dir, "%s/proc", db->ctx->root) == -1) + return -1; + if (statfs(db->root_proc_dir, &stfs) != 0) { + if (errno == ENOENT) mkdir(db->root_proc_dir, 0555); + stfs.f_type = 0; + } + if (stfs.f_type != PROC_SUPER_MAGIC) { + mount("proc", db->root_proc_dir, "proc", 0, 0); + } else { + /* was already mounted. prevent umount on close */ + free(db->root_proc_dir); + db->root_proc_dir = NULL; + } + + return 0; +} + +static void unmount_proc(struct apk_database *db) +{ + if (db->root_proc_dir) { + umount2(db->root_proc_dir, MNT_DETACH|UMOUNT_NOFOLLOW); + free(db->root_proc_dir); + db->root_proc_dir = NULL; + } +} #else static int detect_tmpfs_root(struct apk_database *db) { @@ -1560,6 +1591,17 @@ static void remount_cache(struct apk_database *db) { (void) db; } + +static int mount_proc(struct apk_database *db) +{ + (void) db; + return 0; +} + +static void unmount_proc(struct apk_database *db) +{ + (void) db; +} #endif void apk_db_init(struct apk_database *db) @@ -1646,20 +1688,8 @@ int apk_db_open(struct apk_database *db, struct apk_ctx *ac) sigaction(SIGALRM, &old_sa, NULL); } - /* mount /proc */ - if (asprintf(&db->root_proc_dir, "%s/proc", db->ctx->root) == -1) + if (mount_proc(db) < 0) goto ret_errno; - if (statfs(db->root_proc_dir, &stfs) != 0) { - if (errno == ENOENT) mkdir(db->root_proc_dir, 0555); - stfs.f_type = 0; - } - if (stfs.f_type != PROC_SUPER_MAGIC) { - mount("proc", db->root_proc_dir, "proc", 0, 0); - } else { - /* was already mounted. prevent umount on close */ - free(db->root_proc_dir); - db->root_proc_dir = NULL; - } } blob = APK_BLOB_STR("+etc\n" "@etc/init.d\n" "!etc/apk\n"); @@ -1827,12 +1857,7 @@ void apk_db_close(struct apk_database *db) apk_hash_free(&db->installed.dirs); apk_atom_free(&db->atoms); - if (db->root_proc_dir) { - umount2(db->root_proc_dir, MNT_DETACH|UMOUNT_NOFOLLOW); - free(db->root_proc_dir); - db->root_proc_dir = NULL; - } - + unmount_proc(db); remount_cache(db); if (db->cache_fd > 0) close(db->cache_fd);