]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-device/sd-device.c
cgroup: Continue unit reset if cgroup is busy
[thirdparty/systemd.git] / src / libsystemd / sd-device / sd-device.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <ctype.h>
4 #include <net/if.h>
5 #include <sys/ioctl.h>
6 #include <sys/types.h>
7
8 #include "sd-device.h"
9
10 #include "alloc-util.h"
11 #include "device-internal.h"
12 #include "device-private.h"
13 #include "device-util.h"
14 #include "dirent-util.h"
15 #include "fd-util.h"
16 #include "fileio.h"
17 #include "fs-util.h"
18 #include "hashmap.h"
19 #include "macro.h"
20 #include "parse-util.h"
21 #include "path-util.h"
22 #include "set.h"
23 #include "socket-util.h"
24 #include "stat-util.h"
25 #include "stdio-util.h"
26 #include "string-util.h"
27 #include "strv.h"
28 #include "strxcpyx.h"
29 #include "util.h"
30
31 int device_new_aux(sd_device **ret) {
32 sd_device *device;
33
34 assert(ret);
35
36 device = new(sd_device, 1);
37 if (!device)
38 return -ENOMEM;
39
40 *device = (sd_device) {
41 .n_ref = 1,
42 .watch_handle = -1,
43 .devmode = (mode_t) -1,
44 .devuid = (uid_t) -1,
45 .devgid = (gid_t) -1,
46 .action = _DEVICE_ACTION_INVALID,
47 };
48
49 *ret = device;
50 return 0;
51 }
52
53 static sd_device *device_free(sd_device *device) {
54 assert(device);
55
56 sd_device_unref(device->parent);
57 free(device->syspath);
58 free(device->sysname);
59 free(device->devtype);
60 free(device->devname);
61 free(device->subsystem);
62 free(device->driver_subsystem);
63 free(device->driver);
64 free(device->id_filename);
65 free(device->properties_strv);
66 free(device->properties_nulstr);
67
68 ordered_hashmap_free_free_free(device->properties);
69 ordered_hashmap_free_free_free(device->properties_db);
70 hashmap_free_free_free(device->sysattr_values);
71 set_free_free(device->sysattrs);
72 set_free_free(device->tags);
73 set_free_free(device->devlinks);
74
75 return mfree(device);
76 }
77
78 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device, sd_device, device_free);
79
80 int device_add_property_aux(sd_device *device, const char *_key, const char *_value, bool db) {
81 OrderedHashmap **properties;
82
83 assert(device);
84 assert(_key);
85
86 if (db)
87 properties = &device->properties_db;
88 else
89 properties = &device->properties;
90
91 if (_value) {
92 _cleanup_free_ char *key = NULL, *value = NULL, *old_key = NULL, *old_value = NULL;
93 int r;
94
95 r = ordered_hashmap_ensure_allocated(properties, &string_hash_ops);
96 if (r < 0)
97 return r;
98
99 key = strdup(_key);
100 if (!key)
101 return -ENOMEM;
102
103 value = strdup(_value);
104 if (!value)
105 return -ENOMEM;
106
107 old_value = ordered_hashmap_get2(*properties, key, (void**) &old_key);
108
109 r = ordered_hashmap_replace(*properties, key, value);
110 if (r < 0)
111 return r;
112
113 key = NULL;
114 value = NULL;
115 } else {
116 _cleanup_free_ char *key = NULL;
117 _cleanup_free_ char *value = NULL;
118
119 value = ordered_hashmap_remove2(*properties, _key, (void**) &key);
120 }
121
122 if (!db) {
123 device->properties_generation++;
124 device->properties_buf_outdated = true;
125 }
126
127 return 0;
128 }
129
130 int device_add_property_internal(sd_device *device, const char *key, const char *value) {
131 return device_add_property_aux(device, key, value, false);
132 }
133
134 int device_set_syspath(sd_device *device, const char *_syspath, bool verify) {
135 _cleanup_free_ char *syspath = NULL;
136 const char *devpath;
137 int r;
138
139 assert(device);
140 assert(_syspath);
141
142 /* must be a subdirectory of /sys */
143 if (!path_startswith(_syspath, "/sys/"))
144 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
145 "sd-device: Syspath '%s' is not a subdirectory of /sys",
146 _syspath);
147
148 if (verify) {
149 r = chase_symlinks(_syspath, NULL, 0, &syspath);
150 if (r == -ENOENT)
151 return -ENODEV; /* the device does not exist (any more?) */
152 if (r < 0)
153 return log_debug_errno(r, "sd-device: Failed to get target of '%s': %m", _syspath);
154
155 if (!path_startswith(syspath, "/sys")) {
156 _cleanup_free_ char *real_sys = NULL, *new_syspath = NULL;
157 char *p;
158
159 /* /sys is a symlink to somewhere sysfs is mounted on? In that case, we convert the path to real sysfs to "/sys". */
160 r = chase_symlinks("/sys", NULL, 0, &real_sys);
161 if (r < 0)
162 return log_debug_errno(r, "sd-device: Failed to chase symlink /sys: %m");
163
164 p = path_startswith(syspath, real_sys);
165 if (!p)
166 return log_debug_errno(SYNTHETIC_ERRNO(ENODEV),
167 "sd-device: Canonicalized path '%s' does not starts with sysfs mount point '%s'",
168 syspath, real_sys);
169
170 new_syspath = strjoin("/sys/", p);
171 if (!new_syspath)
172 return -ENOMEM;
173
174 free_and_replace(syspath, new_syspath);
175 path_simplify(syspath, false);
176 }
177
178 if (path_startswith(syspath, "/sys/devices/")) {
179 char *path;
180
181 /* all 'devices' require an 'uevent' file */
182 path = strjoina(syspath, "/uevent");
183 r = access(path, F_OK);
184 if (r < 0) {
185 if (errno == ENOENT)
186 /* this is not a valid device */
187 return -ENODEV;
188
189 return log_debug_errno(errno, "sd-device: %s does not have an uevent file: %m", syspath);
190 }
191 } else {
192 /* everything else just needs to be a directory */
193 if (!is_dir(syspath, false))
194 return -ENODEV;
195 }
196 } else {
197 syspath = strdup(_syspath);
198 if (!syspath)
199 return -ENOMEM;
200 }
201
202 devpath = syspath + STRLEN("/sys");
203
204 r = device_add_property_internal(device, "DEVPATH", devpath);
205 if (r < 0)
206 return r;
207
208 free_and_replace(device->syspath, syspath);
209 device->devpath = devpath;
210 return 0;
211 }
212
213 _public_ int sd_device_new_from_syspath(sd_device **ret, const char *syspath) {
214 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
215 int r;
216
217 assert_return(ret, -EINVAL);
218 assert_return(syspath, -EINVAL);
219
220 r = device_new_aux(&device);
221 if (r < 0)
222 return r;
223
224 r = device_set_syspath(device, syspath, true);
225 if (r < 0)
226 return r;
227
228 *ret = TAKE_PTR(device);
229 return 0;
230 }
231
232 _public_ int sd_device_new_from_devnum(sd_device **ret, char type, dev_t devnum) {
233 char *syspath;
234 char id[DECIMAL_STR_MAX(unsigned) * 2 + 1];
235
236 assert_return(ret, -EINVAL);
237 assert_return(IN_SET(type, 'b', 'c'), -EINVAL);
238
239 /* use /sys/dev/{block,char}/<maj>:<min> link */
240 xsprintf(id, "%u:%u", major(devnum), minor(devnum));
241
242 syspath = strjoina("/sys/dev/", (type == 'b' ? "block" : "char"), "/", id);
243
244 return sd_device_new_from_syspath(ret, syspath);
245 }
246
247 _public_ int sd_device_new_from_subsystem_sysname(sd_device **ret, const char *subsystem, const char *sysname) {
248 char *name, *syspath;
249 size_t len = 0;
250
251 assert_return(ret, -EINVAL);
252 assert_return(subsystem, -EINVAL);
253 assert_return(sysname, -EINVAL);
254
255 if (streq(subsystem, "subsystem")) {
256 syspath = strjoina("/sys/subsystem/", sysname);
257 if (access(syspath, F_OK) >= 0)
258 return sd_device_new_from_syspath(ret, syspath);
259
260 syspath = strjoina("/sys/bus/", sysname);
261 if (access(syspath, F_OK) >= 0)
262 return sd_device_new_from_syspath(ret, syspath);
263
264 syspath = strjoina("/sys/class/", sysname);
265 if (access(syspath, F_OK) >= 0)
266 return sd_device_new_from_syspath(ret, syspath);
267 } else if (streq(subsystem, "module")) {
268 syspath = strjoina("/sys/module/", sysname);
269 if (access(syspath, F_OK) >= 0)
270 return sd_device_new_from_syspath(ret, syspath);
271 } else if (streq(subsystem, "drivers")) {
272 char subsys[PATH_MAX];
273 char *driver;
274
275 strscpy(subsys, sizeof(subsys), sysname);
276 driver = strchr(subsys, ':');
277 if (driver) {
278 driver[0] = '\0';
279 driver++;
280
281 syspath = strjoina("/sys/subsystem/", subsys, "/drivers/", driver);
282 if (access(syspath, F_OK) >= 0)
283 return sd_device_new_from_syspath(ret, syspath);
284
285 syspath = strjoina("/sys/bus/", subsys, "/drivers/", driver);
286 if (access(syspath, F_OK) >= 0)
287 return sd_device_new_from_syspath(ret, syspath);
288 }
289 }
290
291 /* translate sysname back to sysfs filename */
292 name = strdupa(sysname);
293 while (name[len] != '\0') {
294 if (name[len] == '/')
295 name[len] = '!';
296
297 len++;
298 }
299
300 syspath = strjoina("/sys/subsystem/", subsystem, "/devices/", name);
301 if (access(syspath, F_OK) >= 0)
302 return sd_device_new_from_syspath(ret, syspath);
303
304 syspath = strjoina("/sys/bus/", subsystem, "/devices/", name);
305 if (access(syspath, F_OK) >= 0)
306 return sd_device_new_from_syspath(ret, syspath);
307
308 syspath = strjoina("/sys/class/", subsystem, "/", name);
309 if (access(syspath, F_OK) >= 0)
310 return sd_device_new_from_syspath(ret, syspath);
311
312 syspath = strjoina("/sys/firmware/", subsystem, "/", sysname);
313 if (access(syspath, F_OK) >= 0)
314 return sd_device_new_from_syspath(ret, syspath);
315
316 return -ENODEV;
317 }
318
319 int device_set_devtype(sd_device *device, const char *_devtype) {
320 _cleanup_free_ char *devtype = NULL;
321 int r;
322
323 assert(device);
324 assert(_devtype);
325
326 devtype = strdup(_devtype);
327 if (!devtype)
328 return -ENOMEM;
329
330 r = device_add_property_internal(device, "DEVTYPE", devtype);
331 if (r < 0)
332 return r;
333
334 free_and_replace(device->devtype, devtype);
335
336 return 0;
337 }
338
339 int device_set_ifindex(sd_device *device, const char *_ifindex) {
340 int ifindex, r;
341
342 assert(device);
343 assert(_ifindex);
344
345 r = parse_ifindex(_ifindex, &ifindex);
346 if (r < 0)
347 return r;
348
349 r = device_add_property_internal(device, "IFINDEX", _ifindex);
350 if (r < 0)
351 return r;
352
353 device->ifindex = ifindex;
354
355 return 0;
356 }
357
358 int device_set_devname(sd_device *device, const char *_devname) {
359 _cleanup_free_ char *devname = NULL;
360 int r;
361
362 assert(device);
363 assert(_devname);
364
365 if (_devname[0] != '/') {
366 r = asprintf(&devname, "/dev/%s", _devname);
367 if (r < 0)
368 return -ENOMEM;
369 } else {
370 devname = strdup(_devname);
371 if (!devname)
372 return -ENOMEM;
373 }
374
375 r = device_add_property_internal(device, "DEVNAME", devname);
376 if (r < 0)
377 return r;
378
379 free_and_replace(device->devname, devname);
380
381 return 0;
382 }
383
384 int device_set_devmode(sd_device *device, const char *_devmode) {
385 unsigned devmode;
386 int r;
387
388 assert(device);
389 assert(_devmode);
390
391 r = safe_atou(_devmode, &devmode);
392 if (r < 0)
393 return r;
394
395 if (devmode > 07777)
396 return -EINVAL;
397
398 r = device_add_property_internal(device, "DEVMODE", _devmode);
399 if (r < 0)
400 return r;
401
402 device->devmode = devmode;
403
404 return 0;
405 }
406
407 int device_set_devnum(sd_device *device, const char *major, const char *minor) {
408 unsigned maj = 0, min = 0;
409 int r;
410
411 assert(device);
412 assert(major);
413
414 r = safe_atou(major, &maj);
415 if (r < 0)
416 return r;
417 if (!maj)
418 return 0;
419
420 if (minor) {
421 r = safe_atou(minor, &min);
422 if (r < 0)
423 return r;
424 }
425
426 r = device_add_property_internal(device, "MAJOR", major);
427 if (r < 0)
428 return r;
429
430 if (minor) {
431 r = device_add_property_internal(device, "MINOR", minor);
432 if (r < 0)
433 return r;
434 }
435
436 device->devnum = makedev(maj, min);
437
438 return 0;
439 }
440
441 static int handle_uevent_line(sd_device *device, const char *key, const char *value, const char **major, const char **minor) {
442 int r;
443
444 assert(device);
445 assert(key);
446 assert(value);
447 assert(major);
448 assert(minor);
449
450 if (streq(key, "DEVTYPE")) {
451 r = device_set_devtype(device, value);
452 if (r < 0)
453 return r;
454 } else if (streq(key, "IFINDEX")) {
455 r = device_set_ifindex(device, value);
456 if (r < 0)
457 return r;
458 } else if (streq(key, "DEVNAME")) {
459 r = device_set_devname(device, value);
460 if (r < 0)
461 return r;
462 } else if (streq(key, "DEVMODE")) {
463 r = device_set_devmode(device, value);
464 if (r < 0)
465 return r;
466 } else if (streq(key, "MAJOR"))
467 *major = value;
468 else if (streq(key, "MINOR"))
469 *minor = value;
470 else {
471 r = device_add_property_internal(device, key, value);
472 if (r < 0)
473 return r;
474 }
475
476 return 0;
477 }
478
479 int device_read_uevent_file(sd_device *device) {
480 _cleanup_free_ char *uevent = NULL;
481 const char *syspath, *key = NULL, *value = NULL, *major = NULL, *minor = NULL;
482 char *path;
483 size_t uevent_len;
484 unsigned i;
485 int r;
486
487 enum {
488 PRE_KEY,
489 KEY,
490 PRE_VALUE,
491 VALUE,
492 INVALID_LINE,
493 } state = PRE_KEY;
494
495 assert(device);
496
497 if (device->uevent_loaded || device->sealed)
498 return 0;
499
500 r = sd_device_get_syspath(device, &syspath);
501 if (r < 0)
502 return r;
503
504 path = strjoina(syspath, "/uevent");
505
506 r = read_full_file(path, &uevent, &uevent_len);
507 if (r == -EACCES) {
508 /* empty uevent files may be write-only */
509 device->uevent_loaded = true;
510 return 0;
511 }
512 if (r == -ENOENT)
513 /* some devices may not have uevent files, see set_syspath() */
514 return 0;
515 if (r < 0)
516 return log_device_debug_errno(device, r, "sd-device: Failed to read uevent file '%s': %m", path);
517
518 device->uevent_loaded = true;
519
520 for (i = 0; i < uevent_len; i++)
521 switch (state) {
522 case PRE_KEY:
523 if (!strchr(NEWLINE, uevent[i])) {
524 key = &uevent[i];
525
526 state = KEY;
527 }
528
529 break;
530 case KEY:
531 if (uevent[i] == '=') {
532 uevent[i] = '\0';
533
534 state = PRE_VALUE;
535 } else if (strchr(NEWLINE, uevent[i])) {
536 uevent[i] = '\0';
537 log_device_debug(device, "sd-device: Invalid uevent line '%s', ignoring", key);
538
539 state = PRE_KEY;
540 }
541
542 break;
543 case PRE_VALUE:
544 value = &uevent[i];
545 state = VALUE;
546
547 _fallthrough_; /* to handle empty property */
548 case VALUE:
549 if (strchr(NEWLINE, uevent[i])) {
550 uevent[i] = '\0';
551
552 r = handle_uevent_line(device, key, value, &major, &minor);
553 if (r < 0)
554 log_device_debug_errno(device, r, "sd-device: Failed to handle uevent entry '%s=%s', ignoring: %m", key, value);
555
556 state = PRE_KEY;
557 }
558
559 break;
560 default:
561 assert_not_reached("Invalid state when parsing uevent file");
562 }
563
564 if (major) {
565 r = device_set_devnum(device, major, minor);
566 if (r < 0)
567 log_device_debug_errno(device, r, "sd-device: Failed to set 'MAJOR=%s' or 'MINOR=%s' from '%s', ignoring: %m", major, minor, path);
568 }
569
570 return 0;
571 }
572
573 _public_ int sd_device_get_ifindex(sd_device *device, int *ifindex) {
574 int r;
575
576 assert_return(device, -EINVAL);
577
578 r = device_read_uevent_file(device);
579 if (r < 0)
580 return r;
581
582 if (device->ifindex <= 0)
583 return -ENOENT;
584
585 if (ifindex)
586 *ifindex = device->ifindex;
587
588 return 0;
589 }
590
591 _public_ int sd_device_new_from_device_id(sd_device **ret, const char *id) {
592 int r;
593
594 assert_return(ret, -EINVAL);
595 assert_return(id, -EINVAL);
596
597 switch (id[0]) {
598 case 'b':
599 case 'c': {
600 dev_t devt;
601
602 if (isempty(id))
603 return -EINVAL;
604
605 r = parse_dev(id + 1, &devt);
606 if (r < 0)
607 return r;
608
609 return sd_device_new_from_devnum(ret, id[0], devt);
610 }
611
612 case 'n': {
613 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
614 _cleanup_close_ int sk = -1;
615 struct ifreq ifr = {};
616 int ifindex;
617
618 r = parse_ifindex(&id[1], &ifr.ifr_ifindex);
619 if (r < 0)
620 return r;
621
622 sk = socket_ioctl_fd();
623 if (sk < 0)
624 return sk;
625
626 r = ioctl(sk, SIOCGIFNAME, &ifr);
627 if (r < 0)
628 return -errno;
629
630 r = sd_device_new_from_subsystem_sysname(&device, "net", ifr.ifr_name);
631 if (r < 0)
632 return r;
633
634 r = sd_device_get_ifindex(device, &ifindex);
635 if (r < 0)
636 return r;
637
638 /* this is racey, so we might end up with the wrong device */
639 if (ifr.ifr_ifindex != ifindex)
640 return -ENODEV;
641
642 *ret = TAKE_PTR(device);
643 return 0;
644 }
645
646 case '+': {
647 char subsys[PATH_MAX];
648 char *sysname;
649
650 (void) strscpy(subsys, sizeof(subsys), id + 1);
651 sysname = strchr(subsys, ':');
652 if (!sysname)
653 return -EINVAL;
654
655 sysname[0] = '\0';
656 sysname++;
657
658 return sd_device_new_from_subsystem_sysname(ret, subsys, sysname);
659 }
660
661 default:
662 return -EINVAL;
663 }
664 }
665
666 _public_ int sd_device_get_syspath(sd_device *device, const char **ret) {
667 assert_return(device, -EINVAL);
668 assert_return(ret, -EINVAL);
669
670 assert(path_startswith(device->syspath, "/sys/"));
671
672 *ret = device->syspath;
673
674 return 0;
675 }
676
677 static int device_new_from_child(sd_device **ret, sd_device *child) {
678 _cleanup_free_ char *path = NULL;
679 const char *subdir, *syspath;
680 int r;
681
682 assert(ret);
683 assert(child);
684
685 r = sd_device_get_syspath(child, &syspath);
686 if (r < 0)
687 return r;
688
689 path = strdup(syspath);
690 if (!path)
691 return -ENOMEM;
692 subdir = path + STRLEN("/sys");
693
694 for (;;) {
695 char *pos;
696
697 pos = strrchr(subdir, '/');
698 if (!pos || pos < subdir + 2)
699 break;
700
701 *pos = '\0';
702
703 r = sd_device_new_from_syspath(ret, path);
704 if (r < 0)
705 continue;
706
707 return 0;
708 }
709
710 return -ENODEV;
711 }
712
713 _public_ int sd_device_get_parent(sd_device *child, sd_device **ret) {
714
715 assert_return(ret, -EINVAL);
716 assert_return(child, -EINVAL);
717
718 if (!child->parent_set) {
719 child->parent_set = true;
720
721 (void) device_new_from_child(&child->parent, child);
722 }
723
724 if (!child->parent)
725 return -ENOENT;
726
727 *ret = child->parent;
728 return 0;
729 }
730
731 int device_set_subsystem(sd_device *device, const char *_subsystem) {
732 _cleanup_free_ char *subsystem = NULL;
733 int r;
734
735 assert(device);
736 assert(_subsystem);
737
738 subsystem = strdup(_subsystem);
739 if (!subsystem)
740 return -ENOMEM;
741
742 r = device_add_property_internal(device, "SUBSYSTEM", subsystem);
743 if (r < 0)
744 return r;
745
746 device->subsystem_set = true;
747 return free_and_replace(device->subsystem, subsystem);
748 }
749
750 static int device_set_drivers_subsystem(sd_device *device, const char *_subsystem) {
751 _cleanup_free_ char *subsystem = NULL;
752 int r;
753
754 assert(device);
755 assert(_subsystem);
756 assert(*_subsystem);
757
758 subsystem = strdup(_subsystem);
759 if (!subsystem)
760 return -ENOMEM;
761
762 r = device_set_subsystem(device, "drivers");
763 if (r < 0)
764 return r;
765
766 return free_and_replace(device->driver_subsystem, subsystem);
767 }
768
769 _public_ int sd_device_get_subsystem(sd_device *device, const char **ret) {
770 const char *syspath, *drivers = NULL;
771 int r;
772
773 assert_return(ret, -EINVAL);
774 assert_return(device, -EINVAL);
775
776 r = sd_device_get_syspath(device, &syspath);
777 if (r < 0)
778 return r;
779
780 if (!device->subsystem_set) {
781 _cleanup_free_ char *subsystem = NULL;
782 char *path;
783
784 /* read 'subsystem' link */
785 path = strjoina(syspath, "/subsystem");
786 r = readlink_value(path, &subsystem);
787 if (r >= 0)
788 r = device_set_subsystem(device, subsystem);
789 /* use implicit names */
790 else if (path_startswith(device->devpath, "/module/"))
791 r = device_set_subsystem(device, "module");
792 else if (!(drivers = strstr(syspath, "/drivers/")) &&
793 PATH_STARTSWITH_SET(device->devpath, "/subsystem/",
794 "/class/",
795 "/bus/"))
796 r = device_set_subsystem(device, "subsystem");
797 if (r < 0 && r != -ENOENT)
798 return log_device_debug_errno(device, r, "sd-device: Failed to set subsystem for %s: %m", device->devpath);
799
800 device->subsystem_set = true;
801 } else if (!device->driver_subsystem_set)
802 drivers = strstr(syspath, "/drivers/");
803
804 if (!device->driver_subsystem_set) {
805 if (drivers) {
806 _cleanup_free_ char *subpath = NULL;
807
808 subpath = strndup(syspath, drivers - syspath);
809 if (!subpath)
810 r = -ENOMEM;
811 else {
812 const char *subsys;
813
814 subsys = strrchr(subpath, '/');
815 if (!subsys)
816 r = -EINVAL;
817 else
818 r = device_set_drivers_subsystem(device, subsys + 1);
819 }
820 if (r < 0 && r != -ENOENT)
821 return log_device_debug_errno(device, r, "sd-device: Failed to set subsystem for driver %s: %m", device->devpath);
822 }
823
824 device->driver_subsystem_set = true;
825 }
826
827 if (!device->subsystem)
828 return -ENOENT;
829
830 *ret = device->subsystem;
831 return 0;
832 }
833
834 _public_ int sd_device_get_devtype(sd_device *device, const char **devtype) {
835 int r;
836
837 assert(devtype);
838 assert(device);
839
840 r = device_read_uevent_file(device);
841 if (r < 0)
842 return r;
843
844 if (!device->devtype)
845 return -ENOENT;
846
847 *devtype = device->devtype;
848
849 return 0;
850 }
851
852 _public_ int sd_device_get_parent_with_subsystem_devtype(sd_device *child, const char *subsystem, const char *devtype, sd_device **ret) {
853 sd_device *parent = NULL;
854 int r;
855
856 assert_return(child, -EINVAL);
857 assert_return(subsystem, -EINVAL);
858
859 r = sd_device_get_parent(child, &parent);
860 while (r >= 0) {
861 const char *parent_subsystem = NULL;
862 const char *parent_devtype = NULL;
863
864 (void) sd_device_get_subsystem(parent, &parent_subsystem);
865 if (streq_ptr(parent_subsystem, subsystem)) {
866 if (!devtype)
867 break;
868
869 (void) sd_device_get_devtype(parent, &parent_devtype);
870 if (streq_ptr(parent_devtype, devtype))
871 break;
872 }
873 r = sd_device_get_parent(parent, &parent);
874 }
875
876 if (r < 0)
877 return r;
878
879 *ret = parent;
880 return 0;
881 }
882
883 _public_ int sd_device_get_devnum(sd_device *device, dev_t *devnum) {
884 int r;
885
886 assert_return(device, -EINVAL);
887
888 r = device_read_uevent_file(device);
889 if (r < 0)
890 return r;
891
892 if (major(device->devnum) <= 0)
893 return -ENOENT;
894
895 if (devnum)
896 *devnum = device->devnum;
897
898 return 0;
899 }
900
901 int device_set_driver(sd_device *device, const char *_driver) {
902 _cleanup_free_ char *driver = NULL;
903 int r;
904
905 assert(device);
906 assert(_driver);
907
908 driver = strdup(_driver);
909 if (!driver)
910 return -ENOMEM;
911
912 r = device_add_property_internal(device, "DRIVER", driver);
913 if (r < 0)
914 return r;
915
916 device->driver_set = true;
917 return free_and_replace(device->driver, driver);
918 }
919
920 _public_ int sd_device_get_driver(sd_device *device, const char **ret) {
921 assert_return(device, -EINVAL);
922 assert_return(ret, -EINVAL);
923
924 if (!device->driver_set) {
925 _cleanup_free_ char *driver = NULL;
926 const char *syspath;
927 char *path;
928 int r;
929
930 r = sd_device_get_syspath(device, &syspath);
931 if (r < 0)
932 return r;
933
934 path = strjoina(syspath, "/driver");
935 r = readlink_value(path, &driver);
936 if (r >= 0) {
937 r = device_set_driver(device, driver);
938 if (r < 0)
939 return log_device_debug_errno(device, r, "sd-device: Failed to set driver for %s: %m", device->devpath);
940 } else if (r == -ENOENT)
941 device->driver_set = true;
942 else
943 return log_device_debug_errno(device, r, "sd-device: Failed to set driver for %s: %m", device->devpath);
944 }
945
946 if (!device->driver)
947 return -ENOENT;
948
949 *ret = device->driver;
950 return 0;
951 }
952
953 _public_ int sd_device_get_devpath(sd_device *device, const char **devpath) {
954 assert_return(device, -EINVAL);
955 assert_return(devpath, -EINVAL);
956
957 assert(device->devpath);
958 assert(device->devpath[0] == '/');
959
960 *devpath = device->devpath;
961 return 0;
962 }
963
964 _public_ int sd_device_get_devname(sd_device *device, const char **devname) {
965 int r;
966
967 assert_return(device, -EINVAL);
968 assert_return(devname, -EINVAL);
969
970 r = device_read_uevent_file(device);
971 if (r < 0)
972 return r;
973
974 if (!device->devname)
975 return -ENOENT;
976
977 assert(path_startswith(device->devname, "/dev/"));
978
979 *devname = device->devname;
980 return 0;
981 }
982
983 static int device_set_sysname(sd_device *device) {
984 _cleanup_free_ char *sysname = NULL;
985 const char *sysnum = NULL;
986 const char *pos;
987 size_t len = 0;
988
989 if (!device->devpath)
990 return -EINVAL;
991
992 pos = strrchr(device->devpath, '/');
993 if (!pos)
994 return -EINVAL;
995 pos++;
996
997 /* devpath is not a root directory */
998 if (*pos == '\0' || pos <= device->devpath)
999 return -EINVAL;
1000
1001 sysname = strdup(pos);
1002 if (!sysname)
1003 return -ENOMEM;
1004
1005 /* some devices have '!' in their name, change that to '/' */
1006 while (sysname[len] != '\0') {
1007 if (sysname[len] == '!')
1008 sysname[len] = '/';
1009
1010 len++;
1011 }
1012
1013 /* trailing number */
1014 while (len > 0 && isdigit(sysname[--len]))
1015 sysnum = &sysname[len];
1016
1017 if (len == 0)
1018 sysnum = NULL;
1019
1020 device->sysname_set = true;
1021 device->sysnum = sysnum;
1022 return free_and_replace(device->sysname, sysname);
1023 }
1024
1025 _public_ int sd_device_get_sysname(sd_device *device, const char **ret) {
1026 int r;
1027
1028 assert_return(device, -EINVAL);
1029 assert_return(ret, -EINVAL);
1030
1031 if (!device->sysname_set) {
1032 r = device_set_sysname(device);
1033 if (r < 0)
1034 return r;
1035 }
1036
1037 assert_return(device->sysname, -ENOENT);
1038
1039 *ret = device->sysname;
1040 return 0;
1041 }
1042
1043 _public_ int sd_device_get_sysnum(sd_device *device, const char **ret) {
1044 int r;
1045
1046 assert_return(device, -EINVAL);
1047 assert_return(ret, -EINVAL);
1048
1049 if (!device->sysname_set) {
1050 r = device_set_sysname(device);
1051 if (r < 0)
1052 return r;
1053 }
1054
1055 if (!device->sysnum)
1056 return -ENOENT;
1057
1058 *ret = device->sysnum;
1059 return 0;
1060 }
1061
1062 static bool is_valid_tag(const char *tag) {
1063 assert(tag);
1064
1065 return !strchr(tag, ':') && !strchr(tag, ' ');
1066 }
1067
1068 int device_add_tag(sd_device *device, const char *tag) {
1069 int r;
1070
1071 assert(device);
1072 assert(tag);
1073
1074 if (!is_valid_tag(tag))
1075 return -EINVAL;
1076
1077 r = set_ensure_allocated(&device->tags, &string_hash_ops);
1078 if (r < 0)
1079 return r;
1080
1081 r = set_put_strdup(device->tags, tag);
1082 if (r < 0)
1083 return r;
1084
1085 device->tags_generation++;
1086 device->property_tags_outdated = true;
1087
1088 return 0;
1089 }
1090
1091 int device_add_devlink(sd_device *device, const char *devlink) {
1092 int r;
1093
1094 assert(device);
1095 assert(devlink);
1096
1097 r = set_ensure_allocated(&device->devlinks, &string_hash_ops);
1098 if (r < 0)
1099 return r;
1100
1101 r = set_put_strdup(device->devlinks, devlink);
1102 if (r < 0)
1103 return r;
1104
1105 device->devlinks_generation++;
1106 device->property_devlinks_outdated = true;
1107
1108 return 0;
1109 }
1110
1111 static int device_add_property_internal_from_string(sd_device *device, const char *str) {
1112 _cleanup_free_ char *key = NULL;
1113 char *value;
1114 int r;
1115
1116 assert(device);
1117 assert(str);
1118
1119 key = strdup(str);
1120 if (!key)
1121 return -ENOMEM;
1122
1123 value = strchr(key, '=');
1124 if (!value)
1125 return -EINVAL;
1126
1127 *value = '\0';
1128
1129 if (isempty(++value))
1130 value = NULL;
1131
1132 /* Add the property to both sd_device::properties and sd_device::properties_db,
1133 * as this is called by only handle_db_line(). */
1134 r = device_add_property_aux(device, key, value, false);
1135 if (r < 0)
1136 return r;
1137
1138 return device_add_property_aux(device, key, value, true);
1139 }
1140
1141 int device_set_usec_initialized(sd_device *device, usec_t when) {
1142 char s[DECIMAL_STR_MAX(usec_t)];
1143 int r;
1144
1145 assert(device);
1146
1147 xsprintf(s, USEC_FMT, when);
1148
1149 r = device_add_property_internal(device, "USEC_INITIALIZED", s);
1150 if (r < 0)
1151 return r;
1152
1153 device->usec_initialized = when;
1154 return 0;
1155 }
1156
1157 static int handle_db_line(sd_device *device, char key, const char *value) {
1158 char *path;
1159 int r;
1160
1161 assert(device);
1162 assert(value);
1163
1164 switch (key) {
1165 case 'G':
1166 r = device_add_tag(device, value);
1167 if (r < 0)
1168 return r;
1169
1170 break;
1171 case 'S':
1172 path = strjoina("/dev/", value);
1173 r = device_add_devlink(device, path);
1174 if (r < 0)
1175 return r;
1176
1177 break;
1178 case 'E':
1179 r = device_add_property_internal_from_string(device, value);
1180 if (r < 0)
1181 return r;
1182
1183 break;
1184 case 'I': {
1185 usec_t t;
1186
1187 r = safe_atou64(value, &t);
1188 if (r < 0)
1189 return r;
1190
1191 r = device_set_usec_initialized(device, t);
1192 if (r < 0)
1193 return r;
1194
1195 break;
1196 }
1197 case 'L':
1198 r = safe_atoi(value, &device->devlink_priority);
1199 if (r < 0)
1200 return r;
1201
1202 break;
1203 case 'W':
1204 r = safe_atoi(value, &device->watch_handle);
1205 if (r < 0)
1206 return r;
1207
1208 break;
1209 default:
1210 log_device_debug(device, "sd-device: Unknown key '%c' in device db, ignoring", key);
1211 }
1212
1213 return 0;
1214 }
1215
1216 int device_get_id_filename(sd_device *device, const char **ret) {
1217 assert(device);
1218 assert(ret);
1219
1220 if (!device->id_filename) {
1221 _cleanup_free_ char *id = NULL;
1222 const char *subsystem;
1223 dev_t devnum;
1224 int ifindex, r;
1225
1226 r = sd_device_get_subsystem(device, &subsystem);
1227 if (r < 0)
1228 return r;
1229
1230 if (sd_device_get_devnum(device, &devnum) >= 0) {
1231 assert(subsystem);
1232
1233 /* use dev_t — b259:131072, c254:0 */
1234 r = asprintf(&id, "%c%u:%u",
1235 streq(subsystem, "block") ? 'b' : 'c',
1236 major(devnum), minor(devnum));
1237 if (r < 0)
1238 return -ENOMEM;
1239 } else if (sd_device_get_ifindex(device, &ifindex) >= 0) {
1240 /* use netdev ifindex — n3 */
1241 r = asprintf(&id, "n%u", (unsigned) ifindex);
1242 if (r < 0)
1243 return -ENOMEM;
1244 } else {
1245 /* use $subsys:$sysname — pci:0000:00:1f.2
1246 * sysname() has '!' translated, get it from devpath
1247 */
1248 const char *sysname;
1249
1250 sysname = basename(device->devpath);
1251 if (!sysname)
1252 return -EINVAL;
1253
1254 if (!subsystem)
1255 return -EINVAL;
1256
1257 if (streq(subsystem, "drivers")) {
1258 /* the 'drivers' pseudo-subsystem is special, and needs the real subsystem
1259 * encoded as well */
1260 r = asprintf(&id, "+drivers:%s:%s", device->driver_subsystem, sysname);
1261 if (r < 0)
1262 return -ENOMEM;
1263 } else {
1264 r = asprintf(&id, "+%s:%s", subsystem, sysname);
1265 if (r < 0)
1266 return -ENOMEM;
1267 }
1268 }
1269
1270 device->id_filename = TAKE_PTR(id);
1271 }
1272
1273 *ret = device->id_filename;
1274 return 0;
1275 }
1276
1277 int device_read_db_internal_filename(sd_device *device, const char *filename) {
1278 _cleanup_free_ char *db = NULL;
1279 const char *value;
1280 size_t db_len, i;
1281 char key;
1282 int r;
1283
1284 enum {
1285 PRE_KEY,
1286 KEY,
1287 PRE_VALUE,
1288 VALUE,
1289 INVALID_LINE,
1290 } state = PRE_KEY;
1291
1292 assert(device);
1293 assert(filename);
1294
1295 r = read_full_file(filename, &db, &db_len);
1296 if (r < 0) {
1297 if (r == -ENOENT)
1298 return 0;
1299
1300 return log_device_debug_errno(device, r, "sd-device: Failed to read db '%s': %m", filename);
1301 }
1302
1303 /* devices with a database entry are initialized */
1304 device->is_initialized = true;
1305
1306 device->db_loaded = true;
1307
1308 for (i = 0; i < db_len; i++) {
1309 switch (state) {
1310 case PRE_KEY:
1311 if (!strchr(NEWLINE, db[i])) {
1312 key = db[i];
1313
1314 state = KEY;
1315 }
1316
1317 break;
1318 case KEY:
1319 if (db[i] != ':') {
1320 log_device_debug(device, "sd-device: Invalid db entry with key '%c', ignoring", key);
1321
1322 state = INVALID_LINE;
1323 } else {
1324 db[i] = '\0';
1325
1326 state = PRE_VALUE;
1327 }
1328
1329 break;
1330 case PRE_VALUE:
1331 value = &db[i];
1332
1333 state = VALUE;
1334
1335 break;
1336 case INVALID_LINE:
1337 if (strchr(NEWLINE, db[i]))
1338 state = PRE_KEY;
1339
1340 break;
1341 case VALUE:
1342 if (strchr(NEWLINE, db[i])) {
1343 db[i] = '\0';
1344 r = handle_db_line(device, key, value);
1345 if (r < 0)
1346 log_device_debug_errno(device, r, "sd-device: Failed to handle db entry '%c:%s', ignoring: %m", key, value);
1347
1348 state = PRE_KEY;
1349 }
1350
1351 break;
1352 default:
1353 return log_device_debug_errno(device, SYNTHETIC_ERRNO(EINVAL), "sd-device: invalid db syntax.");
1354 }
1355 }
1356
1357 return 0;
1358 }
1359
1360 int device_read_db_internal(sd_device *device, bool force) {
1361 const char *id, *path;
1362 int r;
1363
1364 assert(device);
1365
1366 if (device->db_loaded || (!force && device->sealed))
1367 return 0;
1368
1369 r = device_get_id_filename(device, &id);
1370 if (r < 0)
1371 return r;
1372
1373 path = strjoina("/run/udev/data/", id);
1374
1375 return device_read_db_internal_filename(device, path);
1376 }
1377
1378 _public_ int sd_device_get_is_initialized(sd_device *device) {
1379 int r;
1380
1381 assert_return(device, -EINVAL);
1382
1383 r = device_read_db(device);
1384 if (r < 0)
1385 return r;
1386
1387 return device->is_initialized;
1388 }
1389
1390 _public_ int sd_device_get_usec_since_initialized(sd_device *device, uint64_t *usec) {
1391 usec_t now_ts;
1392 int r;
1393
1394 assert_return(device, -EINVAL);
1395 assert_return(usec, -EINVAL);
1396
1397 r = device_read_db(device);
1398 if (r < 0)
1399 return r;
1400
1401 if (!device->is_initialized)
1402 return -EBUSY;
1403
1404 if (!device->usec_initialized)
1405 return -ENODATA;
1406
1407 now_ts = now(clock_boottime_or_monotonic());
1408
1409 if (now_ts < device->usec_initialized)
1410 return -EIO;
1411
1412 *usec = now_ts - device->usec_initialized;
1413 return 0;
1414 }
1415
1416 _public_ const char *sd_device_get_tag_first(sd_device *device) {
1417 void *v;
1418
1419 assert_return(device, NULL);
1420
1421 (void) device_read_db(device);
1422
1423 device->tags_iterator_generation = device->tags_generation;
1424 device->tags_iterator = ITERATOR_FIRST;
1425
1426 (void) set_iterate(device->tags, &device->tags_iterator, &v);
1427 return v;
1428 }
1429
1430 _public_ const char *sd_device_get_tag_next(sd_device *device) {
1431 void *v;
1432
1433 assert_return(device, NULL);
1434
1435 (void) device_read_db(device);
1436
1437 if (device->tags_iterator_generation != device->tags_generation)
1438 return NULL;
1439
1440 (void) set_iterate(device->tags, &device->tags_iterator, &v);
1441 return v;
1442 }
1443
1444 _public_ const char *sd_device_get_devlink_first(sd_device *device) {
1445 void *v;
1446
1447 assert_return(device, NULL);
1448
1449 (void) device_read_db(device);
1450
1451 device->devlinks_iterator_generation = device->devlinks_generation;
1452 device->devlinks_iterator = ITERATOR_FIRST;
1453
1454 (void) set_iterate(device->devlinks, &device->devlinks_iterator, &v);
1455 return v;
1456 }
1457
1458 _public_ const char *sd_device_get_devlink_next(sd_device *device) {
1459 void *v;
1460
1461 assert_return(device, NULL);
1462
1463 (void) device_read_db(device);
1464
1465 if (device->devlinks_iterator_generation != device->devlinks_generation)
1466 return NULL;
1467
1468 (void) set_iterate(device->devlinks, &device->devlinks_iterator, &v);
1469 return v;
1470 }
1471
1472 int device_properties_prepare(sd_device *device) {
1473 int r;
1474
1475 assert(device);
1476
1477 r = device_read_uevent_file(device);
1478 if (r < 0)
1479 return r;
1480
1481 r = device_read_db(device);
1482 if (r < 0)
1483 return r;
1484
1485 if (device->property_devlinks_outdated) {
1486 _cleanup_free_ char *devlinks = NULL;
1487 size_t devlinks_allocated = 0, devlinks_len = 0;
1488 const char *devlink;
1489
1490 for (devlink = sd_device_get_devlink_first(device); devlink; devlink = sd_device_get_devlink_next(device)) {
1491 char *e;
1492
1493 if (!GREEDY_REALLOC(devlinks, devlinks_allocated, devlinks_len + strlen(devlink) + 2))
1494 return -ENOMEM;
1495 if (devlinks_len > 0)
1496 stpcpy(devlinks + devlinks_len++, " ");
1497 e = stpcpy(devlinks + devlinks_len, devlink);
1498 devlinks_len = e - devlinks;
1499 }
1500
1501 r = device_add_property_internal(device, "DEVLINKS", devlinks);
1502 if (r < 0)
1503 return r;
1504
1505 device->property_devlinks_outdated = false;
1506 }
1507
1508 if (device->property_tags_outdated) {
1509 _cleanup_free_ char *tags = NULL;
1510 size_t tags_allocated = 0, tags_len = 0;
1511 const char *tag;
1512
1513 if (!GREEDY_REALLOC(tags, tags_allocated, 2))
1514 return -ENOMEM;
1515 stpcpy(tags, ":");
1516 tags_len++;
1517
1518 for (tag = sd_device_get_tag_first(device); tag; tag = sd_device_get_tag_next(device)) {
1519 char *e;
1520
1521 if (!GREEDY_REALLOC(tags, tags_allocated, tags_len + strlen(tag) + 2))
1522 return -ENOMEM;
1523 e = stpcpy(stpcpy(tags + tags_len, tag), ":");
1524 tags_len = e - tags;
1525 }
1526
1527 r = device_add_property_internal(device, "TAGS", tags);
1528 if (r < 0)
1529 return r;
1530
1531 device->property_tags_outdated = false;
1532 }
1533
1534 return 0;
1535 }
1536
1537 _public_ const char *sd_device_get_property_first(sd_device *device, const char **_value) {
1538 const char *key;
1539 const char *value;
1540 int r;
1541
1542 assert_return(device, NULL);
1543
1544 r = device_properties_prepare(device);
1545 if (r < 0)
1546 return NULL;
1547
1548 device->properties_iterator_generation = device->properties_generation;
1549 device->properties_iterator = ITERATOR_FIRST;
1550
1551 ordered_hashmap_iterate(device->properties, &device->properties_iterator, (void**)&value, (const void**)&key);
1552
1553 if (_value)
1554 *_value = value;
1555 return key;
1556 }
1557
1558 _public_ const char *sd_device_get_property_next(sd_device *device, const char **_value) {
1559 const char *key;
1560 const char *value;
1561 int r;
1562
1563 assert_return(device, NULL);
1564
1565 r = device_properties_prepare(device);
1566 if (r < 0)
1567 return NULL;
1568
1569 if (device->properties_iterator_generation != device->properties_generation)
1570 return NULL;
1571
1572 ordered_hashmap_iterate(device->properties, &device->properties_iterator, (void**)&value, (const void**)&key);
1573
1574 if (_value)
1575 *_value = value;
1576 return key;
1577 }
1578
1579 static int device_sysattrs_read_all(sd_device *device) {
1580 _cleanup_closedir_ DIR *dir = NULL;
1581 const char *syspath;
1582 struct dirent *dent;
1583 int r;
1584
1585 assert(device);
1586
1587 if (device->sysattrs_read)
1588 return 0;
1589
1590 r = sd_device_get_syspath(device, &syspath);
1591 if (r < 0)
1592 return r;
1593
1594 dir = opendir(syspath);
1595 if (!dir)
1596 return -errno;
1597
1598 r = set_ensure_allocated(&device->sysattrs, &string_hash_ops);
1599 if (r < 0)
1600 return r;
1601
1602 FOREACH_DIRENT_ALL(dent, dir, return -errno) {
1603 char *path;
1604 struct stat statbuf;
1605
1606 /* only handle symlinks and regular files */
1607 if (!IN_SET(dent->d_type, DT_LNK, DT_REG))
1608 continue;
1609
1610 path = strjoina(syspath, "/", dent->d_name);
1611
1612 if (lstat(path, &statbuf) != 0)
1613 continue;
1614
1615 if (!(statbuf.st_mode & S_IRUSR))
1616 continue;
1617
1618 r = set_put_strdup(device->sysattrs, dent->d_name);
1619 if (r < 0)
1620 return r;
1621 }
1622
1623 device->sysattrs_read = true;
1624
1625 return 0;
1626 }
1627
1628 _public_ const char *sd_device_get_sysattr_first(sd_device *device) {
1629 void *v;
1630 int r;
1631
1632 assert_return(device, NULL);
1633
1634 if (!device->sysattrs_read) {
1635 r = device_sysattrs_read_all(device);
1636 if (r < 0) {
1637 errno = -r;
1638 return NULL;
1639 }
1640 }
1641
1642 device->sysattrs_iterator = ITERATOR_FIRST;
1643
1644 (void) set_iterate(device->sysattrs, &device->sysattrs_iterator, &v);
1645 return v;
1646 }
1647
1648 _public_ const char *sd_device_get_sysattr_next(sd_device *device) {
1649 void *v;
1650
1651 assert_return(device, NULL);
1652
1653 if (!device->sysattrs_read)
1654 return NULL;
1655
1656 (void) set_iterate(device->sysattrs, &device->sysattrs_iterator, &v);
1657 return v;
1658 }
1659
1660 _public_ int sd_device_has_tag(sd_device *device, const char *tag) {
1661 assert_return(device, -EINVAL);
1662 assert_return(tag, -EINVAL);
1663
1664 (void) device_read_db(device);
1665
1666 return !!set_contains(device->tags, tag);
1667 }
1668
1669 _public_ int sd_device_get_property_value(sd_device *device, const char *key, const char **_value) {
1670 char *value;
1671 int r;
1672
1673 assert_return(device, -EINVAL);
1674 assert_return(key, -EINVAL);
1675
1676 r = device_properties_prepare(device);
1677 if (r < 0)
1678 return r;
1679
1680 value = ordered_hashmap_get(device->properties, key);
1681 if (!value)
1682 return -ENOENT;
1683
1684 if (_value)
1685 *_value = value;
1686 return 0;
1687 }
1688
1689 /* replaces the value if it already exists */
1690 static int device_add_sysattr_value(sd_device *device, const char *_key, char *value) {
1691 _cleanup_free_ char *key = NULL;
1692 _cleanup_free_ char *value_old = NULL;
1693 int r;
1694
1695 assert(device);
1696 assert(_key);
1697
1698 r = hashmap_ensure_allocated(&device->sysattr_values, &string_hash_ops);
1699 if (r < 0)
1700 return r;
1701
1702 value_old = hashmap_remove2(device->sysattr_values, _key, (void **)&key);
1703 if (!key) {
1704 key = strdup(_key);
1705 if (!key)
1706 return -ENOMEM;
1707 }
1708
1709 r = hashmap_put(device->sysattr_values, key, value);
1710 if (r < 0)
1711 return r;
1712 TAKE_PTR(key);
1713
1714 return 0;
1715 }
1716
1717 static int device_get_sysattr_value(sd_device *device, const char *_key, const char **_value) {
1718 const char *key = NULL, *value;
1719
1720 assert(device);
1721 assert(_key);
1722
1723 value = hashmap_get2(device->sysattr_values, _key, (void **) &key);
1724 if (!key)
1725 return -ENOENT;
1726
1727 if (_value)
1728 *_value = value;
1729 return 0;
1730 }
1731
1732 /* We cache all sysattr lookups. If an attribute does not exist, it is stored
1733 * with a NULL value in the cache, otherwise the returned string is stored */
1734 _public_ int sd_device_get_sysattr_value(sd_device *device, const char *sysattr, const char **_value) {
1735 _cleanup_free_ char *value = NULL;
1736 const char *syspath, *cached_value = NULL;
1737 char *path;
1738 struct stat statbuf;
1739 int r;
1740
1741 assert_return(device, -EINVAL);
1742 assert_return(sysattr, -EINVAL);
1743
1744 /* look for possibly already cached result */
1745 r = device_get_sysattr_value(device, sysattr, &cached_value);
1746 if (r != -ENOENT) {
1747 if (r < 0)
1748 return r;
1749
1750 if (!cached_value)
1751 /* we looked up the sysattr before and it did not exist */
1752 return -ENOENT;
1753
1754 if (_value)
1755 *_value = cached_value;
1756
1757 return 0;
1758 }
1759
1760 r = sd_device_get_syspath(device, &syspath);
1761 if (r < 0)
1762 return r;
1763
1764 path = strjoina(syspath, "/", sysattr);
1765 r = lstat(path, &statbuf);
1766 if (r < 0) {
1767 /* remember that we could not access the sysattr */
1768 r = device_add_sysattr_value(device, sysattr, NULL);
1769 if (r < 0)
1770 return r;
1771
1772 return -ENOENT;
1773 } else if (S_ISLNK(statbuf.st_mode)) {
1774 /* Some core links return only the last element of the target path,
1775 * these are just values, the paths should not be exposed. */
1776 if (STR_IN_SET(sysattr, "driver", "subsystem", "module")) {
1777 r = readlink_value(path, &value);
1778 if (r < 0)
1779 return r;
1780 } else
1781 return -EINVAL;
1782 } else if (S_ISDIR(statbuf.st_mode)) {
1783 /* skip directories */
1784 return -EINVAL;
1785 } else if (!(statbuf.st_mode & S_IRUSR)) {
1786 /* skip non-readable files */
1787 return -EPERM;
1788 } else {
1789 size_t size;
1790
1791 /* read attribute value */
1792 r = read_full_file(path, &value, &size);
1793 if (r < 0)
1794 return r;
1795
1796 /* drop trailing newlines */
1797 while (size > 0 && value[--size] == '\n')
1798 value[size] = '\0';
1799 }
1800
1801 r = device_add_sysattr_value(device, sysattr, value);
1802 if (r < 0)
1803 return r;
1804
1805 *_value = TAKE_PTR(value);
1806
1807 return 0;
1808 }
1809
1810 static void device_remove_sysattr_value(sd_device *device, const char *_key) {
1811 _cleanup_free_ char *key = NULL;
1812
1813 assert(device);
1814 assert(_key);
1815
1816 free(hashmap_remove2(device->sysattr_values, _key, (void **) &key));
1817 }
1818
1819 /* set the attribute and save it in the cache. If a NULL value is passed the
1820 * attribute is cleared from the cache */
1821 _public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr, const char *_value) {
1822 _cleanup_free_ char *value = NULL;
1823 const char *syspath, *path;
1824 size_t len;
1825 int r;
1826
1827 assert_return(device, -EINVAL);
1828 assert_return(sysattr, -EINVAL);
1829
1830 if (!_value) {
1831 device_remove_sysattr_value(device, sysattr);
1832 return 0;
1833 }
1834
1835 r = sd_device_get_syspath(device, &syspath);
1836 if (r < 0)
1837 return r;
1838
1839 path = strjoina(syspath, "/", sysattr);
1840
1841 len = strlen(_value);
1842
1843 /* drop trailing newlines */
1844 while (len > 0 && _value[len - 1] == '\n')
1845 len --;
1846
1847 /* value length is limited to 4k */
1848 if (len > 4096)
1849 return -EINVAL;
1850
1851 value = strndup(_value, len);
1852 if (!value)
1853 return -ENOMEM;
1854
1855 r = write_string_file(path, value, WRITE_STRING_FILE_DISABLE_BUFFER | WRITE_STRING_FILE_NOFOLLOW);
1856 if (r < 0) {
1857 if (r == -ELOOP)
1858 return -EINVAL;
1859 if (r == -EISDIR)
1860 return r;
1861
1862 r = free_and_strdup(&value, "");
1863 if (r < 0)
1864 return r;
1865
1866 r = device_add_sysattr_value(device, sysattr, value);
1867 if (r < 0)
1868 return r;
1869 TAKE_PTR(value);
1870
1871 return -ENXIO;
1872 }
1873
1874 r = device_add_sysattr_value(device, sysattr, value);
1875 if (r < 0)
1876 return r;
1877 TAKE_PTR(value);
1878
1879 return 0;
1880 }