devices.c 23.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (C) 2007 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 <errno.h>
18
#include <stddef.h>
19 20 21 22 23 24 25 26 27 28 29 30 31
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <fcntl.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>

#include <sys/socket.h>
#include <sys/un.h>
#include <linux/netlink.h>
32 33 34

#include <selinux/selinux.h>
#include <selinux/label.h>
35
#include <selinux/android.h>
36

37 38 39
#include <private/android_filesystem_config.h>
#include <sys/time.h>
#include <asm/page.h>
40
#include <sys/wait.h>
41

42
#include <cutils/list.h>
43 44
#include <cutils/uevent.h>

45
#include "devices.h"
46
#include "util.h"
47
#include "log.h"
48 49

#define SYSFS_PREFIX    "/sys"
Brian Swetland's avatar
Brian Swetland committed
50 51
#define FIRMWARE_DIR1   "/etc/firmware"
#define FIRMWARE_DIR2   "/vendor/firmware"
52
#define FIRMWARE_DIR3   "/firmware/image"
53

54
extern struct selabel_handle *sehandle;
55

56 57
static int device_fd = -1;

58 59 60 61 62
struct uevent {
    const char *action;
    const char *path;
    const char *subsystem;
    const char *firmware;
63
    const char *partition_name;
64
    const char *device_name;
65
    int partition_num;
66 67 68 69 70 71
    int major;
    int minor;
};

struct perms_ {
    char *name;
72
    char *attr;
73 74 75 76 77 78 79 80 81 82
    mode_t perm;
    unsigned int uid;
    unsigned int gid;
    unsigned short prefix;
};

struct perm_node {
    struct perms_ dp;
    struct listnode plist;
};
83

84 85
struct platform_node {
    char *name;
86 87
    char *path;
    int path_len;
88 89 90
    struct listnode list;
};

91
static list_declare(sys_perms);
92
static list_declare(dev_perms);
93
static list_declare(platform_names);
94

95 96 97 98
int add_dev_perms(const char *name, const char *attr,
                  mode_t perm, unsigned int uid, unsigned int gid,
                  unsigned short prefix) {
    struct perm_node *node = calloc(1, sizeof(*node));
99 100 101
    if (!node)
        return -ENOMEM;

102 103
    node->dp.name = strdup(name);
    if (!node->dp.name)
104 105
        return -ENOMEM;

106 107 108 109 110 111
    if (attr) {
        node->dp.attr = strdup(attr);
        if (!node->dp.attr)
            return -ENOMEM;
    }

112 113 114 115 116
    node->dp.perm = perm;
    node->dp.uid = uid;
    node->dp.gid = gid;
    node->dp.prefix = prefix;

117 118 119 120 121
    if (attr)
        list_add_tail(&sys_perms, &node->plist);
    else
        list_add_tail(&dev_perms, &node->plist);

122 123 124
    return 0;
}

125
void fixup_sys_perms(const char *upath)
126
{
127 128 129
    char buf[512];
    struct listnode *node;
    struct perms_ *dp;
130

131 132 133 134 135 136 137
        /* upaths omit the "/sys" that paths in this list
         * contain, so we add 4 when comparing...
         */
    list_for_each(node, &sys_perms) {
        dp = &(node_to_item(node, struct perm_node, plist))->dp;
        if (dp->prefix) {
            if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
138 139
                continue;
        } else {
140
            if (strcmp(upath, dp->name + 4))
141 142
                continue;
        }
143 144 145 146 147 148 149 150

        if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
            return;

        sprintf(buf,"/sys%s/%s", upath, dp->attr);
        INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
        chown(buf, dp->uid, dp->gid);
        chmod(buf, dp->perm);
151 152 153 154 155 156
    }
}

