xref: /openbmc/sdbusplus/src/bus.cpp (revision 578b9733)
1 #include <sdbusplus/bus.hpp>
2 #include <sdbusplus/exception.hpp>
3 
4 namespace sdbusplus::bus
5 {
6 
emit_interfaces_added(const char * path,const std::vector<std::string> & ifaces)7 void bus::emit_interfaces_added(const char* path,
8                                 const std::vector<std::string>& ifaces)
9 {
10     details::Strv s{ifaces};
11     _intf->sd_bus_emit_interfaces_added_strv(_bus.get(), path,
12                                              static_cast<char**>(s));
13 }
14 
emit_interfaces_removed(const char * path,const std::vector<std::string> & ifaces)15 void bus::emit_interfaces_removed(const char* path,
16                                   const std::vector<std::string>& ifaces)
17 {
18     details::Strv s{ifaces};
19     _intf->sd_bus_emit_interfaces_removed_strv(_bus.get(), path,
20                                                static_cast<char**>(s));
21 }
22 
23 /* Create a new default connection: system bus if root, session bus if user. */
new_default()24 bus new_default()
25 {
26     sd_bus* b = nullptr;
27     auto rc = sd_bus_default(&b);
28     if (rc < 0)
29     {
30         throw exception::SdBusError(-rc, __PRETTY_FUNCTION__);
31     }
32     return bus(b, std::false_type());
33 }
34 
35 /* Create a new default connection to the session bus. */
new_default_user()36 bus new_default_user()
37 {
38     sd_bus* b = nullptr;
39     auto rc = sd_bus_default_user(&b);
40     if (rc < 0)
41     {
42         throw exception::SdBusError(-rc, __PRETTY_FUNCTION__);
43     }
44     return bus(b, std::false_type());
45 }
46 
47 /* Create a new default connection to the system bus. */
new_default_system()48 bus new_default_system()
49 {
50     sd_bus* b = nullptr;
51     auto rc = sd_bus_default_system(&b);
52     if (rc < 0)
53     {
54         throw exception::SdBusError(-rc, __PRETTY_FUNCTION__);
55     }
56     return bus(b, std::false_type());
57 }
58 
59 /* Create a new connection: system bus if root, session bus if user. */
new_bus()60 bus new_bus()
61 {
62     sd_bus* b = nullptr;
63     auto rc = sd_bus_open(&b);
64     if (rc < 0)
65     {
66         throw exception::SdBusError(-rc, __PRETTY_FUNCTION__);
67     }
68     bus bus(b, std::false_type());
69     bus.set_should_close(true);
70     return bus;
71 }
72 
73 /* Create a new connection to the session bus. */
new_user()74 bus new_user()
75 {
76     sd_bus* b = nullptr;
77     auto rc = sd_bus_open_user(&b);
78     if (rc < 0)
79     {
80         throw exception::SdBusError(-rc, __PRETTY_FUNCTION__);
81     }
82     bus bus(b, std::false_type());
83     bus.set_should_close(true);
84     return bus;
85 }
86 
87 /* Create a new connection to the system bus. */
new_system()88 bus new_system()
89 {
90     sd_bus* b = nullptr;
91     auto rc = sd_bus_open_system(&b);
92     if (rc < 0)
93     {
94         throw exception::SdBusError(-rc, __PRETTY_FUNCTION__);
95     }
96     bus bus(b, std::false_type());
97     bus.set_should_close(true);
98     return bus;
99 }
100 
bus(busp_t b,sdbusplus::SdBusInterface * intf)101 bus::bus(busp_t b, sdbusplus::SdBusInterface* intf) :
102     _intf(intf), _bus(_intf->sd_bus_ref(b), details::BusDeleter(intf))
103 {
104     // Emitting object added causes a message to get the properties
105     // which can trigger a 'transaction' in the server bindings.  If
106     // the bus isn't up far enough, this causes an assert deep in
107     // sd-bus code.  Get the 'unique_name' to ensure the bus is up far
108     // enough to avoid the assert.
109     if (b != nullptr)
110     {
111         get_unique_name();
112     }
113 }
114 
bus(busp_t b)115 bus::bus(busp_t b) :
116     _intf(&sdbus_impl),
117     _bus(_intf->sd_bus_ref(b), details::BusDeleter(&sdbus_impl))
118 {
119     // Emitting object added causes a message to get the properties
120     // which can trigger a 'transaction' in the server bindings.  If
121     // the bus isn't up far enough, this causes an assert deep in
122     // sd-bus code.  Get the 'unique_name' to ensure the bus is up far
123     // enough to avoid the assert.
124     if (b != nullptr)
125     {
126         get_unique_name();
127     }
128 }
129 
bus(busp_t b,std::false_type)130 bus::bus(busp_t b, std::false_type) :
131     _intf(&sdbus_impl), _bus(b, details::BusDeleter(&sdbus_impl))
132 {
133     // Emitting object added causes a message to get the properties
134     // which can trigger a 'transaction' in the server bindings.  If
135     // the bus isn't up far enough, this causes an assert deep in
136     // sd-bus code.  Get the 'unique_name' to ensure the bus is up far
137     // enough to avoid the assert.
138     if (b != nullptr)
139     {
140         get_unique_name();
141     }
142 }
143 
144 } // namespace sdbusplus::bus
145