1
0
mirror of https://github.com/systemd/systemd synced 2024-07-08 20:15:55 +00:00

Merge pull request #32720 from poettering/hostnamed-no-varlink-exit-on-idle

hostnamed: exit-on-idle tweaks
This commit is contained in:
Yu Watanabe 2024-06-12 18:25:24 +09:00 committed by GitHub
commit f7da67db58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 162 additions and 10 deletions

View File

@ -418,6 +418,7 @@ manpages = [
'3',
['sd_bus_path_decode', 'sd_bus_path_decode_many', 'sd_bus_path_encode_many'],
''],
['sd_bus_pending_method_calls', '3', [], ''],
['sd_bus_process', '3', [], ''],
['sd_bus_query_sender_creds', '3', ['sd_bus_query_sender_privilege'], ''],
['sd_bus_reply_method_error',

View File

@ -0,0 +1,88 @@
<?xml version='1.0'?>
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
<refentry id="sd_bus_pending_method_calls"
xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>sd_bus_pending_method_calls</title>
<productname>systemd</productname>
</refentryinfo>
<refmeta>
<refentrytitle>sd_bus_pending_method_calls</refentrytitle>
<manvolnum>3</manvolnum>
</refmeta>
<refnamediv>
<refname>sd_bus_pending_method_calls</refname>
<refpurpose>Return the number of currently pending, outgoing method calls</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcsynopsisinfo>#include &lt;systemd/sd-bus.h&gt;</funcsynopsisinfo>
<funcprototype>
<funcdef>int <function>sd_bus_pending_method_calls</function></funcdef>
<paramdef>sd_bus *<parameter>bus</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
<para><function>sd_bus_pending_method_calls()</function> returns the number of currently pending outgoing
method calls, i.e. method calls enqueued with
<citerefentry><refentrytitle>sd_bus_call_async</refentrytitle><manvolnum>3</manvolnum></citerefentry> for
which no reply has been received yet, and which have not reached a time-out yet.</para>
<para>The <parameter>bus</parameter> argument may be <constant>NULL</constant>, in which case zero is
returned.</para>
</refsect1>
<refsect1>
<title>Return Value</title>
<para>This function returns 0 if there are no pending method calls, or a <constant>NULL</constant> bus
object was specified. On failure, a negative errno-style error code is returned.</para>
<refsect2>
<title>Errors</title>
<para>Returned errors may indicate the following problems:</para>
<variablelist>
<varlistentry>
<term><constant>-ECHILD</constant></term>
<listitem><para>The bus connection has been created in a different process, library or module instance.</para></listitem>
</varlistentry>
</variablelist>
</refsect2>
</refsect1>
<xi:include href="libsystemd-pkgconfig.xml" />
<refsect1>
<title>History</title>
<para><function>sd_bus_pending_method_calls()</function> was added in version 257.</para>
</refsect1>
<refsect1>
<title>See Also</title>
<para><simplelist type="inline">
<member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
<member><citerefentry><refentrytitle>sd-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
<member><citerefentry><refentrytitle>sd_bus_call_async</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
</simplelist></para>
</refsect1>
</refentry>

View File

@ -1682,6 +1682,13 @@ static int connect_varlink(Context *c) {
return 0;
}
static bool context_check_idle(void *userdata) {
Context *c = ASSERT_PTR(userdata);
return varlink_server_current_connections(c->varlink_server) == 0 &&
hashmap_isempty(c->polkit_registry);
}
static int run(int argc, char *argv[]) {
_cleanup_(context_destroy) Context context = {
.hostname_source = _HOSTNAME_INVALID, /* appropriate value will be set later */
@ -1731,8 +1738,8 @@ static int run(int argc, char *argv[]) {
context.bus,
"org.freedesktop.hostname1",
DEFAULT_EXIT_USEC,
/* check_idle= */ NULL,
/* userdata= */ NULL);
context_check_idle,
&context);
if (r < 0)
return log_error_errno(r, "Failed to run event loop: %m");

View File

@ -1728,9 +1728,10 @@ static int manager_add_bus_objects(Manager *m) {
}
static bool manager_check_idle(void *userdata) {
Manager *m = userdata;
Manager *m = ASSERT_PTR(userdata);
return hashmap_isempty(m->transfers);
return hashmap_isempty(m->transfers) &&
hashmap_isempty(m->polkit_registry);
}
static void manager_parse_env(Manager *m) {

View File

@ -843,3 +843,8 @@ global:
sd_journal_stream_fd_with_namespace;
sd_event_source_get_inotify_path;
} LIBSYSTEMD_255;
LIBSYSTEMD_257 {
global:
sd_bus_pending_method_calls;
} LIBSYSTEMD_256;

View File

@ -4471,3 +4471,21 @@ _public_ int sd_bus_enqueue_for_read(sd_bus *bus, sd_bus_message *m) {
bus->rqueue[bus->rqueue_size++] = bus_message_ref_queued(m, bus);
return 0;
}
_public_ int sd_bus_pending_method_calls(sd_bus *bus) {
/* Returns the number of currently pending asynchronous method calls. This is graceful, i.e. an
* unallocated (i.e. NULL) bus connection has no method calls pending. */
if (!bus)
return 0;
assert_return(bus = bus_resolve(bus), -ENOPKG);
if (!BUS_IS_OPEN(bus->state))
return 0;
size_t n = ordered_hashmap_size(bus->reply_callbacks);
return n > INT_MAX ? INT_MAX : (int) n; /* paranoid overflow check */
}

View File

@ -622,6 +622,12 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
return 0;
}
static bool context_check_idle(void *userdata) {
Context *c = ASSERT_PTR(userdata);
return hashmap_isempty(c->polkit_registry);
}
static int run(int argc, char *argv[]) {
_cleanup_(context_clear) Context context = {};
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
@ -662,7 +668,13 @@ static int run(int argc, char *argv[]) {
if (r < 0)
log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
r = bus_event_loop_with_idle(event, bus, "org.freedesktop.locale1", DEFAULT_EXIT_USEC, NULL, NULL);
r = bus_event_loop_with_idle(
event,
bus,
"org.freedesktop.locale1",
DEFAULT_EXIT_USEC,
context_check_idle,
&context);
if (r < 0)
return log_error_errno(r, "Failed to run event loop: %m");

View File

@ -311,7 +311,7 @@ static int manager_startup(Manager *m) {
}
static bool check_idle(void *userdata) {
Manager *m = userdata;
Manager *m = ASSERT_PTR(userdata);
if (m->operations)
return false;
@ -322,6 +322,9 @@ static bool check_idle(void *userdata) {
if (varlink_server_current_connections(m->varlink_machine_server) > 0)
return false;
if (!hashmap_isempty(m->polkit_registry))
return false;
manager_gc(m, true);
return hashmap_isempty(m->machines);

View File

@ -117,9 +117,10 @@ static int manager_startup(Manager *m) {
}
static bool check_idle(void *userdata) {
Manager *m = userdata;
Manager *m = ASSERT_PTR(userdata);
return !m->operations;
return !m->operations &&
hashmap_isempty(m->polkit_registry);
}
static int run(int argc, char *argv[]) {

View File

@ -122,7 +122,9 @@ int bus_event_loop_with_idle(
if (r == SD_EVENT_FINISHED)
break;
if (check_idle)
if (sd_bus_pending_method_calls(bus) > 0)
idle = false;
else if (check_idle)
idle = check_idle(userdata);
else
idle = true;

View File

@ -238,6 +238,8 @@ int sd_bus_add_fallback_vtable(sd_bus *bus, sd_bus_slot **slot, const char *pref
int sd_bus_add_node_enumerator(sd_bus *bus, sd_bus_slot **slot, const char *path, sd_bus_node_enumerator_t callback, void *userdata);
int sd_bus_add_object_manager(sd_bus *bus, sd_bus_slot **slot, const char *path);
int sd_bus_pending_method_calls(sd_bus *bus);
/* Slot object */
sd_bus_slot* sd_bus_slot_ref(sd_bus_slot *slot);

View File

@ -1118,6 +1118,12 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
return 0;
}
static bool context_check_idle(void *userdata) {
Context *c = ASSERT_PTR(userdata);
return hashmap_isempty(c->polkit_registry);
}
static int run(int argc, char *argv[]) {
_cleanup_(context_clear) Context context = {};
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
@ -1164,7 +1170,13 @@ static int run(int argc, char *argv[]) {
if (r < 0)
log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
r = bus_event_loop_with_idle(event, bus, "org.freedesktop.timedate1", DEFAULT_EXIT_USEC, NULL, NULL);
r = bus_event_loop_with_idle(
event,
bus,
"org.freedesktop.timedate1",
DEFAULT_EXIT_USEC,
context_check_idle,
&context);
if (r < 0)
return log_error_errno(r, "Failed to run event loop: %m");