static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
{
    mode_t perm;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    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) {
        perm_node = node_to_item(node, struct perm_node, plist);
        dp = &perm_node->dp;

        if (dp->prefix) {
            if (strncmp(path, dp->name, strlen(dp->name)))
                continue;
        } else {
            if (strcmp(path, dp->name))
                continue;
174
        }
175 176 177
        *uid = dp->uid;
        *gid = dp->gid;
        return dp->perm;
178
    }
179 180 181 182
    /* Default if nothing found. */
    *uid = 0;
    *gid = 0;
    return 0600;
183 184
}

185 186 187
static void make_device(const char *path,
                        const char *upath,
                        int block, int major, int minor)
188 189 190 191 192
{
    unsigned uid;
    unsigned gid;
    mode_t mode;
    dev_t dev;
193
    char *secontext = NULL;
194 195

    mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Root's avatar
Kenny Root committed
196

197 198 199 200
    if (sehandle) {
        selabel_lookup(sehandle, &secontext, path, mode);
        setfscreatecon(secontext);
    }
Kenny Root's avatar
Kenny Root committed
201

202
    dev = makedev(major, minor);
203 204 205 206 207 208
    /* 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);
209
    mknod(path, mode, dev);
210 211
    chown(path, uid, -1);
    setegid(AID_ROOT);
Kenny Root's avatar
Kenny Root committed
212

213 214 215 216 217 218
    if (secontext) {
        freecon(secontext);
        setfscreatecon(NULL);
    }
}

219
static void add_platform_device(const char *path)
220
{
221
    int path_len = strlen(path);
222 223
    struct listnode *node;
    struct platform_node *bus;
224 225 226 227 228 229 230
    const char *name = path;

    if (!strncmp(path, "/devices/", 9)) {
        name += 9;
        if (!strncmp(name, "platform/", 9))
            name += 9;
    }
231 232 233

    list_for_each_reverse(node, &platform_names) {
        bus = node_to_item(node, struct platform_node, list);
234 235 236
        if ((bus->path_len < path_len) &&
                (path[bus->path_len] == '/') &&
                !strncmp(path, bus->path, bus->path_len))
237 238 239 240
            /* subdevice of an existing platform, ignore it */
            return;
    }

241
    INFO("adding platform device %s (%s)\n", name, path);
242 243

    bus = calloc(1, sizeof(struct platform_node));
244 245 246
    bus->path = strdup(path);
    bus->path_len = path_len;
    bus->name = bus->path + (name - path);
247 248 249 250
    list_add_tail(&platform_names, &bus->list);
}

/*
251
 * given a path that may start with a platform device, find the length of the
252 253 254
 * platform device prefix.  If it doesn't start with a platform device, return
 * 0.
 */
255
static struct platform_node *find_platform_device(const char *path)
256
{
257
    int path_len = strlen(path);
258 259 260 261 262
    struct listnode *node;
    struct platform_node *bus;

    list_for_each_reverse(node, &platform_names) {
        bus = node_to_item(node, struct platform_node, list);
263 264 265 266
        if ((bus->path_len < path_len) &&
                (path[bus->path_len] == '/') &&
                !strncmp(path, bus->path, bus->path_len))
            return bus;
267 268 269 270 271
    }

    return NULL;
}

272
static void remove_platform_device(const char *path)
273 274 275 276 277 278
{
    struct listnode *node;
    struct platform_node *bus;

    list_for_each_reverse(node, &platform_names) {
        bus = node_to_item(node, struct platform_node, list);
279 280 281
        if (!strcmp(path, bus->path)) {
            INFO("removing platform device %s\n", bus->name);
            free(bus->path);
282 283 284 285 286 287 288
            list_remove(node);
            free(bus);
            return;
        }
    }
}

289
#if LOG_UEVENTS
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

static inline suseconds_t get_usecs(void)
{
    struct timeval tv;
    gettimeofday(&tv, 0);
    return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
}

#define log_event_print(x...) INFO(x)

#else

#define log_event_print(fmt, args...)   do { } while (0)
#define get_usecs()                     0

