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