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