summaryrefslogtreecommitdiffstats
path: root/src/libsystemd/sd-device/test-sd-device-monitor.c
blob: ae973cdba395aa03e91caac9688868c64d628ad6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <stdbool.h>
#include <unistd.h>

#include "sd-device.h"
#include "sd-event.h"

#include "device-monitor-private.h"
#include "device-private.h"
#include "device-util.h"
#include "macro.h"
#include "string-util.h"
#include "tests.h"
#include "util.h"
#include "virt.h"

static int monitor_handler(sd_device_monitor *m, sd_device *d, void *userdata) {
        const char *s, *syspath = userdata;

        assert_se(sd_device_get_syspath(d, &s) >= 0);
        assert_se(streq(s, syspath));

        return sd_event_exit(sd_device_monitor_get_event(m), 100);
}

static int test_receive_device_fail(void) {
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
        _cleanup_(sd_device_unrefp) sd_device *loopback = NULL;
        const char *syspath;
        int r;

        log_info("/* %s */", __func__);

        /* Try to send device with invalid action and without seqnum. */
        assert_se(sd_device_new_from_syspath(&loopback, "/sys/class/net/lo") >= 0);
        assert_se(device_add_property(loopback, "ACTION", "hoge") >= 0);

        assert_se(sd_device_get_syspath(loopback, &syspath) >= 0);

        assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0);
        assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0);
        assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_server), "sender") >= 0);

        assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0);
        assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0);
        assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0);
        assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_client), "receiver") >= 0);

        /* Do not use assert_se() here. */
        r = device_monitor_send_device(monitor_server, monitor_client, loopback);
        if (r < 0)
                return log_error_errno(r, "Failed to send loopback device: %m");

        assert_se(sd_event_run(sd_device_monitor_get_event(monitor_client), 0) >= 0);

        return 0;
}

static void test_send_receive_one(sd_device *device, bool subsystem_filter, bool tag_filter, bool use_bpf) {
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
        const char *syspath, *subsystem, *tag, *devtype = NULL;

        log_device_info(device, "/* %s(subsystem_filter=%s, tag_filter=%s, use_bpf=%s) */", __func__,
                        true_false(subsystem_filter), true_false(tag_filter), true_false(use_bpf));

        assert_se(sd_device_get_syspath(device, &syspath) >= 0);

        assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0);
        assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0);
        assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_server), "sender") >= 0);

        assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0);
        assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0);
        assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0);
        assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_client), "receiver") >= 0);

        if (subsystem_filter) {
                assert_se(sd_device_get_subsystem(device, &subsystem) >= 0);
                (void) sd_device_get_devtype(device, &devtype);
                assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, subsystem, devtype) >= 0);
        }

        if (tag_filter)
                FOREACH_DEVICE_TAG(device, tag)
                        assert_se(sd_device_monitor_filter_add_match_tag(monitor_client, tag) >= 0);

        if ((subsystem_filter || tag_filter) && use_bpf)
                assert_se(sd_device_monitor_filter_update(monitor_client) >= 0);

        assert_se(device_monitor_send_device(monitor_server, monitor_client, device) >= 0);
        assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 100);
}

static void test_subsystem_filter(sd_device *device) {
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
        const char *syspath, *subsystem;
        sd_device *d;

        log_device_info(device, "/* %s */", __func__);

        assert_se(sd_device_get_syspath(device, &syspath) >= 0);
        assert_se(sd_device_get_subsystem(device, &subsystem) >= 0);

        assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0);
        assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0);
        assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_server), "sender") >= 0);

        assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0);
        assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0);
        assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, subsystem, NULL) >= 0);
        assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0);
        assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_client), "receiver") >= 0);

        assert_se(sd_device_enumerator_new(&e) >= 0);
        assert_se(sd_device_enumerator_add_match_subsystem(e, subsystem, false) >= 0);
        FOREACH_DEVICE(e, d) {
                const char *p, *s;

                assert_se(sd_device_get_syspath(d, &p) >= 0);
                assert_se(sd_device_get_subsystem(d, &s) >= 0);

                log_device_debug(d, "Sending device subsystem:%s syspath:%s", s, p);
                assert_se(device_monitor_send_device(monitor_server, monitor_client, d) >= 0);
        }

        log_device_info(device, "Sending device subsystem:%s syspath:%s", subsystem, syspath);
        assert_se(device_monitor_send_device(monitor_server, monitor_client, device) >= 0);
        assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 100);
}

