devices.c revision e46f9d510db9351682cf17c49115110870147335
14f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project/*
24f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
34f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project *
44f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
54f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * you may not use this file except in compliance with the License.
64f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * You may obtain a copy of the License at
74f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project *
84f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
94f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project *
104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * See the License for the specific language governing permissions and
144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * limitations under the License.
154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project */
164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <errno.h>
18b93e5812faffd3b6c5fb349072413aace31918d8Olivier Bailly#include <stddef.h>
194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <stdio.h>
204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <stdlib.h>
214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/stat.h>
224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/types.h>
234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <fcntl.h>
254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <dirent.h>
264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <unistd.h>
274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <string.h>
284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/socket.h>
304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/un.h>
314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <linux/netlink.h>
32e46f9d510db9351682cf17c49115110870147335Stephen Smalley
33e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
34e46f9d510db9351682cf17c49115110870147335Stephen Smalley#include <selinux/selinux.h>
35e46f9d510db9351682cf17c49115110870147335Stephen Smalley#include <selinux/label.h>
36e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
37e46f9d510db9351682cf17c49115110870147335Stephen Smalley
384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <private/android_filesystem_config.h>
394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/time.h>
404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <asm/page.h>
41982a815c10cc28707d81c46112fc45b1c10df3beColin Cross#include <sys/wait.h>
424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
43da04c52ab1036048520fca265cf02b61dca789e0Dima Zavin#include <cutils/list.h>
443f582e92f07d358f3d0941b86407b39e2e7e67ccVernon Tang#include <cutils/uevent.h>
453f582e92f07d358f3d0941b86407b39e2e7e67ccVernon Tang
464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include "devices.h"
47b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross#include "util.h"
48ed8a7d84428ec945c48b6b53dc5a3a18fabaf683Colin Cross#include "log.h"
494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define SYSFS_PREFIX    "/sys"
5102863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland#define FIRMWARE_DIR1   "/etc/firmware"
5202863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland#define FIRMWARE_DIR2   "/vendor/firmware"
534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
54e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
55e46f9d510db9351682cf17c49115110870147335Stephen Smalleystatic struct selabel_handle *sehandle;
56e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
57e46f9d510db9351682cf17c49115110870147335Stephen Smalley
580dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossstatic int device_fd = -1;
590dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross
604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct uevent {
614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    const char *action;
624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    const char *path;
634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    const char *subsystem;
644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    const char *firmware;
65b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    const char *partition_name;
66b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int partition_num;
674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int major;
684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int minor;
694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project};
704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct perms_ {
724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    char *name;
73bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    char *attr;
744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode_t perm;
754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned int uid;
764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned int gid;
774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned short prefix;
784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project};
794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct perm_node {
814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct perms_ dp;
824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct listnode plist;
834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project};
84bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
85fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstruct platform_node {
86fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    char *name;
87fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    int name_len;
88fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct listnode list;
89fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross};
90fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
91bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandstatic list_declare(sys_perms);
9244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Crossstatic list_declare(dev_perms);
93fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic list_declare(platform_names);
944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
95bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandint add_dev_perms(const char *name, const char *attr,
96bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland                  mode_t perm, unsigned int uid, unsigned int gid,
97bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland                  unsigned short prefix) {
98bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    struct perm_node *node = calloc(1, sizeof(*node));
994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (!node)
1004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -ENOMEM;
1014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
102bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    node->dp.name = strdup(name);
103bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    if (!node->dp.name)
1044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -ENOMEM;
1054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
106bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    if (attr) {
107bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        node->dp.attr = strdup(attr);
108bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        if (!node->dp.attr)
109bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            return -ENOMEM;
110bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    }
111bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
1124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.perm = perm;
1134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.uid = uid;
1144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.gid = gid;
1154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.prefix = prefix;
1164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
117bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    if (attr)
118bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        list_add_tail(&sys_perms, &node->plist);
119bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    else
120bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        list_add_tail(&dev_perms, &node->plist);
121bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
1224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return 0;
1234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
1244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
125bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandvoid fixup_sys_perms(const char *upath)
1264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
127bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    char buf[512];
128bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    struct listnode *node;
129bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    struct perms_ *dp;
1304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
131bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        /* upaths omit the "/sys" that paths in this list
132bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland         * contain, so we add 4 when comparing...
133bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland         */
134bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    list_for_each(node, &sys_perms) {
135bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        dp = &(node_to_item(node, struct perm_node, plist))->dp;
136bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        if (dp->prefix) {
137bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
1384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                continue;
1394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else {
140bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            if (strcmp(upath, dp->name + 4))
1414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                continue;
1424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
143bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
144bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
145bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            return;
146bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
147bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        sprintf(buf,"/sys%s/%s", upath, dp->attr);
148bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
149bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        chown(buf, dp->uid, dp->gid);
150bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        chmod(buf, dp->perm);
1514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
1524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
1534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
1554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
1564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode_t perm;
15744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    struct listnode *node;
15844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    struct perm_node *perm_node;
15944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    struct perms_ *dp;
16044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross
16144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    /* search the perms list in reverse so that ueventd.$hardware can
16244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross     * override ueventd.rc
16344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross     */
16444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    list_for_each_reverse(node, &dev_perms) {
16544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        perm_node = node_to_item(node, struct perm_node, plist);
16644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        dp = &perm_node->dp;
16744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross
16844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        if (dp->prefix) {
16944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross            if (strncmp(path, dp->name, strlen(dp->name)))
17044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross                continue;
17144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        } else {
17244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross            if (strcmp(path, dp->name))
17344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross                continue;
1744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
17544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        *uid = dp->uid;
17644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        *gid = dp->gid;
17744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        return dp->perm;
1784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
17944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    /* Default if nothing found. */
18044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    *uid = 0;
18144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    *gid = 0;
18244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    return 0600;
1834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
1844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
185bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandstatic void make_device(const char *path,
186bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland                        const char *upath,
187bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland                        int block, int major, int minor)
1884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
1894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned uid;
1904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned gid;
1914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode_t mode;
1924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    dev_t dev;
193e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
194e46f9d510db9351682cf17c49115110870147335Stephen Smalley    char *secontext = NULL;
195e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
1964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
198e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
199e46f9d510db9351682cf17c49115110870147335Stephen Smalley    if (sehandle) {
200e46f9d510db9351682cf17c49115110870147335Stephen Smalley        selabel_lookup(sehandle, &secontext, path, mode);
201e46f9d510db9351682cf17c49115110870147335Stephen Smalley        setfscreatecon(secontext);
202e46f9d510db9351682cf17c49115110870147335Stephen Smalley    }
203e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
20417dcc5c57dcffb919f3de65ff7a0134ffa3bd874Colin Cross    dev = makedev(major, minor);
2056405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    /* Temporarily change egid to avoid race condition setting the gid of the
2066405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * device node. Unforunately changing the euid would prevent creation of
2076405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * some device nodes, so the uid has to be set with chown() and is still
2086405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * racy. Fixing the gid race at least fixed the issue with system_server
2096405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * opening dynamic input devices under the AID_INPUT gid. */
2106405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    setegid(gid);
2114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mknod(path, mode, dev);
2126405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    chown(path, uid, -1);
2136405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    setegid(AID_ROOT);
214e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
215e46f9d510db9351682cf17c49115110870147335Stephen Smalley    if (secontext) {
216e46f9d510db9351682cf17c49115110870147335Stephen Smalley        freecon(secontext);
217e46f9d510db9351682cf17c49115110870147335Stephen Smalley        setfscreatecon(NULL);
218e46f9d510db9351682cf17c49115110870147335Stephen Smalley    }
219e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
220e46f9d510db9351682cf17c49115110870147335Stephen Smalley}
221e46f9d510db9351682cf17c49115110870147335Stephen Smalley
222e46f9d510db9351682cf17c49115110870147335Stephen Smalley
223e46f9d510db9351682cf17c49115110870147335Stephen Smalleystatic int make_dir(const char *path, mode_t mode)
224e46f9d510db9351682cf17c49115110870147335Stephen Smalley{
225e46f9d510db9351682cf17c49115110870147335Stephen Smalley    int rc;
226e46f9d510db9351682cf17c49115110870147335Stephen Smalley
227e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
228e46f9d510db9351682cf17c49115110870147335Stephen Smalley    char *secontext = NULL;
229e46f9d510db9351682cf17c49115110870147335Stephen Smalley
230e46f9d510db9351682cf17c49115110870147335Stephen Smalley    if (sehandle) {
231e46f9d510db9351682cf17c49115110870147335Stephen Smalley        selabel_lookup(sehandle, &secontext, path, mode);
232e46f9d510db9351682cf17c49115110870147335Stephen Smalley        setfscreatecon(secontext);
233e46f9d510db9351682cf17c49115110870147335Stephen Smalley    }
234e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
235e46f9d510db9351682cf17c49115110870147335Stephen Smalley
236e46f9d510db9351682cf17c49115110870147335Stephen Smalley    rc = mkdir(path, mode);
237e46f9d510db9351682cf17c49115110870147335Stephen Smalley
238e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
239e46f9d510db9351682cf17c49115110870147335Stephen Smalley    if (secontext) {
240e46f9d510db9351682cf17c49115110870147335Stephen Smalley        freecon(secontext);
241e46f9d510db9351682cf17c49115110870147335Stephen Smalley        setfscreatecon(NULL);
242e46f9d510db9351682cf17c49115110870147335Stephen Smalley    }
243e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
244e46f9d510db9351682cf17c49115110870147335Stephen Smalley    return rc;
2454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
2464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
247e46f9d510db9351682cf17c49115110870147335Stephen Smalley
248fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic void add_platform_device(const char *name)
249fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{
250fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    int name_len = strlen(name);
251fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct listnode *node;
252fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct platform_node *bus;
253fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
254fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    list_for_each_reverse(node, &platform_names) {
255fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        bus = node_to_item(node, struct platform_node, list);
256fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        if ((bus->name_len < name_len) &&
257fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross                (name[bus->name_len] == '/') &&
258fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross                !strncmp(name, bus->name, bus->name_len))
259fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            /* subdevice of an existing platform, ignore it */
260fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            return;
261fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    }
262fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
263fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    INFO("adding platform device %s\n", name);
264fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
265fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    bus = calloc(1, sizeof(struct platform_node));
266fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    bus->name = strdup(name);
267fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    bus->name_len = name_len;
268fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    list_add_tail(&platform_names, &bus->list);
269fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross}
270fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
271fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross/*
272fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross * given a name that may start with a platform device, find the length of the
273fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross * platform device prefix.  If it doesn't start with a platform device, return
274fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross * 0.
275fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross */
276fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic const char *find_platform_device(const char *name)
277fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{
278fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    int name_len = strlen(name);
279fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct listnode *node;
280fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct platform_node *bus;
281fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
282fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    list_for_each_reverse(node, &platform_names) {
283fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        bus = node_to_item(node, struct platform_node, list);
284fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        if ((bus->name_len < name_len) &&
285fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross                (name[bus->name_len] == '/') &&
286fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross                !strncmp(name, bus->name, bus->name_len))
287fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            return bus->name;
288fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    }
289fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
290fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    return NULL;
291fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross}
292fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
293fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic void remove_platform_device(const char *name)
294fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{
295fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct listnode *node;
296fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct platform_node *bus;
297fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
298fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    list_for_each_reverse(node, &platform_names) {
299fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        bus = node_to_item(node, struct platform_node, list);
300fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        if (!strcmp(name, bus->name)) {
301fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            INFO("removing platform device %s\n", name);
302fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            free(bus->name);
303fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            list_remove(node);
304fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            free(bus);
305fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            return;
306fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        }
307fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    }
308fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross}
309fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
3101e070846f8908dff15086efa12fbba01ab7eae15Chuck Tuffli#if LOG_UEVENTS
3114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic inline suseconds_t get_usecs(void)
3134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
3144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct timeval tv;
3154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    gettimeofday(&tv, 0);
3164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
3174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
3184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define log_event_print(x...) INFO(x)
3204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#else
3224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define log_event_print(fmt, args...)   do { } while (0)
3244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define get_usecs()                     0
3254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#endif
3274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void parse_event(const char *msg, struct uevent *uevent)
3294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
3304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->action = "";
3314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->path = "";
3324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->subsystem = "";
3334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->firmware = "";
3344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->major = -1;
3354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->minor = -1;
336b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    uevent->partition_name = NULL;
337b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    uevent->partition_num = -1;
3384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        /* currently ignoring SEQNUM */
3404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    while(*msg) {
3414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(!strncmp(msg, "ACTION=", 7)) {
3424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 7;
3434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->action = msg;
3444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "DEVPATH=", 8)) {
3454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 8;
3464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->path = msg;
3474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
3484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 10;
3494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->subsystem = msg;
3504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "FIRMWARE=", 9)) {
3514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 9;
3524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->firmware = msg;
3534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "MAJOR=", 6)) {
3544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 6;
3554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->major = atoi(msg);
3564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "MINOR=", 6)) {
3574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 6;
3584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->minor = atoi(msg);
359b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        } else if(!strncmp(msg, "PARTN=", 6)) {
360b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            msg += 6;
361b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            uevent->partition_num = atoi(msg);
362b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        } else if(!strncmp(msg, "PARTNAME=", 9)) {
363b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            msg += 9;
364b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            uevent->partition_name = msg;
3654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
3664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            /* advance to after the next \0 */
3684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        while(*msg++)
3694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            ;
3704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
3714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
3734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                    uevent->action, uevent->path, uevent->subsystem,
3744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                    uevent->firmware, uevent->major, uevent->minor);
3754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
3764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
377d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Gobystatic char **get_character_device_symlinks(struct uevent *uevent)
378d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby{
379d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    const char *parent;
380d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    char *slash;
381d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    char **links;
382d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    int link_num = 0;
383d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    int width;
384d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
385d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    if (strncmp(uevent->path, "/devices/platform/", 18))
386d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        return NULL;
387d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
388d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    links = malloc(sizeof(char *) * 2);
389d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    if (!links)
390d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        return NULL;
391d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    memset(links, 0, sizeof(char *) * 2);
392d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
393d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    /* skip "/devices/platform/<driver>" */
394d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    parent = strchr(uevent->path + 18, '/');
395d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    if (!*parent)
396d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        goto err;
397d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
398d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    if (!strncmp(parent, "/usb", 4)) {
399d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        /* skip root hub name and device. use device interface */
400d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        while (*++parent && *parent != '/');
401d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (*parent)
402d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            while (*++parent && *parent != '/');
403d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (!*parent)
404d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            goto err;
405d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        slash = strchr(++parent, '/');
406d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (!slash)
407d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            goto err;
408d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        width = slash - parent;
409d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (width <= 0)
410d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            goto err;
411d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
412d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
413d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            link_num++;
414d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        else
415d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            links[link_num] = NULL;
416d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        mkdir("/dev/usb", 0755);
417d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    }
418d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    else {
419d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        goto err;
420d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    }
421d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
422d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    return links;
423d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Gobyerr:
424d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    free(links);
425d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    return NULL;
426d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby}
427d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
428b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Crossstatic char **parse_platform_block_device(struct uevent *uevent)
429b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross{
430fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    const char *device;
431b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    const char *path;
432b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char *slash;
433b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int width;
434b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char buf[256];
435b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char link_path[256];
436b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int fd;
437b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int link_num = 0;
438b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int ret;
439b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char *p;
440b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    unsigned int size;
441b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    struct stat info;
442b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
443b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char **links = malloc(sizeof(char *) * 4);
444b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (!links)
445b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        return NULL;
446b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    memset(links, 0, sizeof(char *) * 4);
447b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
448b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    /* Drop "/devices/platform/" */
449b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    path = uevent->path;
450fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    device = path + 18;
451fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    device = find_platform_device(device);
452fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    if (!device)
453b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        goto err;
454b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
455fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    INFO("found platform device %s\n", device);
456fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
457fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device);
458b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
459b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (uevent->partition_name) {
460b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        p = strdup(uevent->partition_name);
461b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        sanitize(p);
462b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
463b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            link_num++;
464b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        else
465b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            links[link_num] = NULL;
466b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        free(p);
467b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    }
468b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
469b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (uevent->partition_num >= 0) {
470b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
471b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            link_num++;
472b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        else
473b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            links[link_num] = NULL;
474b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    }
475b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
476b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    slash = strrchr(path, '/');
477b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
478b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        link_num++;
479b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    else
480b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        links[link_num] = NULL;
481b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
482b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    return links;
483b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
484b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Crosserr:
485b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    free(links);
486b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    return NULL;
487b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross}
488b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
489eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_device(const char *action, const char *devpath,
490eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        const char *path, int block, int major, int minor, char **links)
4914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
492b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int i;
4934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
494eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if(!strcmp(action, "add")) {
495eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        make_device(devpath, path, block, major, minor);
496b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (links) {
497b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            for (i = 0; links[i]; i++)
498b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross                make_link(devpath, links[i]);
499b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        }
5004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
5014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
502eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if(!strcmp(action, "remove")) {
503b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (links) {
504b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            for (i = 0; links[i]; i++)
505b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross                remove_link(devpath, links[i]);
506b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        }
5074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        unlink(devpath);
508b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    }
509b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
510b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (links) {
511b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        for (i = 0; links[i]; i++)
512b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            free(links[i]);
513b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        free(links);
5144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
5154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
5164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
517fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic void handle_platform_device_event(struct uevent *uevent)
518fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{
519fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    const char *name = uevent->path + 18; /* length of /devices/platform/ */
520fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
521fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    if (!strcmp(uevent->action, "add"))
522fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        add_platform_device(name);
523fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    else if (!strcmp(uevent->action, "remove"))
524fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        remove_platform_device(name);
525fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross}
526fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
527eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic const char *parse_device_name(struct uevent *uevent, unsigned int len)
528eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{
529eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    const char *name;
530eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
531eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    /* if it's not a /dev device, nothing else to do */
532eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if((uevent->major < 0) || (uevent->minor < 0))
533eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return NULL;
534eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
535eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    /* do we have a name? */
536eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    name = strrchr(uevent->path, '/');
537eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if(!name)
538eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return NULL;
539eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    name++;
540eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
541eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    /* too-long names would overrun our buffer */
542eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if(strlen(name) > len)
543eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return NULL;
544eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
545eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    return name;
546eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross}
547eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
548eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_block_device_event(struct uevent *uevent)
549eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{
550eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    const char *base = "/dev/block/";
551eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    const char *name;
552eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    char devpath[96];
553eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    char **links = NULL;
554eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
555eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    name = parse_device_name(uevent, 64);
556eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!name)
557eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return;
558eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
559eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    snprintf(devpath, sizeof(devpath), "%s%s", base, name);
560e46f9d510db9351682cf17c49115110870147335Stephen Smalley    make_dir(base, 0755);
561eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
562eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!strncmp(uevent->path, "/devices/platform/", 18))
563eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        links = parse_platform_block_device(uevent);
564eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
565eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    handle_device(uevent->action, devpath, uevent->path, 1,
566eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross            uevent->major, uevent->minor, links);
567eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross}
568eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
569eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_generic_device_event(struct uevent *uevent)
570eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{
571eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    char *base;
572eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    const char *name;
573eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    char devpath[96] = {0};
574eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    char **links = NULL;
575eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
576eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    name = parse_device_name(uevent, 64);
577eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!name)
578eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return;
579eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
580eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!strncmp(uevent->subsystem, "usb", 3)) {
581eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         if (!strcmp(uevent->subsystem, "usb")) {
582eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross             /* This imitates the file system that would be created
583eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross              * if we were using devfs instead.
584eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross              * Minors are broken up into groups of 128, starting at "001"
585eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross              */
586eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross             int bus_id = uevent->minor / 128 + 1;
587eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross             int device_id = uevent->minor % 128 + 1;
588eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross             /* build directories */
589e46f9d510db9351682cf17c49115110870147335Stephen Smalley             make_dir("/dev/bus", 0755);
590e46f9d510db9351682cf17c49115110870147335Stephen Smalley             make_dir("/dev/bus/usb", 0755);
591eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross             snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
592e46f9d510db9351682cf17c49115110870147335Stephen Smalley             make_dir(devpath, 0755);
593eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross             snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
594eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         } else {
595eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross             /* ignore other USB events */
596eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross             return;
597eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         }
598eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
599eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/graphics/";
600e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
601eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
602eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/oncrpc/";
603e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
604eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
605eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/adsp/";
606e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
607eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
608eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/msm_camera/";
609e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
610eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if(!strncmp(uevent->subsystem, "input", 5)) {
611eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/input/";
612e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
613eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
614eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/mtd/";
615e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
616eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if(!strncmp(uevent->subsystem, "sound", 5)) {
617eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/snd/";
618e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
619eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if(!strncmp(uevent->subsystem, "misc", 4) &&
620eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross                 !strncmp(name, "log_", 4)) {
621eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/log/";
622e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
623eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         name += 4;
624eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else
625eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/";
626eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     links = get_character_device_symlinks(uevent);
627eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
628eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     if (!devpath[0])
629eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         snprintf(devpath, sizeof(devpath), "%s%s", base, name);
630eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
631eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     handle_device(uevent->action, devpath, uevent->path, 0,
632eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross             uevent->major, uevent->minor, links);
633eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross}
634eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
635eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_device_event(struct uevent *uevent)
636eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{
637eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!strcmp(uevent->action,"add"))
638eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        fixup_sys_perms(uevent->path);
639eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
640eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!strncmp(uevent->subsystem, "block", 5)) {
641eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        handle_block_device_event(uevent);
642fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    } else if (!strncmp(uevent->subsystem, "platform", 8)) {
643fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        handle_platform_device_event(uevent);
644eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    } else {
645eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        handle_generic_device_event(uevent);
646eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    }
647eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross}
648eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
6494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic int load_firmware(int fw_fd, int loading_fd, int data_fd)
6504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
6514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct stat st;
6524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    long len_to_copy;
6534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int ret = 0;
6544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(fstat(fw_fd, &st) < 0)
6564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -1;
6574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    len_to_copy = st.st_size;
6584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    write(loading_fd, "1", 1);  /* start transfer */
6604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    while (len_to_copy > 0) {
6624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        char buf[PAGE_SIZE];
6634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        ssize_t nr;
6644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        nr = read(fw_fd, buf, sizeof(buf));
6664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(!nr)
6674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            break;
6684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(nr < 0) {
6694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            ret = -1;
6704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            break;
6714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
6724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        len_to_copy -= nr;
6744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        while (nr > 0) {
6754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            ssize_t nw = 0;
6764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            nw = write(data_fd, buf + nw, nr);
6784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            if(nw <= 0) {
6794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                ret = -1;
6804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                goto out;
6814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            }
6824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            nr -= nw;
6834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
6844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
6854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectout:
6874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(!ret)
6884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        write(loading_fd, "0", 1);  /* successful end of transfer */
6894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    else
6904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        write(loading_fd, "-1", 2); /* abort transfer */
6914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return ret;
6934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
6944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6958d48c8e45724c7103f0ace7885d339e49399908bBrian Swetlandstatic int is_booting(void)
6968d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland{
6978d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland    return access("/dev/.booting", F_OK) == 0;
6988d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland}
6998d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland
7004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void process_firmware_event(struct uevent *uevent)
7014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
70202863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    char *root, *loading, *data, *file1 = NULL, *file2 = NULL;
7034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int l, loading_fd, data_fd, fw_fd;
7048d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland    int booting = is_booting();
7054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7068d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland    INFO("firmware: loading '%s' for '%s'\n",
7078d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland         uevent->firmware, uevent->path);
7084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7094f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
7104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
7114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return;
7124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    l = asprintf(&loading, "%sloading", root);
7144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
7154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto root_free_out;
7164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    l = asprintf(&data, "%sdata", root);
7184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
7194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto loading_free_out;
7204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
72102863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
72202863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    if (l == -1)
72302863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland        goto data_free_out;
72402863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland
72502863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
7264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
7274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto data_free_out;
7284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    loading_fd = open(loading, O_WRONLY);
7304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(loading_fd < 0)
7314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto file_free_out;
7324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    data_fd = open(data, O_WRONLY);
7344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(data_fd < 0)
7354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto loading_close_out;
7364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7378d48c8e45724c7103f0ace7885d339e49399908bBrian Swetlandtry_loading_again:
73802863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    fw_fd = open(file1, O_RDONLY);
73902863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    if(fw_fd < 0) {
74002863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland        fw_fd = open(file2, O_RDONLY);
741609d8828d3b8266a80606bf902d1294296962cf3Benoit Goby        if (fw_fd < 0) {
7428d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland            if (booting) {
7438d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland                    /* If we're not fully booted, we may be missing
7448d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland                     * filesystems needed for firmware, wait and retry.
7458d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland                     */
7468d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland                usleep(100000);
7478d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland                booting = is_booting();
7488d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland                goto try_loading_again;
7498d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland            }
7508d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland            INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
751609d8828d3b8266a80606bf902d1294296962cf3Benoit Goby            write(loading_fd, "-1", 2);
75202863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland            goto data_close_out;
753609d8828d3b8266a80606bf902d1294296962cf3Benoit Goby        }
75402863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    }
7554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(!load_firmware(fw_fd, loading_fd, data_fd))
7578d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland        INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
7584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    else
7598d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland        INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
7604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    close(fw_fd);
7624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectdata_close_out:
7634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    close(data_fd);
7644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectloading_close_out:
7654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    close(loading_fd);
7664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectfile_free_out:
76702863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    free(file1);
76802863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    free(file2);
7694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectdata_free_out:
7704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    free(data);
7714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectloading_free_out:
7724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    free(loading);
7734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectroot_free_out:
7744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    free(root);
7754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
7764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void handle_firmware_event(struct uevent *uevent)
7784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
7794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    pid_t pid;
780982a815c10cc28707d81c46112fc45b1c10df3beColin Cross    int ret;
7814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(strcmp(uevent->subsystem, "firmware"))
7834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return;
7844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(strcmp(uevent->action, "add"))
7864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return;
7874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    /* we fork, to avoid making large memory allocations in init proper */
7894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    pid = fork();
7904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (!pid) {
7914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        process_firmware_event(uevent);
7924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        exit(EXIT_SUCCESS);
7934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
7944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
7954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define UEVENT_MSG_LEN  1024
7970dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossvoid handle_device_fd()
7984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
7993f582e92f07d358f3d0941b86407b39e2e7e67ccVernon Tang    char msg[UEVENT_MSG_LEN+2];
8003f582e92f07d358f3d0941b86407b39e2e7e67ccVernon Tang    int n;
80157de8b8f27f887ca8084671df777a4ac199ba647Nick Kralevich    while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
8025f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        if(n >= UEVENT_MSG_LEN)   /* overflow -- discard */
8034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            continue;
8044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        msg[n] = '\0';
8064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        msg[n+1] = '\0';
8074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8085f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        struct uevent uevent;
8094f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        parse_event(msg, &uevent);
8104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        handle_device_event(&uevent);
8124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        handle_firmware_event(&uevent);
8134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
8144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
8154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project/* Coldboot walks parts of the /sys tree and pokes the uevent files
8174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** to cause the kernel to regenerate device add events that happened
8184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** before init's device manager was started
8194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project**
8204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** We drain any pending events from the netlink socket every time
8214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** we poke another uevent file to make sure we don't overrun the
8224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** socket's buffer.
8234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project*/
8244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8250dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossstatic void do_coldboot(DIR *d)
8264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
8274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct dirent *de;
8284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int dfd, fd;
8294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    dfd = dirfd(d);
8314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    fd = openat(dfd, "uevent", O_WRONLY);
8334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(fd >= 0) {
8344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        write(fd, "add\n", 4);
8354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        close(fd);
8360dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross        handle_device_fd();
8374f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
8384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    while((de = readdir(d))) {
8404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        DIR *d2;
8414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(de->d_type != DT_DIR || de->d_name[0] == '.')
8434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            continue;
8444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
8464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(fd < 0)
8474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            continue;
8484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        d2 = fdopendir(fd);
8504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(d2 == 0)
8514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            close(fd);
8524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        else {
8530dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross            do_coldboot(d2);
8544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            closedir(d2);
8554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
8564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
8574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
8584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8590dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossstatic void coldboot(const char *path)
8604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
8614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    DIR *d = opendir(path);
8624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(d) {
8630dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross        do_coldboot(d);
8644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        closedir(d);
8654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
8664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
8674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8680dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossvoid device_init(void)
8694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
8704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    suseconds_t t0, t1;
871f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross    struct stat info;
872f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross    int fd;
873e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
874e46f9d510db9351682cf17c49115110870147335Stephen Smalley    struct selinux_opt seopts[] = {
875e46f9d510db9351682cf17c49115110870147335Stephen Smalley        { SELABEL_OPT_PATH, "/file_contexts" }
876e46f9d510db9351682cf17c49115110870147335Stephen Smalley    };
8774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
878e46f9d510db9351682cf17c49115110870147335Stephen Smalley    if (is_selinux_enabled() > 0)
879e46f9d510db9351682cf17c49115110870147335Stephen Smalley        sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
880e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
8812d55e02d0f3c27f0c99ab889ab7b73126280a21cDima Zavin    /* is 64K enough? udev uses 16MB! */
8822d55e02d0f3c27f0c99ab889ab7b73126280a21cDima Zavin    device_fd = uevent_open_socket(64*1024, true);
8830dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross    if(device_fd < 0)
8840dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross        return;
8854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8860dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross    fcntl(device_fd, F_SETFD, FD_CLOEXEC);
8870dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross    fcntl(device_fd, F_SETFL, O_NONBLOCK);
8884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
889f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross    if (stat(coldboot_done, &info) < 0) {
890f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        t0 = get_usecs();
891f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        coldboot("/sys/class");
892f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        coldboot("/sys/block");
893f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        coldboot("/sys/devices");
894f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        t1 = get_usecs();
895f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
896f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        close(fd);
897f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
898f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross    } else {
899f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        log_event_print("skipping coldboot, already done\n");
900f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross    }
9010dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross}
9024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
9030dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossint get_device_fd()
9040dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross{
9050dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross    return device_fd;
9064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
907