blob: 6cd35648ace4ab060797a167b2daf4c632327399 [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"
Daniel Rosenbergd1d96022015-03-20 15:10:29 -070059static const char *firmware_dirs[] = { "/etc/firmware",
60 "/vendor/firmware",
61 "/firmware/image" };
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070062
Stephen Smalleye096e362012-06-11 13:37:39 -040063extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050064
Sandeep Patil35403eb2017-02-08 20:27:12 -080065static android::base::unique_fd device_fd;
Colin Cross0dd7ca62010-04-13 19:25:51 -070066
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070067struct perms_ {
68 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070069 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070 mode_t perm;
71 unsigned int uid;
72 unsigned int gid;
73 unsigned short prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070074 unsigned short wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070077struct perm_node {
78 struct perms_ dp;
79 struct listnode plist;
80};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070081
Colin Crossfadb85e2011-03-30 18:32:12 -070082struct platform_node {
83 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080084 char *path;
85 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070086 struct listnode list;
87};
88
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070089static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070090static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070091static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070092
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070093int add_dev_perms(const char *name, const char *attr,
94 mode_t perm, unsigned int uid, unsigned int gid,
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070095 unsigned short prefix,
96 unsigned short wildcard) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -080097 struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070098 if (!node)
99 return -ENOMEM;
100
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700101 node->dp.name = strdup(name);
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800102 if (!node->dp.name) {
103 free(node);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700104 return -ENOMEM;
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800105 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700106
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700107 if (attr) {
108 node->dp.attr = strdup(attr);
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800109 if (!node->dp.attr) {
110 free(node->dp.name);
111 free(node);
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700112 return -ENOMEM;
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800113 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700114 }
115
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700116 node->dp.perm = perm;
117 node->dp.uid = uid;
118 node->dp.gid = gid;
119 node->dp.prefix = prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700120 node->dp.wildcard = wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700121
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700122 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 Project4f6e8d72008-10-21 07:00:00 -0700127 return 0;
128}
129
Colin Cross43d537e2014-07-02 13:08:13 -0700130static 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 Herring6de783a2016-05-06 10:06:59 -0500146static 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 Herringe5636a32016-05-06 12:28:48 -0500151
Rob Herring6de783a2016-05-06 10:06:59 -0500152 std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path));
153 return perm_path_matches(subsys_path.c_str(), dp);
154}
Rob Herringe5636a32016-05-06 12:28:48 -0500155
Rob Herring6de783a2016-05-06 10:06:59 -0500156static 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 Herringe5636a32016-05-06 12:28:48 -0500162 list_for_each(node, &sys_perms) {
Rob Herring6de783a2016-05-06 10:06:59 -0500163 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 Herringe5636a32016-05-06 12:28:48 -0500170 }
171
172 std::string attr_file = path + "/" + dp->attr;
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700173 LOG(INFO) << "fixup " << attr_file
174 << " " << dp->uid << " " << dp->gid << " " << std::oct << dp->perm;
Rob Herringe5636a32016-05-06 12:28:48 -0500175 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 Shmidt7eed4742016-07-28 13:55:39 -0700180 LOG(VERBOSE) << "restorecon_recursive: " << path;
Paul Lawrencea8d84342016-11-14 15:40:18 -0800181 restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE);
Rob Herringe5636a32016-05-06 12:28:48 -0500182 }
183}
184
Tom Cherry2e344f92017-04-04 17:53:45 -0700185static mode_t get_device_perm(const char* path, const std::vector<std::string>& links,
186 unsigned* uid, unsigned* gid) {
Colin Cross44b65d02010-04-20 14:32:50 -0700187 struct listnode *node;
188 struct perm_node *perm_node;
189 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700190
Colin Cross44b65d02010-04-20 14:32:50 -0700191 /* search the perms list in reverse so that ueventd.$hardware can
192 * override ueventd.rc
193 */
194 list_for_each_reverse(node, &dev_perms) {
195 perm_node = node_to_item(node, struct perm_node, plist);
196 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700197
Tom Cherry2e344f92017-04-04 17:53:45 -0700198 if (perm_path_matches(path, dp) ||
199 std::any_of(links.begin(), links.end(),
200 [dp](const auto& link) { return perm_path_matches(link.c_str(), dp); })) {
Colin Cross43d537e2014-07-02 13:08:13 -0700201 *uid = dp->uid;
202 *gid = dp->gid;
203 return dp->perm;
204 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700205 }
Colin Cross44b65d02010-04-20 14:32:50 -0700206 /* Default if nothing found. */
207 *uid = 0;
208 *gid = 0;
209 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700210}
211
Tom Cherry2e344f92017-04-04 17:53:45 -0700212static void make_device(const char* path, const char* /*upath*/, int block, int major, int minor,
213 const std::vector<std::string>& links) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700214 unsigned uid;
215 unsigned gid;
216 mode_t mode;
217 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500218 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700219
Colin Cross43d537e2014-07-02 13:08:13 -0700220 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700221
Sandeep Patilea239832017-02-03 07:51:55 -0800222 if (sehandle) {
Tom Cherry2e344f92017-04-04 17:53:45 -0700223 std::vector<const char*> c_links;
224 for (const auto& link : links) {
225 c_links.emplace_back(link.c_str());
226 }
227 c_links.emplace_back(nullptr);
228 if (selabel_lookup_best_match(sehandle, &secontext, path, &c_links[0], mode)) {
Sandeep Patilea239832017-02-03 07:51:55 -0800229 PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
230 return;
231 }
232 setfscreatecon(secontext);
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300233 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700234
Colin Cross17dcc5c2010-09-03 12:25:34 -0700235 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800236 /* Temporarily change egid to avoid race condition setting the gid of the
237 * device node. Unforunately changing the euid would prevent creation of
238 * some device nodes, so the uid has to be set with chown() and is still
239 * racy. Fixing the gid race at least fixed the issue with system_server
240 * opening dynamic input devices under the AID_INPUT gid. */
Tom Cherry0506b182017-02-23 13:46:09 -0800241 if (setegid(gid)) {
242 PLOG(ERROR) << "setegid(" << gid << ") for " << path << " device failed";
243 goto out;
244 }
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300245 /* If the node already exists update its SELinux label to handle cases when
246 * it was created with the wrong context during coldboot procedure. */
Sandeep Patilea239832017-02-03 07:51:55 -0800247 if (mknod(path, mode, dev) && (errno == EEXIST) && secontext) {
William Roberts397de142016-06-02 09:53:44 -0700248
249 char* fcon = nullptr;
250 int rc = lgetfilecon(path, &fcon);
251 if (rc < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700252 PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
William Roberts397de142016-06-02 09:53:44 -0700253 goto out;
254 }
255
256 bool different = strcmp(fcon, secontext) != 0;
257 freecon(fcon);
258
259 if (different && lsetfilecon(path, secontext)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700260 PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path << "' device";
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300261 }
262 }
William Roberts397de142016-06-02 09:53:44 -0700263
264out:
Nick Pelly6405c692010-01-21 18:13:39 -0800265 chown(path, uid, -1);
Tom Cherry0506b182017-02-23 13:46:09 -0800266 if (setegid(AID_ROOT)) {
267 PLOG(FATAL) << "setegid(AID_ROOT) failed";
268 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700269
Sandeep Patilea239832017-02-03 07:51:55 -0800270 if (secontext) {
271 freecon(secontext);
272 setfscreatecon(NULL);
273 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700274}
275
Tom Cherryc44f6a42017-04-05 15:58:31 -0700276void add_platform_device(const char* path) {
Dima Zavinf395c922013-03-06 16:23:57 -0800277 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700278 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800279 const char *name = path;
280
281 if (!strncmp(path, "/devices/", 9)) {
282 name += 9;
283 if (!strncmp(name, "platform/", 9))
284 name += 9;
285 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700286
Wei Wanga285dac2016-10-04 14:05:39 -0700287 LOG(VERBOSE) << "adding platform device " << name << " (" << path << ")";
Colin Crossfadb85e2011-03-30 18:32:12 -0700288
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800289 bus = (platform_node*) calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800290 bus->path = strdup(path);
291 bus->path_len = path_len;
292 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700293 list_add_tail(&platform_names, &bus->list);
294}
295
296/*
Dima Zavinf395c922013-03-06 16:23:57 -0800297 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700298 * platform device prefix. If it doesn't start with a platform device, return
299 * 0.
300 */
Dima Zavinf395c922013-03-06 16:23:57 -0800301static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700302{
Dima Zavinf395c922013-03-06 16:23:57 -0800303 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700304 struct listnode *node;
305 struct platform_node *bus;
306
307 list_for_each_reverse(node, &platform_names) {
308 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800309 if ((bus->path_len < path_len) &&
310 (path[bus->path_len] == '/') &&
311 !strncmp(path, bus->path, bus->path_len))
312 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700313 }
314
315 return NULL;
316}
317
Tom Cherryc44f6a42017-04-05 15:58:31 -0700318void remove_platform_device(const char* path) {
Colin Crossfadb85e2011-03-30 18:32:12 -0700319 struct listnode *node;
320 struct platform_node *bus;
321
322 list_for_each_reverse(node, &platform_names) {
323 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800324 if (!strcmp(path, bus->path)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700325 LOG(INFO) << "removing platform device " << bus->name;
Dima Zavinf395c922013-03-06 16:23:57 -0800326 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700327 list_remove(node);
328 free(bus);
329 return;
330 }
331 }
332}
333
Sandeep Patil35403eb2017-02-08 20:27:12 -0800334static void destroy_platform_devices() {
335 struct listnode* node;
336 struct listnode* n;
337 struct platform_node* bus;
338
339 list_for_each_safe(node, n, &platform_names) {
340 list_remove(node);
341 bus = node_to_item(node, struct platform_node, list);
342 free(bus->path);
343 free(bus);
344 }
345}
346
Andrew Boiea885d042013-09-13 17:41:20 -0700347/* Given a path that may start with a PCI device, populate the supplied buffer
348 * with the PCI domain/bus number and the peripheral ID and return 0.
349 * If it doesn't start with a PCI device, or there is some error, return -1 */
Tom Cherry2e344f92017-04-04 17:53:45 -0700350static bool find_pci_device_prefix(const std::string& path, std::string* result) {
351 result->clear();
Andrew Boiea885d042013-09-13 17:41:20 -0700352
Tom Cherry2e344f92017-04-04 17:53:45 -0700353 if (!android::base::StartsWith(path, "/devices/pci")) return false;
Andrew Boiea885d042013-09-13 17:41:20 -0700354
355 /* Beginning of the prefix is the initial "pci" after "/devices/" */
Tom Cherry2e344f92017-04-04 17:53:45 -0700356 std::string::size_type start = 9;
Andrew Boiea885d042013-09-13 17:41:20 -0700357
358 /* End of the prefix is two path '/' later, capturing the domain/bus number
359 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
Tom Cherry2e344f92017-04-04 17:53:45 -0700360 auto end = path.find('/', start);
361 if (end == std::string::npos) return false;
Andrew Boiea885d042013-09-13 17:41:20 -0700362
Tom Cherry2e344f92017-04-04 17:53:45 -0700363 end = path.find('/', end + 1);
364 if (end == std::string::npos) return false;
Andrew Boiea885d042013-09-13 17:41:20 -0700365
Tom Cherry2e344f92017-04-04 17:53:45 -0700366 auto length = end - start;
367 if (length <= 4) {
368 // The minimum string that will get to this check is 'pci/', which is malformed,
369 // so return false
370 return false;
371 }
372
373 *result = path.substr(start, length);
374 return true;
Andrew Boiea885d042013-09-13 17:41:20 -0700375}
376
Jeremy Compostella937309d2017-03-03 16:27:29 +0100377/* Given a path that may start with a virtual block device, populate
378 * the supplied buffer with the virtual block device ID and return 0.
379 * If it doesn't start with a virtual block device, or there is some
380 * error, return -1 */
Tom Cherry2e344f92017-04-04 17:53:45 -0700381static bool find_vbd_device_prefix(const std::string& path, std::string* result) {
382 result->clear();
383
384 if (!android::base::StartsWith(path, "/devices/vbd-")) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100385
386 /* Beginning of the prefix is the initial "vbd-" after "/devices/" */
Tom Cherry2e344f92017-04-04 17:53:45 -0700387 std::string::size_type start = 13;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100388
389 /* End of the prefix is one path '/' later, capturing the
390 virtual block device ID. Example: 768 */
Tom Cherry2e344f92017-04-04 17:53:45 -0700391 auto end = path.find('/', start);
392 if (end == std::string::npos) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100393
Tom Cherry2e344f92017-04-04 17:53:45 -0700394 auto length = end - start;
395 if (length == 0) return false;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100396
Tom Cherry2e344f92017-04-04 17:53:45 -0700397 *result = path.substr(start, length);
398 return true;
Jeremy Compostella937309d2017-03-03 16:27:29 +0100399}
400
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700401static void parse_event(const char *msg, struct uevent *uevent)
402{
403 uevent->action = "";
404 uevent->path = "";
405 uevent->subsystem = "";
406 uevent->firmware = "";
407 uevent->major = -1;
408 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700409 uevent->partition_name = NULL;
410 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700411 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700412
413 /* currently ignoring SEQNUM */
414 while(*msg) {
415 if(!strncmp(msg, "ACTION=", 7)) {
416 msg += 7;
417 uevent->action = msg;
418 } else if(!strncmp(msg, "DEVPATH=", 8)) {
419 msg += 8;
420 uevent->path = msg;
421 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
422 msg += 10;
423 uevent->subsystem = msg;
424 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
425 msg += 9;
426 uevent->firmware = msg;
427 } else if(!strncmp(msg, "MAJOR=", 6)) {
428 msg += 6;
429 uevent->major = atoi(msg);
430 } else if(!strncmp(msg, "MINOR=", 6)) {
431 msg += 6;
432 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700433 } else if(!strncmp(msg, "PARTN=", 6)) {
434 msg += 6;
435 uevent->partition_num = atoi(msg);
436 } else if(!strncmp(msg, "PARTNAME=", 9)) {
437 msg += 9;
438 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700439 } else if(!strncmp(msg, "DEVNAME=", 8)) {
440 msg += 8;
441 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700442 }
443
Wei Zhongf97b8872012-03-23 14:15:34 -0700444 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700445 while(*msg++)
446 ;
447 }
448
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800449 if (LOG_UEVENTS) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700450 LOG(INFO) << android::base::StringPrintf("event { '%s', '%s', '%s', '%s', %d, %d }",
451 uevent->action, uevent->path, uevent->subsystem,
452 uevent->firmware, uevent->major, uevent->minor);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800453 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700454}
455
Tom Cherry2e344f92017-04-04 17:53:45 -0700456std::vector<std::string> get_character_device_symlinks(uevent* uevent) {
457 platform_node* pdev = find_platform_device(uevent->path);
458 if (!pdev) return {};
Benoit Gobyd2278632010-08-03 14:36:02 -0700459
460 /* skip "/devices/platform/<driver>" */
Tom Cherry2e344f92017-04-04 17:53:45 -0700461 std::string parent = std::string(uevent->path);
462 auto parent_start = parent.find('/', pdev->path_len);
463 if (parent_start == std::string::npos) return {};
Benoit Gobyd2278632010-08-03 14:36:02 -0700464
Tom Cherry2e344f92017-04-04 17:53:45 -0700465 parent.erase(0, parent_start);
Benoit Gobyd2278632010-08-03 14:36:02 -0700466
Tom Cherry2e344f92017-04-04 17:53:45 -0700467 if (!android::base::StartsWith(parent, "/usb")) return {};
468
469 // skip root hub name and device. use device interface
470 // skip 3 slashes, including the first / by starting the search at the 1st character, not 0th.
471 // then extract what comes between the 3rd and 4th slash
472 // e.g. "/usb/usb_device/name/tty2-1:1.0" -> "name"
473
474 std::string::size_type start = 0;
475 start = parent.find('/', start + 1);
476 if (start == std::string::npos) return {};
477
478 start = parent.find('/', start + 1);
479 if (start == std::string::npos) return {};
480
481 auto end = parent.find('/', start + 1);
482 if (end == std::string::npos) return {};
483
484 start++; // Skip the first '/'
485
486 auto length = end - start;
487 if (length == 0) return {};
488
489 auto name_string = parent.substr(start, length);
490
491 // TODO: remove std::string() when uevent->subsystem is an std::string
492 std::vector<std::string> links;
493 links.emplace_back("/dev/usb/" + std::string(uevent->subsystem) + name_string);
494
495 mkdir("/dev/usb", 0755);
Benoit Gobyd2278632010-08-03 14:36:02 -0700496
497 return links;
Benoit Gobyd2278632010-08-03 14:36:02 -0700498}
499
Tom Cherryc44f6a42017-04-05 15:58:31 -0700500// replaces any unacceptable characters with '_', the
501// length of the resulting string is equal to the input string
Tom Cherry2e344f92017-04-04 17:53:45 -0700502void sanitize_partition_name(std::string* string) {
Tom Cherryc44f6a42017-04-05 15:58:31 -0700503 const char* accept =
504 "abcdefghijklmnopqrstuvwxyz"
505 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
506 "0123456789"
507 "_-.";
508
Tom Cherry2e344f92017-04-04 17:53:45 -0700509 if (!string) return;
Tom Cherryc44f6a42017-04-05 15:58:31 -0700510
Tom Cherry2e344f92017-04-04 17:53:45 -0700511 std::string::size_type pos = 0;
512 while ((pos = string->find_first_not_of(accept, pos)) != std::string::npos) {
513 (*string)[pos] = '_';
Tom Cherryc44f6a42017-04-05 15:58:31 -0700514 }
515}
516
Tom Cherry2e344f92017-04-04 17:53:45 -0700517std::vector<std::string> get_block_device_symlinks(uevent* uevent) {
518 std::string device;
519 struct platform_node* pdev;
520 std::string type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700521
Dima Zavinf395c922013-03-06 16:23:57 -0800522 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700523 if (pdev) {
524 device = pdev->name;
525 type = "platform";
Tom Cherry2e344f92017-04-04 17:53:45 -0700526 } else if (find_pci_device_prefix(uevent->path, &device)) {
Andrew Boiea885d042013-09-13 17:41:20 -0700527 type = "pci";
Tom Cherry2e344f92017-04-04 17:53:45 -0700528 } else if (find_vbd_device_prefix(uevent->path, &device)) {
Jeremy Compostella937309d2017-03-03 16:27:29 +0100529 type = "vbd";
Andrew Boiea885d042013-09-13 17:41:20 -0700530 } else {
Tom Cherry2e344f92017-04-04 17:53:45 -0700531 return {};
Andrew Boiea885d042013-09-13 17:41:20 -0700532 }
Dima Zavinf395c922013-03-06 16:23:57 -0800533
Tom Cherry2e344f92017-04-04 17:53:45 -0700534 std::vector<std::string> links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700535
Sandeep Patil35403eb2017-02-08 20:27:12 -0800536 LOG(VERBOSE) << "found " << type << " device " << device;
Colin Crossfadb85e2011-03-30 18:32:12 -0700537
Tom Cherry2e344f92017-04-04 17:53:45 -0700538 auto link_path = "/dev/block/" + type + "/" + device;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700539
540 if (uevent->partition_name) {
Tom Cherry2e344f92017-04-04 17:53:45 -0700541 std::string partition_name_sanitized(uevent->partition_name);
542 sanitize_partition_name(&partition_name_sanitized);
543 if (partition_name_sanitized != uevent->partition_name) {
544 LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '"
545 << partition_name_sanitized << "'";
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700546 }
Tom Cherry2e344f92017-04-04 17:53:45 -0700547 links.emplace_back(link_path + "/by-name/" + partition_name_sanitized);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700548 }
549
550 if (uevent->partition_num >= 0) {
Tom Cherry2e344f92017-04-04 17:53:45 -0700551 links.emplace_back(link_path + "/by-num/p" + std::to_string(uevent->partition_num));
Colin Crossb0ab94b2010-04-08 16:16:20 -0700552 }
553
Tom Cherry2e344f92017-04-04 17:53:45 -0700554 // TODO: remove uevent_path when uevent->path is an std::string
555 std::string uevent_path = uevent->path;
556 auto last_slash = uevent_path.rfind('/');
557 links.emplace_back(link_path + "/" + uevent_path.substr(last_slash + 1));
Colin Crossb0ab94b2010-04-08 16:16:20 -0700558
559 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700560}
561
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700562static void make_link_init(const char* oldpath, const char* newpath) {
563 const char* slash = strrchr(newpath, '/');
564 if (!slash) return;
565
566 if (mkdir_recursive(dirname(newpath), 0755)) {
567 PLOG(ERROR) << "Failed to create directory " << dirname(newpath);
568 }
569
570 if (symlink(oldpath, newpath) && errno != EEXIST) {
571 PLOG(ERROR) << "Failed to symlink " << oldpath << " to " << newpath;
572 }
573}
574
575static void remove_link(const char* oldpath, const char* newpath) {
576 std::string path;
577 if (android::base::Readlink(newpath, &path) && path == oldpath) unlink(newpath);
578}
579
Tom Cherry2e344f92017-04-04 17:53:45 -0700580static void handle_device(const char* action, const char* devpath, const char* path, int block,
581 int major, int minor, const std::vector<std::string>& links) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700582 if(!strcmp(action, "add")) {
Tom Cherry2e344f92017-04-04 17:53:45 -0700583 make_device(devpath, path, block, major, minor, links);
584 for (const auto& link : links) {
585 make_link_init(devpath, link.c_str());
Colin Crossb0ab94b2010-04-08 16:16:20 -0700586 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700587 }
588
Colin Crosseb5ba832011-03-30 17:37:17 -0700589 if(!strcmp(action, "remove")) {
Tom Cherry2e344f92017-04-04 17:53:45 -0700590 for (const auto& link : links) {
591 remove_link(devpath, link.c_str());
Colin Crossb0ab94b2010-04-08 16:16:20 -0700592 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700593 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700594 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700595}
596
Colin Crossfadb85e2011-03-30 18:32:12 -0700597static void handle_platform_device_event(struct uevent *uevent)
598{
Dima Zavinf395c922013-03-06 16:23:57 -0800599 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700600
601 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800602 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700603 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800604 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700605}
606
Tom Cherry3fa46732017-04-11 14:19:50 -0700607static void handle_block_device_event(uevent* uevent) {
608 // if it's not a /dev device, nothing to do
609 if (uevent->major < 0 || uevent->minor < 0) return;
Colin Crosseb5ba832011-03-30 17:37:17 -0700610
Tom Cherry3fa46732017-04-11 14:19:50 -0700611 const char* base = "/dev/block/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500612 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700613
Tom Cherry3fa46732017-04-11 14:19:50 -0700614 std::string name = android::base::Basename(uevent->path);
615 std::string devpath = base + name;
616
Tom Cherry2e344f92017-04-04 17:53:45 -0700617 std::vector<std::string> links;
Dima Zavinf395c922013-03-06 16:23:57 -0800618 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700619 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700620
Tom Cherry3fa46732017-04-11 14:19:50 -0700621 handle_device(uevent->action, devpath.c_str(), uevent->path, 1, uevent->major, uevent->minor,
622 links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700623}
624
Tom Cherry3fa46732017-04-11 14:19:50 -0700625static void handle_generic_device_event(uevent* uevent) {
626 // if it's not a /dev device, nothing to do
627 if (uevent->major < 0 || uevent->minor < 0) return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800628
Tom Cherry3fa46732017-04-11 14:19:50 -0700629 std::string name = android::base::Basename(uevent->path);
630 ueventd_subsystem* subsystem = ueventd_subsystem_find_by_name(uevent->subsystem);
Greg Hackmann3312aa82013-11-18 15:24:40 -0800631
Tom Cherry3fa46732017-04-11 14:19:50 -0700632 std::string devpath;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800633
634 if (subsystem) {
Tom Cherry3fa46732017-04-11 14:19:50 -0700635 std::string devname;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800636
637 switch (subsystem->devname_src) {
638 case DEVNAME_UEVENT_DEVNAME:
639 devname = uevent->device_name;
640 break;
641
642 case DEVNAME_UEVENT_DEVPATH:
643 devname = name;
644 break;
645
646 default:
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700647 LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800648 return;
649 }
650
Tom Cherry3fa46732017-04-11 14:19:50 -0700651 // TODO: Remove std::string()
652 devpath = std::string(subsystem->dirname) + "/" + devname;
653 mkdir_recursive(android::base::Dirname(devpath), 0755);
Greg Hackmann3312aa82013-11-18 15:24:40 -0800654 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Tom Cherry3fa46732017-04-11 14:19:50 -0700655 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700656 if (uevent->device_name) {
Tom Cherry3fa46732017-04-11 14:19:50 -0700657 // TODO: Remove std::string
658 devpath = "/dev/" + std::string(uevent->device_name);
659 } else {
660 // This imitates the file system that would be created
661 // if we were using devfs instead.
662 // Minors are broken up into groups of 128, starting at "001"
663 int bus_id = uevent->minor / 128 + 1;
664 int device_id = uevent->minor % 128 + 1;
665 devpath = android::base::StringPrintf("/dev/bus/usb/%03d/%03d", bus_id, device_id);
666 }
667 mkdir_recursive(android::base::Dirname(devpath), 0755);
668 } else {
669 // ignore other USB events
670 return;
671 }
Tom Cherry780a71e2017-04-04 16:30:40 -0700672 } else {
Tom Cherry3fa46732017-04-11 14:19:50 -0700673 devpath = "/dev/" + name;
Tom Cherry780a71e2017-04-04 16:30:40 -0700674 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700675
Tom Cherry780a71e2017-04-04 16:30:40 -0700676 auto links = get_character_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700677
Tom Cherry3fa46732017-04-11 14:19:50 -0700678 handle_device(uevent->action, devpath.c_str(), uevent->path, 0, uevent->major, uevent->minor,
679 links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700680}
681
682static void handle_device_event(struct uevent *uevent)
683{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700684 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Rob Herring6de783a2016-05-06 10:06:59 -0500685 fixup_sys_perms(uevent->path, uevent->subsystem);
Colin Crosseb5ba832011-03-30 17:37:17 -0700686
687 if (!strncmp(uevent->subsystem, "block", 5)) {
688 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700689 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
690 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700691 } else {
692 handle_generic_device_event(uevent);
693 }
694}
695
Elliott Hughes632e99a2016-11-12 11:44:16 -0800696static void load_firmware(uevent* uevent, const std::string& root,
697 int fw_fd, size_t fw_size,
698 int loading_fd, int data_fd) {
699 // Start transfer.
700 android::base::WriteFully(loading_fd, "1", 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700701
Elliott Hughes632e99a2016-11-12 11:44:16 -0800702 // Copy the firmware.
703 int rc = sendfile(data_fd, fw_fd, nullptr, fw_size);
704 if (rc == -1) {
705 PLOG(ERROR) << "firmware: sendfile failed { '" << root << "', '" << uevent->firmware << "' }";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700706 }
707
Elliott Hughes632e99a2016-11-12 11:44:16 -0800708 // Tell the firmware whether to abort or commit.
709 const char* response = (rc != -1) ? "0" : "-1";
710 android::base::WriteFully(loading_fd, response, strlen(response));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700711}
712
Elliott Hughes632e99a2016-11-12 11:44:16 -0800713static int is_booting() {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700714 return access("/dev/.booting", F_OK) == 0;
715}
716
Elliott Hughes632e99a2016-11-12 11:44:16 -0800717static void process_firmware_event(uevent* uevent) {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700718 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700719
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700720 LOG(INFO) << "firmware: loading '" << uevent->firmware << "' for '" << uevent->path << "'";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700721
Elliott Hughes632e99a2016-11-12 11:44:16 -0800722 std::string root = android::base::StringPrintf("/sys%s", uevent->path);
723 std::string loading = root + "/loading";
724 std::string data = root + "/data";
725
726 android::base::unique_fd loading_fd(open(loading.c_str(), O_WRONLY|O_CLOEXEC));
727 if (loading_fd == -1) {
728 PLOG(ERROR) << "couldn't open firmware loading fd for " << uevent->firmware;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700729 return;
Elliott Hughes632e99a2016-11-12 11:44:16 -0800730 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700731
Elliott Hughes632e99a2016-11-12 11:44:16 -0800732 android::base::unique_fd data_fd(open(data.c_str(), O_WRONLY|O_CLOEXEC));
733 if (data_fd == -1) {
734 PLOG(ERROR) << "couldn't open firmware data fd for " << uevent->firmware;
735 return;
736 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700737
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700738try_loading_again:
Elliott Hughes632e99a2016-11-12 11:44:16 -0800739 for (size_t i = 0; i < arraysize(firmware_dirs); i++) {
740 std::string file = android::base::StringPrintf("%s/%s", firmware_dirs[i], uevent->firmware);
741 android::base::unique_fd fw_fd(open(file.c_str(), O_RDONLY|O_CLOEXEC));
742 struct stat sb;
743 if (fw_fd != -1 && fstat(fw_fd, &sb) != -1) {
744 load_firmware(uevent, root, fw_fd, sb.st_size, loading_fd, data_fd);
745 return;
Benoit Goby609d8822010-11-09 18:10:24 -0800746 }
Brian Swetland02863b92010-09-19 03:36:39 -0700747 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700748
Elliott Hughes632e99a2016-11-12 11:44:16 -0800749 if (booting) {
750 // If we're not fully booted, we may be missing
751 // filesystems needed for firmware, wait and retry.
Elliott Hughes290a2282016-11-14 17:08:47 -0800752 std::this_thread::sleep_for(100ms);
Elliott Hughes632e99a2016-11-12 11:44:16 -0800753 booting = is_booting();
754 goto try_loading_again;
755 }
756
757 LOG(ERROR) << "firmware: could not find firmware for " << uevent->firmware;
758
759 // Write "-1" as our response to the kernel's firmware request, since we have nothing for it.
760 write(loading_fd, "-1", 2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700761}
762
Elliott Hughes632e99a2016-11-12 11:44:16 -0800763static void handle_firmware_event(uevent* uevent) {
764 if (strcmp(uevent->subsystem, "firmware")) return;
765 if (strcmp(uevent->action, "add")) return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700766
Elliott Hughes632e99a2016-11-12 11:44:16 -0800767 // Loading the firmware in a child means we can do that in parallel...
768 // (We ignore SIGCHLD rather than wait for our children.)
769 pid_t pid = fork();
770 if (pid == 0) {
771 Timer t;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700772 process_firmware_event(uevent);
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000773 LOG(INFO) << "loading " << uevent->path << " took " << t;
Kenny Root17baff42014-08-20 16:16:44 -0700774 _exit(EXIT_SUCCESS);
Elliott Hughes632e99a2016-11-12 11:44:16 -0800775 } else if (pid == -1) {
776 PLOG(ERROR) << "could not fork to process firmware event for " << uevent->firmware;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700777 }
778}
779
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800780static bool inline should_stop_coldboot(coldboot_action_t act)
781{
782 return (act == COLDBOOT_STOP || act == COLDBOOT_FINISH);
783}
784
Ruchi Kandoic6037202014-06-23 11:22:09 -0700785#define UEVENT_MSG_LEN 2048
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800786
Sandeep Patil44a3ee22017-02-08 15:49:47 -0800787static inline coldboot_action_t handle_device_fd_with(
788 std::function<coldboot_action_t(uevent* uevent)> handle_uevent)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700789{
Vernon Tang3f582e92011-04-25 13:08:17 +1000790 char msg[UEVENT_MSG_LEN+2];
791 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700792 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700793 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700794 continue;
795
796 msg[n] = '\0';
797 msg[n+1] = '\0';
798
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700799 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700800 parse_event(msg, &uevent);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800801 coldboot_action_t act = handle_uevent(&uevent);
802 if (should_stop_coldboot(act))
803 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700804 }
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800805
806 return COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700807}
808
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800809coldboot_action_t handle_device_fd(coldboot_callback fn)
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800810{
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800811 coldboot_action_t ret = handle_device_fd_with(
812 [&](uevent* uevent) -> coldboot_action_t {
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800813 if (selinux_status_updated() > 0) {
814 struct selabel_handle *sehandle2;
815 sehandle2 = selinux_android_file_context_handle();
816 if (sehandle2) {
817 selabel_close(sehandle);
818 sehandle = sehandle2;
819 }
820 }
821
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800822 // default is to always create the devices
823 coldboot_action_t act = COLDBOOT_CREATE;
824 if (fn) {
825 act = fn(uevent);
826 }
827
828 if (act == COLDBOOT_CREATE || act == COLDBOOT_STOP) {
829 handle_device_event(uevent);
830 handle_firmware_event(uevent);
831 }
832
833 return act;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800834 });
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800835
836 return ret;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800837}
838
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700839/* Coldboot walks parts of the /sys tree and pokes the uevent files
840** to cause the kernel to regenerate device add events that happened
841** before init's device manager was started
842**
843** We drain any pending events from the netlink socket every time
844** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800845** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700846*/
847
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800848static coldboot_action_t do_coldboot(DIR *d, coldboot_callback fn)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700849{
850 struct dirent *de;
851 int dfd, fd;
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800852 coldboot_action_t act = COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700853
854 dfd = dirfd(d);
855
856 fd = openat(dfd, "uevent", O_WRONLY);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800857 if (fd >= 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700858 write(fd, "add\n", 4);
859 close(fd);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800860 act = handle_device_fd(fn);
861 if (should_stop_coldboot(act))
862 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700863 }
864
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800865 while (!should_stop_coldboot(act) && (de = readdir(d))) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700866 DIR *d2;
867
868 if(de->d_type != DT_DIR || de->d_name[0] == '.')
869 continue;
870
871 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
872 if(fd < 0)
873 continue;
874
875 d2 = fdopendir(fd);
876 if(d2 == 0)
877 close(fd);
878 else {
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800879 act = do_coldboot(d2, fn);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700880 closedir(d2);
881 }
882 }
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800883
884 // default is always to continue looking for uevents
885 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700886}
887
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800888static coldboot_action_t coldboot(const char *path, coldboot_callback fn)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700889{
James Hawkins588a2ca2016-02-18 14:52:46 -0800890 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800891 if (d) {
892 return do_coldboot(d.get(), fn);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700893 }
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800894
895 return COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700896}
897
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800898void device_init(const char* path, coldboot_callback fn) {
Sandeep Patil971a4602017-02-15 13:37:52 -0800899 if (!sehandle) {
900 sehandle = selinux_android_file_context_handle();
Elliott Hughes56a06562015-03-28 11:23:32 -0700901 }
Sandeep Patil971a4602017-02-15 13:37:52 -0800902 // open uevent socket and selinux status only if it hasn't been
903 // done before
904 if (device_fd == -1) {
905 /* is 256K enough? udev uses 16MB! */
906 device_fd.reset(uevent_open_socket(256 * 1024, true));
907 if (device_fd == -1) {
908 return;
909 }
910 fcntl(device_fd, F_SETFL, O_NONBLOCK);
911 selinux_status_open(true);
912 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700913
Elliott Hughes56a06562015-03-28 11:23:32 -0700914 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700915 LOG(VERBOSE) << "Skipping coldboot, already done!";
Elliott Hughes56a06562015-03-28 11:23:32 -0700916 return;
Colin Crossf83d0b92010-04-21 12:04:20 -0700917 }
Elliott Hughes56a06562015-03-28 11:23:32 -0700918
919 Timer t;
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800920 coldboot_action_t act;
921 if (!path) {
922 act = coldboot("/sys/class", fn);
923 if (!should_stop_coldboot(act)) {
924 act = coldboot("/sys/block", fn);
925 if (!should_stop_coldboot(act)) {
926 act = coldboot("/sys/devices", fn);
927 }
928 }
929 } else {
930 act = coldboot(path, fn);
931 }
932
933 // If we have a callback, then do as it says. If no, then the default is
934 // to always create COLDBOOT_DONE file.
935 if (!fn || (act == COLDBOOT_FINISH)) {
936 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
937 }
938
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000939 LOG(INFO) << "Coldboot took " << t;
Colin Cross0dd7ca62010-04-13 19:25:51 -0700940}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700941
Sandeep Patil35403eb2017-02-08 20:27:12 -0800942void device_close() {
943 destroy_platform_devices();
944 device_fd.reset();
Sandeep Patil971a4602017-02-15 13:37:52 -0800945 selinux_status_close();
Sandeep Patil35403eb2017-02-08 20:27:12 -0800946}
947
Elliott Hughes632e99a2016-11-12 11:44:16 -0800948int get_device_fd() {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700949 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700950}