platform_system_core/init/devices.cpp

1015 lines
30 KiB
C++
Raw Normal View History

2008-10-21 16:00:00 +02:00
/*
* Copyright (C) 2007-2014 The Android Open Source Project
2008-10-21 16:00:00 +02:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <dirent.h>
2008-10-21 16:00:00 +02:00
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <libgen.h>
#include <linux/netlink.h>
#include <stddef.h>
2008-10-21 16:00:00 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/sendfile.h>
#include <sys/socket.h>
#include <sys/time.h>
2008-10-21 16:00:00 +02:00
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
2008-10-21 16:00:00 +02:00
#include <unistd.h>
#include <algorithm>
#include <memory>
#include <string>
#include <thread>
#include <vector>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
#include <cutils/list.h>
#include <cutils/uevent.h>
#include <private/android_filesystem_config.h>
#include <selinux/android.h>
#include <selinux/avc.h>
#include <selinux/label.h>
#include <selinux/selinux.h>
2008-10-21 16:00:00 +02:00
#include "devices.h"
#include "ueventd_parser.h"
#include "util.h"
2008-10-21 16:00:00 +02:00
#define SYSFS_PREFIX "/sys"
static const char *firmware_dirs[] = { "/etc/firmware",
"/vendor/firmware",
"/firmware/image" };
2008-10-21 16:00:00 +02:00
extern struct selabel_handle *sehandle;
static android::base::unique_fd device_fd;
2008-10-21 16:00:00 +02:00
struct perms_ {
char *name;
char *attr;
2008-10-21 16:00:00 +02:00
mode_t perm;
unsigned int uid;
unsigned int gid;
unsigned short prefix;
unsigned short wildcard;
2008-10-21 16:00:00 +02:00
};
struct perm_node {
struct perms_ dp;
struct listnode plist;
};
struct platform_node {
char *name;
char *path;
int path_len;
struct listnode list;
};
static list_declare(sys_perms);
static list_declare(dev_perms);
static list_declare(platform_names);
2008-10-21 16:00:00 +02:00
int add_dev_perms(const char *name, const char *attr,
mode_t perm, unsigned int uid, unsigned int gid,
unsigned short prefix,
unsigned short wildcard) {
struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
2008-10-21 16:00:00 +02:00
if (!node)
return -ENOMEM;
node->dp.name = strdup(name);
if (!node->dp.name) {
free(node);
2008-10-21 16:00:00 +02:00
return -ENOMEM;
}
2008-10-21 16:00:00 +02:00
if (attr) {
node->dp.attr = strdup(attr);
if (!node->dp.attr) {
free(node->dp.name);
free(node);
return -ENOMEM;
}
}
2008-10-21 16:00:00 +02:00
node->dp.perm = perm;
node->dp.uid = uid;
node->dp.gid = gid;
node->dp.prefix = prefix;
node->dp.wildcard = wildcard;
2008-10-21 16:00:00 +02:00
if (attr)
list_add_tail(&sys_perms, &node->plist);
else
list_add_tail(&dev_perms, &node->plist);
2008-10-21 16:00:00 +02:00
return 0;
}
static bool perm_path_matches(const char *path, struct perms_ *dp)
{
if (dp->prefix) {
if (strncmp(path, dp->name, strlen(dp->name)) == 0)
return true;
} else if (dp->wildcard) {
if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
return true;
} else {
if (strcmp(path, dp->name) == 0)
return true;
}
return false;
}
static bool match_subsystem(perms_* dp, const char* pattern,
const char* path, const char* subsystem) {
if (!pattern || !subsystem || strstr(dp->name, subsystem) == NULL) {
return false;
}
std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path));
return perm_path_matches(subsys_path.c_str(), dp);
}
static void fixup_sys_perms(const char* upath, const char* subsystem) {
// upaths omit the "/sys" that paths in this list
// contain, so we prepend it...
std::string path = std::string(SYSFS_PREFIX) + upath;
listnode* node;
list_for_each(node, &sys_perms) {
perms_* dp = &(node_to_item(node, perm_node, plist))->dp;
if (match_subsystem(dp, SYSFS_PREFIX "/class/%s/%s", path.c_str(), subsystem)) {
; // matched
} else if (match_subsystem(dp, SYSFS_PREFIX "/bus/%s/devices/%s", path.c_str(), subsystem)) {
; // matched
} else if (!perm_path_matches(path.c_str(), dp)) {
continue;
}
std::string attr_file = path + "/" + dp->attr;
LOG(INFO) << "fixup " << attr_file
<< " " << dp->uid << " " << dp->gid << " " << std::oct << dp->perm;
chown(attr_file.c_str(), dp->uid, dp->gid);
chmod(attr_file.c_str(), dp->perm);
}
if (access(path.c_str(), F_OK) == 0) {
LOG(VERBOSE) << "restorecon_recursive: " << path;
restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE);
}
}
static mode_t get_device_perm(const char* path, const std::vector<std::string>& links,
unsigned* uid, unsigned* gid) {
struct listnode *node;
struct perm_node *perm_node;
struct perms_ *dp;
/* search the perms list in reverse so that ueventd.$hardware can
* override ueventd.rc
*/
list_for_each_reverse(node, &dev_perms) {
perm_node = node_to_item(node, struct perm_node, plist);
dp = &perm_node->dp;
if (perm_path_matches(path, dp) ||
std::any_of(links.begin(), links.end(),
[dp](const auto& link) { return perm_path_matches(link.c_str(), dp); })) {
*uid = dp->uid;
*gid = dp->gid;
return dp->perm;
2008-10-21 16:00:00 +02:00
}
}
/* Default if nothing found. */
*uid = 0;
*gid = 0;
return 0600;
2008-10-21 16:00:00 +02:00
}
static void make_device(const char* path, const char* /*upath*/, int block, int major, int minor,
const std::vector<std::string>& links) {
2008-10-21 16:00:00 +02:00
unsigned uid;
unsigned gid;
mode_t mode;
dev_t dev;
char *secontext = NULL;
2008-10-21 16:00:00 +02:00
mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
if (sehandle) {
std::vector<const char*> c_links;
for (const auto& link : links) {
c_links.emplace_back(link.c_str());
}
c_links.emplace_back(nullptr);
if (selabel_lookup_best_match(sehandle, &secontext, path, &c_links[0], mode)) {
PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
return;
}
setfscreatecon(secontext);
ueventd: relabel block devices nodes when processing subsequent add events There is a race in ueventd's coldboot procedure that permits creation of device block nodes before platform devices are registered. This happens when the kernel sends events for adding block devices during ueventd's coldboot /sys walk. In this case the device node links used to compute the SELinux context are not known and the node is created under the generic context: u:object_r:block_device:s0. A second add event for block device nodes is triggered after the platform devices are handled by ueventd and the SELinux context is correctly computed but the mknod call fails because the node already exists. This patch handles this error case and updates the node's security context. The race is introduced by the uevent sent from the sdcard device probe function. The issue appears when this uevent is triggered during ueventd's coldboot procedure but before the /sys/devices recursive walk reached the corresponding sdcard platform device path. The backtrace looks something like: 1. ueventd_main() 2. device_init() 3. coldboot("/sys/devices"); 4. do_coldboot() 5. handle_device_fd() 6. handle_device_event() 6.1 handle_block_device_event() 6.2 handle_platform_device_event() Because handle_device_fd() reads all events from the netlink socket it may handle the add events for the sdcard partition nodes send occasionally by the kernel during coldboot /sys walk procedure. If handle_device_event() continues with handle_block_device_event() before handle_platform_device_event() registers the sdcard platform device then handle_block_device_event() will create device nodes without knowing all block device symlinks (get_block_device_symlinks()): 1. handle_device(path=/dev/block/mmcblk0p3, links = NULL) 2. make_device(path=/dev/block/mmcblk0p3, links = NULL) 3. selabel_lookup_best_match(path=/dev/block/mmcblk0p3, links = NULL) returns the default context (u:object_r:block_device:s0) for /dev/block/mmcblk0p3 instead of more specific context like: u:object_r:boot_block_device:s0 4. setfscreatecon(u:object_r:block_device:s0) 5. mknod(/dev/block/mmcblk0p3) So the node is create with the wrong context. Afterwards the coldboot /sys walk continues and make_device() will be called with correct path and links. But even if the secontext is computed correctly this time it will not be applied to the device node because mknod() fails. I see this issue randomly appearing (one time in 10 reboots) on a Minnoboard Turbot with external sdcard as the boot device. BUG=28388946 Change-Id: I96e239af29d82b753e5d349b3ecefaad09edee87 Signed-off-by: Mihai Serban <mihai.serban@intel.com>
2016-04-25 17:22:27 +02:00
}
dev = makedev(major, minor);
/* Temporarily change egid to avoid race condition setting the gid of the
* device node. Unforunately changing the euid would prevent creation of
* some device nodes, so the uid has to be set with chown() and is still
* racy. Fixing the gid race at least fixed the issue with system_server
* opening dynamic input devices under the AID_INPUT gid. */
if (setegid(gid)) {
PLOG(ERROR) << "setegid(" << gid << ") for " << path << " device failed";
goto out;
}
ueventd: relabel block devices nodes when processing subsequent add events There is a race in ueventd's coldboot procedure that permits creation of device block nodes before platform devices are registered. This happens when the kernel sends events for adding block devices during ueventd's coldboot /sys walk. In this case the device node links used to compute the SELinux context are not known and the node is created under the generic context: u:object_r:block_device:s0. A second add event for block device nodes is triggered after the platform devices are handled by ueventd and the SELinux context is correctly computed but the mknod call fails because the node already exists. This patch handles this error case and updates the node's security context. The race is introduced by the uevent sent from the sdcard device probe function. The issue appears when this uevent is triggered during ueventd's coldboot procedure but before the /sys/devices recursive walk reached the corresponding sdcard platform device path. The backtrace looks something like: 1. ueventd_main() 2. device_init() 3. coldboot("/sys/devices"); 4. do_coldboot() 5. handle_device_fd() 6. handle_device_event() 6.1 handle_block_device_event() 6.2 handle_platform_device_event() Because handle_device_fd() reads all events from the netlink socket it may handle the add events for the sdcard partition nodes send occasionally by the kernel during coldboot /sys walk procedure. If handle_device_event() continues with handle_block_device_event() before handle_platform_device_event() registers the sdcard platform device then handle_block_device_event() will create device nodes without knowing all block device symlinks (get_block_device_symlinks()): 1. handle_device(path=/dev/block/mmcblk0p3, links = NULL) 2. make_device(path=/dev/block/mmcblk0p3, links = NULL) 3. selabel_lookup_best_match(path=/dev/block/mmcblk0p3, links = NULL) returns the default context (u:object_r:block_device:s0) for /dev/block/mmcblk0p3 instead of more specific context like: u:object_r:boot_block_device:s0 4. setfscreatecon(u:object_r:block_device:s0) 5. mknod(/dev/block/mmcblk0p3) So the node is create with the wrong context. Afterwards the coldboot /sys walk continues and make_device() will be called with correct path and links. But even if the secontext is computed correctly this time it will not be applied to the device node because mknod() fails. I see this issue randomly appearing (one time in 10 reboots) on a Minnoboard Turbot with external sdcard as the boot device. BUG=28388946 Change-Id: I96e239af29d82b753e5d349b3ecefaad09edee87 Signed-off-by: Mihai Serban <mihai.serban@intel.com>
2016-04-25 17:22:27 +02:00
/* If the node already exists update its SELinux label to handle cases when
* it was created with the wrong context during coldboot procedure. */
if (mknod(path, mode, dev) && (errno == EEXIST) && secontext) {
char* fcon = nullptr;
int rc = lgetfilecon(path, &fcon);
if (rc < 0) {
PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
goto out;
}
bool different = strcmp(fcon, secontext) != 0;
freecon(fcon);
if (different && lsetfilecon(path, secontext)) {
PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path << "' device";
ueventd: relabel block devices nodes when processing subsequent add events There is a race in ueventd's coldboot procedure that permits creation of device block nodes before platform devices are registered. This happens when the kernel sends events for adding block devices during ueventd's coldboot /sys walk. In this case the device node links used to compute the SELinux context are not known and the node is created under the generic context: u:object_r:block_device:s0. A second add event for block device nodes is triggered after the platform devices are handled by ueventd and the SELinux context is correctly computed but the mknod call fails because the node already exists. This patch handles this error case and updates the node's security context. The race is introduced by the uevent sent from the sdcard device probe function. The issue appears when this uevent is triggered during ueventd's coldboot procedure but before the /sys/devices recursive walk reached the corresponding sdcard platform device path. The backtrace looks something like: 1. ueventd_main() 2. device_init() 3. coldboot("/sys/devices"); 4. do_coldboot() 5. handle_device_fd() 6. handle_device_event() 6.1 handle_block_device_event() 6.2 handle_platform_device_event() Because handle_device_fd() reads all events from the netlink socket it may handle the add events for the sdcard partition nodes send occasionally by the kernel during coldboot /sys walk procedure. If handle_device_event() continues with handle_block_device_event() before handle_platform_device_event() registers the sdcard platform device then handle_block_device_event() will create device nodes without knowing all block device symlinks (get_block_device_symlinks()): 1. handle_device(path=/dev/block/mmcblk0p3, links = NULL) 2. make_device(path=/dev/block/mmcblk0p3, links = NULL) 3. selabel_lookup_best_match(path=/dev/block/mmcblk0p3, links = NULL) returns the default context (u:object_r:block_device:s0) for /dev/block/mmcblk0p3 instead of more specific context like: u:object_r:boot_block_device:s0 4. setfscreatecon(u:object_r:block_device:s0) 5. mknod(/dev/block/mmcblk0p3) So the node is create with the wrong context. Afterwards the coldboot /sys walk continues and make_device() will be called with correct path and links. But even if the secontext is computed correctly this time it will not be applied to the device node because mknod() fails. I see this issue randomly appearing (one time in 10 reboots) on a Minnoboard Turbot with external sdcard as the boot device. BUG=28388946 Change-Id: I96e239af29d82b753e5d349b3ecefaad09edee87 Signed-off-by: Mihai Serban <mihai.serban@intel.com>
2016-04-25 17:22:27 +02:00
}
}
out:
chown(path, uid, -1);
if (setegid(AID_ROOT)) {
PLOG(FATAL) << "setegid(AID_ROOT) failed";
}
if (secontext) {
freecon(secontext);
setfscreatecon(NULL);
}
}
void add_platform_device(const char* path) {
int path_len = strlen(path);
struct platform_node *bus;
const char *name = path;
if (!strncmp(path, "/devices/", 9)) {
name += 9;
if (!strncmp(name, "platform/", 9))
name += 9;
}
LOG(VERBOSE) << "adding platform device " << name << " (" << path << ")";
bus = (platform_node*) calloc(1, sizeof(struct platform_node));
bus->path = strdup(path);
bus->path_len = path_len;
bus->name = bus->path + (name - path);
list_add_tail(&platform_names, &bus->list);
}
/*
* given a path that may start with a platform device, find the length of the
* platform device prefix. If it doesn't start with a platform device, return
* 0.
*/
static struct platform_node *find_platform_device(const char *path)
{
int path_len = strlen(path);
struct listnode *node;
struct platform_node *bus;
list_for_each_reverse(node, &platform_names) {
bus = node_to_item(node, struct platform_node, list);
if ((bus->path_len < path_len) &&
(path[bus->path_len] == '/') &&
!strncmp(path, bus->path, bus->path_len))
return bus;
}
return NULL;
}
void remove_platform_device(const char* path) {
struct listnode *node;
struct platform_node *bus;
list_for_each_reverse(node, &platform_names) {
bus = node_to_item(node, struct platform_node, list);
if (!strcmp(path, bus->path)) {
LOG(INFO) << "removing platform device " << bus->name;
free(bus->path);
list_remove(node);
free(bus);
return;
}
}
}
static void destroy_platform_devices() {
struct listnode* node;
struct listnode* n;
struct platform_node* bus;
list_for_each_safe(node, n, &platform_names) {
list_remove(node);
bus = node_to_item(node, struct platform_node, list);
free(bus->path);
free(bus);
}
}
/* Given a path that may start with a PCI device, populate the supplied buffer
* with the PCI domain/bus number and the peripheral ID and return 0.
* If it doesn't start with a PCI device, or there is some error, return -1 */
static bool find_pci_device_prefix(const std::string& path, std::string* result) {
result->clear();
if (!android::base::StartsWith(path, "/devices/pci")) return false;
/* Beginning of the prefix is the initial "pci" after "/devices/" */
std::string::size_type start = 9;
/* End of the prefix is two path '/' later, capturing the domain/bus number
* and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
auto end = path.find('/', start);
if (end == std::string::npos) return false;
end = path.find('/', end + 1);
if (end == std::string::npos) return false;
auto length = end - start;
if (length <= 4) {
// The minimum string that will get to this check is 'pci/', which is malformed,
// so return false
return false;
}
*result = path.substr(start, length);
return true;
}
/* Given a path that may start with a virtual block device, populate
* the supplied buffer with the virtual block device ID and return 0.
* If it doesn't start with a virtual block device, or there is some
* error, return -1 */
static bool find_vbd_device_prefix(const std::string& path, std::string* result) {
result->clear();
if (!android::base::StartsWith(path, "/devices/vbd-")) return false;
/* Beginning of the prefix is the initial "vbd-" after "/devices/" */
std::string::size_type start = 13;
/* End of the prefix is one path '/' later, capturing the
virtual block device ID. Example: 768 */
auto end = path.find('/', start);
if (end == std::string::npos) return false;
auto length = end - start;
if (length == 0) return false;
*result = path.substr(start, length);
return true;
}
2008-10-21 16:00:00 +02:00
static void parse_event(const char *msg, struct uevent *uevent)
{
uevent->action = "";
uevent->path = "";
uevent->subsystem = "";
uevent->firmware = "";
uevent->major = -1;
uevent->minor = -1;
uevent->partition_name = NULL;
uevent->partition_num = -1;
uevent->device_name = NULL;
2008-10-21 16:00:00 +02:00
/* currently ignoring SEQNUM */
while(*msg) {
if(!strncmp(msg, "ACTION=", 7)) {
msg += 7;
uevent->action = msg;
} else if(!strncmp(msg, "DEVPATH=", 8)) {
msg += 8;
uevent->path = msg;
} else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
msg += 10;
uevent->subsystem = msg;
} else if(!strncmp(msg, "FIRMWARE=", 9)) {
msg += 9;
uevent->firmware = msg;
} else if(!strncmp(msg, "MAJOR=", 6)) {
msg += 6;
uevent->major = atoi(msg);
} else if(!strncmp(msg, "MINOR=", 6)) {
msg += 6;
uevent->minor = atoi(msg);
} else if(!strncmp(msg, "PARTN=", 6)) {
msg += 6;
uevent->partition_num = atoi(msg);
} else if(!strncmp(msg, "PARTNAME=", 9)) {
msg += 9;
uevent->partition_name = msg;
} else if(!strncmp(msg, "DEVNAME=", 8)) {
msg += 8;
uevent->device_name = msg;
2008-10-21 16:00:00 +02:00
}
/* advance to after the next \0 */
2008-10-21 16:00:00 +02:00
while(*msg++)
;
}
if (LOG_UEVENTS) {
LOG(INFO) << android::base::StringPrintf("event { '%s', '%s', '%s', '%s', %d, %d }",
uevent->action, uevent->path, uevent->subsystem,
uevent->firmware, uevent->major, uevent->minor);
}
2008-10-21 16:00:00 +02:00
}
std::vector<std::string> get_character_device_symlinks(uevent* uevent) {
platform_node* pdev = find_platform_device(uevent->path);
if (!pdev) return {};
/* skip "/devices/platform/<driver>" */
std::string parent = std::string(uevent->path);
auto parent_start = parent.find('/', pdev->path_len);
if (parent_start == std::string::npos) return {};
parent.erase(0, parent_start);
if (!android::base::StartsWith(parent, "/usb")) return {};
// skip root hub name and device. use device interface
// skip 3 slashes, including the first / by starting the search at the 1st character, not 0th.
// then extract what comes between the 3rd and 4th slash
// e.g. "/usb/usb_device/name/tty2-1:1.0" -> "name"
std::string::size_type start = 0;
start = parent.find('/', start + 1);
if (start == std::string::npos) return {};
start = parent.find('/', start + 1);
if (start == std::string::npos) return {};
auto end = parent.find('/', start + 1);
if (end == std::string::npos) return {};
start++; // Skip the first '/'
auto length = end - start;
if (length == 0) return {};
auto name_string = parent.substr(start, length);
// TODO: remove std::string() when uevent->subsystem is an std::string
std::vector<std::string> links;
links.emplace_back("/dev/usb/" + std::string(uevent->subsystem) + name_string);
mkdir("/dev/usb", 0755);
return links;
}
// replaces any unacceptable characters with '_', the
// length of the resulting string is equal to the input string
void sanitize_partition_name(std::string* string) {
const char* accept =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789"
"_-.";
if (!string) return;
std::string::size_type pos = 0;
while ((pos = string->find_first_not_of(accept, pos)) != std::string::npos) {
(*string)[pos] = '_';
}
}
std::vector<std::string> get_block_device_symlinks(uevent* uevent) {
std::string device;
struct platform_node* pdev;
std::string type;
pdev = find_platform_device(uevent->path);
if (pdev) {
device = pdev->name;
type = "platform";
} else if (find_pci_device_prefix(uevent->path, &device)) {
type = "pci";
} else if (find_vbd_device_prefix(uevent->path, &device)) {
type = "vbd";
} else {
return {};
}
std::vector<std::string> links;
LOG(VERBOSE) << "found " << type << " device " << device;
auto link_path = "/dev/block/" + type + "/" + device;
if (uevent->partition_name) {
std::string partition_name_sanitized(uevent->partition_name);
sanitize_partition_name(&partition_name_sanitized);
if (partition_name_sanitized != uevent->partition_name) {
LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '"
<< partition_name_sanitized << "'";
}
links.emplace_back(link_path + "/by-name/" + partition_name_sanitized);
}
if (uevent->partition_num >= 0) {
links.emplace_back(link_path + "/by-num/p" + std::to_string(uevent->partition_num));
}
// TODO: remove uevent_path when uevent->path is an std::string
std::string uevent_path = uevent->path;
auto last_slash = uevent_path.rfind('/');
links.emplace_back(link_path + "/" + uevent_path.substr(last_slash + 1));
return links;
}
static void make_link_init(const char* oldpath, const char* newpath) {
const char* slash = strrchr(newpath, '/');
if (!slash) return;
if (mkdir_recursive(dirname(newpath), 0755)) {
PLOG(ERROR) << "Failed to create directory " << dirname(newpath);
}
if (symlink(oldpath, newpath) && errno != EEXIST) {
PLOG(ERROR) << "Failed to symlink " << oldpath << " to " << newpath;
}
}
static void remove_link(const char* oldpath, const char* newpath) {
std::string path;
if (android::base::Readlink(newpath, &path) && path == oldpath) unlink(newpath);
}
static void handle_device(const char* action, const char* devpath, const char* path, int block,
int major, int minor, const std::vector<std::string>& links) {
if(!strcmp(action, "add")) {
make_device(devpath, path, block, major, minor, links);
for (const auto& link : links) {
make_link_init(devpath, link.c_str());
}
2008-10-21 16:00:00 +02:00
}
if(!strcmp(action, "remove")) {
for (const auto& link : links) {
remove_link(devpath, link.c_str());
}
2008-10-21 16:00:00 +02:00
unlink(devpath);
}
2008-10-21 16:00:00 +02:00
}
static void handle_platform_device_event(struct uevent *uevent)
{
const char *path = uevent->path;
if (!strcmp(uevent->action, "add"))
add_platform_device(path);
else if (!strcmp(uevent->action, "remove"))
remove_platform_device(path);
}
static const char *parse_device_name(struct uevent *uevent, unsigned int len)
{
const char *name;
/* if it's not a /dev device, nothing else to do */
if((uevent->major < 0) || (uevent->minor < 0))
return NULL;
/* do we have a name? */
name = strrchr(uevent->path, '/');
if(!name)
return NULL;
name++;
/* too-long names would overrun our buffer */
if(strlen(name) > len) {
LOG(ERROR) << "DEVPATH=" << name << " exceeds " << len << "-character limit on filename; ignoring event";
return NULL;
}
return name;
}
#define DEVPATH_LEN 96
#define MAX_DEV_NAME 64
static void handle_block_device_event(struct uevent *uevent)
{
const char *base = "/dev/block/";
const char *name;
char devpath[DEVPATH_LEN];
name = parse_device_name(uevent, MAX_DEV_NAME);
if (!name)
return;
snprintf(devpath, sizeof(devpath), "%s%s", base, name);
make_dir(base, 0755);
std::vector<std::string> links;
if (!strncmp(uevent->path, "/devices/", 9))
links = get_block_device_symlinks(uevent);
handle_device(uevent->action, devpath, uevent->path, 1,
uevent->major, uevent->minor, links);
}
static bool assemble_devpath(char *devpath, const char *dirname,
const char *devname)
{
int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
if (s < 0) {
PLOG(ERROR) << "failed to assemble device path; ignoring event";
return false;
} else if (s >= DEVPATH_LEN) {
LOG(ERROR) << dirname << "/" << devname
<< " exceeds " << DEVPATH_LEN << "-character limit on path; ignoring event";
return false;
}
return true;
}
static void mkdir_recursive_for_devpath(const char *devpath)
{
char dir[DEVPATH_LEN];
char *slash;
strcpy(dir, devpath);
slash = strrchr(dir, '/');
*slash = '\0';
mkdir_recursive(dir, 0755);
}
static void handle_generic_device_event(struct uevent *uevent)
{
const char *name;
char devpath[DEVPATH_LEN] = {0};
name = parse_device_name(uevent, MAX_DEV_NAME);
if (!name)
return;
struct ueventd_subsystem *subsystem =
ueventd_subsystem_find_by_name(uevent->subsystem);
if (subsystem) {
const char *devname;
switch (subsystem->devname_src) {
case DEVNAME_UEVENT_DEVNAME:
devname = uevent->device_name;
break;
case DEVNAME_UEVENT_DEVPATH:
devname = name;
break;
default:
LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event";
return;
}
if (!assemble_devpath(devpath, subsystem->dirname, devname))
return;
mkdir_recursive_for_devpath(devpath);
} else if (!strncmp(uevent->subsystem, "usb", 3)) {
if (!strcmp(uevent->subsystem, "usb")) {
if (uevent->device_name) {
if (!assemble_devpath(devpath, "/dev", uevent->device_name))
return;
mkdir_recursive_for_devpath(devpath);
}
else {
/* This imitates the file system that would be created
* if we were using devfs instead.
* Minors are broken up into groups of 128, starting at "001"
*/
int bus_id = uevent->minor / 128 + 1;
int device_id = uevent->minor % 128 + 1;
/* build directories */
make_dir("/dev/bus", 0755);
make_dir("/dev/bus/usb", 0755);
snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
make_dir(devpath, 0755);
snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
}
} else {
/* ignore other USB events */
return;
}
} else {
snprintf(devpath, sizeof(devpath), "/dev/%s", name);
}
auto links = get_character_device_symlinks(uevent);
handle_device(uevent->action, devpath, uevent->path, 0, uevent->major, uevent->minor, links);
}
static void handle_device_event(struct uevent *uevent)
{
if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
fixup_sys_perms(uevent->path, uevent->subsystem);
if (!strncmp(uevent->subsystem, "block", 5)) {
handle_block_device_event(uevent);
} else if (!strncmp(uevent->subsystem, "platform", 8)) {
handle_platform_device_event(uevent);
} else {
handle_generic_device_event(uevent);
}
}
static void load_firmware(uevent* uevent, const std::string& root,
int fw_fd, size_t fw_size,
int loading_fd, int data_fd) {
// Start transfer.
android::base::WriteFully(loading_fd, "1", 1);
2008-10-21 16:00:00 +02:00
// Copy the firmware.
int rc = sendfile(data_fd, fw_fd, nullptr, fw_size);
if (rc == -1) {
PLOG(ERROR) << "firmware: sendfile failed { '" << root << "', '" << uevent->firmware << "' }";
2008-10-21 16:00:00 +02:00
}
// Tell the firmware whether to abort or commit.
const char* response = (rc != -1) ? "0" : "-1";
android::base::WriteFully(loading_fd, response, strlen(response));
2008-10-21 16:00:00 +02:00
}
static int is_booting() {
return access("/dev/.booting", F_OK) == 0;
}
static void process_firmware_event(uevent* uevent) {
int booting = is_booting();
2008-10-21 16:00:00 +02:00
LOG(INFO) << "firmware: loading '" << uevent->firmware << "' for '" << uevent->path << "'";
2008-10-21 16:00:00 +02:00
std::string root = android::base::StringPrintf("/sys%s", uevent->path);
std::string loading = root + "/loading";
std::string data = root + "/data";
2008-10-21 16:00:00 +02:00
android::base::unique_fd loading_fd(open(loading.c_str(), O_WRONLY|O_CLOEXEC));
if (loading_fd == -1) {
PLOG(ERROR) << "couldn't open firmware loading fd for " << uevent->firmware;
return;
}
2008-10-21 16:00:00 +02:00
android::base::unique_fd data_fd(open(data.c_str(), O_WRONLY|O_CLOEXEC));
if (data_fd == -1) {
PLOG(ERROR) << "couldn't open firmware data fd for " << uevent->firmware;
return;
}
2008-10-21 16:00:00 +02:00
try_loading_again:
for (size_t i = 0; i < arraysize(firmware_dirs); i++) {
std::string file = android::base::StringPrintf("%s/%s", firmware_dirs[i], uevent->firmware);
android::base::unique_fd fw_fd(open(file.c_str(), O_RDONLY|O_CLOEXEC));
struct stat sb;
if (fw_fd != -1 && fstat(fw_fd, &sb) != -1) {
load_firmware(uevent, root, fw_fd, sb.st_size, loading_fd, data_fd);
return;
}
}
2008-10-21 16:00:00 +02:00
if (booting) {
// If we're not fully booted, we may be missing
// filesystems needed for firmware, wait and retry.
std::this_thread::sleep_for(100ms);
booting = is_booting();
goto try_loading_again;
}
2008-10-21 16:00:00 +02:00
LOG(ERROR) << "firmware: could not find firmware for " << uevent->firmware;
2008-10-21 16:00:00 +02:00
// Write "-1" as our response to the kernel's firmware request, since we have nothing for it.
write(loading_fd, "-1", 2);
}
2008-10-21 16:00:00 +02:00
static void handle_firmware_event(uevent* uevent) {
if (strcmp(uevent->subsystem, "firmware")) return;
if (strcmp(uevent->action, "add")) return;
2008-10-21 16:00:00 +02:00
// Loading the firmware in a child means we can do that in parallel...
// (We ignore SIGCHLD rather than wait for our children.)
pid_t pid = fork();
if (pid == 0) {
Timer t;
2008-10-21 16:00:00 +02:00
process_firmware_event(uevent);
Replace the "coldboot" timeout with a property. Also rename init's existing boot-time related properties so they're all "ro.*" properties. Example result: # Three properties showing when init started... [ro.boottime.init]: [5294587604] # ...how long it waited for ueventd... [ro.boottime.init.cold_boot_wait]: [646956470] # ...and how long SELinux initialization took... [ro.boottime.init.selinux]: [45742921] # Plus one property for each service, showing when it first started. [ro.boottime.InputEventFind]: [10278767840] [ro.boottime.adbd]: [8359267180] [ro.boottime.atfwd]: [10338554773] [ro.boottime.audioserver]: [10298157478] [ro.boottime.bootanim]: [9323670089] [ro.boottime.cameraserver]: [10299402321] [ro.boottime.cnd]: [10335931856] [ro.boottime.debuggerd]: [7001352774] [ro.boottime.debuggerd64]: [7002261785] [ro.boottime.drm]: [10301082113] [ro.boottime.fingerprintd]: [10331443314] [ro.boottime.flash-nanohub-fw]: [6995265534] [ro.boottime.gatekeeperd]: [10340355242] [ro.boottime.healthd]: [7856893380] [ro.boottime.hwservicemanager]: [7856051088] [ro.boottime.imscmservice]: [10290530758] [ro.boottime.imsdatadaemon]: [10358136702] [ro.boottime.imsqmidaemon]: [10289084872] [ro.boottime.installd]: [10303296020] [ro.boottime.irsc_util]: [10279807632] [ro.boottime.keystore]: [10305034093] [ro.boottime.lmkd]: [7863506714] [ro.boottime.loc_launcher]: [10324525241] [ro.boottime.logd]: [6526221633] [ro.boottime.logd-reinit]: [7850662702] [ro.boottime.mcfg-sh]: [10337268315] [ro.boottime.media]: [10312152687] [ro.boottime.mediacodec]: [10306852530] [ro.boottime.mediadrm]: [10308707999] [ro.boottime.mediaextractor]: [10310681177] [ro.boottime.msm_irqbalance]: [7862451974] [ro.boottime.netd]: [10313523104] [ro.boottime.netmgrd]: [10285009351] [ro.boottime.oem_qmi_server]: [10293329092] [ro.boottime.per_mgr]: [7857915776] [ro.boottime.per_proxy]: [8335121605] [ro.boottime.perfd]: [10283443101] [ro.boottime.qcamerasvr]: [10329644772] [ro.boottime.qmuxd]: [10282346643] [ro.boottime.qseecomd]: [6855708593] [ro.boottime.qti]: [10286196851] [ro.boottime.ril-daemon]: [10314933677] [ro.boottime.rmt_storage]: [7859105047] [ro.boottime.servicemanager]: [7864555881] [ro.boottime.ss_ramdump]: [8337634938] [ro.boottime.ssr_setup]: [8336268324] [ro.boottime.surfaceflinger]: [7866921402] [ro.boottime.thermal-engine]: [10281249924] [ro.boottime.time_daemon]: [10322006542] [ro.boottime.ueventd]: [5618663938] [ro.boottime.vold]: [7003493920] [ro.boottime.wificond]: [10316641073] [ro.boottime.wpa_supplicant]: [18959816881] [ro.boottime.zygote]: [10295295029] [ro.boottime.zygote_secondary]: [10296637269] Bug: http://b/31800756 Test: boots Change-Id: I094cce0c1bab9406d950ca94212689dc2e15dba5
2016-11-29 20:20:58 +01:00
LOG(INFO) << "loading " << uevent->path << " took " << t;
_exit(EXIT_SUCCESS);
} else if (pid == -1) {
PLOG(ERROR) << "could not fork to process firmware event for " << uevent->firmware;
2008-10-21 16:00:00 +02:00
}
}
static bool inline should_stop_coldboot(coldboot_action_t act)
{
return (act == COLDBOOT_STOP || act == COLDBOOT_FINISH);
}
#define UEVENT_MSG_LEN 2048
static inline coldboot_action_t handle_device_fd_with(
std::function<coldboot_action_t(uevent* uevent)> handle_uevent)
2008-10-21 16:00:00 +02:00
{
char msg[UEVENT_MSG_LEN+2];
int n;
while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
2008-10-21 16:00:00 +02:00
continue;
msg[n] = '\0';
msg[n+1] = '\0';
struct uevent uevent;
2008-10-21 16:00:00 +02:00
parse_event(msg, &uevent);
coldboot_action_t act = handle_uevent(&uevent);
if (should_stop_coldboot(act))
return act;
}
return COLDBOOT_CONTINUE;
}
2008-10-21 16:00:00 +02:00
coldboot_action_t handle_device_fd(coldboot_callback fn)
{
coldboot_action_t ret = handle_device_fd_with(
[&](uevent* uevent) -> coldboot_action_t {
if (selinux_status_updated() > 0) {
struct selabel_handle *sehandle2;
sehandle2 = selinux_android_file_context_handle();
if (sehandle2) {
selabel_close(sehandle);
sehandle = sehandle2;
}
}
// default is to always create the devices
coldboot_action_t act = COLDBOOT_CREATE;
if (fn) {
act = fn(uevent);
}
if (act == COLDBOOT_CREATE || act == COLDBOOT_STOP) {
handle_device_event(uevent);
handle_firmware_event(uevent);
}
return act;
});
return ret;
2008-10-21 16:00:00 +02:00
}
/* Coldboot walks parts of the /sys tree and pokes the uevent files
** to cause the kernel to regenerate device add events that happened
** before init's device manager was started
**
** We drain any pending events from the netlink socket every time
** we poke another uevent file to make sure we don't overrun the
** socket's buffer.
2008-10-21 16:00:00 +02:00
*/
static coldboot_action_t do_coldboot(DIR *d, coldboot_callback fn)
2008-10-21 16:00:00 +02:00
{
struct dirent *de;
int dfd, fd;
coldboot_action_t act = COLDBOOT_CONTINUE;
2008-10-21 16:00:00 +02:00
dfd = dirfd(d);
fd = openat(dfd, "uevent", O_WRONLY);
if (fd >= 0) {
2008-10-21 16:00:00 +02:00
write(fd, "add\n", 4);
close(fd);
act = handle_device_fd(fn);
if (should_stop_coldboot(act))
return act;
2008-10-21 16:00:00 +02:00
}
while (!should_stop_coldboot(act) && (de = readdir(d))) {
2008-10-21 16:00:00 +02:00
DIR *d2;
if(de->d_type != DT_DIR || de->d_name[0] == '.')
continue;
fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
if(fd < 0)
continue;
d2 = fdopendir(fd);
if(d2 == 0)
close(fd);
else {
act = do_coldboot(d2, fn);
2008-10-21 16:00:00 +02:00
closedir(d2);
}
}
// default is always to continue looking for uevents
return act;
2008-10-21 16:00:00 +02:00
}
static coldboot_action_t coldboot(const char *path, coldboot_callback fn)
2008-10-21 16:00:00 +02:00
{
std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
if (d) {
return do_coldboot(d.get(), fn);
2008-10-21 16:00:00 +02:00
}
return COLDBOOT_CONTINUE;
2008-10-21 16:00:00 +02:00
}
void device_init(const char* path, coldboot_callback fn) {
if (!sehandle) {
sehandle = selinux_android_file_context_handle();
}
// open uevent socket and selinux status only if it hasn't been
// done before
if (device_fd == -1) {
/* is 256K enough? udev uses 16MB! */
device_fd.reset(uevent_open_socket(256 * 1024, true));
if (device_fd == -1) {
return;
}
fcntl(device_fd, F_SETFL, O_NONBLOCK);
selinux_status_open(true);
}
2008-10-21 16:00:00 +02:00
if (access(COLDBOOT_DONE, F_OK) == 0) {
LOG(VERBOSE) << "Skipping coldboot, already done!";
return;
}
Timer t;
coldboot_action_t act;
if (!path) {
act = coldboot("/sys/class", fn);
if (!should_stop_coldboot(act)) {
act = coldboot("/sys/block", fn);
if (!should_stop_coldboot(act)) {
act = coldboot("/sys/devices", fn);
}
}
} else {
act = coldboot(path, fn);
}
// If we have a callback, then do as it says. If no, then the default is
// to always create COLDBOOT_DONE file.
if (!fn || (act == COLDBOOT_FINISH)) {
close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
}
Replace the "coldboot" timeout with a property. Also rename init's existing boot-time related properties so they're all "ro.*" properties. Example result: # Three properties showing when init started... [ro.boottime.init]: [5294587604] # ...how long it waited for ueventd... [ro.boottime.init.cold_boot_wait]: [646956470] # ...and how long SELinux initialization took... [ro.boottime.init.selinux]: [45742921] # Plus one property for each service, showing when it first started. [ro.boottime.InputEventFind]: [10278767840] [ro.boottime.adbd]: [8359267180] [ro.boottime.atfwd]: [10338554773] [ro.boottime.audioserver]: [10298157478] [ro.boottime.bootanim]: [9323670089] [ro.boottime.cameraserver]: [10299402321] [ro.boottime.cnd]: [10335931856] [ro.boottime.debuggerd]: [7001352774] [ro.boottime.debuggerd64]: [7002261785] [ro.boottime.drm]: [10301082113] [ro.boottime.fingerprintd]: [10331443314] [ro.boottime.flash-nanohub-fw]: [6995265534] [ro.boottime.gatekeeperd]: [10340355242] [ro.boottime.healthd]: [7856893380] [ro.boottime.hwservicemanager]: [7856051088] [ro.boottime.imscmservice]: [10290530758] [ro.boottime.imsdatadaemon]: [10358136702] [ro.boottime.imsqmidaemon]: [10289084872] [ro.boottime.installd]: [10303296020] [ro.boottime.irsc_util]: [10279807632] [ro.boottime.keystore]: [10305034093] [ro.boottime.lmkd]: [7863506714] [ro.boottime.loc_launcher]: [10324525241] [ro.boottime.logd]: [6526221633] [ro.boottime.logd-reinit]: [7850662702] [ro.boottime.mcfg-sh]: [10337268315] [ro.boottime.media]: [10312152687] [ro.boottime.mediacodec]: [10306852530] [ro.boottime.mediadrm]: [10308707999] [ro.boottime.mediaextractor]: [10310681177] [ro.boottime.msm_irqbalance]: [7862451974] [ro.boottime.netd]: [10313523104] [ro.boottime.netmgrd]: [10285009351] [ro.boottime.oem_qmi_server]: [10293329092] [ro.boottime.per_mgr]: [7857915776] [ro.boottime.per_proxy]: [8335121605] [ro.boottime.perfd]: [10283443101] [ro.boottime.qcamerasvr]: [10329644772] [ro.boottime.qmuxd]: [10282346643] [ro.boottime.qseecomd]: [6855708593] [ro.boottime.qti]: [10286196851] [ro.boottime.ril-daemon]: [10314933677] [ro.boottime.rmt_storage]: [7859105047] [ro.boottime.servicemanager]: [7864555881] [ro.boottime.ss_ramdump]: [8337634938] [ro.boottime.ssr_setup]: [8336268324] [ro.boottime.surfaceflinger]: [7866921402] [ro.boottime.thermal-engine]: [10281249924] [ro.boottime.time_daemon]: [10322006542] [ro.boottime.ueventd]: [5618663938] [ro.boottime.vold]: [7003493920] [ro.boottime.wificond]: [10316641073] [ro.boottime.wpa_supplicant]: [18959816881] [ro.boottime.zygote]: [10295295029] [ro.boottime.zygote_secondary]: [10296637269] Bug: http://b/31800756 Test: boots Change-Id: I094cce0c1bab9406d950ca94212689dc2e15dba5
2016-11-29 20:20:58 +01:00
LOG(INFO) << "Coldboot took " << t;
}
2008-10-21 16:00:00 +02:00
void device_close() {
destroy_platform_devices();
device_fd.reset();
selinux_status_close();
}
int get_device_fd() {
return device_fd;
2008-10-21 16:00:00 +02:00
}