From 8ca6025eed193c252149b2908c3d5e37b00b0107 Mon Sep 17 00:00:00 2001 From: Vernon Mauery Date: Thu, 8 Nov 2018 14:55:34 -0800 Subject: [PATCH] sdbusplus: better mirror bus aquisition methods This adds new functions: * new_default_user() -> return default user bus connection * new_default_system() -> returns default system connection And changes new_default() to return the default type of connection for the user as per the man page: https://www.freedesktop.org/software/systemd/man/sd_bus_default.html Also, update the example and the README to use the default bus calls. Change-Id: I13cd77dda847c4f6018da38e0019816da07710d1 Signed-off-by: Vernon Mauery --- README.md | 2 +- example/list-users.cpp | 2 +- sdbusplus/bus.hpp.in | 20 +++++++++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d49159b4..61897e49 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ type-safety and memory leak protection afforded by modern C++ practices. Consider the following code: ``` -auto b = bus::new_system(); +auto b = bus::new_default_system(); auto m = b.new_method_call("org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", diff --git a/example/list-users.cpp b/example/list-users.cpp index 7b5a1a60..3995f5c4 100644 --- a/example/list-users.cpp +++ b/example/list-users.cpp @@ -11,7 +11,7 @@ int main() { using namespace sdbusplus; - auto b = bus::new_system(); + auto b = bus::new_default_system(); auto m = b.new_method_call("org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "ListUsers"); diff --git a/sdbusplus/bus.hpp.in b/sdbusplus/bus.hpp.in index e80d5edf..5e1ee8af 100644 --- a/sdbusplus/bus.hpp.in +++ b/sdbusplus/bus.hpp.in @@ -490,13 +490,31 @@ inline bus::bus(busp_t b, std::false_type) : _intf(&sdbus_impl), _bus(b) #endif } +/* Create a new default connection: system bus if root, session bus if user */ inline bus new_default() { sd_bus* b = nullptr; - sd_bus_open(&b); + sd_bus_default(&b); return bus(b, std::false_type()); } +/* Create a new default connection to the session bus */ +inline bus new_default_user() +{ + sd_bus* b = nullptr; + sd_bus_default_user(&b); + return bus(b, std::false_type()); +} + +/* Create a new default connection to the system bus */ +inline bus new_default_system() +{ + sd_bus* b = nullptr; + sd_bus_default_system(&b); + return bus(b, std::false_type()); +} + +/* WARNING: THESE ARE NOT THE FUNCTIONS YOU ARE LOOKING FOR! */ inline bus new_user() { sd_bus* b = nullptr;