2 <!DOCTYPE refentry PUBLIC
"-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
4 <!-- SPDX-License-Identifier: LGPL-2.1+ -->
6 <refentry id=
"sd_bus_new" xmlns:
xi=
"http://www.w3.org/2001/XInclude">
9 <title>sd_bus_new
</title>
10 <productname>systemd
</productname>
14 <refentrytitle>sd_bus_new
</refentrytitle>
15 <manvolnum>3</manvolnum>
19 <refname>sd_bus_new
</refname>
20 <refname>sd_bus_ref
</refname>
21 <refname>sd_bus_unref
</refname>
22 <refname>sd_bus_unrefp
</refname>
23 <refname>sd_bus_close_unref
</refname>
24 <refname>sd_bus_close_unrefp
</refname>
25 <refname>sd_bus_flush_close_unref
</refname>
26 <refname>sd_bus_flush_close_unrefp
</refname>
28 <refpurpose>Create a new bus object and create or destroy references to it
</refpurpose>
33 <funcsynopsisinfo>#include
<systemd/sd-bus.h
></funcsynopsisinfo>
36 <funcdef>int
<function>sd_bus_new
</function></funcdef>
37 <paramdef>sd_bus **
<parameter>bus
</parameter></paramdef>
41 <funcdef>sd_bus *
<function>sd_bus_ref
</function></funcdef>
42 <paramdef>sd_bus *
<parameter>bus
</parameter></paramdef>
46 <funcdef>sd_bus *
<function>sd_bus_unref
</function></funcdef>
47 <paramdef>sd_bus *
<parameter>bus
</parameter></paramdef>
51 <funcdef>sd_bus *
<function>sd_bus_close_unref
</function></funcdef>
52 <paramdef>sd_bus *
<parameter>bus
</parameter></paramdef>
56 <funcdef>sd_bus *
<function>sd_bus_flush_close_unref
</function></funcdef>
57 <paramdef>sd_bus *
<parameter>bus
</parameter></paramdef>
61 <funcdef>void
<function>sd_bus_unrefp
</function></funcdef>
62 <paramdef>sd_bus **
<parameter>busp
</parameter></paramdef>
66 <funcdef>void
<function>sd_bus_close_unrefp
</function></funcdef>
67 <paramdef>sd_bus **
<parameter>busp
</parameter></paramdef>
71 <funcdef>void
<function>sd_bus_flush_close_unrefp
</function></funcdef>
72 <paramdef>sd_bus **
<parameter>busp
</parameter></paramdef>
78 <title>Description
</title>
80 <para><function>sd_bus_new()
</function> creates a new bus
81 object. This object is reference-counted, and will be destroyed
82 when all references are gone. Initially, the caller of this
83 function owns the sole reference and the bus object will not be
84 connected to any bus. To connect it to a bus, make sure
85 to set an address with
86 <citerefentry><refentrytitle>sd_bus_set_address
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
87 or a related call, and then start the connection with
88 <citerefentry><refentrytitle>sd_bus_start
</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
</para>
90 <para>In most cases, it is a better idea to invoke
91 <citerefentry><refentrytitle>sd_bus_default_user
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
92 <citerefentry><refentrytitle>sd_bus_default_system
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
93 or related calls instead of the more low-level
94 <function>sd_bus_new()
</function> and
95 <function>sd_bus_start()
</function>. The higher-level calls not
96 only allocate a bus object but also start the connection to a
97 well-known bus in a single function invocation.
</para>
99 <para><function>sd_bus_ref()
</function> increases the reference
100 counter of
<parameter>bus
</parameter> by one.
</para>
102 <para><function>sd_bus_unref()
</function> decreases the reference
103 counter of
<parameter>bus
</parameter> by one. Once the reference
104 count has dropped to zero,
<parameter>bus
</parameter> is destroyed
105 and cannot be used anymore, so further calls to
106 <function>sd_bus_ref()
</function> or
107 <function>sd_bus_unref()
</function> are illegal.
</para>
109 <para><function>sd_bus_unrefp()
</function> is similar to
110 <function>sd_bus_unref()
</function> but takes a pointer to a
111 pointer to an
<type>sd_bus
</type> object. This call is useful in
112 conjunction with GCC's and LLVM's
<ulink
113 url=
"https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html">Clean-up
114 Variable Attribute
</ulink>. Note that this function is defined as
115 inline function. Use a declaration like the following, in order to
116 allocate a bus object that is freed automatically as the code
117 block is left:
</para>
120 __attribute__((cleanup(sd_bus_unrefp)) sd_bus *bus = NULL;
123 r = sd_bus_default(
&bus);
125 fprintf(stderr,
"Failed to allocate bus: %s\n", strerror(-r));
129 <para><function>sd_bus_ref()
</function> and
<function>sd_bus_unref()
</function>
130 execute no operation if the passed in bus object address is
131 <constant>NULL
</constant>.
<function>sd_bus_unrefp()
</function> will first
132 dereference its argument, which must not be
<constant>NULL
</constant>, and will
133 execute no operation if
<emphasis>that
</emphasis> is
<constant>NULL
</constant>.
136 <para><function>sd_bus_close_unref()
</function> is similar to
<function>sd_bus_unref()
</function>, but
138 <citerefentry><refentrytitle>sd_bus_close
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
139 ensuring that the connection is terminated before the reference to the connection is dropped and possibly
140 the object freed.
</para>
142 <para><function>sd_bus_flush_close_unref()
</function> is similar to
<function>sd_bus_unref()
</function>,
144 <citerefentry><refentrytitle>sd_bus_flush
</refentrytitle><manvolnum>3</manvolnum></citerefentry> as well
145 as
<citerefentry><refentrytitle>sd_bus_close
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
146 ensuring that any pending messages are synchronously flushed out before the reference to the connection
147 is dropped and possibly the object freed. This call is particularly useful immediately before exiting
148 from a program as it ensures that any pending outgoing messages are written out, and unprocessed but
149 queued incoming messages released before the connection is terminated and released.
</para>
151 <para><function>sd_bus_close_unrefp()
</function> is similar to
152 <function>sd_bus_close_unref()
</function>, but may be used in GCC's and LLVM's Clean-up Variable
153 Attribute, see above. Similarly,
<function>sd_bus_flush_close_unrefp()
</function> is similar to
154 <function>sd_bus_flush_close_unref()
</function>.
</para>
158 <title>Return Value
</title>
160 <para>On success,
<function>sd_bus_new()
</function> returns
0 or a
161 positive integer. On failure, it returns a negative errno-style
164 <para><function>sd_bus_ref()
</function> always returns the argument.
167 <para><function>sd_bus_unref()
</function> and
<function>sd_bus_flush_close_unref()
</function> always return
168 <constant>NULL
</constant>.
</para>
172 <title>Errors
</title>
174 <para>Returned errors may indicate the following problems:
</para>
178 <term><constant>-ENOMEM
</constant></term>
180 <listitem><para>Memory allocation failed.
</para></listitem>
185 <xi:include href=
"libsystemd-pkgconfig.xml" />
188 <title>See Also
</title>
191 <citerefentry><refentrytitle>systemd
</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
192 <citerefentry><refentrytitle>sd-bus
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
193 <citerefentry><refentrytitle>sd_bus_default_user
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
194 <citerefentry><refentrytitle>sd_bus_default_system
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
195 <citerefentry><refentrytitle>sd_bus_open_user
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
196 <citerefentry><refentrytitle>sd_bus_open_system
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
197 <citerefentry><refentrytitle>sd_bus_close
</refentrytitle><manvolnum>3</manvolnum></citerefentry>