blob: cffc5619b40555e5b0b7ece68bb4bc2dcca8c9f3 [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
Stephen Smalleye46f9d52012-01-13 08:48:47 -050034#include <selinux/selinux.h>
35#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040036#include <selinux/android.h>
Stephen Smalleye2eb69d2013-04-16 09:30:30 -040037#include <selinux/avc.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050038
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070039#include <private/android_filesystem_config.h>
40#include <sys/time.h>
Colin Cross982a8152010-06-03 12:21:01 -070041#include <sys/wait.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070042
Biao Ludc848562016-01-28 16:10:54 +080043#include <android-base/file.h>
Dima Zavinda04c522011-09-01 17:09:44 -070044#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100045#include <cutils/uevent.h>
46
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070047#include "devices.h"
Greg Hackmann3312aa82013-11-18 15:24:40 -080048#include "ueventd_parser.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070049#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070050#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070051
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070052#define SYSFS_PREFIX "/sys"
Daniel Rosenbergd1d96022015-03-20 15:10:29 -070053static const char *firmware_dirs[] = { "/etc/firmware",
54 "/vendor/firmware",
55 "/firmware/image" };
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070056
Stephen Smalleye096e362012-06-11 13:37:39 -040057extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050058
Colin Cross0dd7ca62010-04-13 19:25:51 -070059static int device_fd = -1;
60
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070061struct uevent {
62 const char *action;
63 const char *path;
64 const char *subsystem;
65 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070066 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070067 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070068 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069 int major;
70 int minor;
71};
72
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073struct perms_ {
74 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070075 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076 mode_t perm;
77 unsigned int uid;
78 unsigned int gid;
79 unsigned short prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070080 unsigned short wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070081};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070082
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070083struct perm_node {
84 struct perms_ dp;
85 struct listnode plist;
86};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070087
Colin Crossfadb85e2011-03-30 18:32:12 -070088struct platform_node {
89 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080090 char *path;
91 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070092 struct listnode list;
93};
94
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070095static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070096static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070097static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070098
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070099int add_dev_perms(const char *name, const char *attr,
100 mode_t perm, unsigned int uid, unsigned int gid,
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700101 unsigned short prefix,
102 unsigned short wildcard) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800103 struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700104 if (!node)
105 return -ENOMEM;
106
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700107 node->dp.name = strdup(name);
108 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700109 return -ENOMEM;
110
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700111 if (attr) {
112 node->dp.attr = strdup(attr);
113 if (!node->dp.attr)
114 return -ENOMEM;
115 }
116
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700117 node->dp.perm = perm;
118 node->dp.uid = uid;
119 node->dp.gid = gid;
120 node->dp.prefix = prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700121 node->dp.wildcard = wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700122
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700123 if (attr)
124 list_add_tail(&sys_perms, &node->plist);
125 else
126 list_add_tail(&dev_perms, &node->plist);
127
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700128 return 0;
129}
130
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700131void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700132{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700133 char buf[512];
134 struct listnode *node;
135 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700136
Nick Kralevich3b4c0bd2014-07-03 16:04:34 -0700137 /* upaths omit the "/sys" that paths in this list
138 * contain, so we add 4 when comparing...
139 */
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700140 list_for_each(node, &sys_perms) {
141 dp = &(node_to_item(node, struct perm_node, plist))->dp;
142 if (dp->prefix) {
143 if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700144 continue;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700145 } else if (dp->wildcard) {
146 if (fnmatch(dp->name + 4, upath, FNM_PATHNAME) != 0)
147 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700148 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700149 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700150 continue;
151 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700152
153 if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
Nick Kralevich3b4c0bd2014-07-03 16:04:34 -0700154 break;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700155
Yabin Cuie2d63af2015-02-17 19:27:51 -0800156 snprintf(buf, sizeof(buf), "/sys%s/%s", upath, dp->attr);
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700157 INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
158 chown(buf, dp->uid, dp->gid);
159 chmod(buf, dp->perm);
Nick Kralevich3b4c0bd2014-07-03 16:04:34 -0700160 }
161
162 // Now fixup SELinux file labels
163 int len = snprintf(buf, sizeof(buf), "/sys%s", upath);
164 if ((len < 0) || ((size_t) len >= sizeof(buf))) {
165 // Overflow
166 return;
167 }
168 if (access(buf, F_OK) == 0) {
169 INFO("restorecon_recursive: %s\n", buf);
170 restorecon_recursive(buf);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700171 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700172}
173
Colin Cross43d537e2014-07-02 13:08:13 -0700174static bool perm_path_matches(const char *path, struct perms_ *dp)
175{
176 if (dp->prefix) {
177 if (strncmp(path, dp->name, strlen(dp->name)) == 0)
178 return true;
179 } else if (dp->wildcard) {
180 if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
181 return true;
182 } else {
183 if (strcmp(path, dp->name) == 0)
184 return true;
185 }
186
187 return false;
188}
189
190static mode_t get_device_perm(const char *path, const char **links,
191 unsigned *uid, unsigned *gid)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700192{
Colin Cross44b65d02010-04-20 14:32:50 -0700193 struct listnode *node;
194 struct perm_node *perm_node;
195 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700196
Colin Cross44b65d02010-04-20 14:32:50 -0700197 /* search the perms list in reverse so that ueventd.$hardware can
198 * override ueventd.rc
199 */
200 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700201 bool match = false;
202
Colin Cross44b65d02010-04-20 14:32:50 -0700203 perm_node = node_to_item(node, struct perm_node, plist);
204 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700205
Colin Cross43d537e2014-07-02 13:08:13 -0700206 if (perm_path_matches(path, dp)) {
207 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700208 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700209 if (links) {
210 int i;
211 for (i = 0; links[i]; i++) {
212 if (perm_path_matches(links[i], dp)) {
213 match = true;
214 break;
215 }
216 }
217 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700218 }
Colin Cross43d537e2014-07-02 13:08:13 -0700219
220 if (match) {
221 *uid = dp->uid;
222 *gid = dp->gid;
223 return dp->perm;
224 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700225 }
Colin Cross44b65d02010-04-20 14:32:50 -0700226 /* Default if nothing found. */
227 *uid = 0;
228 *gid = 0;
229 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700230}
231
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700232static void make_device(const char *path,
Elliott Hughesf682b472015-02-06 12:19:48 -0800233 const char */*upath*/,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400234 int block, int major, int minor,
235 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700236{
237 unsigned uid;
238 unsigned gid;
239 mode_t mode;
240 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500241 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700242
Colin Cross43d537e2014-07-02 13:08:13 -0700243 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700244
Mihai Serban721c9ce2016-04-25 18:22:27 +0300245 if (selabel_lookup_best_match(sehandle, &secontext, path, links, mode)) {
246 ERROR("Device '%s' not created; cannot find SELinux label (%s)\n",
247 path, strerror(errno));
248 return;
249 }
Nick Kralevich4d870952015-06-12 22:03:50 -0700250 setfscreatecon(secontext);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700251
Colin Cross17dcc5c2010-09-03 12:25:34 -0700252 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800253 /* Temporarily change egid to avoid race condition setting the gid of the
254 * device node. Unforunately changing the euid would prevent creation of
255 * some device nodes, so the uid has to be set with chown() and is still
256 * racy. Fixing the gid race at least fixed the issue with system_server
257 * opening dynamic input devices under the AID_INPUT gid. */
258 setegid(gid);
Mihai Serban721c9ce2016-04-25 18:22:27 +0300259 /* If the node already exists update its SELinux label to handle cases when
260 * it was created with the wrong context during coldboot procedure. */
261 if (mknod(path, mode, dev) && (errno == EEXIST)) {
262 if (lsetfilecon(path, secontext)) {
263 ERROR("Cannot set '%s' SELinux label on '%s' device (%s)\n",
264 secontext, path, strerror(errno));
265 }
266 }
Nick Pelly6405c692010-01-21 18:13:39 -0800267 chown(path, uid, -1);
268 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700269
Mihai Serban721c9ce2016-04-25 18:22:27 +0300270 freecon(secontext);
271 setfscreatecon(NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700272}
273
Dima Zavinf395c922013-03-06 16:23:57 -0800274static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700275{
Dima Zavinf395c922013-03-06 16:23:57 -0800276 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700277 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800278 const char *name = path;
279
280 if (!strncmp(path, "/devices/", 9)) {
281 name += 9;
282 if (!strncmp(name, "platform/", 9))
283 name += 9;
284 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700285
Dima Zavinf395c922013-03-06 16:23:57 -0800286 INFO("adding platform device %s (%s)\n", name, path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700287
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800288 bus = (platform_node*) calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800289 bus->path = strdup(path);
290 bus->path_len = path_len;
291 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700292 list_add_tail(&platform_names, &bus->list);
293}
294
295/*
Dima Zavinf395c922013-03-06 16:23:57 -0800296 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700297 * platform device prefix. If it doesn't start with a platform device, return
298 * 0.
299 */
Dima Zavinf395c922013-03-06 16:23:57 -0800300static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700301{
Dima Zavinf395c922013-03-06 16:23:57 -0800302 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700303 struct listnode *node;
304 struct platform_node *bus;
305
306 list_for_each_reverse(node, &platform_names) {
307 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800308 if ((bus->path_len < path_len) &&
309 (path[bus->path_len] == '/') &&
310 !strncmp(path, bus->path, bus->path_len))
311 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700312 }
313
314 return NULL;
315}
316
Dima Zavinf395c922013-03-06 16:23:57 -0800317static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700318{
319 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)) {
325 INFO("removing platform device %s\n", bus->name);
326 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700327 list_remove(node);
328 free(bus);
329 return;
330 }
331 }
332}
333
Andrew Boiea885d042013-09-13 17:41:20 -0700334/* Given a path that may start with a PCI device, populate the supplied buffer
335 * with the PCI domain/bus number and the peripheral ID and return 0.
336 * If it doesn't start with a PCI device, or there is some error, return -1 */
337static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
338{
339 const char *start, *end;
340
341 if (strncmp(path, "/devices/pci", 12))
342 return -1;
343
344 /* Beginning of the prefix is the initial "pci" after "/devices/" */
345 start = path + 9;
346
347 /* End of the prefix is two path '/' later, capturing the domain/bus number
348 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
349 end = strchr(start, '/');
350 if (!end)
351 return -1;
352 end = strchr(end + 1, '/');
353 if (!end)
354 return -1;
355
356 /* Make sure we have enough room for the string plus null terminator */
357 if (end - start + 1 > buf_sz)
358 return -1;
359
360 strncpy(buf, start, end - start);
361 buf[end - start] = '\0';
362 return 0;
363}
364
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700365static void parse_event(const char *msg, struct uevent *uevent)
366{
367 uevent->action = "";
368 uevent->path = "";
369 uevent->subsystem = "";
370 uevent->firmware = "";
371 uevent->major = -1;
372 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700373 uevent->partition_name = NULL;
374 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700375 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700376
377 /* currently ignoring SEQNUM */
378 while(*msg) {
379 if(!strncmp(msg, "ACTION=", 7)) {
380 msg += 7;
381 uevent->action = msg;
382 } else if(!strncmp(msg, "DEVPATH=", 8)) {
383 msg += 8;
384 uevent->path = msg;
385 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
386 msg += 10;
387 uevent->subsystem = msg;
388 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
389 msg += 9;
390 uevent->firmware = msg;
391 } else if(!strncmp(msg, "MAJOR=", 6)) {
392 msg += 6;
393 uevent->major = atoi(msg);
394 } else if(!strncmp(msg, "MINOR=", 6)) {
395 msg += 6;
396 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700397 } else if(!strncmp(msg, "PARTN=", 6)) {
398 msg += 6;
399 uevent->partition_num = atoi(msg);
400 } else if(!strncmp(msg, "PARTNAME=", 9)) {
401 msg += 9;
402 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700403 } else if(!strncmp(msg, "DEVNAME=", 8)) {
404 msg += 8;
405 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700406 }
407
Wei Zhongf97b8872012-03-23 14:15:34 -0700408 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700409 while(*msg++)
410 ;
411 }
412
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800413 if (LOG_UEVENTS) {
414 INFO("event { '%s', '%s', '%s', '%s', %d, %d }\n",
415 uevent->action, uevent->path, uevent->subsystem,
416 uevent->firmware, uevent->major, uevent->minor);
417 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700418}
419
Benoit Gobyd2278632010-08-03 14:36:02 -0700420static char **get_character_device_symlinks(struct uevent *uevent)
421{
422 const char *parent;
423 char *slash;
424 char **links;
425 int link_num = 0;
426 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800427 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700428
Dima Zavinf395c922013-03-06 16:23:57 -0800429 pdev = find_platform_device(uevent->path);
430 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700431 return NULL;
432
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800433 links = (char**) malloc(sizeof(char *) * 2);
Benoit Gobyd2278632010-08-03 14:36:02 -0700434 if (!links)
435 return NULL;
436 memset(links, 0, sizeof(char *) * 2);
437
438 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800439 parent = strchr(uevent->path + pdev->path_len, '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100440 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700441 goto err;
442
443 if (!strncmp(parent, "/usb", 4)) {
444 /* skip root hub name and device. use device interface */
445 while (*++parent && *parent != '/');
446 if (*parent)
447 while (*++parent && *parent != '/');
448 if (!*parent)
449 goto err;
450 slash = strchr(++parent, '/');
451 if (!slash)
452 goto err;
453 width = slash - parent;
454 if (width <= 0)
455 goto err;
456
457 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
458 link_num++;
459 else
460 links[link_num] = NULL;
461 mkdir("/dev/usb", 0755);
462 }
463 else {
464 goto err;
465 }
466
467 return links;
468err:
469 free(links);
470 return NULL;
471}
472
Andrew Boiea885d042013-09-13 17:41:20 -0700473static char **get_block_device_symlinks(struct uevent *uevent)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700474{
Colin Crossfadb85e2011-03-30 18:32:12 -0700475 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800476 struct platform_node *pdev;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700477 char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700478 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700479 char buf[256];
480 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700481 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700482 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700483
Dima Zavinf395c922013-03-06 16:23:57 -0800484 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700485 if (pdev) {
486 device = pdev->name;
487 type = "platform";
488 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
489 device = buf;
490 type = "pci";
491 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800492 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700493 }
Dima Zavinf395c922013-03-06 16:23:57 -0800494
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800495 char **links = (char**) malloc(sizeof(char *) * 4);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700496 if (!links)
497 return NULL;
498 memset(links, 0, sizeof(char *) * 4);
499
Andrew Boiea885d042013-09-13 17:41:20 -0700500 INFO("found %s device %s\n", type, device);
Colin Crossfadb85e2011-03-30 18:32:12 -0700501
Andrew Boiea885d042013-09-13 17:41:20 -0700502 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700503
504 if (uevent->partition_name) {
505 p = strdup(uevent->partition_name);
506 sanitize(p);
Johan Redestig93ca79b2012-04-18 16:41:19 +0200507 if (strcmp(uevent->partition_name, p))
508 NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700509 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
510 link_num++;
511 else
512 links[link_num] = NULL;
513 free(p);
514 }
515
516 if (uevent->partition_num >= 0) {
517 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
518 link_num++;
519 else
520 links[link_num] = NULL;
521 }
522
Dima Zavinf395c922013-03-06 16:23:57 -0800523 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700524 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
525 link_num++;
526 else
527 links[link_num] = NULL;
528
529 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700530}
531
Colin Crosseb5ba832011-03-30 17:37:17 -0700532static void handle_device(const char *action, const char *devpath,
533 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700534{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700535 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700536
Colin Crosseb5ba832011-03-30 17:37:17 -0700537 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400538 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700539 if (links) {
540 for (i = 0; links[i]; i++)
Chris Fries79f33842013-09-05 13:19:21 -0500541 make_link_init(devpath, links[i]);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700542 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700543 }
544
Colin Crosseb5ba832011-03-30 17:37:17 -0700545 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700546 if (links) {
547 for (i = 0; links[i]; i++)
548 remove_link(devpath, links[i]);
549 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700550 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700551 }
552
553 if (links) {
554 for (i = 0; links[i]; i++)
555 free(links[i]);
556 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700557 }
558}
559
Colin Crossfadb85e2011-03-30 18:32:12 -0700560static void handle_platform_device_event(struct uevent *uevent)
561{
Dima Zavinf395c922013-03-06 16:23:57 -0800562 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700563
564 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800565 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700566 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800567 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700568}
569
Colin Crosseb5ba832011-03-30 17:37:17 -0700570static const char *parse_device_name(struct uevent *uevent, unsigned int len)
571{
572 const char *name;
573
574 /* if it's not a /dev device, nothing else to do */
575 if((uevent->major < 0) || (uevent->minor < 0))
576 return NULL;
577
578 /* do we have a name? */
579 name = strrchr(uevent->path, '/');
580 if(!name)
581 return NULL;
582 name++;
583
584 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800585 if(strlen(name) > len) {
586 ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n",
587 name, len);
Colin Crosseb5ba832011-03-30 17:37:17 -0700588 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800589 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700590
591 return name;
592}
593
594static void handle_block_device_event(struct uevent *uevent)
595{
596 const char *base = "/dev/block/";
597 const char *name;
598 char devpath[96];
599 char **links = NULL;
600
601 name = parse_device_name(uevent, 64);
602 if (!name)
603 return;
604
605 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500606 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700607
Dima Zavinf395c922013-03-06 16:23:57 -0800608 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700609 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700610
611 handle_device(uevent->action, devpath, uevent->path, 1,
612 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700613}
614
Greg Hackmann3312aa82013-11-18 15:24:40 -0800615#define DEVPATH_LEN 96
616
617static bool assemble_devpath(char *devpath, const char *dirname,
618 const char *devname)
619{
620 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
621 if (s < 0) {
622 ERROR("failed to assemble device path (%s); ignoring event\n",
623 strerror(errno));
624 return false;
625 } else if (s >= DEVPATH_LEN) {
626 ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n",
627 dirname, devname, DEVPATH_LEN);
628 return false;
629 }
630 return true;
631}
632
633static void mkdir_recursive_for_devpath(const char *devpath)
634{
635 char dir[DEVPATH_LEN];
636 char *slash;
637
638 strcpy(dir, devpath);
639 slash = strrchr(dir, '/');
640 *slash = '\0';
641 mkdir_recursive(dir, 0755);
642}
643
Colin Crosseb5ba832011-03-30 17:37:17 -0700644static void handle_generic_device_event(struct uevent *uevent)
645{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800646 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700647 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800648 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700649 char **links = NULL;
650
651 name = parse_device_name(uevent, 64);
652 if (!name)
653 return;
654
Greg Hackmann3312aa82013-11-18 15:24:40 -0800655 struct ueventd_subsystem *subsystem =
656 ueventd_subsystem_find_by_name(uevent->subsystem);
657
658 if (subsystem) {
659 const char *devname;
660
661 switch (subsystem->devname_src) {
662 case DEVNAME_UEVENT_DEVNAME:
663 devname = uevent->device_name;
664 break;
665
666 case DEVNAME_UEVENT_DEVPATH:
667 devname = name;
668 break;
669
670 default:
671 ERROR("%s subsystem's devpath option is not set; ignoring event\n",
672 uevent->subsystem);
673 return;
674 }
675
676 if (!assemble_devpath(devpath, subsystem->dirname, devname))
677 return;
678 mkdir_recursive_for_devpath(devpath);
679 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700680 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700681 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800682 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800683 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800684 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700685 }
686 else {
687 /* This imitates the file system that would be created
688 * if we were using devfs instead.
689 * Minors are broken up into groups of 128, starting at "001"
690 */
691 int bus_id = uevent->minor / 128 + 1;
692 int device_id = uevent->minor % 128 + 1;
693 /* build directories */
694 make_dir("/dev/bus", 0755);
695 make_dir("/dev/bus/usb", 0755);
696 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
697 make_dir(devpath, 0755);
698 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
699 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700700 } else {
701 /* ignore other USB events */
702 return;
703 }
704 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
705 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500706 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200707 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
708 base = "/dev/dri/";
709 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700710 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
711 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500712 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700713 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
714 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500715 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700716 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
717 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500718 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700719 } else if(!strncmp(uevent->subsystem, "input", 5)) {
720 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500721 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700722 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
723 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500724 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700725 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
726 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500727 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700728 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
729 !strncmp(name, "log_", 4)) {
Elliott Hughesf682b472015-02-06 12:19:48 -0800730 INFO("kernel logger is deprecated\n");
Colin Crosseb5ba832011-03-30 17:37:17 -0700731 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500732 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700733 name += 4;
734 } else
735 base = "/dev/";
736 links = get_character_device_symlinks(uevent);
737
738 if (!devpath[0])
739 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
740
741 handle_device(uevent->action, devpath, uevent->path, 0,
742 uevent->major, uevent->minor, links);
743}
744
745static void handle_device_event(struct uevent *uevent)
746{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700747 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700748 fixup_sys_perms(uevent->path);
749
750 if (!strncmp(uevent->subsystem, "block", 5)) {
751 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700752 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
753 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700754 } else {
755 handle_generic_device_event(uevent);
756 }
757}
758
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700759static int load_firmware(int fw_fd, int loading_fd, int data_fd)
760{
761 struct stat st;
762 long len_to_copy;
763 int ret = 0;
764
765 if(fstat(fw_fd, &st) < 0)
766 return -1;
767 len_to_copy = st.st_size;
768
769 write(loading_fd, "1", 1); /* start transfer */
770
771 while (len_to_copy > 0) {
772 char buf[PAGE_SIZE];
773 ssize_t nr;
774
775 nr = read(fw_fd, buf, sizeof(buf));
776 if(!nr)
777 break;
778 if(nr < 0) {
779 ret = -1;
780 break;
781 }
Biao Ludc848562016-01-28 16:10:54 +0800782 if (!android::base::WriteFully(data_fd, buf, nr)) {
783 ret = -1;
784 break;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700785 }
Biao Ludc848562016-01-28 16:10:54 +0800786 len_to_copy -= nr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700787 }
788
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700789 if(!ret)
790 write(loading_fd, "0", 1); /* successful end of transfer */
791 else
792 write(loading_fd, "-1", 2); /* abort transfer */
793
794 return ret;
795}
796
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700797static int is_booting(void)
798{
799 return access("/dev/.booting", F_OK) == 0;
800}
801
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700802static void process_firmware_event(struct uevent *uevent)
803{
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700804 char *root, *loading, *data;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700805 int l, loading_fd, data_fd, fw_fd;
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700806 size_t i;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700807 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700808
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700809 INFO("firmware: loading '%s' for '%s'\n",
810 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700811
812 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
813 if (l == -1)
814 return;
815
816 l = asprintf(&loading, "%sloading", root);
817 if (l == -1)
818 goto root_free_out;
819
820 l = asprintf(&data, "%sdata", root);
821 if (l == -1)
822 goto loading_free_out;
823
Nick Kralevich45a884f2015-02-02 14:37:22 -0800824 loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700825 if(loading_fd < 0)
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700826 goto data_free_out;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700827
Nick Kralevich45a884f2015-02-02 14:37:22 -0800828 data_fd = open(data, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700829 if(data_fd < 0)
830 goto loading_close_out;
831
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700832try_loading_again:
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700833 for (i = 0; i < ARRAY_SIZE(firmware_dirs); i++) {
834 char *file = NULL;
835 l = asprintf(&file, "%s/%s", firmware_dirs[i], uevent->firmware);
836 if (l == -1)
837 goto data_free_out;
838 fw_fd = open(file, O_RDONLY|O_CLOEXEC);
839 free(file);
840 if (fw_fd >= 0) {
841 if(!load_firmware(fw_fd, loading_fd, data_fd))
842 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
843 else
844 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
845 break;
Benoit Goby609d8822010-11-09 18:10:24 -0800846 }
Brian Swetland02863b92010-09-19 03:36:39 -0700847 }
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700848 if (fw_fd < 0) {
849 if (booting) {
850 /* If we're not fully booted, we may be missing
851 * filesystems needed for firmware, wait and retry.
852 */
853 usleep(100000);
854 booting = is_booting();
855 goto try_loading_again;
856 }
Elliott Hughescd67f002015-03-20 17:05:56 -0700857 INFO("firmware: could not open '%s': %s\n", uevent->firmware, strerror(errno));
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700858 write(loading_fd, "-1", 2);
859 goto data_close_out;
860 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700861
862 close(fw_fd);
863data_close_out:
864 close(data_fd);
865loading_close_out:
866 close(loading_fd);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700867data_free_out:
868 free(data);
869loading_free_out:
870 free(loading);
871root_free_out:
872 free(root);
873}
874
875static void handle_firmware_event(struct uevent *uevent)
876{
877 pid_t pid;
878
879 if(strcmp(uevent->subsystem, "firmware"))
880 return;
881
882 if(strcmp(uevent->action, "add"))
883 return;
884
885 /* we fork, to avoid making large memory allocations in init proper */
886 pid = fork();
887 if (!pid) {
888 process_firmware_event(uevent);
Kenny Root17baff42014-08-20 16:16:44 -0700889 _exit(EXIT_SUCCESS);
890 } else if (pid < 0) {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800891 ERROR("could not fork to process firmware event: %s\n", strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700892 }
893}
894
Ruchi Kandoic6037202014-06-23 11:22:09 -0700895#define UEVENT_MSG_LEN 2048
Colin Cross0dd7ca62010-04-13 19:25:51 -0700896void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700897{
Vernon Tang3f582e92011-04-25 13:08:17 +1000898 char msg[UEVENT_MSG_LEN+2];
899 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700900 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700901 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700902 continue;
903
904 msg[n] = '\0';
905 msg[n+1] = '\0';
906
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700907 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700908 parse_event(msg, &uevent);
909
Nick Kralevich4d870952015-06-12 22:03:50 -0700910 if (selinux_status_updated() > 0) {
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400911 struct selabel_handle *sehandle2;
912 sehandle2 = selinux_android_file_context_handle();
913 if (sehandle2) {
914 selabel_close(sehandle);
915 sehandle = sehandle2;
916 }
917 }
918
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700919 handle_device_event(&uevent);
920 handle_firmware_event(&uevent);
921 }
922}
923
924/* Coldboot walks parts of the /sys tree and pokes the uevent files
925** to cause the kernel to regenerate device add events that happened
926** before init's device manager was started
927**
928** We drain any pending events from the netlink socket every time
929** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800930** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700931*/
932
Colin Cross0dd7ca62010-04-13 19:25:51 -0700933static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700934{
935 struct dirent *de;
936 int dfd, fd;
937
938 dfd = dirfd(d);
939
940 fd = openat(dfd, "uevent", O_WRONLY);
941 if(fd >= 0) {
942 write(fd, "add\n", 4);
943 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700944 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700945 }
946
947 while((de = readdir(d))) {
948 DIR *d2;
949
950 if(de->d_type != DT_DIR || de->d_name[0] == '.')
951 continue;
952
953 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
954 if(fd < 0)
955 continue;
956
957 d2 = fdopendir(fd);
958 if(d2 == 0)
959 close(fd);
960 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700961 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700962 closedir(d2);
963 }
964 }
965}
966
Colin Cross0dd7ca62010-04-13 19:25:51 -0700967static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700968{
969 DIR *d = opendir(path);
970 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700971 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700972 closedir(d);
973 }
974}
975
Elliott Hughes74738362015-03-28 10:51:23 -0700976void device_init() {
Nick Kralevich4d870952015-06-12 22:03:50 -0700977 sehandle = selinux_android_file_context_handle();
978 selinux_status_open(true);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700979
Andrew Boied562ca72012-12-04 15:47:20 -0800980 /* is 256K enough? udev uses 16MB! */
981 device_fd = uevent_open_socket(256*1024, true);
Elliott Hughes56a06562015-03-28 11:23:32 -0700982 if (device_fd == -1) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700983 return;
Elliott Hughes56a06562015-03-28 11:23:32 -0700984 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700985 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700986
Elliott Hughes56a06562015-03-28 11:23:32 -0700987 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700988 NOTICE("Skipping coldboot, already done!\n");
Elliott Hughes56a06562015-03-28 11:23:32 -0700989 return;
Colin Crossf83d0b92010-04-21 12:04:20 -0700990 }
Elliott Hughes56a06562015-03-28 11:23:32 -0700991
992 Timer t;
993 coldboot("/sys/class");
994 coldboot("/sys/block");
995 coldboot("/sys/devices");
996 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
997 NOTICE("Coldboot took %.2fs.\n", t.duration());
Colin Cross0dd7ca62010-04-13 19:25:51 -0700998}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700999
Colin Cross0dd7ca62010-04-13 19:25:51 -07001000int get_device_fd()
1001{
1002 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001003}