2008-10-21 16:00:00 +02:00
|
|
|
/*
|
2014-02-20 23:59:07 +01: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.
|
|
|
|
*/
|
|
|
|
|
2016-08-31 23:41:51 +02:00
|
|
|
#include <dirent.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
#include <errno.h>
|
2016-08-31 23:41:51 +02:00
|
|
|
#include <fcntl.h>
|
2012-07-02 20:32:30 +02:00
|
|
|
#include <fnmatch.h>
|
2016-08-31 23:41:51 +02:00
|
|
|
#include <libgen.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <linux/netlink.h>
|
2010-11-17 20:47:23 +01:00
|
|
|
#include <stddef.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-08-31 23:41:51 +02:00
|
|
|
#include <string.h>
|
2016-11-12 20:44:16 +01:00
|
|
|
#include <sys/sendfile.h>
|
2016-08-31 23:41:51 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/time.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
#include <sys/types.h>
|
2016-08-31 23:41:51 +02:00
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/wait.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-04-05 02:53:45 +02:00
|
|
|
#include <algorithm>
|
2016-02-18 23:52:46 +01:00
|
|
|
#include <memory>
|
2017-04-05 02:53:45 +02:00
|
|
|
#include <string>
|
2016-11-15 02:08:47 +01:00
|
|
|
#include <thread>
|
2017-04-05 02:53:45 +02:00
|
|
|
#include <vector>
|
2016-02-18 23:52:46 +01:00
|
|
|
|
2016-01-28 09:10:54 +01:00
|
|
|
#include <android-base/file.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <android-base/logging.h>
|
2016-05-06 17:06:59 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
2017-04-05 02:53:45 +02:00
|
|
|
#include <android-base/strings.h>
|
2016-02-01 08:07:40 +01:00
|
|
|
#include <android-base/unique_fd.h>
|
2011-09-02 02:09:44 +02:00
|
|
|
#include <cutils/list.h>
|
2011-04-25 05:08:17 +02:00
|
|
|
#include <cutils/uevent.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
#include <selinux/android.h>
|
|
|
|
#include <selinux/avc.h>
|
|
|
|
#include <selinux/label.h>
|
|
|
|
#include <selinux/selinux.h>
|
2011-04-25 05:08:17 +02:00
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
#include "devices.h"
|
2013-11-19 00:24:40 +01:00
|
|
|
#include "ueventd_parser.h"
|
2010-04-09 01:16:20 +02:00
|
|
|
#include "util.h"
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
#define SYSFS_PREFIX "/sys"
|
|
|
|
|
2012-06-11 19:37:39 +02:00
|
|
|
extern struct selabel_handle *sehandle;
|
2012-01-13 14:48:47 +01:00
|
|
|
|
2017-02-09 05:27:12 +01:00
|
|
|
static android::base::unique_fd device_fd;
|
2010-04-14 04:25:51 +02:00
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
struct perms_ {
|
|
|
|
char *name;
|
2010-10-27 00:09:43 +02:00
|
|
|
char *attr;
|
2008-10-21 16:00:00 +02:00
|
|
|
mode_t perm;
|
|
|
|
unsigned int uid;
|
|
|
|
unsigned int gid;
|
|
|
|
unsigned short prefix;
|
2012-07-02 20:32:30 +02:00
|
|
|
unsigned short wildcard;
|
2008-10-21 16:00:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct perm_node {
|
|
|
|
struct perms_ dp;
|
|
|
|
struct listnode plist;
|
|
|
|
};
|
2010-10-27 00:09:43 +02:00
|
|
|
|
|
|
|
static list_declare(sys_perms);
|
2010-04-20 23:32:50 +02:00
|
|
|
static list_declare(dev_perms);
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2010-10-27 00:09:43 +02:00
|
|
|
int add_dev_perms(const char *name, const char *attr,
|
|
|
|
mode_t perm, unsigned int uid, unsigned int gid,
|
2012-07-02 20:32:30 +02:00
|
|
|
unsigned short prefix,
|
|
|
|
unsigned short wildcard) {
|
2015-02-04 02:12:07 +01:00
|
|
|
struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
|
2008-10-21 16:00:00 +02:00
|
|
|
if (!node)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2010-10-27 00:09:43 +02:00
|
|
|
node->dp.name = strdup(name);
|
2016-11-16 00:35:16 +01:00
|
|
|
if (!node->dp.name) {
|
|
|
|
free(node);
|
2008-10-21 16:00:00 +02:00
|
|
|
return -ENOMEM;
|
2016-11-16 00:35:16 +01:00
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2010-10-27 00:09:43 +02:00
|
|
|
if (attr) {
|
|
|
|
node->dp.attr = strdup(attr);
|
2016-11-16 00:35:16 +01:00
|
|
|
if (!node->dp.attr) {
|
|
|
|
free(node->dp.name);
|
|
|
|
free(node);
|
2010-10-27 00:09:43 +02:00
|
|
|
return -ENOMEM;
|
2016-11-16 00:35:16 +01:00
|
|
|
}
|
2010-10-27 00:09:43 +02:00
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
node->dp.perm = perm;
|
|
|
|
node->dp.uid = uid;
|
|
|
|
node->dp.gid = gid;
|
|
|
|
node->dp.prefix = prefix;
|
2012-07-02 20:32:30 +02:00
|
|
|
node->dp.wildcard = wildcard;
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2010-10-27 00:09:43 +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;
|
|
|
|
}
|
|
|
|
|
2014-07-02 22:08:13 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-06 17:06:59 +02:00
|
|
|
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;
|
|
|
|
}
|
2016-05-06 19:28:48 +02:00
|
|
|
|
2016-05-06 17:06:59 +02:00
|
|
|
std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path));
|
|
|
|
return perm_path_matches(subsys_path.c_str(), dp);
|
|
|
|
}
|
2016-05-06 19:28:48 +02:00
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
static void fixup_sys_perms(const std::string& upath, const std::string& subsystem) {
|
2016-05-06 17:06:59 +02:00
|
|
|
// upaths omit the "/sys" that paths in this list
|
|
|
|
// contain, so we prepend it...
|
2017-04-11 22:53:37 +02:00
|
|
|
std::string path = SYSFS_PREFIX + upath;
|
2016-05-06 19:28:48 +02:00
|
|
|
|
2016-05-06 17:06:59 +02:00
|
|
|
listnode* node;
|
|
|
|
list_for_each(node, &sys_perms) {
|
|
|
|
perms_* dp = &(node_to_item(node, perm_node, plist))->dp;
|
2017-04-11 22:53:37 +02:00
|
|
|
if (match_subsystem(dp, SYSFS_PREFIX "/class/%s/%s", path.c_str(), subsystem.c_str())) {
|
2016-05-06 17:06:59 +02:00
|
|
|
; // matched
|
2017-04-11 22:53:37 +02:00
|
|
|
} else if (match_subsystem(dp, SYSFS_PREFIX "/bus/%s/devices/%s", path.c_str(),
|
|
|
|
subsystem.c_str())) {
|
2016-05-06 17:06:59 +02:00
|
|
|
; // matched
|
|
|
|
} else if (!perm_path_matches(path.c_str(), dp)) {
|
|
|
|
continue;
|
2016-05-06 19:28:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string attr_file = path + "/" + dp->attr;
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(INFO) << "fixup " << attr_file
|
|
|
|
<< " " << dp->uid << " " << dp->gid << " " << std::oct << dp->perm;
|
2016-05-06 19:28:48 +02:00
|
|
|
chown(attr_file.c_str(), dp->uid, dp->gid);
|
|
|
|
chmod(attr_file.c_str(), dp->perm);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (access(path.c_str(), F_OK) == 0) {
|
2016-07-28 22:55:39 +02:00
|
|
|
LOG(VERBOSE) << "restorecon_recursive: " << path;
|
2016-11-15 00:40:18 +01:00
|
|
|
restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE);
|
2016-05-06 19:28:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-05 02:53:45 +02:00
|
|
|
static mode_t get_device_perm(const char* path, const std::vector<std::string>& links,
|
|
|
|
unsigned* uid, unsigned* gid) {
|
2010-04-20 23:32:50 +02:00
|
|
|
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;
|
|
|
|
|
2017-04-05 02:53:45 +02:00
|
|
|
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); })) {
|
2014-07-02 22:08:13 +02:00
|
|
|
*uid = dp->uid;
|
|
|
|
*gid = dp->gid;
|
|
|
|
return dp->perm;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
2010-04-20 23:32:50 +02:00
|
|
|
/* Default if nothing found. */
|
|
|
|
*uid = 0;
|
|
|
|
*gid = 0;
|
|
|
|
return 0600;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
static void make_device(const std::string& path, int block, int major, int minor,
|
2017-04-05 02:53:45 +02:00
|
|
|
const std::vector<std::string>& links) {
|
2008-10-21 16:00:00 +02:00
|
|
|
unsigned uid;
|
|
|
|
unsigned gid;
|
|
|
|
mode_t mode;
|
|
|
|
dev_t dev;
|
2012-01-13 14:48:47 +01:00
|
|
|
char *secontext = NULL;
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
mode = get_device_perm(path.c_str(), links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
|
2012-10-17 08:07:05 +02:00
|
|
|
|
2017-02-03 16:51:55 +01:00
|
|
|
if (sehandle) {
|
2017-04-05 02:53:45 +02:00
|
|
|
std::vector<const char*> c_links;
|
|
|
|
for (const auto& link : links) {
|
|
|
|
c_links.emplace_back(link.c_str());
|
|
|
|
}
|
|
|
|
c_links.emplace_back(nullptr);
|
2017-04-11 22:53:37 +02:00
|
|
|
if (selabel_lookup_best_match(sehandle, &secontext, path.c_str(), &c_links[0], mode)) {
|
2017-02-03 16:51:55 +01:00
|
|
|
PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setfscreatecon(secontext);
|
2016-04-25 17:22:27 +02:00
|
|
|
}
|
2012-10-17 08:07:05 +02:00
|
|
|
|
2010-09-03 21:25:34 +02:00
|
|
|
dev = makedev(major, minor);
|
2010-01-22 03:13:39 +01:00
|
|
|
/* 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. */
|
2017-02-23 22:46:09 +01:00
|
|
|
if (setegid(gid)) {
|
|
|
|
PLOG(ERROR) << "setegid(" << gid << ") for " << path << " device failed";
|
|
|
|
goto out;
|
|
|
|
}
|
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. */
|
2017-04-11 22:53:37 +02:00
|
|
|
if (mknod(path.c_str(), mode, dev) && (errno == EEXIST) && secontext) {
|
2016-06-02 18:53:44 +02:00
|
|
|
char* fcon = nullptr;
|
2017-04-11 22:53:37 +02:00
|
|
|
int rc = lgetfilecon(path.c_str(), &fcon);
|
2016-06-02 18:53:44 +02:00
|
|
|
if (rc < 0) {
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
|
2016-06-02 18:53:44 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool different = strcmp(fcon, secontext) != 0;
|
|
|
|
freecon(fcon);
|
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
if (different && lsetfilecon(path.c_str(), secontext)) {
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path << "' device";
|
2016-04-25 17:22:27 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-02 18:53:44 +02:00
|
|
|
|
|
|
|
out:
|
2017-04-11 22:53:37 +02:00
|
|
|
chown(path.c_str(), uid, -1);
|
2017-02-23 22:46:09 +01:00
|
|
|
if (setegid(AID_ROOT)) {
|
|
|
|
PLOG(FATAL) << "setegid(AID_ROOT) failed";
|
|
|
|
}
|
2012-10-17 08:07:05 +02:00
|
|
|
|
2017-02-03 16:51:55 +01:00
|
|
|
if (secontext) {
|
|
|
|
freecon(secontext);
|
|
|
|
setfscreatecon(NULL);
|
|
|
|
}
|
2012-01-13 14:48:47 +01:00
|
|
|
}
|
|
|
|
|
2017-04-06 23:41:30 +02:00
|
|
|
// TODO: Move this to be a member variable of a future devices class.
|
|
|
|
std::vector<std::string> platform_devices;
|
2011-03-31 03:32:12 +02:00
|
|
|
|
2017-04-06 23:41:30 +02:00
|
|
|
// 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 false
|
|
|
|
bool find_platform_device(const std::string& path, std::string* out_path) {
|
|
|
|
out_path->clear();
|
|
|
|
// platform_devices is searched backwards, since parents are added before their children,
|
|
|
|
// and we want to match as deep of a child as we can.
|
|
|
|
for (auto it = platform_devices.rbegin(); it != platform_devices.rend(); ++it) {
|
|
|
|
auto platform_device_path_length = it->length();
|
|
|
|
if (platform_device_path_length < path.length() &&
|
|
|
|
path[platform_device_path_length] == '/' &&
|
|
|
|
android::base::StartsWith(path, it->c_str())) {
|
|
|
|
*out_path = *it;
|
|
|
|
return true;
|
2011-03-31 03:32:12 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-06 23:41:30 +02:00
|
|
|
return false;
|
2017-02-09 05:27:12 +01:00
|
|
|
}
|
|
|
|
|
2013-09-14 02:41:20 +02:00
|
|
|
/* 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 */
|
2017-04-05 02:53:45 +02:00
|
|
|
static bool find_pci_device_prefix(const std::string& path, std::string* result) {
|
|
|
|
result->clear();
|
2013-09-14 02:41:20 +02:00
|
|
|
|
2017-04-05 02:53:45 +02:00
|
|
|
if (!android::base::StartsWith(path, "/devices/pci")) return false;
|
2013-09-14 02:41:20 +02:00
|
|
|
|
|
|
|
/* Beginning of the prefix is the initial "pci" after "/devices/" */
|
2017-04-05 02:53:45 +02:00
|
|
|
std::string::size_type start = 9;
|
2013-09-14 02:41:20 +02:00
|
|
|
|
|
|
|
/* End of the prefix is two path '/' later, capturing the domain/bus number
|
|
|
|
* and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
|
2017-04-05 02:53:45 +02:00
|
|
|
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;
|
2013-09-14 02:41:20 +02:00
|
|
|
}
|
|
|
|
|
2017-03-03 16:27:29 +01:00
|
|
|
/* 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 */
|
2017-04-05 02:53:45 +02:00
|
|
|
static bool find_vbd_device_prefix(const std::string& path, std::string* result) {
|
|
|
|
result->clear();
|
|
|
|
|
|
|
|
if (!android::base::StartsWith(path, "/devices/vbd-")) return false;
|
2017-03-03 16:27:29 +01:00
|
|
|
|
|
|
|
/* Beginning of the prefix is the initial "vbd-" after "/devices/" */
|
2017-04-05 02:53:45 +02:00
|
|
|
std::string::size_type start = 13;
|
2017-03-03 16:27:29 +01:00
|
|
|
|
|
|
|
/* End of the prefix is one path '/' later, capturing the
|
|
|
|
virtual block device ID. Example: 768 */
|
2017-04-05 02:53:45 +02:00
|
|
|
auto end = path.find('/', start);
|
|
|
|
if (end == std::string::npos) return false;
|
2017-03-03 16:27:29 +01:00
|
|
|
|
2017-04-05 02:53:45 +02:00
|
|
|
auto length = end - start;
|
|
|
|
if (length == 0) return false;
|
2017-03-03 16:27:29 +01:00
|
|
|
|
2017-04-05 02:53:45 +02:00
|
|
|
*result = path.substr(start, length);
|
|
|
|
return true;
|
2017-03-03 16:27:29 +01:00
|
|
|
}
|
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
void parse_event(const char* msg, uevent* uevent) {
|
|
|
|
uevent->partition_num = -1;
|
2008-10-21 16:00:00 +02:00
|
|
|
uevent->major = -1;
|
|
|
|
uevent->minor = -1;
|
2017-04-11 22:53:37 +02:00
|
|
|
// currently ignoring SEQNUM
|
2008-10-21 16:00:00 +02:00
|
|
|
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);
|
2010-04-09 01:16:20 +02:00
|
|
|
} 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;
|
2012-03-23 22:15:34 +01:00
|
|
|
} else if(!strncmp(msg, "DEVNAME=", 8)) {
|
|
|
|
msg += 8;
|
|
|
|
uevent->device_name = msg;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
// advance to after the next \0
|
2008-10-21 16:00:00 +02:00
|
|
|
while(*msg++)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2015-02-04 23:46:36 +01:00
|
|
|
if (LOG_UEVENTS) {
|
2017-04-11 22:53:37 +02:00
|
|
|
LOG(INFO) << "event { '" << uevent->action << "', '" << uevent->path << "', '"
|
|
|
|
<< uevent->subsystem << "', '" << uevent->firmware << "', " << uevent->major
|
|
|
|
<< ", " << uevent->minor << " }";
|
2015-02-04 23:46:36 +01:00
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2017-04-05 02:53:45 +02:00
|
|
|
std::vector<std::string> get_character_device_symlinks(uevent* uevent) {
|
2017-04-06 23:41:30 +02:00
|
|
|
std::string parent_device;
|
|
|
|
if (!find_platform_device(uevent->path, &parent_device)) return {};
|
2010-08-03 23:36:02 +02:00
|
|
|
|
2017-04-06 23:41:30 +02:00
|
|
|
// skip path to the parent driver
|
|
|
|
std::string path = uevent->path.substr(parent_device.length());
|
2010-08-03 23:36:02 +02:00
|
|
|
|
2017-04-06 23:41:30 +02:00
|
|
|
if (!android::base::StartsWith(path, "/usb")) return {};
|
2017-04-05 02:53:45 +02:00
|
|
|
|
|
|
|
// 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;
|
2017-04-06 23:41:30 +02:00
|
|
|
start = path.find('/', start + 1);
|
2017-04-05 02:53:45 +02:00
|
|
|
if (start == std::string::npos) return {};
|
|
|
|
|
2017-04-06 23:41:30 +02:00
|
|
|
start = path.find('/', start + 1);
|
2017-04-05 02:53:45 +02:00
|
|
|
if (start == std::string::npos) return {};
|
|
|
|
|
2017-04-06 23:41:30 +02:00
|
|
|
auto end = path.find('/', start + 1);
|
2017-04-05 02:53:45 +02:00
|
|
|
if (end == std::string::npos) return {};
|
|
|
|
|
|
|
|
start++; // Skip the first '/'
|
|
|
|
|
|
|
|
auto length = end - start;
|
|
|
|
if (length == 0) return {};
|
|
|
|
|
2017-04-06 23:41:30 +02:00
|
|
|
auto name_string = path.substr(start, length);
|
2017-04-05 02:53:45 +02:00
|
|
|
|
|
|
|
std::vector<std::string> links;
|
2017-04-11 22:53:37 +02:00
|
|
|
links.emplace_back("/dev/usb/" + uevent->subsystem + name_string);
|
2017-04-05 02:53:45 +02:00
|
|
|
|
|
|
|
mkdir("/dev/usb", 0755);
|
2010-08-03 23:36:02 +02:00
|
|
|
|
|
|
|
return links;
|
|
|
|
}
|
|
|
|
|
2017-04-06 00:58:31 +02:00
|
|
|
// replaces any unacceptable characters with '_', the
|
|
|
|
// length of the resulting string is equal to the input string
|
2017-04-05 02:53:45 +02:00
|
|
|
void sanitize_partition_name(std::string* string) {
|
2017-04-06 00:58:31 +02:00
|
|
|
const char* accept =
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"0123456789"
|
|
|
|
"_-.";
|
|
|
|
|
2017-04-05 02:53:45 +02:00
|
|
|
if (!string) return;
|
2017-04-06 00:58:31 +02:00
|
|
|
|
2017-04-05 02:53:45 +02:00
|
|
|
std::string::size_type pos = 0;
|
|
|
|
while ((pos = string->find_first_not_of(accept, pos)) != std::string::npos) {
|
|
|
|
(*string)[pos] = '_';
|
2017-04-06 00:58:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-05 02:53:45 +02:00
|
|
|
std::vector<std::string> get_block_device_symlinks(uevent* uevent) {
|
|
|
|
std::string device;
|
|
|
|
std::string type;
|
2010-04-09 01:16:20 +02:00
|
|
|
|
2017-04-06 23:41:30 +02:00
|
|
|
if (find_platform_device(uevent->path, &device)) {
|
|
|
|
// Skip /devices/platform or /devices/ if present
|
|
|
|
static const std::string devices_platform_prefix = "/devices/platform/";
|
|
|
|
static const std::string devices_prefix = "/devices/";
|
|
|
|
|
|
|
|
if (android::base::StartsWith(device, devices_platform_prefix.c_str())) {
|
|
|
|
device = device.substr(devices_platform_prefix.length());
|
|
|
|
} else if (android::base::StartsWith(device, devices_prefix.c_str())) {
|
|
|
|
device = device.substr(devices_prefix.length());
|
|
|
|
}
|
|
|
|
|
2013-09-14 02:41:20 +02:00
|
|
|
type = "platform";
|
2017-04-05 02:53:45 +02:00
|
|
|
} else if (find_pci_device_prefix(uevent->path, &device)) {
|
2013-09-14 02:41:20 +02:00
|
|
|
type = "pci";
|
2017-04-05 02:53:45 +02:00
|
|
|
} else if (find_vbd_device_prefix(uevent->path, &device)) {
|
2017-03-03 16:27:29 +01:00
|
|
|
type = "vbd";
|
2013-09-14 02:41:20 +02:00
|
|
|
} else {
|
2017-04-05 02:53:45 +02:00
|
|
|
return {};
|
2013-09-14 02:41:20 +02:00
|
|
|
}
|
2013-03-07 01:23:57 +01:00
|
|
|
|
2017-04-05 02:53:45 +02:00
|
|
|
std::vector<std::string> links;
|
2010-04-09 01:16:20 +02:00
|
|
|
|
2017-02-09 05:27:12 +01:00
|
|
|
LOG(VERBOSE) << "found " << type << " device " << device;
|
2011-03-31 03:32:12 +02:00
|
|
|
|
2017-04-05 02:53:45 +02:00
|
|
|
auto link_path = "/dev/block/" + type + "/" + device;
|
2010-04-09 01:16:20 +02:00
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
if (!uevent->partition_name.empty()) {
|
2017-04-05 02:53:45 +02:00
|
|
|
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 << "'";
|
2016-06-25 00:12:21 +02:00
|
|
|
}
|
2017-04-05 02:53:45 +02:00
|
|
|
links.emplace_back(link_path + "/by-name/" + partition_name_sanitized);
|
2010-04-09 01:16:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (uevent->partition_num >= 0) {
|
2017-04-05 02:53:45 +02:00
|
|
|
links.emplace_back(link_path + "/by-num/p" + std::to_string(uevent->partition_num));
|
2010-04-09 01:16:20 +02:00
|
|
|
}
|
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
auto last_slash = uevent->path.rfind('/');
|
|
|
|
links.emplace_back(link_path + "/" + uevent->path.substr(last_slash + 1));
|
2010-04-09 01:16:20 +02:00
|
|
|
|
|
|
|
return links;
|
|
|
|
}
|
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
static void make_link_init(const std::string& oldpath, const std::string& newpath) {
|
|
|
|
if (mkdir_recursive(dirname(newpath.c_str()), 0755)) {
|
|
|
|
PLOG(ERROR) << "Failed to create directory " << dirname(newpath.c_str());
|
|
|
|
}
|
2016-08-31 23:41:51 +02:00
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
if (symlink(oldpath.c_str(), newpath.c_str()) && errno != EEXIST) {
|
|
|
|
PLOG(ERROR) << "Failed to symlink " << oldpath << " to " << newpath;
|
|
|
|
}
|
2016-08-31 23:41:51 +02:00
|
|
|
}
|
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
static void remove_link(const std::string& oldpath, const std::string& newpath) {
|
|
|
|
std::string path;
|
|
|
|
if (android::base::Readlink(newpath, &path) && path == oldpath) unlink(newpath.c_str());
|
2016-08-31 23:41:51 +02:00
|
|
|
}
|
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
static void handle_device(const std::string& action, const std::string& devpath, int block,
|
2017-04-05 02:53:45 +02:00
|
|
|
int major, int minor, const std::vector<std::string>& links) {
|
2017-04-11 22:53:37 +02:00
|
|
|
if (action == "add") {
|
|
|
|
make_device(devpath, block, major, minor, links);
|
2017-04-05 02:53:45 +02:00
|
|
|
for (const auto& link : links) {
|
2017-04-11 22:53:37 +02:00
|
|
|
make_link_init(devpath, link);
|
2010-04-09 01:16:20 +02:00
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
if (action == "remove") {
|
2017-04-05 02:53:45 +02:00
|
|
|
for (const auto& link : links) {
|
2017-04-11 22:53:37 +02:00
|
|
|
remove_link(devpath, link);
|
2010-04-09 01:16:20 +02:00
|
|
|
}
|
2017-04-11 22:53:37 +02:00
|
|
|
unlink(devpath.c_str());
|
2010-04-09 01:16:20 +02:00
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2017-04-06 23:41:30 +02:00
|
|
|
void handle_platform_device_event(uevent* uevent) {
|
2017-04-11 22:53:37 +02:00
|
|
|
if (uevent->action == "add") {
|
2017-04-06 23:41:30 +02:00
|
|
|
platform_devices.emplace_back(uevent->path);
|
2017-04-11 22:53:37 +02:00
|
|
|
} else if (uevent->action == "remove") {
|
2017-04-06 23:41:30 +02:00
|
|
|
auto it = std::find(platform_devices.begin(), platform_devices.end(), uevent->path);
|
|
|
|
if (it != platform_devices.end()) platform_devices.erase(it);
|
2017-04-11 22:53:37 +02:00
|
|
|
}
|
2011-03-31 03:32:12 +02:00
|
|
|
}
|
|
|
|
|
2017-04-11 23:19:50 +02:00
|
|
|
static void handle_block_device_event(uevent* uevent) {
|
|
|
|
// if it's not a /dev device, nothing to do
|
|
|
|
if (uevent->major < 0 || uevent->minor < 0) return;
|
2011-03-31 02:37:17 +02:00
|
|
|
|
2017-04-11 23:19:50 +02:00
|
|
|
const char* base = "/dev/block/";
|
2012-01-13 14:48:47 +01:00
|
|
|
make_dir(base, 0755);
|
2011-03-31 02:37:17 +02:00
|
|
|
|
2017-04-11 23:19:50 +02:00
|
|
|
std::string name = android::base::Basename(uevent->path);
|
|
|
|
std::string devpath = base + name;
|
|
|
|
|
2017-04-05 02:53:45 +02:00
|
|
|
std::vector<std::string> links;
|
2017-04-11 22:53:37 +02:00
|
|
|
if (android::base::StartsWith(uevent->path, "/devices")) {
|
2013-09-14 02:41:20 +02:00
|
|
|
links = get_block_device_symlinks(uevent);
|
2017-04-11 22:53:37 +02:00
|
|
|
}
|
2011-03-31 02:37:17 +02:00
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
handle_device(uevent->action, devpath, 1, uevent->major, uevent->minor, links);
|
2013-11-19 00:24:40 +01:00
|
|
|
}
|
|
|
|
|
2017-04-11 23:19:50 +02:00
|
|
|
static void handle_generic_device_event(uevent* uevent) {
|
|
|
|
// if it's not a /dev device, nothing to do
|
|
|
|
if (uevent->major < 0 || uevent->minor < 0) return;
|
2011-03-31 02:37:17 +02:00
|
|
|
|
2017-04-11 23:19:50 +02:00
|
|
|
std::string name = android::base::Basename(uevent->path);
|
2017-04-11 22:53:37 +02:00
|
|
|
ueventd_subsystem* subsystem = ueventd_subsystem_find_by_name(uevent->subsystem.c_str());
|
2011-03-31 02:37:17 +02:00
|
|
|
|
2017-04-11 23:19:50 +02:00
|
|
|
std::string devpath;
|
2013-11-19 00:24:40 +01:00
|
|
|
|
|
|
|
if (subsystem) {
|
2017-04-11 23:19:50 +02:00
|
|
|
std::string devname;
|
2013-11-19 00:24:40 +01:00
|
|
|
|
|
|
|
switch (subsystem->devname_src) {
|
|
|
|
case DEVNAME_UEVENT_DEVNAME:
|
|
|
|
devname = uevent->device_name;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DEVNAME_UEVENT_DEVPATH:
|
|
|
|
devname = name;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event";
|
2013-11-19 00:24:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-11 23:19:50 +02:00
|
|
|
// TODO: Remove std::string()
|
|
|
|
devpath = std::string(subsystem->dirname) + "/" + devname;
|
|
|
|
mkdir_recursive(android::base::Dirname(devpath), 0755);
|
2017-04-11 22:53:37 +02:00
|
|
|
} else if (android::base::StartsWith(uevent->subsystem, "usb")) {
|
|
|
|
if (uevent->subsystem == "usb") {
|
|
|
|
if (!uevent->device_name.empty()) {
|
|
|
|
devpath = "/dev/" + uevent->device_name;
|
2017-04-11 23:19:50 +02:00
|
|
|
} 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;
|
|
|
|
devpath = android::base::StringPrintf("/dev/bus/usb/%03d/%03d", bus_id, device_id);
|
|
|
|
}
|
|
|
|
mkdir_recursive(android::base::Dirname(devpath), 0755);
|
|
|
|
} else {
|
|
|
|
// ignore other USB events
|
|
|
|
return;
|
|
|
|
}
|
2017-04-05 01:30:40 +02:00
|
|
|
} else {
|
2017-04-11 23:19:50 +02:00
|
|
|
devpath = "/dev/" + name;
|
2017-04-05 01:30:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto links = get_character_device_symlinks(uevent);
|
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
handle_device(uevent->action, devpath, 0, uevent->major, uevent->minor, links);
|
2011-03-31 02:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_device_event(struct uevent *uevent)
|
|
|
|
{
|
2017-04-11 22:53:37 +02:00
|
|
|
if (uevent->action == "add" || uevent->action == "change" || uevent->action == "online") {
|
2016-05-06 17:06:59 +02:00
|
|
|
fixup_sys_perms(uevent->path, uevent->subsystem);
|
2017-04-11 22:53:37 +02:00
|
|
|
}
|
2011-03-31 02:37:17 +02:00
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
if (uevent->subsystem == "block") {
|
2011-03-31 02:37:17 +02:00
|
|
|
handle_block_device_event(uevent);
|
2017-04-11 22:53:37 +02:00
|
|
|
} else if (uevent->subsystem == "platform") {
|
2011-03-31 03:32:12 +02:00
|
|
|
handle_platform_device_event(uevent);
|
2011-03-31 02:37:17 +02:00
|
|
|
} else {
|
|
|
|
handle_generic_device_event(uevent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-12 20:44:16 +01:00
|
|
|
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
|
|
|
|
2016-11-12 20:44:16 +01: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
|
|
|
}
|
|
|
|
|
2016-11-12 20:44:16 +01: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
|
|
|
}
|
|
|
|
|
2016-11-12 20:44:16 +01:00
|
|
|
static int is_booting() {
|
2011-03-24 23:45:30 +01:00
|
|
|
return access("/dev/.booting", F_OK) == 0;
|
|
|
|
}
|
|
|
|
|
2016-11-12 20:44:16 +01:00
|
|
|
static void process_firmware_event(uevent* uevent) {
|
2011-03-24 23:45:30 +01:00
|
|
|
int booting = is_booting();
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(INFO) << "firmware: loading '" << uevent->firmware << "' for '" << uevent->path << "'";
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
std::string root = "/sys" + uevent->path;
|
2016-11-12 20:44:16 +01:00
|
|
|
std::string loading = root + "/loading";
|
|
|
|
std::string data = root + "/data";
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2016-11-12 20:44:16 +01: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
|
|
|
|
2016-11-12 20:44:16 +01: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
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
static const char* firmware_dirs[] = {"/etc/firmware/", "/vendor/firmware/",
|
|
|
|
"/firmware/image/"};
|
|
|
|
|
2011-03-24 23:45:30 +01:00
|
|
|
try_loading_again:
|
2016-11-12 20:44:16 +01:00
|
|
|
for (size_t i = 0; i < arraysize(firmware_dirs); i++) {
|
2017-04-11 22:53:37 +02:00
|
|
|
std::string file = firmware_dirs[i] + uevent->firmware;
|
2016-11-12 20:44:16 +01:00
|
|
|
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;
|
2015-03-20 23:10:29 +01:00
|
|
|
}
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2016-11-12 20:44:16 +01:00
|
|
|
if (booting) {
|
|
|
|
// If we're not fully booted, we may be missing
|
|
|
|
// filesystems needed for firmware, wait and retry.
|
2016-11-15 02:08:47 +01:00
|
|
|
std::this_thread::sleep_for(100ms);
|
2016-11-12 20:44:16 +01:00
|
|
|
booting = is_booting();
|
|
|
|
goto try_loading_again;
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2016-11-12 20:44:16 +01:00
|
|
|
LOG(ERROR) << "firmware: could not find firmware for " << uevent->firmware;
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2016-11-12 20:44:16 +01: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
|
|
|
|
2016-11-12 20:44:16 +01:00
|
|
|
static void handle_firmware_event(uevent* uevent) {
|
2017-04-11 22:53:37 +02:00
|
|
|
if (uevent->subsystem != "firmware" || uevent->action != "add") return;
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2016-11-12 20:44:16 +01: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);
|
2016-11-29 20:20:58 +01:00
|
|
|
LOG(INFO) << "loading " << uevent->path << " took " << t;
|
2014-08-21 01:16:44 +02:00
|
|
|
_exit(EXIT_SUCCESS);
|
2016-11-12 20:44:16 +01:00
|
|
|
} else if (pid == -1) {
|
|
|
|
PLOG(ERROR) << "could not fork to process firmware event for " << uevent->firmware;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 03:11:37 +01:00
|
|
|
static bool inline should_stop_coldboot(coldboot_action_t act)
|
|
|
|
{
|
|
|
|
return (act == COLDBOOT_STOP || act == COLDBOOT_FINISH);
|
|
|
|
}
|
|
|
|
|
2014-06-23 20:22:09 +02:00
|
|
|
#define UEVENT_MSG_LEN 2048
|
2016-02-01 08:07:40 +01:00
|
|
|
|
2017-02-09 00:49:47 +01:00
|
|
|
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
|
|
|
{
|
2011-04-25 05:08:17 +02:00
|
|
|
char msg[UEVENT_MSG_LEN+2];
|
|
|
|
int n;
|
2011-05-11 23:58:24 +02:00
|
|
|
while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
|
2010-07-19 23:31:20 +02:00
|
|
|
if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
|
2008-10-21 16:00:00 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
msg[n] = '\0';
|
|
|
|
msg[n+1] = '\0';
|
|
|
|
|
2017-04-11 22:53:37 +02:00
|
|
|
uevent uevent;
|
2008-10-21 16:00:00 +02:00
|
|
|
parse_event(msg, &uevent);
|
2017-02-08 03:11:37 +01:00
|
|
|
coldboot_action_t act = handle_uevent(&uevent);
|
|
|
|
if (should_stop_coldboot(act))
|
|
|
|
return act;
|
2016-02-01 08:07:40 +01:00
|
|
|
}
|
2017-02-08 03:11:37 +01:00
|
|
|
|
|
|
|
return COLDBOOT_CONTINUE;
|
2016-02-01 08:07:40 +01:00
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2017-02-08 03:11:37 +01:00
|
|
|
coldboot_action_t handle_device_fd(coldboot_callback fn)
|
2016-02-01 08:07:40 +01:00
|
|
|
{
|
2017-02-08 03:11:37 +01:00
|
|
|
coldboot_action_t ret = handle_device_fd_with(
|
|
|
|
[&](uevent* uevent) -> coldboot_action_t {
|
2016-02-01 08:07:40 +01:00
|
|
|
if (selinux_status_updated() > 0) {
|
|
|
|
struct selabel_handle *sehandle2;
|
|
|
|
sehandle2 = selinux_android_file_context_handle();
|
|
|
|
if (sehandle2) {
|
|
|
|
selabel_close(sehandle);
|
|
|
|
sehandle = sehandle2;
|
|
|
|
}
|
2013-04-16 15:30:30 +02:00
|
|
|
}
|
|
|
|
|
2017-02-08 03:11:37 +01:00
|
|
|
// 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;
|
2016-02-01 08:07:40 +01:00
|
|
|
});
|
2017-02-08 03:11:37 +01:00
|
|
|
|
|
|
|
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
|
2015-02-04 23:46:36 +01:00
|
|
|
** socket's buffer.
|
2008-10-21 16:00:00 +02:00
|
|
|
*/
|
|
|
|
|
2017-02-08 03:11:37 +01: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;
|
2017-02-08 03:11:37 +01:00
|
|
|
coldboot_action_t act = COLDBOOT_CONTINUE;
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
dfd = dirfd(d);
|
|
|
|
|
|
|
|
fd = openat(dfd, "uevent", O_WRONLY);
|
2017-02-08 03:11:37 +01:00
|
|
|
if (fd >= 0) {
|
2008-10-21 16:00:00 +02:00
|
|
|
write(fd, "add\n", 4);
|
|
|
|
close(fd);
|
2017-02-08 03:11:37 +01:00
|
|
|
act = handle_device_fd(fn);
|
|
|
|
if (should_stop_coldboot(act))
|
|
|
|
return act;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2017-02-08 03:11:37 +01: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 {
|
2017-02-08 03:11:37 +01:00
|
|
|
act = do_coldboot(d2, fn);
|
2008-10-21 16:00:00 +02:00
|
|
|
closedir(d2);
|
|
|
|
}
|
|
|
|
}
|
2017-02-08 03:11:37 +01:00
|
|
|
|
|
|
|
// default is always to continue looking for uevents
|
|
|
|
return act;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2017-02-08 03:11:37 +01:00
|
|
|
static coldboot_action_t coldboot(const char *path, coldboot_callback fn)
|
2008-10-21 16:00:00 +02:00
|
|
|
{
|
2016-02-18 23:52:46 +01:00
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
|
2017-02-08 03:11:37 +01:00
|
|
|
if (d) {
|
|
|
|
return do_coldboot(d.get(), fn);
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
2017-02-08 03:11:37 +01:00
|
|
|
|
|
|
|
return COLDBOOT_CONTINUE;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2017-02-08 03:11:37 +01:00
|
|
|
void device_init(const char* path, coldboot_callback fn) {
|
2017-02-15 22:37:52 +01:00
|
|
|
if (!sehandle) {
|
|
|
|
sehandle = selinux_android_file_context_handle();
|
|
|
|
}
|
|
|
|
// open uevent socket and selinux status only if it hasn't been
|
|
|
|
// done before
|
2015-03-28 19:23:32 +01:00
|
|
|
if (device_fd == -1) {
|
2017-02-15 22:37:52 +01:00
|
|
|
/* 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);
|
2015-03-28 19:23:32 +01:00
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2015-03-28 19:23:32 +01:00
|
|
|
if (access(COLDBOOT_DONE, F_OK) == 0) {
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(VERBOSE) << "Skipping coldboot, already done!";
|
2015-03-28 19:23:32 +01:00
|
|
|
return;
|
2010-04-21 21:04:20 +02:00
|
|
|
}
|
2015-03-28 19:23:32 +01:00
|
|
|
|
|
|
|
Timer t;
|
2017-02-08 03:11:37 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2016-11-29 20:20:58 +01:00
|
|
|
LOG(INFO) << "Coldboot took " << t;
|
2010-04-14 04:25:51 +02:00
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2017-02-09 05:27:12 +01:00
|
|
|
void device_close() {
|
2017-04-06 23:41:30 +02:00
|
|
|
platform_devices.clear();
|
2017-02-09 05:27:12 +01:00
|
|
|
device_fd.reset();
|
2017-02-15 22:37:52 +01:00
|
|
|
selinux_status_close();
|
2017-02-09 05:27:12 +01:00
|
|
|
}
|
|
|
|
|
2016-11-12 20:44:16 +01:00
|
|
|
int get_device_fd() {
|
2010-04-14 04:25:51 +02:00
|
|
|
return device_fd;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|