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