Release recovery image on destructor 15/246815/1
authorKichan Kwon <k_c.kwon@samsung.com>
Tue, 27 Oct 2020 07:15:04 +0000 (16:15 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Wed, 4 Nov 2020 08:14:27 +0000 (17:14 +0900)
Change-Id: Ic9e05d7785bd73e317d48395ee4c314434c0a19c
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
src/system-recovery.c

index d5931d8a095ab40fa167cc2f7646683a02dbb294..a49be2ef50d489576269386eac5b9614db79f8f0 100644 (file)
 #include "system-recovery.h"
 
 
+static int loop_fd = -1;
+
+static int recovery_image_mounted;
 static char recovery_image_path[PATH_MAX - FIELD_LENGTH];
 
+static unsigned long long total_image_size;
+
 
 #ifdef LOG_FILE
 /**
@@ -49,6 +54,23 @@ static int write_log_file(void)
 }
 #endif
 
+static int release_recovery_image(void)
+{
+       int ret;
+
+       if (recovery_image_mounted) {
+               ret = umount(RECOVERY_IMAGE_MOUNTPOINT);
+               ASSERT_RETV(ret == 0, errno, "Failed to unmount recovery image (%d)", errno);
+       }
+
+       if (loop_fd >= 0) {
+               ret = ioctl(loop_fd, LOOP_CLR_FD, 0);
+               ASSERT_RETV(ret == 0, errno, "Failed to clear loop device (%d)", errno);
+       }
+
+       return SUCCEED;
+}
+
 __attribute__((destructor)) static void __fini__(void)
 {
        int ret = SUCCEED;
@@ -58,9 +80,11 @@ __attribute__((destructor)) static void __fini__(void)
        if (ret != SUCCEED)
                _W("Failed to write log file (%d)", ret);
 #endif
-}
 
-static unsigned long long total_image_size;
+       ret = release_recovery_image();
+       if (ret != SUCCEED)
+               _W("Failed to release recovery image (%d)", ret);
+}
 
 
 static int find_recovery_image(void)
@@ -176,7 +200,6 @@ static int mount_recovery_image(void)
 {
        int ret;
        _CLEANUP_FD_ int loopctl_fd = -1;
-       _CLEANUP_FD_ int loop_fd = -1;
        _CLEANUP_FD_ int img_fd = -1;
        int loop_idx;
        char loop_device_path[32];
@@ -210,6 +233,7 @@ static int mount_recovery_image(void)
                        "squashfs", MS_RDONLY, NULL);
        ASSERT_RETV(ret == 0, errno, "Failed to mount recovery image (%d)", errno);
 
+       recovery_image_mounted = 1;
        return SUCCEED;
 }