blob: d452dd35e5c1138f87a40df148b542666289c68a [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
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700133void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700134{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700135 char buf[512];
136 struct listnode *node;
137 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700138
Nick Kralevich3b4c0bd2014-07-03 16:04:34 -0700139 /* upaths omit the "/sys" that paths in this list
140 * contain, so we add 4 when comparing...
141 */
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700142 list_for_each(node, &sys_perms) {
143 dp = &(node_to_item(node, struct perm_node, plist))->dp;
144 if (dp->prefix) {
145 if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700146 continue;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700147 } else if (dp->wildcard) {
148 if (fnmatch(dp->name + 4, upath, FNM_PATHNAME) != 0)
149 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700150 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700151 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700152 continue;
153 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700154
155 if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
Nick Kralevich3b4c0bd2014-07-03 16:04:34 -0700156 break;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700157
Yabin Cuie2d63af2015-02-17 19:27:51 -0800158 snprintf(buf, sizeof(buf), "/sys%s/%s", upath, dp->attr);
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700159 INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
160 chown(buf, dp->uid, dp->gid);
161 chmod(buf, dp->perm);
Nick Kralevich3b4c0bd2014-07-03 16:04:34 -0700162 }
163
164 // Now fixup SELinux file labels
165 int len = snprintf(buf, sizeof(buf), "/sys%s", upath);
166 if ((len < 0) || ((size_t) len >= sizeof(buf))) {
167 // Overflow
168 return;
169 }
170 if (access(buf, F_OK) == 0) {
171 INFO("restorecon_recursive: %s\n", buf);
172 restorecon_recursive(buf);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700173 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700174}
175
Colin Cross43d537e2014-07-02 13:08:13 -0700176static bool perm_path_matches(const char *path, struct perms_ *dp)
177{
178 if (dp->prefix) {
179 if (strncmp(path, dp->name, strlen(dp->name)) == 0)
180 return true;
181 } else if (dp->wildcard) {
182 if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
183 return true;
184 } else {
185 if (strcmp(path, dp->name) == 0)
186 return true;
187 }
188
189 return false;
190}
191
192static mode_t get_device_perm(const char *path, const char **links,
193 unsigned *uid, unsigned *gid)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700194{
Colin Cross44b65d02010-04-20 14:32:50 -0700195 struct listnode *node;
196 struct perm_node *perm_node;
197 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700198
Colin Cross44b65d02010-04-20 14:32:50 -0700199 /* search the perms list in reverse so that ueventd.$hardware can
200 * override ueventd.rc
201 */
202 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700203 bool match = false;
204
Colin Cross44b65d02010-04-20 14:32:50 -0700205 perm_node = node_to_item(node, struct perm_node, plist);
206 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700207
Colin Cross43d537e2014-07-02 13:08:13 -0700208 if (perm_path_matches(path, dp)) {
209 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700210 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700211 if (links) {
212 int i;
213 for (i = 0; links[i]; i++) {
214 if (perm_path_matches(links[i], dp)) {
215 match = true;
216 break;
217 }
218 }
219 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700220 }
Colin Cross43d537e2014-07-02 13:08:13 -0700221
222 if (match) {
223 *uid = dp->uid;
224 *gid = dp->gid;
225 return dp->perm;
226 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700227 }
Colin Cross44b65d02010-04-20 14:32:50 -0700228 /* Default if nothing found. */
229 *uid = 0;
230 *gid = 0;
231 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700232}
233
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700234static void make_device(const char *path,
Elliott Hughesf682b472015-02-06 12:19:48 -0800235 const char */*upath*/,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400236 int block, int major, int minor,
237 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700238{
239 unsigned uid;
240 unsigned gid;
241 mode_t mode;
242 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500243 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700244
Colin Cross43d537e2014-07-02 13:08:13 -0700245 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700246
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300247 if (selabel_lookup_best_match(sehandle, &secontext, path, links, mode)) {
248 ERROR("Device '%s' not created; cannot find SELinux label (%s)\n",
249 path, strerror(errno));
250 return;
251 }
Nick Kralevich4d870952015-06-12 22:03:50 -0700252 setfscreatecon(secontext);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700253
Colin Cross17dcc5c2010-09-03 12:25:34 -0700254 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800255 /* Temporarily change egid to avoid race condition setting the gid of the
256 * device node. Unforunately changing the euid would prevent creation of
257 * some device nodes, so the uid has to be set with chown() and is still
258 * racy. Fixing the gid race at least fixed the issue with system_server
259 * opening dynamic input devices under the AID_INPUT gid. */
260 setegid(gid);
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300261 /* If the node already exists update its SELinux label to handle cases when
262 * it was created with the wrong context during coldboot procedure. */
263 if (mknod(path, mode, dev) && (errno == EEXIST)) {
264 if (lsetfilecon(path, secontext)) {
265 ERROR("Cannot set '%s' SELinux label on '%s' device (%s)\n",
266 secontext, path, strerror(errno));
267 }
268 }
Nick Pelly6405c692010-01-21 18:13:39 -0800269 chown(path, uid, -1);
270 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700271
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300272 freecon(secontext);
273 setfscreatecon(NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700274}
275
Dima Zavinf395c922013-03-06 16:23:57 -0800276static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700277{
Dima Zavinf395c922013-03-06 16:23:57 -0800278 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700279 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800280 const char *name = path;
281
282 if (!strncmp(path, "/devices/", 9)) {
283 name += 9;
284 if (!strncmp(name, "platform/", 9))
285 name += 9;
286 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700287
Dima Zavinf395c922013-03-06 16:23:57 -0800288 INFO("adding platform device %s (%s)\n", name, path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700289
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800290 bus = (platform_node*) calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800291 bus->path = strdup(path);
292 bus->path_len = path_len;
293 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700294 list_add_tail(&platform_names, &bus->list);
295}
296
297/*
Dima Zavinf395c922013-03-06 16:23:57 -0800298 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700299 * platform device prefix. If it doesn't start with a platform device, return
300 * 0.
301 */
Dima Zavinf395c922013-03-06 16:23:57 -0800302static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700303{
Dima Zavinf395c922013-03-06 16:23:57 -0800304 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700305 struct listnode *node;
306 struct platform_node *bus;
307
308 list_for_each_reverse(node, &platform_names) {
309 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800310 if ((bus->path_len < path_len) &&
311 (path[bus->path_len] == '/') &&
312 !strncmp(path, bus->path, bus->path_len))
313 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700314 }
315
316 return NULL;
317}
318
Dima Zavinf395c922013-03-06 16:23:57 -0800319static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700320{
321 struct listnode *node;
322 struct platform_node *bus;
323
324 list_for_each_reverse(node, &platform_names) {
325 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800326 if (!strcmp(path, bus->path)) {
327 INFO("removing platform device %s\n", bus->name);
328 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700329 list_remove(node);
330 free(bus);
331 return;
332 }
333 }
334}
335
Andrew Boiea885d042013-09-13 17:41:20 -0700336/* Given a path that may start with a PCI device, populate the supplied buffer
337 * with the PCI domain/bus number and the peripheral ID and return 0.
338 * If it doesn't start with a PCI device, or there is some error, return -1 */
339static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
340{
341 const char *start, *end;
342
343 if (strncmp(path, "/devices/pci", 12))
344 return -1;
345
346 /* Beginning of the prefix is the initial "pci" after "/devices/" */
347 start = path + 9;
348
349 /* End of the prefix is two path '/' later, capturing the domain/bus number
350 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
351 end = strchr(start, '/');
352 if (!end)
353 return -1;
354 end = strchr(end + 1, '/');
355 if (!end)
356 return -1;
357
358 /* Make sure we have enough room for the string plus null terminator */
359 if (end - start + 1 > buf_sz)
360 return -1;
361
362 strncpy(buf, start, end - start);
363 buf[end - start] = '\0';
364 return 0;
365}
366
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700367static void parse_event(const char *msg, struct uevent *uevent)
368{
369 uevent->action = "";
370 uevent->path = "";
371 uevent->subsystem = "";
372 uevent->firmware = "";
373 uevent->major = -1;
374 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700375 uevent->partition_name = NULL;
376 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700377 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700378
379 /* currently ignoring SEQNUM */
380 while(*msg) {
381 if(!strncmp(msg, "ACTION=", 7)) {
382 msg += 7;
383 uevent->action = msg;
384 } else if(!strncmp(msg, "DEVPATH=", 8)) {
385 msg += 8;
386 uevent->path = msg;
387 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
388 msg += 10;
389 uevent->subsystem = msg;
390 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
391 msg += 9;
392 uevent->firmware = msg;
393 } else if(!strncmp(msg, "MAJOR=", 6)) {
394 msg += 6;
395 uevent->major = atoi(msg);
396 } else if(!strncmp(msg, "MINOR=", 6)) {
397 msg += 6;
398 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700399 } else if(!strncmp(msg, "PARTN=", 6)) {
400 msg += 6;
401 uevent->partition_num = atoi(msg);
402 } else if(!strncmp(msg, "PARTNAME=", 9)) {
403 msg += 9;
404 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700405 } else if(!strncmp(msg, "DEVNAME=", 8)) {
406 msg += 8;
407 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700408 }
409
Wei Zhongf97b8872012-03-23 14:15:34 -0700410 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700411 while(*msg++)
412 ;
413 }
414
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800415 if (LOG_UEVENTS) {
416 INFO("event { '%s', '%s', '%s', '%s', %d, %d }\n",
417 uevent->action, uevent->path, uevent->subsystem,
418 uevent->firmware, uevent->major, uevent->minor);
419 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700420}
421
Benoit Gobyd2278632010-08-03 14:36:02 -0700422static char **get_character_device_symlinks(struct uevent *uevent)
423{
424 const char *parent;
Dan Austin60b976d2016-03-28 14:22:12 -0700425 const char *slash;
Benoit Gobyd2278632010-08-03 14:36:02 -0700426 char **links;
427 int link_num = 0;
428 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800429 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700430
Dima Zavinf395c922013-03-06 16:23:57 -0800431 pdev = find_platform_device(uevent->path);
432 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700433 return NULL;
434
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800435 links = (char**) malloc(sizeof(char *) * 2);
Benoit Gobyd2278632010-08-03 14:36:02 -0700436 if (!links)
437 return NULL;
438 memset(links, 0, sizeof(char *) * 2);
439
440 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800441 parent = strchr(uevent->path + pdev->path_len, '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100442 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700443 goto err;
444
445 if (!strncmp(parent, "/usb", 4)) {
446 /* skip root hub name and device. use device interface */
447 while (*++parent && *parent != '/');
448 if (*parent)
449 while (*++parent && *parent != '/');
450 if (!*parent)
451 goto err;
452 slash = strchr(++parent, '/');
453 if (!slash)
454 goto err;
455 width = slash - parent;
456 if (width <= 0)
457 goto err;
458
459 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
460 link_num++;
461 else
462 links[link_num] = NULL;
463 mkdir("/dev/usb", 0755);
464 }
465 else {
466 goto err;
467 }
468
469 return links;
470err:
471 free(links);
472 return NULL;
473}
474
Andrew Boiea885d042013-09-13 17:41:20 -0700475static char **get_block_device_symlinks(struct uevent *uevent)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700476{
Colin Crossfadb85e2011-03-30 18:32:12 -0700477 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800478 struct platform_node *pdev;
Dan Austin60b976d2016-03-28 14:22:12 -0700479 const char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700480 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700481 char buf[256];
482 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700483 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700484 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700485
Dima Zavinf395c922013-03-06 16:23:57 -0800486 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700487 if (pdev) {
488 device = pdev->name;
489 type = "platform";
490 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
491 device = buf;
492 type = "pci";
493 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800494 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700495 }
Dima Zavinf395c922013-03-06 16:23:57 -0800496
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800497 char **links = (char**) malloc(sizeof(char *) * 4);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700498 if (!links)
499 return NULL;
500 memset(links, 0, sizeof(char *) * 4);
501
Andrew Boiea885d042013-09-13 17:41:20 -0700502 INFO("found %s device %s\n", type, device);
Colin Crossfadb85e2011-03-30 18:32:12 -0700503
Andrew Boiea885d042013-09-13 17:41:20 -0700504 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700505
506 if (uevent->partition_name) {
507 p = strdup(uevent->partition_name);
508 sanitize(p);
Johan Redestig93ca79b2012-04-18 16:41:19 +0200509 if (strcmp(uevent->partition_name, p))
510 NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700511 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
512 link_num++;
513 else
514 links[link_num] = NULL;
515 free(p);
516 }
517
518 if (uevent->partition_num >= 0) {
519 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
520 link_num++;
521 else
522 links[link_num] = NULL;
523 }
524
Dima Zavinf395c922013-03-06 16:23:57 -0800525 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700526 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
527 link_num++;
528 else
529 links[link_num] = NULL;
530
531 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700532}
533
Colin Crosseb5ba832011-03-30 17:37:17 -0700534static void handle_device(const char *action, const char *devpath,
535 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700536{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700537 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700538
Colin Crosseb5ba832011-03-30 17:37:17 -0700539 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400540 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700541 if (links) {
542 for (i = 0; links[i]; i++)
Chris Fries79f33842013-09-05 13:19:21 -0500543 make_link_init(devpath, links[i]);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700544 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700545 }
546
Colin Crosseb5ba832011-03-30 17:37:17 -0700547 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700548 if (links) {
549 for (i = 0; links[i]; i++)
550 remove_link(devpath, links[i]);
551 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700552 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700553 }
554
555 if (links) {
556 for (i = 0; links[i]; i++)
557 free(links[i]);
558 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700559 }
560}
561
Colin Crossfadb85e2011-03-30 18:32:12 -0700562static void handle_platform_device_event(struct uevent *uevent)
563{
Dima Zavinf395c922013-03-06 16:23:57 -0800564 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700565
566 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800567 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700568 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800569 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700570}
571
Colin Crosseb5ba832011-03-30 17:37:17 -0700572static const char *parse_device_name(struct uevent *uevent, unsigned int len)
573{
574 const char *name;
575
576 /* if it's not a /dev device, nothing else to do */
577 if((uevent->major < 0) || (uevent->minor < 0))
578 return NULL;
579
580 /* do we have a name? */
581 name = strrchr(uevent->path, '/');
582 if(!name)
583 return NULL;
584 name++;
585
586 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800587 if(strlen(name) > len) {
588 ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n",
589 name, len);
Colin Crosseb5ba832011-03-30 17:37:17 -0700590 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800591 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700592
593 return name;
594}
595
596static void handle_block_device_event(struct uevent *uevent)
597{
598 const char *base = "/dev/block/";
599 const char *name;
600 char devpath[96];
601 char **links = NULL;
602
603 name = parse_device_name(uevent, 64);
604 if (!name)
605 return;
606
607 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500608 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700609
Dima Zavinf395c922013-03-06 16:23:57 -0800610 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700611 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700612
613 handle_device(uevent->action, devpath, uevent->path, 1,
614 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700615}
616
Greg Hackmann3312aa82013-11-18 15:24:40 -0800617#define DEVPATH_LEN 96
618
619static bool assemble_devpath(char *devpath, const char *dirname,
620 const char *devname)
621{
622 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
623 if (s < 0) {
624 ERROR("failed to assemble device path (%s); ignoring event\n",
625 strerror(errno));
626 return false;
627 } else if (s >= DEVPATH_LEN) {
628 ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n",
629 dirname, devname, DEVPATH_LEN);
630 return false;
631 }
632 return true;
633}
634
635static void mkdir_recursive_for_devpath(const char *devpath)
636{
637 char dir[DEVPATH_LEN];
638 char *slash;
639
640 strcpy(dir, devpath);
641 slash = strrchr(dir, '/');
642 *slash = '\0';
643 mkdir_recursive(dir, 0755);
644}
645
Colin Crosseb5ba832011-03-30 17:37:17 -0700646static void handle_generic_device_event(struct uevent *uevent)
647{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800648 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700649 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800650 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700651 char **links = NULL;
652
653 name = parse_device_name(uevent, 64);
654 if (!name)
655 return;
656
Greg Hackmann3312aa82013-11-18 15:24:40 -0800657 struct ueventd_subsystem *subsystem =
658 ueventd_subsystem_find_by_name(uevent->subsystem);
659
660 if (subsystem) {
661 const char *devname;
662
663 switch (subsystem->devname_src) {
664 case DEVNAME_UEVENT_DEVNAME:
665 devname = uevent->device_name;
666 break;
667
668 case DEVNAME_UEVENT_DEVPATH:
669 devname = name;
670 break;
671
672 default:
673 ERROR("%s subsystem's devpath option is not set; ignoring event\n",
674 uevent->subsystem);
675 return;
676 }
677
678 if (!assemble_devpath(devpath, subsystem->dirname, devname))
679 return;
680 mkdir_recursive_for_devpath(devpath);
681 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700682 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700683 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800684 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800685 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800686 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700687 }
688 else {
689 /* This imitates the file system that would be created
690 * if we were using devfs instead.
691 * Minors are broken up into groups of 128, starting at "001"
692 */
693 int bus_id = uevent->minor / 128 + 1;
694 int device_id = uevent->minor % 128 + 1;
695 /* build directories */
696 make_dir("/dev/bus", 0755);
697 make_dir("/dev/bus/usb", 0755);
698 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
699 make_dir(devpath, 0755);
700 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
701 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700702 } else {
703 /* ignore other USB events */
704 return;
705 }
706 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
707 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500708 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200709 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
710 base = "/dev/dri/";
711 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700712 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
713 base = "/dev/oncrpc/";
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, "adsp", 4)) {
716 base = "/dev/adsp/";
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, "msm_camera", 10)) {
719 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500720 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700721 } else if(!strncmp(uevent->subsystem, "input", 5)) {
722 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500723 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700724 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
725 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500726 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700727 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
728 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500729 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700730 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
731 !strncmp(name, "log_", 4)) {
Elliott Hughesf682b472015-02-06 12:19:48 -0800732 INFO("kernel logger is deprecated\n");
Colin Crosseb5ba832011-03-30 17:37:17 -0700733 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500734 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700735 name += 4;
736 } else
737 base = "/dev/";
738 links = get_character_device_symlinks(uevent);
739
740 if (!devpath[0])
741 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
742
743 handle_device(uevent->action, devpath, uevent->path, 0,
744 uevent->major, uevent->minor, links);
745}
746
747static void handle_device_event(struct uevent *uevent)
748{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700749 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700750 fixup_sys_perms(uevent->path);
751
752 if (!strncmp(uevent->subsystem, "block", 5)) {
753 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700754 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
755 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700756 } else {
757 handle_generic_device_event(uevent);
758 }
759}
760
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700761static int load_firmware(int fw_fd, int loading_fd, int data_fd)
762{
763 struct stat st;
764 long len_to_copy;
765 int ret = 0;
766
767 if(fstat(fw_fd, &st) < 0)
768 return -1;
769 len_to_copy = st.st_size;
770
771 write(loading_fd, "1", 1); /* start transfer */
772
773 while (len_to_copy > 0) {
774 char buf[PAGE_SIZE];
775 ssize_t nr;
776
777 nr = read(fw_fd, buf, sizeof(buf));
778 if(!nr)
779 break;
780 if(nr < 0) {
781 ret = -1;
782 break;
783 }
Biao Ludc848562016-01-28 16:10:54 +0800784 if (!android::base::WriteFully(data_fd, buf, nr)) {
785 ret = -1;
786 break;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700787 }
Biao Ludc848562016-01-28 16:10:54 +0800788 len_to_copy -= nr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700789 }
790
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700791 if(!ret)
792 write(loading_fd, "0", 1); /* successful end of transfer */
793 else
794 write(loading_fd, "-1", 2); /* abort transfer */
795
796 return ret;
797}
798
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700799static int is_booting(void)
800{
801 return access("/dev/.booting", F_OK) == 0;
802}
803
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700804static void process_firmware_event(struct uevent *uevent)
805{
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700806 char *root, *loading, *data;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700807 int l, loading_fd, data_fd, fw_fd;
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700808 size_t i;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700809 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700810
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700811 INFO("firmware: loading '%s' for '%s'\n",
812 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700813
814 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
815 if (l == -1)
816 return;
817
818 l = asprintf(&loading, "%sloading", root);
819 if (l == -1)
820 goto root_free_out;
821
822 l = asprintf(&data, "%sdata", root);
823 if (l == -1)
824 goto loading_free_out;
825
Nick Kralevich45a884f2015-02-02 14:37:22 -0800826 loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700827 if(loading_fd < 0)
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700828 goto data_free_out;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700829
Nick Kralevich45a884f2015-02-02 14:37:22 -0800830 data_fd = open(data, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700831 if(data_fd < 0)
832 goto loading_close_out;
833
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700834try_loading_again:
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700835 for (i = 0; i < ARRAY_SIZE(firmware_dirs); i++) {
836 char *file = NULL;
837 l = asprintf(&file, "%s/%s", firmware_dirs[i], uevent->firmware);
838 if (l == -1)
839 goto data_free_out;
840 fw_fd = open(file, O_RDONLY|O_CLOEXEC);
841 free(file);
842 if (fw_fd >= 0) {
843 if(!load_firmware(fw_fd, loading_fd, data_fd))
844 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
845 else
846 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
847 break;
Benoit Goby609d8822010-11-09 18:10:24 -0800848 }
Brian Swetland02863b92010-09-19 03:36:39 -0700849 }
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700850 if (fw_fd < 0) {
851 if (booting) {
852 /* If we're not fully booted, we may be missing
853 * filesystems needed for firmware, wait and retry.
854 */
855 usleep(100000);
856 booting = is_booting();
857 goto try_loading_again;
858 }
Elliott Hughescd67f002015-03-20 17:05:56 -0700859 INFO("firmware: could not open '%s': %s\n", uevent->firmware, strerror(errno));
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700860 write(loading_fd, "-1", 2);
861 goto data_close_out;
862 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700863
864 close(fw_fd);
865data_close_out:
866 close(data_fd);
867loading_close_out:
868 close(loading_fd);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700869data_free_out:
870 free(data);
871loading_free_out:
872 free(loading);
873root_free_out:
874 free(root);
875}
876
877static void handle_firmware_event(struct uevent *uevent)
878{
879 pid_t pid;
880
881 if(strcmp(uevent->subsystem, "firmware"))
882 return;
883
884 if(strcmp(uevent->action, "add"))
885 return;
886
887 /* we fork, to avoid making large memory allocations in init proper */
888 pid = fork();
889 if (!pid) {
890 process_firmware_event(uevent);
Kenny Root17baff42014-08-20 16:16:44 -0700891 _exit(EXIT_SUCCESS);
892 } else if (pid < 0) {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800893 ERROR("could not fork to process firmware event: %s\n", strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700894 }
895}
896
Ruchi Kandoic6037202014-06-23 11:22:09 -0700897#define UEVENT_MSG_LEN 2048
Colin Cross0dd7ca62010-04-13 19:25:51 -0700898void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700899{
Vernon Tang3f582e92011-04-25 13:08:17 +1000900 char msg[UEVENT_MSG_LEN+2];
901 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700902 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700903 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700904 continue;
905
906 msg[n] = '\0';
907 msg[n+1] = '\0';
908
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700909 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700910 parse_event(msg, &uevent);
911
Nick Kralevich4d870952015-06-12 22:03:50 -0700912 if (selinux_status_updated() > 0) {
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400913 struct selabel_handle *sehandle2;
914 sehandle2 = selinux_android_file_context_handle();
915 if (sehandle2) {
916 selabel_close(sehandle);
917 sehandle = sehandle2;
918 }
919 }
920
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700921 handle_device_event(&uevent);
922 handle_firmware_event(&uevent);
923 }
924}
925
926/* Coldboot walks parts of the /sys tree and pokes the uevent files
927** to cause the kernel to regenerate device add events that happened
928** before init's device manager was started
929**
930** We drain any pending events from the netlink socket every time
931** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800932** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700933*/
934
Colin Cross0dd7ca62010-04-13 19:25:51 -0700935static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700936{
937 struct dirent *de;
938 int dfd, fd;
939
940 dfd = dirfd(d);
941
942 fd = openat(dfd, "uevent", O_WRONLY);
943 if(fd >= 0) {
944 write(fd, "add\n", 4);
945 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700946 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700947 }
948
949 while((de = readdir(d))) {
950 DIR *d2;
951
952 if(de->d_type != DT_DIR || de->d_name[0] == '.')
953 continue;
954
955 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
956 if(fd < 0)
957 continue;
958
959 d2 = fdopendir(fd);
960 if(d2 == 0)
961 close(fd);
962 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700963 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700964 closedir(d2);
965 }
966 }
967}
968
Colin Cross0dd7ca62010-04-13 19:25:51 -0700969static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700970{
James Hawkins588a2ca2016-02-18 14:52:46 -0800971 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700972 if(d) {
James Hawkins588a2ca2016-02-18 14:52:46 -0800973 do_coldboot(d.get());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700974 }
975}
976
Elliott Hughes74738362015-03-28 10:51:23 -0700977void device_init() {
Nick Kralevich4d870952015-06-12 22:03:50 -0700978 sehandle = selinux_android_file_context_handle();
979 selinux_status_open(true);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700980
Andrew Boied562ca72012-12-04 15:47:20 -0800981 /* is 256K enough? udev uses 16MB! */
982 device_fd = uevent_open_socket(256*1024, true);
Elliott Hughes56a06562015-03-28 11:23:32 -0700983 if (device_fd == -1) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700984 return;
Elliott Hughes56a06562015-03-28 11:23:32 -0700985 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700986 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700987
Elliott Hughes56a06562015-03-28 11:23:32 -0700988 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700989 NOTICE("Skipping coldboot, already done!\n");
Elliott Hughes56a06562015-03-28 11:23:32 -0700990 return;
Colin Crossf83d0b92010-04-21 12:04:20 -0700991 }
Elliott Hughes56a06562015-03-28 11:23:32 -0700992
993 Timer t;
994 coldboot("/sys/class");
995 coldboot("/sys/block");
996 coldboot("/sys/devices");
997 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
998 NOTICE("Coldboot took %.2fs.\n", t.duration());
Colin Cross0dd7ca62010-04-13 19:25:51 -0700999}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001000
Colin Cross0dd7ca62010-04-13 19:25:51 -07001001int get_device_fd()
1002{
1003 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001004}