blob: 92eb6cff6be3a37e70e0d0414dc1cbc4701a48c0 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
Mark Salyzyn154f4602014-02-20 14:59:07 -08002 * Copyright (C) 2007-2014 The Android Open Source Project
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07003 *
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 Hughesf39f7f12016-08-31 14:41:51 -070017#include <dirent.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070018#include <errno.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070019#include <fcntl.h>
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070020#include <fnmatch.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070021#include <libgen.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070022#include <linux/netlink.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -080023#include <stddef.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070024#include <stdio.h>
25#include <stdlib.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070026#include <string.h>
Elliott Hughes632e99a2016-11-12 11:44:16 -080027#include <sys/sendfile.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070028#include <sys/socket.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070029#include <sys/time.h>
30#include <sys/types.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070031#include <sys/un.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070032#include <sys/wait.h>
33#include <unistd.h>
34
Tom Cherry2e344f92017-04-04 17:53:45 -070035#include <algorithm>
James Hawkins588a2ca2016-02-18 14:52:46 -080036#include <memory>
Tom Cherry2e344f92017-04-04 17:53:45 -070037#include <string>
Elliott Hughes290a2282016-11-14 17:08:47 -080038#include <thread>
Tom Cherry2e344f92017-04-04 17:53:45 -070039#include <vector>
James Hawkins588a2ca2016-02-18 14:52:46 -080040
Biao Ludc848562016-01-28 16:10:54 +080041#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070042#include <android-base/logging.h>
Rob Herring6de783a2016-05-06 10:06:59 -050043#include <android-base/stringprintf.h>
Tom Cherry2e344f92017-04-04 17:53:45 -070044#include <android-base/strings.h>
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +080045#include <android-base/unique_fd.h>
Dima Zavinda04c522011-09-01 17:09:44 -070046#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100047#include <cutils/uevent.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070048#include <private/android_filesystem_config.h>
49#include <selinux/android.h>
50#include <selinux/avc.h>
51#include <selinux/label.h>
52#include <selinux/selinux.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100053
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054#include "devices.h"
Greg Hackmann3312aa82013-11-18 15:24:40 -080055#include "ueventd_parser.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070056#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070057
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070058#define SYSFS_PREFIX "/sys"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070059
Stephen Smalleye096e362012-06-11 13:37:39 -040060extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050061
Sandeep Patil35403eb2017-02-08 20:27:12 -080062static android::base::unique_fd device_fd;
Colin Cross0dd7ca62010-04-13 19:25:51 -070063
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064struct perms_ {
65 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070066 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070067 mode_t perm;
68 unsigned int uid;
69 unsigned int gid;
70 unsigned short prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070071 unsigned short wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070074struct perm_node {
75 struct perms_ dp;
76 struct listnode plist;
77};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070078
Colin Crossfadb85e2011-03-30 18:32:12 -070079struct platform_node {
80 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080081 char *path;
82 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070083 struct listnode list;
84};
85
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070086static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070087static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070088static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070089
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070090int add_dev_perms(const char *name, const char *attr,
91 mode_t perm, unsigned int uid, unsigned int gid,
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070092 unsigned short prefix,
93 unsigned short wildcard) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -080094 struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070095 if (!node)
96 return -ENOMEM;
97
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070098 node->dp.name = strdup(name);
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -080099 if (!node->dp.name) {
100 free(node);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700101 return -ENOMEM;
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800102 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700103
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700104 if (attr) {
105 node->dp.attr = strdup(attr);
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800106 if (!node->dp.attr) {
107 free(node->dp.name);
108 free(node);
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700109 return -ENOMEM;
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800110 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700111 }
112
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700113 node->dp.perm = perm;
114 node->dp.uid = uid;
115 node->dp.gid = gid;
116 node->dp.prefix = prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700117 node->dp.wildcard = wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700118
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700119 if (attr)
120 list_add_tail(&sys_perms, &node->plist);
121 else
122 list_add_tail(&dev_perms, &node->plist);
123
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700124 return 0;
125}
126
Colin Cross43d537e2014-07-02 13:08:13 -0700127static bool perm_path_matches(const char *path, struct perms_ *dp)
128{
129 if (dp->prefix) {
130 if (strncmp(path, dp->name, strlen(dp->name)) == 0)
131 return true;
132 } else if (dp->wildcard) {
133 if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
134 return true;
135 } else {
136 if (strcmp(path, dp->name) == 0)
137 return true;
138 }
139
140 return false;
141}
142
Rob Herring6de783a2016-05-06 10:06:59 -0500143static bool match_subsystem(perms_* dp, const char* pattern,
144 const char* path, const char* subsystem) {
145 if (!pattern || !subsystem || strstr(dp->name, subsystem) == NULL) {
146 return false;
147 }
Rob Herringe5636a32016-05-06 12:28:48 -0500148
Rob Herring6de783a2016-05-06 10:06:59 -0500149 std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path));
150 return perm_path_matches(subsys_path.c_str(), dp);
151}
Rob Herringe5636a32016-05-06 12:28:48 -0500152
Tom Cherrye3e48212017-04-11 13:53:37 -0700153static void fixup_sys_perms(const std::string& upath, const std::string& subsystem) {
Rob Herring6de783a2016-05-06 10:06:59 -0500154 // upaths omit the "/sys" that paths in this list
155 // contain, so we prepend it...
Tom Cherrye3e48212017-04-11 13:53:37 -0700156 std::string path = SYSFS_PREFIX + upath;
Rob Herring6de783a2016-05-06 10:06:59 -0500157
158 listnode* node;
Rob Herringe5636a32016-05-06 12:28:48 -0500159 list_for_each(node, &sys_perms) {
Rob Herring6de783a2016-05-06 10:06:59 -0500160 perms_* dp = &(node_to_item(node, perm_node, plist))->dp;
Tom Cherrye3e48212017-04-11 13:53:37 -0700161 if (match_subsystem(dp, SYSFS_PREFIX "/class/%s/%s", path.c_str(), subsystem.c_str())) {
Rob Herring6de783a2016-05-06 10:06:59 -0500162 ; // matched
Tom Cherrye3e48212017-04-11 13:53:37 -0700163 } else if (match_subsystem(dp, SYSFS_PREFIX "/bus/%s/devices/%s", path.c_str(),
164 subsystem.c_str())) {
Rob Herring6de783a2016-05-06 10:06:59 -0500165 ; // matched
166 } else if (!perm_path_matches(path.c_str(), dp)) {
167 continue;
Rob Herringe5636a32016-05-06 12:28:48 -0500168 }
169
170 std::string attr_file = path + "/" + dp->attr;
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700171 LOG(INFO) << "fixup " << attr_file
172 << " " << dp->uid << " " << dp->gid << " " << std::oct << dp->perm;
Rob Herringe5636a32016-05-06 12:28:48 -0500173 chown(attr_file.c_str(), dp->uid, dp->gid);
174 chmod(attr_file.c_str(), dp->perm);
175 }
176
177 if (access(path.c_str(), F_OK) == 0) {
Dmitry Shmidt7eed4742016-07-28 13:55:39 -0700178 LOG(VERBOSE) << "restorecon_recursive: " << path;
Paul Lawrencea8d84342016-11-14 15:40:18 -0800179 restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE);
Rob Herringe5636a32016-05-06 12:28:48 -0500180 }
181}
182
Tom Cherry2e344f92017-04-04 17:53:45 -0700183static mode_t get_device_perm(const char* path, const std::vector<std::string>& links,
184 unsigned* uid, unsigned* gid) {
Colin Cross44b65d02010-04-20 14:32:50 -0700185 struct listnode *node;
186 struct perm_node *perm_node;
187 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700188
Colin Cross44b65d02010-04-20 14:32:50 -0700189 /* search the perms list in reverse so that ueventd.$hardware can
190 * override ueventd.rc
191 */
192 list_for_each_reverse(node, &dev_perms) {
193 perm_node = node_to_item(node, struct perm_node, plist);
194 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700195
Tom Cherry2e344f92017-04-04 17:53:45 -0700196 if (perm_path_matches(path, dp) ||
197 std::any_of(links.begin(), links.end(),
198 [dp](const auto& link) { return perm_path_matches(link.c_str(), dp); })) {
Colin Cross43d537e2014-07-02 13:08:13 -0700199 *uid = dp->uid;
200 *gid = dp->gid;
201 return dp->perm;
202 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700203 }
Colin Cross44b65d02010-04-20 14:32:50 -0700204 /* Default if nothing found. */
205 *uid = 0;
206 *gid = 0;
207 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700208}
209
Tom Cherrye3e48212017-04-11 13:53:37 -0700210static void make_device(const std::string& path, int block, int major, int minor,
Tom Cherry2e344f92017-04-04 17:53:45 -0700211 const std::vector<std::string>& links) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700212 unsigned uid;
213 unsigned gid;
214 mode_t mode;
215 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500216 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700217
Tom Cherrye3e48212017-04-11 13:53:37 -0700218 mode = get_device_perm(path.c_str(), links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700219
Sandeep Patilea239832017-02-03 07:51:55 -0800220 if (sehandle) {
Tom Cherry2e344f92017-04-04 17:53:45 -0700221 std::vector<const char*> c_links;
222 for (const auto& link : links) {
223 c_links.emplace_back(link.c_str());
224 }
225 c_links.emplace_back(nullptr);
Tom Cherrye3e48212017-04-11 13:53:37 -0700226 if (selabel_lookup_best_match(sehandle, &secontext, path.c_str(), &c_links[0], mode)) {
Sandeep Patilea239832017-02-03 07:51:55 -0800227 PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
228 return;
229 }
230 setfscreatecon(secontext);
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300231 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700232
Colin Cross17dcc5c2010-09-03 12:25:34 -0700233 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800234 /* Temporarily change egid to avoid race condition setting the gid of the
235 * device node. Unforunately changing the euid would prevent creation of
236 * some device nodes, so the uid has to be set with chown() and is still
237 * racy. Fixing the gid race at least fixed the issue with system_server
238 * opening dynamic input devices under the AID_INPUT gid. */
Tom Cherry0506b182017-02-23 13:46:09 -0800239 if (setegid(gid)) {
240 PLOG(ERROR) << "setegid(" << gid << ") for " << path << " device failed";
241 goto out;
242 }
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300243 /* If the node already exists update its SELinux label to handle cases when
244 * it was created with the wrong context during coldboot procedure. */
Tom Cherrye3e48212017-04-11 13:53:37 -0700245 if (mknod(path.c_str(), mode, dev) && (errno == EEXIST) && secontext) {
William Roberts397de142016-06-02 09:53:44 -0700246 char* fcon = nullptr;
Tom Cherrye3e48212017-04-11 13:53:37 -0700247 int rc = lgetfilecon(path.c_str(), &fcon);
William Roberts397de142016-06-02 09:53:44 -0700248 if (rc < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700249 PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
William Roberts397de142016-06-02 09:53:44 -0700250 goto out;
251 }
252
253 bool different = strcmp(fcon, secontext) != 0;
254 freecon(fcon);
255
Tom Cherrye3e48212017-04-11 13:53:37 -0700256 if (different && lsetfilecon(path.c_str(), secontext)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700257 PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path << "' device";
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300258 }
259 }
William Roberts397de142016-06-02 09:53:44 -0700260
261out:
Tom Cherrye3e48212017-04-11 13:53:37 -0700262 chown(path.c_str(), uid, -1);
Tom Cherry0506b182017-02-23 13:46:09 -0800263 if (setegid(AID_ROOT)) {
264 PLOG(FATAL) << "setegid(AID_ROOT) failed";
265 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700266
Sandeep Patilea239832017-02-03 07:51:55 -0800267 if (secontext) {
268 freecon(secontext);
269 setfscreatecon(NULL);
270 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700271}
272
Tom Cherryc44f6a42017-04-05 15:58:31 -0700273void add_platform_device(const char* path) {
Dima Zavinf395c922013-03-06 16:23:57 -0800274 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700275 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800276 const char *name = path;
277
278 if (!strncmp(path, "/devices/", 9)) {
279 name += 9;
280 if (!strncmp(name, "platform/", 9))
281 name += 9;
282 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700283
Wei Wanga285dac2016-10-04 14:05:39 -0700284 LOG(VERBOSE) << "adding platform device " << name << " (" << path << ")";
Colin Crossfadb85e2011-03-30 18:32:12 -0700285
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800286 bus = (platform_node*) calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800287 bus->path = strdup(path);
288 bus->path_len = path_len;
289 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700290 list_add_tail(&platform_names, &bus->list);
291}
292
293/*
Dima Zavinf395c922013-03-06 16:23:57 -0800294 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700295 * platform device prefix. If it doesn't start with a platform device, return
296 * 0.
297 */
Tom Cherrye3e48212017-04-11 13:53:37 -0700298static struct platform_node* find_platform_device(const char* path) {
Dima Zavinf395c922013-03-06 16:23:57 -0800299 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700300 struct listnode *node;
301 struct platform_node *bus;
302
303 list_for_each_reverse(node, &platform_names) {
304 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800305 if ((bus->path_len < path_len) &&
306 (path[bus->path_len] == '/') &&
307 !strncmp(path, bus->path, bus->path_len))
308 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700309 }
310
311 return NULL;
312}
313
Tom Cherryc44f6a42017-04-05 15:58:31 -0700314void remove_platform_device(const char* path) {
Colin Crossfadb85e2011-03-30 18:32:12 -0700315 struct listnode *node;
316 struct platform_node *bus;
317
318 list_for_each_reverse(node, &platform_names) {
319 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800320 if (!strcmp(path, bus->path)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700321 LOG(INFO) << "removing platform device " << bus->name;
Dima Zavinf395c922013-03-06 16:23:57 -0800322 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700323 list_remove(node);
324 free(bus);
325 return;
326 }
327 }
328}
329
Sandeep Patil35403eb2017-02-08 20:27:12 -0800330static void destroy_platform_devices() {
331 struct listnode* node;
332 struct listnode* n;
333 struct platform_node* bus;
334
335 list_for_each_safe(node, n, &platform_names) {
336 list_remove(node);
337 bus = node_to_item(node, struct platform_node, list);
338 free(bus->path);
339 free(bus);
340 }
341}
342
Andrew Boiea885d042013-09-13 17:41:20 -0700343/* Given a path that may start with a PCI device, populate the supplied buffer
344 * with the PCI domain/bus number and the peripheral ID and return 0.
345 * If it doesn't start with a PCI device, or there is some error, return -1 */
Tom Cherry2e344f92017-04-04 17:53:45 -0700346static bool find_pci_device_prefix(const std::string& path, std::string* result) {
347 result->clear();
Andrew Boiea885d042013-09-13 17:41:20 -0700348
Tom Cherry2e344f92017-04-04 17:53:45 -0700349 if (!android::base::StartsWith(path, "/devices/pci")) return false;
Andrew Boiea885d042013-09-13 17:41:20 -0700350
351 /* Beginning of the prefix is the initial "pci" after "/devices/" */
Tom Cherry2e344f92017-04-04 17:53:45 -0700352 std::string::size_type start = 9;
Andrew Boiea885d042013-09-13 17:41:20 -0700353
354 /* End of the prefix is two path '/' later, capturing the domain/bus number
355 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
Tom Cherry2e344f92017-04-04 17:53:45 -0700356 auto end = path.find('/', start);
357 if (end == std::string::npos) return false;
Andrew Boiea885d042013-09-13 17:41:20 -0700358
Tom Cherry2e344f92017-04-04 17:53:45 -0700359 end = path.find('/', end + 1);
360 if (end == std::string::npos) return false;
Andrew Boiea885d042013-09-13 17:41:20 -0700361
Tom Cherry2e344f92017-04-04 17:53:45 -0700362 auto length = end - start;
363 if (length <= 4) {
364 // The minimum string that will get to this check is 'pci/', which is malformed,
365 // so return false
366 return false;
367 }
368
369 *result = path.substr(start, length);
370 return true;
Andrew Boiea885d042013-09-13 17:41:20 -0700371}
372
Jeremy Compostella937309d2017-03-03 16:27:29 +0100373/* Given a path that may start with a virtual block device, populate
374 * the supplied buffer with the virtual block device ID and return 0.
375 * If it doesn't start with a virtual block device, or there is some
376 * error, return -1 */
Tom Cherry2e344f92017-04-04 17:53:45 -0700377static bool find_vbd_device_prefix(const std::string& path, std::string* result) {
378 result->clear();
379
380 if (!android::base::StartsWith(path, "/devices/vbd-")) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100381
382 /* Beginning of the prefix is the initial "vbd-" after "/devices/" */
Tom Cherry2e344f92017-04-04 17:53:45 -0700383 std::string::size_type start = 13;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100384
385 /* End of the prefix is one path '/' later, capturing the
386 virtual block device ID. Example: 768 */
Tom Cherry2e344f92017-04-04 17:53:45 -0700387 auto end = path.find('/', start);
388 if (end == std::string::npos) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100389
Tom Cherry2e344f92017-04-04 17:53:45 -0700390 auto length = end - start;
391 if (length == 0) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100392
Tom Cherry2e344f92017-04-04 17:53:45 -0700393 *result = path.substr(start, length);
394 return true;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100395}
396
Tom Cherrye3e48212017-04-11 13:53:37 -0700397void parse_event(const char* msg, uevent* uevent) {
398 uevent->partition_num = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700399 uevent->major = -1;
400 uevent->minor = -1;
Tom Cherrye3e48212017-04-11 13:53:37 -0700401 // currently ignoring SEQNUM
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700402 while(*msg) {
403 if(!strncmp(msg, "ACTION=", 7)) {
404 msg += 7;
405 uevent->action = msg;
406 } else if(!strncmp(msg, "DEVPATH=", 8)) {
407 msg += 8;
408 uevent->path = msg;
409 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
410 msg += 10;
411 uevent->subsystem = msg;
412 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
413 msg += 9;
414 uevent->firmware = msg;
415 } else if(!strncmp(msg, "MAJOR=", 6)) {
416 msg += 6;
417 uevent->major = atoi(msg);
418 } else if(!strncmp(msg, "MINOR=", 6)) {
419 msg += 6;
420 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700421 } else if(!strncmp(msg, "PARTN=", 6)) {
422 msg += 6;
423 uevent->partition_num = atoi(msg);
424 } else if(!strncmp(msg, "PARTNAME=", 9)) {
425 msg += 9;
426 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700427 } else if(!strncmp(msg, "DEVNAME=", 8)) {
428 msg += 8;
429 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700430 }
431
Tom Cherrye3e48212017-04-11 13:53:37 -0700432 // advance to after the next \0
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700433 while(*msg++)
434 ;
435 }
436
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800437 if (LOG_UEVENTS) {
Tom Cherrye3e48212017-04-11 13:53:37 -0700438 LOG(INFO) << "event { '" << uevent->action << "', '" << uevent->path << "', '"
439 << uevent->subsystem << "', '" << uevent->firmware << "', " << uevent->major
440 << ", " << uevent->minor << " }";
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800441 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700442}
443
Tom Cherry2e344f92017-04-04 17:53:45 -0700444std::vector<std::string> get_character_device_symlinks(uevent* uevent) {
Tom Cherrye3e48212017-04-11 13:53:37 -0700445 platform_node* pdev = find_platform_device(uevent->path.c_str());
Tom Cherry2e344f92017-04-04 17:53:45 -0700446 if (!pdev) return {};
Benoit Gobyd2278632010-08-03 14:36:02 -0700447
448 /* skip "/devices/platform/<driver>" */
Tom Cherrye3e48212017-04-11 13:53:37 -0700449 auto parent_start = uevent->path.find('/', pdev->path_len);
Tom Cherry2e344f92017-04-04 17:53:45 -0700450 if (parent_start == std::string::npos) return {};
Benoit Gobyd2278632010-08-03 14:36:02 -0700451
Tom Cherrye3e48212017-04-11 13:53:37 -0700452 std::string parent = uevent->path.substr(parent_start);
Benoit Gobyd2278632010-08-03 14:36:02 -0700453
Tom Cherry2e344f92017-04-04 17:53:45 -0700454 if (!android::base::StartsWith(parent, "/usb")) return {};
455
456 // skip root hub name and device. use device interface
457 // skip 3 slashes, including the first / by starting the search at the 1st character, not 0th.
458 // then extract what comes between the 3rd and 4th slash
459 // e.g. "/usb/usb_device/name/tty2-1:1.0" -> "name"
460
461 std::string::size_type start = 0;
462 start = parent.find('/', start + 1);
463 if (start == std::string::npos) return {};
464
465 start = parent.find('/', start + 1);
466 if (start == std::string::npos) return {};
467
468 auto end = parent.find('/', start + 1);
469 if (end == std::string::npos) return {};
470
471 start++; // Skip the first '/'
472
473 auto length = end - start;
474 if (length == 0) return {};
475
476 auto name_string = parent.substr(start, length);
477
Tom Cherry2e344f92017-04-04 17:53:45 -0700478 std::vector<std::string> links;
Tom Cherrye3e48212017-04-11 13:53:37 -0700479 links.emplace_back("/dev/usb/" + uevent->subsystem + name_string);
Tom Cherry2e344f92017-04-04 17:53:45 -0700480
481 mkdir("/dev/usb", 0755);
Benoit Gobyd2278632010-08-03 14:36:02 -0700482
483 return links;
Benoit Gobyd2278632010-08-03 14:36:02 -0700484}
485
Tom Cherryc44f6a42017-04-05 15:58:31 -0700486// replaces any unacceptable characters with '_', the
487// length of the resulting string is equal to the input string
Tom Cherry2e344f92017-04-04 17:53:45 -0700488void sanitize_partition_name(std::string* string) {
Tom Cherryc44f6a42017-04-05 15:58:31 -0700489 const char* accept =
490 "abcdefghijklmnopqrstuvwxyz"
491 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
492 "0123456789"
493 "_-.";
494
Tom Cherry2e344f92017-04-04 17:53:45 -0700495 if (!string) return;
Tom Cherryc44f6a42017-04-05 15:58:31 -0700496
Tom Cherry2e344f92017-04-04 17:53:45 -0700497 std::string::size_type pos = 0;
498 while ((pos = string->find_first_not_of(accept, pos)) != std::string::npos) {
499 (*string)[pos] = '_';
Tom Cherryc44f6a42017-04-05 15:58:31 -0700500 }
501}
502
Tom Cherry2e344f92017-04-04 17:53:45 -0700503std::vector<std::string> get_block_device_symlinks(uevent* uevent) {
504 std::string device;
505 struct platform_node* pdev;
506 std::string type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700507
Tom Cherrye3e48212017-04-11 13:53:37 -0700508 pdev = find_platform_device(uevent->path.c_str());
Andrew Boiea885d042013-09-13 17:41:20 -0700509 if (pdev) {
510 device = pdev->name;
511 type = "platform";
Tom Cherry2e344f92017-04-04 17:53:45 -0700512 } else if (find_pci_device_prefix(uevent->path, &device)) {
Andrew Boiea885d042013-09-13 17:41:20 -0700513 type = "pci";
Tom Cherry2e344f92017-04-04 17:53:45 -0700514 } else if (find_vbd_device_prefix(uevent->path, &device)) {
Jeremy Compostella937309d2017-03-03 16:27:29 +0100515 type = "vbd";
Andrew Boiea885d042013-09-13 17:41:20 -0700516 } else {
Tom Cherry2e344f92017-04-04 17:53:45 -0700517 return {};
Andrew Boiea885d042013-09-13 17:41:20 -0700518 }
Dima Zavinf395c922013-03-06 16:23:57 -0800519
Tom Cherry2e344f92017-04-04 17:53:45 -0700520 std::vector<std::string> links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700521
Sandeep Patil35403eb2017-02-08 20:27:12 -0800522 LOG(VERBOSE) << "found " << type << " device " << device;
Colin Crossfadb85e2011-03-30 18:32:12 -0700523
Tom Cherry2e344f92017-04-04 17:53:45 -0700524 auto link_path = "/dev/block/" + type + "/" + device;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700525
Tom Cherrye3e48212017-04-11 13:53:37 -0700526 if (!uevent->partition_name.empty()) {
Tom Cherry2e344f92017-04-04 17:53:45 -0700527 std::string partition_name_sanitized(uevent->partition_name);
528 sanitize_partition_name(&partition_name_sanitized);
529 if (partition_name_sanitized != uevent->partition_name) {
530 LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '"
531 << partition_name_sanitized << "'";
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700532 }
Tom Cherry2e344f92017-04-04 17:53:45 -0700533 links.emplace_back(link_path + "/by-name/" + partition_name_sanitized);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700534 }
535
536 if (uevent->partition_num >= 0) {
Tom Cherry2e344f92017-04-04 17:53:45 -0700537 links.emplace_back(link_path + "/by-num/p" + std::to_string(uevent->partition_num));
Colin Crossb0ab94b2010-04-08 16:16:20 -0700538 }
539
Tom Cherrye3e48212017-04-11 13:53:37 -0700540 auto last_slash = uevent->path.rfind('/');
541 links.emplace_back(link_path + "/" + uevent->path.substr(last_slash + 1));
Colin Crossb0ab94b2010-04-08 16:16:20 -0700542
543 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700544}
545
Tom Cherrye3e48212017-04-11 13:53:37 -0700546static void make_link_init(const std::string& oldpath, const std::string& newpath) {
547 if (mkdir_recursive(dirname(newpath.c_str()), 0755)) {
548 PLOG(ERROR) << "Failed to create directory " << dirname(newpath.c_str());
549 }
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700550
Tom Cherrye3e48212017-04-11 13:53:37 -0700551 if (symlink(oldpath.c_str(), newpath.c_str()) && errno != EEXIST) {
552 PLOG(ERROR) << "Failed to symlink " << oldpath << " to " << newpath;
553 }
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700554}
555
Tom Cherrye3e48212017-04-11 13:53:37 -0700556static void remove_link(const std::string& oldpath, const std::string& newpath) {
557 std::string path;
558 if (android::base::Readlink(newpath, &path) && path == oldpath) unlink(newpath.c_str());
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700559}
560
Tom Cherrye3e48212017-04-11 13:53:37 -0700561static void handle_device(const std::string& action, const std::string& devpath, int block,
Tom Cherry2e344f92017-04-04 17:53:45 -0700562 int major, int minor, const std::vector<std::string>& links) {
Tom Cherrye3e48212017-04-11 13:53:37 -0700563 if (action == "add") {
564 make_device(devpath, block, major, minor, links);
Tom Cherry2e344f92017-04-04 17:53:45 -0700565 for (const auto& link : links) {
Tom Cherrye3e48212017-04-11 13:53:37 -0700566 make_link_init(devpath, link);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700567 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700568 }
569
Tom Cherrye3e48212017-04-11 13:53:37 -0700570 if (action == "remove") {
Tom Cherry2e344f92017-04-04 17:53:45 -0700571 for (const auto& link : links) {
Tom Cherrye3e48212017-04-11 13:53:37 -0700572 remove_link(devpath, link);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700573 }
Tom Cherrye3e48212017-04-11 13:53:37 -0700574 unlink(devpath.c_str());
Colin Crossb0ab94b2010-04-08 16:16:20 -0700575 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700576}
577
Tom Cherrye3e48212017-04-11 13:53:37 -0700578static void handle_platform_device_event(uevent* uevent) {
579 if (uevent->action == "add") {
580 add_platform_device(uevent->path.c_str());
581 } else if (uevent->action == "remove") {
582 remove_platform_device(uevent->path.c_str());
583 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700584}
585
Tom Cherry3fa46732017-04-11 14:19:50 -0700586static void handle_block_device_event(uevent* uevent) {
587 // if it's not a /dev device, nothing to do
588 if (uevent->major < 0 || uevent->minor < 0) return;
Colin Crosseb5ba832011-03-30 17:37:17 -0700589
Tom Cherry3fa46732017-04-11 14:19:50 -0700590 const char* base = "/dev/block/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500591 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700592
Tom Cherry3fa46732017-04-11 14:19:50 -0700593 std::string name = android::base::Basename(uevent->path);
594 std::string devpath = base + name;
595
Tom Cherry2e344f92017-04-04 17:53:45 -0700596 std::vector<std::string> links;
Tom Cherrye3e48212017-04-11 13:53:37 -0700597 if (android::base::StartsWith(uevent->path, "/devices")) {
Andrew Boiea885d042013-09-13 17:41:20 -0700598 links = get_block_device_symlinks(uevent);
Tom Cherrye3e48212017-04-11 13:53:37 -0700599 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700600
Tom Cherrye3e48212017-04-11 13:53:37 -0700601 handle_device(uevent->action, devpath, 1, uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700602}
603
Tom Cherry3fa46732017-04-11 14:19:50 -0700604static void handle_generic_device_event(uevent* uevent) {
605 // if it's not a /dev device, nothing to do
606 if (uevent->major < 0 || uevent->minor < 0) return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800607
Tom Cherry3fa46732017-04-11 14:19:50 -0700608 std::string name = android::base::Basename(uevent->path);
Tom Cherrye3e48212017-04-11 13:53:37 -0700609 ueventd_subsystem* subsystem = ueventd_subsystem_find_by_name(uevent->subsystem.c_str());
Greg Hackmann3312aa82013-11-18 15:24:40 -0800610
Tom Cherry3fa46732017-04-11 14:19:50 -0700611 std::string devpath;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800612
613 if (subsystem) {
Tom Cherry3fa46732017-04-11 14:19:50 -0700614 std::string devname;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800615
616 switch (subsystem->devname_src) {
617 case DEVNAME_UEVENT_DEVNAME:
618 devname = uevent->device_name;
619 break;
620
621 case DEVNAME_UEVENT_DEVPATH:
622 devname = name;
623 break;
624
625 default:
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700626 LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800627 return;
628 }
629
Tom Cherry3fa46732017-04-11 14:19:50 -0700630 // TODO: Remove std::string()
631 devpath = std::string(subsystem->dirname) + "/" + devname;
632 mkdir_recursive(android::base::Dirname(devpath), 0755);
Tom Cherrye3e48212017-04-11 13:53:37 -0700633 } else if (android::base::StartsWith(uevent->subsystem, "usb")) {
634 if (uevent->subsystem == "usb") {
635 if (!uevent->device_name.empty()) {
636 devpath = "/dev/" + uevent->device_name;
Tom Cherry3fa46732017-04-11 14:19:50 -0700637 } else {
638 // This imitates the file system that would be created
639 // if we were using devfs instead.
640 // Minors are broken up into groups of 128, starting at "001"
641 int bus_id = uevent->minor / 128 + 1;
642 int device_id = uevent->minor % 128 + 1;
643 devpath = android::base::StringPrintf("/dev/bus/usb/%03d/%03d", bus_id, device_id);
644 }
645 mkdir_recursive(android::base::Dirname(devpath), 0755);
646 } else {
647 // ignore other USB events
648 return;
649 }
Tom Cherry780a71e2017-04-04 16:30:40 -0700650 } else {
Tom Cherry3fa46732017-04-11 14:19:50 -0700651 devpath = "/dev/" + name;
Tom Cherry780a71e2017-04-04 16:30:40 -0700652 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700653
Tom Cherry780a71e2017-04-04 16:30:40 -0700654 auto links = get_character_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700655
Tom Cherrye3e48212017-04-11 13:53:37 -0700656 handle_device(uevent->action, devpath, 0, uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700657}
658
659static void handle_device_event(struct uevent *uevent)
660{
Tom Cherrye3e48212017-04-11 13:53:37 -0700661 if (uevent->action == "add" || uevent->action == "change" || uevent->action == "online") {
Rob Herring6de783a2016-05-06 10:06:59 -0500662 fixup_sys_perms(uevent->path, uevent->subsystem);
Tom Cherrye3e48212017-04-11 13:53:37 -0700663 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700664
Tom Cherrye3e48212017-04-11 13:53:37 -0700665 if (uevent->subsystem == "block") {
Colin Crosseb5ba832011-03-30 17:37:17 -0700666 handle_block_device_event(uevent);
Tom Cherrye3e48212017-04-11 13:53:37 -0700667 } else if (uevent->subsystem == "platform") {
Colin Crossfadb85e2011-03-30 18:32:12 -0700668 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700669 } else {
670 handle_generic_device_event(uevent);
671 }
672}
673
Elliott Hughes632e99a2016-11-12 11:44:16 -0800674static void load_firmware(uevent* uevent, const std::string& root,
675 int fw_fd, size_t fw_size,
676 int loading_fd, int data_fd) {
677 // Start transfer.
678 android::base::WriteFully(loading_fd, "1", 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700679
Elliott Hughes632e99a2016-11-12 11:44:16 -0800680 // Copy the firmware.
681 int rc = sendfile(data_fd, fw_fd, nullptr, fw_size);
682 if (rc == -1) {
683 PLOG(ERROR) << "firmware: sendfile failed { '" << root << "', '" << uevent->firmware << "' }";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700684 }
685
Elliott Hughes632e99a2016-11-12 11:44:16 -0800686 // Tell the firmware whether to abort or commit.
687 const char* response = (rc != -1) ? "0" : "-1";
688 android::base::WriteFully(loading_fd, response, strlen(response));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700689}
690
Elliott Hughes632e99a2016-11-12 11:44:16 -0800691static int is_booting() {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700692 return access("/dev/.booting", F_OK) == 0;
693}
694
Elliott Hughes632e99a2016-11-12 11:44:16 -0800695static void process_firmware_event(uevent* uevent) {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700696 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700697
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700698 LOG(INFO) << "firmware: loading '" << uevent->firmware << "' for '" << uevent->path << "'";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700699
Tom Cherrye3e48212017-04-11 13:53:37 -0700700 std::string root = "/sys" + uevent->path;
Elliott Hughes632e99a2016-11-12 11:44:16 -0800701 std::string loading = root + "/loading";
702 std::string data = root + "/data";
703
704 android::base::unique_fd loading_fd(open(loading.c_str(), O_WRONLY|O_CLOEXEC));
705 if (loading_fd == -1) {
706 PLOG(ERROR) << "couldn't open firmware loading fd for " << uevent->firmware;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700707 return;
Elliott Hughes632e99a2016-11-12 11:44:16 -0800708 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700709
Elliott Hughes632e99a2016-11-12 11:44:16 -0800710 android::base::unique_fd data_fd(open(data.c_str(), O_WRONLY|O_CLOEXEC));
711 if (data_fd == -1) {
712 PLOG(ERROR) << "couldn't open firmware data fd for " << uevent->firmware;
713 return;
714 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700715
Tom Cherrye3e48212017-04-11 13:53:37 -0700716 static const char* firmware_dirs[] = {"/etc/firmware/", "/vendor/firmware/",
717 "/firmware/image/"};
718
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700719try_loading_again:
Elliott Hughes632e99a2016-11-12 11:44:16 -0800720 for (size_t i = 0; i < arraysize(firmware_dirs); i++) {
Tom Cherrye3e48212017-04-11 13:53:37 -0700721 std::string file = firmware_dirs[i] + uevent->firmware;
Elliott Hughes632e99a2016-11-12 11:44:16 -0800722 android::base::unique_fd fw_fd(open(file.c_str(), O_RDONLY|O_CLOEXEC));
723 struct stat sb;
724 if (fw_fd != -1 && fstat(fw_fd, &sb) != -1) {
725 load_firmware(uevent, root, fw_fd, sb.st_size, loading_fd, data_fd);
726 return;
Benoit Goby609d8822010-11-09 18:10:24 -0800727 }
Brian Swetland02863b92010-09-19 03:36:39 -0700728 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700729
Elliott Hughes632e99a2016-11-12 11:44:16 -0800730 if (booting) {
731 // If we're not fully booted, we may be missing
732 // filesystems needed for firmware, wait and retry.
Elliott Hughes290a2282016-11-14 17:08:47 -0800733 std::this_thread::sleep_for(100ms);
Elliott Hughes632e99a2016-11-12 11:44:16 -0800734 booting = is_booting();
735 goto try_loading_again;
736 }
737
738 LOG(ERROR) << "firmware: could not find firmware for " << uevent->firmware;
739
740 // Write "-1" as our response to the kernel's firmware request, since we have nothing for it.
741 write(loading_fd, "-1", 2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700742}
743
Elliott Hughes632e99a2016-11-12 11:44:16 -0800744static void handle_firmware_event(uevent* uevent) {
Tom Cherrye3e48212017-04-11 13:53:37 -0700745 if (uevent->subsystem != "firmware" || uevent->action != "add") return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700746
Elliott Hughes632e99a2016-11-12 11:44:16 -0800747 // Loading the firmware in a child means we can do that in parallel...
748 // (We ignore SIGCHLD rather than wait for our children.)
749 pid_t pid = fork();
750 if (pid == 0) {
751 Timer t;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700752 process_firmware_event(uevent);
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000753 LOG(INFO) << "loading " << uevent->path << " took " << t;
Kenny Root17baff42014-08-20 16:16:44 -0700754 _exit(EXIT_SUCCESS);
Elliott Hughes632e99a2016-11-12 11:44:16 -0800755 } else if (pid == -1) {
756 PLOG(ERROR) << "could not fork to process firmware event for " << uevent->firmware;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700757 }
758}
759
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800760static bool inline should_stop_coldboot(coldboot_action_t act)
761{
762 return (act == COLDBOOT_STOP || act == COLDBOOT_FINISH);
763}
764
Ruchi Kandoic6037202014-06-23 11:22:09 -0700765#define UEVENT_MSG_LEN 2048
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800766
Sandeep Patil44a3ee22017-02-08 15:49:47 -0800767static inline coldboot_action_t handle_device_fd_with(
768 std::function<coldboot_action_t(uevent* uevent)> handle_uevent)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700769{
Vernon Tang3f582e92011-04-25 13:08:17 +1000770 char msg[UEVENT_MSG_LEN+2];
771 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700772 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700773 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700774 continue;
775
776 msg[n] = '\0';
777 msg[n+1] = '\0';
778
Tom Cherrye3e48212017-04-11 13:53:37 -0700779 uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700780 parse_event(msg, &uevent);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800781 coldboot_action_t act = handle_uevent(&uevent);
782 if (should_stop_coldboot(act))
783 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700784 }
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800785
786 return COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700787}
788
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800789coldboot_action_t handle_device_fd(coldboot_callback fn)
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800790{
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800791 coldboot_action_t ret = handle_device_fd_with(
792 [&](uevent* uevent) -> coldboot_action_t {
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800793 if (selinux_status_updated() > 0) {
794 struct selabel_handle *sehandle2;
795 sehandle2 = selinux_android_file_context_handle();
796 if (sehandle2) {
797 selabel_close(sehandle);
798 sehandle = sehandle2;
799 }
800 }
801
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800802 // default is to always create the devices
803 coldboot_action_t act = COLDBOOT_CREATE;
804 if (fn) {
805 act = fn(uevent);
806 }
807
808 if (act == COLDBOOT_CREATE || act == COLDBOOT_STOP) {
809 handle_device_event(uevent);
810 handle_firmware_event(uevent);
811 }
812
813 return act;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800814 });
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800815
816 return ret;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800817}
818
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700819/* Coldboot walks parts of the /sys tree and pokes the uevent files
820** to cause the kernel to regenerate device add events that happened
821** before init's device manager was started
822**
823** We drain any pending events from the netlink socket every time
824** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800825** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700826*/
827
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800828static coldboot_action_t do_coldboot(DIR *d, coldboot_callback fn)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700829{
830 struct dirent *de;
831 int dfd, fd;
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800832 coldboot_action_t act = COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700833
834 dfd = dirfd(d);
835
836 fd = openat(dfd, "uevent", O_WRONLY);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800837 if (fd >= 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700838 write(fd, "add\n", 4);
839 close(fd);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800840 act = handle_device_fd(fn);
841 if (should_stop_coldboot(act))
842 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700843 }
844
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800845 while (!should_stop_coldboot(act) && (de = readdir(d))) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700846 DIR *d2;
847
848 if(de->d_type != DT_DIR || de->d_name[0] == '.')
849 continue;
850
851 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
852 if(fd < 0)
853 continue;
854
855 d2 = fdopendir(fd);
856 if(d2 == 0)
857 close(fd);
858 else {
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800859 act = do_coldboot(d2, fn);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700860 closedir(d2);
861 }
862 }
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800863
864 // default is always to continue looking for uevents
865 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700866}
867
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800868static coldboot_action_t coldboot(const char *path, coldboot_callback fn)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700869{
James Hawkins588a2ca2016-02-18 14:52:46 -0800870 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800871 if (d) {
872 return do_coldboot(d.get(), fn);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700873 }
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800874
875 return COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700876}
877
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800878void device_init(const char* path, coldboot_callback fn) {
Sandeep Patil971a4602017-02-15 13:37:52 -0800879 if (!sehandle) {
880 sehandle = selinux_android_file_context_handle();
Elliott Hughes56a06562015-03-28 11:23:32 -0700881 }
Sandeep Patil971a4602017-02-15 13:37:52 -0800882 // open uevent socket and selinux status only if it hasn't been
883 // done before
884 if (device_fd == -1) {
885 /* is 256K enough? udev uses 16MB! */
886 device_fd.reset(uevent_open_socket(256 * 1024, true));
887 if (device_fd == -1) {
888 return;
889 }
890 fcntl(device_fd, F_SETFL, O_NONBLOCK);
891 selinux_status_open(true);
892 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700893
Elliott Hughes56a06562015-03-28 11:23:32 -0700894 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700895 LOG(VERBOSE) << "Skipping coldboot, already done!";
Elliott Hughes56a06562015-03-28 11:23:32 -0700896 return;
Colin Crossf83d0b92010-04-21 12:04:20 -0700897 }
Elliott Hughes56a06562015-03-28 11:23:32 -0700898
899 Timer t;
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800900 coldboot_action_t act;
901 if (!path) {
902 act = coldboot("/sys/class", fn);
903 if (!should_stop_coldboot(act)) {
904 act = coldboot("/sys/block", fn);
905 if (!should_stop_coldboot(act)) {
906 act = coldboot("/sys/devices", fn);
907 }
908 }
909 } else {
910 act = coldboot(path, fn);
911 }
912
913 // If we have a callback, then do as it says. If no, then the default is
914 // to always create COLDBOOT_DONE file.
915 if (!fn || (act == COLDBOOT_FINISH)) {
916 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
917 }
918
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000919 LOG(INFO) << "Coldboot took " << t;
Colin Cross0dd7ca62010-04-13 19:25:51 -0700920}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700921
Sandeep Patil35403eb2017-02-08 20:27:12 -0800922void device_close() {
923 destroy_platform_devices();
924 device_fd.reset();
Sandeep Patil971a4602017-02-15 13:37:52 -0800925 selinux_status_close();
Sandeep Patil35403eb2017-02-08 20:27:12 -0800926}
927
Elliott Hughes632e99a2016-11-12 11:44:16 -0800928int get_device_fd() {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700929 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700930}