devices.cpp revision bc57d4ce925a62f14c28c55e0ff28af1114f12be
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>
184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <stdio.h>
194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <stdlib.h>
204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/stat.h>
214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/types.h>
224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <fcntl.h>
244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <dirent.h>
254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <unistd.h>
264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <string.h>
274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/socket.h>
294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/un.h>
304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <linux/netlink.h>
314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <private/android_filesystem_config.h>
324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/time.h>
334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <asm/page.h>
34982a815c10cc28707d81c46112fc45b1c10df3beColin Cross#include <sys/wait.h>
354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include "devices.h"
37b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross#include "util.h"
38ed8a7d84428ec945c48b6b53dc5a3a18fabaf683Colin Cross#include "log.h"
39ed8a7d84428ec945c48b6b53dc5a3a18fabaf683Colin Cross#include "list.h"
404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define SYSFS_PREFIX    "/sys"
4202863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland#define FIRMWARE_DIR1   "/etc/firmware"
4302863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland#define FIRMWARE_DIR2   "/vendor/firmware"
444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
450dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossstatic int device_fd = -1;
460dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross
474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct uevent {
484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    const char *action;
494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    const char *path;
504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    const char *subsystem;
514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    const char *firmware;
52b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    const char *partition_name;
53b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int partition_num;
544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int major;
554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int minor;
564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project};
574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic int open_uevent_socket(void)
594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct sockaddr_nl addr;
614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int sz = 64*1024; // XXX larger? udev uses 16MB!
625f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich    int on = 1;
634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int s;
644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    memset(&addr, 0, sizeof(addr));
664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    addr.nl_family = AF_NETLINK;
674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    addr.nl_pid = getpid();
684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    addr.nl_groups = 0xffffffff;
694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(s < 0)
724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -1;
734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));
755f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich    setsockopt(s, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        close(s);
794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -1;
804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return s;
834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct perms_ {
864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    char *name;
87bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    char *attr;
884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode_t perm;
894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned int uid;
904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned int gid;
914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned short prefix;
924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project};
934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct perm_node {
954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct perms_ dp;
964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct listnode plist;
974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project};
98bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
99bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandstatic list_declare(sys_perms);
10044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Crossstatic list_declare(dev_perms);
1014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
102bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandint add_dev_perms(const char *name, const char *attr,
103bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland                  mode_t perm, unsigned int uid, unsigned int gid,
104bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland                  unsigned short prefix) {
105bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    struct perm_node *node = calloc(1, sizeof(*node));
1064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (!node)
1074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -ENOMEM;
1084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
109bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    node->dp.name = strdup(name);
110bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    if (!node->dp.name)
1114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -ENOMEM;
1124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
113bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    if (attr) {
114bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        node->dp.attr = strdup(attr);
115bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        if (!node->dp.attr)
116bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            return -ENOMEM;
117bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    }
118bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
1194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.perm = perm;
1204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.uid = uid;
1214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.gid = gid;
1224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.prefix = prefix;
1234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
124bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    if (attr)
125bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        list_add_tail(&sys_perms, &node->plist);
126bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    else
127bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        list_add_tail(&dev_perms, &node->plist);
128bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
1294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return 0;
1304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
1314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
132bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandvoid fixup_sys_perms(const char *upath)
1334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
134bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    char buf[512];
135bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    struct listnode *node;
136bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    struct perms_ *dp;
1374f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
138bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        /* upaths omit the "/sys" that paths in this list
139bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland         * contain, so we add 4 when comparing...
140bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland         */
141bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    list_for_each(node, &sys_perms) {
142bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        dp = &(node_to_item(node, struct perm_node, plist))->dp;
143bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        if (dp->prefix) {
144bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
1454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                continue;
1464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else {
147bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            if (strcmp(upath, dp->name + 4))
1484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                continue;
1494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
150bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
151bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
152bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            return;
153bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
154bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        sprintf(buf,"/sys%s/%s", upath, dp->attr);
155bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
156bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        chown(buf, dp->uid, dp->gid);
157bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        chmod(buf, dp->perm);
1584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
1594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
1604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
1624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
1634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode_t perm;
16444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    struct listnode *node;
16544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    struct perm_node *perm_node;
16644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    struct perms_ *dp;
16744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross
16844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    /* search the perms list in reverse so that ueventd.$hardware can
16944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross     * override ueventd.rc
17044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross     */
17144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    list_for_each_reverse(node, &dev_perms) {
17244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        perm_node = node_to_item(node, struct perm_node, plist);
17344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        dp = &perm_node->dp;
17444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross
17544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        if (dp->prefix) {
17644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross            if (strncmp(path, dp->name, strlen(dp->name)))
17744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross                continue;
17844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        } else {
17944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross            if (strcmp(path, dp->name))
18044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross                continue;
1814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
18244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        *uid = dp->uid;
18344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        *gid = dp->gid;
18444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        return dp->perm;
1854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
18644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    /* Default if nothing found. */
18744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    *uid = 0;
18844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    *gid = 0;
18944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    return 0600;
1904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
1914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
192bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandstatic void make_device(const char *path,
193bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland                        const char *upath,
194bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland                        int block, int major, int minor)
1954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
1964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned uid;
1974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned gid;
1984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode_t mode;
1994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    dev_t dev;
2004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
20217dcc5c57dcffb919f3de65ff7a0134ffa3bd874Colin Cross    dev = makedev(major, minor);
2036405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    /* Temporarily change egid to avoid race condition setting the gid of the
2046405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * device node. Unforunately changing the euid would prevent creation of
2056405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * some device nodes, so the uid has to be set with chown() and is still
2066405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * racy. Fixing the gid race at least fixed the issue with system_server
2076405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * opening dynamic input devices under the AID_INPUT gid. */
2086405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    setegid(gid);
2094f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mknod(path, mode, dev);
2106405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    chown(path, uid, -1);
2116405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    setegid(AID_ROOT);
2124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
2134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2141e070846f8908dff15086efa12fbba01ab7eae15Chuck Tuffli#if LOG_UEVENTS
2154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic inline suseconds_t get_usecs(void)
2174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
2184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct timeval tv;
2194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    gettimeofday(&tv, 0);
2204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
2214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
2224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define log_event_print(x...) INFO(x)
2244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#else
2264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define log_event_print(fmt, args...)   do { } while (0)
2284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define get_usecs()                     0
2294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#endif
2314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void parse_event(const char *msg, struct uevent *uevent)
2334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
2344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->action = "";
2354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->path = "";
2364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->subsystem = "";
2374f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->firmware = "";
2384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->major = -1;
2394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->minor = -1;
240b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    uevent->partition_name = NULL;
241b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    uevent->partition_num = -1;
2424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        /* currently ignoring SEQNUM */
2444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    while(*msg) {
2454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(!strncmp(msg, "ACTION=", 7)) {
2464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 7;
2474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->action = msg;
2484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "DEVPATH=", 8)) {
2494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 8;
2504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->path = msg;
2514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
2524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 10;
2534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->subsystem = msg;
2544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "FIRMWARE=", 9)) {
2554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 9;
2564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->firmware = msg;
2574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "MAJOR=", 6)) {
2584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 6;
2594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->major = atoi(msg);
2604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "MINOR=", 6)) {
2614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 6;
2624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->minor = atoi(msg);
263b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        } else if(!strncmp(msg, "PARTN=", 6)) {
264b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            msg += 6;
265b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            uevent->partition_num = atoi(msg);
266b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        } else if(!strncmp(msg, "PARTNAME=", 9)) {
267b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            msg += 9;
268b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            uevent->partition_name = msg;
2694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
2704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            /* advance to after the next \0 */
2724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        while(*msg++)
2734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            ;
2744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
2754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
2774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                    uevent->action, uevent->path, uevent->subsystem,
2784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                    uevent->firmware, uevent->major, uevent->minor);
2794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
2804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
281b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Crossstatic char **parse_platform_block_device(struct uevent *uevent)
282b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross{
283b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    const char *driver;
284b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    const char *path;
285b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char *slash;
286b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int width;
287b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char buf[256];
288b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char link_path[256];
289b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int fd;
290b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int link_num = 0;
291b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int ret;
292b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char *p;
293b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    unsigned int size;
294b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    struct stat info;
295b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
296b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char **links = malloc(sizeof(char *) * 4);
297b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (!links)
298b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        return NULL;
299b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    memset(links, 0, sizeof(char *) * 4);
300b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
301b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    /* Drop "/devices/platform/" */
302b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    path = uevent->path;
303b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    driver = path + 18;
304b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    slash = strchr(driver, '/');
305b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (!slash)
306b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        goto err;
307b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    width = slash - driver;
308b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (width <= 0)
309b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        goto err;
310b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
311b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    snprintf(link_path, sizeof(link_path), "/dev/block/platform/%.*s",
312b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross             width, driver);
313b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
314b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (uevent->partition_name) {
315b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        p = strdup(uevent->partition_name);
316b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        sanitize(p);
317b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
318b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            link_num++;
319b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        else
320b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            links[link_num] = NULL;
321b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        free(p);
322b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    }
323b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
324b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (uevent->partition_num >= 0) {
325b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
326b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            link_num++;
327b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        else
328b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            links[link_num] = NULL;
329b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    }
330b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
331b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    slash = strrchr(path, '/');
332b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
333b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        link_num++;
334b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    else
335b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        links[link_num] = NULL;
336b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
337b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    return links;
338b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
339b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Crosserr:
340b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    free(links);
341b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    return NULL;
342b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross}
343b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
3444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void handle_device_event(struct uevent *uevent)
3454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
3464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    char devpath[96];
34793ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood    int devpath_ready = 0;
3484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    char *base, *name;
349b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char **links = NULL;
3504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int block;
351b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int i;
3524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
353bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    if (!strcmp(uevent->action,"add"))
354bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        fixup_sys_perms(uevent->path);
355bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
356bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        /* if it's not a /dev device, nothing else to do */
3574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if((uevent->major < 0) || (uevent->minor < 0))
3584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return;
3594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        /* do we have a name? */
3614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    name = strrchr(uevent->path, '/');
3624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(!name)
3634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return;
3644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    name++;
3654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        /* too-long names would overrun our buffer */
3674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(strlen(name) > 64)
3684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return;
3694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        /* are we block or char? where should we live? */
37135237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project    if(!strncmp(uevent->subsystem, "block", 5)) {
3724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        block = 1;
3734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        base = "/dev/block/";
3744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        mkdir(base, 0755);
375b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (!strncmp(uevent->path, "/devices/platform/", 18))
376b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            links = parse_platform_block_device(uevent);
3774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    } else {
3784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        block = 0;
3794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            /* this should probably be configurable somehow */
38093ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood        if (!strncmp(uevent->subsystem, "usb", 3)) {
38193ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood            if (!strcmp(uevent->subsystem, "usb")) {
38293ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood                /* This imitates the file system that would be created
38393ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood                 * if we were using devfs instead.
38493ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood                 * Minors are broken up into groups of 128, starting at "001"
38593ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood                 */
38693ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood                int bus_id = uevent->minor / 128 + 1;
38793ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood                int device_id = uevent->minor % 128 + 1;
38893ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood                /* build directories */
38993ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood                mkdir("/dev/bus", 0755);
39093ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood                mkdir("/dev/bus/usb", 0755);
39193ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood                snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
39293ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood                mkdir(devpath, 0755);
39393ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood                snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
39493ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood                devpath_ready = 1;
39593ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood            } else {
39693ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood                /* ignore other USB events */
39793ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood                return;
39893ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood            }
39993ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood        } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
4004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            base = "/dev/graphics/";
4014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            mkdir(base, 0755);
40235237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project        } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
4034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            base = "/dev/oncrpc/";
4044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            mkdir(base, 0755);
40535237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project        } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
4064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            base = "/dev/adsp/";
4074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            mkdir(base, 0755);
408fc0182eb1db0620eb71fb6ca219b15a17dcd912fIliyan Malchev        } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
409fc0182eb1db0620eb71fb6ca219b15a17dcd912fIliyan Malchev            base = "/dev/msm_camera/";
410fc0182eb1db0620eb71fb6ca219b15a17dcd912fIliyan Malchev            mkdir(base, 0755);
411fc0182eb1db0620eb71fb6ca219b15a17dcd912fIliyan Malchev        } else if(!strncmp(uevent->subsystem, "input", 5)) {
4124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            base = "/dev/input/";
4134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            mkdir(base, 0755);
41435237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project        } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
4154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            base = "/dev/mtd/";
4164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            mkdir(base, 0755);
4175bb44c8ea2bc513fefc526918980ab3d17494eccXiaopeng Yang        } else if(!strncmp(uevent->subsystem, "sound", 5)) {
4185bb44c8ea2bc513fefc526918980ab3d17494eccXiaopeng Yang            base = "/dev/snd/";
4195bb44c8ea2bc513fefc526918980ab3d17494eccXiaopeng Yang            mkdir(base, 0755);
42035237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project        } else if(!strncmp(uevent->subsystem, "misc", 4) &&
4214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                    !strncmp(name, "log_", 4)) {
4224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            base = "/dev/log/";
4234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            mkdir(base, 0755);
4244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            name += 4;
4254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else
4264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            base = "/dev/";
4274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
4284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
42993ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood    if (!devpath_ready)
43093ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood        snprintf(devpath, sizeof(devpath), "%s%s", base, name);
4314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
4324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(!strcmp(uevent->action, "add")) {
433bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        make_device(devpath, uevent->path, block, uevent->major, uevent->minor);
434b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (links) {
435b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            for (i = 0; links[i]; i++)
436b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross                make_link(devpath, links[i]);
437b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        }
4384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
4394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
4404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(!strcmp(uevent->action, "remove")) {
441b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (links) {
442b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            for (i = 0; links[i]; i++)
443b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross                remove_link(devpath, links[i]);
444b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        }
4454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        unlink(devpath);
446b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    }
447b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
448b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (links) {
449b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        for (i = 0; links[i]; i++)
450b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            free(links[i]);
451b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        free(links);
4524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
4534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
4544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
4554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic int load_firmware(int fw_fd, int loading_fd, int data_fd)
4564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
4574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct stat st;
4584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    long len_to_copy;
4594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int ret = 0;
4604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
4614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(fstat(fw_fd, &st) < 0)
4624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -1;
4634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    len_to_copy = st.st_size;
4644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
4654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    write(loading_fd, "1", 1);  /* start transfer */
4664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
4674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    while (len_to_copy > 0) {
4684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        char buf[PAGE_SIZE];
4694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        ssize_t nr;
4704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
4714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        nr = read(fw_fd, buf, sizeof(buf));
4724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(!nr)
4734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            break;
4744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(nr < 0) {
4754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            ret = -1;
4764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            break;
4774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
4784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
4794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        len_to_copy -= nr;
4804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        while (nr > 0) {
4814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            ssize_t nw = 0;
4824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
4834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            nw = write(data_fd, buf + nw, nr);
4844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            if(nw <= 0) {
4854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                ret = -1;
4864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                goto out;
4874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            }
4884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            nr -= nw;
4894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
4904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
4914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
4924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectout:
4934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(!ret)
4944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        write(loading_fd, "0", 1);  /* successful end of transfer */
4954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    else
4964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        write(loading_fd, "-1", 2); /* abort transfer */
4974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
4984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return ret;
4994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
5004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void process_firmware_event(struct uevent *uevent)
5024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
50302863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    char *root, *loading, *data, *file1 = NULL, *file2 = NULL;
5044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int l, loading_fd, data_fd, fw_fd;
5054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    log_event_print("firmware event { '%s', '%s' }\n",
5074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                    uevent->path, uevent->firmware);
5084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5094f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
5104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
5114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return;
5124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    l = asprintf(&loading, "%sloading", root);
5144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
5154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto root_free_out;
5164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    l = asprintf(&data, "%sdata", root);
5184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
5194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto loading_free_out;
5204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
52102863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
52202863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    if (l == -1)
52302863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland        goto data_free_out;
52402863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland
52502863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
5264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
5274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto data_free_out;
5284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    loading_fd = open(loading, O_WRONLY);
5304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(loading_fd < 0)
5314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto file_free_out;
5324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    data_fd = open(data, O_WRONLY);
5344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(data_fd < 0)
5354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto loading_close_out;
5364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
53702863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    fw_fd = open(file1, O_RDONLY);
53802863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    if(fw_fd < 0) {
53902863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland        fw_fd = open(file2, O_RDONLY);
54002863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland        if(fw_fd < 0)
54102863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland            goto data_close_out;
54202863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    }
5434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(!load_firmware(fw_fd, loading_fd, data_fd))
54502863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland        log_event_print("firmware copy success { '%s', '%s' }\n", root, uevent->firmware);
5464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    else
54702863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland        log_event_print("firmware copy failure { '%s', '%s' }\n", root, uevent->firmware);
5484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    close(fw_fd);
5504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectdata_close_out:
5514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    close(data_fd);
5524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectloading_close_out:
5534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    close(loading_fd);
5544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectfile_free_out:
55502863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    free(file1);
55602863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    free(file2);
5574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectdata_free_out:
5584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    free(data);
5594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectloading_free_out:
5604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    free(loading);
5614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectroot_free_out:
5624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    free(root);
5634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
5644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void handle_firmware_event(struct uevent *uevent)
5664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
5674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    pid_t pid;
568982a815c10cc28707d81c46112fc45b1c10df3beColin Cross    int status;
569982a815c10cc28707d81c46112fc45b1c10df3beColin Cross    int ret;
5704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(strcmp(uevent->subsystem, "firmware"))
5724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return;
5734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(strcmp(uevent->action, "add"))
5754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return;
5764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    /* we fork, to avoid making large memory allocations in init proper */
5784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    pid = fork();
5794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (!pid) {
5804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        process_firmware_event(uevent);
5814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        exit(EXIT_SUCCESS);
582982a815c10cc28707d81c46112fc45b1c10df3beColin Cross    } else {
583982a815c10cc28707d81c46112fc45b1c10df3beColin Cross        do {
584982a815c10cc28707d81c46112fc45b1c10df3beColin Cross            ret = waitpid(pid, &status, 0);
585982a815c10cc28707d81c46112fc45b1c10df3beColin Cross        } while (ret == -1 && errno == EINTR);
5864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
5874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
5884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
5894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define UEVENT_MSG_LEN  1024
5900dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossvoid handle_device_fd()
5914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
5925f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich    for(;;) {
5935f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        char msg[UEVENT_MSG_LEN+2];
5945f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
5955f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        struct iovec iov = {msg, sizeof(msg)};
5965f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        struct sockaddr_nl snl;
5975f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        struct msghdr hdr = {&snl, sizeof(snl), &iov, 1, cred_msg, sizeof(cred_msg), 0};
5985f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich
599fad7204e9eac20a87afacd7547ed8202a39319f8Nick Kralevich        ssize_t n = recvmsg(device_fd, &hdr, 0);
6005f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        if (n <= 0) {
6015f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich            break;
6025f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        }
6034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6045f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        if ((snl.nl_groups != 1) || (snl.nl_pid != 0)) {
6055f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich            /* ignoring non-kernel netlink multicast message */
6065f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich            continue;
6075f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        }
6085f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich
6095f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        struct cmsghdr * cmsg = CMSG_FIRSTHDR(&hdr);
6105f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
6115f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich            /* no sender credentials received, ignore message */
6125f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich            continue;
6135f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        }
6145f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich
6155f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        struct ucred * cred = (struct ucred *)CMSG_DATA(cmsg);
6165f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        if (cred->uid != 0) {
6175f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich            /* message from non-root user, ignore */
6185f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich            continue;
6195f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        }
6204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6215f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        if(n >= UEVENT_MSG_LEN)   /* overflow -- discard */
6224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            continue;
6234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        msg[n] = '\0';
6254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        msg[n+1] = '\0';
6264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6275f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        struct uevent uevent;
6284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        parse_event(msg, &uevent);
6294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        handle_device_event(&uevent);
6314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        handle_firmware_event(&uevent);
6324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
6334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
6344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project/* Coldboot walks parts of the /sys tree and pokes the uevent files
6364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** to cause the kernel to regenerate device add events that happened
6374f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** before init's device manager was started
6384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project**
6394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** We drain any pending events from the netlink socket every time
6404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** we poke another uevent file to make sure we don't overrun the
6414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** socket's buffer.
6424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project*/
6434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6440dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossstatic void do_coldboot(DIR *d)
6454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
6464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct dirent *de;
6474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int dfd, fd;
6484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    dfd = dirfd(d);
6504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    fd = openat(dfd, "uevent", O_WRONLY);
6524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(fd >= 0) {
6534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        write(fd, "add\n", 4);
6544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        close(fd);
6550dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross        handle_device_fd();
6564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
6574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    while((de = readdir(d))) {
6594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        DIR *d2;
6604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(de->d_type != DT_DIR || de->d_name[0] == '.')
6624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            continue;
6634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
6654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(fd < 0)
6664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            continue;
6674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        d2 = fdopendir(fd);
6694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(d2 == 0)
6704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            close(fd);
6714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        else {
6720dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross            do_coldboot(d2);
6734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            closedir(d2);
6744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
6754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
6764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
6774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6780dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossstatic void coldboot(const char *path)
6794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
6804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    DIR *d = opendir(path);
6814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(d) {
6820dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross        do_coldboot(d);
6834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        closedir(d);
6844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
6854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
6864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6870dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossvoid device_init(void)
6884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
6894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    suseconds_t t0, t1;
690f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross    struct stat info;
691f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross    int fd;
6924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6930dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross    device_fd = open_uevent_socket();
6940dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross    if(device_fd < 0)
6950dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross        return;
6964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6970dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross    fcntl(device_fd, F_SETFD, FD_CLOEXEC);
6980dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross    fcntl(device_fd, F_SETFL, O_NONBLOCK);
6994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
700f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross    if (stat(coldboot_done, &info) < 0) {
701f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        t0 = get_usecs();
702f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        coldboot("/sys/class");
703f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        coldboot("/sys/block");
704f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        coldboot("/sys/devices");
705f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        t1 = get_usecs();
706f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
707f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        close(fd);
708f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
709f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross    } else {
710f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        log_event_print("skipping coldboot, already done\n");
711f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross    }
7120dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross}
7134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7140dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossint get_device_fd()
7150dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross{
7160dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross    return device_fd;
7174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
718