2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2013-12-31 05:13:38 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "sd-bus.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2013-12-31 05:13:38 +01:00
|
|
|
#include "bus-internal.h"
|
|
|
|
#include "bus-message.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "bus-util.h"
|
2013-12-31 05:13:38 +01:00
|
|
|
#include "refcnt.h"
|
2018-09-13 13:34:12 +02:00
|
|
|
#include "tests.h"
|
2013-12-31 05:13:38 +01:00
|
|
|
|
|
|
|
static void test_bus_new(void) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
|
2013-12-31 05:13:38 +01:00
|
|
|
|
|
|
|
assert_se(sd_bus_new(&bus) == 0);
|
|
|
|
printf("after new: refcount %u\n", REFCNT_GET(bus->n_ref));
|
|
|
|
}
|
|
|
|
|
2014-10-12 22:05:49 +02:00
|
|
|
static int test_bus_open(void) {
|
2016-01-05 16:35:23 +01:00
|
|
|
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
2014-10-12 22:05:49 +02:00
|
|
|
int r;
|
2013-12-31 05:13:38 +01:00
|
|
|
|
2017-09-19 14:17:57 +02:00
|
|
|
r = sd_bus_open_user(&bus);
|
2017-09-28 10:17:04 +02:00
|
|
|
if (IN_SET(r, -ECONNREFUSED, -ENOENT))
|
2014-10-12 22:05:49 +02:00
|
|
|
return r;
|
|
|
|
|
|
|
|
assert_se(r >= 0);
|
2013-12-31 05:13:38 +01:00
|
|
|
printf("after open: refcount %u\n", REFCNT_GET(bus->n_ref));
|
2014-10-12 22:05:49 +02:00
|
|
|
|
|
|
|
return 0;
|
2013-12-31 05:13:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_bus_new_method_call(void) {
|
|
|
|
sd_bus *bus = NULL;
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
|
2013-12-31 05:13:38 +01:00
|
|
|
|
2017-09-19 14:17:57 +02:00
|
|
|
assert_se(sd_bus_open_user(&bus) >= 0);
|
2013-12-31 05:13:38 +01:00
|
|
|
|
2014-02-19 23:54:58 +01:00
|
|
|
assert_se(sd_bus_message_new_method_call(bus, &m, "a.service.name", "/an/object/path", "an.interface.name", "AMethodName") >= 0);
|
2013-12-31 05:13:38 +01:00
|
|
|
|
|
|
|
printf("after message_new_method_call: refcount %u\n", REFCNT_GET(bus->n_ref));
|
|
|
|
|
2016-01-05 16:35:23 +01:00
|
|
|
sd_bus_flush_close_unref(bus);
|
|
|
|
printf("after bus_flush_close_unref: refcount %u\n", m->n_ref);
|
2013-12-31 05:13:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_bus_new_signal(void) {
|
|
|
|
sd_bus *bus = NULL;
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
|
2013-12-31 05:13:38 +01:00
|
|
|
|
2017-09-19 14:17:57 +02:00
|
|
|
assert_se(sd_bus_open_user(&bus) >= 0);
|
2013-12-31 05:13:38 +01:00
|
|
|
|
2014-02-19 23:54:58 +01:00
|
|
|
assert_se(sd_bus_message_new_signal(bus, &m, "/an/object/path", "an.interface.name", "Name") >= 0);
|
2013-12-31 05:13:38 +01:00
|
|
|
|
|
|
|
printf("after message_new_signal: refcount %u\n", REFCNT_GET(bus->n_ref));
|
|
|
|
|
2016-01-05 16:35:23 +01:00
|
|
|
sd_bus_flush_close_unref(bus);
|
|
|
|
printf("after bus_flush_close_unref: refcount %u\n", m->n_ref);
|
2013-12-31 05:13:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2018-09-13 14:31:13 +02:00
|
|
|
test_setup_logging(LOG_INFO);
|
2013-12-31 05:13:38 +01:00
|
|
|
|
|
|
|
test_bus_new();
|
2018-09-13 13:34:12 +02:00
|
|
|
|
|
|
|
if (test_bus_open() < 0)
|
|
|
|
return log_tests_skipped("Failed to connect to bus");
|
2014-10-12 22:05:49 +02:00
|
|
|
|
2013-12-31 05:13:38 +01:00
|
|
|
test_bus_new_method_call();
|
|
|
|
test_bus_new_signal();
|
2014-10-12 22:05:49 +02:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2013-12-31 05:13:38 +01:00
|
|
|
}
|