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