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