devices.cpp revision c603720aed304fdd36a5815f81eb22bd4d9968b7
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 51154f4608aac6218af0e25c98b71d0803278c047eMark Salyzyn#define UNUSED __attribute__((__unused__)) 52154f4608aac6218af0e25c98b71d0803278c047eMark Salyzyn 534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define SYSFS_PREFIX "/sys" 5402863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland#define FIRMWARE_DIR1 "/etc/firmware" 5502863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland#define FIRMWARE_DIR2 "/vendor/firmware" 56029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev#define FIRMWARE_DIR3 "/firmware/image" 574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 58e096e36e50b4b66638ebc4d3c09c2ee35f538dfaStephen Smalleyextern struct selabel_handle *sehandle; 59e46f9d510db9351682cf17c49115110870147335Stephen Smalley 600dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossstatic int device_fd = -1; 610dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross 624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct uevent { 634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project const char *action; 644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project const char *path; 654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project const char *subsystem; 664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project const char *firmware; 67b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross const char *partition_name; 68f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong const char *device_name; 69b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross int partition_num; 704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int major; 714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int minor; 724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}; 734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct perms_ { 754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project char *name; 76bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland char *attr; 774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mode_t perm; 784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project unsigned int uid; 794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project unsigned int gid; 804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project unsigned short prefix; 81c0c1ffea588c7d3c565b79d4f8bf3d4a8f75abc9Daniel Leung unsigned short wildcard; 824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}; 834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct perm_node { 854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project struct perms_ dp; 864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project struct listnode plist; 874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}; 88bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland 89fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstruct platform_node { 90fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross char *name; 91f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin char *path; 92f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin int path_len; 93fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross struct listnode list; 94fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross}; 95fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 96bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandstatic list_declare(sys_perms); 9744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Crossstatic list_declare(dev_perms); 98fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic list_declare(platform_names); 994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 100bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandint add_dev_perms(const char *name, const char *attr, 101bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland mode_t perm, unsigned int uid, unsigned int gid, 102c0c1ffea588c7d3c565b79d4f8bf3d4a8f75abc9Daniel Leung unsigned short prefix, 103c0c1ffea588c7d3c565b79d4f8bf3d4a8f75abc9Daniel Leung unsigned short wildcard) { 104bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland struct perm_node *node = calloc(1, sizeof(*node)); 1054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (!node) 1064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return -ENOMEM; 1074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 108bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland node->dp.name = strdup(name); 109bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland if (!node->dp.name) 1104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return -ENOMEM; 1114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 112bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland if (attr) { 113bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland node->dp.attr = strdup(attr); 114bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland if (!node->dp.attr) 115bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland return -ENOMEM; 116bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland } 117bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland 1184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project node->dp.perm = perm; 1194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project node->dp.uid = uid; 1204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project node->dp.gid = gid; 1214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project node->dp.prefix = prefix; 122c0c1ffea588c7d3c565b79d4f8bf3d4a8f75abc9Daniel Leung node->dp.wildcard = wildcard; 1234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 124bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland if (attr) 125bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland list_add_tail(&sys_perms, &node->plist); 126bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland else 127bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland list_add_tail(&dev_perms, &node->plist); 128bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland 1294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return 0; 1304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 1314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 132bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandvoid fixup_sys_perms(const char *upath) 1334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 134bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland char buf[512]; 135bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland struct listnode *node; 136bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland struct perms_ *dp; 1375f7b017f41b5bd0b86b5078b17c41ef7bc201c8dStephen Smalley char *secontext; 1384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 139bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland /* upaths omit the "/sys" that paths in this list 140bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland * contain, so we add 4 when comparing... 141bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland */ 142bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland list_for_each(node, &sys_perms) { 143bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland dp = &(node_to_item(node, struct perm_node, plist))->dp; 144bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland if (dp->prefix) { 145bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland if (strncmp(upath, dp->name + 4, strlen(dp->name + 4))) 1464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project continue; 147c0c1ffea588c7d3c565b79d4f8bf3d4a8f75abc9Daniel Leung } else if (dp->wildcard) { 148c0c1ffea588c7d3c565b79d4f8bf3d4a8f75abc9Daniel Leung if (fnmatch(dp->name + 4, upath, FNM_PATHNAME) != 0) 149c0c1ffea588c7d3c565b79d4f8bf3d4a8f75abc9Daniel Leung continue; 1504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else { 151bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland if (strcmp(upath, dp->name + 4)) 1524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project continue; 1534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 154bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland 155bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf)) 156bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland return; 157bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland 158bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland sprintf(buf,"/sys%s/%s", upath, dp->attr); 159bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm); 160bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland chown(buf, dp->uid, dp->gid); 161bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland chmod(buf, dp->perm); 1625f7b017f41b5bd0b86b5078b17c41ef7bc201c8dStephen Smalley if (sehandle) { 1635f7b017f41b5bd0b86b5078b17c41ef7bc201c8dStephen Smalley secontext = NULL; 1645f7b017f41b5bd0b86b5078b17c41ef7bc201c8dStephen Smalley selabel_lookup(sehandle, &secontext, buf, 0); 1655f7b017f41b5bd0b86b5078b17c41ef7bc201c8dStephen Smalley if (secontext) { 1665f7b017f41b5bd0b86b5078b17c41ef7bc201c8dStephen Smalley setfilecon(buf, secontext); 1675f7b017f41b5bd0b86b5078b17c41ef7bc201c8dStephen Smalley freecon(secontext); 1685f7b017f41b5bd0b86b5078b17c41ef7bc201c8dStephen Smalley } 1695f7b017f41b5bd0b86b5078b17c41ef7bc201c8dStephen Smalley } 1704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 1714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 1724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 1734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid) 1744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 1754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mode_t perm; 17644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross struct listnode *node; 17744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross struct perm_node *perm_node; 17844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross struct perms_ *dp; 17944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 18044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross /* search the perms list in reverse so that ueventd.$hardware can 18144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross * override ueventd.rc 18244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross */ 18344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross list_for_each_reverse(node, &dev_perms) { 18444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross perm_node = node_to_item(node, struct perm_node, plist); 18544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross dp = &perm_node->dp; 18644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 18744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross if (dp->prefix) { 18844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross if (strncmp(path, dp->name, strlen(dp->name))) 18944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross continue; 190c0c1ffea588c7d3c565b79d4f8bf3d4a8f75abc9Daniel Leung } else if (dp->wildcard) { 191c0c1ffea588c7d3c565b79d4f8bf3d4a8f75abc9Daniel Leung if (fnmatch(dp->name, path, FNM_PATHNAME) != 0) 192c0c1ffea588c7d3c565b79d4f8bf3d4a8f75abc9Daniel Leung continue; 19344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross } else { 19444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross if (strcmp(path, dp->name)) 19544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross continue; 1964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 19744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross *uid = dp->uid; 19844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross *gid = dp->gid; 19944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross return dp->perm; 2004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 20144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross /* Default if nothing found. */ 20244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross *uid = 0; 20344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross *gid = 0; 20444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross return 0600; 2054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 2064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 207bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandstatic void make_device(const char *path, 208154f4608aac6218af0e25c98b71d0803278c047eMark Salyzyn const char *upath UNUSED, 209b4c5200f51c3568f604a4557119ab545a6ddac94Stephen Smalley int block, int major, int minor, 210b4c5200f51c3568f604a4557119ab545a6ddac94Stephen Smalley const char **links) 2114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 2124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project unsigned uid; 2134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project unsigned gid; 2144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mode_t mode; 2154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project dev_t dev; 216e46f9d510db9351682cf17c49115110870147335Stephen Smalley char *secontext = NULL; 2174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 2184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR); 219b5982bf7c65b30be981ecfe1847437b05e32b2bbKenny Root 220e46f9d510db9351682cf17c49115110870147335Stephen Smalley if (sehandle) { 221b4c5200f51c3568f604a4557119ab545a6ddac94Stephen Smalley selabel_lookup_best_match(sehandle, &secontext, path, links, mode); 222e46f9d510db9351682cf17c49115110870147335Stephen Smalley setfscreatecon(secontext); 223e46f9d510db9351682cf17c49115110870147335Stephen Smalley } 224b5982bf7c65b30be981ecfe1847437b05e32b2bbKenny Root 22517dcc5c57dcffb919f3de65ff7a0134ffa3bd874Colin Cross dev = makedev(major, minor); 2266405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly /* Temporarily change egid to avoid race condition setting the gid of the 2276405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly * device node. Unforunately changing the euid would prevent creation of 2286405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly * some device nodes, so the uid has to be set with chown() and is still 2296405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly * racy. Fixing the gid race at least fixed the issue with system_server 2306405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly * opening dynamic input devices under the AID_INPUT gid. */ 2316405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly setegid(gid); 2324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mknod(path, mode, dev); 2336405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly chown(path, uid, -1); 2346405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly setegid(AID_ROOT); 235b5982bf7c65b30be981ecfe1847437b05e32b2bbKenny Root 236e46f9d510db9351682cf17c49115110870147335Stephen Smalley if (secontext) { 237e46f9d510db9351682cf17c49115110870147335Stephen Smalley freecon(secontext); 238e46f9d510db9351682cf17c49115110870147335Stephen Smalley setfscreatecon(NULL); 239e46f9d510db9351682cf17c49115110870147335Stephen Smalley } 240e46f9d510db9351682cf17c49115110870147335Stephen Smalley} 241e46f9d510db9351682cf17c49115110870147335Stephen Smalley 242f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavinstatic void add_platform_device(const char *path) 243fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{ 244f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin int path_len = strlen(path); 245fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross struct listnode *node; 246fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross struct platform_node *bus; 247f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin const char *name = path; 248f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin 249f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin if (!strncmp(path, "/devices/", 9)) { 250f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin name += 9; 251f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin if (!strncmp(name, "platform/", 9)) 252f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin name += 9; 253f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin } 254fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 255fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross list_for_each_reverse(node, &platform_names) { 256fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross bus = node_to_item(node, struct platform_node, list); 257f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin if ((bus->path_len < path_len) && 258f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin (path[bus->path_len] == '/') && 259f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin !strncmp(path, bus->path, bus->path_len)) 260fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross /* subdevice of an existing platform, ignore it */ 261fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross return; 262fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross } 263fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 264f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin INFO("adding platform device %s (%s)\n", name, path); 265fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 266fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross bus = calloc(1, sizeof(struct platform_node)); 267f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin bus->path = strdup(path); 268f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin bus->path_len = path_len; 269f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin bus->name = bus->path + (name - path); 270fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross list_add_tail(&platform_names, &bus->list); 271fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross} 272fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 273fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross/* 274f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin * given a path that may start with a platform device, find the length of the 275fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross * platform device prefix. If it doesn't start with a platform device, return 276fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross * 0. 277fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross */ 278f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavinstatic struct platform_node *find_platform_device(const char *path) 279fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{ 280f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin int path_len = strlen(path); 281fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross struct listnode *node; 282fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross struct platform_node *bus; 283fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 284fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross list_for_each_reverse(node, &platform_names) { 285fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross bus = node_to_item(node, struct platform_node, list); 286f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin if ((bus->path_len < path_len) && 287f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin (path[bus->path_len] == '/') && 288f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin !strncmp(path, bus->path, bus->path_len)) 289f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin return bus; 290fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross } 291fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 292fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross return NULL; 293fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross} 294fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 295f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavinstatic void remove_platform_device(const char *path) 296fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{ 297fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross struct listnode *node; 298fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross struct platform_node *bus; 299fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 300fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross list_for_each_reverse(node, &platform_names) { 301fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross bus = node_to_item(node, struct platform_node, list); 302f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin if (!strcmp(path, bus->path)) { 303f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin INFO("removing platform device %s\n", bus->name); 304f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin free(bus->path); 305fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross list_remove(node); 306fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross free(bus); 307fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross return; 308fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross } 309fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross } 310fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross} 311fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 312a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie/* Given a path that may start with a PCI device, populate the supplied buffer 313a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie * with the PCI domain/bus number and the peripheral ID and return 0. 314a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie * If it doesn't start with a PCI device, or there is some error, return -1 */ 315a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boiestatic int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz) 316a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie{ 317a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie const char *start, *end; 318a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie 319a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie if (strncmp(path, "/devices/pci", 12)) 320a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie return -1; 321a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie 322a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie /* Beginning of the prefix is the initial "pci" after "/devices/" */ 323a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie start = path + 9; 324a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie 325a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie /* End of the prefix is two path '/' later, capturing the domain/bus number 326a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */ 327a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie end = strchr(start, '/'); 328a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie if (!end) 329a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie return -1; 330a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie end = strchr(end + 1, '/'); 331a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie if (!end) 332a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie return -1; 333a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie 334a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie /* Make sure we have enough room for the string plus null terminator */ 335a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie if (end - start + 1 > buf_sz) 336a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie return -1; 337a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie 338a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie strncpy(buf, start, end - start); 339a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie buf[end - start] = '\0'; 340a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie return 0; 341a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie} 342a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie 3431e070846f8908dff15086efa12fbba01ab7eae15Chuck Tuffli#if LOG_UEVENTS 3444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 3454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic inline suseconds_t get_usecs(void) 3464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 3474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project struct timeval tv; 3484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project gettimeofday(&tv, 0); 3494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec; 3504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 3514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 3524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define log_event_print(x...) INFO(x) 3534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 3544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#else 3554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 3564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define log_event_print(fmt, args...) do { } while (0) 3574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define get_usecs() 0 3584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 3594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#endif 3604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 3614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void parse_event(const char *msg, struct uevent *uevent) 3624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 3634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->action = ""; 3644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->path = ""; 3654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->subsystem = ""; 3664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->firmware = ""; 3674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->major = -1; 3684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->minor = -1; 369b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross uevent->partition_name = NULL; 370b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross uevent->partition_num = -1; 371f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong uevent->device_name = NULL; 3724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 3734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project /* currently ignoring SEQNUM */ 3744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project while(*msg) { 3754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(!strncmp(msg, "ACTION=", 7)) { 3764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg += 7; 3774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->action = msg; 3784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if(!strncmp(msg, "DEVPATH=", 8)) { 3794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg += 8; 3804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->path = msg; 3814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if(!strncmp(msg, "SUBSYSTEM=", 10)) { 3824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg += 10; 3834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->subsystem = msg; 3844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if(!strncmp(msg, "FIRMWARE=", 9)) { 3854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg += 9; 3864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->firmware = msg; 3874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if(!strncmp(msg, "MAJOR=", 6)) { 3884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg += 6; 3894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->major = atoi(msg); 3904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if(!strncmp(msg, "MINOR=", 6)) { 3914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg += 6; 3924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->minor = atoi(msg); 393b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } else if(!strncmp(msg, "PARTN=", 6)) { 394b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross msg += 6; 395b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross uevent->partition_num = atoi(msg); 396b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } else if(!strncmp(msg, "PARTNAME=", 9)) { 397b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross msg += 9; 398b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross uevent->partition_name = msg; 399f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong } else if(!strncmp(msg, "DEVNAME=", 8)) { 400f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong msg += 8; 401f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong uevent->device_name = msg; 4024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 4034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 404f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong /* advance to after the next \0 */ 4054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project while(*msg++) 4064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project ; 4074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 4084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 4094f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n", 4104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->action, uevent->path, uevent->subsystem, 4114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->firmware, uevent->major, uevent->minor); 4124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 4134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 414d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Gobystatic char **get_character_device_symlinks(struct uevent *uevent) 415d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby{ 416d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby const char *parent; 417d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby char *slash; 418d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby char **links; 419d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby int link_num = 0; 420d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby int width; 421f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin struct platform_node *pdev; 422d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 423f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin pdev = find_platform_device(uevent->path); 424f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin if (!pdev) 425d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby return NULL; 426d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 427d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby links = malloc(sizeof(char *) * 2); 428d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (!links) 429d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby return NULL; 430d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby memset(links, 0, sizeof(char *) * 2); 431d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 432d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby /* skip "/devices/platform/<driver>" */ 433f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin parent = strchr(uevent->path + pdev->path_len, '/'); 434d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (!*parent) 435d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby goto err; 436d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 437d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (!strncmp(parent, "/usb", 4)) { 438d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby /* skip root hub name and device. use device interface */ 439d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby while (*++parent && *parent != '/'); 440d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (*parent) 441d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby while (*++parent && *parent != '/'); 442d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (!*parent) 443d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby goto err; 444d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby slash = strchr(++parent, '/'); 445d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (!slash) 446d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby goto err; 447d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby width = slash - parent; 448d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (width <= 0) 449d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby goto err; 450d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 451d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0) 452d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby link_num++; 453d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby else 454d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby links[link_num] = NULL; 455d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby mkdir("/dev/usb", 0755); 456d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby } 457d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby else { 458d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby goto err; 459d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby } 460d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 461d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby return links; 462d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Gobyerr: 463d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby free(links); 464d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby return NULL; 465d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby} 466d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 467a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boiestatic char **get_block_device_symlinks(struct uevent *uevent) 468b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross{ 469fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross const char *device; 470f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin struct platform_node *pdev; 471b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross char *slash; 472a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie const char *type; 473b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross int width; 474b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross char buf[256]; 475b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross char link_path[256]; 476b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross int fd; 477b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross int link_num = 0; 478b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross int ret; 479b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross char *p; 480b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross unsigned int size; 481b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross struct stat info; 482b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 483f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin pdev = find_platform_device(uevent->path); 484a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie if (pdev) { 485a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie device = pdev->name; 486a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie type = "platform"; 487a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) { 488a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie device = buf; 489a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie type = "pci"; 490a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie } else { 491f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin return NULL; 492a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie } 493f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin 494b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross char **links = malloc(sizeof(char *) * 4); 495b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (!links) 496b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross return NULL; 497b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross memset(links, 0, sizeof(char *) * 4); 498b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 499a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie INFO("found %s device %s\n", type, device); 500fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 501a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device); 502b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 503b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (uevent->partition_name) { 504b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross p = strdup(uevent->partition_name); 505b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross sanitize(p); 50693ca79b44535ee24ece08001e1948070db6ab6edJohan Redestig if (strcmp(uevent->partition_name, p)) 50793ca79b44535ee24ece08001e1948070db6ab6edJohan Redestig NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p); 508b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0) 509b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross link_num++; 510b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross else 511b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross links[link_num] = NULL; 512b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross free(p); 513b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } 514b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 515b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (uevent->partition_num >= 0) { 516b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0) 517b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross link_num++; 518b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross else 519b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross links[link_num] = NULL; 520b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } 521b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 522f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin slash = strrchr(uevent->path, '/'); 523b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0) 524b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross link_num++; 525b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross else 526b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross links[link_num] = NULL; 527b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 528b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross return links; 529b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross} 530b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 531eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_device(const char *action, const char *devpath, 532eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross const char *path, int block, int major, int minor, char **links) 5334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 534b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross int i; 5354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 536eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross if(!strcmp(action, "add")) { 537b4c5200f51c3568f604a4557119ab545a6ddac94Stephen Smalley make_device(devpath, path, block, major, minor, (const char **)links); 538b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (links) { 539b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross for (i = 0; links[i]; i++) 540b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross make_link(devpath, links[i]); 541b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } 5424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 5434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 544eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross if(!strcmp(action, "remove")) { 545b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (links) { 546b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross for (i = 0; links[i]; i++) 547b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross remove_link(devpath, links[i]); 548b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } 5494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project unlink(devpath); 550b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } 551b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 552b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (links) { 553b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross for (i = 0; links[i]; i++) 554b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross free(links[i]); 555b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross free(links); 5564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 5574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 5584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 559fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic void handle_platform_device_event(struct uevent *uevent) 560fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{ 561f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin const char *path = uevent->path; 562fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 563fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross if (!strcmp(uevent->action, "add")) 564f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin add_platform_device(path); 565fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross else if (!strcmp(uevent->action, "remove")) 566f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin remove_platform_device(path); 567fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross} 568fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross 569eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic const char *parse_device_name(struct uevent *uevent, unsigned int len) 570eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{ 571eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross const char *name; 572eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 573eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross /* if it's not a /dev device, nothing else to do */ 574eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross if((uevent->major < 0) || (uevent->minor < 0)) 575eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross return NULL; 576eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 577eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross /* do we have a name? */ 578eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross name = strrchr(uevent->path, '/'); 579eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross if(!name) 580eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross return NULL; 581eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross name++; 582eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 583eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross /* too-long names would overrun our buffer */ 584f6e009ee2650d8812942aa7f5761e86402346739Greg Hackmann if(strlen(name) > len) { 585f6e009ee2650d8812942aa7f5761e86402346739Greg Hackmann ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n", 586f6e009ee2650d8812942aa7f5761e86402346739Greg Hackmann name, len); 587eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross return NULL; 588f6e009ee2650d8812942aa7f5761e86402346739Greg Hackmann } 589eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 590eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross return name; 591eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross} 592eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 593eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_block_device_event(struct uevent *uevent) 594eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{ 595eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross const char *base = "/dev/block/"; 596eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross const char *name; 597eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross char devpath[96]; 598eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross char **links = NULL; 599eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 600eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross name = parse_device_name(uevent, 64); 601eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross if (!name) 602eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross return; 603eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 604eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross snprintf(devpath, sizeof(devpath), "%s%s", base, name); 605e46f9d510db9351682cf17c49115110870147335Stephen Smalley make_dir(base, 0755); 606eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 607f395c9237da69f1cc15f26a2525a82a371d5816dDima Zavin if (!strncmp(uevent->path, "/devices/", 9)) 608a885d04ec8c9dd013a3dfc4434ec4f2cd23ef1ffAndrew Boie links = get_block_device_symlinks(uevent); 609eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 610eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross handle_device(uevent->action, devpath, uevent->path, 1, 611eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross uevent->major, uevent->minor, links); 612eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross} 613eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 6143312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann#define DEVPATH_LEN 96 6153312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann 6163312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmannstatic bool assemble_devpath(char *devpath, const char *dirname, 6173312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann const char *devname) 6183312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann{ 6193312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname); 6203312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann if (s < 0) { 6213312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann ERROR("failed to assemble device path (%s); ignoring event\n", 6223312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann strerror(errno)); 6233312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann return false; 6243312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann } else if (s >= DEVPATH_LEN) { 6253312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n", 6263312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann dirname, devname, DEVPATH_LEN); 6273312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann return false; 6283312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann } 6293312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann return true; 6303312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann} 6313312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann 6323312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmannstatic void mkdir_recursive_for_devpath(const char *devpath) 6333312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann{ 6343312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann char dir[DEVPATH_LEN]; 6353312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann char *slash; 6363312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann 6373312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann strcpy(dir, devpath); 6383312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann slash = strrchr(dir, '/'); 6393312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann *slash = '\0'; 6403312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann mkdir_recursive(dir, 0755); 6413312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann} 6423312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann 643154f4608aac6218af0e25c98b71d0803278c047eMark Salyzynstatic inline void __attribute__((__deprecated__)) kernel_logger() 644154f4608aac6218af0e25c98b71d0803278c047eMark Salyzyn{ 645154f4608aac6218af0e25c98b71d0803278c047eMark Salyzyn INFO("kernel logger is deprecated\n"); 646154f4608aac6218af0e25c98b71d0803278c047eMark Salyzyn} 647154f4608aac6218af0e25c98b71d0803278c047eMark Salyzyn 648eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_generic_device_event(struct uevent *uevent) 649eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{ 650eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross char *base; 651eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross const char *name; 6523312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann char devpath[DEVPATH_LEN] = {0}; 653eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross char **links = NULL; 654eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 655eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross name = parse_device_name(uevent, 64); 656eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross if (!name) 657eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross return; 658eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 6593312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann struct ueventd_subsystem *subsystem = 6603312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann ueventd_subsystem_find_by_name(uevent->subsystem); 6613312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann 6623312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann if (subsystem) { 6633312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann const char *devname; 6643312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann 6653312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann switch (subsystem->devname_src) { 6663312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann case DEVNAME_UEVENT_DEVNAME: 6673312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann devname = uevent->device_name; 6683312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann break; 6693312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann 6703312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann case DEVNAME_UEVENT_DEVPATH: 6713312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann devname = name; 6723312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann break; 6733312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann 6743312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann default: 6753312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann ERROR("%s subsystem's devpath option is not set; ignoring event\n", 6763312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann uevent->subsystem); 6773312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann return; 6783312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann } 6793312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann 6803312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann if (!assemble_devpath(devpath, subsystem->dirname, devname)) 6813312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann return; 6823312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann mkdir_recursive_for_devpath(devpath); 6833312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann } else if (!strncmp(uevent->subsystem, "usb", 3)) { 684eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross if (!strcmp(uevent->subsystem, "usb")) { 685f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong if (uevent->device_name) { 6863312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann if (!assemble_devpath(devpath, "/dev", uevent->device_name)) 687f6e009ee2650d8812942aa7f5761e86402346739Greg Hackmann return; 6883312aa8379d877044def52f3b3be5c912a5e61a2Greg Hackmann mkdir_recursive_for_devpath(devpath); 689f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong } 690f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong else { 691f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong /* This imitates the file system that would be created 692f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong * if we were using devfs instead. 693f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong * Minors are broken up into groups of 128, starting at "001" 694f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong */ 695f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong int bus_id = uevent->minor / 128 + 1; 696f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong int device_id = uevent->minor % 128 + 1; 697f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong /* build directories */ 698f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong make_dir("/dev/bus", 0755); 699f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong make_dir("/dev/bus/usb", 0755); 700f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id); 701f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong make_dir(devpath, 0755); 702f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id); 703f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong } 704eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else { 705eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross /* ignore other USB events */ 706eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross return; 707eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } 708eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else if (!strncmp(uevent->subsystem, "graphics", 8)) { 709eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross base = "/dev/graphics/"; 710e46f9d510db9351682cf17c49115110870147335Stephen Smalley make_dir(base, 0755); 711e6f8d45da8c61d67dc9fdc1cec53f40594043817Lukasz Anaczkowski } else if (!strncmp(uevent->subsystem, "drm", 3)) { 712e6f8d45da8c61d67dc9fdc1cec53f40594043817Lukasz Anaczkowski base = "/dev/dri/"; 713e6f8d45da8c61d67dc9fdc1cec53f40594043817Lukasz Anaczkowski make_dir(base, 0755); 714eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) { 715eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross base = "/dev/oncrpc/"; 716e46f9d510db9351682cf17c49115110870147335Stephen Smalley make_dir(base, 0755); 717eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else if (!strncmp(uevent->subsystem, "adsp", 4)) { 718eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross base = "/dev/adsp/"; 719e46f9d510db9351682cf17c49115110870147335Stephen Smalley make_dir(base, 0755); 720eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) { 721eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross base = "/dev/msm_camera/"; 722e46f9d510db9351682cf17c49115110870147335Stephen Smalley make_dir(base, 0755); 723eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else if(!strncmp(uevent->subsystem, "input", 5)) { 724eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross base = "/dev/input/"; 725e46f9d510db9351682cf17c49115110870147335Stephen Smalley make_dir(base, 0755); 726eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else if(!strncmp(uevent->subsystem, "mtd", 3)) { 727eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross base = "/dev/mtd/"; 728e46f9d510db9351682cf17c49115110870147335Stephen Smalley make_dir(base, 0755); 729eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else if(!strncmp(uevent->subsystem, "sound", 5)) { 730eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross base = "/dev/snd/"; 731e46f9d510db9351682cf17c49115110870147335Stephen Smalley make_dir(base, 0755); 732eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else if(!strncmp(uevent->subsystem, "misc", 4) && 733eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross !strncmp(name, "log_", 4)) { 734154f4608aac6218af0e25c98b71d0803278c047eMark Salyzyn kernel_logger(); 735eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross base = "/dev/log/"; 736e46f9d510db9351682cf17c49115110870147335Stephen Smalley make_dir(base, 0755); 737eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross name += 4; 738eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else 739eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross base = "/dev/"; 740eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross links = get_character_device_symlinks(uevent); 741eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 742eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross if (!devpath[0]) 743eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross snprintf(devpath, sizeof(devpath), "%s%s", base, name); 744eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 745eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross handle_device(uevent->action, devpath, uevent->path, 0, 746eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross uevent->major, uevent->minor, links); 747eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross} 748eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 749eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_device_event(struct uevent *uevent) 750eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{ 75175b287b771b302c99797d812122b72f83d2f56f9Ruchi Kandoi if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online")) 752eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross fixup_sys_perms(uevent->path); 753eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 754eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross if (!strncmp(uevent->subsystem, "block", 5)) { 755eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross handle_block_device_event(uevent); 756fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross } else if (!strncmp(uevent->subsystem, "platform", 8)) { 757fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross handle_platform_device_event(uevent); 758eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } else { 759eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross handle_generic_device_event(uevent); 760eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross } 761eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross} 762eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross 7634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic int load_firmware(int fw_fd, int loading_fd, int data_fd) 7644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 7654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project struct stat st; 7664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project long len_to_copy; 7674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int ret = 0; 7684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(fstat(fw_fd, &st) < 0) 7704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return -1; 7714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project len_to_copy = st.st_size; 7724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project write(loading_fd, "1", 1); /* start transfer */ 7744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project while (len_to_copy > 0) { 7764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project char buf[PAGE_SIZE]; 7774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project ssize_t nr; 7784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project nr = read(fw_fd, buf, sizeof(buf)); 7804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(!nr) 7814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project break; 7824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(nr < 0) { 7834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project ret = -1; 7844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project break; 7854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 7864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project len_to_copy -= nr; 7884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project while (nr > 0) { 7894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project ssize_t nw = 0; 7904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project nw = write(data_fd, buf + nw, nr); 7924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(nw <= 0) { 7934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project ret = -1; 7944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project goto out; 7954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 7964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project nr -= nw; 7974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 7984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 7994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectout: 8014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(!ret) 8024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project write(loading_fd, "0", 1); /* successful end of transfer */ 8034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project else 8044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project write(loading_fd, "-1", 2); /* abort transfer */ 8054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return ret; 8074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 8084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8098d48c8e45724c7103f0ace7885d339e49399908bBrian Swetlandstatic int is_booting(void) 8108d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland{ 8118d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland return access("/dev/.booting", F_OK) == 0; 8128d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland} 8138d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland 8144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void process_firmware_event(struct uevent *uevent) 8154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 816029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL; 8174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int l, loading_fd, data_fd, fw_fd; 8188d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland int booting = is_booting(); 8194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8208d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland INFO("firmware: loading '%s' for '%s'\n", 8218d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland uevent->firmware, uevent->path); 8224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path); 8244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (l == -1) 8254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return; 8264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project l = asprintf(&loading, "%sloading", root); 8284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (l == -1) 8294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project goto root_free_out; 8304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project l = asprintf(&data, "%sdata", root); 8324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (l == -1) 8334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project goto loading_free_out; 8344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 83502863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware); 83602863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland if (l == -1) 83702863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland goto data_free_out; 83802863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland 83902863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware); 8404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (l == -1) 8414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project goto data_free_out; 8424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 843029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware); 844029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev if (l == -1) 845029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev goto data_free_out; 846029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev 8474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project loading_fd = open(loading, O_WRONLY); 8484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(loading_fd < 0) 8494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project goto file_free_out; 8504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project data_fd = open(data, O_WRONLY); 8524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(data_fd < 0) 8534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project goto loading_close_out; 8544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8558d48c8e45724c7103f0ace7885d339e49399908bBrian Swetlandtry_loading_again: 85602863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland fw_fd = open(file1, O_RDONLY); 85702863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland if(fw_fd < 0) { 85802863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland fw_fd = open(file2, O_RDONLY); 859609d8828d3b8266a80606bf902d1294296962cf3Benoit Goby if (fw_fd < 0) { 860029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev fw_fd = open(file3, O_RDONLY); 861029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev if (fw_fd < 0) { 862029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev if (booting) { 863029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev /* If we're not fully booted, we may be missing 864029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev * filesystems needed for firmware, wait and retry. 865029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev */ 866029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev usleep(100000); 867029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev booting = is_booting(); 868029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev goto try_loading_again; 869029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev } 870029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno); 871029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev write(loading_fd, "-1", 2); 872029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev goto data_close_out; 8738d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland } 874609d8828d3b8266a80606bf902d1294296962cf3Benoit Goby } 87502863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland } 8764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(!load_firmware(fw_fd, loading_fd, data_fd)) 8788d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware); 8794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project else 8808d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware); 8814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project close(fw_fd); 8834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectdata_close_out: 8844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project close(data_fd); 8854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectloading_close_out: 8864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project close(loading_fd); 8874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectfile_free_out: 88802863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland free(file1); 88902863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland free(file2); 89076c5889325c2ada5dda07fd9af20a4a485978538Ajay Dudani free(file3); 8914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectdata_free_out: 8924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project free(data); 8934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectloading_free_out: 8944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project free(loading); 8954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectroot_free_out: 8964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project free(root); 8974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 8984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 8994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void handle_firmware_event(struct uevent *uevent) 9004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 9014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project pid_t pid; 902982a815c10cc28707d81c46112fc45b1c10df3beColin Cross int ret; 9034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(strcmp(uevent->subsystem, "firmware")) 9054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return; 9064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(strcmp(uevent->action, "add")) 9084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return; 9094f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project /* we fork, to avoid making large memory allocations in init proper */ 9114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project pid = fork(); 9124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (!pid) { 9134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project process_firmware_event(uevent); 9144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project exit(EXIT_SUCCESS); 9154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 9164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 9174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 918c603720aed304fdd36a5815f81eb22bd4d9968b7Ruchi Kandoi#define UEVENT_MSG_LEN 2048 9190dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossvoid handle_device_fd() 9204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 9213f582e92f07d358f3d0941b86407b39e2e7e67ccVernon Tang char msg[UEVENT_MSG_LEN+2]; 9223f582e92f07d358f3d0941b86407b39e2e7e67ccVernon Tang int n; 92357de8b8f27f887ca8084671df777a4ac199ba647Nick Kralevich while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) { 9245f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich if(n >= UEVENT_MSG_LEN) /* overflow -- discard */ 9254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project continue; 9264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg[n] = '\0'; 9284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg[n+1] = '\0'; 9294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9305f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich struct uevent uevent; 9314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project parse_event(msg, &uevent); 9324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 933e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley if (sehandle && selinux_status_updated() > 0) { 934e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley struct selabel_handle *sehandle2; 935e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley sehandle2 = selinux_android_file_context_handle(); 936e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley if (sehandle2) { 937e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley selabel_close(sehandle); 938e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley sehandle = sehandle2; 939e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley } 940e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley } 941e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley 9424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project handle_device_event(&uevent); 9434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project handle_firmware_event(&uevent); 9444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 9454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 9464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project/* Coldboot walks parts of the /sys tree and pokes the uevent files 9484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** to cause the kernel to regenerate device add events that happened 9494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** before init's device manager was started 9504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** 9514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** We drain any pending events from the netlink socket every time 9524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** we poke another uevent file to make sure we don't overrun the 9534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** socket's buffer. 9544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project*/ 9554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9560dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossstatic void do_coldboot(DIR *d) 9574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 9584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project struct dirent *de; 9594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int dfd, fd; 9604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project dfd = dirfd(d); 9624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project fd = openat(dfd, "uevent", O_WRONLY); 9644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(fd >= 0) { 9654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project write(fd, "add\n", 4); 9664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project close(fd); 9670dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross handle_device_fd(); 9684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 9694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project while((de = readdir(d))) { 9714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project DIR *d2; 9724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(de->d_type != DT_DIR || de->d_name[0] == '.') 9744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project continue; 9754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); 9774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(fd < 0) 9784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project continue; 9794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project d2 = fdopendir(fd); 9814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(d2 == 0) 9824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project close(fd); 9834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project else { 9840dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross do_coldboot(d2); 9854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project closedir(d2); 9864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 9874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 9884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 9894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9900dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossstatic void coldboot(const char *path) 9914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 9924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project DIR *d = opendir(path); 9934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(d) { 9940dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross do_coldboot(d); 9954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project closedir(d); 9964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 9974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 9984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 9990dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossvoid device_init(void) 10004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 10014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project suseconds_t t0, t1; 1002f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross struct stat info; 1003f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross int fd; 1004b5982bf7c65b30be981ecfe1847437b05e32b2bbKenny Root 1005ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley sehandle = NULL; 1006ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley if (is_selinux_enabled() > 0) { 1007ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley sehandle = selinux_android_file_context_handle(); 1008e2eb69db6d818b41f5c0f2090cc643f7cff31734Stephen Smalley selinux_status_open(true); 1009ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley } 1010b5982bf7c65b30be981ecfe1847437b05e32b2bbKenny Root 1011d562ca78aa713d186e83f05061c0aed88e200bcdAndrew Boie /* is 256K enough? udev uses 16MB! */ 1012d562ca78aa713d186e83f05061c0aed88e200bcdAndrew Boie device_fd = uevent_open_socket(256*1024, true); 10130dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross if(device_fd < 0) 10140dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross return; 10154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 10160dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross fcntl(device_fd, F_SETFD, FD_CLOEXEC); 10170dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross fcntl(device_fd, F_SETFL, O_NONBLOCK); 10184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 1019f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross if (stat(coldboot_done, &info) < 0) { 1020f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross t0 = get_usecs(); 1021f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross coldboot("/sys/class"); 1022f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross coldboot("/sys/block"); 1023f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross coldboot("/sys/devices"); 1024f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross t1 = get_usecs(); 1025f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000); 1026f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross close(fd); 1027f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross log_event_print("coldboot %ld uS\n", ((long) (t1 - t0))); 1028f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross } else { 1029f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross log_event_print("skipping coldboot, already done\n"); 1030f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross } 10310dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross} 10324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 10330dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossint get_device_fd() 10340dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross{ 10350dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross return device_fd; 10364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 1037