blob: 1410e3bdefe5e3fe601c305481703e41062fd8e3 [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
17#include <errno.h>
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070018#include <fnmatch.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -080019#include <stddef.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070020#include <stdio.h>
21#include <stdlib.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24
25#include <fcntl.h>
26#include <dirent.h>
27#include <unistd.h>
28#include <string.h>
29
30#include <sys/socket.h>
31#include <sys/un.h>
32#include <linux/netlink.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050033
James Hawkins588a2ca2016-02-18 14:52:46 -080034#include <memory>
35
Stephen Smalleye46f9d52012-01-13 08:48:47 -050036#include <selinux/selinux.h>
37#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040038#include <selinux/android.h>
Stephen Smalleye2eb69d2013-04-16 09:30:30 -040039#include <selinux/avc.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050040
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070041#include <private/android_filesystem_config.h>
42#include <sys/time.h>
Colin Cross982a8152010-06-03 12:21:01 -070043#include <sys/wait.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070044
Biao Ludc848562016-01-28 16:10:54 +080045#include <android-base/file.h>
Rob Herring6de783a2016-05-06 10:06:59 -050046#include <android-base/stringprintf.h>
Dima Zavinda04c522011-09-01 17:09:44 -070047#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100048#include <cutils/uevent.h>
49
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070050#include "devices.h"
Greg Hackmann3312aa82013-11-18 15:24:40 -080051#include "ueventd_parser.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070052#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070053#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070055#define SYSFS_PREFIX "/sys"
Daniel Rosenbergd1d96022015-03-20 15:10:29 -070056static const char *firmware_dirs[] = { "/etc/firmware",
57 "/vendor/firmware",
58 "/firmware/image" };
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
Colin Cross0dd7ca62010-04-13 19:25:51 -070062static int device_fd = -1;
63
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064struct uevent {
65 const char *action;
66 const char *path;
67 const char *subsystem;
68 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070069 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070070 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070071 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072 int major;
73 int minor;
74};
75
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076struct perms_ {
77 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070078 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070079 mode_t perm;
80 unsigned int uid;
81 unsigned int gid;
82 unsigned short prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070083 unsigned short wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070084};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070085
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070086struct perm_node {
87 struct perms_ dp;
88 struct listnode plist;
89};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070090
Colin Crossfadb85e2011-03-30 18:32:12 -070091struct platform_node {
92 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080093 char *path;
94 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070095 struct listnode list;
96};
97
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070098static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070099static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -0700100static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700101
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700102int add_dev_perms(const char *name, const char *attr,
103 mode_t perm, unsigned int uid, unsigned int gid,
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700104 unsigned short prefix,
105 unsigned short wildcard) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800106 struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700107 if (!node)
108 return -ENOMEM;
109
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700110 node->dp.name = strdup(name);
111 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700112 return -ENOMEM;
113
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700114 if (attr) {
115 node->dp.attr = strdup(attr);
116 if (!node->dp.attr)
117 return -ENOMEM;
118 }
119
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700120 node->dp.perm = perm;
121 node->dp.uid = uid;
122 node->dp.gid = gid;
123 node->dp.prefix = prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700124 node->dp.wildcard = wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700125
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700126 if (attr)
127 list_add_tail(&sys_perms, &node->plist);
128 else
129 list_add_tail(&dev_perms, &node->plist);
130
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700131 return 0;
132}
133
Colin Cross43d537e2014-07-02 13:08:13 -0700134static bool perm_path_matches(const char *path, struct perms_ *dp)
135{
136 if (dp->prefix) {
137 if (strncmp(path, dp->name, strlen(dp->name)) == 0)
138 return true;
139 } else if (dp->wildcard) {
140 if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
141 return true;
142 } else {
143 if (strcmp(path, dp->name) == 0)
144 return true;
145 }
146
147 return false;
148}
149
Rob Herring6de783a2016-05-06 10:06:59 -0500150static bool match_subsystem(perms_* dp, const char* pattern,
151 const char* path, const char* subsystem) {
152 if (!pattern || !subsystem || strstr(dp->name, subsystem) == NULL) {
153 return false;
154 }
Rob Herringe5636a32016-05-06 12:28:48 -0500155
Rob Herring6de783a2016-05-06 10:06:59 -0500156 std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path));
157 return perm_path_matches(subsys_path.c_str(), dp);
158}
Rob Herringe5636a32016-05-06 12:28:48 -0500159
Rob Herring6de783a2016-05-06 10:06:59 -0500160static void fixup_sys_perms(const char* upath, const char* subsystem) {
161 // upaths omit the "/sys" that paths in this list
162 // contain, so we prepend it...
163 std::string path = std::string(SYSFS_PREFIX) + upath;
164
165 listnode* node;
Rob Herringe5636a32016-05-06 12:28:48 -0500166 list_for_each(node, &sys_perms) {
Rob Herring6de783a2016-05-06 10:06:59 -0500167 perms_* dp = &(node_to_item(node, perm_node, plist))->dp;
168 if (match_subsystem(dp, SYSFS_PREFIX "/class/%s/%s", path.c_str(), subsystem)) {
169 ; // matched
170 } else if (match_subsystem(dp, SYSFS_PREFIX "/bus/%s/devices/%s", path.c_str(), subsystem)) {
171 ; // matched
172 } else if (!perm_path_matches(path.c_str(), dp)) {
173 continue;
Rob Herringe5636a32016-05-06 12:28:48 -0500174 }
175
176 std::string attr_file = path + "/" + dp->attr;
177 INFO("fixup %s %d %d 0%o\n", attr_file.c_str(), dp->uid, dp->gid, dp->perm);
178 chown(attr_file.c_str(), dp->uid, dp->gid);
179 chmod(attr_file.c_str(), dp->perm);
180 }
181
182 if (access(path.c_str(), F_OK) == 0) {
183 INFO("restorecon_recursive: %s\n", path.c_str());
184 restorecon_recursive(path.c_str());
185 }
186}
187
Colin Cross43d537e2014-07-02 13:08:13 -0700188static mode_t get_device_perm(const char *path, const char **links,
189 unsigned *uid, unsigned *gid)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700190{
Colin Cross44b65d02010-04-20 14:32:50 -0700191 struct listnode *node;
192 struct perm_node *perm_node;
193 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700194
Colin Cross44b65d02010-04-20 14:32:50 -0700195 /* search the perms list in reverse so that ueventd.$hardware can
196 * override ueventd.rc
197 */
198 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700199 bool match = false;
200
Colin Cross44b65d02010-04-20 14:32:50 -0700201 perm_node = node_to_item(node, struct perm_node, plist);
202 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700203
Colin Cross43d537e2014-07-02 13:08:13 -0700204 if (perm_path_matches(path, dp)) {
205 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700206 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700207 if (links) {
208 int i;
209 for (i = 0; links[i]; i++) {
210 if (perm_path_matches(links[i], dp)) {
211 match = true;
212 break;
213 }
214 }
215 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700216 }
Colin Cross43d537e2014-07-02 13:08:13 -0700217
218 if (match) {
219 *uid = dp->uid;
220 *gid = dp->gid;
221 return dp->perm;
222 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700223 }
Colin Cross44b65d02010-04-20 14:32:50 -0700224 /* Default if nothing found. */
225 *uid = 0;
226 *gid = 0;
227 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700228}
229
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700230static void make_device(const char *path,
Elliott Hughesf682b472015-02-06 12:19:48 -0800231 const char */*upath*/,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400232 int block, int major, int minor,
233 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700234{
235 unsigned uid;
236 unsigned gid;
237 mode_t mode;
238 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500239 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700240
Colin Cross43d537e2014-07-02 13:08:13 -0700241 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700242
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300243 if (selabel_lookup_best_match(sehandle, &secontext, path, links, mode)) {
244 ERROR("Device '%s' not created; cannot find SELinux label (%s)\n",
245 path, strerror(errno));
246 return;
247 }
Nick Kralevich4d870952015-06-12 22:03:50 -0700248 setfscreatecon(secontext);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700249
Colin Cross17dcc5c2010-09-03 12:25:34 -0700250 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800251 /* Temporarily change egid to avoid race condition setting the gid of the
252 * device node. Unforunately changing the euid would prevent creation of
253 * some device nodes, so the uid has to be set with chown() and is still
254 * racy. Fixing the gid race at least fixed the issue with system_server
255 * opening dynamic input devices under the AID_INPUT gid. */
256 setegid(gid);
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300257 /* If the node already exists update its SELinux label to handle cases when
258 * it was created with the wrong context during coldboot procedure. */
259 if (mknod(path, mode, dev) && (errno == EEXIST)) {
260 if (lsetfilecon(path, secontext)) {
261 ERROR("Cannot set '%s' SELinux label on '%s' device (%s)\n",
262 secontext, path, strerror(errno));
263 }
264 }
Nick Pelly6405c692010-01-21 18:13:39 -0800265 chown(path, uid, -1);
266 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700267
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300268 freecon(secontext);
269 setfscreatecon(NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700270}
271
Dima Zavinf395c922013-03-06 16:23:57 -0800272static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700273{
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
Dima Zavinf395c922013-03-06 16:23:57 -0800284 INFO("adding platform device %s (%s)\n", 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 */
Dima Zavinf395c922013-03-06 16:23:57 -0800298static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700299{
Dima Zavinf395c922013-03-06 16:23:57 -0800300 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700301 struct listnode *node;
302 struct platform_node *bus;
303
304 list_for_each_reverse(node, &platform_names) {
305 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800306 if ((bus->path_len < path_len) &&
307 (path[bus->path_len] == '/') &&
308 !strncmp(path, bus->path, bus->path_len))
309 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700310 }
311
312 return NULL;
313}
314
Dima Zavinf395c922013-03-06 16:23:57 -0800315static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700316{
317 struct listnode *node;
318 struct platform_node *bus;
319
320 list_for_each_reverse(node, &platform_names) {
321 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800322 if (!strcmp(path, bus->path)) {
323 INFO("removing platform device %s\n", bus->name);
324 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700325 list_remove(node);
326 free(bus);
327 return;
328 }
329 }
330}
331
Andrew Boiea885d042013-09-13 17:41:20 -0700332/* Given a path that may start with a PCI device, populate the supplied buffer
333 * with the PCI domain/bus number and the peripheral ID and return 0.
334 * If it doesn't start with a PCI device, or there is some error, return -1 */
335static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
336{
337 const char *start, *end;
338
339 if (strncmp(path, "/devices/pci", 12))
340 return -1;
341
342 /* Beginning of the prefix is the initial "pci" after "/devices/" */
343 start = path + 9;
344
345 /* End of the prefix is two path '/' later, capturing the domain/bus number
346 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
347 end = strchr(start, '/');
348 if (!end)
349 return -1;
350 end = strchr(end + 1, '/');
351 if (!end)
352 return -1;
353
354 /* Make sure we have enough room for the string plus null terminator */
355 if (end - start + 1 > buf_sz)
356 return -1;
357
358 strncpy(buf, start, end - start);
359 buf[end - start] = '\0';
360 return 0;
361}
362
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700363static void parse_event(const char *msg, struct uevent *uevent)
364{
365 uevent->action = "";
366 uevent->path = "";
367 uevent->subsystem = "";
368 uevent->firmware = "";
369 uevent->major = -1;
370 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700371 uevent->partition_name = NULL;
372 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700373 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700374
375 /* currently ignoring SEQNUM */
376 while(*msg) {
377 if(!strncmp(msg, "ACTION=", 7)) {
378 msg += 7;
379 uevent->action = msg;
380 } else if(!strncmp(msg, "DEVPATH=", 8)) {
381 msg += 8;
382 uevent->path = msg;
383 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
384 msg += 10;
385 uevent->subsystem = msg;
386 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
387 msg += 9;
388 uevent->firmware = msg;
389 } else if(!strncmp(msg, "MAJOR=", 6)) {
390 msg += 6;
391 uevent->major = atoi(msg);
392 } else if(!strncmp(msg, "MINOR=", 6)) {
393 msg += 6;
394 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700395 } else if(!strncmp(msg, "PARTN=", 6)) {
396 msg += 6;
397 uevent->partition_num = atoi(msg);
398 } else if(!strncmp(msg, "PARTNAME=", 9)) {
399 msg += 9;
400 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700401 } else if(!strncmp(msg, "DEVNAME=", 8)) {
402 msg += 8;
403 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700404 }
405
Wei Zhongf97b8872012-03-23 14:15:34 -0700406 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700407 while(*msg++)
408 ;
409 }
410
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800411 if (LOG_UEVENTS) {
412 INFO("event { '%s', '%s', '%s', '%s', %d, %d }\n",
413 uevent->action, uevent->path, uevent->subsystem,
414 uevent->firmware, uevent->major, uevent->minor);
415 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700416}
417
Benoit Gobyd2278632010-08-03 14:36:02 -0700418static char **get_character_device_symlinks(struct uevent *uevent)
419{
420 const char *parent;
Dan Austin60b976d2016-03-28 14:22:12 -0700421 const char *slash;
Benoit Gobyd2278632010-08-03 14:36:02 -0700422 char **links;
423 int link_num = 0;
424 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800425 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700426
Dima Zavinf395c922013-03-06 16:23:57 -0800427 pdev = find_platform_device(uevent->path);
428 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700429 return NULL;
430
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800431 links = (char**) malloc(sizeof(char *) * 2);
Benoit Gobyd2278632010-08-03 14:36:02 -0700432 if (!links)
433 return NULL;
434 memset(links, 0, sizeof(char *) * 2);
435
436 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800437 parent = strchr(uevent->path + pdev->path_len, '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100438 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700439 goto err;
440
441 if (!strncmp(parent, "/usb", 4)) {
442 /* skip root hub name and device. use device interface */
443 while (*++parent && *parent != '/');
444 if (*parent)
445 while (*++parent && *parent != '/');
446 if (!*parent)
447 goto err;
448 slash = strchr(++parent, '/');
449 if (!slash)
450 goto err;
451 width = slash - parent;
452 if (width <= 0)
453 goto err;
454
455 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
456 link_num++;
457 else
458 links[link_num] = NULL;
459 mkdir("/dev/usb", 0755);
460 }
461 else {
462 goto err;
463 }
464
465 return links;
466err:
467 free(links);
468 return NULL;
469}
470
Andrew Boiea885d042013-09-13 17:41:20 -0700471static char **get_block_device_symlinks(struct uevent *uevent)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700472{
Colin Crossfadb85e2011-03-30 18:32:12 -0700473 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800474 struct platform_node *pdev;
Dan Austin60b976d2016-03-28 14:22:12 -0700475 const char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700476 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700477 char buf[256];
478 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700479 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700480 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700481
Dima Zavinf395c922013-03-06 16:23:57 -0800482 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700483 if (pdev) {
484 device = pdev->name;
485 type = "platform";
486 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
487 device = buf;
488 type = "pci";
489 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800490 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700491 }
Dima Zavinf395c922013-03-06 16:23:57 -0800492
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800493 char **links = (char**) malloc(sizeof(char *) * 4);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700494 if (!links)
495 return NULL;
496 memset(links, 0, sizeof(char *) * 4);
497
Andrew Boiea885d042013-09-13 17:41:20 -0700498 INFO("found %s device %s\n", type, device);
Colin Crossfadb85e2011-03-30 18:32:12 -0700499
Andrew Boiea885d042013-09-13 17:41:20 -0700500 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700501
502 if (uevent->partition_name) {
503 p = strdup(uevent->partition_name);
504 sanitize(p);
Johan Redestig93ca79b2012-04-18 16:41:19 +0200505 if (strcmp(uevent->partition_name, p))
506 NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700507 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
508 link_num++;
509 else
510 links[link_num] = NULL;
511 free(p);
512 }
513
514 if (uevent->partition_num >= 0) {
515 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
516 link_num++;
517 else
518 links[link_num] = NULL;
519 }
520
Dima Zavinf395c922013-03-06 16:23:57 -0800521 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700522 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
523 link_num++;
524 else
525 links[link_num] = NULL;
526
527 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700528}
529
Colin Crosseb5ba832011-03-30 17:37:17 -0700530static void handle_device(const char *action, const char *devpath,
531 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700532{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700533 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700534
Colin Crosseb5ba832011-03-30 17:37:17 -0700535 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400536 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700537 if (links) {
538 for (i = 0; links[i]; i++)
Chris Fries79f33842013-09-05 13:19:21 -0500539 make_link_init(devpath, links[i]);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700540 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700541 }
542
Colin Crosseb5ba832011-03-30 17:37:17 -0700543 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700544 if (links) {
545 for (i = 0; links[i]; i++)
546 remove_link(devpath, links[i]);
547 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700548 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700549 }
550
551 if (links) {
552 for (i = 0; links[i]; i++)
553 free(links[i]);
554 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700555 }
556}
557
Colin Crossfadb85e2011-03-30 18:32:12 -0700558static void handle_platform_device_event(struct uevent *uevent)
559{
Dima Zavinf395c922013-03-06 16:23:57 -0800560 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700561
562 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800563 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700564 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800565 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700566}
567
Colin Crosseb5ba832011-03-30 17:37:17 -0700568static const char *parse_device_name(struct uevent *uevent, unsigned int len)
569{
570 const char *name;
571
572 /* if it's not a /dev device, nothing else to do */
573 if((uevent->major < 0) || (uevent->minor < 0))
574 return NULL;
575
576 /* do we have a name? */
577 name = strrchr(uevent->path, '/');
578 if(!name)
579 return NULL;
580 name++;
581
582 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800583 if(strlen(name) > len) {
584 ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n",
585 name, len);
Colin Crosseb5ba832011-03-30 17:37:17 -0700586 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800587 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700588
589 return name;
590}
591
592static void handle_block_device_event(struct uevent *uevent)
593{
594 const char *base = "/dev/block/";
595 const char *name;
596 char devpath[96];
597 char **links = NULL;
598
599 name = parse_device_name(uevent, 64);
600 if (!name)
601 return;
602
603 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500604 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700605
Dima Zavinf395c922013-03-06 16:23:57 -0800606 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700607 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700608
609 handle_device(uevent->action, devpath, uevent->path, 1,
610 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700611}
612
Greg Hackmann3312aa82013-11-18 15:24:40 -0800613#define DEVPATH_LEN 96
614
615static bool assemble_devpath(char *devpath, const char *dirname,
616 const char *devname)
617{
618 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
619 if (s < 0) {
620 ERROR("failed to assemble device path (%s); ignoring event\n",
621 strerror(errno));
622 return false;
623 } else if (s >= DEVPATH_LEN) {
624 ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n",
625 dirname, devname, DEVPATH_LEN);
626 return false;
627 }
628 return true;
629}
630
631static void mkdir_recursive_for_devpath(const char *devpath)
632{
633 char dir[DEVPATH_LEN];
634 char *slash;
635
636 strcpy(dir, devpath);
637 slash = strrchr(dir, '/');
638 *slash = '\0';
639 mkdir_recursive(dir, 0755);
640}
641
Colin Crosseb5ba832011-03-30 17:37:17 -0700642static void handle_generic_device_event(struct uevent *uevent)
643{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800644 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700645 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800646 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700647 char **links = NULL;
648
649 name = parse_device_name(uevent, 64);
650 if (!name)
651 return;
652
Greg Hackmann3312aa82013-11-18 15:24:40 -0800653 struct ueventd_subsystem *subsystem =
654 ueventd_subsystem_find_by_name(uevent->subsystem);
655
656 if (subsystem) {
657 const char *devname;
658
659 switch (subsystem->devname_src) {
660 case DEVNAME_UEVENT_DEVNAME:
661 devname = uevent->device_name;
662 break;
663
664 case DEVNAME_UEVENT_DEVPATH:
665 devname = name;
666 break;
667
668 default:
669 ERROR("%s subsystem's devpath option is not set; ignoring event\n",
670 uevent->subsystem);
671 return;
672 }
673
674 if (!assemble_devpath(devpath, subsystem->dirname, devname))
675 return;
676 mkdir_recursive_for_devpath(devpath);
677 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700678 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700679 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800680 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800681 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800682 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700683 }
684 else {
685 /* This imitates the file system that would be created
686 * if we were using devfs instead.
687 * Minors are broken up into groups of 128, starting at "001"
688 */
689 int bus_id = uevent->minor / 128 + 1;
690 int device_id = uevent->minor % 128 + 1;
691 /* build directories */
692 make_dir("/dev/bus", 0755);
693 make_dir("/dev/bus/usb", 0755);
694 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
695 make_dir(devpath, 0755);
696 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
697 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700698 } else {
699 /* ignore other USB events */
700 return;
701 }
702 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
703 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500704 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200705 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
706 base = "/dev/dri/";
707 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700708 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
709 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500710 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700711 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
712 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500713 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700714 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
715 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500716 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700717 } else if(!strncmp(uevent->subsystem, "input", 5)) {
718 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500719 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700720 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
721 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500722 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700723 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
724 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500725 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700726 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
727 !strncmp(name, "log_", 4)) {
Elliott Hughesf682b472015-02-06 12:19:48 -0800728 INFO("kernel logger is deprecated\n");
Colin Crosseb5ba832011-03-30 17:37:17 -0700729 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500730 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700731 name += 4;
732 } else
733 base = "/dev/";
734 links = get_character_device_symlinks(uevent);
735
736 if (!devpath[0])
737 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
738
739 handle_device(uevent->action, devpath, uevent->path, 0,
740 uevent->major, uevent->minor, links);
741}
742
743static void handle_device_event(struct uevent *uevent)
744{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700745 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Rob Herring6de783a2016-05-06 10:06:59 -0500746 fixup_sys_perms(uevent->path, uevent->subsystem);
Colin Crosseb5ba832011-03-30 17:37:17 -0700747
748 if (!strncmp(uevent->subsystem, "block", 5)) {
749 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700750 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
751 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700752 } else {
753 handle_generic_device_event(uevent);
754 }
755}
756
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700757static int load_firmware(int fw_fd, int loading_fd, int data_fd)
758{
759 struct stat st;
760 long len_to_copy;
761 int ret = 0;
762
763 if(fstat(fw_fd, &st) < 0)
764 return -1;
765 len_to_copy = st.st_size;
766
767 write(loading_fd, "1", 1); /* start transfer */
768
769 while (len_to_copy > 0) {
770 char buf[PAGE_SIZE];
771 ssize_t nr;
772
773 nr = read(fw_fd, buf, sizeof(buf));
774 if(!nr)
775 break;
776 if(nr < 0) {
777 ret = -1;
778 break;
779 }
Biao Ludc848562016-01-28 16:10:54 +0800780 if (!android::base::WriteFully(data_fd, buf, nr)) {
781 ret = -1;
782 break;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700783 }
Biao Ludc848562016-01-28 16:10:54 +0800784 len_to_copy -= nr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700785 }
786
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700787 if(!ret)
788 write(loading_fd, "0", 1); /* successful end of transfer */
789 else
790 write(loading_fd, "-1", 2); /* abort transfer */
791
792 return ret;
793}
794
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700795static int is_booting(void)
796{
797 return access("/dev/.booting", F_OK) == 0;
798}
799
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700800static void process_firmware_event(struct uevent *uevent)
801{
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700802 char *root, *loading, *data;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700803 int l, loading_fd, data_fd, fw_fd;
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700804 size_t i;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700805 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700806
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700807 INFO("firmware: loading '%s' for '%s'\n",
808 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700809
810 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
811 if (l == -1)
812 return;
813
814 l = asprintf(&loading, "%sloading", root);
815 if (l == -1)
816 goto root_free_out;
817
818 l = asprintf(&data, "%sdata", root);
819 if (l == -1)
820 goto loading_free_out;
821
Nick Kralevich45a884f2015-02-02 14:37:22 -0800822 loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700823 if(loading_fd < 0)
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700824 goto data_free_out;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700825
Nick Kralevich45a884f2015-02-02 14:37:22 -0800826 data_fd = open(data, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700827 if(data_fd < 0)
828 goto loading_close_out;
829
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700830try_loading_again:
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700831 for (i = 0; i < ARRAY_SIZE(firmware_dirs); i++) {
832 char *file = NULL;
833 l = asprintf(&file, "%s/%s", firmware_dirs[i], uevent->firmware);
834 if (l == -1)
835 goto data_free_out;
836 fw_fd = open(file, O_RDONLY|O_CLOEXEC);
837 free(file);
838 if (fw_fd >= 0) {
839 if(!load_firmware(fw_fd, loading_fd, data_fd))
840 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
841 else
842 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
843 break;
Benoit Goby609d8822010-11-09 18:10:24 -0800844 }
Brian Swetland02863b92010-09-19 03:36:39 -0700845 }
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700846 if (fw_fd < 0) {
847 if (booting) {
848 /* If we're not fully booted, we may be missing
849 * filesystems needed for firmware, wait and retry.
850 */
851 usleep(100000);
852 booting = is_booting();
853 goto try_loading_again;
854 }
Elliott Hughescd67f002015-03-20 17:05:56 -0700855 INFO("firmware: could not open '%s': %s\n", uevent->firmware, strerror(errno));
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700856 write(loading_fd, "-1", 2);
857 goto data_close_out;
858 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700859
860 close(fw_fd);
861data_close_out:
862 close(data_fd);
863loading_close_out:
864 close(loading_fd);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700865data_free_out:
866 free(data);
867loading_free_out:
868 free(loading);
869root_free_out:
870 free(root);
871}
872
873static void handle_firmware_event(struct uevent *uevent)
874{
875 pid_t pid;
876
877 if(strcmp(uevent->subsystem, "firmware"))
878 return;
879
880 if(strcmp(uevent->action, "add"))
881 return;
882
883 /* we fork, to avoid making large memory allocations in init proper */
884 pid = fork();
885 if (!pid) {
886 process_firmware_event(uevent);
Kenny Root17baff42014-08-20 16:16:44 -0700887 _exit(EXIT_SUCCESS);
888 } else if (pid < 0) {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800889 ERROR("could not fork to process firmware event: %s\n", strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700890 }
891}
892
Ruchi Kandoic6037202014-06-23 11:22:09 -0700893#define UEVENT_MSG_LEN 2048
Colin Cross0dd7ca62010-04-13 19:25:51 -0700894void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700895{
Vernon Tang3f582e92011-04-25 13:08:17 +1000896 char msg[UEVENT_MSG_LEN+2];
897 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700898 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700899 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700900 continue;
901
902 msg[n] = '\0';
903 msg[n+1] = '\0';
904
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700905 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700906 parse_event(msg, &uevent);
907
Nick Kralevich4d870952015-06-12 22:03:50 -0700908 if (selinux_status_updated() > 0) {
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400909 struct selabel_handle *sehandle2;
910 sehandle2 = selinux_android_file_context_handle();
911 if (sehandle2) {
912 selabel_close(sehandle);
913 sehandle = sehandle2;
914 }
915 }
916
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700917 handle_device_event(&uevent);
918 handle_firmware_event(&uevent);
919 }
920}
921
922/* Coldboot walks parts of the /sys tree and pokes the uevent files
923** to cause the kernel to regenerate device add events that happened
924** before init's device manager was started
925**
926** We drain any pending events from the netlink socket every time
927** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800928** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700929*/
930
Colin Cross0dd7ca62010-04-13 19:25:51 -0700931static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700932{
933 struct dirent *de;
934 int dfd, fd;
935
936 dfd = dirfd(d);
937
938 fd = openat(dfd, "uevent", O_WRONLY);
939 if(fd >= 0) {
940 write(fd, "add\n", 4);
941 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700942 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700943 }
944
945 while((de = readdir(d))) {
946 DIR *d2;
947
948 if(de->d_type != DT_DIR || de->d_name[0] == '.')
949 continue;
950
951 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
952 if(fd < 0)
953 continue;
954
955 d2 = fdopendir(fd);
956 if(d2 == 0)
957 close(fd);
958 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700959 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700960 closedir(d2);
961 }
962 }
963}
964
Colin Cross0dd7ca62010-04-13 19:25:51 -0700965static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700966{
James Hawkins588a2ca2016-02-18 14:52:46 -0800967 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700968 if(d) {
James Hawkins588a2ca2016-02-18 14:52:46 -0800969 do_coldboot(d.get());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700970 }
971}
972
Elliott Hughes74738362015-03-28 10:51:23 -0700973void device_init() {
Nick Kralevich4d870952015-06-12 22:03:50 -0700974 sehandle = selinux_android_file_context_handle();
975 selinux_status_open(true);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700976
Andrew Boied562ca72012-12-04 15:47:20 -0800977 /* is 256K enough? udev uses 16MB! */
978 device_fd = uevent_open_socket(256*1024, true);
Elliott Hughes56a06562015-03-28 11:23:32 -0700979 if (device_fd == -1) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700980 return;
Elliott Hughes56a06562015-03-28 11:23:32 -0700981 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700982 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700983
Elliott Hughes56a06562015-03-28 11:23:32 -0700984 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700985 NOTICE("Skipping coldboot, already done!\n");
Elliott Hughes56a06562015-03-28 11:23:32 -0700986 return;
Colin Crossf83d0b92010-04-21 12:04:20 -0700987 }
Elliott Hughes56a06562015-03-28 11:23:32 -0700988
989 Timer t;
990 coldboot("/sys/class");
991 coldboot("/sys/block");
992 coldboot("/sys/devices");
993 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
994 NOTICE("Coldboot took %.2fs.\n", t.duration());
Colin Cross0dd7ca62010-04-13 19:25:51 -0700995}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700996
Colin Cross0dd7ca62010-04-13 19:25:51 -0700997int get_device_fd()
998{
999 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001000}