man: substantially update the docs regarding hooking sd-bus objects up with external event loops

Prompted by https://lists.freedesktop.org/archives/systemd-devel/2018-December/041817.html

This also drops all references to select() from our manpages. It's 2018
after all, people should use poll(), or ppoll() or epoll().
This commit is contained in:
Lennart Poettering 2018-12-12 12:31:05 +01:00
parent 41fc3fab1c
commit ffd1a3f688
5 changed files with 297 additions and 52 deletions

View File

@ -194,7 +194,7 @@ manpages = [
'3',
['SD_BUS_ERROR_END', 'SD_BUS_ERROR_MAP', 'sd_bus_error_map'],
''],
['sd_bus_get_fd', '3', [], ''],
['sd_bus_get_fd', '3', ['sd_bus_get_events', 'sd_bus_get_timeout'], ''],
['sd_bus_get_n_queued_read', '3', ['sd_bus_get_n_queued_write'], ''],
['sd_bus_is_open', '3', ['sd_bus_is_ready'], ''],
['sd_bus_message_append', '3', ['sd_bus_message_appendv'], ''],
@ -347,6 +347,7 @@ manpages = [
'sd_bus_track_unref',
'sd_bus_track_unrefp'],
''],
['sd_bus_wait', '3', [], ''],
['sd_event_add_child',
'3',
['sd_event_child_handler_t', 'sd_event_source_get_child_pid'],

View File

@ -71,6 +71,13 @@
<para>The <function>sd_bus_get_event()</function> returns the event loop object the specified bus object is
currently attached to, or <constant>NULL</constant> if it is currently not attached to any.</para>
<para>Note that <function>sd_bus_attach_event()</function> is only one of three supported ways to implement I/O
event handling for bus connections. Alternatively use
<citerefentry><refentrytitle>sd_bus_get_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry> for hooking up a
bus connection object with external or manual event loops. Or use
<citerefentry><refentrytitle>sd_bus_wait</refentrytitle><manvolnum>3</manvolnum></citerefentry> as a simple
synchronous, blocking I/O waiting call.</para>
</refsect1>
<refsect1>
@ -107,7 +114,8 @@
<citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_event_source_set_priority</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_set_close_on_exit</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_wait</refentrytitle><manvolnum>3</manvolnum></citerefentry>
<citerefentry><refentrytitle>sd_bus_wait</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_get_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry>
</para>
</refsect1>

View File

@ -8,7 +8,7 @@
Copyright © 2016 Julian Orth
-->
<refentry id="sd_bus_get_fd">
<refentry id="sd_bus_get_fd" xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>sd_bus_get_fd</title>
@ -22,8 +22,10 @@
<refnamediv>
<refname>sd_bus_get_fd</refname>
<refname>sd_bus_get_events</refname>
<refname>sd_bus_get_timeout</refname>
<refpurpose>Get the file descriptor connected to the message bus</refpurpose>
<refpurpose>Get the file descriptor, I/O events and time-out to wait for from a message bus object</refpurpose>
</refnamediv>
<refsynopsisdiv>
@ -34,39 +36,113 @@
<funcdef>int <function>sd_bus_get_fd</function></funcdef>
<paramdef>sd_bus *<parameter>bus</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_bus_get_events</function></funcdef>
<paramdef>sd_bus *<parameter>bus</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_bus_get_timeout</function></funcdef>
<paramdef>sd_bus *<parameter>bus</parameter></paramdef>
<paramdef>uint64_t *<parameter>timeout_usec</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
<para>
<function>sd_bus_get_fd()</function> returns the file descriptor used to
communicate with the message bus. This descriptor can be used with
<citerefentry
project='die-net'><refentrytitle>select</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry
project='die-net'><refentrytitle>poll</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
or similar functions to wait for incoming messages.
</para>
<para><function>sd_bus_get_fd()</function> returns the file descriptor used to communicate from a message bus
object. This descriptor can be used with <citerefentry
project='man-pages'><refentrytitle>poll</refentrytitle><manvolnum>3</manvolnum></citerefentry> or a similar
function to wait for I/O events on the specified bus connection object. If the bus object was configured with the
<citerefentry><refentrytitle>sd_bus_set_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry> function, then
the <parameter>input_fd</parameter> file descriptor used in that call is returned.</para>
<para>
If the bus was created with the
<citerefentry><refentrytitle>sd_bus_set_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry>
function, then the <parameter>input_fd</parameter> used in that call is
returned.
</para>
<para><function>sd_bus_get_events()</function> returns the I/O events to wait for, suitable for passing to
<function>poll()</function> or a similar call. Returns a combination of <constant>POLLIN</constant>,
<constant>POLLOUT</constant>, … events, or negative on error.</para>
<para><function>sd_bus_get_timeout()</function> returns the time-out in µs to pass to to
<function>poll()</function> or a similar call when waiting for events on the specified bus connection. The returned
time-out may be zero, in which case a subsequent I/O polling call should be invoked in non-blocking mode. The
returned timeout may be <constant>UINT64_MAX</constant> in which case the I/O polling call may block indefinitely,
without any applied time-out. Note that the returned time-out should be considered only a maximum sleeping time. It
is permissible (and even expected) that shorter time-outs are used by the calling program, in case other event
sources are polled in the same event loop. Note that the returned time-value is relative and specified in
microseconds. When converting this value in order to pass it as third argument to <function>poll()</function>
(which expects milliseconds), care should be taken to use a division that rounds up to ensure the I/O polling
operation doesn't sleep for shorter than necessary, which might result in unintended busy looping (alternatively,
use <citerefentry project='man-pages'><refentrytitle>ppoll</refentrytitle><manvolnum>3</manvolnum></citerefentry>
instead of plain <function>poll()</function>, which understands time-outs with nano-second granularity).</para>
<para>These three functions are useful to hook up a bus connection object with an external or manual event loop
involving <function>poll()</function> or a similar I/O polling call. Before each invocation of the I/O polling
call, all three functions should be invoked: the file descriptor returned by <function>sd_bus_get_fd()</function>
should be polled for the events indicated by <function>sd_bus_get_events()</function>, and the I/O call should
block for that up to the time-out returned by <function>sd_bus_get_timeout()</function>. After each I/O polling
call the bus connection needs to process incoming or outgoing data, by invoking
<citerefentry><refentrytitle>sd_bus_process</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
<para>Note that these function are only one of three supported ways to implement I/O event handling for bus
connections. Alternatively use
<citerefentry><refentrytitle>sd_bus_attach_event</refentrytitle><manvolnum>3</manvolnum></citerefentry> to attach a
bus connection to an <citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry>
event loop. Or use <citerefentry><refentrytitle>sd_bus_wait</refentrytitle><manvolnum>3</manvolnum></citerefentry>
as a simple synchronous, blocking I/O waiting call.</para>
</refsect1>
<refsect1>
<title>Return Value</title>
<para>
Returns the file descriptor used for incoming messages from the message
bus.
</para>
<para><function>sd_bus_get_fd()</function> returns the file descriptor used for communication, or a negative
<varname>errno</varname>-style error code on error.</para>
<para><function>sd_bus_get_events()</function> returns the I/O event mask to use for I/O event watching, or a
negative <varname>errno</varname>-style error code on error.</para>
<para><function>sd_bus_get_timeout()</function> returns zero or positive on success, or a negative
<varname>errno</varname>-style error code on error.</para>
</refsect1>
<refsect1>
<title>Errors</title>
<para>Returned errors may indicate the following problems:</para>
<variablelist>
<varlistentry>
<term><constant>-EINVAL</constant></term>
<listitem><para>An invalid bus object was passed.</para></listitem>
</varlistentry>
<varlistentry>
<term><constant>-ECHILD</constant></term>
<listitem><para>The bus connection was allocated in a parent process and is being reused in a child process
after <function>fork()</function>.</para></listitem>
</varlistentry>
<varlistentry>
<term><constant>-ENOTCONN</constant></term>
<listitem><para>The bus connection has been terminated.</para></listitem>
</varlistentry>
<varlistentry>
<term><constant>-EPERM</constant></term>
<listitem><para>Two distinct file descriptors were passed for input and output using
<function>sd_bus_set_fd()</function>, which <function>sd_bus_get_fd()</function> cannot
return.</para></listitem>
</varlistentry>
</variablelist>
</refsect1>
<xi:include href="libsystemd-pkgconfig.xml" />
<refsect1>
<title>See Also</title>
@ -74,6 +150,10 @@
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_set_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_process</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_attach_event</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_wait</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry project='man-pages'><refentrytitle>poll</refentrytitle><manvolnum>3</manvolnum></citerefentry>
</para>
</refsect1>

