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