static void test_tag_filter(sd_device *device) {
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
        const char *syspath;
        sd_device *d;

        log_device_info(device, "/* %s */", __func__);

        assert_se(sd_device_get_syspath(device, &syspath) >= 0);

        assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0);
        assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0);
        assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_server), "sender") >= 0);

        assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0);
        assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0);
        assert_se(sd_device_monitor_filter_add_match_tag(monitor_client, "TEST_SD_DEVICE_MONITOR") >= 0);
        assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0);
        assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_client), "receiver") >= 0);

        assert_se(sd_device_enumerator_new(&e) >= 0);
        FOREACH_DEVICE(e, d) {
                const char *p;

                assert_se(sd_device_get_syspath(d, &p) >= 0);

                log_device_debug(d, "Sending device syspath:%s", p);
                assert_se(device_monitor_send_device(monitor_server, monitor_client, d) >= 0);
        }

        log_device_info(device, "Sending device syspath:%s", syspath);
        assert_se(device_monitor_send_device(monitor_server, monitor_client, device) >= 0);
        assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 100);

}

static void test_sysattr_filter(sd_device *device, const char *sysattr) {
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
        const char *syspath, *subsystem, *sysattr_value;
        sd_device *d;

        log_device_info(device, "/* %s(%s) */", __func__, sysattr);

        assert_se(sd_device_get_syspath(device, &syspath) >= 0);
        assert_se(sd_device_get_subsystem(device, &subsystem) >= 0);
        assert_se(sd_device_get_sysattr_value(device, sysattr, &sysattr_value) >= 0);

        assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0);
        assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0);
        assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_server), "sender") >= 0);

        assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0);
        assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0);
        /* The sysattr filter is not implemented in BPF yet, so the below device_monito_send_device()
         * may cause EAGAIN. So, let's also filter devices with subsystem. */
        assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, subsystem, NULL) >= 0);
        assert_se(sd_device_monitor_filter_add_match_sysattr(monitor_client, sysattr, sysattr_value, true) >= 0);
        assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0);
        assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_client), "receiver") >= 0);

        assert_se(sd_device_enumerator_new(&e) >= 0);
        assert_se(sd_device_enumerator_add_match_sysattr(e, sysattr, sysattr_value, false) >= 0);
        FOREACH_DEVICE(e, d) {
                const char *p;

                assert_se(sd_device_get_syspath(d, &p) >= 0);

                log_device_debug(d, "Sending device syspath:%s", p);
                assert_se(device_monitor_send_device(monitor_server, monitor_client, d) >= 0);
        }

        log_device_info(device, "Sending device syspath:%s", syspath);
        assert_se(device_monitor_send_device(monitor_server, monitor_client, device) >= 0);
        assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 100);

}

static void test_parent_filter(sd_device *device) {
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
        const char *syspath, *subsystem;
        sd_device *parent, *d;
        int r;

        log_device_info(device, "/* %s */", __func__);

        assert_se(sd_device_get_syspath(device, &syspath) >= 0);
        assert_se(sd_device_get_subsystem(device, &subsystem) >= 0);
        r = sd_device_get_parent(device, &parent);
        if (r < 0)
                return (void) log_device_info(device, "Device does not have parent, skipping.");

        assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0);
        assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0);
        assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_server), "sender") >= 0);

        assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0);
        assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0);
        /* The parent filter is not implemented in BPF yet, so the below device_monito_send_device()
         * may cause EAGAIN. So, let's also filter devices with subsystem. */
        assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, subsystem, NULL) >= 0);
        assert_se(sd_device_monitor_filter_add_match_parent(monitor_client, parent, true) >= 0);
        assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0);
        assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_client), "receiver") >= 0);

        assert_se(sd_device_enumerator_new(&e) >= 0);
        FOREACH_DEVICE(e, d) {
                const char *p;

                assert_se(sd_device_get_syspath(d, &p) >= 0);

                log_device_debug(d, "Sending device syspath:%s", p);
                assert_se(device_monitor_send_device(monitor_server, monitor_client, d) >= 0);
        }

        log_device_info(device, "Sending device syspath:%s", syspath);
        assert_se(device_monitor_send_device(monitor_server, monitor_client, device) >= 0);
        assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 100);

}

