]> git.ipfire.org Git - thirdparty/systemd.git/blame - man/vtable-example.c
man: fix link markup
[thirdparty/systemd.git] / man / vtable-example.c
CommitLineData
afb9c0c9
ZJS
1#include <errno.h>
2#include <stdbool.h>
3#include <stddef.h>
4#include <stdlib.h>
5#include <stdio.h>
6#include <systemd/sd-bus.h>
7
8#define _cleanup_(f) __attribute__((cleanup(f)))
9
10typedef struct object {
11 char *name;
12 uint32_t number;
13} object;
14
15static int method(sd_bus_message *m, void *userdata, sd_bus_error *error) {
16 printf("Got called with userdata=%p\n", userdata);
17 return 1;
18}
19
20static const sd_bus_vtable vtable[] = {
21 SD_BUS_VTABLE_START(0),
22 SD_BUS_METHOD(
23 "Method1", "s", "s", method, 0),
24 SD_BUS_METHOD_WITH_NAMES_OFFSET(
25 "Method2",
26 "so", SD_BUS_PARAM(string) SD_BUS_PARAM(path),
27 "s", SD_BUS_PARAM(returnstring),
28 method, offsetof(object, number),
29 SD_BUS_VTABLE_DEPRECATED),
eff7c2d3
DDM
30 SD_BUS_METHOD_WITH_ARGS_OFFSET(
31 "Method3",
32 SD_BUS_ARGS("s", string, "o", path),
33 SD_BUS_RESULT("s", returnstring),
34 method, offsetof(object, number),
35 SD_BUS_VTABLE_UNPRIVILEGED),
36 SD_BUS_METHOD_WITH_ARGS(
37 "Method4",
38 SD_BUS_NO_ARGS,
39 SD_BUS_NO_RESULT,
40 method,
41 SD_BUS_VTABLE_UNPRIVILEGED),
42 SD_BUS_SIGNAL(
43 "Signal1",
44 "so",
45 0),
46 SD_BUS_SIGNAL_WITH_NAMES(
47 "Signal2",
48 "so", SD_BUS_PARAM(string) SD_BUS_PARAM(path),
49 0),
50 SD_BUS_SIGNAL_WITH_ARGS(
51 "Signal3",
52 SD_BUS_ARGS("s", string, "o", path),
53 0),
afb9c0c9
ZJS
54 SD_BUS_WRITABLE_PROPERTY(
55 "AutomaticStringProperty", "s", NULL, NULL,
56 offsetof(object, name),
57 SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
58 SD_BUS_WRITABLE_PROPERTY(
59 "AutomaticIntegerProperty", "u", NULL, NULL,
60 offsetof(object, number),
61 SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
62 SD_BUS_VTABLE_END
63};
64
65#define check(x) ({ \
66 int r = x; \
67 errno = r < 0 ? -r : 0; \
68 printf(#x ": %m\n"); \
69 if (r < 0) \
70 return EXIT_FAILURE; \
71 })
72
73int main(int argc, char **argv) {
74 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
75
76 sd_bus_default(&bus);
77
78 object object = { .number = 666 };
79 check((object.name = strdup("name")) != NULL);
80
81 check(sd_bus_add_object_vtable(bus, NULL, "/object",
82 "org.freedesktop.systemd.VtableExample",
83 vtable,
84 &object));
85
ed0cb346 86 for (;;) {
afb9c0c9
ZJS
87 check(sd_bus_wait(bus, UINT64_MAX));
88 check(sd_bus_process(bus, NULL));
89 }
90
91 free(object.name);
92
93 return 0;
94}