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