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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
|
/*
* Copyright (C) 2007-2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <libgen.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#include <linux/netlink.h>
#include <memory>
#include <selinux/selinux.h>
#include <selinux/label.h>
#include <selinux/android.h>
#include <selinux/avc.h>
#include <private/android_filesystem_config.h>
#include <android-base/file.h>
#include <android-base/stringprintf.h>
#include <android-base/unique_fd.h>
#include <cutils/list.h>
#include <cutils/uevent.h>
#include "devices.h"
#include "ueventd_parser.h"
#include "util.h"
#include "log.h"
#define SYSFS_PREFIX "/sys"
static const char *firmware_dirs[] = { "/etc/firmware",
"/vendor/firmware",
"/firmware/image" };
extern struct selabel_handle *sehandle;
static int device_fd = -1;
struct uevent {
const char *action;
const char *path;
const char *subsystem;
const char *firmware;
const char *partition_name;
const char *device_name;
int partition_num;
int major;
int minor;
};
struct perms_ {
char *name;
char *attr;
mode_t perm;
unsigned int uid;
unsigned int gid;
unsigned short prefix;
unsigned short wildcard;
};
struct perm_node {
struct perms_ dp;
struct listnode plist;
};
struct platform_node {
char *name;
char *path;
int path_len;
struct listnode list;
};
static list_declare(sys_perms);
static list_declare(dev_perms);
static list_declare(platform_names);
int add_dev_perms(const char *name, const char *attr,
mode_t perm, unsigned int uid, unsigned int gid,
unsigned short prefix,
unsigned short wildcard) {
struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
if (!node)
return -ENOMEM;
node->dp.name = strdup(name);
if (!node->dp.name)
return -ENOMEM;
if (attr) {
node->dp.attr = strdup(attr);
if (!node->dp.attr)
return -ENOMEM;
}
node->dp.perm = perm;
node->dp.uid = uid;
node->dp.gid = gid;
node->dp.prefix = prefix;
node->dp.wildcard = wildcard;
if (attr)
list_add_tail(&sys_perms, &node->plist);
else
list_add_tail(&dev_perms, &node->plist);
return 0;
}
static bool perm_path_matches(const char *path, struct perms_ *dp)
{
if (dp->prefix) {
if (strncmp(path, dp->name, strlen(dp->name)) == 0)
return true;
} else if (dp->wildcard) {
if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
return true;
} else {
if (strcmp(path, dp->name) == 0)
return true;
}
return false;
}
static bool match_subsystem(perms_* dp, const char* pattern,
const char* path, const char* subsystem) {
if (!pattern || !subsystem || strstr(dp->name, subsystem) == NULL) {
return false;
}
std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path));
return perm_path_matches(subsys_path.c_str(), dp);
}
static void fixup_sys_perms(const char* upath, const char* subsystem) {
// upaths omit the "/sys" that paths in this list
// contain, so we prepend it...
std::string path = std::string(SYSFS_PREFIX) + upath;
listnode* node;
list_for_each(node, &sys_perms) {
perms_* dp = &(node_to_item(node, perm_node, plist))->dp;
if (match_subsystem(dp, SYSFS_PREFIX "/class/%s/%s", path.c_str(), subsystem)) {
; // matched
} else if (match_subsystem(dp, SYSFS_PREFIX "/bus/%s/devices/%s", path.c_str(), subsystem)) {
; // matched
} else if (!perm_path_matches(path.c_str(), dp)) {
continue;
}
std::string attr_file = path + "/" + dp->attr;
LOG(INFO) << "fixup " << attr_file
<< " " << dp->uid << " " << dp->gid << " " << std::oct << dp->perm;
chown(attr_file.c_str(), dp->uid, dp->gid);
chmod(attr_file.c_str(), dp->perm);
}
if (access(path.c_str(), F_OK) == 0) {
LOG(VERBOSE) << "restorecon_recursive: " << path;
restorecon_recursive(path.c_str());
}
}
static mode_t get_device_perm(const char *path, const char **links,
unsigned *uid, unsigned *gid)
{
struct listnode *node;
struct perm_node *perm_node;
struct perms_ *dp;
/* search the perms list in reverse so that ueventd.$hardware can
* override ueventd.rc
*/
list_for_each_reverse(node, &dev_perms) {
bool match = false;
perm_node = node_to_item(node, struct perm_node, plist);
dp = &perm_node->dp;
if (perm_path_matches(path, dp)) {
match = true;
} else {
if (links) {
int i;
for (i = 0; links[i]; i++) {
if (perm_path_matches(links[i], dp)) {
match = true;
break;
}
}
}
}
if (match) {
*uid = dp->uid;
*gid = dp->gid;
return dp->perm;
}
}
/* Default if nothing found. */
*uid = 0;
*gid = 0;
return 0600;
}
static void make_device(const char *path,
const char */*upath*/,
int block, int major, int minor,
const char **links)
{
unsigned uid;
unsigned gid;
mode_t mode;
dev_t dev;
char *secontext = NULL;
mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
if (selabel_lookup_best_match(sehandle, &secontext, path, links, mode)) {
PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
return;
}
setfscreatecon(secontext);
dev = makedev(major, minor);
/* Temporarily change egid to avoid race condition setting the gid of the
* device node. Unforunately changing the euid would prevent creation of
* some device nodes, so the uid has to be set with chown() and is still
* racy. Fixing the gid race at least fixed the issue with system_server
* opening dynamic input devices under the AID_INPUT gid. */
setegid(gid);
/* If the node already exists update its SELinux label to handle cases when
* it was created with the wrong context during coldboot procedure. */
if (mknod(path, mode, dev) && (errno == EEXIST)) {
char* fcon = nullptr;
int rc = lgetfilecon(path, &fcon);
if (rc < 0) {
PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
goto out;
}
bool different = strcmp(fcon, secontext) != 0;
freecon(fcon);
if (different && lsetfilecon(path, secontext)) {
PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path << "' device";
}
}
out:
chown(path, uid, -1);
setegid(AID_ROOT);
freecon(secontext);
setfscreatecon(NULL);
}
static void add_platform_device(const char *path)
{
int path_len = strlen(path);
struct platform_node *bus;
const char *name = path;
if (!strncmp(path, "/devices/", 9)) {
name += 9;
if (!strncmp(name, "platform/", 9))
name += 9;
}
LOG(VERBOSE) << "adding platform device " << name << " (" << path << ")";
bus = (platform_node*) calloc(1, sizeof(struct platform_node));
bus->path = strdup(path);
bus->path_len = path_len;
bus->name = bus->path + (name - path);
list_add_tail(&platform_names, &bus->list);
}
/*
* given a path that may start with a platform device, find the length of the
* platform device prefix. If it doesn't start with a platform device, return
* 0.
*/
static struct platform_node *find_platform_device(const char *path)
{
int path_len = strlen(path);
struct listnode *node;
struct platform_node *bus;
list_for_each_reverse(node, &platform_names) {
bus = node_to_item(node, struct platform_node, list);
if ((bus->path_len < path_len) &&
(path[bus->path_len] == '/') &&
!strncmp(path, bus->path, bus->path_len))
return bus;
}
return NULL;
}
static void remove_platform_device(const char *path)
{
struct listnode *node;
struct platform_node *bus;
list_for_each_reverse(node, &platform_names) {
bus = node_to_item(node, struct platform_node, list);
if (!strcmp(path, bus->path)) {
LOG(INFO) << "removing platform device " << bus->name;
free(bus->path);
list_remove(node);
free(bus);
return;
}
}
}
/* Given a path that may start with a PCI device, populate the supplied buffer
* with the PCI domain/bus number and the peripheral ID and return 0.
* If it doesn't start with a PCI device, or there is some error, return -1 */
static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
{
const char *start, *end;
if (strncmp(path, "/devices/pci", 12))
return -1;
/* Beginning of the prefix is the initial "pci" after "/devices/" */
start = path + 9;
/* End of the prefix is two path '/' later, capturing the domain/bus number
* and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
end = strchr(start, '/');
if (!end)
return -1;
end = strchr(end + 1, '/');
if (!end)
return -1;
/* Make sure we have enough room for the string plus null terminator */
if (end - start + 1 > buf_sz)
return -1;
strncpy(buf, start, end - start);
buf[end - start] = '\0';
return 0;
}
static void parse_event(const char *msg, struct uevent *uevent)
{
uevent->action = "";
uevent->path = "";
uevent->subsystem = "";
uevent->firmware = "";
uevent->major = -1;
uevent->minor = -1;
uevent->partition_name = NULL;
uevent->partition_num = -1;
uevent->device_name = NULL;
/* currently ignoring SEQNUM */
while(*msg) {
if(!strncmp(msg, "ACTION=", 7)) {
msg += 7;
uevent->action = msg;
} else if(!strncmp(msg, "DEVPATH=", 8)) {
msg += 8;
uevent->path = msg;
} else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
msg += 10;
uevent->subsystem = msg;
} else if(!strncmp(msg, "FIRMWARE=", 9)) {
msg += 9;
uevent->firmware = msg;
} else if(!strncmp(msg, "MAJOR=", 6)) {
msg += 6;
uevent->major = atoi(msg);
} else if(!strncmp(msg, "MINOR=", 6)) {
msg += 6;
uevent->minor = atoi(msg);
} else if(!strncmp(msg, "PARTN=", 6)) {
msg += 6;
uevent->partition_num = atoi(msg);
} else if(!strncmp(msg, "PARTNAME=", 9)) {
msg += 9;
uevent->partition_name = msg;
} else if(!strncmp(msg, "DEVNAME=", 8)) {
msg += 8;
uevent->device_name = msg;
}
/* advance to after the next \0 */
while(*msg++)
;
}
if (LOG_UEVENTS) {
LOG(INFO) << android::base::StringPrintf("event { '%s', '%s', '%s', '%s', %d, %d }",
uevent->action, uevent->path, uevent->subsystem,
uevent->firmware, uevent->major, uevent->minor);
}
}
static char **get_character_device_symlinks(struct uevent *uevent)
{
const char *parent;
const char *slash;
char **links;
int link_num = 0;
int width;
struct platform_node *pdev;
pdev = find_platform_device(uevent->path);
if (!pdev)
return NULL;
links = (char**) malloc(sizeof(char *) * 2);
if (!links)
return NULL;
memset(links, 0, sizeof(char *) * 2);
/* skip "/devices/platform/<driver>" */
parent = strchr(uevent->path + pdev->path_len, '/');
if (!parent)
goto err;
if (!strncmp(parent, "/usb", 4)) {
/* skip root hub name and device. use device interface */
while (*++parent && *parent != '/');
if (*parent)
while (*++parent && *parent != '/');
if (!*parent)
goto err;
slash = strchr(++parent, '/');
if (!slash)
goto err;
width = slash - parent;
if (width <= 0)
goto err;
if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
link_num++;
else
links[link_num] = NULL;
mkdir("/dev/usb", 0755);
}
else {
goto err;
}
return links;
err:
free(links);
return NULL;
}
static char **get_block_device_symlinks(struct uevent *uevent)
{
const char *device;
struct platform_node *pdev;
const char *slash;
const char *type;
char buf[256];
char link_path[256];
int link_num = 0;
char *p;
pdev = find_platform_device(uevent->path);
if (pdev) {
device = pdev->name;
type = "platform";
} else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
device = buf;
type = "pci";
} else {
return NULL;
}
char **links = (char**) malloc(sizeof(char *) * 4);
if (!links)
return NULL;
memset(links, 0, sizeof(char *) * 4);
LOG(INFO) << "found " << type << " device " << device;
snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
if (uevent->partition_name) {
p = strdup(uevent->partition_name);
sanitize(p);
if (strcmp(uevent->partition_name, p)) {
LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '" << p << "'";
}
if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
link_num++;
else
links[link_num] = NULL;
free(p);
}
if (uevent->partition_num >= 0) {
if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
link_num++;
else
links[link_num] = NULL;
}
slash = strrchr(uevent->path, '/');
if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
link_num++;
else
links[link_num] = NULL;
return links;
}
static void make_link_init(const char* oldpath, const char* newpath) {
const char* slash = strrchr(newpath, '/');
if (!slash) return;
if (mkdir_recursive(dirname(newpath), 0755)) {
PLOG(ERROR) << "Failed to create directory " << dirname(newpath);
}
if (symlink(oldpath, newpath) && errno != EEXIST) {
PLOG(ERROR) << "Failed to symlink " << oldpath << " to " << newpath;
}
}
static void remove_link(const char* oldpath, const char* newpath) {
std::string path;
if (android::base::Readlink(newpath, &path) && path == oldpath) unlink(newpath);
}
static void handle_device(const char *action, const char *devpath,
const char *path, int block, int major, int minor, char **links)
{
if(!strcmp(action, "add")) {
make_device(devpath, path, block, major, minor, (const char **)links);
if (links) {
for (int i = 0; links[i]; i++) {
make_link_init(devpath, links[i]);
}
}
}
if(!strcmp(action, "remove")) {
if (links) {
for (int i = 0; links[i]; i++) {
remove_link(devpath, links[i]);
}
}
unlink(devpath);
}
if (links) {
for (int i = 0; links[i]; i++) {
free(links[i]);
}
free(links);
}
}
static void handle_platform_device_event(struct uevent *uevent)
{
const char *path = uevent->path;
if (!strcmp(uevent->action, "add"))
add_platform_device(path);
else if (!strcmp(uevent->action, "remove"))
remove_platform_device(path);
}
static const char *parse_device_name(struct uevent *uevent, unsigned int len)
{
const char *name;
/* if it's not a /dev device, nothing else to do */
if((uevent->major < 0) || (uevent->minor < 0))
return NULL;
/* do we have a name? */
name = strrchr(uevent->path, '/');
if(!name)
return NULL;
name++;
/* too-long names would overrun our buffer */
if(strlen(name) > len) {
LOG(ERROR) << "DEVPATH=" << name << " exceeds " << len << "-character limit on filename; ignoring event";
return NULL;
}
return name;
}
#define DEVPATH_LEN 96
#define MAX_DEV_NAME 64
static void handle_block_device_event(struct uevent *uevent)
{
const char *base = "/dev/block/";
const char *name;
char devpath[DEVPATH_LEN];
char **links = NULL;
name = parse_device_name(uevent, MAX_DEV_NAME);
if (!name)
return;
snprintf(devpath, sizeof(devpath), "%s%s", base, name);
make_dir(base, 0755);
if (!strncmp(uevent->path, "/devices/", 9))
links = get_block_device_symlinks(uevent);
handle_device(uevent->action, devpath, uevent->path, 1,
uevent->major, uevent->minor, links);
}
static bool assemble_devpath(char *devpath, const char *dirname,
const char *devname)
{
int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
if (s < 0) {
PLOG(ERROR) << "failed to assemble device path; ignoring event";
return false;
} else if (s >= DEVPATH_LEN) {
LOG(ERROR) << dirname << "/" << devname
<< " exceeds " << DEVPATH_LEN << "-character limit on path; ignoring event";
return false;
}
return true;
}
static void mkdir_recursive_for_devpath(const char *devpath)
{
char dir[DEVPATH_LEN];
char *slash;
strcpy(dir, devpath);
slash = strrchr(dir, '/');
*slash = '\0';
mkdir_recursive(dir, 0755);
}
static void handle_generic_device_event(struct uevent *uevent)
{
const char *base;
const char *name;
char devpath[DEVPATH_LEN] = {0};
char **links = NULL;
name = parse_device_name(uevent, MAX_DEV_NAME);
if (!name)
return;
struct ueventd_subsystem *subsystem =
ueventd_subsystem_find_by_name(uevent->subsystem);
if (subsystem) {
const char *devname;
switch (subsystem->devname_src) {
case DEVNAME_UEVENT_DEVNAME:
devname = uevent->device_name;
break;
case DEVNAME_UEVENT_DEVPATH:
devname = name;
break;
default:
LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event";
return;
}
if (!assemble_devpath(devpath, subsystem->dirname, devname))
return;
mkdir_recursive_for_devpath(devpath);
} else if (!strncmp(uevent->subsystem, "usb", 3)) {
if (!strcmp(uevent->subsystem, "usb")) {
if (uevent->device_name) {
if (!assemble_devpath(devpath, "/dev", uevent->device_name))
return;
mkdir_recursive_for_devpath(devpath);
}
else {
/* This imitates the file system that would be created
* if we were using devfs instead.
* Minors are broken up into groups of 128, starting at "001"
*/
int bus_id = uevent->minor / 128 + 1;
int device_id = uevent->minor % 128 + 1;
/* build directories */
make_dir("/dev/bus", 0755);
make_dir("/dev/bus/usb", 0755);
snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
make_dir(devpath, 0755);
snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
}
} else {
/* ignore other USB events */
return;
}
} else if (!strncmp(uevent->subsystem, "graphics", 8)) {
base = "/dev/graphics/";
make_dir(base, 0755);
} else if (!strncmp(uevent->subsystem, "drm", 3)) {
base = "/dev/dri/";
make_dir(base, 0755);
} else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
base = "/dev/oncrpc/";
make_dir(base, 0755);
} else if (!strncmp(uevent->subsystem, "adsp", 4)) {
base = "/dev/adsp/";
make_dir(base, 0755);
} else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
base = "/dev/msm_camera/";
make_dir(base, 0755);
} else if(!strncmp(uevent->subsystem, "input", 5)) {
base = "/dev/input/";
make_dir(base, 0755);
} else if(!strncmp(uevent->subsystem, "mtd", 3)) {
base = "/dev/mtd/";
make_dir(base, 0755);
} else if(!strncmp(uevent->subsystem, "sound", 5)) {
base = "/dev/snd/";
make_dir(base, 0755);
} else if(!strncmp(uevent->subsystem, "misc", 4) && !strncmp(name, "log_", 4)) {
LOG(INFO) << "kernel logger is deprecated";
base = "/dev/log/";
make_dir(base, 0755);
name += 4;
} else
base = "/dev/";
links = get_character_device_symlinks(uevent);
if (!devpath[0])
snprintf(devpath, sizeof(devpath), "%s%s", base, name);
handle_device(uevent->action, devpath, uevent->path, 0,
uevent->major, uevent->minor, links);
}
static void handle_device_event(struct uevent *uevent)
{
if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
fixup_sys_perms(uevent->path, uevent->subsystem);
if (!strncmp(uevent->subsystem, "block", 5)) {
handle_block_device_event(uevent);
} else if (!strncmp(uevent->subsystem, "platform", 8)) {
handle_platform_device_event(uevent);
} else {
handle_generic_device_event(uevent);
}
}
static int load_firmware(int fw_fd, int loading_fd, int data_fd)
{
struct stat st;
long len_to_copy;
int ret = 0;
if(fstat(fw_fd, &st) < 0)
return -1;
len_to_copy = st.st_size;
write(loading_fd, "1", 1); /* start transfer */
while (len_to_copy > 0) {
char buf[PAGE_SIZE];
ssize_t nr;
nr = read(fw_fd, buf, sizeof(buf));
if(!nr)
break;
if(nr < 0) {
ret = -1;
break;
}
if (!android::base::WriteFully(data_fd, buf, nr)) {
ret = -1;
break;
}
len_to_copy -= nr;
}
if(!ret)
write(loading_fd, "0", 1); /* successful end of transfer */
else
write(loading_fd, "-1", 2); /* abort transfer */
return ret;
}
static int is_booting(void)
{
return access("/dev/.booting", F_OK) == 0;
}
static void process_firmware_event(struct uevent *uevent)
{
char *root, *loading, *data;
int l, loading_fd, data_fd, fw_fd;
size_t i;
int booting = is_booting();
LOG(INFO) << "firmware: loading '" << uevent->firmware << "' for '" << uevent->path << "'";
l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
if (l == -1)
return;
l = asprintf(&loading, "%sloading", root);
if (l == -1)
goto root_free_out;
l = asprintf(&data, "%sdata", root);
if (l == -1)
goto loading_free_out;
loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
if(loading_fd < 0)
goto data_free_out;
data_fd = open(data, O_WRONLY|O_CLOEXEC);
if(data_fd < 0)
goto loading_close_out;
try_loading_again:
for (i = 0; i < arraysize(firmware_dirs); i++) {
char *file = NULL;
l = asprintf(&file, "%s/%s", firmware_dirs[i], uevent->firmware);
if (l == -1)
goto data_free_out;
fw_fd = open(file, O_RDONLY|O_CLOEXEC);
free(file);
if (fw_fd >= 0) {
if (!load_firmware(fw_fd, loading_fd, data_fd)) {
LOG(INFO) << "firmware: copy success { '" << root << "', '" << uevent->firmware << "' }";
} else {
LOG(ERROR) << "firmware: copy failure { '" << root << "', '" << uevent->firmware << "' }";
}
break;
}
}
if (fw_fd < 0) {
if (booting) {
/* If we're not fully booted, we may be missing
* filesystems needed for firmware, wait and retry.
*/
usleep(100000);
booting = is_booting();
goto try_loading_again;
}
PLOG(ERROR) << "firmware: could not open '" << uevent->firmware << "'";
write(loading_fd, "-1", 2);
goto data_close_out;
}
close(fw_fd);
data_close_out:
close(data_fd);
loading_close_out:
close(loading_fd);
data_free_out:
free(data);
loading_free_out:
free(loading);
root_free_out:
free(root);
}
static void handle_firmware_event(struct uevent *uevent)
{
pid_t pid;
if(strcmp(uevent->subsystem, "firmware"))
return;
if(strcmp(uevent->action, "add"))
return;
/* we fork, to avoid making large memory allocations in init proper */
pid = fork();
if (!pid) {
process_firmware_event(uevent);
_exit(EXIT_SUCCESS);
} else if (pid < 0) {
PLOG(ERROR) << "could not fork to process firmware event";
}
}
#define UEVENT_MSG_LEN 2048
static inline void handle_device_fd_with(void (handle_uevent)(struct uevent*))
{
char msg[UEVENT_MSG_LEN+2];
int n;
while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
continue;
msg[n] = '\0';
msg[n+1] = '\0';
struct uevent uevent;
parse_event(msg, &uevent);
handle_uevent(&uevent);
}
}
void handle_device_fd()
{
handle_device_fd_with(
[](struct uevent *uevent) {
if (selinux_status_updated() > 0) {
struct selabel_handle *sehandle2;
sehandle2 = selinux_android_file_context_handle();
if (sehandle2) {
selabel_close(sehandle);
sehandle = sehandle2;
}
}
handle_device_event(uevent);
handle_firmware_event(uevent);
});
}
/* Coldboot walks parts of the /sys tree and pokes the uevent files
** to cause the kernel to regenerate device add events that happened
** before init's device manager was started
**
** We drain any pending events from the netlink socket every time
** we poke another uevent file to make sure we don't overrun the
** socket's buffer.
*/
static void do_coldboot(DIR *d)
{
struct dirent *de;
int dfd, fd;
dfd = dirfd(d);
fd = openat(dfd, "uevent", O_WRONLY);
if(fd >= 0) {
write(fd, "add\n", 4);
close(fd);
handle_device_fd();
}
while((de = readdir(d))) {
DIR *d2;
if(de->d_type != DT_DIR || de->d_name[0] == '.')
continue;
fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
if(fd < 0)
continue;
d2 = fdopendir(fd);
if(d2 == 0)
close(fd);
else {
do_coldboot(d2);
closedir(d2);
}
}
}
static void coldboot(const char *path)
{
std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
if(d) {
do_coldboot(d.get());
}
}
static void early_uevent_handler(struct uevent *uevent, const char *base, bool is_block)
{
const char *name;
char devpath[DEVPATH_LEN];
if (is_block && strncmp(uevent->subsystem, "block", 5))
return;
name = parse_device_name(uevent, MAX_DEV_NAME);
if (!name) {
LOG(ERROR) << "Failed to parse dev name from uevent: " << uevent->action
<< " " << uevent->partition_name << " " << uevent->partition_num
<< " " << uevent->major << ":" << uevent->minor;
return;
}
snprintf(devpath, sizeof(devpath), "%s%s", base, name);
make_dir(base, 0755);
dev_t dev = makedev(uevent->major, uevent->minor);
mode_t mode = 0600 | (is_block ? S_IFBLK : S_IFCHR);
mknod(devpath, mode, dev);
}
void early_create_dev(const std::string& syspath, early_device_type dev_type)
{
android::base::unique_fd dfd(open(syspath.c_str(), O_RDONLY));
if (dfd < 0) {
LOG(ERROR) << "Failed to open " << syspath;
return;
}
android::base::unique_fd fd(openat(dfd, "uevent", O_WRONLY));
if (fd < 0) {
LOG(ERROR) << "Failed to open " << syspath << "/uevent";
return;
}
fcntl(device_fd, F_SETFL, O_NONBLOCK);
write(fd, "add\n", 4);
handle_device_fd_with(dev_type == EARLY_BLOCK_DEV ?
[](struct uevent *uevent) {
early_uevent_handler(uevent, "/dev/block/", true);
} :
[](struct uevent *uevent) {
early_uevent_handler(uevent, "/dev/", false);
});
}
int early_device_socket_open() {
device_fd = uevent_open_socket(256*1024, true);
return device_fd < 0;
}
void early_device_socket_close() {
close(device_fd);
}
void device_init() {
sehandle = selinux_android_file_context_handle();
selinux_status_open(true);
/* is 256K enough? udev uses 16MB! */
device_fd = uevent_open_socket(256*1024, true);
if (device_fd == -1) {
return;
}
fcntl(device_fd, F_SETFL, O_NONBLOCK);
if (access(COLDBOOT_DONE, F_OK) == 0) {
LOG(VERBOSE) << "Skipping coldboot, already done!";
return;
}
Timer t;
coldboot("/sys/class");
coldboot("/sys/block");
coldboot("/sys/devices");
close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
LOG(INFO) << "Coldboot took " << t.duration() << "s.";
}
int get_device_fd()
{
return device_fd;
}
|