1SD_BUS_NEW(3) sd_bus_new SD_BUS_NEW(3)
2
3
4
6 sd_bus_new, sd_bus_ref, sd_bus_unref, sd_bus_unrefp,
7 sd_bus_close_unref, sd_bus_close_unrefp, sd_bus_flush_close_unref,
8 sd_bus_flush_close_unrefp - Create a new bus object and create or
9 destroy references to it
10
12 #include <systemd/sd-bus.h>
13
14 int sd_bus_new(sd_bus **bus);
15
16 sd_bus *sd_bus_ref(sd_bus *bus);
17
18 sd_bus *sd_bus_unref(sd_bus *bus);
19
20 sd_bus *sd_bus_close_unref(sd_bus *bus);
21
22 sd_bus *sd_bus_flush_close_unref(sd_bus *bus);
23
24 void sd_bus_unrefp(sd_bus **busp);
25
26 void sd_bus_close_unrefp(sd_bus **busp);
27
28 void sd_bus_flush_close_unrefp(sd_bus **busp);
29
31 sd_bus_new() creates a new bus object. This object is
32 reference-counted, and will be destroyed when all references are gone.
33 Initially, the caller of this function owns the sole reference and the
34 bus object will not be connected to any bus. To connect it to a bus,
35 make sure to set an address with sd_bus_set_address(3) or a related
36 call, and then start the connection with sd_bus_start(3).
37
38 In most cases, it is better to use sd_bus_default_user(3),
39 sd_bus_default_system(3) or related calls instead of the more low-level
40 sd_bus_new() and sd_bus_start(). The higher-level functions not only
41 allocate a bus object but also start the connection to a well-known bus
42 in a single function call.
43
44 sd_bus_ref() increases the reference counter of bus by one.
45
46 sd_bus_unref() decreases the reference counter of bus by one. Once the
47 reference count has dropped to zero, bus is destroyed and cannot be
48 used anymore, so further calls to sd_bus_ref() or sd_bus_unref() are
49 illegal.
50
51 sd_bus_unrefp() is similar to sd_bus_unref() but takes a pointer to a
52 pointer to an sd_bus object. This call is useful in conjunction with
53 GCC's and LLVM's Clean-up Variable Attribute[1]. Note that this
54 function is defined as an inline function. Use a declaration like the
55 following, in order to allocate a bus object that is freed
56 automatically as the code block is left:
57
58 {
59 __attribute__((cleanup(sd_bus_unrefp))) sd_bus *bus = NULL;
60 int r;
61 ...
62 r = sd_bus_default(&bus);
63 if (r < 0) {
64 errno = -r;
65 fprintf(stderr, "Failed to allocate bus: %m\n");
66 }
67 ...
68 }
69
70 sd_bus_ref() and sd_bus_unref() execute no operation if the argument is
71 NULL. sd_bus_unrefp() will first dereference its argument, which must
72 not be NULL, and will execute no operation if that is NULL.
73
74 sd_bus_close_unref() is similar to sd_bus_unref(), but first executes
75 sd_bus_close(3), ensuring that the connection is terminated before the
76 reference to the connection is dropped and possibly the object freed.
77
78 sd_bus_flush_close_unref() is similar to sd_bus_unref(), but first
79 executes sd_bus_flush(3) as well as sd_bus_close(3), ensuring that any
80 pending messages are synchronously flushed out before the reference to
81 the connection is dropped and possibly the object freed. This call is
82 particularly useful immediately before exiting from a program as it
83 ensures that any pending outgoing messages are written out, and
84 unprocessed but queued incoming messages released before the connection
85 is terminated and released.
86
87 sd_bus_close_unrefp() is similar to sd_bus_close_unref(), but may be
88 used in GCC's and LLVM's Clean-up Variable Attribute, see above.
89 Similarly, sd_bus_flush_close_unrefp() is similar to
90 sd_bus_flush_close_unref().
91
93 On success, sd_bus_new() returns 0 or a positive integer. On failure,
94 it returns a negative errno-style error code.
95
96 sd_bus_ref() always returns the argument.
97
98 sd_bus_unref() and sd_bus_flush_close_unref() always return NULL.
99
100 Errors
101 Returned errors may indicate the following problems:
102
103 -ENOMEM
104 Memory allocation failed.
105
107 Functions described here are available as a shared library, which can
108 be compiled against and linked to with the libsystemd pkg-config(1)
109 file.
110
111 The code described here uses getenv(3), which is declared to be not
112 multi-thread-safe. This means that the code calling the functions
113 described here must not call setenv(3) from a parallel thread. It is
114 recommended to only do calls to setenv() from an early phase of the
115 program when no other threads have been started.
116
118 systemd(1), sd-bus(3), sd_bus_default_user(3),
119 sd_bus_default_system(3), sd_bus_open_user(3), sd_bus_open_system(3),
120 sd_bus_close(3)
121
123 1. Clean-up Variable Attribute
124 https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
125
126
127
128systemd 254 SD_BUS_NEW(3)