devices.cpp revision 747383649c28198457aa2cbb5b93743fac837f06
14f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project/*
2154f4608aac6218af0e25c98b71d0803278c047eMark Salyzyn * Copyright (C) 2007-2014 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>
18c0c1ffea588c7d3c565b79d4f8bf3d4a8f75abc9Daniel Leung#include <fnmatch.h>
19b93e5812faffd3b6c5fb349072413aace31918d8Olivier Bailly#include <stddef.h>
204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <stdio.h>
214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <stdlib.h>
224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/stat.h>
234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/types.h>
244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <fcntl.h>
264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <dirent.h>
274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <unistd.h>
284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <string.h>
294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/socket.h>
314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/un.h>
324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <linux/netlink.h>
33e46f9d510db9351682cf17c49115110870147335Stephen Smalley
34e46f9d510db9351682cf17c49115110870147335Stephen Smalley#include <selinux/selinux.h>
35e46f9d510db9351682cf17c49115110870147335Stephen Smalley#include <selinux/label.h>
36ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley#include <selinux/android.h>
37e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley#include <selinux/avc.h>
38e46f9d510db9351682cf17c49115110870147335Stephen Smalley
394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <private/android_filesystem_config.h>
404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/time.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"
473312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann#include "ueventd_parser.h"
48b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross#include "util.h"
49ed8a7d84428ec945c48b6b53dc5a3a18fabaf683Colin Cross#include "log.h"
504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define SYSFS_PREFIX    "/sys"
52d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenbergstatic const char *firmware_dirs[] = { "/etc/firmware",
53d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg                                       "/vendor/firmware",
54d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg                                       "/firmware/image" };
554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
56e096e36e50b4b66638ebc4d3c09c2ee35f538dfaStephen Smalleyextern struct selabel_handle *sehandle;
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;
66f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong    const char *device_name;
67b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int partition_num;
684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int major;
694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int minor;
704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project};
714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct perms_ {
734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    char *name;
74bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    char *attr;
754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode_t perm;
764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned int uid;
774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned int gid;
784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned short prefix;
79c0c1ffea588c7d3c565b79d4f8bf3d4a8f75abc9Daniel Leung    unsigned short wildcard;
804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project};
814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct perm_node {
834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct perms_ dp;
844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct listnode plist;
854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project};
86bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
87fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstruct platform_node {
88fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    char *name;
89f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    char *path;
90f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    int path_len;
91fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct listnode list;
92fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross};
93fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
94bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandstatic list_declare(sys_perms);
9544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Crossstatic list_declare(dev_perms);
96fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic list_declare(platform_names);
974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
98bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandint add_dev_perms(const char *name, const char *attr,
99bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland                  mode_t perm, unsigned int uid, unsigned int gid,
100c0c1ffea588c7d3c565b79d4f8bf3d4a8f75abc9Daniel Leung                  unsigned short prefix,
101c0c1ffea588c7d3c565b79d4f8bf3d4a8f75abc9Daniel Leung                  unsigned short wildcard) {
102f3cf438714aa1284d8a58e2f3b108ba93f6d3abbElliott Hughes    struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
1034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (!node)
1044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -ENOMEM;
1054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
106bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    node->dp.name = strdup(name);
107bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    if (!node->dp.name)
1084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -ENOMEM;
1094f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
110bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    if (attr) {
111bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        node->dp.attr = strdup(attr);
112bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        if (!node->dp.attr)
113bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            return -ENOMEM;
114bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    }
115bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
1164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.perm = perm;
1174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.uid = uid;
1184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.gid = gid;
1194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.prefix = prefix;
120c0c1ffea588c7d3c565b79d4f8bf3d4a8f75abc9Daniel Leung    node->dp.wildcard = wildcard;
1214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
122bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    if (attr)
123bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        list_add_tail(&sys_perms, &node->plist);
124bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    else
125bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        list_add_tail(&dev_perms, &node->plist);
126bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
1274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return 0;
1284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
1294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
130bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandvoid fixup_sys_perms(const char *upath)
1314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
132bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    char buf[512];
133bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    struct listnode *node;
134bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    struct perms_ *dp;
1354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1363b4c0bda07ee883df8ad354d7288b7f37bec33baNick Kralevich    /* upaths omit the "/sys" that paths in this list
1373b4c0bda07ee883df8ad354d7288b7f37bec33baNick Kralevich     * contain, so we add 4 when comparing...
1383b4c0bda07ee883df8ad354d7288b7f37bec33baNick Kralevich     */
139bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    list_for_each(node, &sys_perms) {
140bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        dp = &(node_to_item(node, struct perm_node, plist))->dp;
141bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        if (dp->prefix) {
142bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
1434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                continue;
144c0c1ffea588c7d3c565b79d4f8bf3d4a8f75abc9Daniel Leung        } else if (dp->wildcard) {
145c0c1ffea588c7d3c565b79d4f8bf3d4a8f75abc9Daniel Leung            if (fnmatch(dp->name + 4, upath, FNM_PATHNAME) != 0)
146c0c1ffea588c7d3c565b79d4f8bf3d4a8f75abc9Daniel Leung                continue;
1474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else {
148bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            if (strcmp(upath, dp->name + 4))
1494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                continue;
1504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
151bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
152bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
1533b4c0bda07ee883df8ad354d7288b7f37bec33baNick Kralevich            break;
154bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
155e2d63af002a3b494f6bd464f2652b6e1997e7a52Yabin Cui        snprintf(buf, sizeof(buf), "/sys%s/%s", upath, dp->attr);
156bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
157bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        chown(buf, dp->uid, dp->gid);
158bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        chmod(buf, dp->perm);
1593b4c0bda07ee883df8ad354d7288b7f37bec33baNick Kralevich    }
1603b4c0bda07ee883df8ad354d7288b7f37bec33baNick Kralevich
1613b4c0bda07ee883df8ad354d7288b7f37bec33baNick Kralevich    // Now fixup SELinux file labels
1623b4c0bda07ee883df8ad354d7288b7f37bec33baNick Kralevich    int len = snprintf(buf, sizeof(buf), "/sys%s", upath);
1633b4c0bda07ee883df8ad354d7288b7f37bec33baNick Kralevich    if ((len < 0) || ((size_t) len >= sizeof(buf))) {
1643b4c0bda07ee883df8ad354d7288b7f37bec33baNick Kralevich        // Overflow
1653b4c0bda07ee883df8ad354d7288b7f37bec33baNick Kralevich        return;
1663b4c0bda07ee883df8ad354d7288b7f37bec33baNick Kralevich    }
1673b4c0bda07ee883df8ad354d7288b7f37bec33baNick Kralevich    if (access(buf, F_OK) == 0) {
1683b4c0bda07ee883df8ad354d7288b7f37bec33baNick Kralevich        INFO("restorecon_recursive: %s\n", buf);
1693b4c0bda07ee883df8ad354d7288b7f37bec33baNick Kralevich        restorecon_recursive(buf);
1704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
1714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
1724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
17343d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Crossstatic bool perm_path_matches(const char *path, struct perms_ *dp)
17443d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross{
17543d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross    if (dp->prefix) {
17643d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross        if (strncmp(path, dp->name, strlen(dp->name)) == 0)
17743d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross            return true;
17843d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross    } else if (dp->wildcard) {
17943d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross        if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
18043d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross            return true;
18143d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross    } else {
18243d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross        if (strcmp(path, dp->name) == 0)
18343d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross            return true;
18443d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross    }
18543d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross
18643d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross    return false;
18743d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross}
18843d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross
18943d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Crossstatic mode_t get_device_perm(const char *path, const char **links,
19043d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross                unsigned *uid, unsigned *gid)
1914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
19244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    struct listnode *node;
19344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    struct perm_node *perm_node;
19444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    struct perms_ *dp;
19544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross
19644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    /* search the perms list in reverse so that ueventd.$hardware can
19744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross     * override ueventd.rc
19844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross     */
19944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    list_for_each_reverse(node, &dev_perms) {
20043d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross        bool match = false;
20143d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross
20244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        perm_node = node_to_item(node, struct perm_node, plist);
20344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        dp = &perm_node->dp;
20444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross
20543d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross        if (perm_path_matches(path, dp)) {
20643d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross            match = true;
20744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        } else {
20843d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross            if (links) {
20943d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross                int i;
21043d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross                for (i = 0; links[i]; i++) {
21143d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross                    if (perm_path_matches(links[i], dp)) {
21243d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross                        match = true;
21343d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross                        break;
21443d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross                    }
21543d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross                }
21643d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross            }
21743d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross        }
21843d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross
21943d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross        if (match) {
22043d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross            *uid = dp->uid;
22143d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross            *gid = dp->gid;
22243d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross            return dp->perm;
2234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
2244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
22544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    /* Default if nothing found. */
22644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    *uid = 0;
22744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    *gid = 0;
22844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    return 0600;
2294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
2304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
231bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandstatic void make_device(const char *path,
232f682b4786a4093efb23bf80d69bf80eb274b145bElliott Hughes                        const char */*upath*/,
233b4c5200f51c3568f604a4557119ab545a6ddac94Stephen Smalley                        int block, int major, int minor,
234b4c5200f51c3568f604a4557119ab545a6ddac94Stephen Smalley                        const char **links)
2354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
2364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned uid;
2374f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned gid;
2384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode_t mode;
2394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    dev_t dev;
240e46f9d510db9351682cf17c49115110870147335Stephen Smalley    char *secontext = NULL;
2414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
24243d537e0deeb4ba20e13db22e4fc28b87a4621c7Colin Cross    mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
243b5982bf7c65b30be981ecfe1847437b05e32b2bbKenny Root
244e46f9d510db9351682cf17c49115110870147335Stephen Smalley    if (sehandle) {
245b4c5200f51c3568f604a4557119ab545a6ddac94Stephen Smalley        selabel_lookup_best_match(sehandle, &secontext, path, links, mode);
246e46f9d510db9351682cf17c49115110870147335Stephen Smalley        setfscreatecon(secontext);
247e46f9d510db9351682cf17c49115110870147335Stephen Smalley    }
248b5982bf7c65b30be981ecfe1847437b05e32b2bbKenny Root
24917dcc5c57dcffb919f3de65ff7a0134ffa3bd874Colin Cross    dev = makedev(major, minor);
2506405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    /* Temporarily change egid to avoid race condition setting the gid of the
2516405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * device node. Unforunately changing the euid would prevent creation of
2526405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * some device nodes, so the uid has to be set with chown() and is still
2536405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * racy. Fixing the gid race at least fixed the issue with system_server
2546405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * opening dynamic input devices under the AID_INPUT gid. */
2556405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    setegid(gid);
2564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mknod(path, mode, dev);
2576405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    chown(path, uid, -1);
2586405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    setegid(AID_ROOT);
259b5982bf7c65b30be981ecfe1847437b05e32b2bbKenny Root
260e46f9d510db9351682cf17c49115110870147335Stephen Smalley    if (secontext) {
261e46f9d510db9351682cf17c49115110870147335Stephen Smalley        freecon(secontext);
262e46f9d510db9351682cf17c49115110870147335Stephen Smalley        setfscreatecon(NULL);
263e46f9d510db9351682cf17c49115110870147335Stephen Smalley    }
264e46f9d510db9351682cf17c49115110870147335Stephen Smalley}
265e46f9d510db9351682cf17c49115110870147335Stephen Smalley
266f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavinstatic void add_platform_device(const char *path)
267fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{
268f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    int path_len = strlen(path);
269fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct listnode *node;
270fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct platform_node *bus;
271f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    const char *name = path;
272f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin
273f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    if (!strncmp(path, "/devices/", 9)) {
274f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin        name += 9;
275f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin        if (!strncmp(name, "platform/", 9))
276f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin            name += 9;
277f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    }
278fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
279fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    list_for_each_reverse(node, &platform_names) {
280fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        bus = node_to_item(node, struct platform_node, list);
281f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin        if ((bus->path_len < path_len) &&
282f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin                (path[bus->path_len] == '/') &&
283f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin                !strncmp(path, bus->path, bus->path_len))
284fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            /* subdevice of an existing platform, ignore it */
285fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            return;
286fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    }
287fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
288f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    INFO("adding platform device %s (%s)\n", name, path);
289fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
290f3cf438714aa1284d8a58e2f3b108ba93f6d3abbElliott Hughes    bus = (platform_node*) calloc(1, sizeof(struct platform_node));
291f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    bus->path = strdup(path);
292f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    bus->path_len = path_len;
293f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    bus->name = bus->path + (name - path);
294fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    list_add_tail(&platform_names, &bus->list);
295fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross}
296fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
297fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross/*
298f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin * given a path that may start with a platform device, find the length of the
299fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross * platform device prefix.  If it doesn't start with a platform device, return
300fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross * 0.
301fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross */
302f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavinstatic struct platform_node *find_platform_device(const char *path)
303fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{
304f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    int path_len = strlen(path);
305fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct listnode *node;
306fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct platform_node *bus;
307fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
308fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    list_for_each_reverse(node, &platform_names) {
309fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        bus = node_to_item(node, struct platform_node, list);
310f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin        if ((bus->path_len < path_len) &&
311f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin                (path[bus->path_len] == '/') &&
312f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin                !strncmp(path, bus->path, bus->path_len))
313f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin            return bus;
314fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    }
315fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
316fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    return NULL;
317fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross}
318fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
319f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavinstatic void remove_platform_device(const char *path)
320fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{
321fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct listnode *node;
322fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct platform_node *bus;
323fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
324fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    list_for_each_reverse(node, &platform_names) {
325fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        bus = node_to_item(node, struct platform_node, list);
326f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin        if (!strcmp(path, bus->path)) {
327f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin            INFO("removing platform device %s\n", bus->name);
328f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin            free(bus->path);
329fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            list_remove(node);
330fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            free(bus);
331fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            return;
332fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        }
333fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    }
334fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross}
335fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
336a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie/* Given a path that may start with a PCI device, populate the supplied buffer
337a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie * with the PCI domain/bus number and the peripheral ID and return 0.
338a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie * If it doesn't start with a PCI device, or there is some error, return -1 */
339a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boiestatic int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
340a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie{
341a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    const char *start, *end;
342a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie
343a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    if (strncmp(path, "/devices/pci", 12))
344a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie        return -1;
345a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie
346a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    /* Beginning of the prefix is the initial "pci" after "/devices/" */
347a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    start = path + 9;
348a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie
349a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    /* End of the prefix is two path '/' later, capturing the domain/bus number
350a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie     * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
351a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    end = strchr(start, '/');
352a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    if (!end)
353a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie        return -1;
354a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    end = strchr(end + 1, '/');
355a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    if (!end)
356a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie        return -1;
357a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie
358a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    /* Make sure we have enough room for the string plus null terminator */
359a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    if (end - start + 1 > buf_sz)
360a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie        return -1;
361a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie
362a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    strncpy(buf, start, end - start);
363a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    buf[end - start] = '\0';
364a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    return 0;
365a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie}
366a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie
3674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void parse_event(const char *msg, struct uevent *uevent)
3684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
3694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->action = "";
3704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->path = "";
3714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->subsystem = "";
3724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->firmware = "";
3734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->major = -1;
3744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->minor = -1;
375b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    uevent->partition_name = NULL;
376b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    uevent->partition_num = -1;
377f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong    uevent->device_name = NULL;
3784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        /* currently ignoring SEQNUM */
3804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    while(*msg) {
3814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(!strncmp(msg, "ACTION=", 7)) {
3824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 7;
3834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->action = msg;
3844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "DEVPATH=", 8)) {
3854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 8;
3864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->path = msg;
3874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
3884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 10;
3894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->subsystem = msg;
3904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "FIRMWARE=", 9)) {
3914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 9;
3924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->firmware = msg;
3934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "MAJOR=", 6)) {
3944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 6;
3954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->major = atoi(msg);
3964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "MINOR=", 6)) {
3974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 6;
3984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->minor = atoi(msg);
399b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        } else if(!strncmp(msg, "PARTN=", 6)) {
400b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            msg += 6;
401b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            uevent->partition_num = atoi(msg);
402b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        } else if(!strncmp(msg, "PARTNAME=", 9)) {
403b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            msg += 9;
404b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            uevent->partition_name = msg;
405f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong        } else if(!strncmp(msg, "DEVNAME=", 8)) {
406f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong            msg += 8;
407f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong            uevent->device_name = msg;
4084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
4094f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
410f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong        /* advance to after the next \0 */
4114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        while(*msg++)
4124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            ;
4134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
4144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
415c0e919c92062360a69b771722677d041c9998403Elliott Hughes    if (LOG_UEVENTS) {
416c0e919c92062360a69b771722677d041c9998403Elliott Hughes        INFO("event { '%s', '%s', '%s', '%s', %d, %d }\n",
417c0e919c92062360a69b771722677d041c9998403Elliott Hughes             uevent->action, uevent->path, uevent->subsystem,
418c0e919c92062360a69b771722677d041c9998403Elliott Hughes             uevent->firmware, uevent->major, uevent->minor);
419c0e919c92062360a69b771722677d041c9998403Elliott Hughes    }
4204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
4214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
422d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Gobystatic char **get_character_device_symlinks(struct uevent *uevent)
423d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby{
424d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    const char *parent;
425d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    char *slash;
426d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    char **links;
427d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    int link_num = 0;
428d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    int width;
429f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    struct platform_node *pdev;
430d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
431f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    pdev = find_platform_device(uevent->path);
432f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    if (!pdev)
433d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        return NULL;
434d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
435f3cf438714aa1284d8a58e2f3b108ba93f6d3abbElliott Hughes    links = (char**) malloc(sizeof(char *) * 2);
436d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    if (!links)
437d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        return NULL;
438d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    memset(links, 0, sizeof(char *) * 2);
439d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
440d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    /* skip "/devices/platform/<driver>" */
441f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    parent = strchr(uevent->path + pdev->path_len, '/');
442fca58f4b18e0066fc7829b152f0093daa51281e0Tomasz Kondel    if (!parent)
443d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        goto err;
444d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
445d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    if (!strncmp(parent, "/usb", 4)) {
446d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        /* skip root hub name and device. use device interface */
447d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        while (*++parent && *parent != '/');
448d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (*parent)
449d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            while (*++parent && *parent != '/');
450d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (!*parent)
451d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            goto err;
452d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        slash = strchr(++parent, '/');
453d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (!slash)
454d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            goto err;
455d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        width = slash - parent;
456d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (width <= 0)
457d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            goto err;
458d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
459d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
460d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            link_num++;
461d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        else
462d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            links[link_num] = NULL;
463d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        mkdir("/dev/usb", 0755);
464d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    }
465d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    else {
466d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        goto err;
467d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    }
468d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
469d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    return links;
470d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Gobyerr:
471d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    free(links);
472d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    return NULL;
473d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby}
474d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
475a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boiestatic char **get_block_device_symlinks(struct uevent *uevent)
476b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross{
477fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    const char *device;
478f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    struct platform_node *pdev;
479b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char *slash;
480a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    const char *type;
481b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char buf[256];
482b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char link_path[256];
483b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int link_num = 0;
484b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char *p;
485b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
486f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    pdev = find_platform_device(uevent->path);
487a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    if (pdev) {
488a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie        device = pdev->name;
489a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie        type = "platform";
490a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
491a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie        device = buf;
492a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie        type = "pci";
493a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    } else {
494f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin        return NULL;
495a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    }
496f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin
497f3cf438714aa1284d8a58e2f3b108ba93f6d3abbElliott Hughes    char **links = (char**) malloc(sizeof(char *) * 4);
498b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (!links)
499b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        return NULL;
500b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    memset(links, 0, sizeof(char *) * 4);
501b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
502a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    INFO("found %s device %s\n", type, device);
503fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
504a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie    snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
505b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
506b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (uevent->partition_name) {
507b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        p = strdup(uevent->partition_name);
508b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        sanitize(p);
50993ca79b44535ee24ece08001e1948070db6ab6edJohan Redestig        if (strcmp(uevent->partition_name, p))
51093ca79b44535ee24ece08001e1948070db6ab6edJohan Redestig            NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p);
511b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
512b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            link_num++;
513b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        else
514b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            links[link_num] = NULL;
515b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        free(p);
516b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    }
517b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
518b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (uevent->partition_num >= 0) {
519b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
520b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            link_num++;
521b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        else
522b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            links[link_num] = NULL;
523b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    }
524b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
525f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    slash = strrchr(uevent->path, '/');
526b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
527b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        link_num++;
528b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    else
529b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        links[link_num] = NULL;
530b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
531b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    return links;
532b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross}
533b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
534eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_device(const char *action, const char *devpath,
535eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        const char *path, int block, int major, int minor, char **links)
5364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
537b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int i;
5384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
539eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if(!strcmp(action, "add")) {
540b4c5200f51c3568f604a4557119ab545a6ddac94Stephen Smalley        make_device(devpath, path, block, major, minor, (const char **)links);
541b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (links) {
542b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            for (i = 0; links[i]; i++)
543b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross                make_link(devpath, links[i]);
544b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        }
5454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
5464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
547eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if(!strcmp(action, "remove")) {
548b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (links) {
549b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            for (i = 0; links[i]; i++)
550b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross                remove_link(devpath, links[i]);
551b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        }
5524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        unlink(devpath);
553b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    }
554b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
555b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (links) {
556b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        for (i = 0; links[i]; i++)
557b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            free(links[i]);
558b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        free(links);
5594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
5604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
5614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
562fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic void handle_platform_device_event(struct uevent *uevent)
563fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{
564f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    const char *path = uevent->path;
565fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
566fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    if (!strcmp(uevent->action, "add"))
567f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin        add_platform_device(path);
568fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    else if (!strcmp(uevent->action, "remove"))
569f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin        remove_platform_device(path);
570fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross}
571fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
572eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic const char *parse_device_name(struct uevent *uevent, unsigned int len)
573eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{
574eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    const char *name;
575eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
576eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    /* if it's not a /dev device, nothing else to do */
577eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if((uevent->major < 0) || (uevent->minor < 0))
578eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return NULL;
579eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
580eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    /* do we have a name? */
581eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    name = strrchr(uevent->path, '/');
582eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if(!name)
583eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return NULL;
584eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    name++;
585eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
586eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    /* too-long names would overrun our buffer */
587f6e009ee2650d8812942aa7f5761e86402346739Greg Hackmann    if(strlen(name) > len) {
588f6e009ee2650d8812942aa7f5761e86402346739Greg Hackmann        ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n",
589f6e009ee2650d8812942aa7f5761e86402346739Greg Hackmann                name, len);
590eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return NULL;
591f6e009ee2650d8812942aa7f5761e86402346739Greg Hackmann    }
592eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
593eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    return name;
594eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross}
595eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
596eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_block_device_event(struct uevent *uevent)
597eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{
598eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    const char *base = "/dev/block/";
599eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    const char *name;
600eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    char devpath[96];
601eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    char **links = NULL;
602eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
603eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    name = parse_device_name(uevent, 64);
604eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!name)
605eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return;
606eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
607eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    snprintf(devpath, sizeof(devpath), "%s%s", base, name);
608e46f9d510db9351682cf17c49115110870147335Stephen Smalley    make_dir(base, 0755);
609eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
610f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin    if (!strncmp(uevent->path, "/devices/", 9))
611a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie        links = get_block_device_symlinks(uevent);
612eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
613eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    handle_device(uevent->action, devpath, uevent->path, 1,
614eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross            uevent->major, uevent->minor, links);
615eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross}
616eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
6173312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann#define DEVPATH_LEN 96
6183312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann
6193312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmannstatic bool assemble_devpath(char *devpath, const char *dirname,
6203312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann        const char *devname)
6213312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann{
6223312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann    int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
6233312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann    if (s < 0) {
6243312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann        ERROR("failed to assemble device path (%s); ignoring event\n",
6253312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann                strerror(errno));
6263312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann        return false;
6273312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann    } else if (s >= DEVPATH_LEN) {
6283312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann        ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n",
6293312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann                dirname, devname, DEVPATH_LEN);
6303312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann        return false;
6313312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann    }
6323312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann    return true;
6333312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann}
6343312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann
6353312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmannstatic void mkdir_recursive_for_devpath(const char *devpath)
6363312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann{
6373312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann    char dir[DEVPATH_LEN];
6383312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann    char *slash;
6393312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann
6403312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann    strcpy(dir, devpath);
6413312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann    slash = strrchr(dir, '/');
6423312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann    *slash = '\0';
6433312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann    mkdir_recursive(dir, 0755);
6443312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann}
6453312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann
646eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_generic_device_event(struct uevent *uevent)
647eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{
648f3cf438714aa1284d8a58e2f3b108ba93f6d3abbElliott Hughes    const char *base;
649eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    const char *name;
6503312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann    char devpath[DEVPATH_LEN] = {0};
651eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    char **links = NULL;
652eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
653eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    name = parse_device_name(uevent, 64);
654eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!name)
655eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return;
656eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
6573312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann    struct ueventd_subsystem *subsystem =
6583312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann            ueventd_subsystem_find_by_name(uevent->subsystem);
6593312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann
6603312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann    if (subsystem) {
6613312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann        const char *devname;
6623312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann
6633312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann        switch (subsystem->devname_src) {
6643312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann        case DEVNAME_UEVENT_DEVNAME:
6653312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann            devname = uevent->device_name;
6663312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann            break;
6673312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann
6683312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann        case DEVNAME_UEVENT_DEVPATH:
6693312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann            devname = name;
6703312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann            break;
6713312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann
6723312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann        default:
6733312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann            ERROR("%s subsystem's devpath option is not set; ignoring event\n",
6743312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann                    uevent->subsystem);
6753312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann            return;
6763312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann        }
6773312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann
6783312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann        if (!assemble_devpath(devpath, subsystem->dirname, devname))
6793312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann            return;
6803312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann        mkdir_recursive_for_devpath(devpath);
6813312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann    } else if (!strncmp(uevent->subsystem, "usb", 3)) {
682eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         if (!strcmp(uevent->subsystem, "usb")) {
683f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong            if (uevent->device_name) {
6843312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann                if (!assemble_devpath(devpath, "/dev", uevent->device_name))
685f6e009ee2650d8812942aa7f5761e86402346739Greg Hackmann                    return;
6863312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann                mkdir_recursive_for_devpath(devpath);
687f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong             }
688f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong             else {
689f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 /* This imitates the file system that would be created
690f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                  * if we were using devfs instead.
691f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                  * Minors are broken up into groups of 128, starting at "001"
692f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                  */
693f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 int bus_id = uevent->minor / 128 + 1;
694f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 int device_id = uevent->minor % 128 + 1;
695f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 /* build directories */
696f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 make_dir("/dev/bus", 0755);
697f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 make_dir("/dev/bus/usb", 0755);
698f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
699f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 make_dir(devpath, 0755);
700f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
701f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong             }
702eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         } else {
703eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross             /* ignore other USB events */
704eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross             return;
705eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         }
706eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
707eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/graphics/";
708e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
709e6f8d45da8c61d67dc9fdc1cec53f40594043817Lukasz Anaczkowski     } else if (!strncmp(uevent->subsystem, "drm", 3)) {
710e6f8d45da8c61d67dc9fdc1cec53f40594043817Lukasz Anaczkowski         base = "/dev/dri/";
711e6f8d45da8c61d67dc9fdc1cec53f40594043817Lukasz Anaczkowski         make_dir(base, 0755);
712eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
713eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/oncrpc/";
714e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
715eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
716eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/adsp/";
717e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
718eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
719eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/msm_camera/";
720e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
721eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if(!strncmp(uevent->subsystem, "input", 5)) {
722eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/input/";
723e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
724eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
725eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/mtd/";
726e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
727eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if(!strncmp(uevent->subsystem, "sound", 5)) {
728eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/snd/";
729e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
730eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if(!strncmp(uevent->subsystem, "misc", 4) &&
731eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross                 !strncmp(name, "log_", 4)) {
732f682b4786a4093efb23bf80d69bf80eb274b145bElliott Hughes         INFO("kernel logger is deprecated\n");
733eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/log/";
734e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
735eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         name += 4;
736eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else
737eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/";
738eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     links = get_character_device_symlinks(uevent);
739eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
740eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     if (!devpath[0])
741eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         snprintf(devpath, sizeof(devpath), "%s%s", base, name);
742eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
743eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     handle_device(uevent->action, devpath, uevent->path, 0,
744eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross             uevent->major, uevent->minor, links);
745eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross}
746eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
747eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_device_event(struct uevent *uevent)
748eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{
74975b287b771b302c99797d812122b72f83d2f56f9Ruchi Kandoi    if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
750eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        fixup_sys_perms(uevent->path);
751eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
752eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!strncmp(uevent->subsystem, "block", 5)) {
753eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        handle_block_device_event(uevent);
754fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    } else if (!strncmp(uevent->subsystem, "platform", 8)) {
755fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        handle_platform_device_event(uevent);
756eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    } else {
757eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        handle_generic_device_event(uevent);
758eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    }
759eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross}
760eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
7614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic int load_firmware(int fw_fd, int loading_fd, int data_fd)
7624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
7634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct stat st;
7644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    long len_to_copy;
7654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int ret = 0;
7664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(fstat(fw_fd, &st) < 0)
7684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -1;
7694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    len_to_copy = st.st_size;
7704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    write(loading_fd, "1", 1);  /* start transfer */
7724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    while (len_to_copy > 0) {
7744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        char buf[PAGE_SIZE];
7754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        ssize_t nr;
7764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        nr = read(fw_fd, buf, sizeof(buf));
7784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(!nr)
7794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            break;
7804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(nr < 0) {
7814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            ret = -1;
7824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            break;
7834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
7844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        len_to_copy -= nr;
7864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        while (nr > 0) {
7874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            ssize_t nw = 0;
7884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            nw = write(data_fd, buf + nw, nr);
7904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            if(nw <= 0) {
7914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                ret = -1;
7924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                goto out;
7934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            }
7944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            nr -= nw;
7954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
7964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
7974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectout:
7994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(!ret)
8004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        write(loading_fd, "0", 1);  /* successful end of transfer */
8014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    else
8024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        write(loading_fd, "-1", 2); /* abort transfer */
8034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return ret;
8054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
8064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8078d48c8e45724c7103f0ace7885d339e49399908bBrian Swetlandstatic int is_booting(void)
8088d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland{
8098d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland    return access("/dev/.booting", F_OK) == 0;
8108d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland}
8118d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland
8124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void process_firmware_event(struct uevent *uevent)
8134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
814d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg    char *root, *loading, *data;
8154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int l, loading_fd, data_fd, fw_fd;
816d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg    size_t i;
8178d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland    int booting = is_booting();
8184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8198d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland    INFO("firmware: loading '%s' for '%s'\n",
8208d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland         uevent->firmware, uevent->path);
8214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
8234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
8244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return;
8254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    l = asprintf(&loading, "%sloading", root);
8274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
8284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto root_free_out;
8294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    l = asprintf(&data, "%sdata", root);
8314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
8324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto loading_free_out;
8334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
83445a884f85f8eae602fe6e7c9718c90db65675e07Nick Kralevich    loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
8354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(loading_fd < 0)
836d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg        goto data_free_out;
8374f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
83845a884f85f8eae602fe6e7c9718c90db65675e07Nick Kralevich    data_fd = open(data, O_WRONLY|O_CLOEXEC);
8394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(data_fd < 0)
8404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto loading_close_out;
8414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8428d48c8e45724c7103f0ace7885d339e49399908bBrian Swetlandtry_loading_again:
843d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg    for (i = 0; i < ARRAY_SIZE(firmware_dirs); i++) {
844d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg        char *file = NULL;
845d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg        l = asprintf(&file, "%s/%s", firmware_dirs[i], uevent->firmware);
846d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg        if (l == -1)
847d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg            goto data_free_out;
848d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg        fw_fd = open(file, O_RDONLY|O_CLOEXEC);
849d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg        free(file);
850d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg        if (fw_fd >= 0) {
851d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg            if(!load_firmware(fw_fd, loading_fd, data_fd))
852d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg                INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
853d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg            else
854d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg                INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
855d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg            break;
856609d8828d3b8266a80606bf902d1294296962cf3Benoit Goby        }
85702863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    }
858d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg    if (fw_fd < 0) {
859d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg        if (booting) {
860d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg            /* If we're not fully booted, we may be missing
861d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg             * filesystems needed for firmware, wait and retry.
862d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg             */
863d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg            usleep(100000);
864d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg            booting = is_booting();
865d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg            goto try_loading_again;
866d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg        }
867cd67f00e18de1c8d48ddb082ecb902fc42c2baccElliott Hughes        INFO("firmware: could not open '%s': %s\n", uevent->firmware, strerror(errno));
868d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg        write(loading_fd, "-1", 2);
869d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg        goto data_close_out;
870d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg    }
8714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    close(fw_fd);
8734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectdata_close_out:
8744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    close(data_fd);
8754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectloading_close_out:
8764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    close(loading_fd);
8774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectdata_free_out:
8784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    free(data);
8794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectloading_free_out:
8804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    free(loading);
8814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectroot_free_out:
8824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    free(root);
8834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
8844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void handle_firmware_event(struct uevent *uevent)
8864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
8874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    pid_t pid;
8884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(strcmp(uevent->subsystem, "firmware"))
8904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return;
8914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(strcmp(uevent->action, "add"))
8934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return;
8944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    /* we fork, to avoid making large memory allocations in init proper */
8964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    pid = fork();
8974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (!pid) {
8984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        process_firmware_event(uevent);
89917baff480a7be5c07be629ada4fe940bf394ff31Kenny Root        _exit(EXIT_SUCCESS);
90017baff480a7be5c07be629ada4fe940bf394ff31Kenny Root    } else if (pid < 0) {
901c0e919c92062360a69b771722677d041c9998403Elliott Hughes        ERROR("could not fork to process firmware event: %s\n", strerror(errno));
9024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
9034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
9044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
905c603720aed304fdd36a5815f81eb22bd4d9968b7Ruchi Kandoi#define UEVENT_MSG_LEN  2048
9060dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossvoid handle_device_fd()
9074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
9083f582e92f07d358f3d0941b86407b39e2e7e67ccVernon Tang    char msg[UEVENT_MSG_LEN+2];
9093f582e92f07d358f3d0941b86407b39e2e7e67ccVernon Tang    int n;
91057de8b8f27f887ca8084671df777a4ac199ba647Nick Kralevich    while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
9115f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        if(n >= UEVENT_MSG_LEN)   /* overflow -- discard */
9124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            continue;
9134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
9144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        msg[n] = '\0';
9154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        msg[n+1] = '\0';
9164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
9175f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        struct uevent uevent;
9184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        parse_event(msg, &uevent);
9194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
920e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley        if (sehandle && selinux_status_updated() > 0) {
921e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley            struct selabel_handle *sehandle2;
922e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley            sehandle2 = selinux_android_file_context_handle();
923e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley            if (sehandle2) {
924e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley                selabel_close(sehandle);
925e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley                sehandle = sehandle2;
926e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley            }
927e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley        }
928e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley
9294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        handle_device_event(&uevent);
9304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        handle_firmware_event(&uevent);
9314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
9324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
9334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
9344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project/* Coldboot walks parts of the /sys tree and pokes the uevent files
9354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** to cause the kernel to regenerate device add events that happened
9364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** before init's device manager was started
9374f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project**
9384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** We drain any pending events from the netlink socket every time
9394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** we poke another uevent file to make sure we don't overrun the
940c0e919c92062360a69b771722677d041c9998403Elliott Hughes** socket's buffer.
9414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project*/
9424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
9430dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossstatic void do_coldboot(DIR *d)
9444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
9454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct dirent *de;
9464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int dfd, fd;
9474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
9484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    dfd = dirfd(d);
9494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
9504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    fd = openat(dfd, "uevent", O_WRONLY);
9514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(fd >= 0) {
9524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        write(fd, "add\n", 4);
9534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        close(fd);
9540dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross        handle_device_fd();
9554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
9564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
9574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    while((de = readdir(d))) {
9584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        DIR *d2;
9594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
9604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(de->d_type != DT_DIR || de->d_name[0] == '.')
9614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            continue;
9624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
9634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
9644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(fd < 0)
9654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            continue;
9664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
9674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        d2 = fdopendir(fd);
9684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(d2 == 0)
9694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            close(fd);
9704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        else {
9710dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross            do_coldboot(d2);
9724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            closedir(d2);
9734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
9744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
9754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
9764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
9770dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossstatic void coldboot(const char *path)
9784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
9794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    DIR *d = opendir(path);
9804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(d) {
9810dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross        do_coldboot(d);
9824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        closedir(d);
9834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
9844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
9854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
986747383649c28198457aa2cbb5b93743fac837f06Elliott Hughesvoid device_init() {
987ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley    sehandle = NULL;
988ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley    if (is_selinux_enabled() > 0) {
989ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley        sehandle = selinux_android_file_context_handle();
990e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley        selinux_status_open(true);
991ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley    }
992b5982bf7c65b30be981ecfe1847437b05e32b2bbKenny Root
993d562ca78aa713d186e83f05061c0aed88e200bcdAndrew Boie    /* is 256K enough? udev uses 16MB! */
994d562ca78aa713d186e83f05061c0aed88e200bcdAndrew Boie    device_fd = uevent_open_socket(256*1024, true);
9950dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross    if(device_fd < 0)
9960dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross        return;
9974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
9980dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross    fcntl(device_fd, F_SETFL, O_NONBLOCK);
9994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1000747383649c28198457aa2cbb5b93743fac837f06Elliott Hughes    struct stat info;
1001a016c42b4f7122625a417929afa0e9379ffa4887Andreas Gampe    if (stat(COLDBOOT_DONE, &info) < 0) {
1002da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes        Timer t;
1003f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        coldboot("/sys/class");
1004f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        coldboot("/sys/block");
1005f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        coldboot("/sys/devices");
1006747383649c28198457aa2cbb5b93743fac837f06Elliott Hughes        close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
1007da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes        NOTICE("Coldboot took %.2fs.\n", t.duration());
1008da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes    } else {
1009da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes        NOTICE("Skipping coldboot, already done!\n");
1010f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross    }
10110dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross}
10124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
10130dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossint get_device_fd()
10140dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross{
10150dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross    return device_fd;
10164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
1017