sd-bus: also add destroy callbacks to sd_bus_track objects

This augments previous work for this for sd_bus_slot objects, and adds
the same concept to sd_bus_track objects, too.
This commit is contained in:
Lennart Poettering 2018-06-07 12:29:52 +02:00
parent 6a1bae8381
commit 66223497d0
5 changed files with 77 additions and 23 deletions

View File

@ -233,7 +233,10 @@ manpages = [
['sd_bus_set_watch_bind', '3', ['sd_bus_get_watch_bind'], ''],
['sd_bus_slot_set_destroy_callback',
'3',
['sd_bus_slot_get_destroy_callback'],
['sd_bus_destroy_t',
'sd_bus_slot_get_destroy_callback',
'sd_bus_track_get_destroy_callback',
'sd_bus_track_set_destroy_callback'],
''],
['sd_bus_slot_set_floating', '3', ['sd_bus_slot_get_floating'], ''],
['sd_bus_track_add_name',

View File

@ -20,6 +20,9 @@
<refnamediv>
<refname>sd_bus_slot_set_destroy_callback</refname>
<refname>sd_bus_slot_get_destroy_callback</refname>
<refname>sd_bus_track_set_destroy_callback</refname>
<refname>sd_bus_track_get_destroy_callback</refname>
<refname>sd_bus_destroy_t</refname>
<refpurpose>Define the callback function for resource cleanup.</refpurpose>
</refnamediv>
@ -44,35 +47,54 @@
<paramdef>sd_bus_slot *<parameter>slot</parameter></paramdef>
<paramdef>sd_bus_destroy_t *<parameter>callback</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_bus_track_set_destroy_callback</function></funcdef>
<paramdef>sd_bus_track *<parameter>track</parameter></paramdef>
<paramdef>sd_bus_destroy_t <parameter>callback</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_bus_track_get_destroy_callback</function></funcdef>
<paramdef>sd_bus_track *<parameter>track</parameter></paramdef>
<paramdef>sd_bus_destroy_t *<parameter>callback</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
<para><function>sd_bus_slot_set_destroy_callback()</function> sets
<parameter>callback</parameter> as the callback function to be called right before the bus slot
object <parameter>slot</parameter> is deallocated. The <parameter>userdata</parameter> pointer
from the slot object will be passed as the <parameter>userdata</parameter> parameter. This
pointer can be set specified as an argument to the constuctor functions, see
<citerefentry><refentrytitle>sd_bus_add_match</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
or directly, see
<citerefentry><refentrytitle>sd_bus_set_userdata</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
This callback function is called even if <parameter>userdata</parameter> is
<constant>NULL</constant>.</para>
<para><function>sd_bus_slot_set_destroy_callback()</function> sets <parameter>callback</parameter> as the callback
function to be called right before the bus slot object <parameter>slot</parameter> is deallocated. The
<parameter>userdata</parameter> pointer from the slot object will be passed as the <parameter>userdata</parameter>
parameter. This pointer can be set by an argument to the constructor functions, see
<citerefentry><refentrytitle>sd_bus_add_match</refentrytitle><manvolnum>3</manvolnum></citerefentry>, or directly,
see <citerefentry><refentrytitle>sd_bus_slot_set_userdata</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
This callback function is called even if <parameter>userdata</parameter> is <constant>NULL</constant>. Note that
this callback is invoked at a time where the bus slot object itself is already invalidated, and executing
operations or taking new references to the bus slot object is not permissible.</para>
<para><function>sd_bus_slot_get_destroy_callback()</function> returns the current callback
for <parameter>slot</parameter> in the <parameter>callback</parameter> parameter.</para>
<para><function>sd_bus_track_set_destroy_callback()</function> and
<function>sd_bus_track_get_destroy_callback</function> provide equivalent functionality for the
<parameter>userdata</parameter> pointer associated with bus peer tracking objects. For details about bus peer
tracking objects, see
<citerefentry><refentrytitle>sd_bus_track_new</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
</refsect1>
<refsect1>
<title>Return Value</title>
<para>On success, <function>sd_bus_slot_set_destroy_callback()</function> returns 0 or a
positive integer. On failure, it returns a negative errno-style error code.</para>
<para>On success, <function>sd_bus_slot_set_destroy_callback()</function> and
<function>sd_bus_track_set_destroy_callback()</function> return 0 or a positive integer. On failure, they return a
negative errno-style error code.</para>
<para><function>sd_bus_slot_get_destroy_callback()</function> returns 1 if the destroy callback
function is set, 0 if not. On failure, it returns a negative errno-style error code.</para>
<para><function>sd_bus_slot_get_destroy_callback()</function> and
<function>sd_bus_track_get_destroy_callback</function> return positive if the destroy callback function is set, 0
if not. On failure, they return a negative errno-style error code.</para>
</refsect1>
<refsect1>
@ -84,8 +106,8 @@
<varlistentry>
<term><constant>-EINVAL</constant></term>
<listitem><para>The <parameter>slot</parameter> parameter is <constant>NULL</constant>.
</para></listitem>
<listitem><para>The <parameter>slot</parameter> or <parameter>track</parameter> parameter is
<constant>NULL</constant>.</para></listitem>
</varlistentry>
</variablelist>
</refsect1>
@ -98,7 +120,11 @@
<para>
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_slot_set_floating</refentrytitle><manvolnum>3</manvolnum></citerefentry>
<citerefentry><refentrytitle>sd_bus_slot_set_floating</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_add_match</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_track_new</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_slot_set_userdata</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_track_set_userdata</refentrytitle><manvolnum>3</manvolnum></citerefentry>
</para>
</refsect1>

View File

@ -565,6 +565,8 @@ global:
sd_bus_slot_set_floating;
sd_bus_slot_get_destroy_callback;
sd_bus_slot_set_destroy_callback;
sd_bus_track_get_destroy_callback;
sd_bus_track_set_destroy_callback;
sd_event_add_inotify;
sd_event_source_get_inotify_mask;
} LIBSYSTEMD_238;

View File

@ -31,6 +31,7 @@ struct sd_bus_track {
bool in_queue:1; /* In bus->track_queue? */
bool modified:1;
bool recursive:1;
sd_bus_destroy_t destroy_callback;
LIST_FIELDS(sd_bus_track, tracks);
};
@ -164,18 +165,21 @@ _public_ sd_bus_track* sd_bus_track_unref(sd_bus_track *track) {
return NULL;
assert(track->n_ref > 0);
if (track->n_ref > 1) {
track->n_ref--;
if (track->n_ref > 0)
return NULL;
}
if (track->in_list)
LIST_REMOVE(tracks, track->bus->tracks, track);
bus_track_remove_from_queue(track);
hashmap_free_with_destructor(track->names, track_item_free);
sd_bus_unref(track->bus);
track->names = hashmap_free_with_destructor(track->names, track_item_free);
track->bus = sd_bus_unref(track->bus);
if (track->destroy_callback)
track->destroy_callback(track->userdata);
return mfree(track);
}
@ -439,6 +443,22 @@ _public_ void *sd_bus_track_set_userdata(sd_bus_track *track, void *userdata) {
return ret;
}
_public_ int sd_bus_track_set_destroy_callback(sd_bus_track *track, sd_bus_destroy_t callback) {
assert_return(track, -EINVAL);
track->destroy_callback = callback;
return 0;
}
_public_ int sd_bus_track_get_destroy_callback(sd_bus_track *track, sd_bus_destroy_t *ret) {
assert_return(track, -EINVAL);
if (ret)
*ret = track->destroy_callback;
return !!track->destroy_callback;
}
_public_ int sd_bus_track_set_recursive(sd_bus_track *track, int b) {
assert_return(track, -EINVAL);

View File

@ -485,6 +485,9 @@ const char* sd_bus_track_contains(sd_bus_track *track, const char *name);
const char* sd_bus_track_first(sd_bus_track *track);
const char* sd_bus_track_next(sd_bus_track *track);
int sd_bus_track_set_destroy_callback(sd_bus_track *s, sd_bus_destroy_t callback);
int sd_bus_track_get_destroy_callback(sd_bus_track *s, sd_bus_destroy_t *ret);
/* Define helpers so that __attribute__((cleanup(sd_bus_unrefp))) and similar may be used. */
_SD_DEFINE_POINTER_CLEANUP_FUNC(sd_bus, sd_bus_unref);
_SD_DEFINE_POINTER_CLEANUP_FUNC(sd_bus, sd_bus_flush_close_unref);