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 platform_node *bus; 270f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin const char *name = path; 271f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin 272f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin if (!strncmp(path, "/devices/", 9)) { 273f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin name += 9; 274f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin if (!strncmp(name, "platform/", 9)) 275f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin name += 9; 276f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin } 277fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 278f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin INFO("adding platform device %s (%s)\n", name, path); 279fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 280f3cf438714aa1284d8a58e2f3b108ba93f6d3abbElliott Hughes bus = (platform_node*) calloc(1, sizeof(struct platform_node)); 281f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin bus->path = strdup(path); 282f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin bus->path_len = path_len; 283f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin bus->name = bus->path + (name - path); 284fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross list_add_tail(&platform_names, &bus->list); 285fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross} 286fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 287fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross/* 288f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin * given a path that may start with a platform device, find the length of the 289fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross * platform device prefix. If it doesn't start with a platform device, return 290fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross * 0. 291fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross */ 292f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavinstatic struct platform_node *find_platform_device(const char *path) 293fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{ 294f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin int path_len = strlen(path); 295fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross struct listnode *node; 296fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross struct platform_node *bus; 297fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 298fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross list_for_each_reverse(node, &platform_names) { 299fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross bus = node_to_item(node, struct platform_node, list); 300f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin if ((bus->path_len < path_len) && 301f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin (path[bus->path_len] == '/') && 302f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin !strncmp(path, bus->path, bus->path_len)) 303f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin return bus; 304fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross } 305fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 306fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross return NULL; 307fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross} 308fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 309f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavinstatic void remove_platform_device(const char *path) 310fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{ 311fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross struct listnode *node; 312fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross struct platform_node *bus; 313fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 314fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross list_for_each_reverse(node, &platform_names) { 315fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross bus = node_to_item(node, struct platform_node, list); 316f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin if (!strcmp(path, bus->path)) { 317f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin INFO("removing platform device %s\n", bus->name); 318f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin free(bus->path); 319fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross list_remove(node); 320fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross free(bus); 321fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross return; 322fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross } 323fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross } 324fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross} 325fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 326a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie/* Given a path that may start with a PCI device, populate the supplied buffer 327a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie * with the PCI domain/bus number and the peripheral ID and return 0. 328a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie * If it doesn't start with a PCI device, or there is some error, return -1 */ 329a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boiestatic int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz) 330a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie{ 331a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie const char *start, *end; 332a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie 333a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie if (strncmp(path, "/devices/pci", 12)) 334a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie return -1; 335a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie 336a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie /* Beginning of the prefix is the initial "pci" after "/devices/" */ 337a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie start = path + 9; 338a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie 339a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie /* End of the prefix is two path '/' later, capturing the domain/bus number 340a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */ 341a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie end = strchr(start, '/'); 342a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie if (!end) 343a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie return -1; 344a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie end = strchr(end + 1, '/'); 345a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie if (!end) 346a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie return -1; 347a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie 348a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie /* Make sure we have enough room for the string plus null terminator */ 349a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie if (end - start + 1 > buf_sz) 350a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie return -1; 351a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie 352a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie strncpy(buf, start, end - start); 353a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie buf[end - start] = '\0'; 354a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie return 0; 355a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie} 356a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie 3574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void parse_event(const char *msg, struct uevent *uevent) 3584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 3594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->action = ""; 3604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->path = ""; 3614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->subsystem = ""; 3624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->firmware = ""; 3634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->major = -1; 3644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->minor = -1; 365b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross uevent->partition_name = NULL; 366b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross uevent->partition_num = -1; 367f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong uevent->device_name = NULL; 3684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 3694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project /* currently ignoring SEQNUM */ 3704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project while(*msg) { 3714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(!strncmp(msg, "ACTION=", 7)) { 3724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg += 7; 3734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->action = msg; 3744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if(!strncmp(msg, "DEVPATH=", 8)) { 3754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg += 8; 3764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->path = msg; 3774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if(!strncmp(msg, "SUBSYSTEM=", 10)) { 3784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg += 10; 3794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->subsystem = msg; 3804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if(!strncmp(msg, "FIRMWARE=", 9)) { 3814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg += 9; 3824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->firmware = msg; 3834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if(!strncmp(msg, "MAJOR=", 6)) { 3844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg += 6; 3854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->major = atoi(msg); 3864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if(!strncmp(msg, "MINOR=", 6)) { 3874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg += 6; 3884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->minor = atoi(msg); 389b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } else if(!strncmp(msg, "PARTN=", 6)) { 390b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross msg += 6; 391b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross uevent->partition_num = atoi(msg); 392b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } else if(!strncmp(msg, "PARTNAME=", 9)) { 393b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross msg += 9; 394b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross uevent->partition_name = msg; 395f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong } else if(!strncmp(msg, "DEVNAME=", 8)) { 396f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong msg += 8; 397f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong uevent->device_name = msg; 3984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 3994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 400f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong /* advance to after the next \0 */ 4014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project while(*msg++) 4024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project ; 4034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 4044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 405c0e919c92062360a69b771722677d041c9998403Elliott Hughes if (LOG_UEVENTS) { 406c0e919c92062360a69b771722677d041c9998403Elliott Hughes INFO("event { '%s', '%s', '%s', '%s', %d, %d }\n", 407c0e919c92062360a69b771722677d041c9998403Elliott Hughes uevent->action, uevent->path, uevent->subsystem, 408c0e919c92062360a69b771722677d041c9998403Elliott Hughes uevent->firmware, uevent->major, uevent->minor); 409c0e919c92062360a69b771722677d041c9998403Elliott Hughes } 4104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 4114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 412d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Gobystatic char **get_character_device_symlinks(struct uevent *uevent) 413d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby{ 414d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby const char *parent; 415d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby char *slash; 416d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby char **links; 417d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby int link_num = 0; 418d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby int width; 419f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin struct platform_node *pdev; 420d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 421f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin pdev = find_platform_device(uevent->path); 422f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin if (!pdev) 423d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby return NULL; 424d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 425f3cf438714aa1284d8a58e2f3b108ba93f6d3abbElliott Hughes links = (char**) malloc(sizeof(char *) * 2); 426d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (!links) 427d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby return NULL; 428d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby memset(links, 0, sizeof(char *) * 2); 429d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 430d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby /* skip "/devices/platform/<driver>" */ 431f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin parent = strchr(uevent->path + pdev->path_len, '/'); 432fca58f4b18e0066fc7829b152f0093daa51281e0Tomasz Kondel if (!parent) 433d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby goto err; 434d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 435d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (!strncmp(parent, "/usb", 4)) { 436d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby /* skip root hub name and device. use device interface */ 437d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby while (*++parent && *parent != '/'); 438d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (*parent) 439d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby while (*++parent && *parent != '/'); 440d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (!*parent) 441d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby goto err; 442d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby slash = strchr(++parent, '/'); 443d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (!slash) 444d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby goto err; 445d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby width = slash - parent; 446d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (width <= 0) 447d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby goto err; 448d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 449d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0) 450d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby link_num++; 451d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby else 452d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby links[link_num] = NULL; 453d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby mkdir("/dev/usb", 0755); 454d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby } 455d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby else { 456d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby goto err; 457d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby } 458d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 459d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby return links; 460d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Gobyerr: 461d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby free(links); 462d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby return NULL; 463d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby} 464d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 465a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boiestatic char **get_block_device_symlinks(struct uevent *uevent) 466b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross{ 467fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross const char *device; 468f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin struct platform_node *pdev; 469b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross char *slash; 470a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie const char *type; 471b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross char buf[256]; 472b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross char link_path[256]; 473b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross int link_num = 0; 474b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross char *p; 475b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 476f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin pdev = find_platform_device(uevent->path); 477a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie if (pdev) { 478a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie device = pdev->name; 479a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie type = "platform"; 480a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) { 481a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie device = buf; 482a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie type = "pci"; 483a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie } else { 484f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin return NULL; 485a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie } 486f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin 487f3cf438714aa1284d8a58e2f3b108ba93f6d3abbElliott Hughes char **links = (char**) malloc(sizeof(char *) * 4); 488b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (!links) 489b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross return NULL; 490b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross memset(links, 0, sizeof(char *) * 4); 491b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 492a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie INFO("found %s device %s\n", type, device); 493fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 494a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device); 495b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 496b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (uevent->partition_name) { 497b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross p = strdup(uevent->partition_name); 498b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross sanitize(p); 49993ca79b44535ee24ece08001e1948070db6ab6edJohan Redestig if (strcmp(uevent->partition_name, p)) 50093ca79b44535ee24ece08001e1948070db6ab6edJohan Redestig NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p); 501b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0) 502b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross link_num++; 503b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross else 504b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross links[link_num] = NULL; 505b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross free(p); 506b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } 507b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 508b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (uevent->partition_num >= 0) { 509b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0) 510b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross link_num++; 511b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross else 512b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross links[link_num] = NULL; 513b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } 514b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 515f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin slash = strrchr(uevent->path, '/'); 516b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0) 517b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross link_num++; 518b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross else 519b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross links[link_num] = NULL; 520b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 521b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross return links; 522b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross} 523b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 524eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_device(const char *action, const char *devpath, 525eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross const char *path, int block, int major, int minor, char **links) 5264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 527b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross int i; 5284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 529eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross if(!strcmp(action, "add")) { 530b4c5200f51c3568f604a4557119ab545a6ddac94Stephen Smalley make_device(devpath, path, block, major, minor, (const char **)links); 531b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (links) { 532b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross for (i = 0; links[i]; i++) 53379f338465213885900cea5a39f3aeeea083bbe51Chris Fries make_link_init(devpath, links[i]); 534b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } 5354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 5364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 537eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross if(!strcmp(action, "remove")) { 538b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (links) { 539b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross for (i = 0; links[i]; i++) 540b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross remove_link(devpath, links[i]); 541b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } 5424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project unlink(devpath); 543b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } 544b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 545b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (links) { 546b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross for (i = 0; links[i]; i++) 547b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross free(links[i]); 548b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross free(links); 5494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 5504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 5514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 552fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic void handle_platform_device_event(struct uevent *uevent) 553fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{ 554f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin const char *path = uevent->path; 555fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 556fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross if (!strcmp(uevent->action, "add")) 557f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin add_platform_device(path); 558fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross else if (!strcmp(uevent->action, "remove")) 559f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin remove_platform_device(path); 560fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross} 561fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 562eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic const char *parse_device_name(struct uevent *uevent, unsigned int len) 563eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{ 564eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross const char *name; 565eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 566eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross /* if it's not a /dev device, nothing else to do */ 567eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross if((uevent->major < 0) || (uevent->minor < 0)) 568eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross return NULL; 569eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 570eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross /* do we have a name? */ 571eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross name = strrchr(uevent->path, '/'); 572eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross if(!name) 573eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross return NULL; 574eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross name++; 575eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 576eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross /* too-long names would overrun our buffer */ 577f6e009ee2650d8812942aa7f5761e86402346739Greg Hackmann if(strlen(name) > len) { 578f6e009ee2650d8812942aa7f5761e86402346739Greg Hackmann ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n", 579f6e009ee2650d8812942aa7f5761e86402346739Greg Hackmann name, len); 580eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross return NULL; 581f6e009ee2650d8812942aa7f5761e86402346739Greg Hackmann } 582eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 583eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross return name; 584eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross} 585eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 586eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_block_device_event(struct uevent *uevent) 587eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{ 588eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross const char *base = "/dev/block/"; 589eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross const char *name; 590eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross char devpath[96]; 591eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross char **links = NULL; 592eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 593eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross name = parse_device_name(uevent, 64); 594eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross if (!name) 595eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross return; 596eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 597eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross snprintf(devpath, sizeof(devpath), "%s%s", base, name); 598e46f9d510db9351682cf17c49115110870147335Stephen Smalley make_dir(base, 0755); 599eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 600f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin if (!strncmp(uevent->path, "/devices/", 9)) 601a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie links = get_block_device_symlinks(uevent); 602eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 603eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross handle_device(uevent->action, devpath, uevent->path, 1, 604eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross uevent->major, uevent->minor, links); 605eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross} 606eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 6073312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann#define DEVPATH_LEN 96 6083312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann 6093312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmannstatic bool assemble_devpath(char *devpath, const char *dirname, 6103312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann const char *devname) 6113312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann{ 6123312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname); 6133312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann if (s < 0) { 6143312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann ERROR("failed to assemble device path (%s); ignoring event\n", 6153312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann strerror(errno)); 6163312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann return false; 6173312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann } else if (s >= DEVPATH_LEN) { 6183312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n", 6193312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann dirname, devname, DEVPATH_LEN); 6203312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann return false; 6213312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann } 6223312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann return true; 6233312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann} 6243312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann 6253312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmannstatic void mkdir_recursive_for_devpath(const char *devpath) 6263312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann{ 6273312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann char dir[DEVPATH_LEN]; 6283312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann char *slash; 6293312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann 6303312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann strcpy(dir, devpath); 6313312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann slash = strrchr(dir, '/'); 6323312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann *slash = '\0'; 6333312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann mkdir_recursive(dir, 0755); 6343312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann} 6353312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann 636eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_generic_device_event(struct uevent *uevent) 637eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{ 638f3cf438714aa1284d8a58e2f3b108ba93f6d3abbElliott Hughes const char *base; 639eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross const char *name; 6403312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann char devpath[DEVPATH_LEN] = {0}; 641eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross char **links = NULL; 642eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 643eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross name = parse_device_name(uevent, 64); 644eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross if (!name) 645eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross return; 646eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 6473312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann struct ueventd_subsystem *subsystem = 6483312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann ueventd_subsystem_find_by_name(uevent->subsystem); 6493312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann 6503312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann if (subsystem) { 6513312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann const char *devname; 6523312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann 6533312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann switch (subsystem->devname_src) { 6543312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann case DEVNAME_UEVENT_DEVNAME: 6553312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann devname = uevent->device_name; 6563312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann break; 6573312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann 6583312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann case DEVNAME_UEVENT_DEVPATH: 6593312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann devname = name; 6603312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann break; 6613312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann 6623312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann default: 6633312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann ERROR("%s subsystem's devpath option is not set; ignoring event\n", 6643312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann uevent->subsystem); 6653312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann return; 6663312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann } 6673312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann 6683312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann if (!assemble_devpath(devpath, subsystem->dirname, devname)) 6693312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann return; 6703312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann mkdir_recursive_for_devpath(devpath); 6713312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann } else if (!strncmp(uevent->subsystem, "usb", 3)) { 672eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross if (!strcmp(uevent->subsystem, "usb")) { 673f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong if (uevent->device_name) { 6743312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann if (!assemble_devpath(devpath, "/dev", uevent->device_name)) 675f6e009ee2650d8812942aa7f5761e86402346739Greg Hackmann return; 6763312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann mkdir_recursive_for_devpath(devpath); 677f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong } 678f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong else { 679f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong /* This imitates the file system that would be created 680f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong * if we were using devfs instead. 681f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong * Minors are broken up into groups of 128, starting at "001" 682f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong */ 683f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong int bus_id = uevent->minor / 128 + 1; 684f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong int device_id = uevent->minor % 128 + 1; 685f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong /* build directories */ 686f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong make_dir("/dev/bus", 0755); 687f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong make_dir("/dev/bus/usb", 0755); 688f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id); 689f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong make_dir(devpath, 0755); 690f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id); 691f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong } 692eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else { 693eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross /* ignore other USB events */ 694eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross return; 695eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } 696eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else if (!strncmp(uevent->subsystem, "graphics", 8)) { 697eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross base = "/dev/graphics/"; 698e46f9d510db9351682cf17c49115110870147335Stephen Smalley make_dir(base, 0755); 699e6f8d45da8c61d67dc9fdc1cec53f40594043817Lukasz Anaczkowski } else if (!strncmp(uevent->subsystem, "drm", 3)) { 700e6f8d45da8c61d67dc9fdc1cec53f40594043817Lukasz Anaczkowski base = "/dev/dri/"; 701e6f8d45da8c61d67dc9fdc1cec53f40594043817Lukasz Anaczkowski make_dir(base, 0755); 702eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) { 703eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross base = "/dev/oncrpc/"; 704e46f9d510db9351682cf17c49115110870147335Stephen Smalley make_dir(base, 0755); 705eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else if (!strncmp(uevent->subsystem, "adsp", 4)) { 706eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross base = "/dev/adsp/"; 707e46f9d510db9351682cf17c49115110870147335Stephen Smalley make_dir(base, 0755); 708eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) { 709eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross base = "/dev/msm_camera/"; 710e46f9d510db9351682cf17c49115110870147335Stephen Smalley make_dir(base, 0755); 711eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else if(!strncmp(uevent->subsystem, "input", 5)) { 712eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross base = "/dev/input/"; 713e46f9d510db9351682cf17c49115110870147335Stephen Smalley make_dir(base, 0755); 714eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else if(!strncmp(uevent->subsystem, "mtd", 3)) { 715eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross base = "/dev/mtd/"; 716e46f9d510db9351682cf17c49115110870147335Stephen Smalley make_dir(base, 0755); 717eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else if(!strncmp(uevent->subsystem, "sound", 5)) { 718eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross base = "/dev/snd/"; 719e46f9d510db9351682cf17c49115110870147335Stephen Smalley make_dir(base, 0755); 720eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else if(!strncmp(uevent->subsystem, "misc", 4) && 721eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross !strncmp(name, "log_", 4)) { 722f682b4786a4093efb23bf80d69bf80eb274b145bElliott Hughes INFO("kernel logger is deprecated\n"); 723eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross base = "/dev/log/"; 724e46f9d510db9351682cf17c49115110870147335Stephen Smalley make_dir(base, 0755); 725eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross name += 4; 726eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else 727eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross base = "/dev/"; 728eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross links = get_character_device_symlinks(uevent); 729eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 730eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross if (!devpath[0]) 731eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross snprintf(devpath, sizeof(devpath), "%s%s", base, name); 732eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 733eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross handle_device(uevent->action, devpath, uevent->path, 0, 734eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross uevent->major, uevent->minor, links); 735eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross} 736eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 737eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_device_event(struct uevent *uevent) 738eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{ 73975b287b771b302c99797d812122b72f83d2f56f9Ruchi Kandoi if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online")) 740eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross fixup_sys_perms(uevent->path); 741eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 742eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross if (!strncmp(uevent->subsystem, "block", 5)) { 743eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross handle_block_device_event(uevent); 744fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross } else if (!strncmp(uevent->subsystem, "platform", 8)) { 745fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross handle_platform_device_event(uevent); 746eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else { 747eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross handle_generic_device_event(uevent); 748eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } 749eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross} 750eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 7514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic int load_firmware(int fw_fd, int loading_fd, int data_fd) 7524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 7534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project struct stat st; 7544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project long len_to_copy; 7554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int ret = 0; 7564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(fstat(fw_fd, &st) < 0) 7584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return -1; 7594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project len_to_copy = st.st_size; 7604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project write(loading_fd, "1", 1); /* start transfer */ 7624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project while (len_to_copy > 0) { 7644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project char buf[PAGE_SIZE]; 7654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project ssize_t nr; 7664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project nr = read(fw_fd, buf, sizeof(buf)); 7684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(!nr) 7694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project break; 7704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(nr < 0) { 7714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project ret = -1; 7724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project break; 7734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 7744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project len_to_copy -= nr; 7764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project while (nr > 0) { 7774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project ssize_t nw = 0; 7784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project nw = write(data_fd, buf + nw, nr); 7804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(nw <= 0) { 7814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project ret = -1; 7824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project goto out; 7834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 7844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project nr -= nw; 7854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 7864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 7874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectout: 7894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(!ret) 7904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project write(loading_fd, "0", 1); /* successful end of transfer */ 7914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project else 7924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project write(loading_fd, "-1", 2); /* abort transfer */ 7934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return ret; 7954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 7964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7978d48c8e45724c7103f0ace7885d339e49399908bBrian Swetlandstatic int is_booting(void) 7988d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland{ 7998d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland return access("/dev/.booting", F_OK) == 0; 8008d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland} 8018d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland 8024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void process_firmware_event(struct uevent *uevent) 8034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 804d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg char *root, *loading, *data; 8054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int l, loading_fd, data_fd, fw_fd; 806d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg size_t i; 8078d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland int booting = is_booting(); 8084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8098d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland INFO("firmware: loading '%s' for '%s'\n", 8108d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland uevent->firmware, uevent->path); 8114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path); 8134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (l == -1) 8144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return; 8154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project l = asprintf(&loading, "%sloading", root); 8174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (l == -1) 8184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project goto root_free_out; 8194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project l = asprintf(&data, "%sdata", root); 8214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (l == -1) 8224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project goto loading_free_out; 8234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 82445a884f85f8eae602fe6e7c9718c90db65675e07Nick Kralevich loading_fd = open(loading, O_WRONLY|O_CLOEXEC); 8254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(loading_fd < 0) 826d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg goto data_free_out; 8274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 82845a884f85f8eae602fe6e7c9718c90db65675e07Nick Kralevich data_fd = open(data, O_WRONLY|O_CLOEXEC); 8294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(data_fd < 0) 8304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project goto loading_close_out; 8314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8328d48c8e45724c7103f0ace7885d339e49399908bBrian Swetlandtry_loading_again: 833d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg for (i = 0; i < ARRAY_SIZE(firmware_dirs); i++) { 834d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg char *file = NULL; 835d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg l = asprintf(&file, "%s/%s", firmware_dirs[i], uevent->firmware); 836d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg if (l == -1) 837d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg goto data_free_out; 838d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg fw_fd = open(file, O_RDONLY|O_CLOEXEC); 839d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg free(file); 840d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg if (fw_fd >= 0) { 841d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg if(!load_firmware(fw_fd, loading_fd, data_fd)) 842d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware); 843d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg else 844d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware); 845d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg break; 846609d8828d3b8266a80606bf902d1294296962cf3Benoit Goby } 84702863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland } 848d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg if (fw_fd < 0) { 849d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg if (booting) { 850d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg /* If we're not fully booted, we may be missing 851d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg * filesystems needed for firmware, wait and retry. 852d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg */ 853d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg usleep(100000); 854d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg booting = is_booting(); 855d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg goto try_loading_again; 856d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg } 857cd67f00e18de1c8d48ddb082ecb902fc42c2baccElliott Hughes INFO("firmware: could not open '%s': %s\n", uevent->firmware, strerror(errno)); 858d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg write(loading_fd, "-1", 2); 859d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg goto data_close_out; 860d1d9602f89f5c7c0307a1f55e66401173f48f07eDaniel Rosenberg } 8614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project close(fw_fd); 8634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectdata_close_out: 8644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project close(data_fd); 8654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectloading_close_out: 8664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project close(loading_fd); 8674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectdata_free_out: 8684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project free(data); 8694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectloading_free_out: 8704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project free(loading); 8714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectroot_free_out: 8724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project free(root); 8734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 8744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void handle_firmware_event(struct uevent *uevent) 8764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 8774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project pid_t pid; 8784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(strcmp(uevent->subsystem, "firmware")) 8804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return; 8814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(strcmp(uevent->action, "add")) 8834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return; 8844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project /* we fork, to avoid making large memory allocations in init proper */ 8864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project pid = fork(); 8874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (!pid) { 8884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project process_firmware_event(uevent); 88917baff480a7be5c07be629ada4fe940bf394ff31Kenny Root _exit(EXIT_SUCCESS); 89017baff480a7be5c07be629ada4fe940bf394ff31Kenny Root } else if (pid < 0) { 891c0e919c92062360a69b771722677d041c9998403Elliott Hughes ERROR("could not fork to process firmware event: %s\n", strerror(errno)); 8924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 8934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 8944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 895c603720aed304fdd36a5815f81eb22bd4d9968b7Ruchi Kandoi#define UEVENT_MSG_LEN 2048 8960dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossvoid handle_device_fd() 8974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 8983f582e92f07d358f3d0941b86407b39e2e7e67ccVernon Tang char msg[UEVENT_MSG_LEN+2]; 8993f582e92f07d358f3d0941b86407b39e2e7e67ccVernon Tang int n; 90057de8b8f27f887ca8084671df777a4ac199ba647Nick Kralevich while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) { 9015f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich if(n >= UEVENT_MSG_LEN) /* overflow -- discard */ 9024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project continue; 9034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg[n] = '\0'; 9054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg[n+1] = '\0'; 9064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9075f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich struct uevent uevent; 9084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project parse_event(msg, &uevent); 9094f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 910e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley if (sehandle && selinux_status_updated() > 0) { 911e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley struct selabel_handle *sehandle2; 912e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley sehandle2 = selinux_android_file_context_handle(); 913e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley if (sehandle2) { 914e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley selabel_close(sehandle); 915e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley sehandle = sehandle2; 916e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley } 917e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley } 918e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley 9194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project handle_device_event(&uevent); 9204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project handle_firmware_event(&uevent); 9214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 9224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 9234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project/* Coldboot walks parts of the /sys tree and pokes the uevent files 9254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** to cause the kernel to regenerate device add events that happened 9264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** before init's device manager was started 9274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** 9284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** We drain any pending events from the netlink socket every time 9294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** we poke another uevent file to make sure we don't overrun the 930c0e919c92062360a69b771722677d041c9998403Elliott Hughes** socket's buffer. 9314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project*/ 9324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9330dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossstatic void do_coldboot(DIR *d) 9344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 9354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project struct dirent *de; 9364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int dfd, fd; 9374f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project dfd = dirfd(d); 9394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project fd = openat(dfd, "uevent", O_WRONLY); 9414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(fd >= 0) { 9424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project write(fd, "add\n", 4); 9434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project close(fd); 9440dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross handle_device_fd(); 9454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 9464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project while((de = readdir(d))) { 9484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project DIR *d2; 9494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(de->d_type != DT_DIR || de->d_name[0] == '.') 9514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project continue; 9524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); 9544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(fd < 0) 9554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project continue; 9564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project d2 = fdopendir(fd); 9584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(d2 == 0) 9594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project close(fd); 9604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project else { 9610dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross do_coldboot(d2); 9624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project closedir(d2); 9634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 9644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 9654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 9664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9670dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossstatic void coldboot(const char *path) 9684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 9694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project DIR *d = opendir(path); 9704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(d) { 9710dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross do_coldboot(d); 9724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project closedir(d); 9734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 9744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 9754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 976747383649c28198457aa2cbb5b93743fac837f06Elliott Hughesvoid device_init() { 977ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley sehandle = NULL; 978ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley if (is_selinux_enabled() > 0) { 979ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley sehandle = selinux_android_file_context_handle(); 980e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley selinux_status_open(true); 981ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley } 982b5982bf7c65b30be981ecfe1847437b05e32b2bbKenny Root 983d562ca78aa713d186e83f05061c0aed88e200bcdAndrew Boie /* is 256K enough? udev uses 16MB! */ 984d562ca78aa713d186e83f05061c0aed88e200bcdAndrew Boie device_fd = uevent_open_socket(256*1024, true); 98556a0656ed91a01fdf09f9704a1f8a8aeb5d9f3b7Elliott Hughes if (device_fd == -1) { 9860dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross return; 98756a0656ed91a01fdf09f9704a1f8a8aeb5d9f3b7Elliott Hughes } 9880dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross fcntl(device_fd, F_SETFL, O_NONBLOCK); 9894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 99056a0656ed91a01fdf09f9704a1f8a8aeb5d9f3b7Elliott Hughes if (access(COLDBOOT_DONE, F_OK) == 0) { 991da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes NOTICE("Skipping coldboot, already done!\n"); 99256a0656ed91a01fdf09f9704a1f8a8aeb5d9f3b7Elliott Hughes return; 993f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross } 99456a0656ed91a01fdf09f9704a1f8a8aeb5d9f3b7Elliott Hughes 99556a0656ed91a01fdf09f9704a1f8a8aeb5d9f3b7Elliott Hughes Timer t; 99656a0656ed91a01fdf09f9704a1f8a8aeb5d9f3b7Elliott Hughes coldboot("/sys/class"); 99756a0656ed91a01fdf09f9704a1f8a8aeb5d9f3b7Elliott Hughes coldboot("/sys/block"); 99856a0656ed91a01fdf09f9704a1f8a8aeb5d9f3b7Elliott Hughes coldboot("/sys/devices"); 99956a0656ed91a01fdf09f9704a1f8a8aeb5d9f3b7Elliott Hughes close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000)); 100056a0656ed91a01fdf09f9704a1f8a8aeb5d9f3b7Elliott Hughes NOTICE("Coldboot took %.2fs.\n", t.duration()); 10010dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross} 10024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 10030dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossint get_device_fd() 10040dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross{ 10050dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross return device_fd; 10064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 1007