| 1 |
<?xml version='1.0'?> |
| 2 |
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" |
| 3 |
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"> |
| 4 |
|
| 5 |
<!-- |
| 6 |
SPDX-License-Identifier: LGPL-2.1-or-later |
| 7 |
|
| 8 |
Copyright © 2016 Julian Orth |
| 9 |
--> |
| 10 |
|
| 11 |
<refentry id="sd_bus_get_fd" xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 12 |
|
| 13 |
<refentryinfo> |
| 14 |
<title>sd_bus_get_fd</title> |
| 15 |
<productname>systemd</productname> |
| 16 |
</refentryinfo> |
| 17 |
|
| 18 |
<refmeta> |
| 19 |
<refentrytitle>sd_bus_get_fd</refentrytitle> |
| 20 |
<manvolnum>3</manvolnum> |
| 21 |
</refmeta> |
| 22 |
|
| 23 |
<refnamediv> |
| 24 |
<refname>sd_bus_get_fd</refname> |
| 25 |
<refname>sd_bus_get_events</refname> |
| 26 |
<refname>sd_bus_get_timeout</refname> |
| 27 |
|
| 28 |
<refpurpose>Get the file descriptor, I/O events and timeout to wait for from a message bus |
| 29 |
object</refpurpose> |
| 30 |
</refnamediv> |
| 31 |
|
| 32 |
<refsynopsisdiv> |
| 33 |
<funcsynopsis> |
| 34 |
<funcsynopsisinfo>#include <systemd/sd-bus.h></funcsynopsisinfo> |
| 35 |
|
| 36 |
<funcprototype> |
| 37 |
<funcdef>int <function>sd_bus_get_fd</function></funcdef> |
| 38 |
<paramdef>sd_bus *<parameter>bus</parameter></paramdef> |
| 39 |
</funcprototype> |
| 40 |
|
| 41 |
<funcprototype> |
| 42 |
<funcdef>int <function>sd_bus_get_events</function></funcdef> |
| 43 |
<paramdef>sd_bus *<parameter>bus</parameter></paramdef> |
| 44 |
</funcprototype> |
| 45 |
|
| 46 |
<funcprototype> |
| 47 |
<funcdef>int <function>sd_bus_get_timeout</function></funcdef> |
| 48 |
<paramdef>sd_bus *<parameter>bus</parameter></paramdef> |
| 49 |
<paramdef>uint64_t *<parameter>timeout_usec</parameter></paramdef> |
| 50 |
</funcprototype> |
| 51 |
</funcsynopsis> |
| 52 |
</refsynopsisdiv> |
| 53 |
|
| 54 |
<refsect1> |
| 55 |
<title>Description</title> |
| 56 |
|
| 57 |
<para><function>sd_bus_get_fd()</function> returns the file descriptor used to communicate from |
| 58 |
a message bus object. This descriptor can be used with |
| 59 |
<citerefentry project='man-pages'><refentrytitle>poll</refentrytitle><manvolnum>3</manvolnum></citerefentry> |
| 60 |
or a similar function to wait for I/O events on the specified bus connection object. If the bus |
| 61 |
object was configured with the <function>sd_bus_set_fd()</function> function, then the |
| 62 |
<parameter>input_fd</parameter> file descriptor used in that call is returned.</para> |
| 63 |
|
| 64 |
<para><function>sd_bus_get_events()</function> returns the I/O events to wait for, suitable for |
| 65 |
passing to <function>poll()</function> or a similar call. Returns a combination of |
| 66 |
<constant>POLLIN</constant>, <constant>POLLOUT</constant>, … events, or negative on error. |
| 67 |
</para> |
| 68 |
|
| 69 |
<para><function>sd_bus_get_timeout()</function> returns the <emphasis>absolute</emphasis> timeout in μs, |
| 70 |
from which the relative timeout to pass to <function>poll()</function> (or a similar call) can be |
| 71 |
derived, when waiting for events on the specified bus connection. The returned timeout may be zero, in |
| 72 |
which case a subsequent I/O polling call should be invoked in non-blocking mode. The returned timeout may |
| 73 |
be <constant>UINT64_MAX</constant> in which case the I/O polling call may block indefinitely, without any |
| 74 |
applied timeout. Note that the returned timeout should be considered only a maximum sleeping time. It is |
| 75 |
permissible (and even expected) that shorter timeouts are used by the calling program, in case other |
| 76 |
event sources are polled in the same event loop. Note that the returned time-value is absolute, based of |
| 77 |
<constant>CLOCK_MONOTONIC</constant> and specified in microseconds. When converting this value in order |
| 78 |
to pass it as third argument to <function>poll()</function> (which expects relative milliseconds), care |
| 79 |
should be taken to convert to a relative time and use a division that rounds up to ensure the I/O polling |
| 80 |
operation does not sleep for shorter than necessary, which might result in unintended busy looping |
| 81 |
(alternatively, use <citerefentry |
| 82 |
project='man-pages'><refentrytitle>ppoll</refentrytitle><manvolnum>2</manvolnum></citerefentry> instead |
| 83 |
of plain <function>poll()</function>, which understands timeouts with nano-second granularity).</para> |
| 84 |
|
| 85 |
<para>These three functions are useful to hook up a bus connection object with an external or |
| 86 |
manual event loop involving <function>poll()</function> or a similar I/O polling call. Before |
| 87 |
each invocation of the I/O polling call, all three functions should be invoked: the file |
| 88 |
descriptor returned by <function>sd_bus_get_fd()</function> should be polled for the events |
| 89 |
indicated by <function>sd_bus_get_events()</function>, and the I/O call should block for that up |
| 90 |
to the timeout returned by <function>sd_bus_get_timeout()</function>. After each I/O polling |
| 91 |
call the bus connection needs to process incoming or outgoing data, by invoking |
| 92 |
<citerefentry><refentrytitle>sd_bus_process</refentrytitle><manvolnum>3</manvolnum></citerefentry>. |
| 93 |
</para> |
| 94 |
|
| 95 |
<para>Note that these functions are only one of three supported ways to implement I/O event |
| 96 |
handling for bus connections. Alternatively use |
| 97 |
<citerefentry><refentrytitle>sd_bus_attach_event</refentrytitle><manvolnum>3</manvolnum></citerefentry> |
| 98 |
to attach a bus connection to an |
| 99 |
<citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry> |
| 100 |
event loop. Or use |
| 101 |
<citerefentry><refentrytitle>sd_bus_wait</refentrytitle><manvolnum>3</manvolnum></citerefentry> |
| 102 |
as a simple synchronous, blocking I/O waiting call.</para> |
| 103 |
</refsect1> |
| 104 |
|
| 105 |
<refsect1> |
| 106 |
<title>Return Value</title> |
| 107 |
|
| 108 |
<para>On success, <function>sd_bus_get_fd()</function> returns the file descriptor used for |
| 109 |
communication. On failure, it returns a negative errno-style error code.</para> |
| 110 |
|
| 111 |
<para>On success, <function>sd_bus_get_events()</function> returns the I/O event mask to use for |
| 112 |
I/O event watching. On failure, it returns a negative errno-style error code.</para> |
| 113 |
|
| 114 |
<para>On success, <function>sd_bus_get_timeout()</function> returns a non-negative integer. On |
| 115 |
failure, it returns a negative errno-style error code.</para> |
| 116 |
|
| 117 |
<refsect2> |
| 118 |
<title>Errors</title> |
| 119 |
|
| 120 |
<para>Returned errors may indicate the following problems:</para> |
| 121 |
|
| 122 |
<variablelist> |
| 123 |
<varlistentry> |
| 124 |
<term><constant>-EINVAL</constant></term> |
| 125 |
|
| 126 |
<listitem><para>An invalid bus object was passed.</para></listitem> |
| 127 |
</varlistentry> |
| 128 |
|
| 129 |
<varlistentry> |
| 130 |
<term><constant>-ECHILD</constant></term> |
| 131 |
|
| 132 |
<listitem><para>The bus connection was allocated in a parent process and is being reused |
| 133 |
in a child process after <function>fork()</function>.</para></listitem> |
| 134 |
</varlistentry> |
| 135 |
|
| 136 |
<varlistentry> |
| 137 |
<term><constant>-ENOTCONN</constant></term> |
| 138 |
|
| 139 |
<listitem><para>The bus connection has been terminated.</para></listitem> |
| 140 |
</varlistentry> |
| 141 |
|
| 142 |
<varlistentry> |
| 143 |
<term><constant>-EPERM</constant></term> |
| 144 |
|
| 145 |
<listitem><para>Two distinct file descriptors were passed for input and output using |
| 146 |
<function>sd_bus_set_fd()</function>, which <function>sd_bus_get_fd()</function> cannot |
| 147 |
return.</para></listitem> |
| 148 |
</varlistentry> |
| 149 |
|
| 150 |
<varlistentry> |
| 151 |
<term><constant>-ENOPKG</constant></term> |
| 152 |
|
| 153 |
<listitem><para>The bus cannot be resolved.</para></listitem> |
| 154 |
</varlistentry> |
| 155 |
</variablelist> |
| 156 |
</refsect2> |
| 157 |
</refsect1> |
| 158 |
|
| 159 |
<xi:include href="libsystemd-pkgconfig.xml" /> |
| 160 |
|
| 161 |
<refsect1> |
| 162 |
<title>History</title> |
| 163 |
<para><function>sd_bus_get_fd()</function>, |
| 164 |
<function>sd_bus_get_events()</function>, and |
| 165 |
<function>sd_bus_get_timeout()</function> were added in version 221.</para> |
| 166 |
</refsect1> |
| 167 |
|
| 168 |
<refsect1> |
| 169 |
<title>See Also</title> |
| 170 |
|
| 171 |
<para><simplelist type="inline"> |
| 172 |
<member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member> |
| 173 |
<member><citerefentry><refentrytitle>sd-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 174 |
<member><citerefentry><refentrytitle>sd_bus_process</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 175 |
<member><citerefentry><refentrytitle>sd_bus_attach_event</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 176 |
<member><citerefentry><refentrytitle>sd_bus_wait</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 177 |
<member><citerefentry><refentrytitle>sd_bus_set_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 178 |
<member><citerefentry project='man-pages'><refentrytitle>poll</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 179 |
</simplelist></para> |
| 180 |
</refsect1> |
| 181 |
|
| 182 |
</refentry> |