Commit Graph
127 Commits
Author SHA1 Message Date
Stanislav Angelovič fbb4a23e62 refactor: rename connection creation methods (#406)
This PR makes things around connection factories a little more consistent and more intuitive:

    * createConnection() has been removed. One shall call more expressive createSystemConnection() instead to get a connection to the system bus.
    * createDefaultBusConnection() has been renamed to createBusConnection(), so as not to be confused with libsystemd's default_bus, which is a different thing (a reusable thread-local bus).

Proxies still by default call createBusConnection() to get a connection when the connection is not provided explicitly by the caller, but now createBusConnection() does a different thing, so now the proxies connect to either session bus or system bus depending on the context (as opposed to always to system bus like before).
2024-02-16 18:57:51 +01:00
Stanislav Angelovič 2ff5a09432 refactor: remove deprecated API stuff (#403) 2024-02-10 20:27:14 +01:00
Stanislav Angelovič 94b8058dc5 refactor: use optional for passing potential call errors (#396)
This switches from a raw pointer to std::optional type to pass prospective call errors to the client (using std::optional was not possible years back when sdbus-c++ was based on C++14). This makes the API a little clearer, safer, idiomatically more expressive, and removes potential confusion associated with raw pointers (like ownership, lifetime questions, etc.).
2024-01-10 15:43:37 +01:00
Stanislav Angelovič cda14c9702 refactor: add nodiscard attribute for more functions 2024-01-10 15:07:30 +01:00
Stanislav Angelovič 4b6fa20491 refactor: use correct header include order 2024-01-10 15:07:30 +01:00
Stanislav Angelovič 7c402b5ba9 refactor: add Async suffix to async callMethod functions (#394)
This makes naming consistent, so all asynchronously working functions have Async suffix
2024-01-08 09:28:19 +01:00
Stanislav Angelovic f7afe3d1b1 refactor: add nodiscard attribute for some functions 2023-12-30 21:11:02 +01:00
Stanislav Angelovic 18aece04d5 chore: remove legacy comments 2023-12-30 21:11:02 +01:00
Stanislav Angelovic 9092bbd44c refactor: remove deprecated dont_request_slot_t tag 2023-12-30 21:11:02 +01:00
Stanislav Angelovic fe773958d1 refactor: rename request_slot tag to return_slot 2023-12-30 21:11:02 +01:00
Stanislav Angelovič bdd0bc2c27 refactor: improve Proxy signal subscription (#389)
This makes D-Bus proxy signal registration more flexible, more dynamic, and less error-prone since no `finishRegistration()` call is needed. A proxy can register to a signal at any time during its lifetime, and can unregister freely by simply destroying the associated slot.
2023-12-30 21:11:02 +01:00
Stanislav Angelovič e3040c0998 refactor: improve Object vtable registration (#388)
This improves the D-Bus object API registration/unregistration by making it more flexible, more dynamic, closer to sd-bus API design but still on high abstraction level, and -- most importantly -- less error-prone since no `finishRegistration()` call is needed anymore.
2023-12-30 21:11:02 +01:00
Stanislav Angelovič 29bae0aaa8 fix: add missing header to ScopeGuard.h 2023-12-30 21:11:02 +01:00
Stanislav Angelovič 8affda1678 refactor: use sd_bus_match_signal() for signal registration (#372)
sd_bus_match_signal() is more convenient than more general sd_bus_add_match() for signal registration, and requires less code on our side. Plus, systemd of at least v238 is required for sdbus-c++ v2, and this version already ships with sd_bus_match_signal().
2023-12-30 21:11:02 +01:00
Stanislav Angelovič f38d578478 refactor: simplify async call state flag (#368)
Since synchronous D-Bus calls are simplified in v2.0 and no more implemented in terms of an async call, the issue reported in #362 is no more relevant, and we can return to the simple boolean flag for indicating a finished call.
2023-12-30 21:11:02 +01:00
Stanislav Angelovič 071e38c9de refactor: let callbacks take message objects by value (#367)
Signatures of callbacks async_reply_handler, signal_handler, message_handler and property_set_callback were modified to take input message objects by value, as opposed to non-const ref.

The callee assumes ownership of the message. This API is more idiomatic, more expressive, cleaner and safer. Move semantics is used to pass messages to the callback handlers. In some cases, this also improves performance.
2023-12-30 21:11:02 +01:00
Stanislav Angelovic 21dd77ba6c refactor: use sd-bus API to get current message 2023-12-30 21:11:02 +01:00
Marcel Hellwig ab2032592b fix: prevent installing a time event, when there's no need for a timeout
See #324 for discussion
2023-12-30 21:11:02 +01:00
Stanislav Angelovič e95024192c feat: introduce sd-event integration 2023-12-30 21:11:02 +01:00
Stanislav Angelovic 0f99724449 refactor: simplify async D-Bus connection handling 2023-12-30 21:10:51 +01:00
Stanislav Angelovič 28921ad424 fix: request name signal handling issue (#392)
In case a signal arrives during the `RequestName` or `ReleaseName` D-Bus call (which is a synchronous call), the signal may not be processed immediately, which is a bug. This is solved now by waking up the event loop.
2023-12-30 17:37:00 +01:00
Stanislav Angelovič 721f583db1 refactor: handle exceptions correctly also in match callback handlers (#386)
This is a follow-up to #375. It covers match callback handlers that were missed in #375.
2023-12-05 19:06:41 +01:00
Stanislav Angelovič 47a84ab889 feat: implement Overload pattern in utils (#385) 2023-12-05 18:16:59 +01:00
Stanislav Angelovič d80483cdc0 feat: extend ScopeGuard with success and failure overloads (#384) 2023-12-05 18:16:45 +01:00
David ReissandDavid Reiss 6e348f3910 refactor: minor UnixFd cleanups (#376)
* chore: Use std::exchange in UnixFd

This was suggested in code review for #376 .

* fix: Protect against UnixFd self-assignment

While self-assignment is rare, it is expected to be safe.  Add a check
to prevent putting the object in an invalid state.

* fix: Improve hygiene around dup system call

- Don't try to call dup on a negative value.
- Check dup return code and throw if it fails, rather than returning an
  empty UnixFd object.

* chore: Move UnixFd::close to Types.cpp

Minor convenience for applications: unistd.h doesn't have to be included
in the public header.

---------

Co-authored-by: David Reiss <dreiss@meta.com>
2023-11-18 18:11:00 +01:00
Stanislav Angelovič e2b3e98374 refactor: improve handling of exceptions from callback handlers (#375)
* Catch and process all exceptions (not just sdbus::Error) from callback handlers
* Unify handling of exceptions from all types of callbacks -- always set sd_bus_error and return a negative result number in case of exception

Although libsystemd logs (with DEBUG severity) all errors from such callback handlers (except method callback handler), it seems to be out of our control. One of handy sdbus-c++ features could be the ability for clients to install a log callback, which sdbus-c++ would call in case of exceptions flying from callback handlers. In case something doesn't work for clients (especially novices), they can first look into these logs.

This may be handy in common situations like ignored signals on client side because of the inadvertent mismatch between real signal signature and signal handler signature. Like here: #373. (Although in this specific case of signals, there is a solution with an additional const sdbus::Error* argument that would reveal such an error.)
2023-11-03 18:03:01 +01:00
Stanislav Angelovič 9490b3351f feat: add support for async registration of matches (#374) 2023-11-03 17:57:52 +01:00
Stanislav Angelovič 9da18aec25 chore: remove obsolete TODO comments 2023-10-31 15:54:49 +01:00
0ad2553417 fix: use-after-return in synchronous calls (#362)
* fix: Use-after-return in synchronous calls

This bug was introduced by c39bc637b8 and can be reproduced by
configuring with

  cmake -S . -B build -DBUILD_TESTS=yes -DCMAKE_CXX_COMPILER=clang++ \
    -DCMAKE_CXX_FLAGS="-Wno-error=deprecated-copy -fno-omit-frame-pointer -fsanitize=address -fsanitize-address-use-after-scope"

and running `cmake --buid build && cmake --build build -t test` or
`build/tests/sdbus-c++-integration-tests --gtest_filter=SdbusTestObject.HandlesCorrectlyABulkOfParallelServerSideAsyncMethods`

The issue is that `sdbus_async_reply_handler` can call `removeCall`, which
writes to `data->finished`, but `data` can point to the stack of
`sendMethodCallMessageAndWaitForReply`, which can return as soon as
`asyncCallData->callback` is called.

As a fix, I restored some of the logic removed in c39bc637b8.
Specifically, in `sdbus_async_reply_handler`, I make a copy of some data
from `asyncCallData` (a new `state` field instead of `slot`), and in the
`SCOPE_GUARD`, I don't call `removeCall` if the call was actually
synchronous.

* refactor: use enum class instead of int

---------

Co-authored-by: David Reiss <dreiss@meta.com>
Co-authored-by: Stanislav Angelovič <stanislav.angelovic@protonmail.com>
2023-10-09 20:01:45 +02:00
Stanislav Angelovič 621b3d0862 feat: enable creation of IConnection from sd_bus object (#363) 2023-10-09 15:46:39 +02:00
cfb71bd6cf feat: add support for direct connections (#350)
* feat: add support for direct connections

* refactor: simplify a bit, change comments, extend tests

* fix: compiler warning about unused variable

* docs: add section on direct connections to the tutorial

---------

Co-authored-by: Maksim Fedyarov <m.fedyarov@omp.ru>
Co-authored-by: Stanislav Angelovič <stanislav.angelovic@protonmail.com>
2023-09-25 20:12:34 +02:00
Jan Beich 1e2d13a04a feat: add FreeBSD support (#358)
* chore: don't use systemd headers with elogind

In file included from src/VTableUtils.c:27:
src/VTableUtils.h:30:10: fatal error: 'systemd/sd-bus.h' file not found
 #include <systemd/sd-bus.h>
          ^~~~~~~~~~~~~~~~~~

* chore: add basu support

Similar to elogind but also supported on non-Linux.

* chore(tests): permit /var/lib/machine-id on non-systemd

https://github.com/elogind/elogind/commit/84fdc0fc61c1
https://git.sr.ht/~emersion/basu/commit/8324e6729231

* chore(ci): add simple freebsd job

Mainly to cover libc++ and basu.

* chore(ci): explicitly pass CMAKE_INSTALL_PREFIX

Some sdbus-cpp tests require configuring system bus. However, Linux
testing relies on writing outside of prefix in order to affect current
system bus instance instead of launching a dedicated one.

* chore(tests): respect CMAKE_INSTALL_PREFIX for system bus config

DBus isn't part of base system on BSDs, so may not use /etc for configs.
Also, testing installation failed as non-root:

$ cmake -DBUILD_TESTS=1 -DCMAKE_INSTALL_PREFIX=/tmp/sdbus-cpp_prefix -DTESTS_INSTALL_PATH=/tmp/sdbus-cpp_prefix/tests
$ cmake --build .
$ cmake --install .
[...]
CMake Error at tests/cmake_install.cmake:105 (file):
  file cannot create directory: /etc/dbus-1/system.d.  Maybe need
  administrative privileges.

* chore(tests): temporarily skip 1 test on FreeBSD to keep CI happy

* chore(ci): run tests in freebsd job
2023-09-18 11:35:23 +02:00
Stanislav Angelovič 3e84b254e9 refactor: improve type extensibility and its safety (#348) 2023-08-09 12:39:16 +02:00
Stanislav Angelovič 24a3d83c3f chore: fix file permissions 2023-08-04 13:30:34 +02:00
Marcel Hellwig dcd9d46b9c style: remove trailing whitespace (#345)
* style: restore correct 644 file permission

* style: remove trailing whitespace
2023-08-04 13:26:45 +02:00
Stanislav AngelovičandMarcel Hellwig fb61420bf0 feat: support serialization of array, span and unordered_map (#342)
* feat: support serialization of array, span and unordered_map

* fix some spelling mistakes

* docs: update table of valid c++ types

---------

Co-authored-by: Marcel Hellwig <github@cookiesoft.de>
2023-08-03 13:55:37 +02:00
29c877a89a perf: optimize serialization of arrays of trivial D-Bus types (#340)
* Improve performance of std::vector<> per Issue #339.

* refactor: improve performance of vector serialization

---------

Co-authored-by: Plinio Andrade <plinio.andrade@oracle.com>
Co-authored-by: Stanislav Angelovič <stanislav.angelovic@protonmail.com>
2023-07-27 18:31:49 +02:00
Stanislav Angelovič 98f4929337 fix: pseudo-connection static lifetime issue 2023-07-26 08:57:21 +02:00
Anthony Brandon c39bc637b8 fix: race condition in async Proxy::callMethod
Signed-off-by: Anthony Brandon <anthony@amarulasolutions.com>
2023-02-07 22:23:35 +01:00
Stanislav Angelovič 737f04abc7 feat: add support for std::future-based async calls 2023-02-07 12:31:31 +01:00
Stanislav Angelovic 3a56113422 fix: fix namespace name in ScopeGuard 2023-01-21 01:34:34 +01:00
Stanislav Angelovic c9e157e3e1 fix: flush long messages after sending 2023-01-05 15:12:39 +01:00
Stanislav Angelovic 74d849d933 feat: add support for proxy with no event loop thread 2022-09-05 17:25:37 +02:00
Stanislav Angelovic 2991fa4960 refactor: use pseudo D-Bus connection for plain messages 2022-08-09 08:55:29 +02:00
alivenetsandStanislav Angelovic 0f2362d8c3 feat: add support for session bus connection at custom address (#273)
* Add methods to initiate custom session bus connection

The new function helper `createSessionBusConnectionWithAddress` allows to create connection to session bus with custom address.

Signed-off-by: Alexander Livenets <a.livenets@gmail.com>

* feat: add support for session bus connection at custom address

Co-authored-by: Stanislav Angelovic <stanislav.angelovic@siemens.com>
2022-08-08 13:54:09 +02:00
Stanislav Angelovic e07c1f3981 chore: update doxygen header info 2022-07-05 18:10:05 +02:00
Stanislav Angelovic 4e105081c9 fix: remove executable flag from source files 2022-06-27 12:17:18 +02:00
Stanislav Angelovic 5ec6027d5f feat: add support for match rules 2022-06-27 12:14:57 +02:00
Stanislav Angelovic d864e1dfa4 refactor: rename dont_request_slot tag to floating_slot 2022-06-27 12:05:06 +02:00
Stanislav Angelovic b7f3d7c876 refactor: little fixes and reorganizations around Connection class 2022-06-22 18:12:42 +02:00