blob: db402c68de7e5e59cf3bd67883bf836340c7a000 [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>
Dima Zavinda04c522011-09-01 17:09:44 -070046#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100047#include <cutils/uevent.h>
48
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070049#include "devices.h"
Greg Hackmann3312aa82013-11-18 15:24:40 -080050#include "ueventd_parser.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070051#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070052#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054#define SYSFS_PREFIX "/sys"
Daniel Rosenbergd1d96022015-03-20 15:10:29 -070055static const char *firmware_dirs[] = { "/etc/firmware",
56 "/vendor/firmware",
57 "/firmware/image" };
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070058
Stephen Smalleye096e362012-06-11 13:37:39 -040059extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050060
Colin Cross0dd7ca62010-04-13 19:25:51 -070061static int device_fd = -1;
62
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070063struct uevent {
64 const char *action;
65 const char *path;
66 const char *subsystem;
67 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070068 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070069 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070070 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070071 int major;
72 int minor;
73};
74
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075struct perms_ {
76 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070077 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070078 mode_t perm;
79 unsigned int uid;
80 unsigned int gid;
81 unsigned short prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070082 unsigned short wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070083};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070084
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070085struct perm_node {
86 struct perms_ dp;
87 struct listnode plist;
88};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070089
Colin Crossfadb85e2011-03-30 18:32:12 -070090struct platform_node {
91 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080092 char *path;
93 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070094 struct listnode list;
95};
96
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070097static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070098static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070099static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700100
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700101int add_dev_perms(const char *name, const char *attr,
102 mode_t perm, unsigned int uid, unsigned int gid,
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700103 unsigned short prefix,
104 unsigned short wildcard) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800105 struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700106 if (!node)
107 return -ENOMEM;
108
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700109 node->dp.name = strdup(name);
110 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700111 return -ENOMEM;
112
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700113 if (attr) {
114 node->dp.attr = strdup(attr);
115 if (!node->dp.attr)
116 return -ENOMEM;
117 }
118
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700119 node->dp.perm = perm;
120 node->dp.uid = uid;
121 node->dp.gid = gid;
122 node->dp.prefix = prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700123 node->dp.wildcard = wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700124
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700125 if (attr)
126 list_add_tail(&sys_perms, &node->plist);
127 else
128 list_add_tail(&dev_perms, &node->plist);
129
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700130 return 0;
131}
132
Colin Cross43d537e2014-07-02 13:08:13 -0700133static bool perm_path_matches(const char *path, struct perms_ *dp)
134{
135 if (dp->prefix) {
136 if (strncmp(path, dp->name, strlen(dp->name)) == 0)
137 return true;
138 } else if (dp->wildcard) {
139 if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
140 return true;
141 } else {
142 if (strcmp(path, dp->name) == 0)
143 return true;
144 }
145
146 return false;
147}
148
Rob Herringe5636a32016-05-06 12:28:48 -0500149void fixup_sys_perms(const char *upath)
150{
151 struct listnode *node;
152
153 /* upaths omit the "/sys" that paths in this list
154 * contain, so we prepend it...
155 */
156 std::string path = SYSFS_PREFIX;
157 path += upath;
158
159 list_for_each(node, &sys_perms) {
160 perms_ *dp;
161
162 dp = &(node_to_item(node, struct perm_node, plist))->dp;
163 if (!perm_path_matches(path.c_str(), dp)) {
164 continue;
165 }
166
167 std::string attr_file = path + "/" + dp->attr;
168 INFO("fixup %s %d %d 0%o\n", attr_file.c_str(), dp->uid, dp->gid, dp->perm);
169 chown(attr_file.c_str(), dp->uid, dp->gid);
170 chmod(attr_file.c_str(), dp->perm);
171 }
172
173 if (access(path.c_str(), F_OK) == 0) {
174 INFO("restorecon_recursive: %s\n", path.c_str());
175 restorecon_recursive(path.c_str());
176 }
177}
178
Colin Cross43d537e2014-07-02 13:08:13 -0700179static mode_t get_device_perm(const char *path, const char **links,
180 unsigned *uid, unsigned *gid)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700181{
Colin Cross44b65d02010-04-20 14:32:50 -0700182 struct listnode *node;
183 struct perm_node *perm_node;
184 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700185
Colin Cross44b65d02010-04-20 14:32:50 -0700186 /* search the perms list in reverse so that ueventd.$hardware can
187 * override ueventd.rc
188 */
189 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700190 bool match = false;
191
Colin Cross44b65d02010-04-20 14:32:50 -0700192 perm_node = node_to_item(node, struct perm_node, plist);
193 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700194
Colin Cross43d537e2014-07-02 13:08:13 -0700195 if (perm_path_matches(path, dp)) {
196 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700197 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700198 if (links) {
199 int i;
200 for (i = 0; links[i]; i++) {
201 if (perm_path_matches(links[i], dp)) {
202 match = true;
203 break;
204 }
205 }
206 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700207 }
Colin Cross43d537e2014-07-02 13:08:13 -0700208
209 if (match) {
210 *uid = dp->uid;
211 *gid = dp->gid;
212 return dp->perm;
213 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700214 }
Colin Cross44b65d02010-04-20 14:32:50 -0700215 /* Default if nothing found. */
216 *uid = 0;
217 *gid = 0;
218 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700219}
220
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700221static void make_device(const char *path,
Elliott Hughesf682b472015-02-06 12:19:48 -0800222 const char */*upath*/,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400223 int block, int major, int minor,
224 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700225{
226 unsigned uid;
227 unsigned gid;
228 mode_t mode;
229 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500230 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700231
Colin Cross43d537e2014-07-02 13:08:13 -0700232 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700233
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300234 if (selabel_lookup_best_match(sehandle, &secontext, path, links, mode)) {
235 ERROR("Device '%s' not created; cannot find SELinux label (%s)\n",
236 path, strerror(errno));
237 return;
238 }
Nick Kralevich4d870952015-06-12 22:03:50 -0700239 setfscreatecon(secontext);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700240
Colin Cross17dcc5c2010-09-03 12:25:34 -0700241 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800242 /* Temporarily change egid to avoid race condition setting the gid of the
243 * device node. Unforunately changing the euid would prevent creation of
244 * some device nodes, so the uid has to be set with chown() and is still
245 * racy. Fixing the gid race at least fixed the issue with system_server
246 * opening dynamic input devices under the AID_INPUT gid. */
247 setegid(gid);
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300248 /* If the node already exists update its SELinux label to handle cases when
249 * it was created with the wrong context during coldboot procedure. */
250 if (mknod(path, mode, dev) && (errno == EEXIST)) {
251 if (lsetfilecon(path, secontext)) {
252 ERROR("Cannot set '%s' SELinux label on '%s' device (%s)\n",
253 secontext, path, strerror(errno));
254 }
255 }
Nick Pelly6405c692010-01-21 18:13:39 -0800256 chown(path, uid, -1);
257 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700258
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300259 freecon(secontext);
260 setfscreatecon(NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700261}
262
Dima Zavinf395c922013-03-06 16:23:57 -0800263static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700264{
Dima Zavinf395c922013-03-06 16:23:57 -0800265 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700266 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800267 const char *name = path;
268
269 if (!strncmp(path, "/devices/", 9)) {
270 name += 9;
271 if (!strncmp(name, "platform/", 9))
272 name += 9;
273 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700274
Dima Zavinf395c922013-03-06 16:23:57 -0800275 INFO("adding platform device %s (%s)\n", name, path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700276
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800277 bus = (platform_node*) calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800278 bus->path = strdup(path);
279 bus->path_len = path_len;
280 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700281 list_add_tail(&platform_names, &bus->list);
282}
283
284/*
Dima Zavinf395c922013-03-06 16:23:57 -0800285 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700286 * platform device prefix. If it doesn't start with a platform device, return
287 * 0.
288 */
Dima Zavinf395c922013-03-06 16:23:57 -0800289static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700290{
Dima Zavinf395c922013-03-06 16:23:57 -0800291 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700292 struct listnode *node;
293 struct platform_node *bus;
294
295 list_for_each_reverse(node, &platform_names) {
296 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800297 if ((bus->path_len < path_len) &&
298 (path[bus->path_len] == '/') &&
299 !strncmp(path, bus->path, bus->path_len))
300 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700301 }
302
303 return NULL;
304}
305
Dima Zavinf395c922013-03-06 16:23:57 -0800306static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700307{
308 struct listnode *node;
309 struct platform_node *bus;
310
311 list_for_each_reverse(node, &platform_names) {
312 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800313 if (!strcmp(path, bus->path)) {
314 INFO("removing platform device %s\n", bus->name);
315 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700316 list_remove(node);
317 free(bus);
318 return;
319 }
320 }
321}
322
Andrew Boiea885d042013-09-13 17:41:20 -0700323/* Given a path that may start with a PCI device, populate the supplied buffer
324 * with the PCI domain/bus number and the peripheral ID and return 0.
325 * If it doesn't start with a PCI device, or there is some error, return -1 */
326static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
327{
328 const char *start, *end;
329
330 if (strncmp(path, "/devices/pci", 12))
331 return -1;
332
333 /* Beginning of the prefix is the initial "pci" after "/devices/" */
334 start = path + 9;
335
336 /* End of the prefix is two path '/' later, capturing the domain/bus number
337 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
338 end = strchr(start, '/');
339 if (!end)
340 return -1;
341 end = strchr(end + 1, '/');
342 if (!end)
343 return -1;
344
345 /* Make sure we have enough room for the string plus null terminator */
346 if (end - start + 1 > buf_sz)
347 return -1;
348
349 strncpy(buf, start, end - start);
350 buf[end - start] = '\0';
351 return 0;
352}
353
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700354static void parse_event(const char *msg, struct uevent *uevent)
355{
356 uevent->action = "";
357 uevent->path = "";
358 uevent->subsystem = "";
359 uevent->firmware = "";
360 uevent->major = -1;
361 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700362 uevent->partition_name = NULL;
363 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700364 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700365
366 /* currently ignoring SEQNUM */
367 while(*msg) {
368 if(!strncmp(msg, "ACTION=", 7)) {
369 msg += 7;
370 uevent->action = msg;
371 } else if(!strncmp(msg, "DEVPATH=", 8)) {
372 msg += 8;
373 uevent->path = msg;
374 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
375 msg += 10;
376 uevent->subsystem = msg;
377 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
378 msg += 9;
379 uevent->firmware = msg;
380 } else if(!strncmp(msg, "MAJOR=", 6)) {
381 msg += 6;
382 uevent->major = atoi(msg);
383 } else if(!strncmp(msg, "MINOR=", 6)) {
384 msg += 6;
385 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700386 } else if(!strncmp(msg, "PARTN=", 6)) {
387 msg += 6;
388 uevent->partition_num = atoi(msg);
389 } else if(!strncmp(msg, "PARTNAME=", 9)) {
390 msg += 9;
391 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700392 } else if(!strncmp(msg, "DEVNAME=", 8)) {
393 msg += 8;
394 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700395 }
396
Wei Zhongf97b8872012-03-23 14:15:34 -0700397 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700398 while(*msg++)
399 ;
400 }
401
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800402 if (LOG_UEVENTS) {
403 INFO("event { '%s', '%s', '%s', '%s', %d, %d }\n",
404 uevent->action, uevent->path, uevent->subsystem,
405 uevent->firmware, uevent->major, uevent->minor);
406 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700407}
408
Benoit Gobyd2278632010-08-03 14:36:02 -0700409static char **get_character_device_symlinks(struct uevent *uevent)
410{
411 const char *parent;
Dan Austin60b976d2016-03-28 14:22:12 -0700412 const char *slash;
Benoit Gobyd2278632010-08-03 14:36:02 -0700413 char **links;
414 int link_num = 0;
415 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800416 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700417
Dima Zavinf395c922013-03-06 16:23:57 -0800418 pdev = find_platform_device(uevent->path);
419 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700420 return NULL;
421
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800422 links = (char**) malloc(sizeof(char *) * 2);
Benoit Gobyd2278632010-08-03 14:36:02 -0700423 if (!links)
424 return NULL;
425 memset(links, 0, sizeof(char *) * 2);
426
427 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800428 parent = strchr(uevent->path + pdev->path_len, '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100429 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700430 goto err;
431
432 if (!strncmp(parent, "/usb", 4)) {
433 /* skip root hub name and device. use device interface */
434 while (*++parent && *parent != '/');
435 if (*parent)
436 while (*++parent && *parent != '/');
437 if (!*parent)
438 goto err;
439 slash = strchr(++parent, '/');
440 if (!slash)
441 goto err;
442 width = slash - parent;
443 if (width <= 0)
444 goto err;
445
446 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
447 link_num++;
448 else
449 links[link_num] = NULL;
450 mkdir("/dev/usb", 0755);
451 }
452 else {
453 goto err;
454 }
455
456 return links;
457err:
458 free(links);
459 return NULL;
460}
461
Andrew Boiea885d042013-09-13 17:41:20 -0700462static char **get_block_device_symlinks(struct uevent *uevent)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700463{
Colin Crossfadb85e2011-03-30 18:32:12 -0700464 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800465 struct platform_node *pdev;
Dan Austin60b976d2016-03-28 14:22:12 -0700466 const char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700467 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700468 char buf[256];
469 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700470 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700471 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700472
Dima Zavinf395c922013-03-06 16:23:57 -0800473 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700474 if (pdev) {
475 device = pdev->name;
476 type = "platform";
477 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
478 device = buf;
479 type = "pci";
480 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800481 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700482 }
Dima Zavinf395c922013-03-06 16:23:57 -0800483
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800484 char **links = (char**) malloc(sizeof(char *) * 4);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700485 if (!links)
486 return NULL;
487 memset(links, 0, sizeof(char *) * 4);
488
Andrew Boiea885d042013-09-13 17:41:20 -0700489 INFO("found %s device %s\n", type, device);
Colin Crossfadb85e2011-03-30 18:32:12 -0700490
Andrew Boiea885d042013-09-13 17:41:20 -0700491 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700492
493 if (uevent->partition_name) {
494 p = strdup(uevent->partition_name);
495 sanitize(p);
Johan Redestig93ca79b2012-04-18 16:41:19 +0200496 if (strcmp(uevent->partition_name, p))
497 NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700498 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
499 link_num++;
500 else
501 links[link_num] = NULL;
502 free(p);
503 }
504
505 if (uevent->partition_num >= 0) {
506 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
507 link_num++;
508 else
509 links[link_num] = NULL;
510 }
511
Dima Zavinf395c922013-03-06 16:23:57 -0800512 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700513 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
514 link_num++;
515 else
516 links[link_num] = NULL;
517
518 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700519}
520
Colin Crosseb5ba832011-03-30 17:37:17 -0700521static void handle_device(const char *action, const char *devpath,
522 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700523{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700524 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700525
Colin Crosseb5ba832011-03-30 17:37:17 -0700526 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400527 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700528 if (links) {
529 for (i = 0; links[i]; i++)
Chris Fries79f33842013-09-05 13:19:21 -0500530 make_link_init(devpath, links[i]);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700531 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700532 }
533
Colin Crosseb5ba832011-03-30 17:37:17 -0700534 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700535 if (links) {
536 for (i = 0; links[i]; i++)
537 remove_link(devpath, links[i]);
538 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700539 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700540 }
541
542 if (links) {
543 for (i = 0; links[i]; i++)
544 free(links[i]);
545 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700546 }
547}
548
Colin Crossfadb85e2011-03-30 18:32:12 -0700549static void handle_platform_device_event(struct uevent *uevent)
550{
Dima Zavinf395c922013-03-06 16:23:57 -0800551 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700552
553 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800554 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700555 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800556 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700557}
558
Colin Crosseb5ba832011-03-30 17:37:17 -0700559static const char *parse_device_name(struct uevent *uevent, unsigned int len)
560{
561 const char *name;
562
563 /* if it's not a /dev device, nothing else to do */
564 if((uevent->major < 0) || (uevent->minor < 0))
565 return NULL;
566
567 /* do we have a name? */
568 name = strrchr(uevent->path, '/');
569 if(!name)
570 return NULL;
571 name++;
572
573 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800574 if(strlen(name) > len) {
575 ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n",
576 name, len);
Colin Crosseb5ba832011-03-30 17:37:17 -0700577 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800578 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700579
580 return name;
581}
582
583static void handle_block_device_event(struct uevent *uevent)
584{
585 const char *base = "/dev/block/";
586 const char *name;
587 char devpath[96];
588 char **links = NULL;
589
590 name = parse_device_name(uevent, 64);
591 if (!name)
592 return;
593
594 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500595 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700596
Dima Zavinf395c922013-03-06 16:23:57 -0800597 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700598 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700599
600 handle_device(uevent->action, devpath, uevent->path, 1,
601 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700602}
603
Greg Hackmann3312aa82013-11-18 15:24:40 -0800604#define DEVPATH_LEN 96
605
606static bool assemble_devpath(char *devpath, const char *dirname,
607 const char *devname)
608{
609 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
610 if (s < 0) {
611 ERROR("failed to assemble device path (%s); ignoring event\n",
612 strerror(errno));
613 return false;
614 } else if (s >= DEVPATH_LEN) {
615 ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n",
616 dirname, devname, DEVPATH_LEN);
617 return false;
618 }
619 return true;
620}
621
622static void mkdir_recursive_for_devpath(const char *devpath)
623{
624 char dir[DEVPATH_LEN];
625 char *slash;
626
627 strcpy(dir, devpath);
628 slash = strrchr(dir, '/');
629 *slash = '\0';
630 mkdir_recursive(dir, 0755);
631}
632
Colin Crosseb5ba832011-03-30 17:37:17 -0700633static void handle_generic_device_event(struct uevent *uevent)
634{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800635 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700636 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800637 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700638 char **links = NULL;
639
640 name = parse_device_name(uevent, 64);
641 if (!name)
642 return;
643
Greg Hackmann3312aa82013-11-18 15:24:40 -0800644 struct ueventd_subsystem *subsystem =
645 ueventd_subsystem_find_by_name(uevent->subsystem);
646
647 if (subsystem) {
648 const char *devname;
649
650 switch (subsystem->devname_src) {
651 case DEVNAME_UEVENT_DEVNAME:
652 devname = uevent->device_name;
653 break;
654
655 case DEVNAME_UEVENT_DEVPATH:
656 devname = name;
657 break;
658
659 default:
660 ERROR("%s subsystem's devpath option is not set; ignoring event\n",
661 uevent->subsystem);
662 return;
663 }
664
665 if (!assemble_devpath(devpath, subsystem->dirname, devname))
666 return;
667 mkdir_recursive_for_devpath(devpath);
668 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700669 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700670 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800671 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800672 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800673 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700674 }
675 else {
676 /* This imitates the file system that would be created
677 * if we were using devfs instead.
678 * Minors are broken up into groups of 128, starting at "001"
679 */
680 int bus_id = uevent->minor / 128 + 1;
681 int device_id = uevent->minor % 128 + 1;
682 /* build directories */
683 make_dir("/dev/bus", 0755);
684 make_dir("/dev/bus/usb", 0755);
685 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
686 make_dir(devpath, 0755);
687 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
688 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700689 } else {
690 /* ignore other USB events */
691 return;
692 }
693 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
694 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500695 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200696 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
697 base = "/dev/dri/";
698 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700699 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
700 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500701 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700702 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
703 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500704 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700705 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
706 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500707 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700708 } else if(!strncmp(uevent->subsystem, "input", 5)) {
709 base = "/dev/input/";
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, "mtd", 3)) {
712 base = "/dev/mtd/";
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, "sound", 5)) {
715 base = "/dev/snd/";
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, "misc", 4) &&
718 !strncmp(name, "log_", 4)) {
Elliott Hughesf682b472015-02-06 12:19:48 -0800719 INFO("kernel logger is deprecated\n");
Colin Crosseb5ba832011-03-30 17:37:17 -0700720 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500721 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700722 name += 4;
723 } else
724 base = "/dev/";
725 links = get_character_device_symlinks(uevent);
726
727 if (!devpath[0])
728 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
729
730 handle_device(uevent->action, devpath, uevent->path, 0,
731 uevent->major, uevent->minor, links);
732}
733
734static void handle_device_event(struct uevent *uevent)
735{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700736 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700737 fixup_sys_perms(uevent->path);
738
739 if (!strncmp(uevent->subsystem, "block", 5)) {
740 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700741 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
742 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700743 } else {
744 handle_generic_device_event(uevent);
745 }
746}
747
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700748static int load_firmware(int fw_fd, int loading_fd, int data_fd)
749{
750 struct stat st;
751 long len_to_copy;
752 int ret = 0;
753
754 if(fstat(fw_fd, &st) < 0)
755 return -1;
756 len_to_copy = st.st_size;
757
758 write(loading_fd, "1", 1); /* start transfer */
759
760 while (len_to_copy > 0) {
761 char buf[PAGE_SIZE];
762 ssize_t nr;
763
764 nr = read(fw_fd, buf, sizeof(buf));
765 if(!nr)
766 break;
767 if(nr < 0) {
768 ret = -1;
769 break;
770 }
Biao Ludc848562016-01-28 16:10:54 +0800771 if (!android::base::WriteFully(data_fd, buf, nr)) {
772 ret = -1;
773 break;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700774 }
Biao Ludc848562016-01-28 16:10:54 +0800775 len_to_copy -= nr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700776 }
777
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700778 if(!ret)
779 write(loading_fd, "0", 1); /* successful end of transfer */
780 else
781 write(loading_fd, "-1", 2); /* abort transfer */
782
783 return ret;
784}
785
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700786static int is_booting(void)
787{
788 return access("/dev/.booting", F_OK) == 0;
789}
790
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700791static void process_firmware_event(struct uevent *uevent)
792{
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700793 char *root, *loading, *data;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700794 int l, loading_fd, data_fd, fw_fd;
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700795 size_t i;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700796 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700797
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700798 INFO("firmware: loading '%s' for '%s'\n",
799 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700800
801 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
802 if (l == -1)
803 return;
804
805 l = asprintf(&loading, "%sloading", root);
806 if (l == -1)
807 goto root_free_out;
808
809 l = asprintf(&data, "%sdata", root);
810 if (l == -1)
811 goto loading_free_out;
812
Nick Kralevich45a884f2015-02-02 14:37:22 -0800813 loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700814 if(loading_fd < 0)
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700815 goto data_free_out;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700816
Nick Kralevich45a884f2015-02-02 14:37:22 -0800817 data_fd = open(data, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700818 if(data_fd < 0)
819 goto loading_close_out;
820
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700821try_loading_again:
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700822 for (i = 0; i < ARRAY_SIZE(firmware_dirs); i++) {
823 char *file = NULL;
824 l = asprintf(&file, "%s/%s", firmware_dirs[i], uevent->firmware);
825 if (l == -1)
826 goto data_free_out;
827 fw_fd = open(file, O_RDONLY|O_CLOEXEC);
828 free(file);
829 if (fw_fd >= 0) {
830 if(!load_firmware(fw_fd, loading_fd, data_fd))
831 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
832 else
833 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
834 break;
Benoit Goby609d8822010-11-09 18:10:24 -0800835 }
Brian Swetland02863b92010-09-19 03:36:39 -0700836 }
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700837 if (fw_fd < 0) {
838 if (booting) {
839 /* If we're not fully booted, we may be missing
840 * filesystems needed for firmware, wait and retry.
841 */
842 usleep(100000);
843 booting = is_booting();
844 goto try_loading_again;
845 }
Elliott Hughescd67f002015-03-20 17:05:56 -0700846 INFO("firmware: could not open '%s': %s\n", uevent->firmware, strerror(errno));
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700847 write(loading_fd, "-1", 2);
848 goto data_close_out;
849 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700850
851 close(fw_fd);
852data_close_out:
853 close(data_fd);
854loading_close_out:
855 close(loading_fd);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700856data_free_out:
857 free(data);
858loading_free_out:
859 free(loading);
860root_free_out:
861 free(root);
862}
863
864static void handle_firmware_event(struct uevent *uevent)
865{
866 pid_t pid;
867
868 if(strcmp(uevent->subsystem, "firmware"))
869 return;
870
871 if(strcmp(uevent->action, "add"))
872 return;
873
874 /* we fork, to avoid making large memory allocations in init proper */
875 pid = fork();
876 if (!pid) {
877 process_firmware_event(uevent);
Kenny Root17baff42014-08-20 16:16:44 -0700878 _exit(EXIT_SUCCESS);
879 } else if (pid < 0) {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800880 ERROR("could not fork to process firmware event: %s\n", strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700881 }
882}
883
Ruchi Kandoic6037202014-06-23 11:22:09 -0700884#define UEVENT_MSG_LEN 2048
Colin Cross0dd7ca62010-04-13 19:25:51 -0700885void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700886{
Vernon Tang3f582e92011-04-25 13:08:17 +1000887 char msg[UEVENT_MSG_LEN+2];
888 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700889 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700890 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700891 continue;
892
893 msg[n] = '\0';
894 msg[n+1] = '\0';
895
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700896 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700897 parse_event(msg, &uevent);
898
Nick Kralevich4d870952015-06-12 22:03:50 -0700899 if (selinux_status_updated() > 0) {
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400900 struct selabel_handle *sehandle2;
901 sehandle2 = selinux_android_file_context_handle();
902 if (sehandle2) {
903 selabel_close(sehandle);
904 sehandle = sehandle2;
905 }
906 }
907
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700908 handle_device_event(&uevent);
909 handle_firmware_event(&uevent);
910 }
911}
912
913/* Coldboot walks parts of the /sys tree and pokes the uevent files
914** to cause the kernel to regenerate device add events that happened
915** before init's device manager was started
916**
917** We drain any pending events from the netlink socket every time
918** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800919** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700920*/
921
Colin Cross0dd7ca62010-04-13 19:25:51 -0700922static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700923{
924 struct dirent *de;
925 int dfd, fd;
926
927 dfd = dirfd(d);
928
929 fd = openat(dfd, "uevent", O_WRONLY);
930 if(fd >= 0) {
931 write(fd, "add\n", 4);
932 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700933 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700934 }
935
936 while((de = readdir(d))) {
937 DIR *d2;
938
939 if(de->d_type != DT_DIR || de->d_name[0] == '.')
940 continue;
941
942 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
943 if(fd < 0)
944 continue;
945
946 d2 = fdopendir(fd);
947 if(d2 == 0)
948 close(fd);
949 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700950 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700951 closedir(d2);
952 }
953 }
954}
955
Colin Cross0dd7ca62010-04-13 19:25:51 -0700956static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700957{
James Hawkins588a2ca2016-02-18 14:52:46 -0800958 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700959 if(d) {
James Hawkins588a2ca2016-02-18 14:52:46 -0800960 do_coldboot(d.get());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700961 }
962}
963
Elliott Hughes74738362015-03-28 10:51:23 -0700964void device_init() {
Nick Kralevich4d870952015-06-12 22:03:50 -0700965 sehandle = selinux_android_file_context_handle();
966 selinux_status_open(true);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700967
Andrew Boied562ca72012-12-04 15:47:20 -0800968 /* is 256K enough? udev uses 16MB! */
969 device_fd = uevent_open_socket(256*1024, true);
Elliott Hughes56a06562015-03-28 11:23:32 -0700970 if (device_fd == -1) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700971 return;
Elliott Hughes56a06562015-03-28 11:23:32 -0700972 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700973 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700974
Elliott Hughes56a06562015-03-28 11:23:32 -0700975 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700976 NOTICE("Skipping coldboot, already done!\n");
Elliott Hughes56a06562015-03-28 11:23:32 -0700977 return;
Colin Crossf83d0b92010-04-21 12:04:20 -0700978 }
Elliott Hughes56a06562015-03-28 11:23:32 -0700979
980 Timer t;
981 coldboot("/sys/class");
982 coldboot("/sys/block");
983 coldboot("/sys/devices");
984 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
985 NOTICE("Coldboot took %.2fs.\n", t.duration());
Colin Cross0dd7ca62010-04-13 19:25:51 -0700986}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700987
Colin Cross0dd7ca62010-04-13 19:25:51 -0700988int get_device_fd()
989{
990 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700991}