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