/
linux.c
1480 lines (1046 loc) · 38.9 KB
/
linux.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
/*
vdev: a virtual device manager for *nix
Copyright (C) 2015 Jude Nelson
This program is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 or later as
published by the Free Software Foundation. For the terms of this
license, see LICENSE.GPLv3+ or <http://www.gnu.org/licenses/>.
You are free to use this program under the terms of the GNU General
Public License, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
Alternatively, you are free to use this program under the terms of the
Internet Software Consortium License, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the terms of this license, see LICENSE.ISC or
<http://www.isc.org/downloads/software-support-policy/isc-license/>.
*/
#ifdef _VDEV_OS_LINUX
#include "linux.h"
#include "workqueue.h"
#include "libvdev/sglib.h"
// parse a uevent action
static vdev_device_request_t vdev_linux_parse_device_request_type( char const* type ) {
if( strcmp(type, "add") == 0 ) {
return VDEV_DEVICE_ADD;
}
else if( strcmp(type, "remove") == 0 ) {
return VDEV_DEVICE_REMOVE;
}
else if( strcmp(type, "change") == 0 ) {
return VDEV_DEVICE_CHANGE;
}
return VDEV_DEVICE_INVALID;
}
// make the full sysfs path from the dev path, plus an additional path
// return NULL on OOM
static char* vdev_linux_sysfs_fullpath( char const* sysfs_mountpoint, char const* devpath, char const* attr_path ) {
char* tmp = NULL;
char* ret = NULL;
tmp = vdev_fullpath( sysfs_mountpoint, devpath, NULL );
if( tmp == NULL ) {
return NULL;
}
ret = vdev_fullpath( tmp, attr_path, NULL );
free( tmp );
return ret;
}
// parse a device number pair
// return 0 on success, and set *major and *minor
// return -EINVAL if we failed to parse
static int vdev_linux_sysfs_parse_device_nums( char const* devbuf, unsigned int* major, unsigned int* minor ) {
int rc = 0;
// parse devpath
rc = sscanf( devbuf, "%u:%u", major, minor );
if( rc != 2 ) {
vdev_error("sscanf('%s') for major:minor rc = %d\n", devbuf, rc );
rc = -EINVAL;
}
else {
rc = 0;
}
return rc;
}
// read the device major and minor number, using the devpath
// return 0 on success, and set *major and *minor
// return -ENOMEM on OOM
// return -errno on failure to open or read
static int vdev_linux_sysfs_read_dev_nums( struct vdev_linux_context* ctx, char const* devpath, unsigned int* major, unsigned int* minor ) {
int rc = 0;
int fd = 0;
ssize_t nr = 0;
char devbuf[101];
memset( devbuf, 0, 101 );
char* full_devpath = vdev_linux_sysfs_fullpath( ctx->sysfs_mountpoint, devpath, "dev" );
if( full_devpath == NULL ) {
return -ENOMEM;
}
// open device path
fd = open( full_devpath, O_RDONLY );
if( fd < 0 ) {
rc = -errno;
if( rc != -ENOENT ) {
vdev_error("open('%s') rc = %d\n", full_devpath, rc );
}
free( full_devpath );
return rc;
}
nr = vdev_read_uninterrupted( fd, devbuf, 100 );
if( nr < 0 ) {
rc = nr;
vdev_error("read('%s') rc = %d\n", full_devpath, rc );
free( full_devpath );
close( fd );
return rc;
}
close( fd );
free( full_devpath );
rc = vdev_linux_sysfs_parse_device_nums( devbuf, major, minor );
if( rc != 0 ) {
vdev_error("Failed to parse '%s'\n", devbuf );
rc = -EIO;
}
return rc;
}
// read the kernel-given device subsystem from sysfs
// return 0 on success, and set *subsystem
// return -ENOMEM on OOM
// return negative on readlink failure
static int vdev_linux_sysfs_read_subsystem( struct vdev_linux_context* ctx, char const* devpath, char** subsystem ) {
int rc = 0;
char linkpath[PATH_MAX+1];
size_t linkpath_len = PATH_MAX;
char* subsystem_path = NULL;
memset( linkpath, 0, PATH_MAX+1 );
subsystem_path = vdev_linux_sysfs_fullpath( ctx->sysfs_mountpoint, devpath, "subsystem" );
if( subsystem_path == NULL ) {
return -ENOMEM;
}
rc = readlink( subsystem_path, linkpath, linkpath_len );
if( rc < 0 ) {
rc = -errno;
vdev_error("readlink('%s') rc = %d\n", subsystem_path, rc );
free( subsystem_path );
return rc;
}
free( subsystem_path );
*subsystem = vdev_basename( linkpath, NULL );
if( *subsystem == NULL ) {
return -ENOMEM;
}
return 0;
}
// print a uevent, either with debugging or error loglevels
static int vdev_linux_log_uevent( char const* uevent_buf, size_t uevent_buf_len, bool debug ) {
for( unsigned int i = 0; i < uevent_buf_len; ) {
if( debug ) {
vdev_debug("uevent '%s'\n", uevent_buf + i );
}
else {
vdev_error("uevent '%s'\n", uevent_buf + i );
}
i += strlen(uevent_buf + i) + 1;
}
return 0;
}
#define vdev_linux_debug_uevent( uevent_buf, uevent_buf_len ) vdev_linux_log_uevent( uevent_buf, uevent_buf_len, true )
#define vdev_linux_error_uevent( uevent_buf, uevent_buf_len ) vdev_linux_log_uevent( uevent_buf, uevent_buf_len, false )
// parse a uevent, and use the information to fill in a device request.
// nlbuf must be a contiguous concatenation of null-terminated KEY=VALUE strings.
// return 0 on success
static int vdev_linux_parse_request( struct vdev_linux_context* ctx, struct vdev_device_request* vreq, char* nlbuf, ssize_t buflen ) {
char* buf = nlbuf;
char* key = NULL;
char* value = NULL;
int offset = 0;
int rc = 0;
unsigned int major = 0;
unsigned int minor = 0;
bool have_major = false;
bool have_minor = false;
mode_t dev_mode = 0;
int line_count = 0;
bool not_param = false; // if set to true, add as an OS-specific parameter to the vreq
char* devpath = NULL; // sysfs devpath
char* subsystem = NULL; // sysfs subsystem
char* devname = (char*)VDEV_DEVICE_PATH_UNKNOWN; // DEVNAME from uevent
vdev_device_request_t reqtype = VDEV_DEVICE_INVALID;
vdev_debug("%p: uevent buffer\n", vreq );
vdev_linux_debug_uevent( nlbuf, buflen );
// sanity check: if the first line is $action@$devpath, then skip it (since the information
// contained in the uevent will encode the very same bits of information)
if( strchr(buf, '@') != NULL ) {
// advance to the next line
offset += strlen(buf) + 1;
}
// get key/value pairs
while( offset < buflen ) {
line_count++;
not_param = false;
rc = vdev_keyvalue_next( buf + offset, &key, &value );
if( rc < 0 ) {
vdev_error("Invalid line %d (byte %d): '%s'\n", line_count, offset, buf + offset );
vdev_linux_error_uevent( nlbuf, buflen );
return -EINVAL;
}
offset += rc + 1; // count the \0 at the end
rc = 0;
// is this the action to take?
if( strcmp(key, "ACTION") == 0 ) {
reqtype = vdev_linux_parse_device_request_type( value );
if( reqtype == VDEV_DEVICE_INVALID ) {
vdev_error("Invalid ACTION '%s'\n", value );
vdev_linux_error_uevent( nlbuf, buflen );
return -EINVAL;
}
vdev_device_request_set_type( vreq, reqtype );
not_param = true;
}
// is this the sysfs device path?
else if( strcmp(key, "DEVPATH") == 0 ) {
devpath = value;
}
// is this the devname?
else if( strcmp(key, "DEVNAME") == 0 ) {
devname = value;
}
// subsystem given?
else if( strcmp(key, "SUBSYSTEM") == 0 ) {
subsystem = vdev_strdup_or_null( value );
}
// is this the major device number?
else if( strcmp(key, "MAJOR") == 0 && !have_major ) {
char* tmp = NULL;
major = (int)strtol( value, &tmp, 10 );
if( *tmp != '\0' ) {
vdev_error("Invalid 'MAJOR' value '%s'\n", value);
vdev_linux_error_uevent( nlbuf, buflen );
return -EINVAL;
}
have_major = true;
not_param = true;
}
// is this the minor device number?
else if( strcmp(key, "MINOR") == 0 && !have_minor ) {
char* tmp = NULL;
minor = (int)strtol( value, &tmp, 10 ) ;
if( *tmp != '\0' ) {
vdev_error("Invalid 'MINOR' value '%s'\n", value );
vdev_linux_error_uevent( nlbuf, buflen );
return -EINVAL;
}
have_minor = true;
not_param = true;
}
if( !not_param ) {
// add to OS params
rc = vdev_device_request_add_param( vreq, key, value );
if( rc != 0 ) {
// could be OOM
if( subsystem != NULL ) {
free( subsystem );
}
return rc;
}
}
}
if( reqtype == VDEV_DEVICE_INVALID ) {
vdev_error("%s", "No ACTION given\n");
vdev_linux_error_uevent( nlbuf, buflen );
if( subsystem != NULL ) {
free( subsystem );
}
return -EINVAL;
}
if( (!have_major && have_minor) || (have_major && !have_minor) ) {
vdev_error("Missing device information: major=%d, minor=%d\n", have_major, have_minor );
vdev_linux_error_uevent( nlbuf, buflen );
if( subsystem != NULL ) {
free( subsystem );
}
return -EINVAL;
}
if( have_major && have_minor ) {
// explicit major and minor device numbers given
vdev_device_request_set_dev( vreq, makedev(major, minor) );
}
if( devname != NULL ) {
// use this as the device's path
vdev_device_request_set_path( vreq, devname );
}
if( devpath != NULL ) {
// get any remaining information from sysfs
// check major/minor?
if( !have_major || !have_minor ) {
// see if we have major/minor device numbers for this device...
rc = vdev_linux_sysfs_read_dev_nums( ctx, devpath, &major, &minor );
if( rc == 0 ) {
// yup!
vdev_device_request_set_dev( vreq, makedev(major, minor) );
have_major = true;
have_minor = true;
}
else {
// it's okay to not have dev numbers
rc = 0;
}
}
// subsystem?
if( subsystem == NULL ) {
// see if we have a subsystem
rc = vdev_linux_sysfs_read_subsystem( ctx, devpath, &subsystem );
if( rc == 0 ) {
// yup!
rc = vdev_device_request_add_param( vreq, "SUBSYSTEM", subsystem );
if( rc != 0 ) {
// OOM
free( subsystem );
return rc;
}
}
else if( rc != -ENOMEM ) {
// this is weird...
vdev_warn("no subsystem found for '%s'\n", devpath );
rc = 0;
}
}
}
if( have_major && have_minor ) {
if( subsystem != NULL && strcasecmp(subsystem, "block") == 0 ) {
// this is a block
dev_mode = S_IFBLK;
}
else {
// this is a character device--we have major/minor numbers
dev_mode = S_IFCHR;
}
vdev_device_request_set_mode( vreq, dev_mode );
}
vdev_debug("subsystem = '%s', have_major=%d, major = %u, have_minor=%d, minor = %u, mode = %o\n", subsystem, have_major, major, have_minor, minor, dev_mode );
if( subsystem != NULL ) {
free( subsystem );
}
// tell helpers where /sys is mounted
rc = vdev_device_request_add_param( vreq, "SYSFS_MOUNTPOINT", ctx->sysfs_mountpoint );
if( rc != 0 ) {
// OOM
return rc;
}
return rc;
}
// yield the next device event
// return 0 on success
// return 1 if there are no more devices
// return -EAGAIN if vdev should try to get this device again
// return -errno on failure to poll for devices or read the next device packet.
int vdev_os_next_device( struct vdev_device_request* vreq, void* cls ) {
int rc = 0;
struct vdev_linux_context* ctx = (struct vdev_linux_context*)cls;
char buf[VDEV_LINUX_NETLINK_BUF_MAX];
ssize_t len = 0;
char cbuf[CMSG_SPACE(sizeof(struct ucred))];
struct cmsghdr *chdr = NULL;
struct ucred *cred = NULL;
struct msghdr hdr;
struct iovec iov;
struct sockaddr_nl cnls;
pthread_mutex_lock( &ctx->initial_requests_lock );
// do we have initial requests?
if( ctx->initial_requests != NULL ) {
// next request
struct vdev_device_request* req = ctx->initial_requests;
// consume
ctx->initial_requests = ctx->initial_requests->next;
memcpy( vreq, req, sizeof(struct vdev_device_request) );
free( req );
pthread_mutex_unlock( &ctx->initial_requests_lock );
// was that the last of them?
if( ctx->initial_requests == NULL ) {
// tell vdevd that we've finished coldplug processing
vdev_os_context_signal_coldplug_finished( ctx->os_ctx );
}
return 0;
}
else if( ctx->os_ctx->coldplug_only ) {
// out of coldplug requests; die
pthread_mutex_unlock( &ctx->initial_requests_lock );
return 1;
}
else {
pthread_mutex_unlock( &ctx->initial_requests_lock );
}
memset(&hdr, 0, sizeof(struct msghdr));
// next event (wait forever)
// NOTE: this is a cancellation point!
rc = poll( &ctx->pfd, 1, -1 );
if( rc < 0 ) {
rc = -errno;
if( rc == -EINTR ) {
// try again
return -EAGAIN;
}
vdev_error("FATAL: poll(%d) rc = %d\n", ctx->pfd.fd, rc );
return rc;
}
// get the event
iov.iov_base = buf;
iov.iov_len = VDEV_LINUX_NETLINK_BUF_MAX;
hdr.msg_iov = &iov;
hdr.msg_iovlen = 1;
// get control-plane messages
hdr.msg_control = cbuf;
hdr.msg_controllen = sizeof(cbuf);
hdr.msg_name = &cnls;
hdr.msg_namelen = sizeof(cnls);
// get the event
len = recvmsg( ctx->pfd.fd, &hdr, 0 );
if( len < 0 ) {
rc = -errno;
vdev_error("FATAL: recvmsg(%d) rc = %d\n", ctx->pfd.fd, rc );
return rc;
}
// big enough?
if( len < 32 || len >= VDEV_LINUX_NETLINK_BUF_MAX ) {
vdev_error("Netlink message is %zd bytes; ignoring...\n", len );
return -EAGAIN;
}
// control message, for credentials
chdr = CMSG_FIRSTHDR( &hdr );
if( chdr == NULL || chdr->cmsg_type != SCM_CREDENTIALS ) {
vdev_error("%s", "Netlink message has no credentials\n");
return -EAGAIN;
}
// get the credentials
cred = (struct ucred *)CMSG_DATA(chdr);
// if not root, ignore
if( cred->uid != 0 ) {
vdev_error("Ignoring message from non-root ID %d\n", cred->uid );
return -EAGAIN;
}
// if udev, ignore
if( memcmp( buf, VDEV_LINUX_NETLINK_UDEV_HEADER, VDEV_LINUX_NETLINK_UDEV_HEADER_LEN ) == 0 ) {
// message from udev; ignore
vdev_warn("%s", "Ignoring libudev message\n");
return -EAGAIN;
}
// kernel messages don't come from userspace
if( cnls.nl_pid > 0 ) {
// from userspace???
vdev_warn("Ignoring message from PID %d\n", (int)cnls.nl_pid );
return -EAGAIN;
}
// parse the event buffer
vdev_debug("%p from netlink\n", vreq );
rc = vdev_linux_parse_request( ctx, vreq, buf, len );
if( rc != 0 ) {
vdev_error("vdev_linux_parse_request rc = %d\n", rc );
return -EAGAIN;
}
return 0;
}
// find sysfs mountpoint in /proc/mounts
// this is apparently superfluous (it *should* be mounted at /sys), but you never know.
// mountpoint must be big enough (PATH_MAX will do)
// return 0 on success
// return -ENOSYS if sysfs is not mounted
// return -ENOMEM if the buffer isn't big enough
// return -EINVAL if somehow we failed to parse a mount entry
// return negative for some other errors (like access permission failures, or /proc not mounted)
static int vdev_linux_find_sysfs_mountpoint( char* mountpoint, size_t mountpoint_len ) {
FILE* f = NULL;
int rc = 0;
char mntbuf[4096];
struct mntent ment_buf;
struct mntent* ment_ptr = NULL;
int ent_count = 1;
bool found = false;
f = fopen( "/proc/mounts", "r" );
if( f == NULL ) {
rc = -errno;
fprintf(stderr, "Failed to open /proc/mounts, rc = %d\n", rc );
return rc;
}
// scan for sysfs mount type
while( 1 ) {
ment_ptr = getmntent_r( f, &ment_buf, mntbuf, 4096 );
if( ment_ptr == NULL ) {
vdev_error("Failed on processing entry #%d of /proc/mounts\n", ent_count );
rc = -EINVAL;
break;
}
if( strcmp( ment_ptr->mnt_type, "sysfs" ) == 0 ) {
// found!
strncpy( mountpoint, ment_ptr->mnt_dir, mountpoint_len - 1 );
found = true;
rc = 0;
break;
}
ent_count++;
}
fclose( f );
if( rc == 0 && !found ) {
fprintf(stderr, "Failed to find mounted sysfs filesystem\n");
return -ENOSYS;
}
return rc;
}
// get a uevent from a uevent file
// replace newlines with '\0', making the uevent look like it came from the netlink socket
// (i.e. so it can be parsed by vdev_linux_parse_request)
// return 0 on success
// return -ENOMEM on OOM
// return -errno on failure to stat or read
static int vdev_linux_sysfs_read_uevent( char const* fp_uevent, char** ret_uevent_buf, size_t* ret_uevent_len ) {
int rc = 0;
struct stat sb;
char* uevent_buf = NULL;
size_t uevent_buf_len = 0;
size_t uevent_len = 0;
// get uevent size
rc = stat( fp_uevent, &sb );
if( rc != 0 ) {
rc = -errno;
vdev_error("stat('%s') rc = %d\n", fp_uevent, rc );
return rc;
}
else {
uevent_buf_len = sb.st_size;
}
// read the uevent
if( fp_uevent != NULL ) {
uevent_buf = VDEV_CALLOC( char, uevent_buf_len );
if( uevent_buf == NULL ) {
return -ENOMEM;
}
rc = vdev_read_file( fp_uevent, uevent_buf, uevent_buf_len );
if( rc != 0 ) {
// failed in this
vdev_error("vdev_read_file('%s') rc = %d\n", fp_uevent, rc );
free( uevent_buf );
}
else {
for( unsigned int i = 0; i < uevent_buf_len; i++ ) {
if( uevent_buf[i] == '\n' ) {
uevent_buf[i] = '\0';
}
}
// NOTE: the stat size is an upper-bound. Find the exact number of bytes.
for( uevent_len = 0; uevent_len < uevent_buf_len; ) {
if( *(uevent_buf + uevent_len) == '\0' ) {
break;
}
uevent_len += strlen( uevent_buf + uevent_len ) + 1;
}
*ret_uevent_buf = uevent_buf;
*ret_uevent_len = uevent_len;
}
}
return rc;
}
// append a key/value pair to a uevent buffer
// return 0 on success
// return -ENOMEM on OOM
static int vdev_linux_uevent_append( char** ret_uevent_buf, size_t* ret_uevent_buf_len, char const* key, char const* value ) {
char* tmp = NULL;
char* uevent_buf = *ret_uevent_buf;
size_t uevent_buf_len = *ret_uevent_buf_len;
// add it to the uevent buffer, so we can parse it like a normal uevent
tmp = (char*)realloc( uevent_buf, uevent_buf_len + 1 + strlen(key) + 1 + strlen(value) + 1 );
if( tmp == NULL ) {
return -ENOMEM;
}
uevent_buf = tmp;
// add key
memcpy( uevent_buf + uevent_buf_len, key, strlen(key) );
uevent_buf_len += strlen(key);
// add '='
*(uevent_buf + uevent_buf_len) = '=';
uevent_buf_len ++;
// add value
memcpy( uevent_buf + uevent_buf_len, value, strlen(value) );
uevent_buf_len += strlen(value);
// NULL-terminate
*(uevent_buf + uevent_buf_len) = '\0';
uevent_buf_len ++;
*ret_uevent_buf = uevent_buf;
*ret_uevent_buf_len = uevent_buf_len;
return 0;
}
// register a device from sysfs, given the path to its uevent file.
// that is, read the uevent file, generate a device request, and add it to the initial_requests list in the ctx.
// NOTE: it is assumed that fp_uevent--the full path to the uevent file--lives in /sys/devices
// return 0 on success
// return -ENOMEM on OOM
// return negative on error.
static int vdev_linux_sysfs_register_device( struct vdev_linux_context* ctx, char const* fp_uevent ) {
int rc = 0;
struct stat sb;
char* uevent_buf = NULL;
size_t uevent_buf_len = 0;
char* full_devpath = NULL;
char* devpath = NULL;
char* devname = NULL;
char* delim = NULL;
// extract the devpath from the uevent path
full_devpath = vdev_dirname( fp_uevent, NULL );
if( full_devpath == NULL ) {
return -ENOMEM;
}
// get uevent
rc = vdev_linux_sysfs_read_uevent( fp_uevent, &uevent_buf, &uevent_buf_len );
if( rc != 0 ) {
vdev_error("vdev_linux_sysfs_read_uevent('%s') rc = %d\n", fp_uevent, rc );
free( full_devpath );
return rc;
}
if( uevent_buf_len == 0 ) {
// no messages to be had
vdev_debug("Empty uevent file at '%s'\n", fp_uevent );
free( full_devpath );
free( uevent_buf );
return 0;
}
// truncate to /devices
devpath = full_devpath + strlen( ctx->sysfs_mountpoint );
// we're adding this, so make ACTION=add
rc = vdev_linux_uevent_append( &uevent_buf, &uevent_buf_len, "ACTION", "add" );
if( rc != 0 ) {
vdev_error("vdev_linux_uevent_append('%s=%s') rc = %d\n", "ACTION", "add", rc );
free( uevent_buf );
free( full_devpath );
return rc;
}
// see if the uevent has a devname. Use that over our devname, if need be
devname = strstr( uevent_buf, "DEVNAME=" );
if( devname != NULL ) {
// have a devname!
devname += strlen("DEVNAME=") + 1;
size_t devname_len = strcspn( devname, "\n\0" );
if( devname_len > 0 ) {
char* tmp = VDEV_CALLOC( char, devname_len + 1 );
if( tmp == NULL ) {
free( uevent_buf );
free( full_devpath );
return -ENOMEM;
}
strncpy( tmp, devname, devname_len );
devname = tmp;
}
}
// include the device path
rc = vdev_linux_uevent_append( &uevent_buf, &uevent_buf_len, "DEVPATH", devpath );
if( rc != 0 ) {
vdev_error("vdev_linux_uevent_append('%s=%s') rc = %d\n", "DEVPATH", devpath, rc );
free( uevent_buf );
free( full_devpath );
free( devname );
return rc;
}
// make the device request
struct vdev_device_request* vreq = VDEV_CALLOC( struct vdev_device_request, 1 );
if( vreq == NULL ) {
free( full_devpath );
free( uevent_buf );
return -ENOMEM;
}
// build up the request
vdev_device_request_init( vreq, ctx->os_ctx->state, VDEV_DEVICE_INVALID, devname );
// parse from our uevent
rc = vdev_linux_parse_request( ctx, vreq, uevent_buf, uevent_buf_len );
free( uevent_buf );
uevent_buf = NULL;
if( rc != 0 ) {
vdev_error("vdev_linux_parse_request('%s') rc = %d\n", fp_uevent, rc );
free( full_devpath );
free( devname );
return rc;
}
free( full_devpath );
free( devname );
pthread_mutex_lock( &ctx->initial_requests_lock );
// append
if( ctx->initial_requests == NULL ) {
ctx->initial_requests = vreq;
ctx->initial_requests_tail = vreq;
}
else {
ctx->initial_requests_tail->next = vreq;
ctx->initial_requests_tail = vreq;
}
vreq->next = NULL;
pthread_mutex_unlock( &ctx->initial_requests_lock );
return rc;
}
// scan structure for finding new device directories, and signalling whether or not
// the given directory has a uevent.
struct vdev_linux_sysfs_scan_context {
char* uevent_path;
struct sglib_cstr_vector* device_frontier;
};
// scan a directory in /sys/devices directory, to find its child directories
// return 0 on success
// return -ENOMEM on OOM
// return -errno on failure to stat
static int vdev_linux_sysfs_scan_device_directory( char const* fp, void* cls ) {
struct vdev_linux_sysfs_scan_context* scan_ctx = (struct vdev_linux_sysfs_scan_context*)cls;
struct sglib_cstr_vector* device_frontier = scan_ctx->device_frontier;
struct stat sb;
int rc = 0;
char* fp_base = NULL;
char* fp_dup = NULL;
fp_base = rindex( fp, '/' );
if( fp_base == NULL ) {
return 0;
}
// skip . and ..
if( strcmp( fp_base, "/." ) == 0 || strcmp( fp_base, "/.." ) == 0 ) {
return 0;
}
// add directories
rc = lstat( fp, &sb );
if( rc != 0 ) {
rc = -errno;
vdev_error("lstat('%s') rc = %d\n", fp, rc );
return rc;
}
if( !S_ISDIR( sb.st_mode ) && strcmp( fp_base, "/uevent" ) != 0 ) {
// not a directory, and not a uevent
return 0;
}
fp_dup = vdev_strdup_or_null( fp );
if( fp_dup == NULL ) {
return -ENOMEM;
}