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