2 <!DOCTYPE refentry PUBLIC
"-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
6 SPDX-License-Identifier: LGPL-2.1-or-later
8 Copyright © 2016 Julian Orth
11 <refentry id=
"sd_bus_get_fd" xmlns:
xi=
"http://www.w3.org/2001/XInclude">
14 <title>sd_bus_get_fd
</title>
15 <productname>systemd
</productname>
19 <refentrytitle>sd_bus_get_fd
</refentrytitle>
20 <manvolnum>3</manvolnum>
24 <refname>sd_bus_get_fd
</refname>
25 <refname>sd_bus_set_fd
</refname>
26 <refname>sd_bus_get_events
</refname>
27 <refname>sd_bus_get_timeout
</refname>
29 <refpurpose>Get the file descriptor, I/O events and timeout to wait for from a message bus
35 <funcsynopsisinfo>#include
<systemd/sd-bus.h
></funcsynopsisinfo>
38 <funcdef>int
<function>sd_bus_get_fd
</function></funcdef>
39 <paramdef>sd_bus *
<parameter>bus
</parameter></paramdef>
43 <funcdef>int
<function>sd_bus_set_fd
</function></funcdef>
44 <paramdef>sd_bus *
<parameter>bus
</parameter></paramdef>
45 <paramdef>int
<parameter>input_fd
</parameter></paramdef>
46 <paramdef>int
<parameter>output_fd
</parameter></paramdef>
50 <funcdef>int
<function>sd_bus_get_events
</function></funcdef>
51 <paramdef>sd_bus *
<parameter>bus
</parameter></paramdef>
55 <funcdef>int
<function>sd_bus_get_timeout
</function></funcdef>
56 <paramdef>sd_bus *
<parameter>bus
</parameter></paramdef>
57 <paramdef>uint64_t *
<parameter>timeout_usec
</parameter></paramdef>
63 <title>Description
</title>
65 <para><function>sd_bus_get_fd()
</function> returns the file descriptor used to communicate from
66 a message bus object. This descriptor can be used with
67 <citerefentry project='man-pages'
><refentrytitle>poll
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
68 or a similar function to wait for I/O events on the specified bus connection object. If the bus
69 object was configured with the
<function>sd_bus_set_fd()
</function> function, then the
70 <parameter>input_fd
</parameter> file descriptor used in that call is returned.
</para>
72 <para><function>sd_bus_set_fd()
</function> sets the file descriptors used to communicate from a
73 message bus object. Both
<parameter>input_fd
</parameter> and
<parameter>output_fd
</parameter>
74 must be valid file descriptors. The same file descriptor may be used as both the input and the
75 output file descriptor. This function must be called before the bus is started.
</para>
77 <para><function>sd_bus_get_events()
</function> returns the I/O events to wait for, suitable for
78 passing to
<function>poll()
</function> or a similar call. Returns a combination of
79 <constant>POLLIN
</constant>,
<constant>POLLOUT
</constant>, … events, or negative on error.
82 <para><function>sd_bus_get_timeout()
</function> returns the timeout in µs to pass to
83 <function>poll()
</function> or a similar call when waiting for events on the specified bus
84 connection. The returned timeout may be zero, in which case a subsequent I/O polling call
85 should be invoked in non-blocking mode. The returned timeout may be
86 <constant>UINT64_MAX
</constant> in which case the I/O polling call may block indefinitely,
87 without any applied timeout. Note that the returned timeout should be considered only a
88 maximum sleeping time. It is permissible (and even expected) that shorter timeouts are used by
89 the calling program, in case other event sources are polled in the same event loop. Note that
90 the returned time-value is relative and specified in microseconds. When converting this value in
91 order to pass it as third argument to
<function>poll()
</function> (which expects milliseconds),
92 care should be taken to use a division that rounds up to ensure the I/O polling operation
93 doesn't sleep for shorter than necessary, which might result in unintended busy looping
95 <citerefentry project='man-pages'
><refentrytitle>ppoll
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
96 instead of plain
<function>poll()
</function>, which understands timeouts with nano-second
99 <para>These three functions are useful to hook up a bus connection object with an external or
100 manual event loop involving
<function>poll()
</function> or a similar I/O polling call. Before
101 each invocation of the I/O polling call, all three functions should be invoked: the file
102 descriptor returned by
<function>sd_bus_get_fd()
</function> should be polled for the events
103 indicated by
<function>sd_bus_get_events()
</function>, and the I/O call should block for that up
104 to the timeout returned by
<function>sd_bus_get_timeout()
</function>. After each I/O polling
105 call the bus connection needs to process incoming or outgoing data, by invoking
106 <citerefentry><refentrytitle>sd_bus_process
</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
109 <para>Note that these functions are only one of three supported ways to implement I/O event
110 handling for bus connections. Alternatively use
111 <citerefentry><refentrytitle>sd_bus_attach_event
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
112 to attach a bus connection to an
113 <citerefentry><refentrytitle>sd-event
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
115 <citerefentry><refentrytitle>sd_bus_wait
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
116 as a simple synchronous, blocking I/O waiting call.
</para>
120 <title>Return Value
</title>
122 <para>On success,
<function>sd_bus_get_fd()
</function> returns the file descriptor used for
123 communication. On failure, it returns a negative errno-style error code.
</para>
125 <para>On success,
<function>sd_bus_set_fd()
</function> returns a non-negative integer. On
126 failure, it returns a negative errno-style error code.
</para>
128 <para>On success,
<function>sd_bus_get_events()
</function> returns the I/O event mask to use for
129 I/O event watching. On failure, it returns a negative errno-style error code.
</para>
131 <para>On success,
<function>sd_bus_get_timeout()
</function> returns a non-negative integer. On
132 failure, it returns a negative errno-style error code.
</para>
135 <title>Errors
</title>
137 <para>Returned errors may indicate the following problems:
</para>
141 <term><constant>-EINVAL
</constant></term>
143 <listitem><para>An invalid bus object was passed.
</para></listitem>
147 <term><constant>-ECHILD
</constant></term>
149 <listitem><para>The bus connection was allocated in a parent process and is being reused
150 in a child process after
<function>fork()
</function>.
</para></listitem>
154 <term><constant>-ENOTCONN
</constant></term>
156 <listitem><para>The bus connection has been terminated.
</para></listitem>
160 <term><constant>-EPERM
</constant></term>
162 <listitem><para>Two distinct file descriptors were passed for input and output using
163 <function>sd_bus_set_fd()
</function>, which
<function>sd_bus_get_fd()
</function> cannot
164 return.
</para></listitem>
168 <term><constant>-EBADF
</constant></term>
170 <listitem><para>An invalid file descriptor was passed to
171 <function>sd_bus_set_fd()
</function>.
</para></listitem>
175 <term><constant>-ENOPKG
</constant></term>
177 <listitem><para>The bus cannot be resolved.
</para></listitem>
183 <xi:include href=
"libsystemd-pkgconfig.xml" />
186 <title>See Also
</title>
189 <citerefentry><refentrytitle>systemd
</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
190 <citerefentry><refentrytitle>sd-bus
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
191 <citerefentry><refentrytitle>sd_bus_process
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
192 <citerefentry><refentrytitle>sd_bus_attach_event
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
193 <citerefentry><refentrytitle>sd_bus_wait
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
194 <citerefentry project='man-pages'
><refentrytitle>poll
</refentrytitle><manvolnum>3</manvolnum></citerefentry>