1 <?xml version='
1.0'
?> <!--*- Mode: nxml; nxml-child-indent: 2; indent-tabs-mode: nil -*-->
2 <!DOCTYPE refentry PUBLIC
"-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
6 SPDX-License-Identifier: LGPL-2.1+
8 This file is part of systemd.
10 Copyright 2014 Zbigniew Jędrzejewski-Szmek
13 <refentry id=
"sd_bus_new">
16 <title>sd_bus_new
</title>
17 <productname>systemd
</productname>
21 <contrib>A monkey with a typewriter
</contrib>
22 <firstname>Zbigniew
</firstname>
23 <surname>Jędrzejewski-Szmek
</surname>
24 <email>zbyszek@in.waw.pl
</email>
30 <refentrytitle>sd_bus_new
</refentrytitle>
31 <manvolnum>3</manvolnum>
35 <refname>sd_bus_new
</refname>
36 <refname>sd_bus_ref
</refname>
37 <refname>sd_bus_unref
</refname>
38 <refname>sd_bus_unrefp
</refname>
40 <refpurpose>Create a new bus object and create or destroy references to it
</refpurpose>
45 <funcsynopsisinfo>#include
<systemd/sd-bus.h
></funcsynopsisinfo>
48 <funcdef>int
<function>sd_bus_new
</function></funcdef>
49 <paramdef>sd_bus **
<parameter>bus
</parameter></paramdef>
53 <funcdef>sd_bus *
<function>sd_bus_ref
</function></funcdef>
54 <paramdef>sd_bus *
<parameter>bus
</parameter></paramdef>
58 <funcdef>sd_bus *
<function>sd_bus_unref
</function></funcdef>
59 <paramdef>sd_bus *
<parameter>bus
</parameter></paramdef>
63 <funcdef>void
<function>sd_bus_unrefp
</function></funcdef>
64 <paramdef>sd_bus **
<parameter>bus
</parameter></paramdef>
70 <title>Description
</title>
72 <para><function>sd_bus_new()
</function> creates a new bus
73 object. This object is reference-counted, and will be destroyed
74 when all references are gone. Initially, the caller of this
75 function owns the sole reference and the bus object will not be
76 connected to any bus. To connect it to a bus, make sure
77 to set an address with
78 <citerefentry><refentrytitle>sd_bus_set_address
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
79 or a related call, and then start the connection with
80 <citerefentry><refentrytitle>sd_bus_start
</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
</para>
82 <para>In most cases, it is a better idea to invoke
83 <citerefentry><refentrytitle>sd_bus_default_user
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
84 <citerefentry><refentrytitle>sd_bus_default_system
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
85 or related calls instead of the more low-level
86 <function>sd_bus_new()
</function> and
87 <function>sd_bus_start()
</function>. The higher-level calls not
88 only allocate a bus object but also start the connection to a
89 well-known bus in a single function invocation.
</para>
91 <para><function>sd_bus_ref()
</function> increases the reference
92 counter of
<parameter>bus
</parameter> by one.
</para>
94 <para><function>sd_bus_unref()
</function> decreases the reference
95 counter of
<parameter>bus
</parameter> by one. Once the reference
96 count has dropped to zero,
<parameter>bus
</parameter> is destroyed
97 and cannot be used anymore, so further calls to
98 <function>sd_bus_ref()
</function> or
99 <function>sd_bus_unref()
</function> are illegal.
</para>
101 <para><function>sd_bus_unrefp()
</function> is similar to
102 <function>sd_bus_unref()
</function> but takes a pointer to a
103 pointer to an
<type>sd_bus
</type> object. This call is useful in
104 conjunction with GCC's and LLVM's
<ulink
105 url=
"https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html">Clean-up
106 Variable Attribute
</ulink>. Note that this function is defined as
107 inline function. Use a declaration like the following, in order to
108 allocate a bus object that is freed automatically as the code
109 block is left:
</para>
112 __attribute__((cleanup(sd_bus_unrefp)) sd_bus *bus = NULL;
115 r = sd_bus_default(
&bus);
117 fprintf(stderr,
"Failed to allocate bus: %s\n", strerror(-r));
121 <para><function>sd_bus_ref()
</function>,
122 <function>sd_bus_unref()
</function> and
123 <function>sd_bus_unrefp()
</function> execute no operation if the
124 passed in bus object is
<constant>NULL
</constant>.
</para>
128 <title>Return Value
</title>
130 <para>On success,
<function>sd_bus_new()
</function> returns
0 or a
131 positive integer. On failure, it returns a negative errno-style
134 <para><function>sd_bus_ref()
</function> always returns the argument.
137 <para><function>sd_bus_unref()
</function> always returns
138 <constant>NULL
</constant>.
</para>
142 <title>Errors
</title>
144 <para>Returned errors may indicate the following problems:
</para>
148 <term><constant>-ENOMEM
</constant></term>
150 <listitem><para>Memory allocation failed.
</para></listitem>
158 <para><function>sd_bus_new()
</function> and other functions
159 described here are available as a shared library, which can be
160 compiled and linked to with the
161 <constant>libsystemd
</constant> <citerefentry project='die-net'
><refentrytitle>pkg-config
</refentrytitle><manvolnum>1</manvolnum></citerefentry>
166 <title>See Also
</title>
169 <citerefentry><refentrytitle>systemd
</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
170 <citerefentry><refentrytitle>sd-bus
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
171 <citerefentry><refentrytitle>sd_bus_default_user
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
172 <citerefentry><refentrytitle>sd_bus_default_system
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
173 <citerefentry><refentrytitle>sd_bus_open_user
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
174 <citerefentry><refentrytitle>sd_bus_open_system
</refentrytitle><manvolnum>3</manvolnum></citerefentry>