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