-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.c
1052 lines (871 loc) · 37.3 KB
/
device.c
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
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <libudev.h>
#include <linux/fs.h>
#include <linux/usbdevice_fs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include "crc32.h"
#include "device.h"
#include "messages.h"
#include "mfst.h"
int is_block_device(char *filename) {
struct stat fs;
if(stat(filename, &fs)) {
return -1;
}
return S_ISBLK(fs.st_mode);
}
int did_device_disconnect(dev_t device_num) {
const char *syspath, *device_size_str;
size_t device_size;
struct stat sysstat;
struct udev *udev_handle;
struct udev_device *udev_dev;
// Sleep for a bit to give udev a chance to register the disconnect
usleep(250000);
udev_handle = udev_new();
if(!udev_handle) {
return 0;
}
udev_dev = udev_device_new_from_devnum(udev_handle, 'b', device_num);
if(!udev_dev) {
udev_unref(udev_handle);
return 1;
}
syspath = udev_device_get_syspath(udev_dev);
if(!syspath) {
udev_device_unref(udev_dev);
udev_unref(udev_handle);
return 1;
}
// Check to see if the path still exists
if(stat(syspath, &sysstat)) {
// Yeah, device went away...
udev_device_unref(udev_dev);
udev_unref(udev_handle);
return 1;
}
// Did the device's size change to 0? (E.g., did the SD card get pulled
// out of the reader?)
device_size_str = udev_device_get_sysattr_value(udev_dev, "size");
if(!device_size_str) {
udev_device_unref(udev_dev);
udev_unref(udev_handle);
return 1;
}
device_size = strtoull(device_size_str, NULL, 10);
if(!device_size) {
udev_device_unref(udev_dev);
udev_unref(udev_handle);
return 1;
}
// Nope, device is still there.
udev_device_unref(udev_dev);
udev_unref(udev_handle);
return 0;
}
/**
* Compares the two devices and determines whether they are identical.
*
* @returns 0 if the two devices are identical; 1 if they are not, if one or
* both of the specified devices do not exist, or if one or both of
* the specified devices aren't actually devices; or -1 if an error
* occurred.
*/
int are_devices_identical(const char *devname1, const char *devname2) {
struct stat devstat1, devstat2;
if(stat(devname1, &devstat1)) {
if(errno == ENOENT) {
// Non-existant devices shall be considered non-identical.
return 1;
} else {
return -1;
}
}
if(stat(devname2, &devstat2)) {
if(errno == ENOENT) {
return 1;
} else {
return -1;
}
}
if(!S_ISBLK(devstat1.st_mode) || !S_ISBLK(devstat2.st_mode) || !S_ISCHR(devstat1.st_mode) || !S_ISCHR(devstat2.st_mode)) {
return 1;
}
if((devstat1.st_mode & S_IFMT) != (devstat2.st_mode & S_IFMT)) {
// Devices aren't the same type
return 1;
}
if(devstat1.st_rdev != devstat2.st_rdev) {
return 1;
}
return 0;
}
/**
* Reads the beginning-of-device and middle-of-device data and compares it with
* what is stored in bod_buffer/mod_buffer.
*
* Note that this function will move the position of the file pointer
* represented by fd. Callers are responsible for saving the position of the
* file pointer and re-seeking to that position after this call completes.
*
* @param fd A handle to the device with the data to compare.
* @param device_size The physical size of the device, in bytes.
* @param bod_buffer A pointer to a buffer containing the data
* expected at the beginning of the device.
* @param mod_buffer A pointer to a buffer containing the data
* expected at the middle of the device.
* @param bod_mod_buffer_size The amount of data in bod_buffer and mod_buffer
* (each), in bytes.
* @returns 0 if either the BOD or MOD data is an exact match, 1 if the data
* did not match, or -1 if an error occurred.
*/
int compare_bod_mod_data(int fd, size_t device_size, char *bod_buffer, char *mod_buffer, size_t bod_mod_buffer_size) {
char *read_buffer;
size_t bytes_left_to_read, middle;
size_t matching_sectors = 0;
size_t partial_match_threshold;
int ret;
int sector_size;
uint64_t num_sectors_to_read;
if(!(read_buffer = malloc(bod_mod_buffer_size))) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_MALLOC_ERROR, strerror(errno));
return -1;
}
// Get the device's sector size.
if(ioctl(fd, BLKSSZGET, §or_size)) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_IOCTL_ERROR, strerror(errno));
free(read_buffer);
return -1;
}
partial_match_threshold = bod_mod_buffer_size / sector_size; // 50%
// Make sure we're at the beginning of the device
if(lseek(fd, 0, SEEK_SET) == -1) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_LSEEK_ERROR, strerror(errno));
free(read_buffer);
return -1;
}
// Read in the first 1MB.
bytes_left_to_read = bod_mod_buffer_size;
while(bytes_left_to_read) {
num_sectors_to_read = get_max_writable_sectors((bod_mod_buffer_size - bytes_left_to_read) / device_stats.sector_size, bytes_left_to_read / device_stats.sector_size);
if(num_sectors_to_read) {
if((ret = read(fd, read_buffer + (bod_mod_buffer_size - bytes_left_to_read), (num_sectors_to_read * device_stats.sector_size) + (bytes_left_to_read % device_stats.sector_size))) == -1) {
// If we get a read error here, we'll just zero out the rest of the sector and move on.
memset(read_buffer + (bod_mod_buffer_size - bytes_left_to_read), 0, ((bytes_left_to_read % device_stats.sector_size) == 0) ? device_stats.sector_size : (bytes_left_to_read % device_stats.sector_size));
bytes_left_to_read -= ((bytes_left_to_read % device_stats.sector_size) == 0) ? device_stats.sector_size : (bytes_left_to_read % device_stats.sector_size);
if(lseek(fd, bod_mod_buffer_size - bytes_left_to_read, SEEK_SET) == -1) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_LSEEK_ERROR, strerror(errno));
free(read_buffer);
return -1;
}
} else {
bytes_left_to_read -= ret;
}
}
// For unwritable sectors, just zero out the read buffer
num_sectors_to_read = get_max_unwritable_sectors((bod_mod_buffer_size - bytes_left_to_read) / device_stats.sector_size, bytes_left_to_read / device_stats.sector_size);
if(num_sectors_to_read) {
memset(read_buffer + (bod_mod_buffer_size - bytes_left_to_read), 0, num_sectors_to_read * device_stats.sector_size);
bytes_left_to_read -= num_sectors_to_read * device_stats.sector_size;
// Seek past the bad sectors
if(lseek(fd, bod_mod_buffer_size - bytes_left_to_read, SEEK_SET) == -1) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_LSEEK_ERROR, strerror(errno));
free(read_buffer);
return -1;
}
}
}
if(!memcmp(read_buffer, bod_buffer, bod_mod_buffer_size)) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_COMPARE_BOD_MOD_DATA_BOD_MATCHES);
free(read_buffer);
return 0;
} else {
// Do a sector-by-sector comparison and count up the sectors
for(bytes_left_to_read = 0; bytes_left_to_read < bod_mod_buffer_size; bytes_left_to_read += sector_size) {
if(!memcmp(read_buffer + bytes_left_to_read, bod_buffer + bytes_left_to_read, sector_size)) {
matching_sectors++;
}
}
}
// Read in the middle 1MB.
bytes_left_to_read = bod_mod_buffer_size;
middle = device_size / 2;
if(lseek(fd, middle, SEEK_SET) == -1) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_LSEEK_ERROR, strerror(errno));
free(read_buffer);
return -1;
}
while(bytes_left_to_read) {
num_sectors_to_read = get_max_writable_sectors((middle + (bod_mod_buffer_size - bytes_left_to_read)) / device_stats.sector_size, bytes_left_to_read / device_stats.sector_size);
if(num_sectors_to_read) {
if((ret = read(fd, read_buffer + (bod_mod_buffer_size - bytes_left_to_read), (num_sectors_to_read * device_stats.sector_size) + (bytes_left_to_read % device_stats.sector_size))) == -1) {
// If we get a read error here, we'll just zero out the rest of the sector and move on
memset(read_buffer + (bod_mod_buffer_size - bytes_left_to_read), 0, ((bytes_left_to_read % device_stats.sector_size) == 0) ? device_stats.sector_size : (bytes_left_to_read % device_stats.sector_size));
bytes_left_to_read -= ((bytes_left_to_read % device_stats.sector_size) == 0) ? device_stats.sector_size : (bytes_left_to_read % device_stats.sector_size);
if(lseek(fd, middle + (bod_mod_buffer_size - bytes_left_to_read), SEEK_SET) == -1) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_LSEEK_ERROR, strerror(errno));
free(read_buffer);
return -1;
}
} else {
bytes_left_to_read -= ret;
}
}
// For unwritable sectors, just zero out the read buffer
num_sectors_to_read = get_max_unwritable_sectors((middle + (bod_mod_buffer_size - bytes_left_to_read)) / device_stats.sector_size, bytes_left_to_read / device_stats.sector_size);
if(num_sectors_to_read) {
memset(read_buffer + (bod_mod_buffer_size - bytes_left_to_read), 0, num_sectors_to_read * device_stats.sector_size);
bytes_left_to_read -= num_sectors_to_read * device_stats.sector_size;
// Seek past the bad sectors
if(lseek(fd, middle + (bod_mod_buffer_size - bytes_left_to_read), SEEK_SET) == -1) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_LSEEK_ERROR, strerror(errno));
free(read_buffer);
return -1;
}
}
}
if(!memcmp(read_buffer, mod_buffer, bod_mod_buffer_size)) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_COMPARE_BOD_MOD_DATA_MOD_MATCHES);
free(read_buffer);
return 0;
} else {
// We're done with read_buffer now, we can go ahead and free() it
free(read_buffer);
// Do a sector-by-sector comparison and count up the sectors
for(bytes_left_to_read = 0; bytes_left_to_read < bod_mod_buffer_size; bytes_left_to_read += sector_size) {
if(!memcmp(read_buffer + bytes_left_to_read, mod_buffer + bytes_left_to_read, sector_size)) {
matching_sectors++;
}
}
if(matching_sectors >= partial_match_threshold) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_COMPARE_BOD_MOD_DATA_PARTIAL_MATCH);
return 0;
}
}
if(matching_sectors > 0) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_COMPARE_BOD_MOD_DATA_ONLY_X_SECTORS_MATCHED, matching_sectors, matching_sectors == 1 ? "" : "s");
} else {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_COMPARE_BOD_MOD_DATA_NO_SECTORS_MATCHED);
}
return 1;
}
/**
* Reads 4,096 random sectors from the device and extracts the UUID embedded in
* the sector data and compares it to the expected UUID (provided in
* expected_device_uuid). Returns a success if at least half the sectors read
* contained UUIDs that matched the expected UUID.
*
* @param fd A handle to the device to be queried.
* @param device_size The size of the device, in bytes.
* @param expected_device_uuid The expected device UUID.
* @param sector_data A pointer to a buffer containing the current
* sector map.
*
* @returns 0 if at least half of the UUIDs read match the expected UUID, 1 if
* less than half the UUIDs read did not match the expected UUID, or -1
* if an error occurred (including if the sector map does not contain
* at least 4,096 good sectors).
*/
int compare_device_uuids(int fd, uint64_t device_size, uuid_t expected_device_uuid, char *sector_map) {
int num_matching_sectors = 0, i, j, sector_size;
uint64_t num_sectors, num_bad_sectors = 0, k;
int found;
const uint64_t num_sectors_to_check = 4096;
uint64_t sectors_to_check[num_sectors_to_check];
uint64_t cur_sector;
long a, b, c;
char *buffer;
uuid_t device_uuid;
int ret;
// Get the device's sector size.
if(ioctl(fd, BLKSSZGET, §or_size)) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_IOCTL_ERROR, strerror(errno));
free(buffer);
return -1;
}
num_sectors = device_size / sector_size;
// Count up how many bad sectors there are on the device
for(k = 0; k < num_sectors; k++) {
if(sector_map[k] & 0x01) {
num_bad_sectors++;
}
}
// If there aren't enough good sectors to query, give up now
if(num_sectors < (num_bad_sectors + num_sectors_to_check)) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_COMPARE_DEVICE_UUIDS_NOT_ENOUGH_GOOD_SECTORS);
return -1;
}
if(!(buffer = malloc(sector_size))) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_MALLOC_ERROR, strerror(errno));
return -1;
}
// Initialize the random number generator
srandom(time(NULL));
// Determine the list of sectors we're going to check
for(i = 0; i < num_sectors_to_check; ) {
// random() only generates 31 bits of random data, so let's do some
// fuckery to turn that into 64 bits
a = random();
b = random();
c = random();
cur_sector = ((((uint64_t) a) << 33) | (((uint64_t) b) << 2) | ((uint64_t) (c >> 29))) % num_sectors;
found = 0;
// Make sure the sector isn't already marked bad
if(sector_map[cur_sector] & 0x01) {
found = 1;
}
// Make sure the list of sesctors we're going to check is unique
for(j = 0; j < i && !found; j++) {
if(sectors_to_check[j] == cur_sector) {
found = 1;
}
}
if(!found) {
sectors_to_check[i++] = cur_sector;
}
}
// Ok, we have a list of sectors to check -- let's go!
for(i = 0; i < num_sectors_to_check && (num_matching_sectors + (num_sectors_to_check - i)) >= (num_sectors_to_check / 2); i++) {
if((ret = lseek(fd, sectors_to_check[i] * sector_size, SEEK_SET)) == -1) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_LSEEK_ERROR, strerror(errno));
free(buffer);
return -1;
}
if((ret = read(fd, buffer, sector_size)) != sector_size) {
if(ret == -1) {
// Just skip over this sector
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_READ_ERROR, strerror(errno));
continue;
} else {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_SHORT_READ, ret, sector_size);
continue;
}
}
// Make sure the sector's CRC32 is correct
if(calculate_crc32c(0, buffer, sector_size)) {
continue;
}
get_embedded_device_uuid(buffer, device_uuid);
if(!memcmp(device_uuid, expected_device_uuid, sizeof(uuid_t))) {
if(++num_matching_sectors >= (num_sectors_to_check / 2)) {
free(buffer);
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_COMPARE_DEVICE_UUIDS_MATCHED);
return 0;
}
}
}
free(buffer);
if(!num_matching_sectors) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_COMPARE_DEVICE_UUIDS_NO_SECTORS_MATCHED);
} else {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_COMPARE_DEVICE_UUIDS_ONLY_X_SECTORS_MATCHED, num_matching_sectors, num_matching_sectors == 1 ? "" : "s");
}
return 1;
}
int find_device(char * preferred_dev_name,
int must_match,
size_t expected_device_size,
size_t physical_device_size,
char * bod_buffer,
char * mod_buffer,
size_t bod_mod_buffer_size,
uuid_t expected_device_uuid,
char * sector_map,
char **matched_dev_name,
dev_t * matched_dev_num,
int * newfd) {
struct udev *udev_handle;
struct udev_enumerate *udev_enum;
struct udev_list_entry *list_entry;
struct udev_device *device;
const char *dev_size_str, *dev_name;
size_t reported_device_size;
int fd, ret, i;
char **matched_devices = NULL, **new_matched_devices;
int num_matches = 0, match_index;
struct stat fs;
void free_matched_devices() {
for(int j = 0; j < num_matches; j++) {
free(matched_devices[j]);
}
free(matched_devices);
}
if(must_match) {
if(!preferred_dev_name) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_MUST_MATCH_WITHOUT_PREFERRED_DEV_NAME);
errno = EINVAL;
return -1;
}
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_FIND_DEVICE_CHECKING_DEVICE, preferred_dev_name);
// Let's just probe preferred_dev_name instead of bothering udev
if((fd = open(preferred_dev_name, O_LARGEFILE | O_RDONLY)) == -1) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_OPEN_ERROR, strerror(errno));
return 0;
}
if(ioctl(fd, BLKGETSIZE64, &reported_device_size)) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_IOCTL_ERROR, strerror(errno));
close(fd);
return 0;
}
if(reported_device_size != expected_device_size) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_FIND_DEVICE_DEVICE_SIZE_MISMATCH, expected_device_size, reported_device_size);
close(fd);
return 0;
}
if(ret = compare_bod_mod_data(fd, physical_device_size, bod_buffer, mod_buffer, bod_mod_buffer_size)) {
if(ret == -1) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_FIND_DEVICE_COMPARE_BOD_MOD_DATA_ERROR);
} else {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_FIND_DEVICE_BOD_MOD_DATA_MISMATCH);
}
// Try to match by the device UUID
if(expected_device_uuid) {
if(ret = compare_device_uuids(fd, physical_device_size, expected_device_uuid, sector_map)) {
if(ret == -1) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_COMPARE_DEVICE_UUIDS_ERROR, preferred_dev_name);
} else {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_DEVICE_UUIDS_MISMATCH, preferred_dev_name);
}
} else {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_MATCHED_DEVICE_BY_COMPARING_DEVICE_UUIDS, preferred_dev_name);
}
}
close(fd);
if(ret) {
return 0;
}
} else {
close(fd);
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_FIND_DEVICE_BOD_MOD_DATA_MATCH, preferred_dev_name);
}
// Add the device to our list of devices
if(!(matched_devices = malloc(++num_matches * sizeof(char *)))) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_MALLOC_ERROR, strerror(errno));
errno = ENOMEM;
return -1;
}
if(!(matched_devices[0] = strdup(preferred_dev_name))) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_STRDUP_ERROR, strerror(errno));
free(matched_devices);
errno = ENOMEM;
return -1;
}
} else {
// Scan through the available block devices
udev_handle = udev_new();
if(!udev_handle) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_UDEV_NEW_ERROR);
free_matched_devices();
errno = ELIBACC;
return -1;
}
if(!(udev_enum = udev_enumerate_new(udev_handle))) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_UDEV_ENUMERATE_NEW_ERROR);
udev_unref(udev_handle);
free_matched_devices();
errno = ELIBACC;
return -1;
}
if(udev_enumerate_add_match_subsystem(udev_enum, "block") < 0) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_UDEV_ENUMERATE_ADD_MATCH_SUBSYSTEM_ERROR);
udev_enumerate_unref(udev_enum);
udev_unref(udev_handle);
free_matched_devices();
errno = ELIBACC;
return -1;
}
if(udev_enumerate_scan_devices(udev_enum) < 0) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_UDEV_ENUMERATE_SCAN_DEVICES_ERROR);
udev_enumerate_unref(udev_enum);
udev_unref(udev_handle);
free_matched_devices();
errno = ELIBACC;
return -1;
}
if(!(list_entry = udev_enumerate_get_list_entry(udev_enum))) {
// This scenario has to be an error. What system wouldn't have any
// block devices??
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_UDEV_ENUMERATE_GET_LIST_ENTRY_ERROR);
udev_enumerate_unref(udev_enum);
udev_unref(udev_handle);
free_matched_devices();
errno = ELIBACC;
return -1;
}
while(list_entry) {
if(!(device = udev_device_new_from_syspath(udev_handle, udev_list_entry_get_name(list_entry)))) {
list_entry = udev_list_entry_get_next(list_entry);
continue;
}
if(!(dev_size_str = udev_device_get_sysattr_value(device, "size"))) {
udev_device_unref(device);
list_entry = udev_list_entry_get_next(list_entry);
continue;
}
if(!(dev_name = udev_device_get_devnode(device))) {
udev_device_unref(device);
list_entry = udev_list_entry_get_next(list_entry);
continue;
}
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_FIND_DEVICE_LOOKING_AT_DEVICE, dev_name);
reported_device_size = strtoull(dev_size_str, NULL, 10) * 512;
if(reported_device_size != expected_device_size) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_REJECTING_DEVICE_DEVICE_SIZE_MISMATCH, dev_name, expected_device_size, reported_device_size);
udev_device_unref(device);
list_entry = udev_list_entry_get_next(list_entry);
continue;
}
if((fd = open(dev_name, O_LARGEFILE | O_RDONLY)) == -1) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_REJECTING_DEVICE_OPEN_ERROR, dev_name, strerror(errno));
udev_device_unref(device);
list_entry = udev_list_entry_get_next(list_entry);
continue;
}
if(ret = compare_bod_mod_data(fd, physical_device_size, bod_buffer, mod_buffer, bod_mod_buffer_size)) {
if(ret == -1) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_COMPARE_BOD_MOD_ERROR, dev_name);
} else {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_BOD_MOD_MISMATCH, dev_name);
}
if(expected_device_uuid) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_COMPARING_DEVICE_UUIDS);
if(ret = compare_device_uuids(fd, physical_device_size, expected_device_uuid, sector_map)) {
if(ret == -1) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_COMPARE_DEVICE_UUIDS_ERROR, dev_name);
} else {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_DEVICE_UUIDS_MISMATCH, dev_name);
}
} else {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_MATCHED_DEVICE_BY_COMPARING_DEVICE_UUIDS, dev_name);
}
}
close(fd);
if(ret) {
udev_device_unref(device);
list_entry = udev_list_entry_get_next(list_entry);
continue;
}
} else {
close(fd);
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_FIND_DEVICE_BOD_MOD_DATA_MATCH, dev_name);
}
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_FIND_DEVICE_DEVICE_MATCHED, dev_name);
// Add the device to our list of devices
if(!(new_matched_devices = realloc(matched_devices, ++num_matches * sizeof(char *)))) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_REALLOC_ERROR, strerror(errno));
udev_device_unref(device);
udev_enumerate_unref(udev_enum);
udev_unref(udev_handle);
free_matched_devices();
errno = ENOMEM;
return -1;
}
matched_devices = new_matched_devices;
if(!(matched_devices[num_matches - 1] = strdup(dev_name))) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_STRDUP_ERROR, strerror(errno));
udev_device_unref(device);
udev_enumerate_unref(udev_enum);
udev_unref(udev_handle);
free_matched_devices();
errno = ENOMEM;
return -1;
}
udev_device_unref(device);
list_entry = udev_list_entry_get_next(list_entry);
}
// We're done with udev now
udev_enumerate_unref(udev_enum);
udev_unref(udev_handle);
}
if(!num_matches) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_FIND_DEVICE_NO_MATCHING_DEVICES_FOUND);
return 0;
} else if(num_matches > 1) {
// We found more than one match. Was a preferred_dev_name provided?
match_index = -1;
if(preferred_dev_name) {
// Does preferred_dev_name match one of the devices we found?
for(i = 0; i < num_matches; i++) {
if(!are_devices_identical(preferred_dev_name, matched_devices[i])) {
// Cool, we found a match.
match_index = i;
}
}
}
if(match_index == -1) {
// We didn't find a match.
free_matched_devices();
if(preferred_dev_name) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_FIND_DEVICE_AMBIGUOUS_PREFERRED_DEV);
} else {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_FIND_DEVICE_AMBIGUOUS_RESULT_NO_PREFERRED_DEV);
}
return num_matches;
}
} else {
match_index = 0;
}
// Ok, we have a single match. Grab the device number for it.
if(stat(matched_devices[match_index], &fs)) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_FIND_DEVICE_STAT_ERROR, matched_devices[match_index], strerror(errno));
free_matched_devices();
errno = EINTR;
return -1;
}
// Ok, we have a single match. Re-open the device read/write.
if((fd = open(matched_devices[match_index], O_DIRECT | O_SYNC | O_LARGEFILE | O_RDWR)) == -1) {
// Well crap.
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_FIND_DEVICE_OPEN_ERROR, matched_devices[match_index], strerror(errno));
free_matched_devices();
errno = EINTR;
return -1;
}
// Set newfd, matched_dev_name, and matched_dev_num
*newfd = fd;
*matched_dev_name = matched_devices[match_index];
*matched_dev_num = fs.st_rdev;
// Free all the matched devices *except* for the one at match_index
for(i = 0; i < num_matches; i++) {
if(i != match_index) {
free(matched_devices[i]);
}
}
free(matched_devices);
return 1;
}
int wait_for_device_reconnect(size_t expected_device_size,
size_t physical_device_size,
char * bod_buffer,
char * mod_buffer,
size_t bod_mod_buffer_size,
uuid_t expected_device_uuid,
char * sector_map,
char **matched_dev_name,
dev_t * matched_dev_num,
int * newfd) {
struct udev_monitor *monitor;
struct udev_device *device;
struct udev *udev_handle;
const char *dev_size_str;
const char *dev_name;
size_t reported_device_size;
int fd, ret;
char *new_dev_name;
struct stat fs;
udev_handle = udev_new();
if(!udev_handle) {
return -1;
}
monitor = udev_monitor_new_from_netlink(udev_handle, "udev");
if(!monitor) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_UDEV_MONITOR_NEW_FROM_NETLINK_ERROR);
udev_unref(udev_handle);
return -1;
}
if(udev_monitor_filter_add_match_subsystem_devtype(monitor, "block", "disk") < 0) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_UDEV_MONITOR_FILTER_ADD_MATCH_SUBSYSTEM_DEVTYPE_ERROR);
udev_monitor_unref(monitor);
udev_unref(udev_handle);
return -1;
}
if(udev_monitor_enable_receiving(monitor) < 0) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_UDEV_MONITOR_ENABLE_RECEIVING_ERROR);
udev_monitor_unref(monitor);
udev_unref(udev_handle);
return -1;
}
while(1) {
while(device = udev_monitor_receive_device(monitor)) {
dev_name = udev_device_get_devnode(device);
if(!dev_name) {
// Can't even get the name of the device... :-(
udev_device_unref(device);
continue;
}
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_DETECTED_NEW_DEVICE, dev_name);
// Check the size of the device.
dev_size_str = udev_device_get_sysattr_value(device, "size");
if(!dev_size_str) {
// Nope, let's move on.
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_REJECTING_DEVICE_CANT_GET_SIZE_OF_DEVICE, dev_name);
udev_device_unref(device);
continue;
}
reported_device_size = strtoull(dev_size_str, NULL, 10) * 512;
if(reported_device_size != expected_device_size) {
// Device's reported size doesn't match
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_REJECTING_DEVICE_DEVICE_SIZE_MISMATCH, dev_name, expected_device_size, reported_device_size);
udev_device_unref(device);
continue;
}
if((fd = open(dev_name, O_LARGEFILE | O_RDONLY)) == -1) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_REJECTING_DEVICE_OPEN_ERROR, dev_name, strerror(errno));
udev_device_unref(device);
continue;
}
ret = compare_bod_mod_data(fd, physical_device_size, bod_buffer, mod_buffer, bod_mod_buffer_size);
if(ret) {
if(ret == -1) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_COMPARE_BOD_MOD_ERROR, dev_name);
} else {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_BOD_MOD_MISMATCH, dev_name);
}
if(expected_device_uuid) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_COMPARING_DEVICE_UUIDS);
if(ret = compare_device_uuids(fd, physical_device_size, expected_device_uuid, sector_map)) {
if(ret == -1) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_COMPARE_DEVICE_UUIDS_ERROR, dev_name);
} else {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_DEVICE_UUIDS_MISMATCH, dev_name);
}
} else {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_MATCHED_DEVICE_BY_COMPARING_DEVICE_UUIDS, dev_name);
}
}
close(fd);
if(ret) {
udev_device_unref(device);
continue;
}
} else {
close(fd);
}
// Ok, we have a match. Get stats on the device.
if(stat(dev_name, &fs) == -1) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_FIND_DEVICE_STAT_ERROR, dev_name, strerror(errno));
udev_device_unref(device);
continue;
}
// Re-open the device read/write.
if((fd = open(dev_name, O_DIRECT | O_SYNC | O_LARGEFILE | O_RDWR)) == -1) {
// Well crap.
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_FIND_DEVICE_OPEN_ERROR, dev_name, strerror(errno));
udev_device_unref(device);
continue;
}
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_FIND_DEVICE_DEVICE_MATCHED, dev_name);
// Copy the device name over to device_name.
if(!(new_dev_name = strdup(dev_name))) {
log_log(__func__, SEVERITY_LEVEL_DEBUG, MSG_STRDUP_ERROR, strerror(errno));
close(fd);
udev_device_unref(device);
udev_monitor_unref(monitor);
udev_unref(udev_handle);
return -1;
}
*matched_dev_name = new_dev_name;
*matched_dev_num = fs.st_rdev;
*newfd = fd;
// Cleanup and exit
udev_device_unref(device);
udev_monitor_unref(monitor);
udev_unref(udev_handle);
return 0;
}
usleep(100000);
}
}
int can_reset_device(dev_t device_num) {
struct udev *udev_handle;
struct udev_device *child_device, *parent_device;
udev_handle = udev_new();
if(!udev_handle) {
return 0;
}
child_device = udev_device_new_from_devnum(udev_handle, 'b', device_num);
if(!child_device) {
udev_unref(udev_handle);
return 0;
}
// We only know how to reset USB devices -- so if it's not a USB device, we're gonna fail
parent_device = udev_device_get_parent_with_subsystem_devtype(child_device, "usb", "usb_device");
if(!parent_device) {
udev_device_unref(child_device);
udev_unref(udev_handle);
return 0;
}
udev_device_unref(child_device);
udev_unref(udev_handle);
return 1;
}
int reset_device(int device_fd) {
struct udev *udev_handle;
struct udev_device *child_device, *parent_device;
const char *device_name;
char *new_device_name;
dev_t device_num, new_device_num;
int fd, ret;
struct stat dev_stat;
if(ret = fstat(device_fd, &dev_stat)) {
// Can't stat the device
return -1;
}
if(!S_ISBLK(dev_stat.st_mode)) {
// Device isn't a block device
return -1;
}
device_num = dev_stat.st_rdev;
udev_handle = udev_new();
if(!udev_handle) {
return -1;
}
child_device = udev_device_new_from_devnum(udev_handle, 'b', device_num);
if(!child_device) {
udev_unref(udev_handle);
return -1;
}
parent_device = udev_device_get_parent_with_subsystem_devtype(child_device, "usb", "usb_device");