]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
89ffcd2a | 2 | |
cf0fbc49 | 3 | #include <fcntl.h> |
89ffcd2a | 4 | #include <pthread.h> |
cf0fbc49 | 5 | #include <stdlib.h> |
e3017af9 | 6 | #include <unistd.h> |
89ffcd2a | 7 | |
89ffcd2a | 8 | #include "sd-bus.h" |
07630cea | 9 | |
b5efdb8a | 10 | #include "alloc-util.h" |
e3017af9 | 11 | #include "bus-error.h" |
392d5b37 | 12 | #include "bus-internal.h" |
07630cea | 13 | #include "bus-match.h" |
4bbccb02 | 14 | #include "errno-util.h" |
cf0fbc49 | 15 | #include "fd-util.h" |
f97b34a6 | 16 | #include "format-util.h" |
07630cea LP |
17 | #include "log.h" |
18 | #include "macro.h" | |
6969a671 | 19 | #include "string-util.h" |
317bb217 | 20 | #include "tests.h" |
392d5b37 | 21 | |
19070062 | 22 | static int match_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
85bc6b05 EM |
23 | log_info("Match triggered! destination=%s interface=%s member=%s", |
24 | strna(sd_bus_message_get_destination(m)), | |
25 | strna(sd_bus_message_get_interface(m)), | |
26 | strna(sd_bus_message_get_member(m))); | |
392d5b37 LP |
27 | return 0; |
28 | } | |
89ffcd2a | 29 | |
19070062 | 30 | static int object_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
a652755d LP |
31 | int r; |
32 | ||
eb01ba5d | 33 | if (sd_bus_message_is_method_error(m, NULL)) |
a652755d LP |
34 | return 0; |
35 | ||
36 | if (sd_bus_message_is_method_call(m, "org.object.test", "Foobar")) { | |
a652755d LP |
37 | log_info("Invoked Foobar() on %s", sd_bus_message_get_path(m)); |
38 | ||
df2d202e | 39 | r = sd_bus_reply_method_return(m, NULL); |
f647962d MS |
40 | if (r < 0) |
41 | return log_error_errno(r, "Failed to send reply: %m"); | |
a652755d LP |
42 | |
43 | return 1; | |
44 | } | |
45 | ||
46 | return 0; | |
47 | } | |
48 | ||
89ffcd2a LP |
49 | static int server_init(sd_bus **_bus) { |
50 | sd_bus *bus = NULL; | |
d728d708 | 51 | sd_id128_t id; |
89ffcd2a | 52 | int r; |
56fbd718 | 53 | const char *unique, *desc; |
89ffcd2a | 54 | |
0c0cdb06 | 55 | assert_se(_bus); |
89ffcd2a | 56 | |
56fbd718 | 57 | r = sd_bus_open_user_with_description(&bus, "my bus!"); |
89ffcd2a | 58 | if (r < 0) { |
da927ba9 | 59 | log_error_errno(r, "Failed to connect to user bus: %m"); |
89ffcd2a LP |
60 | goto fail; |
61 | } | |
62 | ||
5c302692 | 63 | r = sd_bus_get_bus_id(bus, &id); |
d728d708 | 64 | if (r < 0) { |
da927ba9 | 65 | log_error_errno(r, "Failed to get server ID: %m"); |
d728d708 LP |
66 | goto fail; |
67 | } | |
68 | ||
20902f3e LP |
69 | r = sd_bus_get_unique_name(bus, &unique); |
70 | if (r < 0) { | |
da927ba9 | 71 | log_error_errno(r, "Failed to get unique name: %m"); |
20902f3e LP |
72 | goto fail; |
73 | } | |
74 | ||
90e207e4 | 75 | assert_se(sd_bus_get_description(bus, &desc) >= 0); |
56fbd718 ZJS |
76 | assert_se(streq(desc, "my bus!")); |
77 | ||
d728d708 | 78 | log_info("Peer ID is " SD_ID128_FORMAT_STR ".", SD_ID128_FORMAT_VAL(id)); |
20902f3e | 79 | log_info("Unique ID: %s", unique); |
d728d708 | 80 | log_info("Can send file handles: %i", sd_bus_can_send(bus, 'h')); |
d728d708 | 81 | |
89ffcd2a LP |
82 | r = sd_bus_request_name(bus, "org.freedesktop.systemd.test", 0); |
83 | if (r < 0) { | |
da927ba9 | 84 | log_error_errno(r, "Failed to acquire name: %m"); |
89ffcd2a LP |
85 | goto fail; |
86 | } | |
87 | ||
19befb2d | 88 | r = sd_bus_add_fallback(bus, NULL, "/foo/bar", object_callback, NULL); |
a652755d | 89 | if (r < 0) { |
da927ba9 | 90 | log_error_errno(r, "Failed to add object: %m"); |
a652755d LP |
91 | goto fail; |
92 | } | |
93 | ||
75152a4d | 94 | r = sd_bus_match_signal(bus, NULL, NULL, NULL, "foo.bar", "Notify", match_callback, NULL); |
392d5b37 | 95 | if (r < 0) { |
75152a4d | 96 | log_error_errno(r, "Failed to request match: %m"); |
392d5b37 LP |
97 | goto fail; |
98 | } | |
99 | ||
85bc6b05 EM |
100 | r = sd_bus_match_signal(bus, NULL, NULL, NULL, "foo.bar", "NotifyTo", match_callback, NULL); |
101 | if (r < 0) { | |
102 | log_error_errno(r, "Failed to request match: %m"); | |
103 | goto fail; | |
104 | } | |
105 | ||
19befb2d | 106 | r = sd_bus_add_match(bus, NULL, "type='signal',interface='org.freedesktop.DBus',member='NameOwnerChanged'", match_callback, NULL); |
392d5b37 | 107 | if (r < 0) { |
da927ba9 | 108 | log_error_errno(r, "Failed to add match: %m"); |
392d5b37 LP |
109 | goto fail; |
110 | } | |
111 | ||
fc561c8e | 112 | bus_match_dump(stdout, &bus->match_callbacks, 0); |
392d5b37 | 113 | |
89ffcd2a LP |
114 | *_bus = bus; |
115 | return 0; | |
116 | ||
117 | fail: | |
3e044c49 | 118 | sd_bus_unref(bus); |
89ffcd2a LP |
119 | return r; |
120 | } | |
121 | ||
e3017af9 | 122 | static int server(sd_bus *bus) { |
89ffcd2a | 123 | int r; |
e3017af9 | 124 | bool client1_gone = false, client2_gone = false; |
89ffcd2a | 125 | |
e3017af9 | 126 | while (!client1_gone || !client2_gone) { |
4afd3348 | 127 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
2571ead1 | 128 | pid_t pid = 0; |
4a875b61 | 129 | const char *label = NULL; |
89ffcd2a LP |
130 | |
131 | r = sd_bus_process(bus, &m); | |
132 | if (r < 0) { | |
da927ba9 | 133 | log_error_errno(r, "Failed to process requests: %m"); |
89ffcd2a LP |
134 | goto fail; |
135 | } | |
e3017af9 | 136 | |
89ffcd2a | 137 | if (r == 0) { |
f5fbe71d | 138 | r = sd_bus_wait(bus, UINT64_MAX); |
89ffcd2a | 139 | if (r < 0) { |
da927ba9 | 140 | log_error_errno(r, "Failed to wait: %m"); |
89ffcd2a LP |
141 | goto fail; |
142 | } | |
143 | ||
144 | continue; | |
145 | } | |
146 | ||
e3017af9 LP |
147 | if (!m) |
148 | continue; | |
149 | ||
5b12334d LP |
150 | sd_bus_creds_get_pid(sd_bus_message_get_creds(m), &pid); |
151 | sd_bus_creds_get_selinux_context(sd_bus_message_get_creds(m), &label); | |
de0671ee | 152 | log_info("Got message! member=%s pid="PID_FMT" label=%s", |
4a875b61 | 153 | strna(sd_bus_message_get_member(m)), |
de0671ee | 154 | pid, |
4a875b61 | 155 | strna(label)); |
2b4a65b6 | 156 | /* sd_bus_message_dump(m); */ |
89ffcd2a LP |
157 | /* sd_bus_message_rewind(m, true); */ |
158 | ||
159 | if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "LowerCase")) { | |
160 | const char *hello; | |
161 | _cleanup_free_ char *lowercase = NULL; | |
162 | ||
163 | r = sd_bus_message_read(m, "s", &hello); | |
164 | if (r < 0) { | |
da927ba9 | 165 | log_error_errno(r, "Failed to get parameter: %m"); |
89ffcd2a LP |
166 | goto fail; |
167 | } | |
168 | ||
89ffcd2a LP |
169 | lowercase = strdup(hello); |
170 | if (!lowercase) { | |
171 | r = log_oom(); | |
172 | goto fail; | |
173 | } | |
174 | ||
175 | ascii_strlower(lowercase); | |
176 | ||
df2d202e | 177 | r = sd_bus_reply_method_return(m, "s", lowercase); |
89ffcd2a | 178 | if (r < 0) { |
da927ba9 | 179 | log_error_errno(r, "Failed to send reply: %m"); |
89ffcd2a LP |
180 | goto fail; |
181 | } | |
e3017af9 LP |
182 | } else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "ExitClient1")) { |
183 | ||
df2d202e | 184 | r = sd_bus_reply_method_return(m, NULL); |
e3017af9 | 185 | if (r < 0) { |
da927ba9 | 186 | log_error_errno(r, "Failed to send reply: %m"); |
e3017af9 LP |
187 | goto fail; |
188 | } | |
189 | ||
190 | client1_gone = true; | |
191 | } else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "ExitClient2")) { | |
192 | ||
df2d202e | 193 | r = sd_bus_reply_method_return(m, NULL); |
e3017af9 | 194 | if (r < 0) { |
da927ba9 | 195 | log_error_errno(r, "Failed to send reply: %m"); |
e3017af9 LP |
196 | goto fail; |
197 | } | |
198 | ||
199 | client2_gone = true; | |
200 | } else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Slow")) { | |
201 | ||
b7f247e0 LP |
202 | sleep(1); |
203 | ||
df2d202e | 204 | r = sd_bus_reply_method_return(m, NULL); |
e3017af9 | 205 | if (r < 0) { |
da927ba9 | 206 | log_error_errno(r, "Failed to send reply: %m"); |
e3017af9 LP |
207 | goto fail; |
208 | } | |
209 | ||
2c93b4ef LP |
210 | } else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "FileDescriptor")) { |
211 | int fd; | |
212 | static const char x = 'X'; | |
213 | ||
214 | r = sd_bus_message_read(m, "h", &fd); | |
215 | if (r < 0) { | |
da927ba9 | 216 | log_error_errno(r, "Failed to get parameter: %m"); |
2c93b4ef LP |
217 | goto fail; |
218 | } | |
219 | ||
5a330cda ZJS |
220 | log_info("Received fd=%d", fd); |
221 | ||
2c93b4ef | 222 | if (write(fd, &x, 1) < 0) { |
56f64d95 | 223 | log_error_errno(errno, "Failed to write to fd: %m"); |
03e334a1 | 224 | safe_close(fd); |
2c93b4ef LP |
225 | goto fail; |
226 | } | |
227 | ||
df2d202e | 228 | r = sd_bus_reply_method_return(m, NULL); |
2c93b4ef | 229 | if (r < 0) { |
da927ba9 | 230 | log_error_errno(r, "Failed to send reply: %m"); |
2c93b4ef LP |
231 | goto fail; |
232 | } | |
233 | ||
e3017af9 | 234 | } else if (sd_bus_message_is_method_call(m, NULL, NULL)) { |
89ffcd2a | 235 | |
d4100e24 | 236 | r = sd_bus_reply_method_error( |
df2d202e | 237 | m, |
14c24659 | 238 | &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_UNKNOWN_METHOD, "Unknown method.")); |
89ffcd2a | 239 | if (r < 0) { |
da927ba9 | 240 | log_error_errno(r, "Failed to send reply: %m"); |
89ffcd2a LP |
241 | goto fail; |
242 | } | |
243 | } | |
244 | } | |
245 | ||
246 | r = 0; | |
247 | ||
248 | fail: | |
e3017af9 LP |
249 | if (bus) { |
250 | sd_bus_flush(bus); | |
89ffcd2a | 251 | sd_bus_unref(bus); |
e3017af9 | 252 | } |
89ffcd2a | 253 | |
e3017af9 | 254 | return r; |
89ffcd2a LP |
255 | } |
256 | ||
f2a3de01 | 257 | static void* client1(void *p) { |
4afd3348 LP |
258 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
259 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
260 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
89ffcd2a LP |
261 | const char *hello; |
262 | int r; | |
19ee48a6 | 263 | _cleanup_close_pair_ int pp[2] = PIPE_EBADF; |
2c93b4ef | 264 | char x; |
89ffcd2a LP |
265 | |
266 | r = sd_bus_open_user(&bus); | |
267 | if (r < 0) { | |
da927ba9 | 268 | log_error_errno(r, "Failed to connect to user bus: %m"); |
89ffcd2a LP |
269 | goto finish; |
270 | } | |
271 | ||
d4100e24 | 272 | r = sd_bus_call_method( |
89ffcd2a LP |
273 | bus, |
274 | "org.freedesktop.systemd.test", | |
275 | "/", | |
276 | "org.freedesktop.systemd.test", | |
277 | "LowerCase", | |
d4100e24 LP |
278 | &error, |
279 | &reply, | |
280 | "s", | |
281 | "HELLO"); | |
89ffcd2a | 282 | if (r < 0) { |
da927ba9 | 283 | log_error_errno(r, "Failed to issue method call: %m"); |
89ffcd2a LP |
284 | goto finish; |
285 | } | |
286 | ||
287 | r = sd_bus_message_read(reply, "s", &hello); | |
288 | if (r < 0) { | |
da927ba9 | 289 | log_error_errno(r, "Failed to get string: %m"); |
89ffcd2a LP |
290 | goto finish; |
291 | } | |
292 | ||
0c0cdb06 | 293 | assert_se(streq(hello, "hello")); |
89ffcd2a | 294 | |
2c93b4ef | 295 | if (pipe2(pp, O_CLOEXEC|O_NONBLOCK) < 0) { |
d7a0f1f4 | 296 | r = log_error_errno(errno, "Failed to allocate pipe: %m"); |
2c93b4ef LP |
297 | goto finish; |
298 | } | |
299 | ||
5a330cda ZJS |
300 | log_info("Sending fd=%d", pp[1]); |
301 | ||
b7f247e0 | 302 | r = sd_bus_call_method( |
2c93b4ef LP |
303 | bus, |
304 | "org.freedesktop.systemd.test", | |
305 | "/", | |
306 | "org.freedesktop.systemd.test", | |
307 | "FileDescriptor", | |
b7f247e0 LP |
308 | &error, |
309 | NULL, | |
310 | "h", | |
311 | pp[1]); | |
2c93b4ef | 312 | if (r < 0) { |
da927ba9 | 313 | log_error_errno(r, "Failed to issue method call: %m"); |
2c93b4ef LP |
314 | goto finish; |
315 | } | |
316 | ||
317 | errno = 0; | |
318 | if (read(pp[0], &x, 1) <= 0) { | |
f69ae858 | 319 | log_error("Failed to read from pipe: %s", STRERROR_OR_EOF(errno)); |
2c93b4ef LP |
320 | goto finish; |
321 | } | |
322 | ||
89ffcd2a LP |
323 | r = 0; |
324 | ||
325 | finish: | |
326 | if (bus) { | |
b9c54c46 | 327 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *q = NULL; |
89ffcd2a LP |
328 | |
329 | r = sd_bus_message_new_method_call( | |
330 | bus, | |
151b9b96 | 331 | &q, |
89ffcd2a LP |
332 | "org.freedesktop.systemd.test", |
333 | "/", | |
334 | "org.freedesktop.systemd.test", | |
151b9b96 | 335 | "ExitClient1"); |
b7f247e0 | 336 | if (r < 0) |
da927ba9 | 337 | log_error_errno(r, "Failed to allocate method call: %m"); |
b7f247e0 LP |
338 | else |
339 | sd_bus_send(bus, q, NULL); | |
89ffcd2a | 340 | |
89ffcd2a LP |
341 | } |
342 | ||
e3017af9 LP |
343 | return INT_TO_PTR(r); |
344 | } | |
345 | ||
19070062 | 346 | static int quit_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
e3017af9 LP |
347 | bool *x = userdata; |
348 | ||
9eec7d12 | 349 | log_error_errno(sd_bus_message_get_errno(m), "Quit callback: %m"); |
e3017af9 LP |
350 | |
351 | *x = 1; | |
352 | return 1; | |
353 | } | |
354 | ||
f2a3de01 | 355 | static void* client2(void *p) { |
4afd3348 LP |
356 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
357 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
358 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
e3017af9 | 359 | bool quit = false; |
b9bf7e2b LP |
360 | const char *mid; |
361 | int r; | |
e3017af9 LP |
362 | |
363 | r = sd_bus_open_user(&bus); | |
364 | if (r < 0) { | |
da927ba9 | 365 | log_error_errno(r, "Failed to connect to user bus: %m"); |
e3017af9 LP |
366 | goto finish; |
367 | } | |
368 | ||
a652755d LP |
369 | r = sd_bus_message_new_method_call( |
370 | bus, | |
151b9b96 | 371 | &m, |
a652755d LP |
372 | "org.freedesktop.systemd.test", |
373 | "/foo/bar/waldo/piep", | |
374 | "org.object.test", | |
151b9b96 | 375 | "Foobar"); |
a652755d | 376 | if (r < 0) { |
da927ba9 | 377 | log_error_errno(r, "Failed to allocate method call: %m"); |
a652755d LP |
378 | goto finish; |
379 | } | |
380 | ||
381 | r = sd_bus_send(bus, m, NULL); | |
382 | if (r < 0) { | |
2a03b9ed | 383 | log_error("Failed to issue method call: %s", bus_error_message(&error, r)); |
a652755d LP |
384 | goto finish; |
385 | } | |
386 | ||
0e851143 | 387 | m = sd_bus_message_unref(m); |
a652755d | 388 | |
392d5b37 LP |
389 | r = sd_bus_message_new_signal( |
390 | bus, | |
151b9b96 | 391 | &m, |
392d5b37 LP |
392 | "/foobar", |
393 | "foo.bar", | |
151b9b96 | 394 | "Notify"); |
392d5b37 | 395 | if (r < 0) { |
da927ba9 | 396 | log_error_errno(r, "Failed to allocate signal: %m"); |
392d5b37 LP |
397 | goto finish; |
398 | } | |
399 | ||
400 | r = sd_bus_send(bus, m, NULL); | |
401 | if (r < 0) { | |
2a03b9ed | 402 | log_error("Failed to issue signal: %s", bus_error_message(&error, r)); |
392d5b37 LP |
403 | goto finish; |
404 | } | |
405 | ||
0e851143 | 406 | m = sd_bus_message_unref(m); |
392d5b37 | 407 | |
85bc6b05 EM |
408 | r = sd_bus_message_new_signal_to( |
409 | bus, | |
410 | &m, | |
411 | "org.freedesktop.systemd.test", | |
412 | "/foobar", | |
413 | "foo.bar", | |
414 | "NotifyTo"); | |
415 | if (r < 0) { | |
416 | log_error_errno(r, "Failed to allocate signal to: %m"); | |
417 | goto finish; | |
418 | } | |
419 | ||
420 | r = sd_bus_send(bus, m, NULL); | |
421 | if (r < 0) { | |
422 | log_error("Failed to issue signal to: %s", bus_error_message(&error, r)); | |
423 | goto finish; | |
424 | } | |
425 | ||
426 | m = sd_bus_message_unref(m); | |
427 | ||
b9bf7e2b LP |
428 | r = sd_bus_message_new_method_call( |
429 | bus, | |
151b9b96 | 430 | &m, |
b9bf7e2b LP |
431 | "org.freedesktop.systemd.test", |
432 | "/", | |
433 | "org.freedesktop.DBus.Peer", | |
151b9b96 | 434 | "GetMachineId"); |
b9bf7e2b | 435 | if (r < 0) { |
da927ba9 | 436 | log_error_errno(r, "Failed to allocate method call: %m"); |
b9bf7e2b LP |
437 | goto finish; |
438 | } | |
439 | ||
c49b30a2 | 440 | r = sd_bus_call(bus, m, 0, &error, &reply); |
b9bf7e2b | 441 | if (r < 0) { |
2a03b9ed | 442 | log_error("Failed to issue method call: %s", bus_error_message(&error, r)); |
b9bf7e2b LP |
443 | goto finish; |
444 | } | |
445 | ||
446 | r = sd_bus_message_read(reply, "s", &mid); | |
447 | if (r < 0) { | |
da927ba9 | 448 | log_error_errno(r, "Failed to parse machine ID: %m"); |
b9bf7e2b LP |
449 | goto finish; |
450 | } | |
451 | ||
452 | log_info("Machine ID is %s.", mid); | |
453 | ||
0e851143 | 454 | m = sd_bus_message_unref(m); |
b9bf7e2b | 455 | |
e3017af9 LP |
456 | r = sd_bus_message_new_method_call( |
457 | bus, | |
151b9b96 | 458 | &m, |
e3017af9 LP |
459 | "org.freedesktop.systemd.test", |
460 | "/", | |
461 | "org.freedesktop.systemd.test", | |
151b9b96 | 462 | "Slow"); |
e3017af9 | 463 | if (r < 0) { |
da927ba9 | 464 | log_error_errno(r, "Failed to allocate method call: %m"); |
e3017af9 LP |
465 | goto finish; |
466 | } | |
467 | ||
0e851143 | 468 | reply = sd_bus_message_unref(reply); |
b9bf7e2b | 469 | |
c49b30a2 | 470 | r = sd_bus_call(bus, m, 200 * USEC_PER_MSEC, &error, &reply); |
e3017af9 | 471 | if (r < 0) |
2a03b9ed | 472 | log_info("Failed to issue method call: %s", bus_error_message(&error, r)); |
e3017af9 LP |
473 | else |
474 | log_info("Slow call succeed."); | |
475 | ||
0e851143 | 476 | m = sd_bus_message_unref(m); |
e3017af9 LP |
477 | |
478 | r = sd_bus_message_new_method_call( | |
479 | bus, | |
151b9b96 | 480 | &m, |
e3017af9 LP |
481 | "org.freedesktop.systemd.test", |
482 | "/", | |
483 | "org.freedesktop.systemd.test", | |
151b9b96 | 484 | "Slow"); |
e3017af9 | 485 | if (r < 0) { |
da927ba9 | 486 | log_error_errno(r, "Failed to allocate method call: %m"); |
e3017af9 LP |
487 | goto finish; |
488 | } | |
489 | ||
19befb2d | 490 | r = sd_bus_call_async(bus, NULL, m, quit_callback, &quit, 200 * USEC_PER_MSEC); |
e3017af9 | 491 | if (r < 0) { |
2a03b9ed | 492 | log_info("Failed to issue method call: %s", bus_error_message(&error, r)); |
e3017af9 LP |
493 | goto finish; |
494 | } | |
495 | ||
496 | while (!quit) { | |
497 | r = sd_bus_process(bus, NULL); | |
498 | if (r < 0) { | |
da927ba9 | 499 | log_error_errno(r, "Failed to process requests: %m"); |
e3017af9 LP |
500 | goto finish; |
501 | } | |
502 | if (r == 0) { | |
f5fbe71d | 503 | r = sd_bus_wait(bus, UINT64_MAX); |
e3017af9 | 504 | if (r < 0) { |
da927ba9 | 505 | log_error_errno(r, "Failed to wait: %m"); |
e3017af9 LP |
506 | goto finish; |
507 | } | |
508 | } | |
509 | } | |
510 | ||
511 | r = 0; | |
512 | ||
513 | finish: | |
514 | if (bus) { | |
b9c54c46 | 515 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *q = NULL; |
e3017af9 LP |
516 | |
517 | r = sd_bus_message_new_method_call( | |
518 | bus, | |
151b9b96 | 519 | &q, |
e3017af9 LP |
520 | "org.freedesktop.systemd.test", |
521 | "/", | |
522 | "org.freedesktop.systemd.test", | |
151b9b96 | 523 | "ExitClient2"); |
e3017af9 | 524 | if (r < 0) { |
da927ba9 | 525 | log_error_errno(r, "Failed to allocate method call: %m"); |
e3017af9 LP |
526 | goto finish; |
527 | } | |
528 | ||
0e851143 | 529 | (void) sd_bus_send(bus, q, NULL); |
e3017af9 LP |
530 | } |
531 | ||
e3017af9 | 532 | return INT_TO_PTR(r); |
89ffcd2a LP |
533 | } |
534 | ||
535 | int main(int argc, char *argv[]) { | |
e3017af9 | 536 | pthread_t c1, c2; |
89ffcd2a LP |
537 | sd_bus *bus; |
538 | void *p; | |
539 | int q, r; | |
540 | ||
6d7c4033 ZJS |
541 | test_setup_logging(LOG_INFO); |
542 | ||
89ffcd2a | 543 | r = server_init(&bus); |
317bb217 ZJS |
544 | if (r < 0) |
545 | return log_tests_skipped("Failed to connect to bus"); | |
89ffcd2a | 546 | |
e3017af9 LP |
547 | log_info("Initialized..."); |
548 | ||
549 | r = pthread_create(&c1, NULL, client1, bus); | |
550 | if (r != 0) | |
89ffcd2a | 551 | return EXIT_FAILURE; |
89ffcd2a | 552 | |
e3017af9 LP |
553 | r = pthread_create(&c2, NULL, client2, bus); |
554 | if (r != 0) | |
555 | return EXIT_FAILURE; | |
89ffcd2a | 556 | |
e3017af9 LP |
557 | r = server(bus); |
558 | ||
559 | q = pthread_join(c1, &p); | |
560 | if (q != 0) | |
561 | return EXIT_FAILURE; | |
2181a7f5 LP |
562 | if (PTR_TO_INT(p) < 0) |
563 | return EXIT_FAILURE; | |
564 | ||
e3017af9 | 565 | q = pthread_join(c2, &p); |
89ffcd2a LP |
566 | if (q != 0) |
567 | return EXIT_FAILURE; | |
2181a7f5 LP |
568 | if (PTR_TO_INT(p) < 0) |
569 | return EXIT_FAILURE; | |
570 | ||
89ffcd2a LP |
571 | if (r < 0) |
572 | return EXIT_FAILURE; | |
573 | ||
574 | return EXIT_SUCCESS; | |
575 | } |