]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
dissect-image: introduce reference counter for DecryptedImage
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 11 Sep 2022 14:07:29 +0000 (23:07 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 17 Sep 2022 22:56:32 +0000 (07:56 +0900)
src/shared/dissect-image.c
src/shared/dissect-image.h

index 5e4883a7a7c6028bb933b4a3b1f0329f83dc746d..5e1af809975d257e5b7ad0773f0e45dba7986c10 100644 (file)
@@ -1544,19 +1544,22 @@ int dissected_image_mount_and_warn(
 }
 
 #if HAVE_LIBCRYPTSETUP
-typedef struct DecryptedPartition {
+struct DecryptedPartition {
         struct crypt_device *device;
         char *name;
         bool relinquished;
-} DecryptedPartition;
+};
+#endif
+
+typedef struct DecryptedPartition DecryptedPartition;
 
 struct DecryptedImage {
+        unsigned n_ref;
         DecryptedPartition *decrypted;
         size_t n_decrypted;
 };
-#endif
 
-DecryptedImage* decrypted_image_unref(DecryptedImage* d) {
+static DecryptedImage* decrypted_image_free(DecryptedImage *d) {
 #if HAVE_LIBCRYPTSETUP
         int r;
 
@@ -1584,7 +1587,25 @@ DecryptedImage* decrypted_image_unref(DecryptedImage* d) {
         return NULL;
 }
 
+DEFINE_TRIVIAL_REF_UNREF_FUNC(DecryptedImage, decrypted_image, decrypted_image_free);
+
 #if HAVE_LIBCRYPTSETUP
+static int decrypted_image_new(DecryptedImage **ret) {
+        _cleanup_(decrypted_image_unrefp) DecryptedImage *d = NULL;
+
+        assert(ret);
+
+        d = new(DecryptedImage, 1);
+        if (!d)
+                return -ENOMEM;
+
+        *d = (DecryptedImage) {
+                .n_ref = 1,
+        };
+
+        *ret = TAKE_PTR(d);
+        return 0;
+}
 
 static int make_dm_name_and_node(const void *original_node, const char *suffix, char **ret_name, char **ret_node) {
         _cleanup_free_ char *name = NULL, *node = NULL;
@@ -2080,9 +2101,9 @@ int dissected_image_decrypt(
         }
 
 #if HAVE_LIBCRYPTSETUP
-        d = new0(DecryptedImage, 1);
-        if (!d)
-                return -ENOMEM;
+        r = decrypted_image_new(&d);
+        if (r < 0)
+                return r;
 
         for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
                 DissectedPartition *p = m->partitions + i;
index fe478bde493b0434014f4d6b5af343500d6ab6ec..6dd52460426a6391f7169b5225073f22964861ce 100644 (file)
@@ -277,6 +277,7 @@ int dissected_image_mount_and_warn(DissectedImage *m, const char *where, uid_t u
 
 int dissected_image_acquire_metadata(DissectedImage *m, DissectImageFlags extra_flags);
 
+DecryptedImage* decrypted_image_ref(DecryptedImage *p);
 DecryptedImage* decrypted_image_unref(DecryptedImage *p);
 DEFINE_TRIVIAL_CLEANUP_FUNC(DecryptedImage*, decrypted_image_unref);
 int decrypted_image_relinquish(DecryptedImage *d);