#endif

static void parse_event(const char *msg, struct uevent *uevent)
{
    uevent->action = "";
    uevent->path = "";
    uevent->subsystem = "";
    uevent->firmware = "";
    uevent->major = -1;
    uevent->minor = -1;
315 316
    uevent->partition_name = NULL;
    uevent->partition_num = -1;
317
    uevent->device_name = NULL;
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338

        /* 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);
339 340 341 342 343 344
        } 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;
345 346 347
        } else if(!strncmp(msg, "DEVNAME=", 8)) {
            msg += 8;
            uevent->device_name = msg;
348 349
        }

350
        /* advance to after the next \0 */
351 352 353 354 355 356 357 358 359
        while(*msg++)
            ;
    }

    log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
                    uevent->action, uevent->path, uevent->subsystem,
                    uevent->firmware, uevent->major, uevent->minor);
}

360 361 362 363 364 365 366
static char **get_character_device_symlinks(struct uevent *uevent)
{
    const char *parent;
    char *slash;
    char **links;
    int link_num = 0;
    int width;
367
    struct platform_node *pdev;
368

369 370
    pdev = find_platform_device(uevent->path);
    if (!pdev)
371 372 373 374 375 376 377 378
        return NULL;

    links = malloc(sizeof(char *) * 2);
    if (!links)
        return NULL;
    memset(links, 0, sizeof(char *) * 2);

    /* skip "/devices/platform/<driver>" */
379
    parent = strchr(uevent->path + pdev->path_len, '/');
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
    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;
}

413 414
static char **parse_platform_block_device(struct uevent *uevent)
{
415
    const char *device;
416
    struct platform_node *pdev;
417 418 419 420 421 422 423 424 425 426 427
    char *slash;
    int width;
    char buf[256];
    char link_path[256];
    int fd;
    int link_num = 0;
    int ret;
    char *p;
    unsigned int size;
    struct stat info;

428 429 430 431 432
    pdev = find_platform_device(uevent->path);
    if (!pdev)
        return NULL;
    device = pdev->name;

433 434 435 436 437
    char **links = malloc(sizeof(char *) * 4);
    if (!links)
        return NULL;
    memset(links, 0, sizeof(char *) * 4);

438 439 440
    INFO("found platform device %s\n", device);

    snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device);
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458

    if (uevent->partition_name) {
        p = strdup(uevent->partition_name);
        sanitize(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;
    }

459
    slash = strrchr(uevent->path, '/');
460 461 462 463 464 465 466 467
    if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
        link_num++;
    else
        links[link_num] = NULL;

    return links;
}

468 469
static void handle_device(const char *action, const char *devpath,
        const char *path, int block, int major, int minor, char **links)
470
{
471
    int i;
472

473 474
    if(!strcmp(action, "add")) {
        make_device(devpath, path, block, major, minor);
475 476 477 478
        if (links) {
            for (i = 0; links[i]; i++)
                make_link(devpath, links[i]);
        }
479 480
    }

481
    if(!strcmp(action, "remove")) {
482 483 484 485
        if (links) {
            for (i = 0; links[i]; i++)
                remove_link(devpath, links[i]);
        }
486
        unlink(devpath);
487 488 489 490 491 492
    }

    if (links) {
        for (i = 0; links[i]; i++)
            free(links[i]);
        free(links);
493 494 495
    }
}

496 497
static void handle_platform_device_event(struct uevent *uevent)
{
498
    const char *path = uevent->path;
499 500

    if (!strcmp(uevent->action, "add"))
501
        add_platform_device(path);
502
    else if (!strcmp(uevent->action, "remove"))
503
        remove_platform_device(path);
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
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)
        return NULL;

    return name;
}