View File

@ -8,7 +8,7 @@
Copyright © 2016 Julian Orth
-->
<refentry id="sd_bus_process">
<refentry id="sd_bus_process" xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>sd_bus_process</title>
@ -33,7 +33,7 @@
<funcprototype>
<funcdef>int <function>sd_bus_process</function></funcdef>
<paramdef>sd_bus *<parameter>bus</parameter></paramdef>
<paramdef>sd_bus_message **<parameter>r</parameter></paramdef>
<paramdef>sd_bus_message **<parameter>ret</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
@ -41,49 +41,92 @@
<refsect1>
<title>Description</title>
<para>
<function>sd_bus_process()</function> drives the connection between the
message bus and the client. That is, it handles connecting,
authentication, and message processing. It should be called in a loop
until no further progress can be made or an error occurs.
<para><function>sd_bus_process()</function> drives the connection between the client and the message bus. That is,
it handles connecting, authentication, and message processing. When invoked pending I/O work is executed, and
queued incoming messages are dispatched to registered callbacks. Each time it is invoked a single operation is
executed. It returns zero when no operations were pending and positive if a message was processed. When zero is
returned the caller should synchronously poll for I/O events before calling into
<function>sd_bus_process()</function> again. For that either user the simple, synchronous
<citerefentry><refentrytitle>sd_bus_wait</refentrytitle><manvolnum>3</manvolnum></citerefentry> call, or hook up
the bus connection object to an external or manual event loop using
<citerefentry><refentrytitle>sd_bus_get_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
</para>
<para>
Once no further progress can be made,
<citerefentry><refentrytitle>sd_bus_wait</refentrytitle><manvolnum>3</manvolnum></citerefentry>
should be called. Alternatively the user can wait for incoming data on
the file descriptor returned by
<citerefentry><refentrytitle>sd_bus_get_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
</para>
<para><function>sd_bus_process()</function> processes at most one incoming message per call. If the parameter
<parameter>ret</parameter> is not <constant>NULL</constant> and the call processed a message,
<parameter>*ret</parameter> is set to this message. The caller owns a reference to this message and should call
<citerefentry><refentrytitle>sd_bus_message_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry> when the
message is no longer needed. If <parameter>ret</parameter> is not NULL, progress was made, but no message was
processed, <parameter>*ret</parameter> is set to <constant>NULL</constant>.</para>
<para>
<function>sd_bus_process</function> processes at most one incoming
message per call. If the parameter <parameter>r</parameter> is not NULL
and the call processed a message, <code>*r</code> is set to this message.
The caller owns a reference to this message and should call
<citerefentry><refentrytitle>sd_bus_message_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>
when the message is no longer needed. If <parameter>r</parameter> is not
NULL, progress was made, but no message was processed, <code>*r</code> is
set to NULL.
</para>
<para>If a the bus object is connected to an
<citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry> event loop (with
<citerefentry><refentrytitle>sd_bus_attach_event</refentrytitle><manvolnum>3</manvolnum></citerefentry>), it is not
necessary to call <function>sd_bus_process()</function> directly as it is invoked automatically when
necessary.</para>
</refsect1>
<refsect1>
<title>Return Value</title>
<para>
If progress was made, a positive integer is returned. If no progress was
made, 0 is returned. If an error occurs, a negative errno-style error code
is returned.
</para>
<para>If progress was made, a positive integer is returned. If no progress was made, 0 is returned. If an error
occurs, a negative <varname>errno</varname>-style error code is returned.</para>
</refsect1>
<refsect1>
<title>Errors</title>
<para>Returned errors may indicate the following problems:</para>
<variablelist>
<varlistentry>
<term><constant>-EINVAL</constant></term>
<listitem><para>An invalid bus object was passed.</para></listitem>
</varlistentry>
<varlistentry>
<term><constant>-ECHILD</constant></term>
<listitem><para>The bus connection was allocated in a parent process and is being reused in a child process
after <function>fork()</function>.</para></listitem>
</varlistentry>
<varlistentry>
<term><constant>-ENOTCONN</constant></term>
<listitem><para>The bus connection has been terminated already.</para></listitem>
</varlistentry>
<varlistentry>
<term><constant>-ECONNRESET</constant></term>
<listitem><para>The bus connection has been terminated just now.</para></listitem>
</varlistentry>
<varlistentry>
<term><constant>-EBUSY</constant></term>
<listitem><para>This function is already being called, i.e. <function>sd_bus_process()</function> has been
called from a callback function that itself was called by
<function>sd_bus_process()</function>.</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-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_wait</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_get_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_message_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_attach_event</refentrytitle><manvolnum>3</manvolnum></citerefentry>
</para>
</refsect1>

