]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-device/device-private.c
test: fix the default timeout values described in README.testsuite
[thirdparty/systemd.git] / src / libsystemd / sd-device / device-private.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <ctype.h>
4 #include <net/if.h>
5 #include <sys/types.h>
6
7 #include "sd-device.h"
8
9 #include "alloc-util.h"
10 #include "device-internal.h"
11 #include "device-private.h"
12 #include "device-util.h"
13 #include "fd-util.h"
14 #include "fileio.h"
15 #include "fs-util.h"
16 #include "hashmap.h"
17 #include "macro.h"
18 #include "mkdir.h"
19 #include "nulstr-util.h"
20 #include "parse-util.h"
21 #include "path-util.h"
22 #include "set.h"
23 #include "stdio-util.h"
24 #include "string-table.h"
25 #include "string-util.h"
26 #include "strv.h"
27 #include "strxcpyx.h"
28 #include "tmpfile-util.h"
29 #include "user-util.h"
30
31 int device_add_property(sd_device *device, const char *key, const char *value) {
32 int r;
33
34 assert(device);
35 assert(key);
36
37 r = device_add_property_aux(device, key, value, false);
38 if (r < 0)
39 return r;
40
41 if (key[0] != '.') {
42 r = device_add_property_aux(device, key, value, true);
43 if (r < 0)
44 return r;
45 }
46
47 return 0;
48 }
49
50 int device_add_propertyf(sd_device *device, const char *key, const char *format, ...) {
51 _cleanup_free_ char *value = NULL;
52 va_list ap;
53 int r;
54
55 assert(device);
56 assert(key);
57
58 if (!format)
59 return device_add_property(device, key, NULL);
60
61 va_start(ap, format);
62 r = vasprintf(&value, format, ap);
63 va_end(ap);
64
65 if (r < 0)
66 return -ENOMEM;
67
68 return device_add_property(device, key, value);
69 }
70
71 void device_set_devlink_priority(sd_device *device, int priority) {
72 assert(device);
73
74 device->devlink_priority = priority;
75 }
76
77 void device_set_is_initialized(sd_device *device) {
78 assert(device);
79
80 device->is_initialized = true;
81 }
82
83 int device_ensure_usec_initialized(sd_device *device, sd_device *device_old) {
84 usec_t when;
85
86 assert(device);
87
88 if (device_old && device_old->usec_initialized > 0)
89 when = device_old->usec_initialized;
90 else
91 when = now(CLOCK_MONOTONIC);
92
93 return device_set_usec_initialized(device, when);
94 }
95
96 uint64_t device_get_properties_generation(sd_device *device) {
97 assert(device);
98
99 return device->properties_generation;
100 }
101
102 uint64_t device_get_tags_generation(sd_device *device) {
103 assert(device);
104
105 return device->tags_generation;
106 }
107
108 uint64_t device_get_devlinks_generation(sd_device *device) {
109 assert(device);
110
111 return device->devlinks_generation;
112 }
113
114 int device_get_devnode_mode(sd_device *device, mode_t *ret) {
115 int r;
116
117 assert(device);
118
119 r = device_read_db(device);
120 if (r < 0)
121 return r;
122
123 if (device->devmode == MODE_INVALID)
124 return -ENOENT;
125
126 if (ret)
127 *ret = device->devmode;
128
129 return 0;
130 }
131
132 int device_get_devnode_uid(sd_device *device, uid_t *ret) {
133 int r;
134
135 assert(device);
136
137 r = device_read_db(device);
138 if (r < 0)
139 return r;
140
141 if (device->devuid == UID_INVALID)
142 return -ENOENT;
143
144 if (ret)
145 *ret = device->devuid;
146
147 return 0;
148 }
149
150 static int device_set_devuid(sd_device *device, const char *uid) {
151 uid_t u;
152 int r;
153
154 assert(device);
155 assert(uid);
156
157 r = parse_uid(uid, &u);
158 if (r < 0)
159 return r;
160
161 r = device_add_property_internal(device, "DEVUID", uid);
162 if (r < 0)
163 return r;
164
165 device->devuid = u;
166
167 return 0;
168 }
169
170 int device_get_devnode_gid(sd_device *device, gid_t *ret) {
171 int r;
172
173 assert(device);
174
175 r = device_read_db(device);
176 if (r < 0)
177 return r;
178
179 if (device->devgid == GID_INVALID)
180 return -ENOENT;
181
182 if (ret)
183 *ret = device->devgid;
184
185 return 0;
186 }
187
188 static int device_set_devgid(sd_device *device, const char *gid) {
189 gid_t g;
190 int r;
191
192 assert(device);
193 assert(gid);
194
195 r = parse_gid(gid, &g);
196 if (r < 0)
197 return r;
198
199 r = device_add_property_internal(device, "DEVGID", gid);
200 if (r < 0)
201 return r;
202
203 device->devgid = g;
204
205 return 0;
206 }
207
208 int device_set_action(sd_device *device, sd_device_action_t a) {
209 int r;
210
211 assert(device);
212 assert(a >= 0 && a < _SD_DEVICE_ACTION_MAX);
213
214 r = device_add_property_internal(device, "ACTION", device_action_to_string(a));
215 if (r < 0)
216 return r;
217
218 device->action = a;
219
220 return 0;
221 }
222
223 static int device_set_action_from_string(sd_device *device, const char *action) {
224 sd_device_action_t a;
225
226 assert(device);
227 assert(action);
228
229 a = device_action_from_string(action);
230 if (a < 0)
231 return a;
232
233 return device_set_action(device, a);
234 }
235
236 static int device_set_seqnum(sd_device *device, const char *str) {
237 uint64_t seqnum;
238 int r;
239
240 assert(device);
241 assert(str);
242
243 r = safe_atou64(str, &seqnum);
244 if (r < 0)
245 return r;
246 if (seqnum == 0)
247 return -EINVAL;
248
249 r = device_add_property_internal(device, "SEQNUM", str);
250 if (r < 0)
251 return r;
252
253 device->seqnum = seqnum;
254
255 return 0;
256 }
257
258 static int device_amend(sd_device *device, const char *key, const char *value) {
259 int r;
260
261 assert(device);
262 assert(key);
263 assert(value);
264
265 if (streq(key, "DEVPATH")) {
266 char *path;
267
268 path = strjoina("/sys", value);
269
270 /* the caller must verify or trust this data (e.g., if it comes from the kernel) */
271 r = device_set_syspath(device, path, false);
272 if (r < 0)
273 return log_device_debug_errno(device, r, "sd-device: Failed to set syspath to '%s': %m", path);
274 } else if (streq(key, "SUBSYSTEM")) {
275 r = device_set_subsystem(device, value);
276 if (r < 0)
277 return log_device_debug_errno(device, r, "sd-device: Failed to set subsystem to '%s': %m", value);
278 } else if (streq(key, "DEVTYPE")) {
279 r = device_set_devtype(device, value);
280 if (r < 0)
281 return log_device_debug_errno(device, r, "sd-device: Failed to set devtype to '%s': %m", value);
282 } else if (streq(key, "DEVNAME")) {
283 r = device_set_devname(device, value);
284 if (r < 0)
285 return log_device_debug_errno(device, r, "sd-device: Failed to set devname to '%s': %m", value);
286 } else if (streq(key, "USEC_INITIALIZED")) {
287 usec_t t;
288
289 r = safe_atou64(value, &t);
290 if (r < 0)
291 return log_device_debug_errno(device, r, "sd-device: Failed to parse timestamp '%s': %m", value);
292
293 r = device_set_usec_initialized(device, t);
294 if (r < 0)
295 return log_device_debug_errno(device, r, "sd-device: Failed to set usec-initialized to '%s': %m", value);
296 } else if (streq(key, "DRIVER")) {
297 r = device_set_driver(device, value);
298 if (r < 0)
299 return log_device_debug_errno(device, r, "sd-device: Failed to set driver to '%s': %m", value);
300 } else if (streq(key, "IFINDEX")) {
301 r = device_set_ifindex(device, value);
302 if (r < 0)
303 return log_device_debug_errno(device, r, "sd-device: Failed to set ifindex to '%s': %m", value);
304 } else if (streq(key, "DEVMODE")) {
305 r = device_set_devmode(device, value);
306 if (r < 0)
307 return log_device_debug_errno(device, r, "sd-device: Failed to set devmode to '%s': %m", value);
308 } else if (streq(key, "DEVUID")) {
309 r = device_set_devuid(device, value);
310 if (r < 0)
311 return log_device_debug_errno(device, r, "sd-device: Failed to set devuid to '%s': %m", value);
312 } else if (streq(key, "DEVGID")) {
313 r = device_set_devgid(device, value);
314 if (r < 0)
315 return log_device_debug_errno(device, r, "sd-device: Failed to set devgid to '%s': %m", value);
316 } else if (streq(key, "ACTION")) {
317 r = device_set_action_from_string(device, value);
318 if (r < 0)
319 return log_device_debug_errno(device, r, "sd-device: Failed to set action to '%s': %m", value);
320 } else if (streq(key, "SEQNUM")) {
321 r = device_set_seqnum(device, value);
322 if (r < 0)
323 return log_device_debug_errno(device, r, "sd-device: Failed to set SEQNUM to '%s': %m", value);
324 } else if (streq(key, "DISKSEQ")) {
325 r = device_set_diskseq(device, value);
326 if (r < 0)
327 return log_device_debug_errno(device, r, "sd-device: Failed to set DISKSEQ to '%s': %m", value);
328 } else if (streq(key, "DEVLINKS")) {
329 for (const char *p = value;;) {
330 _cleanup_free_ char *word = NULL;
331
332 /* udev rules may set escaped strings, and sd-device does not modify the input
333 * strings. So, it is also necessary to keep the strings received through
334 * sd-device-monitor. */
335 r = extract_first_word(&p, &word, NULL, EXTRACT_RETAIN_ESCAPE);
336 if (r < 0)
337 return r;
338 if (r == 0)
339 break;
340
341 r = device_add_devlink(device, word);
342 if (r < 0)
343 return log_device_debug_errno(device, r, "sd-device: Failed to add devlink '%s': %m", word);
344 }
345 } else if (STR_IN_SET(key, "TAGS", "CURRENT_TAGS")) {
346 for (const char *p = value;;) {
347 _cleanup_free_ char *word = NULL;
348
349 r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
350 if (r < 0)
351 return r;
352 if (r == 0)
353 break;
354
355 r = device_add_tag(device, word, streq(key, "CURRENT_TAGS"));
356 if (r < 0)
357 return log_device_debug_errno(device, r, "sd-device: Failed to add tag '%s': %m", word);
358 }
359 } else if (streq(key, "UDEV_DATABASE_VERSION")) {
360 r = safe_atou(value, &device->database_version);
361 if (r < 0)
362 return log_device_debug_errno(device, r, "sd-device: Failed to parse udev database version '%s': %m", value);
363 } else {
364 r = device_add_property_internal(device, key, value);
365 if (r < 0)
366 return log_device_debug_errno(device, r, "sd-device: Failed to add property '%s=%s': %m", key, value);
367 }
368
369 return 0;
370 }
371
372 static int device_append(
373 sd_device *device,
374 char *key,
375 const char **_major,
376 const char **_minor) {
377
378 const char *major = NULL, *minor = NULL;
379 char *value;
380 int r;
381
382 assert(device);
383 assert(key);
384 assert(_major);
385 assert(_minor);
386
387 value = strchr(key, '=');
388 if (!value)
389 return log_device_debug_errno(device, SYNTHETIC_ERRNO(EINVAL),
390 "sd-device: Not a key-value pair: '%s'", key);
391
392 *value = '\0';
393
394 value++;
395
396 if (streq(key, "MAJOR"))
397 major = value;
398 else if (streq(key, "MINOR"))
399 minor = value;
400 else {
401 r = device_amend(device, key, value);
402 if (r < 0)
403 return r;
404 }
405
406 if (major)
407 *_major = major;
408
409 if (minor)
410 *_minor = minor;
411
412 return 0;
413 }
414
415 void device_seal(sd_device *device) {
416 assert(device);
417
418 device->sealed = true;
419 }
420
421 static int device_verify(sd_device *device) {
422 int r;
423
424 assert(device);
425
426 if (!device->devpath || !device->subsystem || device->action < 0 || device->seqnum == 0)
427 return log_device_debug_errno(device, SYNTHETIC_ERRNO(EINVAL),
428 "sd-device: Device created from strv or nulstr lacks devpath, subsystem, action or seqnum.");
429
430 if (streq(device->subsystem, "drivers")) {
431 r = device_set_drivers_subsystem(device);
432 if (r < 0)
433 return r;
434 }
435
436 device->sealed = true;
437
438 return 0;
439 }
440
441 int device_new_from_strv(sd_device **ret, char **strv) {
442 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
443 const char *major = NULL, *minor = NULL;
444 int r;
445
446 assert(ret);
447 assert(strv);
448
449 r = device_new_aux(&device);
450 if (r < 0)
451 return r;
452
453 STRV_FOREACH(key, strv) {
454 r = device_append(device, *key, &major, &minor);
455 if (r < 0)
456 return r;
457 }
458
459 if (major) {
460 r = device_set_devnum(device, major, minor);
461 if (r < 0)
462 return log_device_debug_errno(device, r, "sd-device: Failed to set devnum %s:%s: %m", major, minor);
463 }
464
465 r = device_verify(device);
466 if (r < 0)
467 return r;
468
469 *ret = TAKE_PTR(device);
470
471 return 0;
472 }
473
474 int device_new_from_nulstr(sd_device **ret, char *nulstr, size_t len) {
475 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
476 const char *major = NULL, *minor = NULL;
477 int r;
478
479 assert(ret);
480 assert(nulstr);
481 assert(len);
482
483 r = device_new_aux(&device);
484 if (r < 0)
485 return r;
486
487 for (size_t i = 0; i < len; ) {
488 char *key;
489 const char *end;
490
491 key = nulstr + i;
492 end = memchr(key, '\0', len - i);
493 if (!end)
494 return log_device_debug_errno(device, SYNTHETIC_ERRNO(EINVAL),
495 "sd-device: Failed to parse nulstr");
496
497 i += end - key + 1;
498
499 /* netlink messages for some devices contain an unwanted newline at the end of value.
500 * Let's drop the newline and remaining characters after the newline. */
501 truncate_nl(key);
502
503 r = device_append(device, key, &major, &minor);
504 if (r < 0)
505 return r;
506 }
507
508 if (major) {
509 r = device_set_devnum(device, major, minor);
510 if (r < 0)
511 return log_device_debug_errno(device, r, "sd-device: Failed to set devnum %s:%s: %m", major, minor);
512 }
513
514 r = device_verify(device);
515 if (r < 0)
516 return r;
517
518 *ret = TAKE_PTR(device);
519
520 return 0;
521 }
522
523 static int device_update_properties_bufs(sd_device *device) {
524 _cleanup_free_ char **buf_strv = NULL, *buf_nulstr = NULL;
525 size_t nulstr_len = 0, num = 0;
526 const char *val, *prop;
527
528 assert(device);
529
530 if (!device->properties_buf_outdated)
531 return 0;
532
533 /* append udev database version */
534 buf_nulstr = newdup(char, "UDEV_DATABASE_VERSION=" STRINGIFY(LATEST_UDEV_DATABASE_VERSION) "\0",
535 STRLEN("UDEV_DATABASE_VERSION=" STRINGIFY(LATEST_UDEV_DATABASE_VERSION)) + 2);
536 if (!buf_nulstr)
537 return -ENOMEM;
538
539 nulstr_len += STRLEN("UDEV_DATABASE_VERSION=" STRINGIFY(LATEST_UDEV_DATABASE_VERSION)) + 1;
540 num++;
541
542 FOREACH_DEVICE_PROPERTY(device, prop, val) {
543 size_t len = 0;
544
545 len = strlen(prop) + 1 + strlen(val);
546
547 buf_nulstr = GREEDY_REALLOC0(buf_nulstr, nulstr_len + len + 2);
548 if (!buf_nulstr)
549 return -ENOMEM;
550
551 strscpyl(buf_nulstr + nulstr_len, len + 1, prop, "=", val, NULL);
552 nulstr_len += len + 1;
553 num++;
554 }
555
556 /* build buf_strv from buf_nulstr */
557 buf_strv = new0(char*, num + 1);
558 if (!buf_strv)
559 return -ENOMEM;
560
561 size_t i = 0;
562 char *p;
563 NULSTR_FOREACH(p, buf_nulstr)
564 buf_strv[i++] = p;
565 assert(i == num);
566
567 free_and_replace(device->properties_nulstr, buf_nulstr);
568 device->properties_nulstr_len = nulstr_len;
569 free_and_replace(device->properties_strv, buf_strv);
570
571 device->properties_buf_outdated = false;
572 return 0;
573 }
574
575 int device_get_properties_nulstr(sd_device *device, const char **ret_nulstr, size_t *ret_len) {
576 int r;
577
578 assert(device);
579
580 r = device_update_properties_bufs(device);
581 if (r < 0)
582 return r;
583
584 if (ret_nulstr)
585 *ret_nulstr = device->properties_nulstr;
586 if (ret_len)
587 *ret_len = device->properties_nulstr_len;
588
589 return 0;
590 }
591
592 int device_get_properties_strv(sd_device *device, char ***ret) {
593 int r;
594
595 assert(device);
596
597 r = device_update_properties_bufs(device);
598 if (r < 0)
599 return r;
600
601 if (ret)
602 *ret = device->properties_strv;
603
604 return 0;
605 }
606
607 int device_get_devlink_priority(sd_device *device, int *ret) {
608 int r;
609
610 assert(device);
611
612 r = device_read_db(device);
613 if (r < 0)
614 return r;
615
616 if (ret)
617 *ret = device->devlink_priority;
618
619 return 0;
620 }
621
622 int device_rename(sd_device *device, const char *name) {
623 _cleanup_free_ char *new_syspath = NULL;
624 const char *interface;
625 int r;
626
627 assert(device);
628 assert(name);
629
630 if (!filename_is_valid(name))
631 return -EINVAL;
632
633 r = path_extract_directory(device->syspath, &new_syspath);
634 if (r < 0)
635 return r;
636
637 if (!path_extend(&new_syspath, name))
638 return -ENOMEM;
639
640 if (!path_is_safe(new_syspath))
641 return -EINVAL;
642
643 /* At the time this is called, the renamed device may not exist yet. Hence, we cannot validate
644 * the new syspath. */
645 r = device_set_syspath(device, new_syspath, false);
646 if (r < 0)
647 return r;
648
649 /* Here, only clear the sysname and sysnum. They will be set when requested. */
650 device->sysnum = NULL;
651 device->sysname = mfree(device->sysname);
652
653 r = sd_device_get_property_value(device, "INTERFACE", &interface);
654 if (r == -ENOENT)
655 return 0;
656 if (r < 0)
657 return r;
658
659 /* like DEVPATH_OLD, INTERFACE_OLD is not saved to the db, but only stays around for the current event */
660 r = device_add_property_internal(device, "INTERFACE_OLD", interface);
661 if (r < 0)
662 return r;
663
664 return device_add_property_internal(device, "INTERFACE", name);
665 }
666
667 static int device_shallow_clone(sd_device *device, sd_device **ret) {
668 _cleanup_(sd_device_unrefp) sd_device *dest = NULL;
669 const char *val = NULL;
670 int r;
671
672 assert(device);
673 assert(ret);
674
675 r = device_new_aux(&dest);
676 if (r < 0)
677 return r;
678
679 r = device_set_syspath(dest, device->syspath, false);
680 if (r < 0)
681 return r;
682
683 (void) sd_device_get_subsystem(device, &val);
684 r = device_set_subsystem(dest, val);
685 if (r < 0)
686 return r;
687 if (streq_ptr(val, "drivers")) {
688 r = free_and_strdup(&dest->driver_subsystem, device->driver_subsystem);
689 if (r < 0)
690 return r;
691 }
692
693 /* The device may be already removed. Let's copy minimal set of information to make
694 * device_get_device_id() work without uevent file. */
695
696 if (sd_device_get_property_value(device, "IFINDEX", &val) >= 0) {
697 r = device_set_ifindex(dest, val);
698 if (r < 0)
699 return r;
700 }
701
702 if (sd_device_get_property_value(device, "MAJOR", &val) >= 0) {
703 const char *minor = NULL;
704
705 (void) sd_device_get_property_value(device, "MINOR", &minor);
706 r = device_set_devnum(dest, val, minor);
707 if (r < 0)
708 return r;
709 }
710
711 r = device_read_uevent_file(dest);
712 if (r < 0)
713 return r;
714
715 *ret = TAKE_PTR(dest);
716 return 0;
717 }
718
719 int device_clone_with_db(sd_device *device, sd_device **ret) {
720 _cleanup_(sd_device_unrefp) sd_device *dest = NULL;
721 int r;
722
723 assert(device);
724 assert(ret);
725
726 r = device_shallow_clone(device, &dest);
727 if (r < 0)
728 return r;
729
730 r = device_read_db(dest);
731 if (r < 0)
732 return r;
733
734 dest->sealed = true;
735
736 *ret = TAKE_PTR(dest);
737 return 0;
738 }
739
740 void device_cleanup_tags(sd_device *device) {
741 assert(device);
742
743 device->all_tags = set_free_free(device->all_tags);
744 device->current_tags = set_free_free(device->current_tags);
745 device->property_tags_outdated = true;
746 device->tags_generation++;
747 }
748
749 void device_cleanup_devlinks(sd_device *device) {
750 assert(device);
751
752 set_free_free(device->devlinks);
753 device->devlinks = NULL;
754 device->property_devlinks_outdated = true;
755 device->devlinks_generation++;
756 }
757
758 void device_remove_tag(sd_device *device, const char *tag) {
759 assert(device);
760 assert(tag);
761
762 free(set_remove(device->current_tags, tag));
763 device->property_tags_outdated = true;
764 device->tags_generation++;
765 }
766
767 static int device_tag(sd_device *device, const char *tag, bool add) {
768 const char *id;
769 char *path;
770 int r;
771
772 assert(device);
773 assert(tag);
774
775 r = device_get_device_id(device, &id);
776 if (r < 0)
777 return r;
778
779 path = strjoina("/run/udev/tags/", tag, "/", id);
780
781 if (add)
782 return touch_file(path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, 0444);
783
784 if (unlink(path) < 0 && errno != ENOENT)
785 return -errno;
786
787 return 0;
788 }
789
790 int device_tag_index(sd_device *device, sd_device *device_old, bool add) {
791 const char *tag;
792 int r = 0, k;
793
794 if (add && device_old)
795 /* delete possible left-over tags */
796 FOREACH_DEVICE_TAG(device_old, tag)
797 if (!sd_device_has_tag(device, tag)) {
798 k = device_tag(device_old, tag, false);
799 if (r >= 0 && k < 0)
800 r = k;
801 }
802
803 FOREACH_DEVICE_TAG(device, tag) {
804 k = device_tag(device, tag, add);
805 if (r >= 0 && k < 0)
806 r = k;
807 }
808
809 return r;
810 }
811
812 static bool device_has_info(sd_device *device) {
813 assert(device);
814
815 if (!set_isempty(device->devlinks))
816 return true;
817
818 if (device->devlink_priority != 0)
819 return true;
820
821 if (!ordered_hashmap_isempty(device->properties_db))
822 return true;
823
824 if (!set_isempty(device->all_tags))
825 return true;
826
827 if (!set_isempty(device->current_tags))
828 return true;
829
830 return false;
831 }
832
833 void device_set_db_persist(sd_device *device) {
834 assert(device);
835
836 device->db_persist = true;
837 }
838
839 int device_update_db(sd_device *device) {
840 const char *id;
841 char *path;
842 _cleanup_fclose_ FILE *f = NULL;
843 _cleanup_free_ char *path_tmp = NULL;
844 bool has_info;
845 int r;
846
847 assert(device);
848
849 has_info = device_has_info(device);
850
851 r = device_get_device_id(device, &id);
852 if (r < 0)
853 return r;
854
855 path = strjoina("/run/udev/data/", id);
856
857 /* do not store anything for otherwise empty devices */
858 if (!has_info && major(device->devnum) == 0 && device->ifindex == 0) {
859 if (unlink(path) < 0 && errno != ENOENT)
860 return -errno;
861
862 return 0;
863 }
864
865 /* write a database file */
866 r = mkdir_parents(path, 0755);
867 if (r < 0)
868 return r;
869
870 r = fopen_temporary(path, &f, &path_tmp);
871 if (r < 0)
872 return r;
873
874 /* set 'sticky' bit to indicate that we should not clean the database when we transition from initrd
875 * to the real root */
876 if (fchmod(fileno(f), device->db_persist ? 01644 : 0644) < 0) {
877 r = -errno;
878 goto fail;
879 }
880
881 if (has_info) {
882 const char *property, *value, *tag;
883
884 if (major(device->devnum) > 0) {
885 const char *devlink;
886
887 FOREACH_DEVICE_DEVLINK(device, devlink)
888 fprintf(f, "S:%s\n", devlink + STRLEN("/dev/"));
889
890 if (device->devlink_priority != 0)
891 fprintf(f, "L:%i\n", device->devlink_priority);
892 }
893
894 if (device->usec_initialized > 0)
895 fprintf(f, "I:"USEC_FMT"\n", device->usec_initialized);
896
897 ORDERED_HASHMAP_FOREACH_KEY(value, property, device->properties_db)
898 fprintf(f, "E:%s=%s\n", property, value);
899
900 FOREACH_DEVICE_TAG(device, tag)
901 fprintf(f, "G:%s\n", tag); /* Any tag */
902
903 SET_FOREACH(tag, device->current_tags)
904 fprintf(f, "Q:%s\n", tag); /* Current tag */
905
906 /* Always write the latest database version here, instead of the value stored in
907 * device->database_version, as which may be 0. */
908 fputs("V:" STRINGIFY(LATEST_UDEV_DATABASE_VERSION) "\n", f);
909 }
910
911 r = fflush_and_check(f);
912 if (r < 0)
913 goto fail;
914
915 if (rename(path_tmp, path) < 0) {
916 r = -errno;
917 goto fail;
918 }
919
920 log_device_debug(device, "sd-device: Created %s file '%s' for '%s'", has_info ? "db" : "empty",
921 path, device->devpath);
922
923 return 0;
924
925 fail:
926 (void) unlink(path);
927 (void) unlink(path_tmp);
928
929 return log_device_debug_errno(device, r, "sd-device: Failed to create %s file '%s' for '%s'", has_info ? "db" : "empty", path, device->devpath);
930 }
931
932 int device_delete_db(sd_device *device) {
933 const char *id;
934 char *path;
935 int r;
936
937 assert(device);
938
939 r = device_get_device_id(device, &id);
940 if (r < 0)
941 return r;
942
943 path = strjoina("/run/udev/data/", id);
944
945 if (unlink(path) < 0 && errno != ENOENT)
946 return -errno;
947
948 return 0;
949 }
950
951 static const char* const device_action_table[_SD_DEVICE_ACTION_MAX] = {
952 [SD_DEVICE_ADD] = "add",
953 [SD_DEVICE_REMOVE] = "remove",
954 [SD_DEVICE_CHANGE] = "change",
955 [SD_DEVICE_MOVE] = "move",
956 [SD_DEVICE_ONLINE] = "online",
957 [SD_DEVICE_OFFLINE] = "offline",
958 [SD_DEVICE_BIND] = "bind",
959 [SD_DEVICE_UNBIND] = "unbind",
960 };
961
962 DEFINE_STRING_TABLE_LOOKUP(device_action, sd_device_action_t);
963
964 void dump_device_action_table(void) {
965 DUMP_STRING_TABLE(device_action, sd_device_action_t, _SD_DEVICE_ACTION_MAX);
966 }