1 /* SPDX-License-Identifier: MIT-0 */
8 #include <systemd/sd-bus.h>
10 #define _cleanup_(f) __attribute__((cleanup(f)))
14 errno = r < 0 ? -r : 0; \
15 printf(#x ": %m\n"); \
17 return EXIT_FAILURE; \
20 typedef struct object
{
25 static int method(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
26 printf("Got called with userdata=%p\n", userdata
);
28 if (sd_bus_message_is_method_call(m
,
29 "org.freedesktop.systemd.VtableExample",
34 check(sd_bus_message_read(m
, "s", &string
));
35 check(sd_bus_reply_method_return(m
, "s", string
));
40 static const sd_bus_vtable vtable
[] = {
41 SD_BUS_VTABLE_START(0),
43 "Method1", "s", "s", method
, 0),
44 SD_BUS_METHOD_WITH_NAMES_OFFSET(
46 "so", SD_BUS_PARAM(string
) SD_BUS_PARAM(path
),
47 "s", SD_BUS_PARAM(returnstring
),
48 method
, offsetof(object
, number
),
49 SD_BUS_VTABLE_DEPRECATED
),
50 SD_BUS_METHOD_WITH_ARGS_OFFSET(
52 SD_BUS_ARGS("s", string
, "o", path
),
53 SD_BUS_RESULT("s", returnstring
),
54 method
, offsetof(object
, number
),
55 SD_BUS_VTABLE_UNPRIVILEGED
),
56 SD_BUS_METHOD_WITH_ARGS(
61 SD_BUS_VTABLE_UNPRIVILEGED
),
66 SD_BUS_SIGNAL_WITH_NAMES(
68 "so", SD_BUS_PARAM(string
) SD_BUS_PARAM(path
),
70 SD_BUS_SIGNAL_WITH_ARGS(
72 SD_BUS_ARGS("s", string
, "o", path
),
74 SD_BUS_WRITABLE_PROPERTY(
75 "AutomaticStringProperty", "s", NULL
, NULL
,
76 offsetof(object
, name
),
77 SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
78 SD_BUS_WRITABLE_PROPERTY(
79 "AutomaticIntegerProperty", "u", NULL
, NULL
,
80 offsetof(object
, number
),
81 SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
85 int main(int argc
, char **argv
) {
86 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
90 object object
= { .number
= 666 };
91 check((object
.name
= strdup("name")) != NULL
);
93 check(sd_bus_add_object_vtable(bus
, NULL
,
94 "/org/freedesktop/systemd/VtableExample",
95 "org.freedesktop.systemd.VtableExample",
99 check(sd_bus_request_name(bus
,
100 "org.freedesktop.systemd.VtableExample",
104 check(sd_bus_wait(bus
, UINT64_MAX
));
105 check(sd_bus_process(bus
, NULL
));
108 check(sd_bus_release_name(bus
, "org.freedesktop.systemd.VtableExample"));