]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/test-bus-track.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2016 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/socket.h>
28 static bool track_cb_called_x
= false;
29 static bool track_cb_called_y
= false;
31 static int track_cb_x(sd_bus_track
*t
, void *userdata
) {
33 log_error("TRACK CB X");
35 assert_se(!track_cb_called_x
);
36 track_cb_called_x
= true;
38 /* This means b's name disappeared. Let's now disconnect, to make sure the track handling on disconnect works
41 assert_se(shutdown(sd_bus_get_fd(sd_bus_track_get_bus(t
)), SHUT_RDWR
) >= 0);
45 static int track_cb_y(sd_bus_track
*t
, void *userdata
) {
48 log_error("TRACK CB Y");
50 assert_se(!track_cb_called_y
);
51 track_cb_called_y
= true;
53 /* We got disconnected, let's close everything */
55 r
= sd_event_exit(sd_bus_get_event(sd_bus_track_get_bus(t
)), EXIT_SUCCESS
);
61 int main(int argc
, char *argv
[]) {
62 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
63 _cleanup_(sd_bus_track_unrefp
) sd_bus_track
*x
= NULL
, *y
= NULL
;
64 _cleanup_(sd_bus_unrefp
) sd_bus
*a
= NULL
, *b
= NULL
;
68 r
= sd_event_default(&event
);
71 r
= sd_bus_open_user(&a
);
72 if (IN_SET(r
, -ECONNREFUSED
, -ENOENT
)) {
73 log_info("Failed to connect to bus, skipping tests.");
74 return EXIT_TEST_SKIP
;
78 r
= sd_bus_attach_event(a
, event
, SD_EVENT_PRIORITY_NORMAL
);
81 r
= sd_bus_open_user(&b
);
84 r
= sd_bus_attach_event(b
, event
, SD_EVENT_PRIORITY_NORMAL
);
87 /* Watch b's name from a */
88 r
= sd_bus_track_new(a
, &x
, track_cb_x
, NULL
);
91 r
= sd_bus_get_unique_name(b
, &unique
);
94 r
= sd_bus_track_add_name(x
, unique
);
97 /* Watch's a's own name from a */
98 r
= sd_bus_track_new(a
, &y
, track_cb_y
, NULL
);
101 r
= sd_bus_get_unique_name(a
, &unique
);
104 r
= sd_bus_track_add_name(y
, unique
);
107 /* Now make b's name disappear */
110 r
= sd_event_loop(event
);
113 assert_se(track_cb_called_x
);
114 assert_se(track_cb_called_y
);