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