1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <ctype.h>
4 #include <fcntl.h>
5 
6 #include "device-enumerator-private.h"
7 #include "device-internal.h"
8 #include "device-private.h"
9 #include "device-util.h"
10 #include "errno-util.h"
11 #include "fd-util.h"
12 #include "hashmap.h"
13 #include "nulstr-util.h"
14 #include "path-util.h"
15 #include "string-util.h"
16 #include "tests.h"
17 #include "time-util.h"
18 
test_sd_device_one(sd_device * d)19 static void test_sd_device_one(sd_device *d) {
20         _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
21         const char *syspath, *sysname, *subsystem = NULL, *devname, *val;
22         bool is_block = false;
23         dev_t devnum;
24         usec_t usec;
25         int ifindex, r;
26 
27         assert_se(sd_device_get_syspath(d, &syspath) >= 0);
28         assert_se(path_startswith(syspath, "/sys"));
29         assert_se(sd_device_get_sysname(d, &sysname) >= 0);
30 
31         log_info("%s(%s)", __func__, syspath);
32 
33         assert_se(sd_device_new_from_syspath(&dev, syspath) >= 0);
34         assert_se(sd_device_get_syspath(dev, &val) >= 0);
35         assert_se(streq(syspath, val));
36         dev = sd_device_unref(dev);
37 
38         assert_se(sd_device_new_from_path(&dev, syspath) >= 0);
39         assert_se(sd_device_get_syspath(dev, &val) >= 0);
40         assert_se(streq(syspath, val));
41         dev = sd_device_unref(dev);
42 
43         r = sd_device_get_ifindex(d, &ifindex);
44         if (r >= 0) {
45                 assert_se(ifindex > 0);
46 
47                 r = sd_device_new_from_ifindex(&dev, ifindex);
48                 if (r == -ENODEV)
49                         log_device_warning_errno(d, r,
50                                                  "Failed to create sd-device object from ifindex %i. "
51                                                  "Maybe running on a non-host network namespace.", ifindex);
52                 else {
53                         assert_se(r >= 0);
54                         assert_se(sd_device_get_syspath(dev, &val) >= 0);
55                         assert_se(streq(syspath, val));
56                         dev = sd_device_unref(dev);
57                 }
58 
59                 /* This does not require the interface really exists on the network namespace.
60                  * Hence, this should always succeed. */
61                 assert_se(sd_device_new_from_ifname(&dev, sysname) >= 0);
62                 assert_se(sd_device_get_syspath(dev, &val) >= 0);
63                 assert_se(streq(syspath, val));
64                 dev = sd_device_unref(dev);
65         } else
66                 assert_se(r == -ENOENT);
67 
68         r = sd_device_get_subsystem(d, &subsystem);
69         if (r < 0)
70                 assert_se(r == -ENOENT);
71         else if (!streq(subsystem, "gpio")) { /* Unfortunately, there exist /sys/class/gpio and /sys/bus/gpio.
72                                                * Hence, sd_device_new_from_subsystem_sysname() and
73                                                * sd_device_new_from_device_id() may not work as expected. */
74                 const char *name, *id;
75 
76                 if (streq(subsystem, "drivers"))
77                         name = strjoina(d->driver_subsystem, ":", sysname);
78                 else
79                         name = sysname;
80                 assert_se(sd_device_new_from_subsystem_sysname(&dev, subsystem, name) >= 0);
81                 assert_se(sd_device_get_syspath(dev, &val) >= 0);
82                 assert_se(streq(syspath, val));
83                 dev = sd_device_unref(dev);
84 
85                 /* The device ID depends on subsystem. */
86                 assert_se(device_get_device_id(d, &id) >= 0);
87                 r = sd_device_new_from_device_id(&dev, id);
88                 if (r == -ENODEV && ifindex > 0)
89                         log_device_warning_errno(d, r,
90                                                  "Failed to create sd-device object from device ID \"%s\". "
91                                                  "Maybe running on a non-host network namespace.", id);
92                 else {
93                         assert_se(r >= 0);
94                         assert_se(sd_device_get_syspath(dev, &val) >= 0);
95                         assert_se(streq(syspath, val));
96                         dev = sd_device_unref(dev);
97                 }
98 
99                 /* These require udev database, and reading database requires device ID. */
100                 r = sd_device_get_is_initialized(d);
101                 if (r > 0) {
102                         r = sd_device_get_usec_since_initialized(d, &usec);
103                         assert_se((r >= 0 && usec > 0) || r == -ENODATA);
104                 } else
105                         assert(r == 0);
106 
107                 r = sd_device_get_property_value(d, "ID_NET_DRIVER", &val);
108                 assert_se(r >= 0 || r == -ENOENT);
109         }
110 
111         is_block = streq_ptr(subsystem, "block");
112 
113         r = sd_device_get_devname(d, &devname);
114         if (r >= 0) {
115                 r = sd_device_new_from_devname(&dev, devname);
116                 if (r >= 0) {
117                         assert_se(sd_device_get_syspath(dev, &val) >= 0);
118                         assert_se(streq(syspath, val));
119                         dev = sd_device_unref(dev);
120                 } else
121                         assert_se(r == -ENODEV || ERRNO_IS_PRIVILEGE(r));
122 
123                 r = sd_device_new_from_path(&dev, devname);
124                 if (r >= 0) {
125                         assert_se(sd_device_get_syspath(dev, &val) >= 0);
126                         assert_se(streq(syspath, val));
127                         dev = sd_device_unref(dev);
128 
129                         _cleanup_close_ int fd = -1;
130                         fd = sd_device_open(d, O_CLOEXEC| O_NONBLOCK | (is_block ? O_RDONLY : O_NOCTTY | O_PATH));
131                         assert_se(fd >= 0 || ERRNO_IS_PRIVILEGE(fd));
132                 } else
133                         assert_se(r == -ENODEV || ERRNO_IS_PRIVILEGE(r));
134         } else
135                 assert_se(r == -ENOENT);
136 
137         r = sd_device_get_devnum(d, &devnum);
138         if (r >= 0) {
139                 _cleanup_free_ char *p = NULL;
140 
141                 assert_se(major(devnum) > 0);
142 
143                 assert_se(sd_device_new_from_devnum(&dev, is_block ? 'b' : 'c', devnum) >= 0);
144                 assert_se(sd_device_get_syspath(dev, &val) >= 0);
145                 assert_se(streq(syspath, val));
146                 dev = sd_device_unref(dev);
147 
148                 assert_se(asprintf(&p, "/dev/%s/%u:%u", is_block ? "block" : "char", major(devnum), minor(devnum)) >= 0);
149                 assert_se(sd_device_new_from_devname(&dev, p) >= 0);
150                 assert_se(sd_device_get_syspath(dev, &val) >= 0);
151                 assert_se(streq(syspath, val));
152                 dev = sd_device_unref(dev);
153 
154                 assert_se(sd_device_new_from_path(&dev, p) >= 0);
155                 assert_se(sd_device_get_syspath(dev, &val) >= 0);
156                 assert_se(streq(syspath, val));
157                 dev = sd_device_unref(dev);
158         } else
159                 assert_se(r == -ENOENT);
160 
161         assert_se(sd_device_get_devpath(d, &val) >= 0);
162 
163         r = sd_device_get_devtype(d, &val);
164         assert_se(r >= 0 || r == -ENOENT);
165 
166         r = sd_device_get_driver(d, &val);
167         assert_se(r >= 0 || r == -ENOENT);
168 
169         r = sd_device_get_sysnum(d, &val);
170         if (r >= 0) {
171                 assert_se(val > sysname);
172                 assert_se(val < sysname + strlen(sysname));
173                 assert_se(in_charset(val, DIGITS));
174                 assert_se(!isdigit(val[-1]));
175         } else
176                 assert_se(r == -ENOENT);
177 
178         r = sd_device_get_sysattr_value(d, "name_assign_type", &val);
179         assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r) || IN_SET(r, -ENOENT, -EINVAL));
180 }
181 
TEST(sd_device_enumerator_devices)182 TEST(sd_device_enumerator_devices) {
183         _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
184         sd_device *d;
185 
186         assert_se(sd_device_enumerator_new(&e) >= 0);
187         assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
188         /* On some CI environments, it seems some loop block devices and corresponding bdi devices sometimes
189          * disappear during running this test. Let's exclude them here for stability. */
190         assert_se(sd_device_enumerator_add_match_subsystem(e, "bdi", false) >= 0);
191         assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
192         FOREACH_DEVICE(e, d)
193                 test_sd_device_one(d);
194 }
195 
TEST(sd_device_enumerator_subsystems)196 TEST(sd_device_enumerator_subsystems) {
197         _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
198         sd_device *d;
199 
200         assert_se(sd_device_enumerator_new(&e) >= 0);
201         assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
202         FOREACH_SUBSYSTEM(e, d)
203                 test_sd_device_one(d);
204 }
205 
test_sd_device_enumerator_filter_subsystem_one(const char * subsystem,Hashmap * h,unsigned * ret_n_new_dev,unsigned * ret_n_removed_dev)206 static void test_sd_device_enumerator_filter_subsystem_one(
207                 const char *subsystem,
208                 Hashmap *h,
209                 unsigned *ret_n_new_dev,
210                 unsigned *ret_n_removed_dev) {
211 
212         _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
213         unsigned n_new_dev = 0, n_removed_dev = 0;
214         sd_device *d;
215 
216         assert_se(sd_device_enumerator_new(&e) >= 0);
217         assert_se(sd_device_enumerator_add_match_subsystem(e, subsystem, true) >= 0);
218         if (streq(subsystem, "block"))
219                 assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
220 
221         FOREACH_DEVICE(e, d) {
222                 const char *syspath;
223                 sd_device *t;
224 
225                 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
226                 t = hashmap_remove(h, syspath);
227 
228                 if (!t) {
229                         log_warning("New device found: subsystem:%s syspath:%s", subsystem, syspath);
230                         n_new_dev++;
231                 }
232 
233                 assert_se(!sd_device_unref(t));
234         }
235 
236         HASHMAP_FOREACH(d, h) {
237                 const char *syspath;
238 
239                 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
240                 log_warning("Device removed: subsystem:%s syspath:%s", subsystem, syspath);
241                 n_removed_dev++;
242 
243                 assert_se(!sd_device_unref(d));
244         }
245 
246         hashmap_free(h);
247 
248         *ret_n_new_dev = n_new_dev;
249         *ret_n_removed_dev = n_removed_dev;
250 }
251 
TEST(sd_device_enumerator_filter_subsystem)252 TEST(sd_device_enumerator_filter_subsystem) {
253         _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
254         _cleanup_(hashmap_freep) Hashmap *subsystems;
255         unsigned n_new_dev = 0, n_removed_dev = 0;
256         sd_device *d;
257         Hashmap *h;
258         char *s;
259 
260         assert_se(subsystems = hashmap_new(&string_hash_ops));
261         assert_se(sd_device_enumerator_new(&e) >= 0);
262         /* See comments in TEST(sd_device_enumerator_devices). */
263         assert_se(sd_device_enumerator_add_match_subsystem(e, "bdi", false) >= 0);
264         assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
265 
266         FOREACH_DEVICE(e, d) {
267                 const char *syspath, *subsystem;
268                 int r;
269 
270                 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
271 
272                 r = sd_device_get_subsystem(d, &subsystem);
273                 assert_se(r >= 0 || r == -ENOENT);
274                 if (r < 0)
275                         continue;
276 
277                 h = hashmap_get(subsystems, subsystem);
278                 if (!h) {
279                         char *str;
280                         assert_se(str = strdup(subsystem));
281                         assert_se(h = hashmap_new(&string_hash_ops));
282                         assert_se(hashmap_put(subsystems, str, h) >= 0);
283                 }
284 
285                 assert_se(hashmap_put(h, syspath, d) >= 0);
286                 assert_se(sd_device_ref(d));
287 
288                 log_debug("Added subsystem:%s syspath:%s", subsystem, syspath);
289         }
290 
291         while ((h = hashmap_steal_first_key_and_value(subsystems, (void**) &s))) {
292                 unsigned n, m;
293 
294                 test_sd_device_enumerator_filter_subsystem_one(s, TAKE_PTR(h), &n, &m);
295                 free(s);
296 
297                 n_new_dev += n;
298                 n_removed_dev += m;
299         }
300 
301         if (n_new_dev > 0)
302                 log_warning("%u new devices are found in re-scan", n_new_dev);
303         if (n_removed_dev > 0)
304                 log_warning("%u devices removed in re-scan", n_removed_dev);
305 
306         /* Assume that not so many devices are plugged or unplugged. */
307         assert_se(n_new_dev + n_removed_dev <= 10);
308 }
309 
TEST(sd_device_new_from_nulstr)310 TEST(sd_device_new_from_nulstr) {
311         const char *devlinks =
312                 "/dev/disk/by-partuuid/1290d63a-42cc-4c71-b87c-xxxxxxxxxxxx\0"
313                 "/dev/disk/by-path/pci-0000:00:0f.0-scsi-0:0:0:0-part3\0"
314                 "/dev/disk/by-label/Arch\\x20Linux\0"
315                 "/dev/disk/by-uuid/a07b87e5-4af5-4a59-bde9-yyyyyyyyyyyy\0"
316                 "/dev/disk/by-partlabel/Arch\\x20Linux\0"
317                 "\0";
318 
319         _cleanup_(sd_device_unrefp) sd_device *device = NULL, *from_nulstr = NULL;
320         _cleanup_free_ uint8_t *nulstr_copy = NULL;
321         const char *devlink;
322         const uint8_t *nulstr;
323         size_t len;
324 
325         assert_se(sd_device_new_from_syspath(&device, "/sys/class/net/lo") >= 0);
326 
327         /* Yeah, of course, setting devlink to the loopback interface is nonsense. But this is just a
328          * test for generating and parsing nulstr. For issue #17772. */
329         NULSTR_FOREACH(devlink, devlinks) {
330                 log_device_info(device, "setting devlink: %s", devlink);
331                 assert_se(device_add_devlink(device, devlink) >= 0);
332                 assert_se(set_contains(device->devlinks, devlink));
333         }
334 
335         /* These properties are necessary for device_new_from_nulstr(). See device_verify(). */
336         assert_se(device_add_property_internal(device, "SEQNUM", "1") >= 0);
337         assert_se(device_add_property_internal(device, "ACTION", "change") >= 0);
338 
339         assert_se(device_get_properties_nulstr(device, &nulstr, &len) >= 0);
340         assert_se(nulstr_copy = newdup(uint8_t, nulstr, len));
341         assert_se(device_new_from_nulstr(&from_nulstr, nulstr_copy, len) >= 0);
342 
343         NULSTR_FOREACH(devlink, devlinks) {
344                 log_device_info(from_nulstr, "checking devlink: %s", devlink);
345                 assert_se(set_contains(from_nulstr->devlinks, devlink));
346         }
347 }
348 
349 DEFINE_TEST_MAIN(LOG_INFO);
350