summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAriadne Conill <ariadne@dereferenced.org>2021-12-28 16:50:25 -0600
committerTimo Teräs <timo.teras@iki.fi>2021-12-29 20:37:28 +0200
commit10b26851a4ab933d5af41f6e8836fed6bf62c267 (patch)
treecc35924427f7b6f8761c069d411012433a58ee16
parentdfe2e141ca10a4090f270f0d7fe1c2bd9b0f3fe2 (diff)
downloadapk-tools-10b26851a4ab933d5af41f6e8836fed6bf62c267.tar.gz
apk-tools-10b26851a4ab933d5af41f6e8836fed6bf62c267.tar.bz2
apk-tools-10b26851a4ab933d5af41f6e8836fed6bf62c267.tar.xz
apk-tools-10b26851a4ab933d5af41f6e8836fed6bf62c267.zip
database: refactor mounting and unmounting /proc
-rw-r--r--src/database.c63
1 files 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);