]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/test-bus-track.c
log: minimize includes in log.h
[thirdparty/systemd.git] / src / libsystemd / sd-bus / test-bus-track.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
21 #include <errno.h>
22 #include <sys/socket.h>
23
24 #include "sd-bus.h"
25
26 #include "macro.h"
27
28 static bool track_cb_called_x = false;
29 static bool track_cb_called_y = false;
30
31 static int track_cb_x(sd_bus_track *t, void *userdata) {
32
33 log_error("TRACK CB X");
34
35 assert_se(!track_cb_called_x);
36 track_cb_called_x = true;
37
38 /* This means b's name disappeared. Let's now disconnect, to make sure the track handling on disconnect works
39 * as it should. */
40
41 assert_se(shutdown(sd_bus_get_fd(sd_bus_track_get_bus(t)), SHUT_RDWR) >= 0);
42 return 1;
43 }
44
45 static int track_cb_y(sd_bus_track *t, void *userdata) {
46 int r;
47
48 log_error("TRACK CB Y");
49
50 assert_se(!track_cb_called_y);
51 track_cb_called_y = true;
52
53 /* We got disconnected, let's close everything */
54
55 r = sd_event_exit(sd_bus_get_event(sd_bus_track_get_bus(t)), EXIT_SUCCESS);
56 assert_se(r >= 0);
57
58 return 0;
59 }
60
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;
65 const char *unique;
66 int r;
67
68 r = sd_event_default(&event);
69 assert_se(r >= 0);
70
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;
75 }
76 assert_se(r >= 0);
77
78 r = sd_bus_attach_event(a, event, SD_EVENT_PRIORITY_NORMAL);
79 assert_se(r >= 0);
80
81 r = sd_bus_open_user(&b);
82 assert_se(r >= 0);
83
84 r = sd_bus_attach_event(b, event, SD_EVENT_PRIORITY_NORMAL);
85 assert_se(r >= 0);
86
87 /* Watch b's name from a */
88 r = sd_bus_track_new(a, &x, track_cb_x, NULL);
89 assert_se(r >= 0);
90
91 r = sd_bus_get_unique_name(b, &unique);
92 assert_se(r >= 0);
93
94 r = sd_bus_track_add_name(x, unique);
95 assert_se(r >= 0);
96
97 /* Watch's a's own name from a */
98 r = sd_bus_track_new(a, &y, track_cb_y, NULL);
99 assert_se(r >= 0);
100
101 r = sd_bus_get_unique_name(a, &unique);
102 assert_se(r >= 0);
103
104 r = sd_bus_track_add_name(y, unique);
105 assert_se(r >= 0);
106
107 /* Now make b's name disappear */
108 sd_bus_close(b);
109
110 r = sd_event_loop(event);
111 assert_se(r >= 0);
112
113 assert_se(track_cb_called_x);
114 assert_se(track_cb_called_y);
115
116 return 0;
117 }