]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-device/device-private.c
update TODO
[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 NULSTR_FOREACH(p, buf_nulstr)
563 buf_strv[i++] = p;
564 assert(i == num);
565
566 free_and_replace(device->properties_nulstr, buf_nulstr);
567 device->properties_nulstr_len = nulstr_len;
568 free_and_replace(device->properties_strv, buf_strv);
569
570 device->properties_buf_outdated = false;
571 return 0;
572 }
573
574 int device_get_properties_nulstr(sd_device *device, const char **ret_nulstr, size_t *ret_len) {
575 int r;
576
577 assert(device);
578
579 r = device_update_properties_bufs(device);
580 if (r < 0)
581 return r;
582
583 if (ret_nulstr)
584 *ret_nulstr = device->properties_nulstr;
585 if (ret_len)
586 *ret_len = device->properties_nulstr_len;
587
588 return 0;
589 }
590
591 int device_get_properties_strv(sd_device *device, char ***ret) {
592 int r;
593
594 assert(device);
595
596 r = device_update_properties_bufs(device);
597 if (r < 0)
598 return r;
599
600 if (ret)
601 *ret = device->properties_strv;
602
603 return 0;
604 }
605
606 int device_get_devlink_priority(sd_device *device, int *ret) {
607 int r;
608
609 assert(device);
610
611 r = device_read_db(device);
612 if (r < 0)
613 return r;
614
615 if (ret)
616 *ret = device->devlink_priority;
617
618 return 0;
619 }
620
621 static int device_shallow_clone(sd_device *device, sd_device **ret) {
622 _cleanup_(sd_device_unrefp) sd_device *dest = NULL;
623 const char *val = NULL;
624 int r;
625
626 assert(device);
627 assert(ret);
628
629 r = device_new_aux(&dest);
630 if (r < 0)
631 return r;
632
633 r = device_set_syspath(dest, device->syspath, false);
634 if (r < 0)
635 return r;
636
637 (void) sd_device_get_subsystem(device, &val);
638 r = device_set_subsystem(dest, val);
639 if (r < 0)
640 return r;
641 if (streq_ptr(val, "drivers")) {
642 r = free_and_strdup(&dest->driver_subsystem, device->driver_subsystem);
643 if (r < 0)
644 return r;
645 }
646
647 /* The device may be already removed. Let's copy minimal set of information to make
648 * device_get_device_id() work without uevent file. */
649
650 if (sd_device_get_property_value(device, "IFINDEX", &val) >= 0) {
651 r = device_set_ifindex(dest, val);
652 if (r < 0)
653 return r;
654 }
655
656 if (sd_device_get_property_value(device, "MAJOR", &val) >= 0) {
657 const char *minor = NULL;
658
659 (void) sd_device_get_property_value(device, "MINOR", &minor);
660 r = device_set_devnum(dest, val, minor);
661 if (r < 0)
662 return r;
663 }
664
665 r = device_read_uevent_file(dest);
666 if (r < 0)
667 return r;
668
669 *ret = TAKE_PTR(dest);
670 return 0;
671 }
672
673 int device_clone_with_db(sd_device *device, sd_device **ret) {
674 _cleanup_(sd_device_unrefp) sd_device *dest = NULL;
675 int r;
676
677 assert(device);
678 assert(ret);
679
680 r = device_shallow_clone(device, &dest);
681 if (r < 0)
682 return r;
683
684 r = device_read_db(dest);
685 if (r < 0)
686 return r;
687
688 dest->sealed = true;
689
690 *ret = TAKE_PTR(dest);
691 return 0;
692 }
693
694 void device_cleanup_tags(sd_device *device) {
695 assert(device);
696
697 device->all_tags = set_free_free(device->all_tags);
698 device->current_tags = set_free_free(device->current_tags);
699 device->property_tags_outdated = true;
700 device->tags_generation++;
701 }
702
703 void device_cleanup_devlinks(sd_device *device) {
704 assert(device);
705
706 set_free_free(device->devlinks);
707 device->devlinks = NULL;
708 device->property_devlinks_outdated = true;
709 device->devlinks_generation++;
710 }
711
712 void device_remove_tag(sd_device *device, const char *tag) {
713 assert(device);
714 assert(tag);
715
716 free(set_remove(device->current_tags, tag));
717 device->property_tags_outdated = true;
718 device->tags_generation++;
719 }
720
721 static int device_tag(sd_device *device, const char *tag, bool add) {
722 const char *id;
723 char *path;
724 int r;
725
726 assert(device);
727 assert(tag);
728
729 r = device_get_device_id(device, &id);
730 if (r < 0)
731 return r;
732
733 path = strjoina("/run/udev/tags/", tag, "/", id);
734
735 if (add)
736 return touch_file(path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, 0444);
737
738 if (unlink(path) < 0 && errno != ENOENT)
739 return -errno;
740
741 return 0;
742 }
743
744 int device_tag_index(sd_device *device, sd_device *device_old, bool add) {
745 const char *tag;
746 int r = 0, k;
747
748 if (add && device_old)
749 /* delete possible left-over tags */
750 FOREACH_DEVICE_TAG(device_old, tag)
751 if (!sd_device_has_tag(device, tag)) {
752 k = device_tag(device_old, tag, false);
753 if (r >= 0 && k < 0)
754 r = k;
755 }
756
757 FOREACH_DEVICE_TAG(device, tag) {
758 k = device_tag(device, tag, add);
759 if (r >= 0 && k < 0)
760 r = k;
761 }
762
763 return r;
764 }
765
766 static bool device_has_info(sd_device *device) {
767 assert(device);
768
769 if (!set_isempty(device->devlinks))
770 return true;
771
772 if (device->devlink_priority != 0)
773 return true;
774
775 if (!ordered_hashmap_isempty(device->properties_db))
776 return true;
777
778 if (!set_isempty(device->all_tags))
779 return true;
780
781 if (!set_isempty(device->current_tags))
782 return true;
783
784 return false;
785 }
786
787 void device_set_db_persist(sd_device *device) {
788 assert(device);
789
790 device->db_persist = true;
791 }
792
793 int device_update_db(sd_device *device) {
794 const char *id;
795 char *path;
796 _cleanup_fclose_ FILE *f = NULL;
797 _cleanup_(unlink_and_freep) char *path_tmp = NULL;
798 bool has_info;
799 int r;
800
801 assert(device);
802
803 has_info = device_has_info(device);
804
805 r = device_get_device_id(device, &id);
806 if (r < 0)
807 return r;
808
809 path = strjoina("/run/udev/data/", id);
810
811 /* do not store anything for otherwise empty devices */
812 if (!has_info && major(device->devnum) == 0 && device->ifindex == 0) {
813 if (unlink(path) < 0 && errno != ENOENT)
814 return -errno;
815
816 return 0;
817 }
818
819 /* write a database file */
820 r = mkdir_parents(path, 0755);
821 if (r < 0)
822 return r;
823
824 r = fopen_temporary(path, &f, &path_tmp);
825 if (r < 0)
826 return r;
827
828 /* set 'sticky' bit to indicate that we should not clean the database when we transition from initrd
829 * to the real root */
830 if (fchmod(fileno(f), device->db_persist ? 01644 : 0644) < 0) {
831 r = -errno;
832 goto fail;
833 }
834
835 if (has_info) {
836 const char *property, *value, *tag;
837
838 if (major(device->devnum) > 0) {
839 const char *devlink;
840
841 FOREACH_DEVICE_DEVLINK(device, devlink)
842 fprintf(f, "S:%s\n", devlink + STRLEN("/dev/"));
843
844 if (device->devlink_priority != 0)
845 fprintf(f, "L:%i\n", device->devlink_priority);
846 }
847
848 if (device->usec_initialized > 0)
849 fprintf(f, "I:"USEC_FMT"\n", device->usec_initialized);
850
851 ORDERED_HASHMAP_FOREACH_KEY(value, property, device->properties_db)
852 fprintf(f, "E:%s=%s\n", property, value);
853
854 FOREACH_DEVICE_TAG(device, tag)
855 fprintf(f, "G:%s\n", tag); /* Any tag */
856
857 SET_FOREACH(tag, device->current_tags)
858 fprintf(f, "Q:%s\n", tag); /* Current tag */
859
860 /* Always write the latest database version here, instead of the value stored in
861 * device->database_version, as which may be 0. */
862 fputs("V:" STRINGIFY(LATEST_UDEV_DATABASE_VERSION) "\n", f);
863 }
864
865 r = fflush_and_check(f);
866 if (r < 0)
867 goto fail;
868
869 if (rename(path_tmp, path) < 0) {
870 r = -errno;
871 goto fail;
872 }
873
874 path_tmp = mfree(path_tmp);
875
876 log_device_debug(device, "sd-device: Created %s file '%s' for '%s'", has_info ? "db" : "empty",
877 path, device->devpath);
878
879 return 0;
880
881 fail:
882 (void) unlink(path);
883
884 return log_device_debug_errno(device, r, "sd-device: Failed to create %s file '%s' for '%s'", has_info ? "db" : "empty", path, device->devpath);
885 }
886
887 int device_delete_db(sd_device *device) {
888 const char *id;
889 char *path;
890 int r;
891
892 assert(device);
893
894 r = device_get_device_id(device, &id);
895 if (r < 0)
896 return r;
897
898 path = strjoina("/run/udev/data/", id);
899
900 if (unlink(path) < 0 && errno != ENOENT)
901 return -errno;
902
903 return 0;
904 }
905
906 static const char* const device_action_table[_SD_DEVICE_ACTION_MAX] = {
907 [SD_DEVICE_ADD] = "add",
908 [SD_DEVICE_REMOVE] = "remove",
909 [SD_DEVICE_CHANGE] = "change",
910 [SD_DEVICE_MOVE] = "move",
911 [SD_DEVICE_ONLINE] = "online",
912 [SD_DEVICE_OFFLINE] = "offline",
913 [SD_DEVICE_BIND] = "bind",
914 [SD_DEVICE_UNBIND] = "unbind",
915 };
916
917 DEFINE_STRING_TABLE_LOOKUP(device_action, sd_device_action_t);
918
919 void dump_device_action_table(void) {
920 DUMP_STRING_TABLE(device_action, sd_device_action_t, _SD_DEVICE_ACTION_MAX);
921 }