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
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <ctype.h>
#include <fcntl.h>
#include "device-enumerator-private.h"
#include "device-internal.h"
#include "device-private.h"
#include "device-util.h"
#include "errno-util.h"
#include "fd-util.h"
#include "hashmap.h"
#include "nulstr-util.h"
#include "path-util.h"
#include "string-util.h"
#include "tests.h"
#include "time-util.h"
static void test_sd_device_one(sd_device *d) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
const char *syspath, *sysname, *subsystem = NULL, *devname, *val;
bool is_block = false;
dev_t devnum;
usec_t usec;
int ifindex, r;
assert_se(sd_device_get_syspath(d, &syspath) >= 0);
assert_se(path_startswith(syspath, "/sys"));
assert_se(sd_device_get_sysname(d, &sysname) >= 0);
log_info("%s(%s)", __func__, syspath);
assert_se(sd_device_new_from_syspath(&dev, syspath) >= 0);
assert_se(sd_device_get_syspath(dev, &val) >= 0);
assert_se(streq(syspath, val));
dev = sd_device_unref(dev);
assert_se(sd_device_new_from_path(&dev, syspath) >= 0);
assert_se(sd_device_get_syspath(dev, &val) >= 0);
assert_se(streq(syspath, val));
dev = sd_device_unref(dev);
r = sd_device_get_ifindex(d, &ifindex);
if (r >= 0) {
assert_se(ifindex > 0);
r = sd_device_new_from_ifindex(&dev, ifindex);
if (r == -ENODEV)
log_device_warning_errno(d, r,
"Failed to create sd-device object from ifindex %i. "
"Maybe running on a non-host network namespace.", ifindex);
else {
assert_se(r >= 0);
assert_se(sd_device_get_syspath(dev, &val) >= 0);
assert_se(streq(syspath, val));
dev = sd_device_unref(dev);
}
/* This does not require the interface really exists on the network namespace.
* Hence, this should always succeed. */
assert_se(sd_device_new_from_ifname(&dev, sysname) >= 0);
assert_se(sd_device_get_syspath(dev, &val) >= 0);
assert_se(streq(syspath, val));
dev = sd_device_unref(dev);
} else
assert_se(r == -ENOENT);
r = sd_device_get_subsystem(d, &subsystem);
if (r >= 0) {
const char *name, *id;
if (streq(subsystem, "drivers"))
name = strjoina(d->driver_subsystem, ":", sysname);
else
name = sysname;
assert_se(sd_device_new_from_subsystem_sysname(&dev, subsystem, name) >= 0);
assert_se(sd_device_get_syspath(dev, &val) >= 0);
assert_se(streq(syspath, val));
dev = sd_device_unref(dev);
/* The device ID depends on subsystem. */
assert_se(device_get_device_id(d, &id) >= 0);
r = sd_device_new_from_device_id(&dev, id);
if (r == -ENODEV && ifindex > 0)
log_device_warning_errno(d, r,
"Failed to create sd-device object from device ID \"%s\". "
"Maybe running on a non-host network namespace.", id);
else {
assert_se(r >= 0);
assert_se(sd_device_get_syspath(dev, &val) >= 0);
assert_se(streq(syspath, val));
dev = sd_device_unref(dev);
}
/* These require udev database, and reading database requires device ID. */
r = sd_device_get_is_initialized(d);
if (r > 0) {
r = sd_device_get_usec_since_initialized(d, &usec);
assert_se((r >= 0 && usec > 0) || r == -ENODATA);
} else
assert(r == 0);
r = sd_device_get_property_value(d, "ID_NET_DRIVER", &val);
assert_se(r >= 0 || r == -ENOENT);
} else
assert_se(r == -ENOENT);
is_block = streq_ptr(subsystem, "block");
r = sd_device_get_devname(d, &devname);
if (r >= 0) {
r = sd_device_new_from_devname(&dev, devname);
if (r >= 0) {
assert_se(sd_device_get_syspath(dev, &val) >= 0);
assert_se(streq(syspath, val));
dev = sd_device_unref(dev);
} else
assert_se(r == -ENODEV || ERRNO_IS_PRIVILEGE(r));
r = sd_device_new_from_path(&dev, devname);
if (r >= 0) {
assert_se(sd_device_get_syspath(dev, &val) >= 0);
assert_se(streq(syspath, val));
dev = sd_device_unref(dev);
_cleanup_close_ int fd = -1;
fd = sd_device_open(d, O_CLOEXEC| O_NONBLOCK | (is_block ? O_RDONLY : O_NOCTTY | O_PATH));
assert_se(fd >= 0 || ERRNO_IS_PRIVILEGE(fd));
} else
assert_se(r == -ENODEV || ERRNO_IS_PRIVILEGE(r));
} else
assert_se(r == -ENOENT);
r = sd_device_get_devnum(d, &devnum);
if (r >= 0) {
_cleanup_free_ char *p = NULL;
assert_se(major(devnum) > 0);
assert_se(sd_device_new_from_devnum(&dev, is_block ? 'b' : 'c', devnum) >= 0);
assert_se(sd_device_get_syspath(dev, &val) >= 0);
assert_se(streq(syspath, val));
dev = sd_device_unref(dev);
assert_se(asprintf(&p, "/dev/%s/%u:%u", is_block ? "block" : "char", major(devnum), minor(devnum)) >= 0);
assert_se(sd_device_new_from_devname(&dev, p) >= 0);
assert_se(sd_device_get_syspath(dev, &val) >= 0);
assert_se(streq(syspath, val));
dev = sd_device_unref(dev);
assert_se(sd_device_new_from_path(&dev, p) >= 0);
assert_se(sd_device_get_syspath(dev, &val) >= 0);
assert_se(streq(syspath, val));
dev = sd_device_unref(dev);
} else
assert_se(r == -ENOENT);
assert_se(sd_device_get_devpath(d, &val) >= 0);
r = sd_device_get_devtype(d, &val);
assert_se(r >= 0 || r == -ENOENT);
r = sd_device_get_driver(d, &val);
assert_se(r >= 0 || r == -ENOENT);
r = sd_device_get_sysnum(d, &val);
if (r >= 0) {
assert_se(val > sysname);
assert_se(val < sysname + strlen(sysname));
assert_se(in_charset(val, DIGITS));
assert_se(!isdigit(val[-1]));
} else
assert_se(r == -ENOENT);
r = sd_device_get_sysattr_value(d, "name_assign_type", &val);
assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r) || IN_SET(r, -ENOENT, -EINVAL));
}
TEST(sd_device_enumerator_devices) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
sd_device *d;
assert_se(sd_device_enumerator_new(&e) >= 0);
assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
/* On some CI environments, it seems some loop block devices and corresponding bdi devices sometimes
* disappear during running this test. Let's exclude them here for stability. */
assert_se(sd_device_enumerator_add_match_subsystem(e, "bdi", false) >= 0);
assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
FOREACH_DEVICE(e, d)
test_sd_device_one(d);
}
TEST(sd_device_enumerator_subsystems) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
sd_device *d;
assert_se(sd_device_enumerator_new(&e) >= 0);
assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
FOREACH_SUBSYSTEM(e, d)
test_sd_device_one(d);
}
static void test_sd_device_enumerator_filter_subsystem_one(
const char *subsystem,
Hashmap *h,
unsigned *ret_n_new_dev,
unsigned *ret_n_removed_dev) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
unsigned n_new_dev = 0, n_removed_dev = 0;
sd_device *d;
assert_se(sd_device_enumerator_new(&e) >= 0);
assert_se(sd_device_enumerator_add_match_subsystem(e, subsystem, true) >= 0);
if (streq(subsystem, "block"))
assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
FOREACH_DEVICE(e, d) {
const char *syspath;
sd_device *t;
assert_se(sd_device_get_syspath(d, &syspath) >= 0);
t = hashmap_remove(h, syspath);
if (!t) {
log_warning("New device found: subsystem:%s syspath:%s", subsystem, syspath);
n_new_dev++;
}
assert_se(!sd_device_unref(t));
}
HASHMAP_FOREACH(d, h) {
const char *syspath;
assert_se(sd_device_get_syspath(d, &syspath) >= 0);
log_warning("Device removed: subsystem:%s syspath:%s", subsystem, syspath);
n_removed_dev++;
assert_se(!sd_device_unref(d));
}
hashmap_free(h);
*ret_n_new_dev = n_new_dev;
*ret_n_removed_dev = n_removed_dev;
}
TEST(sd_device_enumerator_filter_subsystem) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
_cleanup_(hashmap_freep) Hashmap *subsystems;
unsigned n_new_dev = 0, n_removed_dev = 0;
sd_device *d;
Hashmap *h;
char *s;
assert_se(subsystems = hashmap_new(&string_hash_ops));
assert_se(sd_device_enumerator_new(&e) >= 0);
/* See comments in TEST(sd_device_enumerator_devices). */
assert_se(sd_device_enumerator_add_match_subsystem(e, "bdi", false) >= 0);
assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
FOREACH_DEVICE(e, d) {
const char *syspath, *subsystem;
int r;
assert_se(sd_device_get_syspath(d, &syspath) >= 0);
r = sd_device_get_subsystem(d, &subsystem);
assert_se(r >= 0 || r == -ENOENT);
if (r < 0)
continue;
h = hashmap_get(subsystems, subsystem);
if (!h) {
char *str;
assert_se(str = strdup(subsystem));
assert_se(h = hashmap_new(&string_hash_ops));
assert_se(hashmap_put(subsystems, str, h) >= 0);
}
assert_se(hashmap_put(h, syspath, d) >= 0);
assert_se(sd_device_ref(d));
log_debug("Added subsystem:%s syspath:%s", subsystem, syspath);
}
while ((h = hashmap_steal_first_key_and_value(subsystems, (void**) &s))) {
unsigned n, m;
test_sd_device_enumerator_filter_subsystem_one(s, TAKE_PTR(h), &n, &m);
free(s);
n_new_dev += n;
n_removed_dev += m;
}
if (n_new_dev > 0)
log_warning("%u new devices are found in re-scan", n_new_dev);
if (n_removed_dev > 0)
log_warning("%u devices removed in re-scan", n_removed_dev);
/* Assume that not so many devices are plugged or unplugged. */
assert_se(n_new_dev + n_removed_dev <= 10);
}
TEST(sd_device_new_from_nulstr) {
const char *devlinks =
"/dev/disk/by-partuuid/1290d63a-42cc-4c71-b87c-xxxxxxxxxxxx\0"
"/dev/disk/by-path/pci-0000:00:0f.0-scsi-0:0:0:0-part3\0"
"/dev/disk/by-label/Arch\\x20Linux\0"
"/dev/disk/by-uuid/a07b87e5-4af5-4a59-bde9-yyyyyyyyyyyy\0"
"/dev/disk/by-partlabel/Arch\\x20Linux\0"
"\0";
_cleanup_(sd_device_unrefp) sd_device *device = NULL, *from_nulstr = NULL;
_cleanup_free_ uint8_t *nulstr_copy = NULL;
const char *devlink;
const uint8_t *nulstr;
size_t len;
assert_se(sd_device_new_from_syspath(&device, "/sys/class/net/lo") >= 0);
/* Yeah, of course, setting devlink to the loopback interface is nonsense. But this is just a
* test for generating and parsing nulstr. For issue #17772. */
NULSTR_FOREACH(devlink, devlinks) {
log_device_info(device, "setting devlink: %s", devlink);
assert_se(device_add_devlink(device, devlink) >= 0);
assert_se(set_contains(device->devlinks, devlink));
}
/* These properties are necessary for device_new_from_nulstr(). See device_verify(). */
assert_se(device_add_property_internal(device, "SEQNUM", "1") >= 0);
assert_se(device_add_property_internal(device, "ACTION", "change") >= 0);
assert_se(device_get_properties_nulstr(device, &nulstr, &len) >= 0);
assert_se(nulstr_copy = newdup(uint8_t, nulstr, len));
assert_se(device_new_from_nulstr(&from_nulstr, nulstr_copy, len) >= 0);
NULSTR_FOREACH(devlink, devlinks) {
log_device_info(from_nulstr, "checking devlink: %s", devlink);
assert_se(set_contains(from_nulstr->devlinks, devlink));
}
}
DEFINE_TEST_MAIN(LOG_INFO);
|