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