diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-06-08 15:35:45 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-08 15:35:45 +0200 |
commit | 89278d96dcbb98ca219b233d454e18cd18501e3a (patch) | |
tree | 29c00e4da44cfd7906683101666322cfc167ef18 | |
parent | resolved: whenever a link starts/stops being relevant flush global DNS caches (diff) | |
parent | sd-event: document sd_event_source_set_io_fd_own() (diff) | |
download | systemd-89278d96dcbb98ca219b233d454e18cd18501e3a.tar.xz systemd-89278d96dcbb98ca219b233d454e18cd18501e3a.zip |
Merge pull request #9221 from poettering/bus-track-destroy
add sd_bus_track and sd_event_source destroy callbacks too
-rw-r--r-- | man/rules/meson.build | 13 | ||||
-rw-r--r-- | man/sd_bus_slot_set_destroy_callback.xml | 60 | ||||
-rw-r--r-- | man/sd_event_add_io.xml | 24 | ||||
-rw-r--r-- | man/sd_event_source_set_destroy_callback.xml | 112 | ||||
-rw-r--r-- | src/libsystemd/libsystemd.sym | 4 | ||||
-rw-r--r-- | src/libsystemd/sd-bus/bus-track.c | 30 | ||||
-rw-r--r-- | src/libsystemd/sd-event/sd-event.c | 23 | ||||
-rw-r--r-- | src/systemd/sd-bus.h | 3 | ||||
-rw-r--r-- | src/systemd/sd-event.h | 3 |
9 files changed, 247 insertions, 25 deletions
diff --git a/man/rules/meson.build b/man/rules/meson.build index efbd940fe0..04f03c6786 100644 --- a/man/rules/meson.build +++ b/man/rules/meson.build @@ -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', @@ -277,9 +280,11 @@ manpages = [ 'sd_event_source', 'sd_event_source_get_io_events', 'sd_event_source_get_io_fd', + 'sd_event_source_get_io_fd_own', 'sd_event_source_get_io_revents', 'sd_event_source_set_io_events', - 'sd_event_source_set_io_fd'], + 'sd_event_source_set_io_fd', + 'sd_event_source_set_io_fd_own'], ''], ['sd_event_add_signal', '3', @@ -314,6 +319,10 @@ manpages = [ '3', ['sd_event_source_get_description'], ''], + ['sd_event_source_set_destroy_callback', + '3', + ['sd_event_destroy_t', 'sd_event_source_get_destroy_callback'], + ''], ['sd_event_source_set_enabled', '3', ['SD_EVENT_OFF', diff --git a/man/sd_bus_slot_set_destroy_callback.xml b/man/sd_bus_slot_set_destroy_callback.xml index 6334444f9a..a54b7e2d61 100644 --- a/man/sd_bus_slot_set_destroy_callback.xml +++ b/man/sd_bus_slot_set_destroy_callback.xml @@ -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> diff --git a/man/sd_event_add_io.xml b/man/sd_event_add_io.xml index d5ef462e53..bd513a7814 100644 --- a/man/sd_event_add_io.xml +++ b/man/sd_event_add_io.xml @@ -38,6 +38,8 @@ <refname>sd_event_source_get_io_revents</refname> <refname>sd_event_source_get_io_fd</refname> <refname>sd_event_source_set_io_fd</refname> + <refname>sd_event_source_get_io_fd_own</refname> + <refname>sd_event_source_set_io_fd_own</refname> <refname>sd_event_source</refname> <refname>sd_event_io_handler_t</refname> @@ -97,6 +99,17 @@ <paramdef>int <parameter>fd</parameter></paramdef> </funcprototype> + <funcprototype> + <funcdef>int <function>sd_event_source_get_io_fd_own</function></funcdef> + <paramdef>sd_event_source *<parameter>source</parameter></paramdef> + </funcprototype> + + <funcprototype> + <funcdef>int <function>sd_event_source_set_io_fd_own</function></funcdef> + <paramdef>sd_event_source *<parameter>source</parameter></paramdef> + <paramdef>int <parameter>b</parameter></paramdef> + </funcprototype> + </funcsynopsis> </refsynopsisdiv> @@ -212,6 +225,17 @@ changes the UNIX file descriptor of an I/O event source created previously with <function>sd_event_add_io()</function>. It takes the event source object and the new file descriptor.</para> + + <para><function>sd_event_source_set_io_fd_own()</function> controls whether the file descriptor of the event source + shall be closed automatically when the event source is freed, i.e. whether it shall be considered 'owned' by the + event source object. By default it is not closed automatically, and the application has to do this on its own. The + <parameter>b</parameter> parameter is a boolean parameter: if zero, the file descriptor is not closed automatically + when the event source is freed, otherwise it is closed.</para> + + <para><function>sd_event_source_get_io_fd_own()</function> may be used to query the current setting of the file + descriptor ownership boolean flag as set with <function>sd_event_source_set_io_fd_own()</function>. It returns + positive if the file descriptor is closed automatically when the event source is destroyed, zero if not, and + negative on error.</para> </refsect1> <refsect1> diff --git a/man/sd_event_source_set_destroy_callback.xml b/man/sd_event_source_set_destroy_callback.xml new file mode 100644 index 0000000000..34f7652c86 --- /dev/null +++ b/man/sd_event_source_set_destroy_callback.xml @@ -0,0 +1,112 @@ +<?xml version='1.0'?> <!--*- Mode: nxml; nxml-child-indent: 2; indent-tabs-mode: nil -*--> +<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" +"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"> + +<!-- SPDX-License-Identifier: LGPL-2.1+ --> + +<refentry id="sd_event_source_set_destroy_callback" + xmlns:xi="http://www.w3.org/2001/XInclude"> + + <refentryinfo> + <title>sd_event_source_set_destroy_callback</title> + <productname>systemd</productname> + </refentryinfo> + + <refmeta> + <refentrytitle>sd_event_source_set_destroy_callback</refentrytitle> + <manvolnum>3</manvolnum> + </refmeta> + + <refnamediv> + <refname>sd_event_source_set_destroy_callback</refname> + <refname>sd_event_source_get_destroy_callback</refname> + <refname>sd_event_destroy_t</refname> + + <refpurpose>Define the callback function for resource cleanup.</refpurpose> + </refnamediv> + + <refsynopsisdiv> + <funcsynopsis> + <funcsynopsisinfo>#include <systemd/sd-event.h></funcsynopsisinfo> + + <funcprototype> + <funcdef>typedef int (*<function>sd_event_destroy_t</function>)</funcdef> + <paramdef>void *<parameter>userdata</parameter></paramdef> + </funcprototype> + + <funcprototype> + <funcdef>int <function>sd_event_source_set_destroy_callback</function></funcdef> + <paramdef>sd_event_source *<parameter>source</parameter></paramdef> + <paramdef>sd_event_destroy_t <parameter>callback</parameter></paramdef> + </funcprototype> + + <funcprototype> + <funcdef>int <function>sd_event_source_get_destroy_callback</function></funcdef> + <paramdef>sd_event_source *<parameter>source</parameter></paramdef> + <paramdef>sd_event_destroy_t *<parameter>callback</parameter></paramdef> + </funcprototype> + + </funcsynopsis> + </refsynopsisdiv> + + <refsect1> + <title>Description</title> + + <para><function>sd_event_source_set_destroy_callback()</function> sets <parameter>callback</parameter> as the + callback function to be called right before the event source object <parameter>source</parameter> is + deallocated. The <parameter>userdata</parameter> pointer from the event source 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_event_add_io</refentrytitle><manvolnum>3</manvolnum></citerefentry>, or directly, + see + <citerefentry><refentrytitle>sd_event_source_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 event source object itself is already invalidated, and executing + operations or taking new references to the event source object is not permissible.</para> + + <para><function>sd_event_source_get_destroy_callback()</function> returns the current callback + for <parameter>source</parameter> in the <parameter>callback</parameter> parameter.</para> + </refsect1> + + <refsect1> + <title>Return Value</title> + + <para>On success, <function>sd_event_source_set_destroy_callback()</function> returns 0 or a positive integer. On + failure, it returns a negative errno-style error code.</para> + + <para><function>sd_event_source_get_destroy_callback()</function> returns positive if the destroy callback function + is set, 0 if not. On failure, returns a negative errno-style error code.</para> + </refsect1> + + <refsect1> + <title>Errors</title> + + <para>Returned errors may indicate the following problems:</para> + + <variablelist> + <varlistentry> + <term><constant>-EINVAL</constant></term> + + <listitem><para>The <parameter>source</parameter> parameter is <constant>NULL</constant>.</para></listitem> + </varlistentry> + </variablelist> + </refsect1> + + <xi:include href="libsystemd-pkgconfig.xml" /> + + <refsect1> + <title>See Also</title> + + <para> + <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>, + <citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry>, + <citerefentry><refentrytitle>sd_event_add_io</refentrytitle><manvolnum>3</manvolnum></citerefentry>, + <citerefentry><refentrytitle>sd_event_add_time</refentrytitle><manvolnum>3</manvolnum></citerefentry>, + <citerefentry><refentrytitle>sd_event_add_signal</refentrytitle><manvolnum>3</manvolnum></citerefentry>, + <citerefentry><refentrytitle>sd_event_add_child</refentrytitle><manvolnum>3</manvolnum></citerefentry>, + <citerefentry><refentrytitle>sd_event_add_inotify</refentrytitle><manvolnum>3</manvolnum></citerefentry>, + <citerefentry><refentrytitle>sd_event_add_defer</refentrytitle><manvolnum>3</manvolnum></citerefentry>, + <citerefentry><refentrytitle>sd_event_source_set_userdata</refentrytitle><manvolnum>3</manvolnum></citerefentry> + </para> + </refsect1> + +</refentry> diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym index dc9c2d241a..270fe37e9f 100644 --- a/src/libsystemd/libsystemd.sym +++ b/src/libsystemd/libsystemd.sym @@ -565,6 +565,10 @@ 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; + sd_event_source_set_destroy_callback; + sd_event_source_get_destroy_callback; } LIBSYSTEMD_238; diff --git a/src/libsystemd/sd-bus/bus-track.c b/src/libsystemd/sd-bus/bus-track.c index 168c6c5ac7..1819eaffd4 100644 --- a/src/libsystemd/sd-bus/bus-track.c +++ b/src/libsystemd/sd-bus/bus-track.c @@ -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); + track->n_ref--; - 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); diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c index 1b983dbe37..196b90d59b 100644 --- a/src/libsystemd/sd-event/sd-event.c +++ b/src/libsystemd/sd-event/sd-event.c @@ -106,6 +106,8 @@ struct sd_event_source { uint64_t pending_iteration; uint64_t prepare_iteration; + sd_event_destroy_t destroy_callback; + LIST_FIELDS(sd_event_source, sources); union { @@ -1016,7 +1018,10 @@ static void source_free(sd_event_source *s) { source_disconnect(s); if (s->type == SOURCE_IO && s->io.owned) - safe_close(s->io.fd); + s->io.fd = safe_close(s->io.fd); + + if (s->destroy_callback) + s->destroy_callback(s->userdata); free(s->description); free(s); @@ -3775,3 +3780,19 @@ _public_ int sd_event_get_iteration(sd_event *e, uint64_t *ret) { *ret = e->iteration; return 0; } + +_public_ int sd_event_source_set_destroy_callback(sd_event_source *s, sd_event_destroy_t callback) { + assert_return(s, -EINVAL); + + s->destroy_callback = callback; + return 0; +} + +_public_ int sd_event_source_get_destroy_callback(sd_event_source *s, sd_event_destroy_t *ret) { + assert_return(s, -EINVAL); + + if (ret) + *ret = s->destroy_callback; + + return !!s->destroy_callback; +} diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h index b2394763c1..a1b1570663 100644 --- a/src/systemd/sd-bus.h +++ b/src/systemd/sd-bus.h @@ -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); diff --git a/src/systemd/sd-event.h b/src/systemd/sd-event.h index 28939ca550..fc5a75b6b6 100644 --- a/src/systemd/sd-event.h +++ b/src/systemd/sd-event.h @@ -80,6 +80,7 @@ typedef int (*sd_event_child_handler_t)(sd_event_source *s, const siginfo_t *si, typedef void* sd_event_child_handler_t; #endif typedef int (*sd_event_inotify_handler_t)(sd_event_source *s, const struct inotify_event *event, void *userdata); +typedef void (*sd_event_destroy_t)(void *userdata); int sd_event_default(sd_event **e); @@ -143,6 +144,8 @@ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock); int sd_event_source_get_signal(sd_event_source *s); int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid); int sd_event_source_get_inotify_mask(sd_event_source *s, uint32_t *ret); +int sd_event_source_set_destroy_callback(sd_event_source *s, sd_event_destroy_t callback); +int sd_event_source_get_destroy_callback(sd_event_source *s, sd_event_destroy_t *ret); /* Define helpers so that __attribute__((cleanup(sd_event_unrefp))) and similar may be used. */ _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_event, sd_event_unref); |