1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <errno.h>
4 #include <sys/socket.h>
5 
6 #include "sd-bus.h"
7 
8 #include "macro.h"
9 #include "tests.h"
10 
11 static bool track_cb_called_x = false;
12 static bool track_cb_called_y = false;
13 static bool track_destroy_called_z = false;
14 
track_cb_x(sd_bus_track * t,void * userdata)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 
track_cb_y(sd_bus_track * t,void * userdata)29 static int track_cb_y(sd_bus_track *t, void *userdata) {
30 
31         log_error("TRACK CB Y");
32 
33         assert_se(!track_cb_called_y);
34         track_cb_called_y = true;
35 
36         /* We got disconnected, let's close everything */
37 
38         assert_se(sd_event_exit(sd_bus_get_event(sd_bus_track_get_bus(t)), EXIT_SUCCESS) >= 0);
39 
40         return 0;
41 }
42 
track_cb_z(sd_bus_track * t,void * userdata)43 static int track_cb_z(sd_bus_track *t, void *userdata) {
44         assert_not_reached();
45 }
46 
track_destroy_z(void * userdata)47 static void track_destroy_z(void *userdata) {
48         track_destroy_called_z = true;
49 }
50 
main(int argc,char * argv[])51 int main(int argc, char *argv[]) {
52         _cleanup_(sd_event_unrefp) sd_event *event = NULL;
53         _cleanup_(sd_bus_track_unrefp) sd_bus_track *x = NULL, *y = NULL, *z = NULL;
54         _cleanup_(sd_bus_unrefp) sd_bus *a = NULL, *b = NULL;
55         bool use_system_bus = false;
56         const char *unique;
57         int r;
58 
59         test_setup_logging(LOG_INFO);
60 
61         assert_se(sd_event_default(&event) >= 0);
62 
63         r = sd_bus_open_user(&a);
64         if (IN_SET(r, -ECONNREFUSED, -ENOENT, -ENOMEDIUM)) {
65                 r = sd_bus_open_system(&a);
66                 if (IN_SET(r, -ECONNREFUSED, -ENOENT))
67                         return log_tests_skipped("Failed to connect to bus");
68                 use_system_bus = true;
69         }
70         assert_se(r >= 0);
71 
72         assert_se(sd_bus_attach_event(a, event, SD_EVENT_PRIORITY_NORMAL) >= 0);
73 
74         if (use_system_bus)
75                 assert_se(sd_bus_open_system(&b) >= 0);
76         else
77                 assert_se(sd_bus_open_user(&b) >= 0);
78 
79         assert_se(sd_bus_attach_event(b, event, SD_EVENT_PRIORITY_NORMAL) >= 0);
80 
81         /* Watch b's name from a */
82         assert_se(sd_bus_track_new(a, &x, track_cb_x, NULL) >= 0);
83 
84         assert_se(sd_bus_get_unique_name(b, &unique) >= 0);
85 
86         assert_se(sd_bus_track_add_name(x, unique) >= 0);
87 
88         /* Watch's a's own name from a */
89         assert_se(sd_bus_track_new(a, &y, track_cb_y, NULL) >= 0);
90 
91         assert_se(sd_bus_get_unique_name(a, &unique) >= 0);
92 
93         assert_se(sd_bus_track_add_name(y, unique) >= 0);
94 
95         /* Basic tests. */
96         assert_se(sd_bus_track_new(a, &z, track_cb_z, NULL) >= 0);
97 
98         /* non-recursive case */
99         assert_se(sd_bus_track_set_recursive(z, false) >= 0);
100         assert_se(sd_bus_track_get_recursive(z) == 0);
101         assert_se(!sd_bus_track_contains(z, unique));
102         assert_se(sd_bus_track_count_name(z, unique) == 0);
103         assert_se(sd_bus_track_remove_name(z, unique) == 0);
104         assert_se(sd_bus_track_add_name(z, unique) >= 0);
105         assert_se(sd_bus_track_add_name(z, unique) >= 0);
106         assert_se(sd_bus_track_add_name(z, unique) >= 0);
107         assert_se(sd_bus_track_set_recursive(z, true) == -EBUSY);
108         assert_se(sd_bus_track_contains(z, unique));
109         assert_se(sd_bus_track_count_name(z, unique) == 1);
110         assert_se(sd_bus_track_remove_name(z, unique) == 1);
111         assert_se(!sd_bus_track_contains(z, unique));
112         assert_se(sd_bus_track_count_name(z, unique) == 0);
113         assert_se(sd_bus_track_remove_name(z, unique) == 0);
114 
115         /* recursive case */
116         assert_se(sd_bus_track_set_recursive(z, true) >= 0);
117         assert_se(sd_bus_track_get_recursive(z) == 1);
118         assert_se(!sd_bus_track_contains(z, unique));
119         assert_se(sd_bus_track_count_name(z, unique) == 0);
120         assert_se(sd_bus_track_remove_name(z, unique) == 0);
121         assert_se(sd_bus_track_add_name(z, unique) >= 0);
122         assert_se(sd_bus_track_add_name(z, unique) >= 0);
123         assert_se(sd_bus_track_add_name(z, unique) >= 0);
124         assert_se(sd_bus_track_set_recursive(z, false) == -EBUSY);
125         assert_se(sd_bus_track_contains(z, unique));
126         assert_se(sd_bus_track_count_name(z, unique) == 3);
127         assert_se(sd_bus_track_remove_name(z, unique) == 1);
128         assert_se(sd_bus_track_contains(z, unique));
129         assert_se(sd_bus_track_count_name(z, unique) == 2);
130         assert_se(sd_bus_track_remove_name(z, unique) == 1);
131         assert_se(sd_bus_track_contains(z, unique));
132         assert_se(sd_bus_track_count_name(z, unique) == 1);
133         assert_se(sd_bus_track_remove_name(z, unique) == 1);
134         assert_se(!sd_bus_track_contains(z, unique));
135         assert_se(sd_bus_track_count_name(z, unique) == 0);
136         assert_se(sd_bus_track_remove_name(z, unique) == 0);
137 
138         assert_se(sd_bus_track_set_destroy_callback(z, track_destroy_z) >= 0);
139         z = sd_bus_track_unref(z);
140         assert_se(track_destroy_called_z);
141 
142         /* Now make b's name disappear */
143         sd_bus_close(b);
144 
145         assert_se(sd_event_loop(event) >= 0);
146 
147         assert_se(track_cb_called_x);
148         assert_se(track_cb_called_y);
149 
150         return 0;
151 }
152