devices.cpp revision eb5ba8306522acdbf3a63ca847e5e6b990d842b9
14f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project/* 24f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project 34f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * 44f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 54f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * you may not use this file except in compliance with the License. 64f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * You may obtain a copy of the License at 74f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * 84f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 94f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * 104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * See the License for the specific language governing permissions and 144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * limitations under the License. 154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project */ 164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <errno.h> 184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <stddef.h> 194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <stdio.h> 204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <stdlib.h> 214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/stat.h> 224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/types.h> 234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <fcntl.h> 254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <dirent.h> 264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <unistd.h> 274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <string.h> 284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/socket.h> 304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/un.h> 314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <linux/netlink.h> 324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <private/android_filesystem_config.h> 334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/time.h> 34982a815c10cc28707d81c46112fc45b1c10df3beColin Cross#include <asm/page.h> 354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/wait.h> 364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 37b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross#include "devices.h" 38ed8a7d84428ec945c48b6b53dc5a3a18fabaf683Colin Cross#include "util.h" 39ed8a7d84428ec945c48b6b53dc5a3a18fabaf683Colin Cross#include "log.h" 404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include "list.h" 414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define SYSFS_PREFIX "/sys" 434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define FIRMWARE_DIR1 "/etc/firmware" 440dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross#define FIRMWARE_DIR2 "/vendor/firmware" 450dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross 464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic int device_fd = -1; 474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct uevent { 494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project const char *action; 504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project const char *path; 51b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross const char *subsystem; 52b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross const char *firmware; 534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project const char *partition_name; 544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int partition_num; 554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int major; 564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int minor; 574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}; 584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic int open_uevent_socket(void) 604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 615f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich struct sockaddr_nl addr; 624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int sz = 64*1024; // XXX larger? udev uses 16MB! 634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int on = 1; 644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int s; 654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project memset(&addr, 0, sizeof(addr)); 674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project addr.nl_family = AF_NETLINK; 684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project addr.nl_pid = getpid(); 694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project addr.nl_groups = 0xffffffff; 704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); 724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(s < 0) 734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return -1; 745f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich 754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)); 764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project setsockopt(s, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); 774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project close(s); 804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return -1; 814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return s; 844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct perms_ { 874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project char *name; 884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project char *attr; 894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mode_t perm; 904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project unsigned int uid; 914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project unsigned int gid; 924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project unsigned short prefix; 934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}; 944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct perm_node { 9644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross struct perms_ dp; 974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project struct listnode plist; 984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}; 994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 1004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic list_declare(sys_perms); 1014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic list_declare(dev_perms); 10244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 10344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Crossint add_dev_perms(const char *name, const char *attr, 1044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mode_t perm, unsigned int uid, unsigned int gid, 10544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross unsigned short prefix) { 1064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project struct perm_node *node = calloc(1, sizeof(*node)); 1074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (!node) 1084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return -ENOMEM; 1094f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 1104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project node->dp.name = strdup(name); 1114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (!node->dp.name) 1124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return -ENOMEM; 1134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 1144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (attr) { 1154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project node->dp.attr = strdup(attr); 1164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (!node->dp.attr) 1174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return -ENOMEM; 1184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 1194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 12044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross node->dp.perm = perm; 1214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project node->dp.uid = uid; 1224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project node->dp.gid = gid; 1234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project node->dp.prefix = prefix; 1244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 1254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (attr) 1264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project list_add_tail(&sys_perms, &node->plist); 1274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project else 1284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project list_add_tail(&dev_perms, &node->plist); 1294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 1304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return 0; 1314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 1324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 1334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectvoid fixup_sys_perms(const char *upath) 1344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 1354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project char buf[512]; 1364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project struct listnode *node; 1374f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project struct perms_ *dp; 1384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 1394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project /* upaths omit the "/sys" that paths in this list 1404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * contain, so we add 4 when comparing... 1414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project */ 1424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project list_for_each(node, &sys_perms) { 1434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project dp = &(node_to_item(node, struct perm_node, plist))->dp; 1444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (dp->prefix) { 1454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (strncmp(upath, dp->name + 4, strlen(dp->name + 4))) 1464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project continue; 1474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else { 1484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (strcmp(upath, dp->name + 4)) 14944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross continue; 15044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross } 15144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 15244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf)) 15344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross return; 15444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 15544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross sprintf(buf,"/sys%s/%s", upath, dp->attr); 15644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm); 15744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross chown(buf, dp->uid, dp->gid); 15844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross chmod(buf, dp->perm); 15944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross } 16044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross} 16144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 16244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Crossstatic mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid) 16344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross{ 16444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross mode_t perm; 16544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross struct listnode *node; 1664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project struct perm_node *perm_node; 16744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross struct perms_ *dp; 16844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 16944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross /* search the perms list in reverse so that ueventd.$hardware can 1704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * override ueventd.rc 17144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross */ 17244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross list_for_each_reverse(node, &dev_perms) { 17344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross perm_node = node_to_item(node, struct perm_node, plist); 17444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross dp = &perm_node->dp; 1754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 1764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (dp->prefix) { 1774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (strncmp(path, dp->name, strlen(dp->name))) 1784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project continue; 1794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else { 1804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (strcmp(path, dp->name)) 1814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project continue; 1824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 1834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project *uid = dp->uid; 1844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project *gid = dp->gid; 1854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return dp->perm; 1864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 1874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project /* Default if nothing found. */ 1884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project *uid = 0; 1896405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly *gid = 0; 1906405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly return 0600; 1916405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly} 1926405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly 1936405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pellystatic void make_device(const char *path, 1946405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly const char *upath, 1954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int block, int major, int minor) 1966405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly{ 1976405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly unsigned uid; 1984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project unsigned gid; 1994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mode_t mode; 2001e070846f8908dff15086efa12fbba01ab7eae15Chuck Tuffli dev_t dev; 2014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 2024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR); 2034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project dev = makedev(major, minor); 2044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project /* Temporarily change egid to avoid race condition setting the gid of the 2054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * device node. Unforunately changing the euid would prevent creation of 2064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * some device nodes, so the uid has to be set with chown() and is still 2074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * racy. Fixing the gid race at least fixed the issue with system_server 2084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * opening dynamic input devices under the AID_INPUT gid. */ 2094f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project setegid(gid); 2104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mknod(path, mode, dev); 2114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project chown(path, uid, -1); 2124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project setegid(AID_ROOT); 2134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 2144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 2154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#if LOG_UEVENTS 2164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 2174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic inline suseconds_t get_usecs(void) 2184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 2194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project struct timeval tv; 2204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project gettimeofday(&tv, 0); 2214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec; 2224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 2234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 2244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define log_event_print(x...) INFO(x) 2254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 226b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross#else 227b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 2284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define log_event_print(fmt, args...) do { } while (0) 2294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define get_usecs() 0 2304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 2314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#endif 2324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 2334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void parse_event(const char *msg, struct uevent *uevent) 2344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 2354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->action = ""; 2364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->path = ""; 2374f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->subsystem = ""; 2384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->firmware = ""; 2394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->major = -1; 2404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->minor = -1; 2414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->partition_name = NULL; 2424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->partition_num = -1; 2434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 2444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project /* currently ignoring SEQNUM */ 2454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project while(*msg) { 2464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(!strncmp(msg, "ACTION=", 7)) { 2474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg += 7; 2484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->action = msg; 249b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } else if(!strncmp(msg, "DEVPATH=", 8)) { 250b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross msg += 8; 251b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross uevent->path = msg; 252b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } else if(!strncmp(msg, "SUBSYSTEM=", 10)) { 253b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross msg += 10; 254b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross uevent->subsystem = msg; 2554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if(!strncmp(msg, "FIRMWARE=", 9)) { 2564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg += 9; 2574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->firmware = msg; 2584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if(!strncmp(msg, "MAJOR=", 6)) { 2594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg += 6; 2604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->major = atoi(msg); 2614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if(!strncmp(msg, "MINOR=", 6)) { 2624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg += 6; 2634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->minor = atoi(msg); 2644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if(!strncmp(msg, "PARTN=", 6)) { 2654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project msg += 6; 2664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->partition_num = atoi(msg); 267d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby } else if(!strncmp(msg, "PARTNAME=", 9)) { 268d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby msg += 9; 269d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby uevent->partition_name = msg; 270d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby } 271d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 272d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby /* advance to after the next \0 */ 273d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby while(*msg++) 274d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby ; 275d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby } 276d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 277d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n", 278d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby uevent->action, uevent->path, uevent->subsystem, 279d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby uevent->firmware, uevent->major, uevent->minor); 280d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby} 281d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 282d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Gobystatic char **get_character_device_symlinks(struct uevent *uevent) 283d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby{ 284d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby const char *parent; 285d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby char *slash; 286d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby char **links; 287d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby int link_num = 0; 288d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby int width; 289d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 290d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (strncmp(uevent->path, "/devices/platform/", 18)) 291d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby return NULL; 292d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 293d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby links = malloc(sizeof(char *) * 2); 294d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (!links) 295d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby return NULL; 296d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby memset(links, 0, sizeof(char *) * 2); 297d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 298d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby /* skip "/devices/platform/<driver>" */ 299d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby parent = strchr(uevent->path + 18, '/'); 300d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (!*parent) 301d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby goto err; 302d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 303d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (!strncmp(parent, "/usb", 4)) { 304d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby /* skip root hub name and device. use device interface */ 305d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby while (*++parent && *parent != '/'); 306d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (*parent) 307d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby while (*++parent && *parent != '/'); 308d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (!*parent) 309d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby goto err; 310d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby slash = strchr(++parent, '/'); 311d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (!slash) 312d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby goto err; 313d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby width = slash - parent; 314d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (width <= 0) 315d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby goto err; 316d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 317d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0) 318b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross link_num++; 319b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross else 320b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross links[link_num] = NULL; 321b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross mkdir("/dev/usb", 0755); 322b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } 323b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross else { 324b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross goto err; 325b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } 326b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 327b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross return links; 328b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Crosserr: 329b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross free(links); 330b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross return NULL; 331b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross} 332b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 333b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Crossstatic char **parse_platform_block_device(struct uevent *uevent) 334b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross{ 335b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross const char *driver; 336b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross const char *path; 337b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross char *slash; 338b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross int width; 339b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross char buf[256]; 340b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross char link_path[256]; 341b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross int fd; 342b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross int link_num = 0; 343b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross int ret; 344b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross char *p; 345b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross unsigned int size; 346b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross struct stat info; 347b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 348b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross char **links = malloc(sizeof(char *) * 4); 349b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (!links) 350b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross return NULL; 351b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross memset(links, 0, sizeof(char *) * 4); 352b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 353b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross /* Drop "/devices/platform/" */ 354b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross path = uevent->path; 355b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross driver = path + 18; 356b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross slash = strchr(driver, '/'); 357b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (!slash) 358b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross goto err; 359b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross width = slash - driver; 360b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (width <= 0) 361b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross goto err; 362b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 363b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross snprintf(link_path, sizeof(link_path), "/dev/block/platform/%.*s", 364b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross width, driver); 365b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 366b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (uevent->partition_name) { 367b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross p = strdup(uevent->partition_name); 368b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross sanitize(p); 369b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0) 370b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross link_num++; 371b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross else 372b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross links[link_num] = NULL; 373b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross free(p); 374b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross } 375b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 376b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (uevent->partition_num >= 0) { 377b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0) 378b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross link_num++; 379b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross else 380b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross links[link_num] = NULL; 3814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 3824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 3834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project slash = strrchr(path, '/'); 38493ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0) 3854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project link_num++; 386b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross else 3874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project links[link_num] = NULL; 388b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 3894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return links; 3904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 3914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projecterr: 3924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project free(links); 3934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return NULL; 3944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 3954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 3964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void handle_device(const char *action, const char *devpath, 3974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project const char *path, int block, int major, int minor, char **links) 3984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 3994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int i; 4004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 4014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(!strcmp(action, "add")) { 4024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project make_device(devpath, path, block, major, minor); 4034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (links) { 4044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project for (i = 0; links[i]; i++) 40535237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project make_link(devpath, links[i]); 4064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 4074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 4084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 409b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if(!strcmp(action, "remove")) { 410b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (links) { 4114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project for (i = 0; links[i]; i++) 4124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project remove_link(devpath, links[i]); 4134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 41493ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood unlink(devpath); 41593ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood } 41693ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood 41793ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood if (links) { 41893ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood for (i = 0; links[i]; i++) 41993ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood free(links[i]); 42093ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood free(links); 42193ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood } 42293ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood} 42393ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood 42493ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwoodstatic const char *parse_device_name(struct uevent *uevent, unsigned int len) 42593ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood{ 42693ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood const char *name; 42793ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood 42893ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood /* if it's not a /dev device, nothing else to do */ 42993ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood if((uevent->major < 0) || (uevent->minor < 0)) 43093ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood return NULL; 43193ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood 43293ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood /* do we have a name? */ 43393ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood name = strrchr(uevent->path, '/'); 4344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(!name) 4354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return NULL; 43635237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project name++; 4374f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 4384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project /* too-long names would overrun our buffer */ 43935237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project if(strlen(name) > len) 4404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return NULL; 4414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 442fc0182eb1db0620eb71fb6ca219b15a17dcd912fIliyan Malchev return name; 443fc0182eb1db0620eb71fb6ca219b15a17dcd912fIliyan Malchev} 444fc0182eb1db0620eb71fb6ca219b15a17dcd912fIliyan Malchev 445fc0182eb1db0620eb71fb6ca219b15a17dcd912fIliyan Malchevstatic void handle_block_device_event(struct uevent *uevent) 4464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 4474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project const char *base = "/dev/block/"; 44835237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project const char *name; 4494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project char devpath[96]; 4504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project char **links = NULL; 4515bb44c8ea2bc513fefc526918980ab3d17494eccXiaopeng Yang 4525bb44c8ea2bc513fefc526918980ab3d17494eccXiaopeng Yang name = parse_device_name(uevent, 64); 4535bb44c8ea2bc513fefc526918980ab3d17494eccXiaopeng Yang if (!name) 45435237d135807af84bf9b0e5b8d7f8633e58db6f5The Android Open Source Project return; 4554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 4564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project snprintf(devpath, sizeof(devpath), "%s%s", base, name); 4574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mkdir(base, 0755); 4584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 4594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (!strncmp(uevent->path, "/devices/platform/", 18)) 4604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project links = parse_platform_block_device(uevent); 461d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby 4624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project handle_device(uevent->action, devpath, uevent->path, 1, 4634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->major, uevent->minor, links); 46493ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood 46593ac1559b8c7ad3125ddcd896082b030faadbbd4Mike Lockwood} 4664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 4674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void handle_generic_device_event(struct uevent *uevent) 4684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 469b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross char *base; 470b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross const char *name; 471b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross char devpath[96] = {0}; 472b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross char **links = NULL; 4734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 4744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project name = parse_device_name(uevent, 64); 4754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (!name) 476b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross return; 477b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross 478b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (!strncmp(uevent->subsystem, "usb", 3)) { 479b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross if (!strcmp(uevent->subsystem, "usb")) { 4804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project /* This imitates the file system that would be created 481b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross * if we were using devfs instead. 482b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross * Minors are broken up into groups of 128, starting at "001" 483b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross */ 484b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross int bus_id = uevent->minor / 128 + 1; 485b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross int device_id = uevent->minor % 128 + 1; 486b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross /* build directories */ 4874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mkdir("/dev/bus", 0755); 4884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mkdir("/dev/bus/usb", 0755); 4894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id); 4904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mkdir(devpath, 0755); 4914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id); 4924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else { 4934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project /* ignore other USB events */ 4944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return; 4954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 4964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if (!strncmp(uevent->subsystem, "graphics", 8)) { 4974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project base = "/dev/graphics/"; 4984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mkdir(base, 0755); 4994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) { 5004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project base = "/dev/oncrpc/"; 5014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mkdir(base, 0755); 5024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if (!strncmp(uevent->subsystem, "adsp", 4)) { 5034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project base = "/dev/adsp/"; 5044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mkdir(base, 0755); 5054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) { 5064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project base = "/dev/msm_camera/"; 5074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mkdir(base, 0755); 5084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if(!strncmp(uevent->subsystem, "input", 5)) { 5094f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project base = "/dev/input/"; 5104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mkdir(base, 0755); 5114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if(!strncmp(uevent->subsystem, "mtd", 3)) { 5124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project base = "/dev/mtd/"; 5134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mkdir(base, 0755); 5144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if(!strncmp(uevent->subsystem, "sound", 5)) { 5154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project base = "/dev/snd/"; 5164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mkdir(base, 0755); 5174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else if(!strncmp(uevent->subsystem, "misc", 4) && 5184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project !strncmp(name, "log_", 4)) { 5194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project base = "/dev/log/"; 5204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project mkdir(base, 0755); 5214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project name += 4; 5224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else 5234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project base = "/dev/"; 5244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project links = get_character_device_symlinks(uevent); 5254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 5264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (!devpath[0]) 5274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project snprintf(devpath, sizeof(devpath), "%s%s", base, name); 5284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 5294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project handle_device(uevent->action, devpath, uevent->path, 0, 5304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->major, uevent->minor, links); 5314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 5324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 5334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void handle_device_event(struct uevent *uevent) 5344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 5354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (!strcmp(uevent->action,"add")) 5364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project fixup_sys_perms(uevent->path); 5374f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 5384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (!strncmp(uevent->subsystem, "block", 5)) { 5394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project handle_block_device_event(uevent); 5404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } else { 5414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project handle_generic_device_event(uevent); 5424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 5434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 5444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 5454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic int load_firmware(int fw_fd, int loading_fd, int data_fd) 5464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 5474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project struct stat st; 5484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project long len_to_copy; 5494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int ret = 0; 5504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 5514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(fstat(fw_fd, &st) < 0) 5524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return -1; 5534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project len_to_copy = st.st_size; 5544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 5554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project write(loading_fd, "1", 1); /* start transfer */ 5564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 5574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project while (len_to_copy > 0) { 5584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project char buf[PAGE_SIZE]; 5594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project ssize_t nr; 5604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 5614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project nr = read(fw_fd, buf, sizeof(buf)); 5624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(!nr) 5634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project break; 5644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(nr < 0) { 5654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project ret = -1; 5664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project break; 5674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 5684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 5694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project len_to_copy -= nr; 5704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project while (nr > 0) { 5714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project ssize_t nw = 0; 5724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 5734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project nw = write(data_fd, buf + nw, nr); 5744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(nw <= 0) { 5754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project ret = -1; 5764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project goto out; 5774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 5784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project nr -= nw; 5794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 5804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 5814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 5824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectout: 5834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(!ret) 5844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project write(loading_fd, "0", 1); /* successful end of transfer */ 5854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project else 5864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project write(loading_fd, "-1", 2); /* abort transfer */ 5874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 5884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return ret; 5894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 5904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 5914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic int is_booting(void) 5924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 5934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return access("/dev/.booting", F_OK) == 0; 5944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 595982a815c10cc28707d81c46112fc45b1c10df3beColin Cross 596982a815c10cc28707d81c46112fc45b1c10df3beColin Crossstatic void process_firmware_event(struct uevent *uevent) 5974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 5984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project char *root, *loading, *data, *file1 = NULL, *file2 = NULL; 5994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int l, loading_fd, data_fd, fw_fd; 6004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int booting = is_booting(); 6014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 6024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project INFO("firmware: loading '%s' for '%s'\n", 6034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project uevent->firmware, uevent->path); 6044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 6054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path); 6064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (l == -1) 6074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return; 6084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 609982a815c10cc28707d81c46112fc45b1c10df3beColin Cross l = asprintf(&loading, "%sloading", root); 610982a815c10cc28707d81c46112fc45b1c10df3beColin Cross if (l == -1) 611982a815c10cc28707d81c46112fc45b1c10df3beColin Cross goto root_free_out; 612982a815c10cc28707d81c46112fc45b1c10df3beColin Cross 6134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project l = asprintf(&data, "%sdata", root); 6144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (l == -1) 6154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project goto loading_free_out; 6164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 6170dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware); 6184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (l == -1) 6195f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich goto data_free_out; 6205f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich 6215f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware); 6225f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich if (l == -1) 6235f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich goto data_free_out; 6245f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich 6255f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich loading_fd = open(loading, O_WRONLY); 626fad7204e9eac20a87afacd7547ed8202a39319f8Nick Kralevich if(loading_fd < 0) 6275f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich goto file_free_out; 6285f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich 6295f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich data_fd = open(data, O_WRONLY); 6304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(data_fd < 0) 6315f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich goto loading_close_out; 6325f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich 6335f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevichtry_loading_again: 6345f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich fw_fd = open(file1, O_RDONLY); 6355f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich if(fw_fd < 0) { 6365f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich fw_fd = open(file2, O_RDONLY); 6375f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich if (fw_fd < 0) { 6385f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich if (booting) { 6395f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich /* If we're not fully booted, we may be missing 6405f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich * filesystems needed for firmware, wait and retry. 6415f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich */ 6425f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich usleep(100000); 6435f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich booting = is_booting(); 6445f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich goto try_loading_again; 6455f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich } 6465f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno); 6474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project write(loading_fd, "-1", 2); 6485f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich goto data_close_out; 6494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 6504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 6514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 6524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(!load_firmware(fw_fd, loading_fd, data_fd)) 6534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware); 6545f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich else 6554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware); 6564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 6574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project close(fw_fd); 6584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectdata_close_out: 6594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project close(data_fd); 6604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectloading_close_out: 6614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project close(loading_fd); 6624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectfile_free_out: 6634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project free(file1); 6644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project free(file2); 6654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectdata_free_out: 6664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project free(data); 6674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectloading_free_out: 6684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project free(loading); 6694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectroot_free_out: 6704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project free(root); 6710dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross} 6724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 6734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void handle_firmware_event(struct uevent *uevent) 6744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 6754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project pid_t pid; 6764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project int ret; 6774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 6784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(strcmp(uevent->subsystem, "firmware")) 6794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project return; 6804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 6814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if(strcmp(uevent->action, "add")) 6820dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross return; 6834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 6844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project /* we fork, to avoid making large memory allocations in init proper */ 6854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project pid = fork(); 6864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (!pid) { 6874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project process_firmware_event(uevent); 6884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project exit(EXIT_SUCCESS); 6894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 6904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project} 6914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 6924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define UEVENT_MSG_LEN 1024 6934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectvoid handle_device_fd() 6944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{ 6954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project for(;;) { 6964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project char msg[UEVENT_MSG_LEN+2]; 6974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project char cred_msg[CMSG_SPACE(sizeof(struct ucred))]; 6984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project struct iovec iov = {msg, sizeof(msg)}; 6990dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross struct sockaddr_nl snl; 7004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project struct msghdr hdr = {&snl, sizeof(snl), &iov, 1, cred_msg, sizeof(cred_msg), 0}; 7014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project ssize_t n = recvmsg(device_fd, &hdr, 0); 7034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (n <= 0) { 7044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project break; 7050dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross } 7064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if ((snl.nl_groups != 1) || (snl.nl_pid != 0)) { 7084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project /* ignoring non-kernel netlink multicast message */ 7090dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross continue; 7104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 7114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project struct cmsghdr * cmsg = CMSG_FIRSTHDR(&hdr); 7134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) { 7140dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross /* no sender credentials received, ignore message */ 7154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project continue; 7164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project } 717f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross 718f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross struct ucred * cred = (struct ucred *)CMSG_DATA(cmsg); 7194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project if (cred->uid != 0) { 7200dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross /* message from non-root user, ignore */ 7210dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross continue; 7220dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross } 7234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 7240dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross if(n >= UEVENT_MSG_LEN) /* overflow -- discard */ 7250dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross continue; 7264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project 727f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross msg[n] = '\0'; 728f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross msg[n+1] = '\0'; 729f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross 730f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross struct uevent uevent; 731f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross parse_event(msg, &uevent); 732f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross 733f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross handle_device_event(&uevent); 734f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross handle_firmware_event(&uevent); 735f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross } 736f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross} 737f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross 738f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross/* Coldboot walks parts of the /sys tree and pokes the uevent files 7390dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross** to cause the kernel to regenerate device add events that happened 7404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** before init's device manager was started 7410dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross** 7420dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross** We drain any pending events from the netlink socket every time 7430dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross** we poke another uevent file to make sure we don't overrun the 7444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** socket's buffer. 745*/ 746 747static void do_coldboot(DIR *d) 748{ 749 struct dirent *de; 750 int dfd, fd; 751 752 dfd = dirfd(d); 753 754 fd = openat(dfd, "uevent", O_WRONLY); 755 if(fd >= 0) { 756 write(fd, "add\n", 4); 757 close(fd); 758 handle_device_fd(); 759 } 760 761 while((de = readdir(d))) { 762 DIR *d2; 763 764 if(de->d_type != DT_DIR || de->d_name[0] == '.') 765 continue; 766 767 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); 768 if(fd < 0) 769 continue; 770 771 d2 = fdopendir(fd); 772 if(d2 == 0) 773 close(fd); 774 else { 775 do_coldboot(d2); 776 closedir(d2); 777 } 778 } 779} 780 781static void coldboot(const char *path) 782{ 783 DIR *d = opendir(path); 784 if(d) { 785 do_coldboot(d); 786 closedir(d); 787 } 788} 789 790void device_init(void) 791{ 792 suseconds_t t0, t1; 793 struct stat info; 794 int fd; 795 796 device_fd = open_uevent_socket(); 797 if(device_fd < 0) 798 return; 799 800 fcntl(device_fd, F_SETFD, FD_CLOEXEC); 801 fcntl(device_fd, F_SETFL, O_NONBLOCK); 802 803 if (stat(coldboot_done, &info) < 0) { 804 t0 = get_usecs(); 805 coldboot("/sys/class"); 806 coldboot("/sys/block"); 807 coldboot("/sys/devices"); 808 t1 = get_usecs(); 809 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000); 810 close(fd); 811 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0))); 812 } else { 813 log_event_print("skipping coldboot, already done\n"); 814 } 815} 816 817int get_device_fd() 818{ 819 return device_fd; 820} 821