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