blob: d556e307c5385f12182af8c8d1b77418ce697c6c [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
Dima Zavinda04c522011-09-01 17:09:44 -070043#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100044#include <cutils/uevent.h>
45
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070046#include "devices.h"
Greg Hackmann3312aa82013-11-18 15:24:40 -080047#include "ueventd_parser.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070048#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070049#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070050
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070051#define SYSFS_PREFIX "/sys"
Daniel Rosenbergd1d96022015-03-20 15:10:29 -070052static const char *firmware_dirs[] = { "/etc/firmware",
53 "/vendor/firmware",
54 "/firmware/image" };
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070055
Stephen Smalleye096e362012-06-11 13:37:39 -040056extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050057
Colin Cross0dd7ca62010-04-13 19:25:51 -070058static int device_fd = -1;
59
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060struct uevent {
61 const char *action;
62 const char *path;
63 const char *subsystem;
64 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070065 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070066 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070067 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068 int major;
69 int minor;
70};
71
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072struct perms_ {
73 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070074 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075 mode_t perm;
76 unsigned int uid;
77 unsigned int gid;
78 unsigned short prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070079 unsigned short wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070080};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070081
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070082struct perm_node {
83 struct perms_ dp;
84 struct listnode plist;
85};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070086
Colin Crossfadb85e2011-03-30 18:32:12 -070087struct platform_node {
88 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080089 char *path;
90 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070091 struct listnode list;
92};
93
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070094static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070095static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070096static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070097
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070098int add_dev_perms(const char *name, const char *attr,
99 mode_t perm, unsigned int uid, unsigned int gid,
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700100 unsigned short prefix,
101 unsigned short wildcard) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800102 struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700103 if (!node)
104 return -ENOMEM;
105
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700106 node->dp.name = strdup(name);
107 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700108 return -ENOMEM;
109
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700110 if (attr) {
111 node->dp.attr = strdup(attr);
112 if (!node->dp.attr)
113 return -ENOMEM;
114 }
115
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700116 node->dp.perm = perm;
117 node->dp.uid = uid;
118 node->dp.gid = gid;
119 node->dp.prefix = prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700120 node->dp.wildcard = wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700121
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700122 if (attr)
123 list_add_tail(&sys_perms, &node->plist);
124 else
125 list_add_tail(&dev_perms, &node->plist);
126
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700127 return 0;
128}
129
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700130void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700131{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700132 char buf[512];
133 struct listnode *node;
134 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700135
Nick Kralevich3b4c0bd2014-07-03 16:04:34 -0700136 /* upaths omit the "/sys" that paths in this list
137 * contain, so we add 4 when comparing...
138 */
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700139 list_for_each(node, &sys_perms) {
140 dp = &(node_to_item(node, struct perm_node, plist))->dp;
141 if (dp->prefix) {
142 if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700143 continue;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700144 } else if (dp->wildcard) {
145 if (fnmatch(dp->name + 4, upath, FNM_PATHNAME) != 0)
146 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700147 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700148 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700149 continue;
150 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700151
152 if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
Nick Kralevich3b4c0bd2014-07-03 16:04:34 -0700153 break;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700154
Yabin Cuie2d63af2015-02-17 19:27:51 -0800155 snprintf(buf, sizeof(buf), "/sys%s/%s", upath, dp->attr);
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700156 INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
157 chown(buf, dp->uid, dp->gid);
158 chmod(buf, dp->perm);
Nick Kralevich3b4c0bd2014-07-03 16:04:34 -0700159 }
160
161 // Now fixup SELinux file labels
162 int len = snprintf(buf, sizeof(buf), "/sys%s", upath);
163 if ((len < 0) || ((size_t) len >= sizeof(buf))) {
164 // Overflow
165 return;
166 }
167 if (access(buf, F_OK) == 0) {
168 INFO("restorecon_recursive: %s\n", buf);
169 restorecon_recursive(buf);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700170 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700171}
172
Colin Cross43d537e2014-07-02 13:08:13 -0700173static bool perm_path_matches(const char *path, struct perms_ *dp)
174{
175 if (dp->prefix) {
176 if (strncmp(path, dp->name, strlen(dp->name)) == 0)
177 return true;
178 } else if (dp->wildcard) {
179 if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
180 return true;
181 } else {
182 if (strcmp(path, dp->name) == 0)
183 return true;
184 }
185
186 return false;
187}
188
189static mode_t get_device_perm(const char *path, const char **links,
190 unsigned *uid, unsigned *gid)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700191{
Colin Cross44b65d02010-04-20 14:32:50 -0700192 struct listnode *node;
193 struct perm_node *perm_node;
194 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700195
Colin Cross44b65d02010-04-20 14:32:50 -0700196 /* search the perms list in reverse so that ueventd.$hardware can
197 * override ueventd.rc
198 */
199 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700200 bool match = false;
201
Colin Cross44b65d02010-04-20 14:32:50 -0700202 perm_node = node_to_item(node, struct perm_node, plist);
203 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700204
Colin Cross43d537e2014-07-02 13:08:13 -0700205 if (perm_path_matches(path, dp)) {
206 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700207 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700208 if (links) {
209 int i;
210 for (i = 0; links[i]; i++) {
211 if (perm_path_matches(links[i], dp)) {
212 match = true;
213 break;
214 }
215 }
216 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700217 }
Colin Cross43d537e2014-07-02 13:08:13 -0700218
219 if (match) {
220 *uid = dp->uid;
221 *gid = dp->gid;
222 return dp->perm;
223 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700224 }
Colin Cross44b65d02010-04-20 14:32:50 -0700225 /* Default if nothing found. */
226 *uid = 0;
227 *gid = 0;
228 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700229}
230
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700231static void make_device(const char *path,
Elliott Hughesf682b472015-02-06 12:19:48 -0800232 const char */*upath*/,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400233 int block, int major, int minor,
234 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700235{
236 unsigned uid;
237 unsigned gid;
238 mode_t mode;
239 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500240 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700241
Colin Cross43d537e2014-07-02 13:08:13 -0700242 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700243
Nick Kralevich4d870952015-06-12 22:03:50 -0700244 selabel_lookup_best_match(sehandle, &secontext, path, links, mode);
245 setfscreatecon(secontext);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700246
Colin Cross17dcc5c2010-09-03 12:25:34 -0700247 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800248 /* Temporarily change egid to avoid race condition setting the gid of the
249 * device node. Unforunately changing the euid would prevent creation of
250 * some device nodes, so the uid has to be set with chown() and is still
251 * racy. Fixing the gid race at least fixed the issue with system_server
252 * opening dynamic input devices under the AID_INPUT gid. */
253 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700254 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800255 chown(path, uid, -1);
256 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700257
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500258 if (secontext) {
259 freecon(secontext);
260 setfscreatecon(NULL);
261 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700262}
263
Dima Zavinf395c922013-03-06 16:23:57 -0800264static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700265{
Dima Zavinf395c922013-03-06 16:23:57 -0800266 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700267 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800268 const char *name = path;
269
270 if (!strncmp(path, "/devices/", 9)) {
271 name += 9;
272 if (!strncmp(name, "platform/", 9))
273 name += 9;
274 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700275
Dima Zavinf395c922013-03-06 16:23:57 -0800276 INFO("adding platform device %s (%s)\n", name, path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700277
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800278 bus = (platform_node*) calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800279 bus->path = strdup(path);
280 bus->path_len = path_len;
281 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700282 list_add_tail(&platform_names, &bus->list);
283}
284
285/*
Dima Zavinf395c922013-03-06 16:23:57 -0800286 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700287 * platform device prefix. If it doesn't start with a platform device, return
288 * 0.
289 */
Dima Zavinf395c922013-03-06 16:23:57 -0800290static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700291{
Dima Zavinf395c922013-03-06 16:23:57 -0800292 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700293 struct listnode *node;
294 struct platform_node *bus;
295
296 list_for_each_reverse(node, &platform_names) {
297 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800298 if ((bus->path_len < path_len) &&
299 (path[bus->path_len] == '/') &&
300 !strncmp(path, bus->path, bus->path_len))
301 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700302 }
303
304 return NULL;
305}
306
Dima Zavinf395c922013-03-06 16:23:57 -0800307static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700308{
309 struct listnode *node;
310 struct platform_node *bus;
311
312 list_for_each_reverse(node, &platform_names) {
313 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800314 if (!strcmp(path, bus->path)) {
315 INFO("removing platform device %s\n", bus->name);
316 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700317 list_remove(node);
318 free(bus);
319 return;
320 }
321 }
322}
323
Andrew Boiea885d042013-09-13 17:41:20 -0700324/* Given a path that may start with a PCI device, populate the supplied buffer
325 * with the PCI domain/bus number and the peripheral ID and return 0.
326 * If it doesn't start with a PCI device, or there is some error, return -1 */
327static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
328{
329 const char *start, *end;
330
331 if (strncmp(path, "/devices/pci", 12))
332 return -1;
333
334 /* Beginning of the prefix is the initial "pci" after "/devices/" */
335 start = path + 9;
336
337 /* End of the prefix is two path '/' later, capturing the domain/bus number
338 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
339 end = strchr(start, '/');
340 if (!end)
341 return -1;
342 end = strchr(end + 1, '/');
343 if (!end)
344 return -1;
345
346 /* Make sure we have enough room for the string plus null terminator */
347 if (end - start + 1 > buf_sz)
348 return -1;
349
350 strncpy(buf, start, end - start);
351 buf[end - start] = '\0';
352 return 0;
353}
354
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700355static void parse_event(const char *msg, struct uevent *uevent)
356{
357 uevent->action = "";
358 uevent->path = "";
359 uevent->subsystem = "";
360 uevent->firmware = "";
361 uevent->major = -1;
362 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700363 uevent->partition_name = NULL;
364 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700365 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700366
367 /* currently ignoring SEQNUM */
368 while(*msg) {
369 if(!strncmp(msg, "ACTION=", 7)) {
370 msg += 7;
371 uevent->action = msg;
372 } else if(!strncmp(msg, "DEVPATH=", 8)) {
373 msg += 8;
374 uevent->path = msg;
375 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
376 msg += 10;
377 uevent->subsystem = msg;
378 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
379 msg += 9;
380 uevent->firmware = msg;
381 } else if(!strncmp(msg, "MAJOR=", 6)) {
382 msg += 6;
383 uevent->major = atoi(msg);
384 } else if(!strncmp(msg, "MINOR=", 6)) {
385 msg += 6;
386 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700387 } else if(!strncmp(msg, "PARTN=", 6)) {
388 msg += 6;
389 uevent->partition_num = atoi(msg);
390 } else if(!strncmp(msg, "PARTNAME=", 9)) {
391 msg += 9;
392 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700393 } else if(!strncmp(msg, "DEVNAME=", 8)) {
394 msg += 8;
395 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700396 }
397
Wei Zhongf97b8872012-03-23 14:15:34 -0700398 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700399 while(*msg++)
400 ;
401 }
402
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800403 if (LOG_UEVENTS) {
404 INFO("event { '%s', '%s', '%s', '%s', %d, %d }\n",
405 uevent->action, uevent->path, uevent->subsystem,
406 uevent->firmware, uevent->major, uevent->minor);
407 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700408}
409
Benoit Gobyd2278632010-08-03 14:36:02 -0700410static char **get_character_device_symlinks(struct uevent *uevent)
411{
412 const char *parent;
413 char *slash;
414 char **links;
415 int link_num = 0;
416 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800417 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700418
Dima Zavinf395c922013-03-06 16:23:57 -0800419 pdev = find_platform_device(uevent->path);
420 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700421 return NULL;
422
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800423 links = (char**) malloc(sizeof(char *) * 2);
Benoit Gobyd2278632010-08-03 14:36:02 -0700424 if (!links)
425 return NULL;
426 memset(links, 0, sizeof(char *) * 2);
427
428 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800429 parent = strchr(uevent->path + pdev->path_len, '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100430 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700431 goto err;
432
433 if (!strncmp(parent, "/usb", 4)) {
434 /* skip root hub name and device. use device interface */
435 while (*++parent && *parent != '/');
436 if (*parent)
437 while (*++parent && *parent != '/');
438 if (!*parent)
439 goto err;
440 slash = strchr(++parent, '/');
441 if (!slash)
442 goto err;
443 width = slash - parent;
444 if (width <= 0)
445 goto err;
446
447 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
448 link_num++;
449 else
450 links[link_num] = NULL;
451 mkdir("/dev/usb", 0755);
452 }
453 else {
454 goto err;
455 }
456
457 return links;
458err:
459 free(links);
460 return NULL;
461}
462
Andrew Boiea885d042013-09-13 17:41:20 -0700463static char **get_block_device_symlinks(struct uevent *uevent)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700464{
Colin Crossfadb85e2011-03-30 18:32:12 -0700465 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800466 struct platform_node *pdev;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700467 char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700468 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700469 char buf[256];
470 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700471 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700472 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700473
Dima Zavinf395c922013-03-06 16:23:57 -0800474 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700475 if (pdev) {
476 device = pdev->name;
477 type = "platform";
478 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
479 device = buf;
480 type = "pci";
481 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800482 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700483 }
Dima Zavinf395c922013-03-06 16:23:57 -0800484
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800485 char **links = (char**) malloc(sizeof(char *) * 4);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700486 if (!links)
487 return NULL;
488 memset(links, 0, sizeof(char *) * 4);
489
Andrew Boiea885d042013-09-13 17:41:20 -0700490 INFO("found %s device %s\n", type, device);
Colin Crossfadb85e2011-03-30 18:32:12 -0700491
Andrew Boiea885d042013-09-13 17:41:20 -0700492 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700493
494 if (uevent->partition_name) {
495 p = strdup(uevent->partition_name);
496 sanitize(p);
Johan Redestig93ca79b2012-04-18 16:41:19 +0200497 if (strcmp(uevent->partition_name, p))
498 NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700499 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
500 link_num++;
501 else
502 links[link_num] = NULL;
503 free(p);
504 }
505
506 if (uevent->partition_num >= 0) {
507 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
508 link_num++;
509 else
510 links[link_num] = NULL;
511 }
512
Dima Zavinf395c922013-03-06 16:23:57 -0800513 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700514 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
515 link_num++;
516 else
517 links[link_num] = NULL;
518
519 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700520}
521
Colin Crosseb5ba832011-03-30 17:37:17 -0700522static void handle_device(const char *action, const char *devpath,
523 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700524{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700525 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700526
Colin Crosseb5ba832011-03-30 17:37:17 -0700527 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400528 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700529 if (links) {
530 for (i = 0; links[i]; i++)
Chris Fries79f33842013-09-05 13:19:21 -0500531 make_link_init(devpath, links[i]);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700532 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700533 }
534
Colin Crosseb5ba832011-03-30 17:37:17 -0700535 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700536 if (links) {
537 for (i = 0; links[i]; i++)
538 remove_link(devpath, links[i]);
539 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700540 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700541 }
542
543 if (links) {
544 for (i = 0; links[i]; i++)
545 free(links[i]);
546 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700547 }
548}
549
Colin Crossfadb85e2011-03-30 18:32:12 -0700550static void handle_platform_device_event(struct uevent *uevent)
551{
Dima Zavinf395c922013-03-06 16:23:57 -0800552 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700553
554 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800555 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700556 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800557 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700558}
559
Colin Crosseb5ba832011-03-30 17:37:17 -0700560static const char *parse_device_name(struct uevent *uevent, unsigned int len)
561{
562 const char *name;
563
564 /* if it's not a /dev device, nothing else to do */
565 if((uevent->major < 0) || (uevent->minor < 0))
566 return NULL;
567
568 /* do we have a name? */
569 name = strrchr(uevent->path, '/');
570 if(!name)
571 return NULL;
572 name++;
573
574 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800575 if(strlen(name) > len) {
576 ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n",
577 name, len);
Colin Crosseb5ba832011-03-30 17:37:17 -0700578 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800579 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700580
581 return name;
582}
583
584static void handle_block_device_event(struct uevent *uevent)
585{
586 const char *base = "/dev/block/";
587 const char *name;
588 char devpath[96];
589 char **links = NULL;
590
591 name = parse_device_name(uevent, 64);
592 if (!name)
593 return;
594
595 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500596 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700597
Dima Zavinf395c922013-03-06 16:23:57 -0800598 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700599 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700600
601 handle_device(uevent->action, devpath, uevent->path, 1,
602 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700603}
604
Greg Hackmann3312aa82013-11-18 15:24:40 -0800605#define DEVPATH_LEN 96
606
607static bool assemble_devpath(char *devpath, const char *dirname,
608 const char *devname)
609{
610 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
611 if (s < 0) {
612 ERROR("failed to assemble device path (%s); ignoring event\n",
613 strerror(errno));
614 return false;
615 } else if (s >= DEVPATH_LEN) {
616 ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n",
617 dirname, devname, DEVPATH_LEN);
618 return false;
619 }
620 return true;
621}
622
623static void mkdir_recursive_for_devpath(const char *devpath)
624{
625 char dir[DEVPATH_LEN];
626 char *slash;
627
628 strcpy(dir, devpath);
629 slash = strrchr(dir, '/');
630 *slash = '\0';
631 mkdir_recursive(dir, 0755);
632}
633
Colin Crosseb5ba832011-03-30 17:37:17 -0700634static void handle_generic_device_event(struct uevent *uevent)
635{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800636 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700637 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800638 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700639 char **links = NULL;
640
641 name = parse_device_name(uevent, 64);
642 if (!name)
643 return;
644
Greg Hackmann3312aa82013-11-18 15:24:40 -0800645 struct ueventd_subsystem *subsystem =
646 ueventd_subsystem_find_by_name(uevent->subsystem);
647
648 if (subsystem) {
649 const char *devname;
650
651 switch (subsystem->devname_src) {
652 case DEVNAME_UEVENT_DEVNAME:
653 devname = uevent->device_name;
654 break;
655
656 case DEVNAME_UEVENT_DEVPATH:
657 devname = name;
658 break;
659
660 default:
661 ERROR("%s subsystem's devpath option is not set; ignoring event\n",
662 uevent->subsystem);
663 return;
664 }
665
666 if (!assemble_devpath(devpath, subsystem->dirname, devname))
667 return;
668 mkdir_recursive_for_devpath(devpath);
669 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700670 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700671 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800672 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800673 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800674 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700675 }
676 else {
677 /* This imitates the file system that would be created
678 * if we were using devfs instead.
679 * Minors are broken up into groups of 128, starting at "001"
680 */
681 int bus_id = uevent->minor / 128 + 1;
682 int device_id = uevent->minor % 128 + 1;
683 /* build directories */
684 make_dir("/dev/bus", 0755);
685 make_dir("/dev/bus/usb", 0755);
686 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
687 make_dir(devpath, 0755);
688 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
689 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700690 } else {
691 /* ignore other USB events */
692 return;
693 }
694 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
695 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500696 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200697 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
698 base = "/dev/dri/";
699 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700700 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
701 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500702 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700703 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
704 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500705 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700706 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
707 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500708 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700709 } else if(!strncmp(uevent->subsystem, "input", 5)) {
710 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500711 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700712 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
713 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500714 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700715 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
716 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500717 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700718 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
719 !strncmp(name, "log_", 4)) {
Elliott Hughesf682b472015-02-06 12:19:48 -0800720 INFO("kernel logger is deprecated\n");
Colin Crosseb5ba832011-03-30 17:37:17 -0700721 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500722 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700723 name += 4;
724 } else
725 base = "/dev/";
726 links = get_character_device_symlinks(uevent);
727
728 if (!devpath[0])
729 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
730
731 handle_device(uevent->action, devpath, uevent->path, 0,
732 uevent->major, uevent->minor, links);
733}
734
735static void handle_device_event(struct uevent *uevent)
736{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700737 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700738 fixup_sys_perms(uevent->path);
739
740 if (!strncmp(uevent->subsystem, "block", 5)) {
741 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700742 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
743 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700744 } else {
745 handle_generic_device_event(uevent);
746 }
747}
748
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700749static int load_firmware(int fw_fd, int loading_fd, int data_fd)
750{
751 struct stat st;
752 long len_to_copy;
753 int ret = 0;
754
755 if(fstat(fw_fd, &st) < 0)
756 return -1;
757 len_to_copy = st.st_size;
758
759 write(loading_fd, "1", 1); /* start transfer */
760
761 while (len_to_copy > 0) {
762 char buf[PAGE_SIZE];
763 ssize_t nr;
764
765 nr = read(fw_fd, buf, sizeof(buf));
766 if(!nr)
767 break;
768 if(nr < 0) {
769 ret = -1;
770 break;
771 }
772
773 len_to_copy -= nr;
774 while (nr > 0) {
775 ssize_t nw = 0;
776
777 nw = write(data_fd, buf + nw, nr);
778 if(nw <= 0) {
779 ret = -1;
780 goto out;
781 }
782 nr -= nw;
783 }
784 }
785
786out:
787 if(!ret)
788 write(loading_fd, "0", 1); /* successful end of transfer */
789 else
790 write(loading_fd, "-1", 2); /* abort transfer */
791
792 return ret;
793}
794
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700795static int is_booting(void)
796{
797 return access("/dev/.booting", F_OK) == 0;
798}
799
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700800static void process_firmware_event(struct uevent *uevent)
801{
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700802 char *root, *loading, *data;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700803 int l, loading_fd, data_fd, fw_fd;
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700804 size_t i;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700805 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700806
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700807 INFO("firmware: loading '%s' for '%s'\n",
808 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700809
810 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
811 if (l == -1)
812 return;
813
814 l = asprintf(&loading, "%sloading", root);
815 if (l == -1)
816 goto root_free_out;
817
818 l = asprintf(&data, "%sdata", root);
819 if (l == -1)
820 goto loading_free_out;
821
Nick Kralevich45a884f2015-02-02 14:37:22 -0800822 loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700823 if(loading_fd < 0)
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700824 goto data_free_out;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700825
Nick Kralevich45a884f2015-02-02 14:37:22 -0800826 data_fd = open(data, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700827 if(data_fd < 0)
828 goto loading_close_out;
829
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700830try_loading_again:
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700831 for (i = 0; i < ARRAY_SIZE(firmware_dirs); i++) {
832 char *file = NULL;
833 l = asprintf(&file, "%s/%s", firmware_dirs[i], uevent->firmware);
834 if (l == -1)
835 goto data_free_out;
836 fw_fd = open(file, O_RDONLY|O_CLOEXEC);
837 free(file);
838 if (fw_fd >= 0) {
839 if(!load_firmware(fw_fd, loading_fd, data_fd))
840 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
841 else
842 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
843 break;
Benoit Goby609d8822010-11-09 18:10:24 -0800844 }
Brian Swetland02863b92010-09-19 03:36:39 -0700845 }
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700846 if (fw_fd < 0) {
847 if (booting) {
848 /* If we're not fully booted, we may be missing
849 * filesystems needed for firmware, wait and retry.
850 */
851 usleep(100000);
852 booting = is_booting();
853 goto try_loading_again;
854 }
Elliott Hughescd67f002015-03-20 17:05:56 -0700855 INFO("firmware: could not open '%s': %s\n", uevent->firmware, strerror(errno));
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700856 write(loading_fd, "-1", 2);
857 goto data_close_out;
858 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700859
860 close(fw_fd);
861data_close_out:
862 close(data_fd);
863loading_close_out:
864 close(loading_fd);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700865data_free_out:
866 free(data);
867loading_free_out:
868 free(loading);
869root_free_out:
870 free(root);
871}
872
873static void handle_firmware_event(struct uevent *uevent)
874{
875 pid_t pid;
876
877 if(strcmp(uevent->subsystem, "firmware"))
878 return;
879
880 if(strcmp(uevent->action, "add"))
881 return;
882
883 /* we fork, to avoid making large memory allocations in init proper */
884 pid = fork();
885 if (!pid) {
886 process_firmware_event(uevent);
Kenny Root17baff42014-08-20 16:16:44 -0700887 _exit(EXIT_SUCCESS);
888 } else if (pid < 0) {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800889 ERROR("could not fork to process firmware event: %s\n", strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700890 }
891}
892
Ruchi Kandoic6037202014-06-23 11:22:09 -0700893#define UEVENT_MSG_LEN 2048
Colin Cross0dd7ca62010-04-13 19:25:51 -0700894void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700895{
Vernon Tang3f582e92011-04-25 13:08:17 +1000896 char msg[UEVENT_MSG_LEN+2];
897 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700898 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700899 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700900 continue;
901
902 msg[n] = '\0';
903 msg[n+1] = '\0';
904
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700905 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700906 parse_event(msg, &uevent);
907
Nick Kralevich4d870952015-06-12 22:03:50 -0700908 if (selinux_status_updated() > 0) {
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400909 struct selabel_handle *sehandle2;
910 sehandle2 = selinux_android_file_context_handle();
911 if (sehandle2) {
912 selabel_close(sehandle);
913 sehandle = sehandle2;
914 }
915 }
916
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700917 handle_device_event(&uevent);
918 handle_firmware_event(&uevent);
919 }
920}
921
922/* Coldboot walks parts of the /sys tree and pokes the uevent files
923** to cause the kernel to regenerate device add events that happened
924** before init's device manager was started
925**
926** We drain any pending events from the netlink socket every time
927** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800928** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700929*/
930
Colin Cross0dd7ca62010-04-13 19:25:51 -0700931static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700932{
933 struct dirent *de;
934 int dfd, fd;
935
936 dfd = dirfd(d);
937
938 fd = openat(dfd, "uevent", O_WRONLY);
939 if(fd >= 0) {
940 write(fd, "add\n", 4);
941 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700942 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700943 }
944
945 while((de = readdir(d))) {
946 DIR *d2;
947
948 if(de->d_type != DT_DIR || de->d_name[0] == '.')
949 continue;
950
951 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
952 if(fd < 0)
953 continue;
954
955 d2 = fdopendir(fd);
956 if(d2 == 0)
957 close(fd);
958 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700959 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700960 closedir(d2);
961 }
962 }
963}
964
Colin Cross0dd7ca62010-04-13 19:25:51 -0700965static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700966{
967 DIR *d = opendir(path);
968 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700969 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700970 closedir(d);
971 }
972}
973
Elliott Hughes74738362015-03-28 10:51:23 -0700974void device_init() {
Nick Kralevich4d870952015-06-12 22:03:50 -0700975 sehandle = selinux_android_file_context_handle();
976 selinux_status_open(true);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700977
Andrew Boied562ca72012-12-04 15:47:20 -0800978 /* is 256K enough? udev uses 16MB! */
979 device_fd = uevent_open_socket(256*1024, true);
Elliott Hughes56a06562015-03-28 11:23:32 -0700980 if (device_fd == -1) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700981 return;
Elliott Hughes56a06562015-03-28 11:23:32 -0700982 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700983 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700984
Elliott Hughes56a06562015-03-28 11:23:32 -0700985 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700986 NOTICE("Skipping coldboot, already done!\n");
Elliott Hughes56a06562015-03-28 11:23:32 -0700987 return;
Colin Crossf83d0b92010-04-21 12:04:20 -0700988 }
Elliott Hughes56a06562015-03-28 11:23:32 -0700989
990 Timer t;
991 coldboot("/sys/class");
992 coldboot("/sys/block");
993 coldboot("/sys/devices");
994 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
995 NOTICE("Coldboot took %.2fs.\n", t.duration());
Colin Cross0dd7ca62010-04-13 19:25:51 -0700996}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700997
Colin Cross0dd7ca62010-04-13 19:25:51 -0700998int get_device_fd()
999{
1000 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001001}