devices.cpp revision 99c4a8a6b3852c33828b03cbd0aef0c625957a39
1/*
2 * Copyright (C) 2007-2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <fnmatch.h>
19#include <stddef.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24
25#include <fcntl.h>
26#include <dirent.h>
27#include <unistd.h>
28#include <string.h>
29
30#include <sys/socket.h>
31#include <sys/un.h>
32#include <linux/netlink.h>
33
34#include <memory>
35
36#include <selinux/selinux.h>
37#include <selinux/label.h>
38#include <selinux/android.h>
39#include <selinux/avc.h>
40
41#include <private/android_filesystem_config.h>
42#include <sys/time.h>
43#include <sys/wait.h>
44
45#include <android-base/file.h>
46#include <android-base/stringprintf.h>
47#include <android-base/unique_fd.h>
48#include <cutils/list.h>
49#include <cutils/uevent.h>
50
51#include "devices.h"
52#include "ueventd_parser.h"
53#include "util.h"
54#include "log.h"
55
56#define SYSFS_PREFIX    "/sys"
57static const char *firmware_dirs[] = { "/etc/firmware",
58                                       "/vendor/firmware",
59                                       "/firmware/image" };
60
61extern struct selabel_handle *sehandle;
62
63static int device_fd = -1;
64
65struct uevent {
66    const char *action;
67    const char *path;
68    const char *subsystem;
69    const char *firmware;
70    const char *partition_name;
71    const char *device_name;
72    int partition_num;
73    int major;
74    int minor;
75};
76
77struct perms_ {
78    char *name;
79    char *attr;
80    mode_t perm;
81    unsigned int uid;
82    unsigned int gid;
83    unsigned short prefix;
84    unsigned short wildcard;
85};
86
87struct perm_node {
88    struct perms_ dp;
89    struct listnode plist;
90};
91
92struct platform_node {
93    char *name;
94    char *path;
95    int path_len;
96    struct listnode list;
97};
98
99static list_declare(sys_perms);
100static list_declare(dev_perms);
101static list_declare(platform_names);
102
103int add_dev_perms(const char *name, const char *attr,
104                  mode_t perm, unsigned int uid, unsigned int gid,
105                  unsigned short prefix,
106                  unsigned short wildcard) {
107    struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
108    if (!node)
109        return -ENOMEM;
110
111    node->dp.name = strdup(name);
112    if (!node->dp.name)
113        return -ENOMEM;
114
115    if (attr) {
116        node->dp.attr = strdup(attr);
117        if (!node->dp.attr)
118            return -ENOMEM;
119    }
120
121    node->dp.perm = perm;
122    node->dp.uid = uid;
123    node->dp.gid = gid;
124    node->dp.prefix = prefix;
125    node->dp.wildcard = wildcard;
126
127    if (attr)
128        list_add_tail(&sys_perms, &node->plist);
129    else
130        list_add_tail(&dev_perms, &node->plist);
131
132    return 0;
133}
134
135static bool perm_path_matches(const char *path, struct perms_ *dp)
136{
137    if (dp->prefix) {
138        if (strncmp(path, dp->name, strlen(dp->name)) == 0)
139            return true;
140    } else if (dp->wildcard) {
141        if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
142            return true;
143    } else {
144        if (strcmp(path, dp->name) == 0)
145            return true;
146    }
147
148    return false;
149}
150
151static bool match_subsystem(perms_* dp, const char* pattern,
152                            const char* path, const char* subsystem) {
153    if (!pattern || !subsystem || strstr(dp->name, subsystem) == NULL) {
154        return false;
155    }
156
157    std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path));
158    return perm_path_matches(subsys_path.c_str(), dp);
159}
160
161static void fixup_sys_perms(const char* upath, const char* subsystem) {
162    // upaths omit the "/sys" that paths in this list
163    // contain, so we prepend it...
164    std::string path = std::string(SYSFS_PREFIX) + upath;
165
166    listnode* node;
167    list_for_each(node, &sys_perms) {
168        perms_* dp = &(node_to_item(node, perm_node, plist))->dp;
169        if (match_subsystem(dp, SYSFS_PREFIX "/class/%s/%s", path.c_str(), subsystem)) {
170            ; // matched
171        } else if (match_subsystem(dp, SYSFS_PREFIX "/bus/%s/devices/%s", path.c_str(), subsystem)) {
172            ; // matched
173        } else if (!perm_path_matches(path.c_str(), dp)) {
174            continue;
175        }
176
177        std::string attr_file = path + "/" + dp->attr;
178        LOG(INFO) << "fixup " << attr_file
179                  << " " << dp->uid << " " << dp->gid << " " << std::oct << dp->perm;
180        chown(attr_file.c_str(), dp->uid, dp->gid);
181        chmod(attr_file.c_str(), dp->perm);
182    }
183
184    if (access(path.c_str(), F_OK) == 0) {
185        LOG(VERBOSE) << "restorecon_recursive: " << path;
186        restorecon_recursive(path.c_str());
187    }
188}
189
190static mode_t get_device_perm(const char *path, const char **links,
191                unsigned *uid, unsigned *gid)
192{
193    struct listnode *node;
194    struct perm_node *perm_node;
195    struct perms_ *dp;
196
197    /* search the perms list in reverse so that ueventd.$hardware can
198     * override ueventd.rc
199     */
200    list_for_each_reverse(node, &dev_perms) {
201        bool match = false;
202
203        perm_node = node_to_item(node, struct perm_node, plist);
204        dp = &perm_node->dp;
205
206        if (perm_path_matches(path, dp)) {
207            match = true;
208        } else {
209            if (links) {
210                int i;
211                for (i = 0; links[i]; i++) {
212                    if (perm_path_matches(links[i], dp)) {
213                        match = true;
214                        break;
215                    }
216                }
217            }
218        }
219
220        if (match) {
221            *uid = dp->uid;
222            *gid = dp->gid;
223            return dp->perm;
224        }
225    }
226    /* Default if nothing found. */
227    *uid = 0;
228    *gid = 0;
229    return 0600;
230}
231
232static void make_device(const char *path,
233                        const char */*upath*/,
234                        int block, int major, int minor,
235                        const char **links)
236{
237    unsigned uid;
238    unsigned gid;
239    mode_t mode;
240    dev_t dev;
241    char *secontext = NULL;
242
243    mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
244
245    if (selabel_lookup_best_match(sehandle, &secontext, path, links, mode)) {
246        PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
247        return;
248    }
249    setfscreatecon(secontext);
250
251    dev = makedev(major, minor);
252    /* Temporarily change egid to avoid race condition setting the gid of the
253     * device node. Unforunately changing the euid would prevent creation of
254     * some device nodes, so the uid has to be set with chown() and is still
255     * racy. Fixing the gid race at least fixed the issue with system_server
256     * opening dynamic input devices under the AID_INPUT gid. */
257    setegid(gid);
258    /* If the node already exists update its SELinux label to handle cases when
259     * it was created with the wrong context during coldboot procedure. */
260    if (mknod(path, mode, dev) && (errno == EEXIST)) {
261
262        char* fcon = nullptr;
263        int rc = lgetfilecon(path, &fcon);
264        if (rc < 0) {
265            PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
266            goto out;
267        }
268
269        bool different = strcmp(fcon, secontext) != 0;
270        freecon(fcon);
271
272        if (different && lsetfilecon(path, secontext)) {
273            PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path << "' device";
274        }
275    }
276
277out:
278    chown(path, uid, -1);
279    setegid(AID_ROOT);
280
281    freecon(secontext);
282    setfscreatecon(NULL);
283}
284
285static void add_platform_device(const char *path)
286{
287    int path_len = strlen(path);
288    struct platform_node *bus;
289    const char *name = path;
290
291    if (!strncmp(path, "/devices/", 9)) {
292        name += 9;
293        if (!strncmp(name, "platform/", 9))
294            name += 9;
295    }
296
297    LOG(INFO) << "adding platform device " << name << " (" << path << ")";
298
299    bus = (platform_node*) calloc(1, sizeof(struct platform_node));
300    bus->path = strdup(path);
301    bus->path_len = path_len;
302    bus->name = bus->path + (name - path);
303    list_add_tail(&platform_names, &bus->list);
304}
305
306/*
307 * given a path that may start with a platform device, find the length of the
308 * platform device prefix.  If it doesn't start with a platform device, return
309 * 0.
310 */
311static struct platform_node *find_platform_device(const char *path)
312{
313    int path_len = strlen(path);
314    struct listnode *node;
315    struct platform_node *bus;
316
317    list_for_each_reverse(node, &platform_names) {
318        bus = node_to_item(node, struct platform_node, list);
319        if ((bus->path_len < path_len) &&
320                (path[bus->path_len] == '/') &&
321                !strncmp(path, bus->path, bus->path_len))
322            return bus;
323    }
324
325    return NULL;
326}
327
328static void remove_platform_device(const char *path)
329{
330    struct listnode *node;
331    struct platform_node *bus;
332
333    list_for_each_reverse(node, &platform_names) {
334        bus = node_to_item(node, struct platform_node, list);
335        if (!strcmp(path, bus->path)) {
336            LOG(INFO) << "removing platform device " << bus->name;
337            free(bus->path);
338            list_remove(node);
339            free(bus);
340            return;
341        }
342    }
343}
344
345/* Given a path that may start with a PCI device, populate the supplied buffer
346 * with the PCI domain/bus number and the peripheral ID and return 0.
347 * If it doesn't start with a PCI device, or there is some error, return -1 */
348static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
349{
350    const char *start, *end;
351
352    if (strncmp(path, "/devices/pci", 12))
353        return -1;
354
355    /* Beginning of the prefix is the initial "pci" after "/devices/" */
356    start = path + 9;
357
358    /* End of the prefix is two path '/' later, capturing the domain/bus number
359     * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
360    end = strchr(start, '/');
361    if (!end)
362        return -1;
363    end = strchr(end + 1, '/');
364    if (!end)
365        return -1;
366
367    /* Make sure we have enough room for the string plus null terminator */
368    if (end - start + 1 > buf_sz)
369        return -1;
370
371    strncpy(buf, start, end - start);
372    buf[end - start] = '\0';
373    return 0;
374}
375
376static void parse_event(const char *msg, struct uevent *uevent)
377{
378    uevent->action = "";
379    uevent->path = "";
380    uevent->subsystem = "";
381    uevent->firmware = "";
382    uevent->major = -1;
383    uevent->minor = -1;
384    uevent->partition_name = NULL;
385    uevent->partition_num = -1;
386    uevent->device_name = NULL;
387
388        /* currently ignoring SEQNUM */
389    while(*msg) {
390        if(!strncmp(msg, "ACTION=", 7)) {
391            msg += 7;
392            uevent->action = msg;
393        } else if(!strncmp(msg, "DEVPATH=", 8)) {
394            msg += 8;
395            uevent->path = msg;
396        } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
397            msg += 10;
398            uevent->subsystem = msg;
399        } else if(!strncmp(msg, "FIRMWARE=", 9)) {
400            msg += 9;
401            uevent->firmware = msg;
402        } else if(!strncmp(msg, "MAJOR=", 6)) {
403            msg += 6;
404            uevent->major = atoi(msg);
405        } else if(!strncmp(msg, "MINOR=", 6)) {
406            msg += 6;
407            uevent->minor = atoi(msg);
408        } else if(!strncmp(msg, "PARTN=", 6)) {
409            msg += 6;
410            uevent->partition_num = atoi(msg);
411        } else if(!strncmp(msg, "PARTNAME=", 9)) {
412            msg += 9;
413            uevent->partition_name = msg;
414        } else if(!strncmp(msg, "DEVNAME=", 8)) {
415            msg += 8;
416            uevent->device_name = msg;
417        }
418
419        /* advance to after the next \0 */
420        while(*msg++)
421            ;
422    }
423
424    if (LOG_UEVENTS) {
425        LOG(INFO) << android::base::StringPrintf("event { '%s', '%s', '%s', '%s', %d, %d }",
426                                                 uevent->action, uevent->path, uevent->subsystem,
427                                                 uevent->firmware, uevent->major, uevent->minor);
428    }
429}
430
431static char **get_character_device_symlinks(struct uevent *uevent)
432{
433    const char *parent;
434    const char *slash;
435    char **links;
436    int link_num = 0;
437    int width;
438    struct platform_node *pdev;
439
440    pdev = find_platform_device(uevent->path);
441    if (!pdev)
442        return NULL;
443
444    links = (char**) malloc(sizeof(char *) * 2);
445    if (!links)
446        return NULL;
447    memset(links, 0, sizeof(char *) * 2);
448
449    /* skip "/devices/platform/<driver>" */
450    parent = strchr(uevent->path + pdev->path_len, '/');
451    if (!parent)
452        goto err;
453
454    if (!strncmp(parent, "/usb", 4)) {
455        /* skip root hub name and device. use device interface */
456        while (*++parent && *parent != '/');
457        if (*parent)
458            while (*++parent && *parent != '/');
459        if (!*parent)
460            goto err;
461        slash = strchr(++parent, '/');
462        if (!slash)
463            goto err;
464        width = slash - parent;
465        if (width <= 0)
466            goto err;
467
468        if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
469            link_num++;
470        else
471            links[link_num] = NULL;
472        mkdir("/dev/usb", 0755);
473    }
474    else {
475        goto err;
476    }
477
478    return links;
479err:
480    free(links);
481    return NULL;
482}
483
484static char **get_block_device_symlinks(struct uevent *uevent)
485{
486    const char *device;
487    struct platform_node *pdev;
488    const char *slash;
489    const char *type;
490    char buf[256];
491    char link_path[256];
492    int link_num = 0;
493    char *p;
494
495    pdev = find_platform_device(uevent->path);
496    if (pdev) {
497        device = pdev->name;
498        type = "platform";
499    } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
500        device = buf;
501        type = "pci";
502    } else {
503        return NULL;
504    }
505
506    char **links = (char**) malloc(sizeof(char *) * 4);
507    if (!links)
508        return NULL;
509    memset(links, 0, sizeof(char *) * 4);
510
511    LOG(INFO) << "found " << type << " device " << device;
512
513    snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
514
515    if (uevent->partition_name) {
516        p = strdup(uevent->partition_name);
517        sanitize(p);
518        if (strcmp(uevent->partition_name, p)) {
519            LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '" << p << "'";
520        }
521        if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
522            link_num++;
523        else
524            links[link_num] = NULL;
525        free(p);
526    }
527
528    if (uevent->partition_num >= 0) {
529        if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
530            link_num++;
531        else
532            links[link_num] = NULL;
533    }
534
535    slash = strrchr(uevent->path, '/');
536    if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
537        link_num++;
538    else
539        links[link_num] = NULL;
540
541    return links;
542}
543
544static void handle_device(const char *action, const char *devpath,
545        const char *path, int block, int major, int minor, char **links)
546{
547    int i;
548
549    if(!strcmp(action, "add")) {
550        make_device(devpath, path, block, major, minor, (const char **)links);
551        if (links) {
552            for (i = 0; links[i]; i++)
553                make_link_init(devpath, links[i]);
554        }
555    }
556
557    if(!strcmp(action, "remove")) {
558        if (links) {
559            for (i = 0; links[i]; i++)
560                remove_link(devpath, links[i]);
561        }
562        unlink(devpath);
563    }
564
565    if (links) {
566        for (i = 0; links[i]; i++)
567            free(links[i]);
568        free(links);
569    }
570}
571
572static void handle_platform_device_event(struct uevent *uevent)
573{
574    const char *path = uevent->path;
575
576    if (!strcmp(uevent->action, "add"))
577        add_platform_device(path);
578    else if (!strcmp(uevent->action, "remove"))
579        remove_platform_device(path);
580}
581
582static const char *parse_device_name(struct uevent *uevent, unsigned int len)
583{
584    const char *name;
585
586    /* if it's not a /dev device, nothing else to do */
587    if((uevent->major < 0) || (uevent->minor < 0))
588        return NULL;
589
590    /* do we have a name? */
591    name = strrchr(uevent->path, '/');
592    if(!name)
593        return NULL;
594    name++;
595
596    /* too-long names would overrun our buffer */
597    if(strlen(name) > len) {
598        LOG(ERROR) << "DEVPATH=" << name << " exceeds " << len << "-character limit on filename; ignoring event";
599        return NULL;
600    }
601
602    return name;
603}
604
605#define DEVPATH_LEN 96
606#define MAX_DEV_NAME 64
607
608static void handle_block_device_event(struct uevent *uevent)
609{
610    const char *base = "/dev/block/";
611    const char *name;
612    char devpath[DEVPATH_LEN];
613    char **links = NULL;
614
615    name = parse_device_name(uevent, MAX_DEV_NAME);
616    if (!name)
617        return;
618
619    snprintf(devpath, sizeof(devpath), "%s%s", base, name);
620    make_dir(base, 0755);
621
622    if (!strncmp(uevent->path, "/devices/", 9))
623        links = get_block_device_symlinks(uevent);
624
625    handle_device(uevent->action, devpath, uevent->path, 1,
626            uevent->major, uevent->minor, links);
627}
628
629static bool assemble_devpath(char *devpath, const char *dirname,
630        const char *devname)
631{
632    int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
633    if (s < 0) {
634        PLOG(ERROR) << "failed to assemble device path; ignoring event";
635        return false;
636    } else if (s >= DEVPATH_LEN) {
637        LOG(ERROR) << dirname << "/" << devname
638                   << " exceeds " << DEVPATH_LEN << "-character limit on path; ignoring event";
639        return false;
640    }
641    return true;
642}
643
644static void mkdir_recursive_for_devpath(const char *devpath)
645{
646    char dir[DEVPATH_LEN];
647    char *slash;
648
649    strcpy(dir, devpath);
650    slash = strrchr(dir, '/');
651    *slash = '\0';
652    mkdir_recursive(dir, 0755);
653}
654
655static void handle_generic_device_event(struct uevent *uevent)
656{
657    const char *base;
658    const char *name;
659    char devpath[DEVPATH_LEN] = {0};
660    char **links = NULL;
661
662    name = parse_device_name(uevent, MAX_DEV_NAME);
663    if (!name)
664        return;
665
666    struct ueventd_subsystem *subsystem =
667            ueventd_subsystem_find_by_name(uevent->subsystem);
668
669    if (subsystem) {
670        const char *devname;
671
672        switch (subsystem->devname_src) {
673        case DEVNAME_UEVENT_DEVNAME:
674            devname = uevent->device_name;
675            break;
676
677        case DEVNAME_UEVENT_DEVPATH:
678            devname = name;
679            break;
680
681        default:
682            LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event";
683            return;
684        }
685
686        if (!assemble_devpath(devpath, subsystem->dirname, devname))
687            return;
688        mkdir_recursive_for_devpath(devpath);
689    } else if (!strncmp(uevent->subsystem, "usb", 3)) {
690         if (!strcmp(uevent->subsystem, "usb")) {
691            if (uevent->device_name) {
692                if (!assemble_devpath(devpath, "/dev", uevent->device_name))
693                    return;
694                mkdir_recursive_for_devpath(devpath);
695             }
696             else {
697                 /* This imitates the file system that would be created
698                  * if we were using devfs instead.
699                  * Minors are broken up into groups of 128, starting at "001"
700                  */
701                 int bus_id = uevent->minor / 128 + 1;
702                 int device_id = uevent->minor % 128 + 1;
703                 /* build directories */
704                 make_dir("/dev/bus", 0755);
705                 make_dir("/dev/bus/usb", 0755);
706                 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
707                 make_dir(devpath, 0755);
708                 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
709             }
710         } else {
711             /* ignore other USB events */
712             return;
713         }
714     } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
715         base = "/dev/graphics/";
716         make_dir(base, 0755);
717     } else if (!strncmp(uevent->subsystem, "drm", 3)) {
718         base = "/dev/dri/";
719         make_dir(base, 0755);
720     } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
721         base = "/dev/oncrpc/";
722         make_dir(base, 0755);
723     } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
724         base = "/dev/adsp/";
725         make_dir(base, 0755);
726     } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
727         base = "/dev/msm_camera/";
728         make_dir(base, 0755);
729     } else if(!strncmp(uevent->subsystem, "input", 5)) {
730         base = "/dev/input/";
731         make_dir(base, 0755);
732     } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
733         base = "/dev/mtd/";
734         make_dir(base, 0755);
735     } else if(!strncmp(uevent->subsystem, "sound", 5)) {
736         base = "/dev/snd/";
737         make_dir(base, 0755);
738     } else if(!strncmp(uevent->subsystem, "misc", 4) && !strncmp(name, "log_", 4)) {
739         LOG(INFO) << "kernel logger is deprecated";
740         base = "/dev/log/";
741         make_dir(base, 0755);
742         name += 4;
743     } else
744         base = "/dev/";
745     links = get_character_device_symlinks(uevent);
746
747     if (!devpath[0])
748         snprintf(devpath, sizeof(devpath), "%s%s", base, name);
749
750     handle_device(uevent->action, devpath, uevent->path, 0,
751             uevent->major, uevent->minor, links);
752}
753
754static void handle_device_event(struct uevent *uevent)
755{
756    if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
757        fixup_sys_perms(uevent->path, uevent->subsystem);
758
759    if (!strncmp(uevent->subsystem, "block", 5)) {
760        handle_block_device_event(uevent);
761    } else if (!strncmp(uevent->subsystem, "platform", 8)) {
762        handle_platform_device_event(uevent);
763    } else {
764        handle_generic_device_event(uevent);
765    }
766}
767
768static int load_firmware(int fw_fd, int loading_fd, int data_fd)
769{
770    struct stat st;
771    long len_to_copy;
772    int ret = 0;
773
774    if(fstat(fw_fd, &st) < 0)
775        return -1;
776    len_to_copy = st.st_size;
777
778    write(loading_fd, "1", 1);  /* start transfer */
779
780    while (len_to_copy > 0) {
781        char buf[PAGE_SIZE];
782        ssize_t nr;
783
784        nr = read(fw_fd, buf, sizeof(buf));
785        if(!nr)
786            break;
787        if(nr < 0) {
788            ret = -1;
789            break;
790        }
791        if (!android::base::WriteFully(data_fd, buf, nr)) {
792            ret = -1;
793            break;
794        }
795        len_to_copy -= nr;
796    }
797
798    if(!ret)
799        write(loading_fd, "0", 1);  /* successful end of transfer */
800    else
801        write(loading_fd, "-1", 2); /* abort transfer */
802
803    return ret;
804}
805
806static int is_booting(void)
807{
808    return access("/dev/.booting", F_OK) == 0;
809}
810
811static void process_firmware_event(struct uevent *uevent)
812{
813    char *root, *loading, *data;
814    int l, loading_fd, data_fd, fw_fd;
815    size_t i;
816    int booting = is_booting();
817
818    LOG(INFO) << "firmware: loading '" << uevent->firmware << "' for '" << uevent->path << "'";
819
820    l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
821    if (l == -1)
822        return;
823
824    l = asprintf(&loading, "%sloading", root);
825    if (l == -1)
826        goto root_free_out;
827
828    l = asprintf(&data, "%sdata", root);
829    if (l == -1)
830        goto loading_free_out;
831
832    loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
833    if(loading_fd < 0)
834        goto data_free_out;
835
836    data_fd = open(data, O_WRONLY|O_CLOEXEC);
837    if(data_fd < 0)
838        goto loading_close_out;
839
840try_loading_again:
841    for (i = 0; i < arraysize(firmware_dirs); i++) {
842        char *file = NULL;
843        l = asprintf(&file, "%s/%s", firmware_dirs[i], uevent->firmware);
844        if (l == -1)
845            goto data_free_out;
846        fw_fd = open(file, O_RDONLY|O_CLOEXEC);
847        free(file);
848        if (fw_fd >= 0) {
849            if (!load_firmware(fw_fd, loading_fd, data_fd)) {
850                LOG(INFO) << "firmware: copy success { '" << root << "', '" << uevent->firmware << "' }";
851            } else {
852                LOG(ERROR) << "firmware: copy failure { '" << root << "', '" << uevent->firmware << "' }";
853            }
854            break;
855        }
856    }
857    if (fw_fd < 0) {
858        if (booting) {
859            /* If we're not fully booted, we may be missing
860             * filesystems needed for firmware, wait and retry.
861             */
862            usleep(100000);
863            booting = is_booting();
864            goto try_loading_again;
865        }
866        PLOG(ERROR) << "firmware: could not open '" << uevent->firmware << "'";
867        write(loading_fd, "-1", 2);
868        goto data_close_out;
869    }
870
871    close(fw_fd);
872data_close_out:
873    close(data_fd);
874loading_close_out:
875    close(loading_fd);
876data_free_out:
877    free(data);
878loading_free_out:
879    free(loading);
880root_free_out:
881    free(root);
882}
883
884static void handle_firmware_event(struct uevent *uevent)
885{
886    pid_t pid;
887
888    if(strcmp(uevent->subsystem, "firmware"))
889        return;
890
891    if(strcmp(uevent->action, "add"))
892        return;
893
894    /* we fork, to avoid making large memory allocations in init proper */
895    pid = fork();
896    if (!pid) {
897        process_firmware_event(uevent);
898        _exit(EXIT_SUCCESS);
899    } else if (pid < 0) {
900        PLOG(ERROR) << "could not fork to process firmware event";
901    }
902}
903
904#define UEVENT_MSG_LEN  2048
905
906static inline void handle_device_fd_with(void (handle_uevent)(struct uevent*))
907{
908    char msg[UEVENT_MSG_LEN+2];
909    int n;
910    while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
911        if(n >= UEVENT_MSG_LEN)   /* overflow -- discard */
912            continue;
913
914        msg[n] = '\0';
915        msg[n+1] = '\0';
916
917        struct uevent uevent;
918        parse_event(msg, &uevent);
919        handle_uevent(&uevent);
920    }
921}
922
923void handle_device_fd()
924{
925    handle_device_fd_with(
926        [](struct uevent *uevent) {
927            if (selinux_status_updated() > 0) {
928                struct selabel_handle *sehandle2;
929                sehandle2 = selinux_android_file_context_handle();
930                if (sehandle2) {
931                    selabel_close(sehandle);
932                    sehandle = sehandle2;
933                }
934            }
935
936            handle_device_event(uevent);
937            handle_firmware_event(uevent);
938        });
939}
940
941/* Coldboot walks parts of the /sys tree and pokes the uevent files
942** to cause the kernel to regenerate device add events that happened
943** before init's device manager was started
944**
945** We drain any pending events from the netlink socket every time
946** we poke another uevent file to make sure we don't overrun the
947** socket's buffer.
948*/
949
950static void do_coldboot(DIR *d)
951{
952    struct dirent *de;
953    int dfd, fd;
954
955    dfd = dirfd(d);
956
957    fd = openat(dfd, "uevent", O_WRONLY);
958    if(fd >= 0) {
959        write(fd, "add\n", 4);
960        close(fd);
961        handle_device_fd();
962    }
963
964    while((de = readdir(d))) {
965        DIR *d2;
966
967        if(de->d_type != DT_DIR || de->d_name[0] == '.')
968            continue;
969
970        fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
971        if(fd < 0)
972            continue;
973
974        d2 = fdopendir(fd);
975        if(d2 == 0)
976            close(fd);
977        else {
978            do_coldboot(d2);
979            closedir(d2);
980        }
981    }
982}
983
984static void coldboot(const char *path)
985{
986    std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
987    if(d) {
988        do_coldboot(d.get());
989    }
990}
991
992static void early_uevent_handler(struct uevent *uevent, const char *base, bool is_block)
993{
994    const char *name;
995    char devpath[DEVPATH_LEN];
996
997    if (is_block && strncmp(uevent->subsystem, "block", 5))
998        return;
999
1000    name = parse_device_name(uevent, MAX_DEV_NAME);
1001    if (!name) {
1002        LOG(ERROR) << "Failed to parse dev name from uevent: " << uevent->action
1003                   << " " << uevent->partition_name << " " << uevent->partition_num
1004                   << " " << uevent->major << ":" << uevent->minor;
1005        return;
1006    }
1007
1008    snprintf(devpath, sizeof(devpath), "%s%s", base, name);
1009    make_dir(base, 0755);
1010
1011    dev_t dev = makedev(uevent->major, uevent->minor);
1012    mode_t mode = 0600 | (is_block ? S_IFBLK : S_IFCHR);
1013    mknod(devpath, mode, dev);
1014}
1015
1016void early_create_dev(const std::string& syspath, early_device_type dev_type)
1017{
1018    android::base::unique_fd dfd(open(syspath.c_str(), O_RDONLY));
1019    if (dfd < 0) {
1020        LOG(ERROR) << "Failed to open " << syspath;
1021        return;
1022    }
1023
1024    android::base::unique_fd fd(openat(dfd, "uevent", O_WRONLY));
1025    if (fd < 0) {
1026        LOG(ERROR) << "Failed to open " << syspath << "/uevent";
1027        return;
1028    }
1029
1030    fcntl(device_fd, F_SETFL, O_NONBLOCK);
1031
1032    write(fd, "add\n", 4);
1033    handle_device_fd_with(dev_type == EARLY_BLOCK_DEV ?
1034        [](struct uevent *uevent) {
1035            early_uevent_handler(uevent, "/dev/block/", true);
1036        } :
1037        [](struct uevent *uevent) {
1038            early_uevent_handler(uevent, "/dev/", false);
1039        });
1040}
1041
1042int early_device_socket_open() {
1043    device_fd = uevent_open_socket(256*1024, true);
1044    return device_fd < 0;
1045}
1046
1047void early_device_socket_close() {
1048    close(device_fd);
1049}
1050
1051void device_init() {
1052    sehandle = selinux_android_file_context_handle();
1053    selinux_status_open(true);
1054
1055    /* is 256K enough? udev uses 16MB! */
1056    device_fd = uevent_open_socket(256*1024, true);
1057    if (device_fd == -1) {
1058        return;
1059    }
1060    fcntl(device_fd, F_SETFL, O_NONBLOCK);
1061
1062    if (access(COLDBOOT_DONE, F_OK) == 0) {
1063        LOG(VERBOSE) << "Skipping coldboot, already done!";
1064        return;
1065    }
1066
1067    Timer t;
1068    coldboot("/sys/class");
1069    coldboot("/sys/block");
1070    coldboot("/sys/devices");
1071    close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
1072    LOG(INFO) << "Coldboot took " << t.duration() << "s.";
1073}
1074
1075int get_device_fd()
1076{
1077    return device_fd;
1078}
1079