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