]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: introduce reference counting for libmnt_lock
authorKarel Zak <kzak@redhat.com>
Fri, 19 Jan 2024 11:15:48 +0000 (12:15 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 19 Jan 2024 11:15:48 +0000 (12:15 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/docs/libmount-sections.txt
libmount/src/context.c
libmount/src/libmount.h.in
libmount/src/libmount.sym
libmount/src/lock.c

index 86a762277dfcd515640c04336b02d420a4df2524..58e1b99f5da74879b3f0561f012f8aaaf24bf183 100644 (file)
@@ -302,6 +302,8 @@ libmnt_lock
 mnt_free_lock
 mnt_lock_file
 mnt_new_lock
+mnt_ref_lock
+mnt_unref_lock
 mnt_unlock_file
 mnt_lock_block_signals
 </SECTION>
index 810dd70a3c06f918909c76135cf90b72082bb06d..ed05e7ca3925939b33869ab8bd83beeee6f48b8f 100644 (file)
@@ -107,7 +107,7 @@ void mnt_free_context(struct libmnt_context *cxt)
        mnt_unref_optlist(cxt->optlist_saved);
        mnt_unref_optlist(cxt->optlist);
 
-       mnt_free_lock(cxt->lock);
+       mnt_unref_lock(cxt->lock);
        mnt_free_update(cxt->update);
 
        mnt_context_set_target_ns(cxt, NULL);
index 06c27047d2dfd5151376681ba433c685652f414a..97d151a693095e072f8c60b19aed8e415f21ddaa 100644 (file)
@@ -444,6 +444,8 @@ extern const struct libmnt_optmap *mnt_get_builtin_optmap(int id);
 /* lock.c */
 extern struct libmnt_lock *mnt_new_lock(const char *datafile, pid_t id)
                        __ul_attribute__((warn_unused_result));
+extern void mnt_ref_lock(struct libmnt_lock *ml);
+extern void mnt_unref_lock(struct libmnt_lock *ml);
 extern void mnt_free_lock(struct libmnt_lock *ml);
 
 extern void mnt_unlock_file(struct libmnt_lock *ml);
index 715bb5c5f02119070bd6a619d5d89f9a0f23c1e5..3df497bd9946d3667aff3558a96ea2fecbc7b1da 100644 (file)
@@ -375,3 +375,8 @@ MOUNT_2_39 {
        mnt_table_enable_noautofs;
        mnt_table_is_noautofs;
 } MOUNT_2_38;
+
+MOUNT_2_40 {
+       mnt_ref_lock;
+       mnt_unref_lock;
+} MOUNT_2_39;
index cbc74a6034cc265b8142ebb5848bcd92da377d3d..8aca8a7768d3af6a36517f4e3754d87441034fe4 100644 (file)
@@ -36,6 +36,7 @@
  * lock handler
  */
 struct libmnt_lock {
+       int     refcount;       /* reference counter */
        char    *lockfile;      /* path to lock file (e.g. /etc/mtab~) */
        int     lockfile_fd;    /* lock file descriptor */
 
@@ -73,6 +74,7 @@ struct libmnt_lock *mnt_new_lock(const char *datafile, pid_t id __attribute__((_
        if (!ml)
                goto err;
 
+       ml->refcount = 1;
        ml->lockfile_fd = -1;
        ml->lockfile = lo;
 
@@ -89,17 +91,56 @@ err:
  * mnt_free_lock:
  * @ml: struct libmnt_lock handler
  *
- * Deallocates mnt_lock.
+ * Deallocates libmnt_lock. This function does not care about reference count. Don't
+ * use this function directly -- it's better to use mnt_unref_lock().
+ *
+ * The reference counting is supported since util-linux v2.40.
  */
 void mnt_free_lock(struct libmnt_lock *ml)
 {
        if (!ml)
                return;
-       DBG(LOCKS, ul_debugobj(ml, "free%s", ml->locked ? " !!! LOCKED !!!" : ""));
+
+       DBG(LOCKS, ul_debugobj(ml, "free%s [refcount=%d]",
+                                       ml->locked ? " !!! LOCKED !!!" : "",
+                                       ml->refcount));
        free(ml->lockfile);
        free(ml);
 }
 
+/**
+ * mnt_ref_lock:
+ * @ml: lock pointer
+ *
+ * Increments reference counter.
+ *
+ * Since: 2.40
+ */
+void mnt_ref_lock(struct libmnt_lock *ml)
+{
+       if (ml) {
+               ml->refcount++;
+               /*DBG(FS, ul_debugobj(fs, "ref=%d", ml->refcount));*/
+       }
+}
+
+/**
+ * mnt_unref_lock:
+ * @ml: lock pointer
+ *
+ * De-increments reference counter, on zero the @ml is automatically
+ * deallocated by mnt_free_lock).
+ */
+void mnt_unref_lock(struct libmnt_lock *ml)
+{
+       if (ml) {
+               ml->refcount--;
+               /*DBG(FS, ul_debugobj(fs, "unref=%d", ml->refcount));*/
+               if (ml->refcount <= 0)
+                       mnt_free_lock(ml);
+       }
+}
+
 /**
  * mnt_lock_block_signals:
  * @ml: struct libmnt_lock handler
@@ -286,7 +327,7 @@ static void clean_lock(void)
        if (!lock)
                return;
        mnt_unlock_file(lock);
-       mnt_free_lock(lock);
+       mnt_unref_lock(lock);
 }
 
 static void __attribute__((__noreturn__)) sig_handler(int sig)
@@ -367,7 +408,7 @@ static int test_lock(struct libmnt_test *ts __attribute__((unused)),
                increment_data(datafile, verbose, l);
 
                mnt_unlock_file(lock);
-               mnt_free_lock(lock);
+               mnt_unref_lock(lock);
                lock = NULL;
 
                /* The mount command usually finishes after a mtab update. We