blob: 39cd70656921bd20193ad80c77eb5cb553552503 [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
Nick Kralevich4d870952015-06-12 22:03:50 -0700245 selabel_lookup_best_match(sehandle, &secontext, path, links, mode);
246 setfscreatecon(secontext);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700247
Colin Cross17dcc5c2010-09-03 12:25:34 -0700248 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800249 /* Temporarily change egid to avoid race condition setting the gid of the
250 * device node. Unforunately changing the euid would prevent creation of
251 * some device nodes, so the uid has to be set with chown() and is still
252 * racy. Fixing the gid race at least fixed the issue with system_server
253 * opening dynamic input devices under the AID_INPUT gid. */
254 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700255 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800256 chown(path, uid, -1);
257 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700258
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500259 if (secontext) {
260 freecon(secontext);
261 setfscreatecon(NULL);
262 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700263}
264
Dima Zavinf395c922013-03-06 16:23:57 -0800265static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700266{
Dima Zavinf395c922013-03-06 16:23:57 -0800267 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700268 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800269 const char *name = path;
270
271 if (!strncmp(path, "/devices/", 9)) {
272 name += 9;
273 if (!strncmp(name, "platform/", 9))
274 name += 9;
275 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700276
Dima Zavinf395c922013-03-06 16:23:57 -0800277 INFO("adding platform device %s (%s)\n", name, path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700278
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800279 bus = (platform_node*) calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800280 bus->path = strdup(path);
281 bus->path_len = path_len;
282 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700283 list_add_tail(&platform_names, &bus->list);
284}
285
286/*
Dima Zavinf395c922013-03-06 16:23:57 -0800287 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700288 * platform device prefix. If it doesn't start with a platform device, return
289 * 0.
290 */
Dima Zavinf395c922013-03-06 16:23:57 -0800291static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700292{
Dima Zavinf395c922013-03-06 16:23:57 -0800293 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700294 struct listnode *node;
295 struct platform_node *bus;
296
297 list_for_each_reverse(node, &platform_names) {
298 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800299 if ((bus->path_len < path_len) &&
300 (path[bus->path_len] == '/') &&
301 !strncmp(path, bus->path, bus->path_len))
302 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700303 }
304
305 return NULL;
306}
307
Dima Zavinf395c922013-03-06 16:23:57 -0800308static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700309{
310 struct listnode *node;
311 struct platform_node *bus;
312
313 list_for_each_reverse(node, &platform_names) {
314 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800315 if (!strcmp(path, bus->path)) {
316 INFO("removing platform device %s\n", bus->name);
317 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700318 list_remove(node);
319 free(bus);
320 return;
321 }
322 }
323}
324
Andrew Boiea885d042013-09-13 17:41:20 -0700325/* Given a path that may start with a PCI device, populate the supplied buffer
326 * with the PCI domain/bus number and the peripheral ID and return 0.
327 * If it doesn't start with a PCI device, or there is some error, return -1 */
328static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
329{
330 const char *start, *end;
331
332 if (strncmp(path, "/devices/pci", 12))
333 return -1;
334
335 /* Beginning of the prefix is the initial "pci" after "/devices/" */
336 start = path + 9;
337
338 /* End of the prefix is two path '/' later, capturing the domain/bus number
339 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
340 end = strchr(start, '/');
341 if (!end)
342 return -1;
343 end = strchr(end + 1, '/');
344 if (!end)
345 return -1;
346
347 /* Make sure we have enough room for the string plus null terminator */
348 if (end - start + 1 > buf_sz)
349 return -1;
350
351 strncpy(buf, start, end - start);
352 buf[end - start] = '\0';
353 return 0;
354}
355
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700356static void parse_event(const char *msg, struct uevent *uevent)
357{
358 uevent->action = "";
359 uevent->path = "";
360 uevent->subsystem = "";
361 uevent->firmware = "";
362 uevent->major = -1;
363 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700364 uevent->partition_name = NULL;
365 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700366 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700367
368 /* currently ignoring SEQNUM */
369 while(*msg) {
370 if(!strncmp(msg, "ACTION=", 7)) {
371 msg += 7;
372 uevent->action = msg;
373 } else if(!strncmp(msg, "DEVPATH=", 8)) {
374 msg += 8;
375 uevent->path = msg;
376 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
377 msg += 10;
378 uevent->subsystem = msg;
379 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
380 msg += 9;
381 uevent->firmware = msg;
382 } else if(!strncmp(msg, "MAJOR=", 6)) {
383 msg += 6;
384 uevent->major = atoi(msg);
385 } else if(!strncmp(msg, "MINOR=", 6)) {
386 msg += 6;
387 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700388 } else if(!strncmp(msg, "PARTN=", 6)) {
389 msg += 6;
390 uevent->partition_num = atoi(msg);
391 } else if(!strncmp(msg, "PARTNAME=", 9)) {
392 msg += 9;
393 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700394 } else if(!strncmp(msg, "DEVNAME=", 8)) {
395 msg += 8;
396 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700397 }
398
Wei Zhongf97b8872012-03-23 14:15:34 -0700399 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700400 while(*msg++)
401 ;
402 }
403
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800404 if (LOG_UEVENTS) {
405 INFO("event { '%s', '%s', '%s', '%s', %d, %d }\n",
406 uevent->action, uevent->path, uevent->subsystem,
407 uevent->firmware, uevent->major, uevent->minor);
408 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700409}
410
Benoit Gobyd2278632010-08-03 14:36:02 -0700411static char **get_character_device_symlinks(struct uevent *uevent)
412{
413 const char *parent;
414 char *slash;
415 char **links;
416 int link_num = 0;
417 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800418 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700419
Dima Zavinf395c922013-03-06 16:23:57 -0800420 pdev = find_platform_device(uevent->path);
421 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700422 return NULL;
423
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800424 links = (char**) malloc(sizeof(char *) * 2);
Benoit Gobyd2278632010-08-03 14:36:02 -0700425 if (!links)
426 return NULL;
427 memset(links, 0, sizeof(char *) * 2);
428
429 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800430 parent = strchr(uevent->path + pdev->path_len, '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100431 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700432 goto err;
433
434 if (!strncmp(parent, "/usb", 4)) {
435 /* skip root hub name and device. use device interface */
436 while (*++parent && *parent != '/');
437 if (*parent)
438 while (*++parent && *parent != '/');
439 if (!*parent)
440 goto err;
441 slash = strchr(++parent, '/');
442 if (!slash)
443 goto err;
444 width = slash - parent;
445 if (width <= 0)
446 goto err;
447
448 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
449 link_num++;
450 else
451 links[link_num] = NULL;
452 mkdir("/dev/usb", 0755);
453 }
454 else {
455 goto err;
456 }
457
458 return links;
459err:
460 free(links);
461 return NULL;
462}
463
Andrew Boiea885d042013-09-13 17:41:20 -0700464static char **get_block_device_symlinks(struct uevent *uevent)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700465{
Colin Crossfadb85e2011-03-30 18:32:12 -0700466 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800467 struct platform_node *pdev;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700468 char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700469 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700470 char buf[256];
471 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700472 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700473 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700474
Dima Zavinf395c922013-03-06 16:23:57 -0800475 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700476 if (pdev) {
477 device = pdev->name;
478 type = "platform";
479 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
480 device = buf;
481 type = "pci";
482 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800483 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700484 }
Dima Zavinf395c922013-03-06 16:23:57 -0800485
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800486 char **links = (char**) malloc(sizeof(char *) * 4);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700487 if (!links)
488 return NULL;
489 memset(links, 0, sizeof(char *) * 4);
490
Andrew Boiea885d042013-09-13 17:41:20 -0700491 INFO("found %s device %s\n", type, device);
Colin Crossfadb85e2011-03-30 18:32:12 -0700492
Andrew Boiea885d042013-09-13 17:41:20 -0700493 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700494
495 if (uevent->partition_name) {
496 p = strdup(uevent->partition_name);
497 sanitize(p);
Johan Redestig93ca79b2012-04-18 16:41:19 +0200498 if (strcmp(uevent->partition_name, p))
499 NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700500 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
501 link_num++;
502 else
503 links[link_num] = NULL;
504 free(p);
505 }
506
507 if (uevent->partition_num >= 0) {
508 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
509 link_num++;
510 else
511 links[link_num] = NULL;
512 }
513
Dima Zavinf395c922013-03-06 16:23:57 -0800514 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700515 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
516 link_num++;
517 else
518 links[link_num] = NULL;
519
520 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700521}
522
Colin Crosseb5ba832011-03-30 17:37:17 -0700523static void handle_device(const char *action, const char *devpath,
524 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700525{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700526 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700527
Colin Crosseb5ba832011-03-30 17:37:17 -0700528 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400529 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700530 if (links) {
531 for (i = 0; links[i]; i++)
Chris Fries79f33842013-09-05 13:19:21 -0500532 make_link_init(devpath, links[i]);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700533 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700534 }
535
Colin Crosseb5ba832011-03-30 17:37:17 -0700536 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700537 if (links) {
538 for (i = 0; links[i]; i++)
539 remove_link(devpath, links[i]);
540 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700541 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700542 }
543
544 if (links) {
545 for (i = 0; links[i]; i++)
546 free(links[i]);
547 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700548 }
549}
550
Colin Crossfadb85e2011-03-30 18:32:12 -0700551static void handle_platform_device_event(struct uevent *uevent)
552{
Dima Zavinf395c922013-03-06 16:23:57 -0800553 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700554
555 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800556 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700557 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800558 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700559}
560
Colin Crosseb5ba832011-03-30 17:37:17 -0700561static const char *parse_device_name(struct uevent *uevent, unsigned int len)
562{
563 const char *name;
564
565 /* if it's not a /dev device, nothing else to do */
566 if((uevent->major < 0) || (uevent->minor < 0))
567 return NULL;
568
569 /* do we have a name? */
570 name = strrchr(uevent->path, '/');
571 if(!name)
572 return NULL;
573 name++;
574
575 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800576 if(strlen(name) > len) {
577 ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n",
578 name, len);
Colin Crosseb5ba832011-03-30 17:37:17 -0700579 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800580 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700581
582 return name;
583}
584
585static void handle_block_device_event(struct uevent *uevent)
586{
587 const char *base = "/dev/block/";
588 const char *name;
589 char devpath[96];
590 char **links = NULL;
591
592 name = parse_device_name(uevent, 64);
593 if (!name)
594 return;
595
596 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500597 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700598
Dima Zavinf395c922013-03-06 16:23:57 -0800599 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700600 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700601
602 handle_device(uevent->action, devpath, uevent->path, 1,
603 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700604}
605
Greg Hackmann3312aa82013-11-18 15:24:40 -0800606#define DEVPATH_LEN 96
607
608static bool assemble_devpath(char *devpath, const char *dirname,
609 const char *devname)
610{
611 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
612 if (s < 0) {
613 ERROR("failed to assemble device path (%s); ignoring event\n",
614 strerror(errno));
615 return false;
616 } else if (s >= DEVPATH_LEN) {
617 ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n",
618 dirname, devname, DEVPATH_LEN);
619 return false;
620 }
621 return true;
622}
623
624static void mkdir_recursive_for_devpath(const char *devpath)
625{
626 char dir[DEVPATH_LEN];
627 char *slash;
628
629 strcpy(dir, devpath);
630 slash = strrchr(dir, '/');
631 *slash = '\0';
632 mkdir_recursive(dir, 0755);
633}
634
Colin Crosseb5ba832011-03-30 17:37:17 -0700635static void handle_generic_device_event(struct uevent *uevent)
636{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800637 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700638 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800639 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700640 char **links = NULL;
641
642 name = parse_device_name(uevent, 64);
643 if (!name)
644 return;
645
Greg Hackmann3312aa82013-11-18 15:24:40 -0800646 struct ueventd_subsystem *subsystem =
647 ueventd_subsystem_find_by_name(uevent->subsystem);
648
649 if (subsystem) {
650 const char *devname;
651
652 switch (subsystem->devname_src) {
653 case DEVNAME_UEVENT_DEVNAME:
654 devname = uevent->device_name;
655 break;
656
657 case DEVNAME_UEVENT_DEVPATH:
658 devname = name;
659 break;
660
661 default:
662 ERROR("%s subsystem's devpath option is not set; ignoring event\n",
663 uevent->subsystem);
664 return;
665 }
666
667 if (!assemble_devpath(devpath, subsystem->dirname, devname))
668 return;
669 mkdir_recursive_for_devpath(devpath);
670 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700671 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700672 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800673 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800674 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800675 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700676 }
677 else {
678 /* This imitates the file system that would be created
679 * if we were using devfs instead.
680 * Minors are broken up into groups of 128, starting at "001"
681 */
682 int bus_id = uevent->minor / 128 + 1;
683 int device_id = uevent->minor % 128 + 1;
684 /* build directories */
685 make_dir("/dev/bus", 0755);
686 make_dir("/dev/bus/usb", 0755);
687 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
688 make_dir(devpath, 0755);
689 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
690 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700691 } else {
692 /* ignore other USB events */
693 return;
694 }
695 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
696 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500697 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200698 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
699 base = "/dev/dri/";
700 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700701 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
702 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500703 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700704 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
705 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500706 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700707 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
708 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500709 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700710 } else if(!strncmp(uevent->subsystem, "input", 5)) {
711 base = "/dev/input/";
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, "mtd", 3)) {
714 base = "/dev/mtd/";
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, "sound", 5)) {
717 base = "/dev/snd/";
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, "misc", 4) &&
720 !strncmp(name, "log_", 4)) {
Elliott Hughesf682b472015-02-06 12:19:48 -0800721 INFO("kernel logger is deprecated\n");
Colin Crosseb5ba832011-03-30 17:37:17 -0700722 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500723 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700724 name += 4;
725 } else
726 base = "/dev/";
727 links = get_character_device_symlinks(uevent);
728
729 if (!devpath[0])
730 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
731
732 handle_device(uevent->action, devpath, uevent->path, 0,
733 uevent->major, uevent->minor, links);
734}
735
736static void handle_device_event(struct uevent *uevent)
737{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700738 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700739 fixup_sys_perms(uevent->path);
740
741 if (!strncmp(uevent->subsystem, "block", 5)) {
742 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700743 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
744 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700745 } else {
746 handle_generic_device_event(uevent);
747 }
748}
749
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700750static int load_firmware(int fw_fd, int loading_fd, int data_fd)
751{
752 struct stat st;
753 long len_to_copy;
754 int ret = 0;
755
756 if(fstat(fw_fd, &st) < 0)
757 return -1;
758 len_to_copy = st.st_size;
759
760 write(loading_fd, "1", 1); /* start transfer */
761
762 while (len_to_copy > 0) {
763 char buf[PAGE_SIZE];
764 ssize_t nr;
765
766 nr = read(fw_fd, buf, sizeof(buf));
767 if(!nr)
768 break;
769 if(nr < 0) {
770 ret = -1;
771 break;
772 }
Biao Ludc848562016-01-28 16:10:54 +0800773 if (!android::base::WriteFully(data_fd, buf, nr)) {
774 ret = -1;
775 break;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700776 }
Biao Ludc848562016-01-28 16:10:54 +0800777 len_to_copy -= nr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700778 }
779
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700780 if(!ret)
781 write(loading_fd, "0", 1); /* successful end of transfer */
782 else
783 write(loading_fd, "-1", 2); /* abort transfer */
784
785 return ret;
786}
787
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700788static int is_booting(void)
789{
790 return access("/dev/.booting", F_OK) == 0;
791}
792
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700793static void process_firmware_event(struct uevent *uevent)
794{
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700795 char *root, *loading, *data;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700796 int l, loading_fd, data_fd, fw_fd;
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700797 size_t i;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700798 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700799
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700800 INFO("firmware: loading '%s' for '%s'\n",
801 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700802
803 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
804 if (l == -1)
805 return;
806
807 l = asprintf(&loading, "%sloading", root);
808 if (l == -1)
809 goto root_free_out;
810
811 l = asprintf(&data, "%sdata", root);
812 if (l == -1)
813 goto loading_free_out;
814
Nick Kralevich45a884f2015-02-02 14:37:22 -0800815 loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700816 if(loading_fd < 0)
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700817 goto data_free_out;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700818
Nick Kralevich45a884f2015-02-02 14:37:22 -0800819 data_fd = open(data, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700820 if(data_fd < 0)
821 goto loading_close_out;
822
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700823try_loading_again:
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700824 for (i = 0; i < ARRAY_SIZE(firmware_dirs); i++) {
825 char *file = NULL;
826 l = asprintf(&file, "%s/%s", firmware_dirs[i], uevent->firmware);
827 if (l == -1)
828 goto data_free_out;
829 fw_fd = open(file, O_RDONLY|O_CLOEXEC);
830 free(file);
831 if (fw_fd >= 0) {
832 if(!load_firmware(fw_fd, loading_fd, data_fd))
833 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
834 else
835 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
836 break;
Benoit Goby609d8822010-11-09 18:10:24 -0800837 }
Brian Swetland02863b92010-09-19 03:36:39 -0700838 }
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700839 if (fw_fd < 0) {
840 if (booting) {
841 /* If we're not fully booted, we may be missing
842 * filesystems needed for firmware, wait and retry.
843 */
844 usleep(100000);
845 booting = is_booting();
846 goto try_loading_again;
847 }
Elliott Hughescd67f002015-03-20 17:05:56 -0700848 INFO("firmware: could not open '%s': %s\n", uevent->firmware, strerror(errno));
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700849 write(loading_fd, "-1", 2);
850 goto data_close_out;
851 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700852
853 close(fw_fd);
854data_close_out:
855 close(data_fd);
856loading_close_out:
857 close(loading_fd);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700858data_free_out:
859 free(data);
860loading_free_out:
861 free(loading);
862root_free_out:
863 free(root);
864}
865
866static void handle_firmware_event(struct uevent *uevent)
867{
868 pid_t pid;
869
870 if(strcmp(uevent->subsystem, "firmware"))
871 return;
872
873 if(strcmp(uevent->action, "add"))
874 return;
875
876 /* we fork, to avoid making large memory allocations in init proper */
877 pid = fork();
878 if (!pid) {
879 process_firmware_event(uevent);
Kenny Root17baff42014-08-20 16:16:44 -0700880 _exit(EXIT_SUCCESS);
881 } else if (pid < 0) {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800882 ERROR("could not fork to process firmware event: %s\n", strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700883 }
884}
885
Ruchi Kandoic6037202014-06-23 11:22:09 -0700886#define UEVENT_MSG_LEN 2048
Colin Cross0dd7ca62010-04-13 19:25:51 -0700887void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700888{
Vernon Tang3f582e92011-04-25 13:08:17 +1000889 char msg[UEVENT_MSG_LEN+2];
890 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700891 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700892 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700893 continue;
894
895 msg[n] = '\0';
896 msg[n+1] = '\0';
897
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700898 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700899 parse_event(msg, &uevent);
900
Nick Kralevich4d870952015-06-12 22:03:50 -0700901 if (selinux_status_updated() > 0) {
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400902 struct selabel_handle *sehandle2;
903 sehandle2 = selinux_android_file_context_handle();
904 if (sehandle2) {
905 selabel_close(sehandle);
906 sehandle = sehandle2;
907 }
908 }
909
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700910 handle_device_event(&uevent);
911 handle_firmware_event(&uevent);
912 }
913}
914
915/* Coldboot walks parts of the /sys tree and pokes the uevent files
916** to cause the kernel to regenerate device add events that happened
917** before init's device manager was started
918**
919** We drain any pending events from the netlink socket every time
920** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800921** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700922*/
923
Colin Cross0dd7ca62010-04-13 19:25:51 -0700924static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700925{
926 struct dirent *de;
927 int dfd, fd;
928
929 dfd = dirfd(d);
930
931 fd = openat(dfd, "uevent", O_WRONLY);
932 if(fd >= 0) {
933 write(fd, "add\n", 4);
934 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700935 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700936 }
937
938 while((de = readdir(d))) {
939 DIR *d2;
940
941 if(de->d_type != DT_DIR || de->d_name[0] == '.')
942 continue;
943
944 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
945 if(fd < 0)
946 continue;
947
948 d2 = fdopendir(fd);
949 if(d2 == 0)
950 close(fd);
951 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700952 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700953 closedir(d2);
954 }
955 }
956}
957
Colin Cross0dd7ca62010-04-13 19:25:51 -0700958static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700959{
960 DIR *d = opendir(path);
961 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700962 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700963 closedir(d);
964 }
965}
966
Elliott Hughes74738362015-03-28 10:51:23 -0700967void device_init() {
Nick Kralevich4d870952015-06-12 22:03:50 -0700968 sehandle = selinux_android_file_context_handle();
969 selinux_status_open(true);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700970
Andrew Boied562ca72012-12-04 15:47:20 -0800971 /* is 256K enough? udev uses 16MB! */
972 device_fd = uevent_open_socket(256*1024, true);
Elliott Hughes56a06562015-03-28 11:23:32 -0700973 if (device_fd == -1) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700974 return;
Elliott Hughes56a06562015-03-28 11:23:32 -0700975 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700976 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700977
Elliott Hughes56a06562015-03-28 11:23:32 -0700978 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700979 NOTICE("Skipping coldboot, already done!\n");
Elliott Hughes56a06562015-03-28 11:23:32 -0700980 return;
Colin Crossf83d0b92010-04-21 12:04:20 -0700981 }
Elliott Hughes56a06562015-03-28 11:23:32 -0700982
983 Timer t;
984 coldboot("/sys/class");
985 coldboot("/sys/block");
986 coldboot("/sys/devices");
987 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
988 NOTICE("Coldboot took %.2fs.\n", t.duration());
Colin Cross0dd7ca62010-04-13 19:25:51 -0700989}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700990
Colin Cross0dd7ca62010-04-13 19:25:51 -0700991int get_device_fd()
992{
993 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700994}