]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
70cb8b7b | 2 | /*** |
70cb8b7b LP |
3 | ***/ |
4 | ||
dccca82b LP |
5 | #include <errno.h> |
6 | #include <sys/socket.h> | |
7 | ||
62cc1c55 | 8 | #include "sd-bus.h" |
70cb8b7b LP |
9 | |
10 | #include "macro.h" | |
11 | ||
12 | static bool track_cb_called_x = false; | |
13 | static bool track_cb_called_y = false; | |
14 | ||
15 | static int track_cb_x(sd_bus_track *t, void *userdata) { | |
16 | ||
17 | log_error("TRACK CB X"); | |
18 | ||
19 | assert_se(!track_cb_called_x); | |
20 | track_cb_called_x = true; | |
21 | ||
22 | /* This means b's name disappeared. Let's now disconnect, to make sure the track handling on disconnect works | |
23 | * as it should. */ | |
24 | ||
25 | assert_se(shutdown(sd_bus_get_fd(sd_bus_track_get_bus(t)), SHUT_RDWR) >= 0); | |
26 | return 1; | |
27 | } | |
28 | ||
29 | static int track_cb_y(sd_bus_track *t, void *userdata) { | |
30 | int r; | |
31 | ||
32 | log_error("TRACK CB Y"); | |
33 | ||
34 | assert_se(!track_cb_called_y); | |
35 | track_cb_called_y = true; | |
36 | ||
37 | /* We got disconnected, let's close everything */ | |
38 | ||
39 | r = sd_event_exit(sd_bus_get_event(sd_bus_track_get_bus(t)), EXIT_SUCCESS); | |
40 | assert_se(r >= 0); | |
41 | ||
42 | return 0; | |
43 | } | |
44 | ||
45 | int main(int argc, char *argv[]) { | |
46 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; | |
47 | _cleanup_(sd_bus_track_unrefp) sd_bus_track *x = NULL, *y = NULL; | |
48 | _cleanup_(sd_bus_unrefp) sd_bus *a = NULL, *b = NULL; | |
49 | const char *unique; | |
50 | int r; | |
51 | ||
52 | r = sd_event_default(&event); | |
53 | assert_se(r >= 0); | |
54 | ||
6349cda2 | 55 | r = sd_bus_open_user(&a); |
70cb8b7b LP |
56 | if (IN_SET(r, -ECONNREFUSED, -ENOENT)) { |
57 | log_info("Failed to connect to bus, skipping tests."); | |
58 | return EXIT_TEST_SKIP; | |
59 | } | |
60 | assert_se(r >= 0); | |
61 | ||
62 | r = sd_bus_attach_event(a, event, SD_EVENT_PRIORITY_NORMAL); | |
63 | assert_se(r >= 0); | |
64 | ||
6349cda2 | 65 | r = sd_bus_open_user(&b); |
70cb8b7b LP |
66 | assert_se(r >= 0); |
67 | ||
68 | r = sd_bus_attach_event(b, event, SD_EVENT_PRIORITY_NORMAL); | |
69 | assert_se(r >= 0); | |
70 | ||
71 | /* Watch b's name from a */ | |
72 | r = sd_bus_track_new(a, &x, track_cb_x, NULL); | |
73 | assert_se(r >= 0); | |
74 | ||
75 | r = sd_bus_get_unique_name(b, &unique); | |
76 | assert_se(r >= 0); | |
77 | ||
78 | r = sd_bus_track_add_name(x, unique); | |
79 | assert_se(r >= 0); | |
80 | ||
81 | /* Watch's a's own name from a */ | |
82 | r = sd_bus_track_new(a, &y, track_cb_y, NULL); | |
83 | assert_se(r >= 0); | |
84 | ||
85 | r = sd_bus_get_unique_name(a, &unique); | |
86 | assert_se(r >= 0); | |
87 | ||
88 | r = sd_bus_track_add_name(y, unique); | |
89 | assert_se(r >= 0); | |
90 | ||
91 | /* Now make b's name disappear */ | |
92 | sd_bus_close(b); | |
93 | ||
94 | r = sd_event_loop(event); | |
95 | assert_se(r >= 0); | |
96 | ||
97 | assert_se(track_cb_called_x); | |
98 | assert_se(track_cb_called_y); | |
99 | ||
100 | return 0; | |
101 | } |