static void handle_block_device_event(struct uevent *uevent)
{
    const char *base = "/dev/block/";
    const char *name;
    char devpath[96];
    char **links = NULL;

    name = parse_device_name(uevent, 64);
    if (!name)
        return;

    snprintf(devpath, sizeof(devpath), "%s%s", base, name);
539
    make_dir(base, 0755);
540

541
    if (!strncmp(uevent->path, "/devices/", 9))
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
        links = parse_platform_block_device(uevent);

    handle_device(uevent->action, devpath, uevent->path, 1,
            uevent->major, uevent->minor, links);
}

static void handle_generic_device_event(struct uevent *uevent)
{
    char *base;
    const char *name;
    char devpath[96] = {0};
    char **links = NULL;

    name = parse_device_name(uevent, 64);
    if (!name)
        return;

    if (!strncmp(uevent->subsystem, "usb", 3)) {
         if (!strcmp(uevent->subsystem, "usb")) {
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
            if (uevent->device_name) {
                /*
                 * create device node provided by kernel if present
                 * see drivers/base/core.c
                 */
                char *p = devpath;
                snprintf(devpath, sizeof(devpath), "/dev/%s", uevent->device_name);
                /* skip leading /dev/ */
                p += 5;
                /* build directories */
                while (*p) {
                    if (*p == '/') {
                        *p = 0;
                        make_dir(devpath, 0755);
                        *p = '/';
                    }
                    p++;
                }
             }
             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);
             }
594 595 596 597 598 599
         } else {
             /* ignore other USB events */
             return;
         }
     } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
         base = "/dev/graphics/";
600
         make_dir(base, 0755);
601 602 603
     } else if (!strncmp(uevent->subsystem, "drm", 3)) {
         base = "/dev/dri/";
         make_dir(base, 0755);
604 605
     } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
         base = "/dev/oncrpc/";
606
         make_dir(base, 0755);
607 608
     } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
         base = "/dev/adsp/";
609
         make_dir(base, 0755);
610 611
     } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
         base = "/dev/msm_camera/";
612
         make_dir(base, 0755);
613 614
     } else if(!strncmp(uevent->subsystem, "input", 5)) {
         base = "/dev/input/";
615
         make_dir(base, 0755);
616 617
     } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
         base = "/dev/mtd/";
618
         make_dir(base, 0755);
619 620
     } else if(!strncmp(uevent->subsystem, "sound", 5)) {
         base = "/dev/snd/";
621
         make_dir(base, 0755);
622 623 624
     } else if(!strncmp(uevent->subsystem, "misc", 4) &&
                 !strncmp(name, "log_", 4)) {
         base = "/dev/log/";
625
         make_dir(base, 0755);
626 627 628 629 630 631 632 633 634 635 636 637 638 639
         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)
{
640
    if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change"))
641 642 643 644
        fixup_sys_perms(uevent->path);

    if (!strncmp(uevent->subsystem, "block", 5)) {
        handle_block_device_event(uevent);
645 646
    } else if (!strncmp(uevent->subsystem, "platform", 8)) {
        handle_platform_device_event(uevent);
647 648 649 650 651
    } else {
        handle_generic_device_event(uevent);
    }
}

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
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;
        }

        len_to_copy -= nr;
        while (nr > 0) {
            ssize_t nw = 0;

            nw = write(data_fd, buf + nw, nr);
            if(nw <= 0) {
                ret = -1;
                goto out;
            }
            nr -= nw;
        }
    }

out:
    if(!ret)
        write(loading_fd, "0", 1);  /* successful end of transfer */
    else
        write(loading_fd, "-1", 2); /* abort transfer */

    return ret;
}

698 699 700 701 702
static int is_booting(void)
{
    return access("/dev/.booting", F_OK) == 0;
}

