The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 2 | * Copyright (C) 2007-2014 The Android Open Source Project |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Elliott Hughes | f39f7f1 | 2016-08-31 14:41:51 -0700 | [diff] [blame] | 17 | #include <dirent.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 18 | #include <errno.h> |
Elliott Hughes | f39f7f1 | 2016-08-31 14:41:51 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Daniel Leung | c0c1ffe | 2012-07-02 11:32:30 -0700 | [diff] [blame] | 20 | #include <fnmatch.h> |
Elliott Hughes | f39f7f1 | 2016-08-31 14:41:51 -0700 | [diff] [blame] | 21 | #include <libgen.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 22 | #include <linux/netlink.h> |
Olivier Bailly | b93e581 | 2010-11-17 11:47:23 -0800 | [diff] [blame] | 23 | #include <stddef.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | #include <string.h> |
Elliott Hughes | 632e99a | 2016-11-12 11:44:16 -0800 | [diff] [blame] | 27 | #include <sys/sendfile.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 28 | #include <sys/socket.h> |
Elliott Hughes | f39f7f1 | 2016-08-31 14:41:51 -0700 | [diff] [blame] | 29 | #include <sys/time.h> |
| 30 | #include <sys/types.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 31 | #include <sys/un.h> |
Elliott Hughes | f39f7f1 | 2016-08-31 14:41:51 -0700 | [diff] [blame] | 32 | #include <sys/wait.h> |
| 33 | #include <unistd.h> |
| 34 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 35 | #include <algorithm> |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 36 | #include <memory> |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 37 | #include <string> |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 38 | #include <thread> |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 39 | #include <vector> |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 40 | |
Biao Lu | dc84856 | 2016-01-28 16:10:54 +0800 | [diff] [blame] | 41 | #include <android-base/file.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 42 | #include <android-base/logging.h> |
Rob Herring | 6de783a | 2016-05-06 10:06:59 -0500 | [diff] [blame] | 43 | #include <android-base/stringprintf.h> |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 44 | #include <android-base/strings.h> |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 45 | #include <android-base/unique_fd.h> |
Dima Zavin | da04c52 | 2011-09-01 17:09:44 -0700 | [diff] [blame] | 46 | #include <cutils/list.h> |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 47 | #include <cutils/uevent.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 48 | #include <private/android_filesystem_config.h> |
| 49 | #include <selinux/android.h> |
| 50 | #include <selinux/avc.h> |
| 51 | #include <selinux/label.h> |
| 52 | #include <selinux/selinux.h> |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 53 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | #include "devices.h" |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 55 | #include "ueventd_parser.h" |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 56 | #include "util.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 57 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 58 | #define SYSFS_PREFIX "/sys" |
Daniel Rosenberg | d1d9602 | 2015-03-20 15:10:29 -0700 | [diff] [blame] | 59 | static const char *firmware_dirs[] = { "/etc/firmware", |
| 60 | "/vendor/firmware", |
| 61 | "/firmware/image" }; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 62 | |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 63 | extern struct selabel_handle *sehandle; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 64 | |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 65 | static android::base::unique_fd device_fd; |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 66 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 67 | struct perms_ { |
| 68 | char *name; |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 69 | char *attr; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 70 | mode_t perm; |
| 71 | unsigned int uid; |
| 72 | unsigned int gid; |
| 73 | unsigned short prefix; |
Daniel Leung | c0c1ffe | 2012-07-02 11:32:30 -0700 | [diff] [blame] | 74 | unsigned short wildcard; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 75 | }; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 76 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 77 | struct perm_node { |
| 78 | struct perms_ dp; |
| 79 | struct listnode plist; |
| 80 | }; |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 81 | |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 82 | struct platform_node { |
| 83 | char *name; |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 84 | char *path; |
| 85 | int path_len; |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 86 | struct listnode list; |
| 87 | }; |
| 88 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 89 | static list_declare(sys_perms); |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 90 | static list_declare(dev_perms); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 91 | static list_declare(platform_names); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 92 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 93 | int add_dev_perms(const char *name, const char *attr, |
| 94 | mode_t perm, unsigned int uid, unsigned int gid, |
Daniel Leung | c0c1ffe | 2012-07-02 11:32:30 -0700 | [diff] [blame] | 95 | unsigned short prefix, |
| 96 | unsigned short wildcard) { |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 97 | struct perm_node *node = (perm_node*) calloc(1, sizeof(*node)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 98 | if (!node) |
| 99 | return -ENOMEM; |
| 100 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 101 | node->dp.name = strdup(name); |
Ting-Yuan Huang | 09bd41d | 2016-11-15 15:35:16 -0800 | [diff] [blame] | 102 | if (!node->dp.name) { |
| 103 | free(node); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 104 | return -ENOMEM; |
Ting-Yuan Huang | 09bd41d | 2016-11-15 15:35:16 -0800 | [diff] [blame] | 105 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 106 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 107 | if (attr) { |
| 108 | node->dp.attr = strdup(attr); |
Ting-Yuan Huang | 09bd41d | 2016-11-15 15:35:16 -0800 | [diff] [blame] | 109 | if (!node->dp.attr) { |
| 110 | free(node->dp.name); |
| 111 | free(node); |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 112 | return -ENOMEM; |
Ting-Yuan Huang | 09bd41d | 2016-11-15 15:35:16 -0800 | [diff] [blame] | 113 | } |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 114 | } |
| 115 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 116 | node->dp.perm = perm; |
| 117 | node->dp.uid = uid; |
| 118 | node->dp.gid = gid; |
| 119 | node->dp.prefix = prefix; |
Daniel Leung | c0c1ffe | 2012-07-02 11:32:30 -0700 | [diff] [blame] | 120 | node->dp.wildcard = wildcard; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 121 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 122 | if (attr) |
| 123 | list_add_tail(&sys_perms, &node->plist); |
| 124 | else |
| 125 | list_add_tail(&dev_perms, &node->plist); |
| 126 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
Colin Cross | 43d537e | 2014-07-02 13:08:13 -0700 | [diff] [blame] | 130 | static bool perm_path_matches(const char *path, struct perms_ *dp) |
| 131 | { |
| 132 | if (dp->prefix) { |
| 133 | if (strncmp(path, dp->name, strlen(dp->name)) == 0) |
| 134 | return true; |
| 135 | } else if (dp->wildcard) { |
| 136 | if (fnmatch(dp->name, path, FNM_PATHNAME) == 0) |
| 137 | return true; |
| 138 | } else { |
| 139 | if (strcmp(path, dp->name) == 0) |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | return false; |
| 144 | } |
| 145 | |
Rob Herring | 6de783a | 2016-05-06 10:06:59 -0500 | [diff] [blame] | 146 | static bool match_subsystem(perms_* dp, const char* pattern, |
| 147 | const char* path, const char* subsystem) { |
| 148 | if (!pattern || !subsystem || strstr(dp->name, subsystem) == NULL) { |
| 149 | return false; |
| 150 | } |
Rob Herring | e5636a3 | 2016-05-06 12:28:48 -0500 | [diff] [blame] | 151 | |
Rob Herring | 6de783a | 2016-05-06 10:06:59 -0500 | [diff] [blame] | 152 | std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path)); |
| 153 | return perm_path_matches(subsys_path.c_str(), dp); |
| 154 | } |
Rob Herring | e5636a3 | 2016-05-06 12:28:48 -0500 | [diff] [blame] | 155 | |
Rob Herring | 6de783a | 2016-05-06 10:06:59 -0500 | [diff] [blame] | 156 | static void fixup_sys_perms(const char* upath, const char* subsystem) { |
| 157 | // upaths omit the "/sys" that paths in this list |
| 158 | // contain, so we prepend it... |
| 159 | std::string path = std::string(SYSFS_PREFIX) + upath; |
| 160 | |
| 161 | listnode* node; |
Rob Herring | e5636a3 | 2016-05-06 12:28:48 -0500 | [diff] [blame] | 162 | list_for_each(node, &sys_perms) { |
Rob Herring | 6de783a | 2016-05-06 10:06:59 -0500 | [diff] [blame] | 163 | perms_* dp = &(node_to_item(node, perm_node, plist))->dp; |
| 164 | if (match_subsystem(dp, SYSFS_PREFIX "/class/%s/%s", path.c_str(), subsystem)) { |
| 165 | ; // matched |
| 166 | } else if (match_subsystem(dp, SYSFS_PREFIX "/bus/%s/devices/%s", path.c_str(), subsystem)) { |
| 167 | ; // matched |
| 168 | } else if (!perm_path_matches(path.c_str(), dp)) { |
| 169 | continue; |
Rob Herring | e5636a3 | 2016-05-06 12:28:48 -0500 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | std::string attr_file = path + "/" + dp->attr; |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 173 | LOG(INFO) << "fixup " << attr_file |
| 174 | << " " << dp->uid << " " << dp->gid << " " << std::oct << dp->perm; |
Rob Herring | e5636a3 | 2016-05-06 12:28:48 -0500 | [diff] [blame] | 175 | chown(attr_file.c_str(), dp->uid, dp->gid); |
| 176 | chmod(attr_file.c_str(), dp->perm); |
| 177 | } |
| 178 | |
| 179 | if (access(path.c_str(), F_OK) == 0) { |
Dmitry Shmidt | 7eed474 | 2016-07-28 13:55:39 -0700 | [diff] [blame] | 180 | LOG(VERBOSE) << "restorecon_recursive: " << path; |
Paul Lawrence | a8d8434 | 2016-11-14 15:40:18 -0800 | [diff] [blame] | 181 | restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE); |
Rob Herring | e5636a3 | 2016-05-06 12:28:48 -0500 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 185 | static mode_t get_device_perm(const char* path, const std::vector<std::string>& links, |
| 186 | unsigned* uid, unsigned* gid) { |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 187 | struct listnode *node; |
| 188 | struct perm_node *perm_node; |
| 189 | struct perms_ *dp; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 190 | |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 191 | /* search the perms list in reverse so that ueventd.$hardware can |
| 192 | * override ueventd.rc |
| 193 | */ |
| 194 | list_for_each_reverse(node, &dev_perms) { |
| 195 | perm_node = node_to_item(node, struct perm_node, plist); |
| 196 | dp = &perm_node->dp; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 197 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 198 | if (perm_path_matches(path, dp) || |
| 199 | std::any_of(links.begin(), links.end(), |
| 200 | [dp](const auto& link) { return perm_path_matches(link.c_str(), dp); })) { |
Colin Cross | 43d537e | 2014-07-02 13:08:13 -0700 | [diff] [blame] | 201 | *uid = dp->uid; |
| 202 | *gid = dp->gid; |
| 203 | return dp->perm; |
| 204 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 205 | } |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 206 | /* Default if nothing found. */ |
| 207 | *uid = 0; |
| 208 | *gid = 0; |
| 209 | return 0600; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 212 | static void make_device(const char* path, const char* /*upath*/, int block, int major, int minor, |
| 213 | const std::vector<std::string>& links) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 214 | unsigned uid; |
| 215 | unsigned gid; |
| 216 | mode_t mode; |
| 217 | dev_t dev; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 218 | char *secontext = NULL; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 219 | |
Colin Cross | 43d537e | 2014-07-02 13:08:13 -0700 | [diff] [blame] | 220 | mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR); |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 221 | |
Sandeep Patil | ea23983 | 2017-02-03 07:51:55 -0800 | [diff] [blame] | 222 | if (sehandle) { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 223 | std::vector<const char*> c_links; |
| 224 | for (const auto& link : links) { |
| 225 | c_links.emplace_back(link.c_str()); |
| 226 | } |
| 227 | c_links.emplace_back(nullptr); |
| 228 | if (selabel_lookup_best_match(sehandle, &secontext, path, &c_links[0], mode)) { |
Sandeep Patil | ea23983 | 2017-02-03 07:51:55 -0800 | [diff] [blame] | 229 | PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label"; |
| 230 | return; |
| 231 | } |
| 232 | setfscreatecon(secontext); |
Mihai Serban | 24a3cbf | 2016-04-25 18:22:27 +0300 | [diff] [blame] | 233 | } |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 234 | |
Colin Cross | 17dcc5c | 2010-09-03 12:25:34 -0700 | [diff] [blame] | 235 | dev = makedev(major, minor); |
Nick Pelly | 6405c69 | 2010-01-21 18:13:39 -0800 | [diff] [blame] | 236 | /* Temporarily change egid to avoid race condition setting the gid of the |
| 237 | * device node. Unforunately changing the euid would prevent creation of |
| 238 | * some device nodes, so the uid has to be set with chown() and is still |
| 239 | * racy. Fixing the gid race at least fixed the issue with system_server |
| 240 | * opening dynamic input devices under the AID_INPUT gid. */ |
Tom Cherry | 0506b18 | 2017-02-23 13:46:09 -0800 | [diff] [blame] | 241 | if (setegid(gid)) { |
| 242 | PLOG(ERROR) << "setegid(" << gid << ") for " << path << " device failed"; |
| 243 | goto out; |
| 244 | } |
Mihai Serban | 24a3cbf | 2016-04-25 18:22:27 +0300 | [diff] [blame] | 245 | /* If the node already exists update its SELinux label to handle cases when |
| 246 | * it was created with the wrong context during coldboot procedure. */ |
Sandeep Patil | ea23983 | 2017-02-03 07:51:55 -0800 | [diff] [blame] | 247 | if (mknod(path, mode, dev) && (errno == EEXIST) && secontext) { |
William Roberts | 397de14 | 2016-06-02 09:53:44 -0700 | [diff] [blame] | 248 | |
| 249 | char* fcon = nullptr; |
| 250 | int rc = lgetfilecon(path, &fcon); |
| 251 | if (rc < 0) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 252 | PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device"; |
William Roberts | 397de14 | 2016-06-02 09:53:44 -0700 | [diff] [blame] | 253 | goto out; |
| 254 | } |
| 255 | |
| 256 | bool different = strcmp(fcon, secontext) != 0; |
| 257 | freecon(fcon); |
| 258 | |
| 259 | if (different && lsetfilecon(path, secontext)) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 260 | PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path << "' device"; |
Mihai Serban | 24a3cbf | 2016-04-25 18:22:27 +0300 | [diff] [blame] | 261 | } |
| 262 | } |
William Roberts | 397de14 | 2016-06-02 09:53:44 -0700 | [diff] [blame] | 263 | |
| 264 | out: |
Nick Pelly | 6405c69 | 2010-01-21 18:13:39 -0800 | [diff] [blame] | 265 | chown(path, uid, -1); |
Tom Cherry | 0506b18 | 2017-02-23 13:46:09 -0800 | [diff] [blame] | 266 | if (setegid(AID_ROOT)) { |
| 267 | PLOG(FATAL) << "setegid(AID_ROOT) failed"; |
| 268 | } |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 269 | |
Sandeep Patil | ea23983 | 2017-02-03 07:51:55 -0800 | [diff] [blame] | 270 | if (secontext) { |
| 271 | freecon(secontext); |
| 272 | setfscreatecon(NULL); |
| 273 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 276 | void add_platform_device(const char* path) { |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 277 | int path_len = strlen(path); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 278 | struct platform_node *bus; |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 279 | const char *name = path; |
| 280 | |
| 281 | if (!strncmp(path, "/devices/", 9)) { |
| 282 | name += 9; |
| 283 | if (!strncmp(name, "platform/", 9)) |
| 284 | name += 9; |
| 285 | } |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 286 | |
Wei Wang | a285dac | 2016-10-04 14:05:39 -0700 | [diff] [blame] | 287 | LOG(VERBOSE) << "adding platform device " << name << " (" << path << ")"; |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 288 | |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 289 | bus = (platform_node*) calloc(1, sizeof(struct platform_node)); |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 290 | bus->path = strdup(path); |
| 291 | bus->path_len = path_len; |
| 292 | bus->name = bus->path + (name - path); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 293 | list_add_tail(&platform_names, &bus->list); |
| 294 | } |
| 295 | |
| 296 | /* |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 297 | * given a path that may start with a platform device, find the length of the |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 298 | * platform device prefix. If it doesn't start with a platform device, return |
| 299 | * 0. |
| 300 | */ |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 301 | static struct platform_node *find_platform_device(const char *path) |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 302 | { |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 303 | int path_len = strlen(path); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 304 | struct listnode *node; |
| 305 | struct platform_node *bus; |
| 306 | |
| 307 | list_for_each_reverse(node, &platform_names) { |
| 308 | bus = node_to_item(node, struct platform_node, list); |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 309 | if ((bus->path_len < path_len) && |
| 310 | (path[bus->path_len] == '/') && |
| 311 | !strncmp(path, bus->path, bus->path_len)) |
| 312 | return bus; |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | return NULL; |
| 316 | } |
| 317 | |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 318 | void remove_platform_device(const char* path) { |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 319 | struct listnode *node; |
| 320 | struct platform_node *bus; |
| 321 | |
| 322 | list_for_each_reverse(node, &platform_names) { |
| 323 | bus = node_to_item(node, struct platform_node, list); |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 324 | if (!strcmp(path, bus->path)) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 325 | LOG(INFO) << "removing platform device " << bus->name; |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 326 | free(bus->path); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 327 | list_remove(node); |
| 328 | free(bus); |
| 329 | return; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 334 | static void destroy_platform_devices() { |
| 335 | struct listnode* node; |
| 336 | struct listnode* n; |
| 337 | struct platform_node* bus; |
| 338 | |
| 339 | list_for_each_safe(node, n, &platform_names) { |
| 340 | list_remove(node); |
| 341 | bus = node_to_item(node, struct platform_node, list); |
| 342 | free(bus->path); |
| 343 | free(bus); |
| 344 | } |
| 345 | } |
| 346 | |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 347 | /* Given a path that may start with a PCI device, populate the supplied buffer |
| 348 | * with the PCI domain/bus number and the peripheral ID and return 0. |
| 349 | * If it doesn't start with a PCI device, or there is some error, return -1 */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 350 | static bool find_pci_device_prefix(const std::string& path, std::string* result) { |
| 351 | result->clear(); |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 352 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 353 | if (!android::base::StartsWith(path, "/devices/pci")) return false; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 354 | |
| 355 | /* Beginning of the prefix is the initial "pci" after "/devices/" */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 356 | std::string::size_type start = 9; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 357 | |
| 358 | /* End of the prefix is two path '/' later, capturing the domain/bus number |
| 359 | * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 360 | auto end = path.find('/', start); |
| 361 | if (end == std::string::npos) return false; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 362 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 363 | end = path.find('/', end + 1); |
| 364 | if (end == std::string::npos) return false; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 365 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 366 | auto length = end - start; |
| 367 | if (length <= 4) { |
| 368 | // The minimum string that will get to this check is 'pci/', which is malformed, |
| 369 | // so return false |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | *result = path.substr(start, length); |
| 374 | return true; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 377 | /* Given a path that may start with a virtual block device, populate |
| 378 | * the supplied buffer with the virtual block device ID and return 0. |
| 379 | * If it doesn't start with a virtual block device, or there is some |
| 380 | * error, return -1 */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 381 | static bool find_vbd_device_prefix(const std::string& path, std::string* result) { |
| 382 | result->clear(); |
| 383 | |
| 384 | if (!android::base::StartsWith(path, "/devices/vbd-")) return false; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 385 | |
| 386 | /* Beginning of the prefix is the initial "vbd-" after "/devices/" */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 387 | std::string::size_type start = 13; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 388 | |
| 389 | /* End of the prefix is one path '/' later, capturing the |
| 390 | virtual block device ID. Example: 768 */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 391 | auto end = path.find('/', start); |
| 392 | if (end == std::string::npos) return false; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 393 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 394 | auto length = end - start; |
| 395 | if (length == 0) return false; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 396 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 397 | *result = path.substr(start, length); |
| 398 | return true; |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 399 | } |
| 400 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 401 | static void parse_event(const char *msg, struct uevent *uevent) |
| 402 | { |
| 403 | uevent->action = ""; |
| 404 | uevent->path = ""; |
| 405 | uevent->subsystem = ""; |
| 406 | uevent->firmware = ""; |
| 407 | uevent->major = -1; |
| 408 | uevent->minor = -1; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 409 | uevent->partition_name = NULL; |
| 410 | uevent->partition_num = -1; |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 411 | uevent->device_name = NULL; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 412 | |
| 413 | /* currently ignoring SEQNUM */ |
| 414 | while(*msg) { |
| 415 | if(!strncmp(msg, "ACTION=", 7)) { |
| 416 | msg += 7; |
| 417 | uevent->action = msg; |
| 418 | } else if(!strncmp(msg, "DEVPATH=", 8)) { |
| 419 | msg += 8; |
| 420 | uevent->path = msg; |
| 421 | } else if(!strncmp(msg, "SUBSYSTEM=", 10)) { |
| 422 | msg += 10; |
| 423 | uevent->subsystem = msg; |
| 424 | } else if(!strncmp(msg, "FIRMWARE=", 9)) { |
| 425 | msg += 9; |
| 426 | uevent->firmware = msg; |
| 427 | } else if(!strncmp(msg, "MAJOR=", 6)) { |
| 428 | msg += 6; |
| 429 | uevent->major = atoi(msg); |
| 430 | } else if(!strncmp(msg, "MINOR=", 6)) { |
| 431 | msg += 6; |
| 432 | uevent->minor = atoi(msg); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 433 | } else if(!strncmp(msg, "PARTN=", 6)) { |
| 434 | msg += 6; |
| 435 | uevent->partition_num = atoi(msg); |
| 436 | } else if(!strncmp(msg, "PARTNAME=", 9)) { |
| 437 | msg += 9; |
| 438 | uevent->partition_name = msg; |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 439 | } else if(!strncmp(msg, "DEVNAME=", 8)) { |
| 440 | msg += 8; |
| 441 | uevent->device_name = msg; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 444 | /* advance to after the next \0 */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 445 | while(*msg++) |
| 446 | ; |
| 447 | } |
| 448 | |
Elliott Hughes | c0e919c | 2015-02-04 14:46:36 -0800 | [diff] [blame] | 449 | if (LOG_UEVENTS) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 450 | LOG(INFO) << android::base::StringPrintf("event { '%s', '%s', '%s', '%s', %d, %d }", |
| 451 | uevent->action, uevent->path, uevent->subsystem, |
| 452 | uevent->firmware, uevent->major, uevent->minor); |
Elliott Hughes | c0e919c | 2015-02-04 14:46:36 -0800 | [diff] [blame] | 453 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 456 | std::vector<std::string> get_character_device_symlinks(uevent* uevent) { |
| 457 | platform_node* pdev = find_platform_device(uevent->path); |
| 458 | if (!pdev) return {}; |
Benoit Goby | d227863 | 2010-08-03 14:36:02 -0700 | [diff] [blame] | 459 | |
| 460 | /* skip "/devices/platform/<driver>" */ |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 461 | std::string parent = std::string(uevent->path); |
| 462 | auto parent_start = parent.find('/', pdev->path_len); |
| 463 | if (parent_start == std::string::npos) return {}; |
Benoit Goby | d227863 | 2010-08-03 14:36:02 -0700 | [diff] [blame] | 464 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 465 | parent.erase(0, parent_start); |
Benoit Goby | d227863 | 2010-08-03 14:36:02 -0700 | [diff] [blame] | 466 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 467 | if (!android::base::StartsWith(parent, "/usb")) return {}; |
| 468 | |
| 469 | // skip root hub name and device. use device interface |
| 470 | // skip 3 slashes, including the first / by starting the search at the 1st character, not 0th. |
| 471 | // then extract what comes between the 3rd and 4th slash |
| 472 | // e.g. "/usb/usb_device/name/tty2-1:1.0" -> "name" |
| 473 | |
| 474 | std::string::size_type start = 0; |
| 475 | start = parent.find('/', start + 1); |
| 476 | if (start == std::string::npos) return {}; |
| 477 | |
| 478 | start = parent.find('/', start + 1); |
| 479 | if (start == std::string::npos) return {}; |
| 480 | |
| 481 | auto end = parent.find('/', start + 1); |
| 482 | if (end == std::string::npos) return {}; |
| 483 | |
| 484 | start++; // Skip the first '/' |
| 485 | |
| 486 | auto length = end - start; |
| 487 | if (length == 0) return {}; |
| 488 | |
| 489 | auto name_string = parent.substr(start, length); |
| 490 | |
| 491 | // TODO: remove std::string() when uevent->subsystem is an std::string |
| 492 | std::vector<std::string> links; |
| 493 | links.emplace_back("/dev/usb/" + std::string(uevent->subsystem) + name_string); |
| 494 | |
| 495 | mkdir("/dev/usb", 0755); |
Benoit Goby | d227863 | 2010-08-03 14:36:02 -0700 | [diff] [blame] | 496 | |
| 497 | return links; |
Benoit Goby | d227863 | 2010-08-03 14:36:02 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 500 | // replaces any unacceptable characters with '_', the |
| 501 | // length of the resulting string is equal to the input string |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 502 | void sanitize_partition_name(std::string* string) { |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 503 | const char* accept = |
| 504 | "abcdefghijklmnopqrstuvwxyz" |
| 505 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 506 | "0123456789" |
| 507 | "_-."; |
| 508 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 509 | if (!string) return; |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 510 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 511 | std::string::size_type pos = 0; |
| 512 | while ((pos = string->find_first_not_of(accept, pos)) != std::string::npos) { |
| 513 | (*string)[pos] = '_'; |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 517 | std::vector<std::string> get_block_device_symlinks(uevent* uevent) { |
| 518 | std::string device; |
| 519 | struct platform_node* pdev; |
| 520 | std::string type; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 521 | |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 522 | pdev = find_platform_device(uevent->path); |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 523 | if (pdev) { |
| 524 | device = pdev->name; |
| 525 | type = "platform"; |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 526 | } else if (find_pci_device_prefix(uevent->path, &device)) { |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 527 | type = "pci"; |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 528 | } else if (find_vbd_device_prefix(uevent->path, &device)) { |
Jeremy Compostella | 937309d | 2017-03-03 16:27:29 +0100 | [diff] [blame] | 529 | type = "vbd"; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 530 | } else { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 531 | return {}; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 532 | } |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 533 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 534 | std::vector<std::string> links; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 535 | |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 536 | LOG(VERBOSE) << "found " << type << " device " << device; |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 537 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 538 | auto link_path = "/dev/block/" + type + "/" + device; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 539 | |
| 540 | if (uevent->partition_name) { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 541 | std::string partition_name_sanitized(uevent->partition_name); |
| 542 | sanitize_partition_name(&partition_name_sanitized); |
| 543 | if (partition_name_sanitized != uevent->partition_name) { |
| 544 | LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '" |
| 545 | << partition_name_sanitized << "'"; |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 546 | } |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 547 | links.emplace_back(link_path + "/by-name/" + partition_name_sanitized); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | if (uevent->partition_num >= 0) { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 551 | links.emplace_back(link_path + "/by-num/p" + std::to_string(uevent->partition_num)); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 554 | // TODO: remove uevent_path when uevent->path is an std::string |
| 555 | std::string uevent_path = uevent->path; |
| 556 | auto last_slash = uevent_path.rfind('/'); |
| 557 | links.emplace_back(link_path + "/" + uevent_path.substr(last_slash + 1)); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 558 | |
| 559 | return links; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Elliott Hughes | f39f7f1 | 2016-08-31 14:41:51 -0700 | [diff] [blame] | 562 | static void make_link_init(const char* oldpath, const char* newpath) { |
| 563 | const char* slash = strrchr(newpath, '/'); |
| 564 | if (!slash) return; |
| 565 | |
| 566 | if (mkdir_recursive(dirname(newpath), 0755)) { |
| 567 | PLOG(ERROR) << "Failed to create directory " << dirname(newpath); |
| 568 | } |
| 569 | |
| 570 | if (symlink(oldpath, newpath) && errno != EEXIST) { |
| 571 | PLOG(ERROR) << "Failed to symlink " << oldpath << " to " << newpath; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | static void remove_link(const char* oldpath, const char* newpath) { |
| 576 | std::string path; |
| 577 | if (android::base::Readlink(newpath, &path) && path == oldpath) unlink(newpath); |
| 578 | } |
| 579 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 580 | static void handle_device(const char* action, const char* devpath, const char* path, int block, |
| 581 | int major, int minor, const std::vector<std::string>& links) { |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 582 | if(!strcmp(action, "add")) { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 583 | make_device(devpath, path, block, major, minor, links); |
| 584 | for (const auto& link : links) { |
| 585 | make_link_init(devpath, link.c_str()); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 586 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 589 | if(!strcmp(action, "remove")) { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 590 | for (const auto& link : links) { |
| 591 | remove_link(devpath, link.c_str()); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 592 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 593 | unlink(devpath); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 594 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 597 | static void handle_platform_device_event(struct uevent *uevent) |
| 598 | { |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 599 | const char *path = uevent->path; |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 600 | |
| 601 | if (!strcmp(uevent->action, "add")) |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 602 | add_platform_device(path); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 603 | else if (!strcmp(uevent->action, "remove")) |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 604 | remove_platform_device(path); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 607 | static const char *parse_device_name(struct uevent *uevent, unsigned int len) |
| 608 | { |
| 609 | const char *name; |
| 610 | |
| 611 | /* if it's not a /dev device, nothing else to do */ |
| 612 | if((uevent->major < 0) || (uevent->minor < 0)) |
| 613 | return NULL; |
| 614 | |
| 615 | /* do we have a name? */ |
| 616 | name = strrchr(uevent->path, '/'); |
| 617 | if(!name) |
| 618 | return NULL; |
| 619 | name++; |
| 620 | |
| 621 | /* too-long names would overrun our buffer */ |
Greg Hackmann | f6e009e | 2013-11-21 13:26:48 -0800 | [diff] [blame] | 622 | if(strlen(name) > len) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 623 | LOG(ERROR) << "DEVPATH=" << name << " exceeds " << len << "-character limit on filename; ignoring event"; |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 624 | return NULL; |
Greg Hackmann | f6e009e | 2013-11-21 13:26:48 -0800 | [diff] [blame] | 625 | } |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 626 | |
| 627 | return name; |
| 628 | } |
| 629 | |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 630 | #define DEVPATH_LEN 96 |
| 631 | #define MAX_DEV_NAME 64 |
| 632 | |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 633 | static void handle_block_device_event(struct uevent *uevent) |
| 634 | { |
| 635 | const char *base = "/dev/block/"; |
| 636 | const char *name; |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 637 | char devpath[DEVPATH_LEN]; |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 638 | |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 639 | name = parse_device_name(uevent, MAX_DEV_NAME); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 640 | if (!name) |
| 641 | return; |
| 642 | |
| 643 | snprintf(devpath, sizeof(devpath), "%s%s", base, name); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 644 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 645 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 646 | std::vector<std::string> links; |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 647 | if (!strncmp(uevent->path, "/devices/", 9)) |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 648 | links = get_block_device_symlinks(uevent); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 649 | |
| 650 | handle_device(uevent->action, devpath, uevent->path, 1, |
| 651 | uevent->major, uevent->minor, links); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 654 | static bool assemble_devpath(char *devpath, const char *dirname, |
| 655 | const char *devname) |
| 656 | { |
| 657 | int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname); |
| 658 | if (s < 0) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 659 | PLOG(ERROR) << "failed to assemble device path; ignoring event"; |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 660 | return false; |
| 661 | } else if (s >= DEVPATH_LEN) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 662 | LOG(ERROR) << dirname << "/" << devname |
| 663 | << " exceeds " << DEVPATH_LEN << "-character limit on path; ignoring event"; |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 664 | return false; |
| 665 | } |
| 666 | return true; |
| 667 | } |
| 668 | |
| 669 | static void mkdir_recursive_for_devpath(const char *devpath) |
| 670 | { |
| 671 | char dir[DEVPATH_LEN]; |
| 672 | char *slash; |
| 673 | |
| 674 | strcpy(dir, devpath); |
| 675 | slash = strrchr(dir, '/'); |
| 676 | *slash = '\0'; |
| 677 | mkdir_recursive(dir, 0755); |
| 678 | } |
| 679 | |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 680 | static void handle_generic_device_event(struct uevent *uevent) |
| 681 | { |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 682 | const char *name; |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 683 | char devpath[DEVPATH_LEN] = {0}; |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 684 | |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 685 | name = parse_device_name(uevent, MAX_DEV_NAME); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 686 | if (!name) |
| 687 | return; |
| 688 | |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 689 | struct ueventd_subsystem *subsystem = |
| 690 | ueventd_subsystem_find_by_name(uevent->subsystem); |
| 691 | |
| 692 | if (subsystem) { |
| 693 | const char *devname; |
| 694 | |
| 695 | switch (subsystem->devname_src) { |
| 696 | case DEVNAME_UEVENT_DEVNAME: |
| 697 | devname = uevent->device_name; |
| 698 | break; |
| 699 | |
| 700 | case DEVNAME_UEVENT_DEVPATH: |
| 701 | devname = name; |
| 702 | break; |
| 703 | |
| 704 | default: |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 705 | LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event"; |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 706 | return; |
| 707 | } |
| 708 | |
| 709 | if (!assemble_devpath(devpath, subsystem->dirname, devname)) |
| 710 | return; |
| 711 | mkdir_recursive_for_devpath(devpath); |
| 712 | } else if (!strncmp(uevent->subsystem, "usb", 3)) { |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 713 | if (!strcmp(uevent->subsystem, "usb")) { |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 714 | if (uevent->device_name) { |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 715 | if (!assemble_devpath(devpath, "/dev", uevent->device_name)) |
Greg Hackmann | f6e009e | 2013-11-21 13:26:48 -0800 | [diff] [blame] | 716 | return; |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 717 | mkdir_recursive_for_devpath(devpath); |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 718 | } |
| 719 | else { |
| 720 | /* This imitates the file system that would be created |
| 721 | * if we were using devfs instead. |
| 722 | * Minors are broken up into groups of 128, starting at "001" |
| 723 | */ |
| 724 | int bus_id = uevent->minor / 128 + 1; |
| 725 | int device_id = uevent->minor % 128 + 1; |
| 726 | /* build directories */ |
| 727 | make_dir("/dev/bus", 0755); |
| 728 | make_dir("/dev/bus/usb", 0755); |
| 729 | snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id); |
| 730 | make_dir(devpath, 0755); |
| 731 | snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id); |
| 732 | } |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 733 | } else { |
| 734 | /* ignore other USB events */ |
| 735 | return; |
| 736 | } |
Tom Cherry | 780a71e | 2017-04-04 16:30:40 -0700 | [diff] [blame^] | 737 | } else { |
| 738 | snprintf(devpath, sizeof(devpath), "/dev/%s", name); |
| 739 | } |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 740 | |
Tom Cherry | 780a71e | 2017-04-04 16:30:40 -0700 | [diff] [blame^] | 741 | auto links = get_character_device_symlinks(uevent); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 742 | |
Tom Cherry | 780a71e | 2017-04-04 16:30:40 -0700 | [diff] [blame^] | 743 | handle_device(uevent->action, devpath, uevent->path, 0, uevent->major, uevent->minor, links); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | static void handle_device_event(struct uevent *uevent) |
| 747 | { |
Ruchi Kandoi | 75b287b | 2014-04-29 19:14:37 -0700 | [diff] [blame] | 748 | if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online")) |
Rob Herring | 6de783a | 2016-05-06 10:06:59 -0500 | [diff] [blame] | 749 | fixup_sys_perms(uevent->path, uevent->subsystem); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 750 | |
| 751 | if (!strncmp(uevent->subsystem, "block", 5)) { |
| 752 | handle_block_device_event(uevent); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 753 | } else if (!strncmp(uevent->subsystem, "platform", 8)) { |
| 754 | handle_platform_device_event(uevent); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 755 | } else { |
| 756 | handle_generic_device_event(uevent); |
| 757 | } |
| 758 | } |
| 759 | |
Elliott Hughes | 632e99a | 2016-11-12 11:44:16 -0800 | [diff] [blame] | 760 | static void load_firmware(uevent* uevent, const std::string& root, |
| 761 | int fw_fd, size_t fw_size, |
| 762 | int loading_fd, int data_fd) { |
| 763 | // Start transfer. |
| 764 | android::base::WriteFully(loading_fd, "1", 1); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 765 | |
Elliott Hughes | 632e99a | 2016-11-12 11:44:16 -0800 | [diff] [blame] | 766 | // Copy the firmware. |
| 767 | int rc = sendfile(data_fd, fw_fd, nullptr, fw_size); |
| 768 | if (rc == -1) { |
| 769 | PLOG(ERROR) << "firmware: sendfile failed { '" << root << "', '" << uevent->firmware << "' }"; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Elliott Hughes | 632e99a | 2016-11-12 11:44:16 -0800 | [diff] [blame] | 772 | // Tell the firmware whether to abort or commit. |
| 773 | const char* response = (rc != -1) ? "0" : "-1"; |
| 774 | android::base::WriteFully(loading_fd, response, strlen(response)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Elliott Hughes | 632e99a | 2016-11-12 11:44:16 -0800 | [diff] [blame] | 777 | static int is_booting() { |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 778 | return access("/dev/.booting", F_OK) == 0; |
| 779 | } |
| 780 | |
Elliott Hughes | 632e99a | 2016-11-12 11:44:16 -0800 | [diff] [blame] | 781 | static void process_firmware_event(uevent* uevent) { |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 782 | int booting = is_booting(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 783 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 784 | LOG(INFO) << "firmware: loading '" << uevent->firmware << "' for '" << uevent->path << "'"; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 785 | |
Elliott Hughes | 632e99a | 2016-11-12 11:44:16 -0800 | [diff] [blame] | 786 | std::string root = android::base::StringPrintf("/sys%s", uevent->path); |
| 787 | std::string loading = root + "/loading"; |
| 788 | std::string data = root + "/data"; |
| 789 | |
| 790 | android::base::unique_fd loading_fd(open(loading.c_str(), O_WRONLY|O_CLOEXEC)); |
| 791 | if (loading_fd == -1) { |
| 792 | PLOG(ERROR) << "couldn't open firmware loading fd for " << uevent->firmware; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 793 | return; |
Elliott Hughes | 632e99a | 2016-11-12 11:44:16 -0800 | [diff] [blame] | 794 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 795 | |
Elliott Hughes | 632e99a | 2016-11-12 11:44:16 -0800 | [diff] [blame] | 796 | android::base::unique_fd data_fd(open(data.c_str(), O_WRONLY|O_CLOEXEC)); |
| 797 | if (data_fd == -1) { |
| 798 | PLOG(ERROR) << "couldn't open firmware data fd for " << uevent->firmware; |
| 799 | return; |
| 800 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 801 | |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 802 | try_loading_again: |
Elliott Hughes | 632e99a | 2016-11-12 11:44:16 -0800 | [diff] [blame] | 803 | for (size_t i = 0; i < arraysize(firmware_dirs); i++) { |
| 804 | std::string file = android::base::StringPrintf("%s/%s", firmware_dirs[i], uevent->firmware); |
| 805 | android::base::unique_fd fw_fd(open(file.c_str(), O_RDONLY|O_CLOEXEC)); |
| 806 | struct stat sb; |
| 807 | if (fw_fd != -1 && fstat(fw_fd, &sb) != -1) { |
| 808 | load_firmware(uevent, root, fw_fd, sb.st_size, loading_fd, data_fd); |
| 809 | return; |
Benoit Goby | 609d882 | 2010-11-09 18:10:24 -0800 | [diff] [blame] | 810 | } |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 811 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 812 | |
Elliott Hughes | 632e99a | 2016-11-12 11:44:16 -0800 | [diff] [blame] | 813 | if (booting) { |
| 814 | // If we're not fully booted, we may be missing |
| 815 | // filesystems needed for firmware, wait and retry. |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 816 | std::this_thread::sleep_for(100ms); |
Elliott Hughes | 632e99a | 2016-11-12 11:44:16 -0800 | [diff] [blame] | 817 | booting = is_booting(); |
| 818 | goto try_loading_again; |
| 819 | } |
| 820 | |
| 821 | LOG(ERROR) << "firmware: could not find firmware for " << uevent->firmware; |
| 822 | |
| 823 | // Write "-1" as our response to the kernel's firmware request, since we have nothing for it. |
| 824 | write(loading_fd, "-1", 2); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Elliott Hughes | 632e99a | 2016-11-12 11:44:16 -0800 | [diff] [blame] | 827 | static void handle_firmware_event(uevent* uevent) { |
| 828 | if (strcmp(uevent->subsystem, "firmware")) return; |
| 829 | if (strcmp(uevent->action, "add")) return; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 830 | |
Elliott Hughes | 632e99a | 2016-11-12 11:44:16 -0800 | [diff] [blame] | 831 | // Loading the firmware in a child means we can do that in parallel... |
| 832 | // (We ignore SIGCHLD rather than wait for our children.) |
| 833 | pid_t pid = fork(); |
| 834 | if (pid == 0) { |
| 835 | Timer t; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 836 | process_firmware_event(uevent); |
Elliott Hughes | 331cf2f | 2016-11-29 19:20:58 +0000 | [diff] [blame] | 837 | LOG(INFO) << "loading " << uevent->path << " took " << t; |
Kenny Root | 17baff4 | 2014-08-20 16:16:44 -0700 | [diff] [blame] | 838 | _exit(EXIT_SUCCESS); |
Elliott Hughes | 632e99a | 2016-11-12 11:44:16 -0800 | [diff] [blame] | 839 | } else if (pid == -1) { |
| 840 | PLOG(ERROR) << "could not fork to process firmware event for " << uevent->firmware; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 844 | static bool inline should_stop_coldboot(coldboot_action_t act) |
| 845 | { |
| 846 | return (act == COLDBOOT_STOP || act == COLDBOOT_FINISH); |
| 847 | } |
| 848 | |
Ruchi Kandoi | c603720 | 2014-06-23 11:22:09 -0700 | [diff] [blame] | 849 | #define UEVENT_MSG_LEN 2048 |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 850 | |
Sandeep Patil | 44a3ee2 | 2017-02-08 15:49:47 -0800 | [diff] [blame] | 851 | static inline coldboot_action_t handle_device_fd_with( |
| 852 | std::function<coldboot_action_t(uevent* uevent)> handle_uevent) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 853 | { |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 854 | char msg[UEVENT_MSG_LEN+2]; |
| 855 | int n; |
Nick Kralevich | 57de8b8 | 2011-05-11 14:58:24 -0700 | [diff] [blame] | 856 | while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) { |
Nick Kralevich | 5f5d5c8 | 2010-07-19 14:31:20 -0700 | [diff] [blame] | 857 | if(n >= UEVENT_MSG_LEN) /* overflow -- discard */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 858 | continue; |
| 859 | |
| 860 | msg[n] = '\0'; |
| 861 | msg[n+1] = '\0'; |
| 862 | |
Nick Kralevich | 5f5d5c8 | 2010-07-19 14:31:20 -0700 | [diff] [blame] | 863 | struct uevent uevent; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 864 | parse_event(msg, &uevent); |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 865 | coldboot_action_t act = handle_uevent(&uevent); |
| 866 | if (should_stop_coldboot(act)) |
| 867 | return act; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 868 | } |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 869 | |
| 870 | return COLDBOOT_CONTINUE; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 873 | coldboot_action_t handle_device_fd(coldboot_callback fn) |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 874 | { |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 875 | coldboot_action_t ret = handle_device_fd_with( |
| 876 | [&](uevent* uevent) -> coldboot_action_t { |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 877 | if (selinux_status_updated() > 0) { |
| 878 | struct selabel_handle *sehandle2; |
| 879 | sehandle2 = selinux_android_file_context_handle(); |
| 880 | if (sehandle2) { |
| 881 | selabel_close(sehandle); |
| 882 | sehandle = sehandle2; |
| 883 | } |
| 884 | } |
| 885 | |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 886 | // default is to always create the devices |
| 887 | coldboot_action_t act = COLDBOOT_CREATE; |
| 888 | if (fn) { |
| 889 | act = fn(uevent); |
| 890 | } |
| 891 | |
| 892 | if (act == COLDBOOT_CREATE || act == COLDBOOT_STOP) { |
| 893 | handle_device_event(uevent); |
| 894 | handle_firmware_event(uevent); |
| 895 | } |
| 896 | |
| 897 | return act; |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 898 | }); |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 899 | |
| 900 | return ret; |
Hung-ying Tyan | 99c4a8a | 2016-02-01 15:07:40 +0800 | [diff] [blame] | 901 | } |
| 902 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 903 | /* Coldboot walks parts of the /sys tree and pokes the uevent files |
| 904 | ** to cause the kernel to regenerate device add events that happened |
| 905 | ** before init's device manager was started |
| 906 | ** |
| 907 | ** We drain any pending events from the netlink socket every time |
| 908 | ** we poke another uevent file to make sure we don't overrun the |
Elliott Hughes | c0e919c | 2015-02-04 14:46:36 -0800 | [diff] [blame] | 909 | ** socket's buffer. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 910 | */ |
| 911 | |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 912 | static coldboot_action_t do_coldboot(DIR *d, coldboot_callback fn) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 913 | { |
| 914 | struct dirent *de; |
| 915 | int dfd, fd; |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 916 | coldboot_action_t act = COLDBOOT_CONTINUE; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 917 | |
| 918 | dfd = dirfd(d); |
| 919 | |
| 920 | fd = openat(dfd, "uevent", O_WRONLY); |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 921 | if (fd >= 0) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 922 | write(fd, "add\n", 4); |
| 923 | close(fd); |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 924 | act = handle_device_fd(fn); |
| 925 | if (should_stop_coldboot(act)) |
| 926 | return act; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 927 | } |
| 928 | |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 929 | while (!should_stop_coldboot(act) && (de = readdir(d))) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 930 | DIR *d2; |
| 931 | |
| 932 | if(de->d_type != DT_DIR || de->d_name[0] == '.') |
| 933 | continue; |
| 934 | |
| 935 | fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); |
| 936 | if(fd < 0) |
| 937 | continue; |
| 938 | |
| 939 | d2 = fdopendir(fd); |
| 940 | if(d2 == 0) |
| 941 | close(fd); |
| 942 | else { |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 943 | act = do_coldboot(d2, fn); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 944 | closedir(d2); |
| 945 | } |
| 946 | } |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 947 | |
| 948 | // default is always to continue looking for uevents |
| 949 | return act; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 950 | } |
| 951 | |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 952 | static coldboot_action_t coldboot(const char *path, coldboot_callback fn) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 953 | { |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 954 | std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir); |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 955 | if (d) { |
| 956 | return do_coldboot(d.get(), fn); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 957 | } |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 958 | |
| 959 | return COLDBOOT_CONTINUE; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 962 | void device_init(const char* path, coldboot_callback fn) { |
Sandeep Patil | 971a460 | 2017-02-15 13:37:52 -0800 | [diff] [blame] | 963 | if (!sehandle) { |
| 964 | sehandle = selinux_android_file_context_handle(); |
Elliott Hughes | 56a0656 | 2015-03-28 11:23:32 -0700 | [diff] [blame] | 965 | } |
Sandeep Patil | 971a460 | 2017-02-15 13:37:52 -0800 | [diff] [blame] | 966 | // open uevent socket and selinux status only if it hasn't been |
| 967 | // done before |
| 968 | if (device_fd == -1) { |
| 969 | /* is 256K enough? udev uses 16MB! */ |
| 970 | device_fd.reset(uevent_open_socket(256 * 1024, true)); |
| 971 | if (device_fd == -1) { |
| 972 | return; |
| 973 | } |
| 974 | fcntl(device_fd, F_SETFL, O_NONBLOCK); |
| 975 | selinux_status_open(true); |
| 976 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 977 | |
Elliott Hughes | 56a0656 | 2015-03-28 11:23:32 -0700 | [diff] [blame] | 978 | if (access(COLDBOOT_DONE, F_OK) == 0) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 979 | LOG(VERBOSE) << "Skipping coldboot, already done!"; |
Elliott Hughes | 56a0656 | 2015-03-28 11:23:32 -0700 | [diff] [blame] | 980 | return; |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 981 | } |
Elliott Hughes | 56a0656 | 2015-03-28 11:23:32 -0700 | [diff] [blame] | 982 | |
| 983 | Timer t; |
Sandeep Patil | 957e4ab | 2017-02-07 18:11:37 -0800 | [diff] [blame] | 984 | coldboot_action_t act; |
| 985 | if (!path) { |
| 986 | act = coldboot("/sys/class", fn); |
| 987 | if (!should_stop_coldboot(act)) { |
| 988 | act = coldboot("/sys/block", fn); |
| 989 | if (!should_stop_coldboot(act)) { |
| 990 | act = coldboot("/sys/devices", fn); |
| 991 | } |
| 992 | } |
| 993 | } else { |
| 994 | act = coldboot(path, fn); |
| 995 | } |
| 996 | |
| 997 | // If we have a callback, then do as it says. If no, then the default is |
| 998 | // to always create COLDBOOT_DONE file. |
| 999 | if (!fn || (act == COLDBOOT_FINISH)) { |
| 1000 | close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000)); |
| 1001 | } |
| 1002 | |
Elliott Hughes | 331cf2f | 2016-11-29 19:20:58 +0000 | [diff] [blame] | 1003 | LOG(INFO) << "Coldboot took " << t; |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 1004 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1005 | |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 1006 | void device_close() { |
| 1007 | destroy_platform_devices(); |
| 1008 | device_fd.reset(); |
Sandeep Patil | 971a460 | 2017-02-15 13:37:52 -0800 | [diff] [blame] | 1009 | selinux_status_close(); |
Sandeep Patil | 35403eb | 2017-02-08 20:27:12 -0800 | [diff] [blame] | 1010 | } |
| 1011 | |
Elliott Hughes | 632e99a | 2016-11-12 11:44:16 -0800 | [diff] [blame] | 1012 | int get_device_fd() { |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 1013 | return device_fd; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1014 | } |