113
man/sd_bus_wait.xml Normal file
View File

@ -0,0 +1,113 @@
<?xml version='1.0'?>
<!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+
Copyright © 2016 Julian Orth
-->
<refentry id="sd_bus_wait" xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>sd_bus_wait</title>
<productname>systemd</productname>
</refentryinfo>
<refmeta>
<refentrytitle>sd_bus_wait</refentrytitle>
<manvolnum>3</manvolnum>
</refmeta>
<refnamediv>
<refname>sd_bus_wait</refname>
<refpurpose>Wait for I/O on a bus connection</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis>
<funcsynopsisinfo>#include &lt;systemd/sd-bus.h&gt;</funcsynopsisinfo>
<funcprototype>
<funcdef>int <function>sd_bus_wait</function></funcdef>
<paramdef>sd_bus *<parameter>bus</parameter></paramdef>
<paramdef>uint64_t <parameter>timeout_usec</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
<para><function>sd_bus_wait()</function> synchronously waits for I/O on the specified bus connection object. This
function is supposed to be called whenever
<citerefentry><refentrytitle>sd_bus_process</refentrytitle><manvolnum>3</manvolnum></citerefentry> returns zero,
indicating that no work is pending on the connection. Internally, this call invokes <citerefentry
project='man-pages'><refentrytitle>ppoll</refentrytitle><manvolnum>3</manvolnum></citerefentry>, to wait for I/O on
the bus connection. If the <parameter>timeout_sec</parameter> parameter is specified, the call will block at most
for the specified amount of time in µs. Pass <constant>UINT64_MAX</constant> to permit it to sleep
indefinitely.</para>
<para>After each invocation of <function>sd_bus_wait()</function> the <function>sd_bus_process()</function> call
should be invoked in order to process any now pending I/O work.</para>
<para>Note that <function>sd_bus_wait()</function> is suitable only for simple programs as it does not permit
waiting for other I/O events. For more complex programs either connect the bus connection object to an external
event loop using <citerefentry><refentrytitle>sd_bus_get_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry>
or to an <citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry> event loop
using
<citerefentry><refentrytitle>sd_bus_attach_event</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
</refsect1>
<refsect1>
<title>Return Value</title>
<para>If any I/O was seen, a positive value is returned, zero otherwise. If an error occurs, a negative
<varname>errno</varname>-style error code is returned.</para>
</refsect1>
<refsect1>
<title>Errors</title>
<para>Returned errors may indicate the following problems:</para>
<variablelist>
<varlistentry>
<term><constant>-EINVAL</constant></term>
<listitem><para>An invalid bus object was passed.</para></listitem>
</varlistentry>
<varlistentry>
<term><constant>-ECHILD</constant></term>
<listitem><para>The bus connection was allocated in a parent process and is being reused in a child process
after <function>fork()</function>.</para></listitem>
</varlistentry>
<varlistentry>
<term><constant>-ENOTCONN</constant></term>
<listitem><para>The bus connection has been terminated already.</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-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_process</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_get_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_bus_attach_event</refentrytitle><manvolnum>3</manvolnum></citerefentry>
</para>
</refsect1>
</refentry>