]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/test-bus-watch-bind.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / libsystemd / sd-bus / test-bus-watch-bind.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <pthread.h>
4
5 #include "sd-bus.h"
6 #include "sd-event.h"
7 #include "sd-id128.h"
8
9 #include "alloc-util.h"
10 #include "fd-util.h"
11 #include "fileio.h"
12 #include "fs-util.h"
13 #include "mkdir.h"
14 #include "path-util.h"
15 #include "random-util.h"
16 #include "rm-rf.h"
17 #include "socket-util.h"
18 #include "string-util.h"
19
20 static int method_foobar(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
21 log_info("Got Foobar() call.");
22
23 assert_se(sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), 0) >= 0);
24 return sd_bus_reply_method_return(m, NULL);
25 }
26
27 static int method_exit(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
28 log_info("Got Exit() call");
29 assert_se(sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), 1) >= 0);
30 return sd_bus_reply_method_return(m, NULL);
31 }
32
33 static const sd_bus_vtable vtable[] = {
34 SD_BUS_VTABLE_START(0),
35 SD_BUS_METHOD("Foobar", NULL, NULL, method_foobar, SD_BUS_VTABLE_UNPRIVILEGED),
36 SD_BUS_METHOD("Exit", NULL, NULL, method_exit, SD_BUS_VTABLE_UNPRIVILEGED),
37 SD_BUS_VTABLE_END,
38 };
39
40 static void* thread_server(void *p) {
41 _cleanup_free_ char *suffixed = NULL, *suffixed2 = NULL, *d = NULL;
42 _cleanup_close_ int fd = -1;
43 union sockaddr_union u = {};
44 const char *path = p;
45 int salen;
46
47 log_debug("Initializing server");
48
49 /* Let's play some games, by slowly creating the socket directory, and renaming it in the middle */
50 (void) usleep(100 * USEC_PER_MSEC);
51
52 assert_se(mkdir_parents(path, 0755) >= 0);
53 (void) usleep(100 * USEC_PER_MSEC);
54
55 d = dirname_malloc(path);
56 assert_se(d);
57 assert_se(asprintf(&suffixed, "%s.%" PRIx64, d, random_u64()) >= 0);
58 assert_se(rename(d, suffixed) >= 0);
59 (void) usleep(100 * USEC_PER_MSEC);
60
61 assert_se(asprintf(&suffixed2, "%s.%" PRIx64, d, random_u64()) >= 0);
62 assert_se(symlink(suffixed2, d) >= 0);
63 (void) usleep(100 * USEC_PER_MSEC);
64
65 assert_se(symlink(basename(suffixed), suffixed2) >= 0);
66 (void) usleep(100 * USEC_PER_MSEC);
67
68 salen = sockaddr_un_set_path(&u.un, path);
69 assert_se(salen >= 0);
70
71 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
72 assert_se(fd >= 0);
73
74 assert_se(bind(fd, &u.sa, salen) >= 0);
75 usleep(100 * USEC_PER_MSEC);
76
77 assert_se(listen(fd, SOMAXCONN) >= 0);
78 usleep(100 * USEC_PER_MSEC);
79
80 assert_se(touch(path) >= 0);
81 usleep(100 * USEC_PER_MSEC);
82
83 log_debug("Initialized server");
84
85 for (;;) {
86 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
87 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
88 sd_id128_t id;
89 int bus_fd, code;
90
91 assert_se(sd_id128_randomize(&id) >= 0);
92
93 assert_se(sd_event_new(&event) >= 0);
94
95 bus_fd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
96 assert_se(bus_fd >= 0);
97
98 log_debug("Accepted server connection");
99
100 assert_se(sd_bus_new(&bus) >= 0);
101 assert_se(sd_bus_set_description(bus, "server") >= 0);
102 assert_se(sd_bus_set_fd(bus, bus_fd, bus_fd) >= 0);
103 assert_se(sd_bus_set_server(bus, true, id) >= 0);
104 /* assert_se(sd_bus_set_anonymous(bus, true) >= 0); */
105
106 assert_se(sd_bus_attach_event(bus, event, 0) >= 0);
107
108 assert_se(sd_bus_add_object_vtable(bus, NULL, "/foo", "foo.TestInterface", vtable, NULL) >= 0);
109
110 assert_se(sd_bus_start(bus) >= 0);
111
112 assert_se(sd_event_loop(event) >= 0);
113
114 assert_se(sd_event_get_exit_code(event, &code) >= 0);
115
116 if (code > 0)
117 break;
118 }
119
120 log_debug("Server done");
121
122 return NULL;
123 }
124
125 static void* thread_client1(void *p) {
126 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
127 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
128 const char *path = p, *t;
129 int r;
130
131 log_debug("Initializing client1");
132
133 assert_se(sd_bus_new(&bus) >= 0);
134 assert_se(sd_bus_set_description(bus, "client1") >= 0);
135
136 t = strjoina("unix:path=", path);
137 assert_se(sd_bus_set_address(bus, t) >= 0);
138 assert_se(sd_bus_set_watch_bind(bus, true) >= 0);
139 assert_se(sd_bus_start(bus) >= 0);
140
141 r = sd_bus_call_method(bus, "foo.bar", "/foo", "foo.TestInterface", "Foobar", &error, NULL, NULL);
142 assert_se(r >= 0);
143
144 log_debug("Client1 done");
145
146 return NULL;
147 }
148
149 static int client2_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
150 assert_se(sd_bus_message_is_method_error(m, NULL) == 0);
151 assert_se(sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), 0) >= 0);
152 return 0;
153 }
154
155 static void* thread_client2(void *p) {
156 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
157 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
158 const char *path = p, *t;
159
160 log_debug("Initializing client2");
161
162 assert_se(sd_event_new(&event) >= 0);
163 assert_se(sd_bus_new(&bus) >= 0);
164 assert_se(sd_bus_set_description(bus, "client2") >= 0);
165
166 t = strjoina("unix:path=", path);
167 assert_se(sd_bus_set_address(bus, t) >= 0);
168 assert_se(sd_bus_set_watch_bind(bus, true) >= 0);
169 assert_se(sd_bus_attach_event(bus, event, 0) >= 0);
170 assert_se(sd_bus_start(bus) >= 0);
171
172 assert_se(sd_bus_call_method_async(bus, NULL, "foo.bar", "/foo", "foo.TestInterface", "Foobar", client2_callback, NULL, NULL) >= 0);
173
174 assert_se(sd_event_loop(event) >= 0);
175
176 log_debug("Client2 done");
177
178 return NULL;
179 }
180
181 static void request_exit(const char *path) {
182 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
183 const char *t;
184
185 assert_se(sd_bus_new(&bus) >= 0);
186
187 t = strjoina("unix:path=", path);
188 assert_se(sd_bus_set_address(bus, t) >= 0);
189 assert_se(sd_bus_set_watch_bind(bus, true) >= 0);
190 assert_se(sd_bus_set_description(bus, "request-exit") >= 0);
191 assert_se(sd_bus_start(bus) >= 0);
192
193 assert_se(sd_bus_call_method(bus, "foo.bar", "/foo", "foo.TestInterface", "Exit", NULL, NULL, NULL) >= 0);
194 }
195
196 int main(int argc, char *argv[]) {
197 _cleanup_(rm_rf_physical_and_freep) char *d = NULL;
198 pthread_t server, client1, client2;
199 char *path;
200
201 log_set_max_level(LOG_DEBUG);
202
203 /* We use /dev/shm here rather than /tmp, since some weird distros might set up /tmp as some weird fs that
204 * doesn't support inotify properly. */
205 assert_se(mkdtemp_malloc("/dev/shm/systemd-watch-bind-XXXXXX", &d) >= 0);
206
207 path = strjoina(d, "/this/is/a/socket");
208
209 assert_se(pthread_create(&server, NULL, thread_server, path) == 0);
210 assert_se(pthread_create(&client1, NULL, thread_client1, path) == 0);
211 assert_se(pthread_create(&client2, NULL, thread_client2, path) == 0);
212
213 assert_se(pthread_join(client1, NULL) == 0);
214 assert_se(pthread_join(client2, NULL) == 0);
215
216 request_exit(path);
217
218 assert_se(pthread_join(server, NULL) == 0);
219
220 return 0;
221 }