You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

105 lines
3.5 KiB

From 6fcc3befd0ca9897071af071287e8758a30046f0 Mon Sep 17 00:00:00 2001
From: Yu Watanabe <watanabe.yu+github@gmail.com>
Date: Sun, 17 Apr 2022 07:20:16 +0900
Subject: [PATCH] sd-bus: introduce ref/unref function for track_item
(cherry picked from commit c2d7dd35d2a8cda439384a385b0c1bec804b9b79)
Related: #2087652
---
src/libsystemd/sd-bus/bus-track.c | 35 ++++++++++++++-----------------
1 file changed, 16 insertions(+), 19 deletions(-)
diff --git a/src/libsystemd/sd-bus/bus-track.c b/src/libsystemd/sd-bus/bus-track.c
index 891fd0c899..135dfddc5f 100644
--- a/src/libsystemd/sd-bus/bus-track.c
+++ b/src/libsystemd/sd-bus/bus-track.c
@@ -40,7 +40,6 @@ struct sd_bus_track {
"arg0='", name, "'")
static struct track_item* track_item_free(struct track_item *i) {
-
if (!i)
return NULL;
@@ -49,7 +48,8 @@ static struct track_item* track_item_free(struct track_item *i) {
return mfree(i);
}
-DEFINE_TRIVIAL_CLEANUP_FUNC(struct track_item*, track_item_free);
+DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(struct track_item, track_item, track_item_free);
+DEFINE_TRIVIAL_CLEANUP_FUNC(struct track_item*, track_item_unref);
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(track_item_hash_ops, char, string_hash_func, string_compare_func,
struct track_item, track_item_free);
@@ -180,7 +180,7 @@ static int on_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus
}
_public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) {
- _cleanup_(track_item_freep) struct track_item *n = NULL;
+ _cleanup_(track_item_unrefp) struct track_item *n = NULL;
struct track_item *i;
const char *match;
int r;
@@ -190,14 +190,8 @@ _public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) {
i = hashmap_get(track->names, name);
if (i) {
- if (track->recursive) {
- unsigned k = i->n_ref + 1;
-
- if (k < i->n_ref) /* Check for overflow */
- return -EOVERFLOW;
-
- i->n_ref = k;
- }
+ if (track->recursive)
+ track_item_ref(i);
bus_track_remove_from_queue(track);
return 0;
@@ -207,9 +201,14 @@ _public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) {
if (r < 0)
return r;
- n = new0(struct track_item, 1);
+ n = new(struct track_item, 1);
if (!n)
return -ENOMEM;
+
+ *n = (struct track_item) {
+ .n_ref = 1,
+ };
+
n->name = strdup(name);
if (!n->name)
return -ENOMEM;
@@ -241,8 +240,7 @@ _public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) {
return r;
}
- n->n_ref = 1;
- n = NULL;
+ TAKE_PTR(n);
bus_track_remove_from_queue(track);
track->modified = true;
@@ -264,14 +262,13 @@ _public_ int sd_bus_track_remove_name(sd_bus_track *track, const char *name) {
i = hashmap_get(track->names, name);
if (!i)
return -EUNATCH;
- if (i->n_ref <= 0)
- return -EUNATCH;
- i->n_ref--;
-
- if (i->n_ref <= 0)
+ assert(i->n_ref >= 1);
+ if (i->n_ref <= 1)
return bus_track_remove_name_fully(track, name);
+ track_item_unref(i);
+
return 1;
}