static void test_sd_device_monitor_filter_remove(sd_device *device) {
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
        const char *syspath;

        log_device_info(device, "/* %s */", __func__);

        assert_se(sd_device_get_syspath(device, &syspath) >= 0);

        assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0);
        assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0);
        assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_server), "sender") >= 0);

        assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0);
        assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0);
        assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0);
        assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_client), "receiver") >= 0);

        assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, "hoge", NULL) >= 0);
        assert_se(sd_device_monitor_filter_update(monitor_client) >= 0);

        assert_se(device_monitor_send_device(monitor_server, monitor_client, device) >= 0);
        assert_se(sd_event_run(sd_device_monitor_get_event(monitor_client), 0) >= 0);

        assert_se(sd_device_monitor_filter_remove(monitor_client) >= 0);

        assert_se(device_monitor_send_device(monitor_server, monitor_client, device) >= 0);
        assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 100);
}

static void test_device_copy_properties(sd_device *device) {
        _cleanup_(sd_device_unrefp) sd_device *copy = NULL;

        assert_se(device_shallow_clone(device, &copy) >= 0);
        assert_se(device_copy_properties(copy, device) >= 0);

        test_send_receive_one(copy, false, false, false);
}

int main(int argc, char *argv[]) {
        _cleanup_(sd_device_unrefp) sd_device *loopback = NULL, *sda = NULL;
        int r;

        test_setup_logging(LOG_INFO);

        if (getuid() != 0)
                return log_tests_skipped("not root");

        r = test_receive_device_fail();
        if (r < 0) {
                assert_se(r == -EPERM && detect_container() > 0);
                return log_tests_skipped("Running in container? Skipping remaining tests");
        }

        assert_se(sd_device_new_from_syspath(&loopback, "/sys/class/net/lo") >= 0);
        assert_se(device_add_property(loopback, "ACTION", "add") >= 0);
        assert_se(device_add_property(loopback, "SEQNUM", "10") >= 0);
        assert_se(device_add_tag(loopback, "TEST_SD_DEVICE_MONITOR", true) >= 0);

        test_send_receive_one(loopback, false, false, false);
        test_send_receive_one(loopback,  true, false, false);
        test_send_receive_one(loopback, false,  true, false);
        test_send_receive_one(loopback,  true,  true, false);
        test_send_receive_one(loopback,  true, false,  true);
        test_send_receive_one(loopback, false,  true,  true);
        test_send_receive_one(loopback,  true,  true,  true);

        test_subsystem_filter(loopback);
        test_tag_filter(loopback);
        test_sysattr_filter(loopback, "ifindex");
        test_sd_device_monitor_filter_remove(loopback);
        test_device_copy_properties(loopback);

        r = sd_device_new_from_subsystem_sysname(&sda, "block", "sda");
        if (r < 0) {
                log_info_errno(r, "Failed to create sd_device for sda, skipping remaining tests: %m");
                return 0;
        }

        assert_se(device_add_property(sda, "ACTION", "change") >= 0);
        assert_se(device_add_property(sda, "SEQNUM", "11") >= 0);

        test_send_receive_one(sda, false, false, false);
        test_send_receive_one(sda,  true, false, false);
        test_send_receive_one(sda, false,  true, false);
        test_send_receive_one(sda,  true,  true, false);
        test_send_receive_one(sda,  true, false,  true);
        test_send_receive_one(sda, false,  true,  true);
        test_send_receive_one(sda,  true,  true,  true);

        test_parent_filter(sda);

        return 0;
}