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