703 704
static void process_firmware_event(struct uevent *uevent)
{
705
    char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL;
706
    int l, loading_fd, data_fd, fw_fd;
707
    int booting = is_booting();
708

709 710
    INFO("firmware: loading '%s' for '%s'\n",
         uevent->firmware, uevent->path);
711 712 713 714 715 716 717 718 719 720 721 722 723

    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;

Brian Swetland's avatar
Brian Swetland committed
724 725 726 727 728
    l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
    if (l == -1)
        goto data_free_out;

    l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
729 730 731
    if (l == -1)
        goto data_free_out;

732 733 734 735
    l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware);
    if (l == -1)
        goto data_free_out;

736 737 738 739 740 741 742 743
    loading_fd = open(loading, O_WRONLY);
    if(loading_fd < 0)
        goto file_free_out;

    data_fd = open(data, O_WRONLY);
    if(data_fd < 0)
        goto loading_close_out;

744
try_loading_again:
Brian Swetland's avatar
Brian Swetland committed
745 746 747
    fw_fd = open(file1, O_RDONLY);
    if(fw_fd < 0) {
        fw_fd = open(file2, O_RDONLY);
748
        if (fw_fd < 0) {
749 750 751 752 753 754 755 756 757 758 759 760 761
            fw_fd = open(file3, O_RDONLY);
            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;
                }
                INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
                write(loading_fd, "-1", 2);
                goto data_close_out;
762
            }
763
        }
Brian Swetland's avatar
Brian Swetland committed
764
    }
765 766

    if(!load_firmware(fw_fd, loading_fd, data_fd))
767
        INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
768
    else
769
        INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
770 771 772 773 774 775 776

    close(fw_fd);
data_close_out:
    close(data_fd);
loading_close_out:
    close(loading_fd);
file_free_out:
Brian Swetland's avatar
Brian Swetland committed
777 778
    free(file1);
    free(file2);
779 780 781 782 783 784 785 786 787 788 789
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;
790
    int ret;
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806

    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);
    }
}

#define UEVENT_MSG_LEN  1024
807
void handle_device_fd()
808
{
809 810
    char msg[UEVENT_MSG_LEN+2];
    int n;
811
    while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
812
        if(n >= UEVENT_MSG_LEN)   /* overflow -- discard */
813 814 815 816 817
            continue;

        msg[n] = '\0';
        msg[n+1] = '\0';

818
        struct uevent uevent;
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
        parse_event(msg, &uevent);

        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.  
*/

835
static void do_coldboot(DIR *d)
836 837 838 839 840 841 842 843 844 845
{
    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);
846
        handle_device_fd();
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
    }

    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 {
863
            do_coldboot(d2);
864 865 866 867 868
            closedir(d2);
        }
    }
}

869
static void coldboot(const char *path)
870 871 872
{
    DIR *d = opendir(path);
    if(d) {
873
        do_coldboot(d);
874 875 876 877
        closedir(d);
    }
}

878
void device_init(void)
879 880
{
    suseconds_t t0, t1;
881 882
    struct stat info;
    int fd;
Kenny Root's avatar
Kenny Root committed
883

884 885 886 887
    sehandle = NULL;
    if (is_selinux_enabled() > 0) {
        sehandle = selinux_android_file_context_handle();
    }
Kenny Root's avatar
Kenny Root committed
888

889 890
    /* is 256K enough? udev uses 16MB! */
    device_fd = uevent_open_socket(256*1024, true);
891 892
    if(device_fd < 0)
        return;
893

894 895
    fcntl(device_fd, F_SETFD, FD_CLOEXEC);
    fcntl(device_fd, F_SETFL, O_NONBLOCK);
896

897 898 899 900 901 902 903 904 905 906 907 908
    if (stat(coldboot_done, &info) < 0) {
        t0 = get_usecs();
        coldboot("/sys/class");
        coldboot("/sys/block");
        coldboot("/sys/devices");
        t1 = get_usecs();
        fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
        close(fd);
        log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
    } else {
        log_event_print("skipping coldboot, already done\n");
    }
909
}
910

911 912 913
int get_device_fd()
{
    return device_fd;
914
}