]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-bus/test-bus-track.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / libsystemd / sd-bus / test-bus-track.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
70cb8b7b
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2016 Lennart Poettering
6
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.
11
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.
16
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/>.
19***/
20
62cc1c55 21#include "sd-bus.h"
70cb8b7b
LP
22
23#include "macro.h"
24
25static bool track_cb_called_x = false;
26static bool track_cb_called_y = false;
27
28static int track_cb_x(sd_bus_track *t, void *userdata) {
29
30 log_error("TRACK CB X");
31
32 assert_se(!track_cb_called_x);
33 track_cb_called_x = true;
34
35 /* This means b's name disappeared. Let's now disconnect, to make sure the track handling on disconnect works
36 * as it should. */
37
38 assert_se(shutdown(sd_bus_get_fd(sd_bus_track_get_bus(t)), SHUT_RDWR) >= 0);
39 return 1;
40}
41
42static int track_cb_y(sd_bus_track *t, void *userdata) {
43 int r;
44
45 log_error("TRACK CB Y");
46
47 assert_se(!track_cb_called_y);
48 track_cb_called_y = true;
49
50 /* We got disconnected, let's close everything */
51
52 r = sd_event_exit(sd_bus_get_event(sd_bus_track_get_bus(t)), EXIT_SUCCESS);
53 assert_se(r >= 0);
54
55 return 0;
56}
57
58int main(int argc, char *argv[]) {
59 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
60 _cleanup_(sd_bus_track_unrefp) sd_bus_track *x = NULL, *y = NULL;
61 _cleanup_(sd_bus_unrefp) sd_bus *a = NULL, *b = NULL;
62 const char *unique;
63 int r;
64
65 r = sd_event_default(&event);
66 assert_se(r >= 0);
67
6349cda2 68 r = sd_bus_open_user(&a);
70cb8b7b
LP
69 if (IN_SET(r, -ECONNREFUSED, -ENOENT)) {
70 log_info("Failed to connect to bus, skipping tests.");
71 return EXIT_TEST_SKIP;
72 }
73 assert_se(r >= 0);
74
75 r = sd_bus_attach_event(a, event, SD_EVENT_PRIORITY_NORMAL);
76 assert_se(r >= 0);
77
6349cda2 78 r = sd_bus_open_user(&b);
70cb8b7b
LP
79 assert_se(r >= 0);
80
81 r = sd_bus_attach_event(b, event, SD_EVENT_PRIORITY_NORMAL);
82 assert_se(r >= 0);
83
84 /* Watch b's name from a */
85 r = sd_bus_track_new(a, &x, track_cb_x, NULL);
86 assert_se(r >= 0);
87
88 r = sd_bus_get_unique_name(b, &unique);
89 assert_se(r >= 0);
90
91 r = sd_bus_track_add_name(x, unique);
92 assert_se(r >= 0);
93
94 /* Watch's a's own name from a */
95 r = sd_bus_track_new(a, &y, track_cb_y, NULL);
96 assert_se(r >= 0);
97
98 r = sd_bus_get_unique_name(a, &unique);
99 assert_se(r >= 0);
100
101 r = sd_bus_track_add_name(y, unique);
102 assert_se(r >= 0);
103
104 /* Now make b's name disappear */
105 sd_bus_close(b);
106
107 r = sd_event_loop(event);
108 assert_se(r >= 0);
109
110 assert_se(track_cb_called_x);
111 assert_se(track_cb_called_y);
112
113 return 0;
114}