blob: 32fec52c5de73aff5f040d9bc4c57c98f04754bb [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>
Rob Herring6de783a2016-05-06 10:06:59 -050046#include <android-base/stringprintf.h>
Dima Zavinda04c522011-09-01 17:09:44 -070047#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100048#include <cutils/uevent.h>
49
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070050#include "devices.h"
Greg Hackmann3312aa82013-11-18 15:24:40 -080051#include "ueventd_parser.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070052#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070053#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070055#define SYSFS_PREFIX "/sys"
Daniel Rosenbergd1d96022015-03-20 15:10:29 -070056static const char *firmware_dirs[] = { "/etc/firmware",
57 "/vendor/firmware",
58 "/firmware/image" };
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070059
Stephen Smalleye096e362012-06-11 13:37:39 -040060extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050061
Colin Cross0dd7ca62010-04-13 19:25:51 -070062static int device_fd = -1;
63
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064struct uevent {
65 const char *action;
66 const char *path;
67 const char *subsystem;
68 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070069 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070070 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070071 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072 int major;
73 int minor;
74};
75
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076struct perms_ {
77 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070078 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070079 mode_t perm;
80 unsigned int uid;
81 unsigned int gid;
82 unsigned short prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070083 unsigned short wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070084};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070085
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070086struct perm_node {
87 struct perms_ dp;
88 struct listnode plist;
89};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070090
Colin Crossfadb85e2011-03-30 18:32:12 -070091struct platform_node {
92 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080093 char *path;
94 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070095 struct listnode list;
96};
97
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070098static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070099static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -0700100static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700101
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700102int add_dev_perms(const char *name, const char *attr,
103 mode_t perm, unsigned int uid, unsigned int gid,
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700104 unsigned short prefix,
105 unsigned short wildcard) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800106 struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700107 if (!node)
108 return -ENOMEM;
109
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700110 node->dp.name = strdup(name);
111 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700112 return -ENOMEM;
113
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700114 if (attr) {
115 node->dp.attr = strdup(attr);
116 if (!node->dp.attr)
117 return -ENOMEM;
118 }
119
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700120 node->dp.perm = perm;
121 node->dp.uid = uid;
122 node->dp.gid = gid;
123 node->dp.prefix = prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700124 node->dp.wildcard = wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700125
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700126 if (attr)
127 list_add_tail(&sys_perms, &node->plist);
128 else
129 list_add_tail(&dev_perms, &node->plist);
130
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700131 return 0;
132}
133
Colin Cross43d537e2014-07-02 13:08:13 -0700134static bool perm_path_matches(const char *path, struct perms_ *dp)
135{
136 if (dp->prefix) {
137 if (strncmp(path, dp->name, strlen(dp->name)) == 0)
138 return true;
139 } else if (dp->wildcard) {
140 if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
141 return true;
142 } else {
143 if (strcmp(path, dp->name) == 0)
144 return true;
145 }
146
147 return false;
148}
149
Rob Herring6de783a2016-05-06 10:06:59 -0500150static bool match_subsystem(perms_* dp, const char* pattern,
151 const char* path, const char* subsystem) {
152 if (!pattern || !subsystem || strstr(dp->name, subsystem) == NULL) {
153 return false;
154 }
Rob Herringe5636a32016-05-06 12:28:48 -0500155
Rob Herring6de783a2016-05-06 10:06:59 -0500156 std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path));
157 return perm_path_matches(subsys_path.c_str(), dp);
158}
Rob Herringe5636a32016-05-06 12:28:48 -0500159
Rob Herring6de783a2016-05-06 10:06:59 -0500160static void fixup_sys_perms(const char* upath, const char* subsystem) {
161 // upaths omit the "/sys" that paths in this list
162 // contain, so we prepend it...
163 std::string path = std::string(SYSFS_PREFIX) + upath;
164
165 listnode* node;
Rob Herringe5636a32016-05-06 12:28:48 -0500166 list_for_each(node, &sys_perms) {
Rob Herring6de783a2016-05-06 10:06:59 -0500167 perms_* dp = &(node_to_item(node, perm_node, plist))->dp;
168 if (match_subsystem(dp, SYSFS_PREFIX "/class/%s/%s", path.c_str(), subsystem)) {
169 ; // matched
170 } else if (match_subsystem(dp, SYSFS_PREFIX "/bus/%s/devices/%s", path.c_str(), subsystem)) {
171 ; // matched
172 } else if (!perm_path_matches(path.c_str(), dp)) {
173 continue;
Rob Herringe5636a32016-05-06 12:28:48 -0500174 }
175
176 std::string attr_file = path + "/" + dp->attr;
177 INFO("fixup %s %d %d 0%o\n", attr_file.c_str(), dp->uid, dp->gid, dp->perm);
178 chown(attr_file.c_str(), dp->uid, dp->gid);
179 chmod(attr_file.c_str(), dp->perm);
180 }
181
182 if (access(path.c_str(), F_OK) == 0) {
183 INFO("restorecon_recursive: %s\n", path.c_str());
184 restorecon_recursive(path.c_str());
185 }
186}
187
Colin Cross43d537e2014-07-02 13:08:13 -0700188static mode_t get_device_perm(const char *path, const char **links,
189 unsigned *uid, unsigned *gid)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700190{
Colin Cross44b65d02010-04-20 14:32:50 -0700191 struct listnode *node;
192 struct perm_node *perm_node;
193 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700194
Colin Cross44b65d02010-04-20 14:32:50 -0700195 /* search the perms list in reverse so that ueventd.$hardware can
196 * override ueventd.rc
197 */
198 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700199 bool match = false;
200
Colin Cross44b65d02010-04-20 14:32:50 -0700201 perm_node = node_to_item(node, struct perm_node, plist);
202 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700203
Colin Cross43d537e2014-07-02 13:08:13 -0700204 if (perm_path_matches(path, dp)) {
205 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700206 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700207 if (links) {
208 int i;
209 for (i = 0; links[i]; i++) {
210 if (perm_path_matches(links[i], dp)) {
211 match = true;
212 break;
213 }
214 }
215 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700216 }
Colin Cross43d537e2014-07-02 13:08:13 -0700217
218 if (match) {
219 *uid = dp->uid;
220 *gid = dp->gid;
221 return dp->perm;
222 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700223 }
Colin Cross44b65d02010-04-20 14:32:50 -0700224 /* Default if nothing found. */
225 *uid = 0;
226 *gid = 0;
227 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700228}
229
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700230static void make_device(const char *path,
Elliott Hughesf682b472015-02-06 12:19:48 -0800231 const char */*upath*/,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400232 int block, int major, int minor,
233 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700234{
235 unsigned uid;
236 unsigned gid;
237 mode_t mode;
238 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500239 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700240
Colin Cross43d537e2014-07-02 13:08:13 -0700241 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700242
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300243 if (selabel_lookup_best_match(sehandle, &secontext, path, links, mode)) {
244 ERROR("Device '%s' not created; cannot find SELinux label (%s)\n",
245 path, strerror(errno));
246 return;
247 }
Nick Kralevich4d870952015-06-12 22:03:50 -0700248 setfscreatecon(secontext);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700249
Colin Cross17dcc5c2010-09-03 12:25:34 -0700250 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800251 /* Temporarily change egid to avoid race condition setting the gid of the
252 * device node. Unforunately changing the euid would prevent creation of
253 * some device nodes, so the uid has to be set with chown() and is still
254 * racy. Fixing the gid race at least fixed the issue with system_server
255 * opening dynamic input devices under the AID_INPUT gid. */
256 setegid(gid);
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300257 /* If the node already exists update its SELinux label to handle cases when
258 * it was created with the wrong context during coldboot procedure. */
259 if (mknod(path, mode, dev) && (errno == EEXIST)) {
William Roberts397de142016-06-02 09:53:44 -0700260
261 char* fcon = nullptr;
262 int rc = lgetfilecon(path, &fcon);
263 if (rc < 0) {
264 ERROR("Cannot get SELinux label on '%s' device (%s)\n",
265 path, strerror(errno));
266 goto out;
267 }
268
269 bool different = strcmp(fcon, secontext) != 0;
270 freecon(fcon);
271
272 if (different && lsetfilecon(path, secontext)) {
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300273 ERROR("Cannot set '%s' SELinux label on '%s' device (%s)\n",
274 secontext, path, strerror(errno));
275 }
276 }
William Roberts397de142016-06-02 09:53:44 -0700277
278out:
Nick Pelly6405c692010-01-21 18:13:39 -0800279 chown(path, uid, -1);
280 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700281
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300282 freecon(secontext);
283 setfscreatecon(NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700284}
285
Dima Zavinf395c922013-03-06 16:23:57 -0800286static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700287{
Dima Zavinf395c922013-03-06 16:23:57 -0800288 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700289 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800290 const char *name = path;
291
292 if (!strncmp(path, "/devices/", 9)) {
293 name += 9;
294 if (!strncmp(name, "platform/", 9))
295 name += 9;
296 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700297
Dima Zavinf395c922013-03-06 16:23:57 -0800298 INFO("adding platform device %s (%s)\n", name, path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700299
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800300 bus = (platform_node*) calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800301 bus->path = strdup(path);
302 bus->path_len = path_len;
303 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700304 list_add_tail(&platform_names, &bus->list);
305}
306
307/*
Dima Zavinf395c922013-03-06 16:23:57 -0800308 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700309 * platform device prefix. If it doesn't start with a platform device, return
310 * 0.
311 */
Dima Zavinf395c922013-03-06 16:23:57 -0800312static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700313{
Dima Zavinf395c922013-03-06 16:23:57 -0800314 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700315 struct listnode *node;
316 struct platform_node *bus;
317
318 list_for_each_reverse(node, &platform_names) {
319 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800320 if ((bus->path_len < path_len) &&
321 (path[bus->path_len] == '/') &&
322 !strncmp(path, bus->path, bus->path_len))
323 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700324 }
325
326 return NULL;
327}
328
Dima Zavinf395c922013-03-06 16:23:57 -0800329static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700330{
331 struct listnode *node;
332 struct platform_node *bus;
333
334 list_for_each_reverse(node, &platform_names) {
335 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800336 if (!strcmp(path, bus->path)) {
337 INFO("removing platform device %s\n", bus->name);
338 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700339 list_remove(node);
340 free(bus);
341 return;
342 }
343 }
344}
345
Andrew Boiea885d042013-09-13 17:41:20 -0700346/* Given a path that may start with a PCI device, populate the supplied buffer
347 * with the PCI domain/bus number and the peripheral ID and return 0.
348 * If it doesn't start with a PCI device, or there is some error, return -1 */
349static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
350{
351 const char *start, *end;
352
353 if (strncmp(path, "/devices/pci", 12))
354 return -1;
355
356 /* Beginning of the prefix is the initial "pci" after "/devices/" */
357 start = path + 9;
358
359 /* End of the prefix is two path '/' later, capturing the domain/bus number
360 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
361 end = strchr(start, '/');
362 if (!end)
363 return -1;
364 end = strchr(end + 1, '/');
365 if (!end)
366 return -1;
367
368 /* Make sure we have enough room for the string plus null terminator */
369 if (end - start + 1 > buf_sz)
370 return -1;
371
372 strncpy(buf, start, end - start);
373 buf[end - start] = '\0';
374 return 0;
375}
376
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700377static void parse_event(const char *msg, struct uevent *uevent)
378{
379 uevent->action = "";
380 uevent->path = "";
381 uevent->subsystem = "";
382 uevent->firmware = "";
383 uevent->major = -1;
384 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700385 uevent->partition_name = NULL;
386 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700387 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700388
389 /* currently ignoring SEQNUM */
390 while(*msg) {
391 if(!strncmp(msg, "ACTION=", 7)) {
392 msg += 7;
393 uevent->action = msg;
394 } else if(!strncmp(msg, "DEVPATH=", 8)) {
395 msg += 8;
396 uevent->path = msg;
397 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
398 msg += 10;
399 uevent->subsystem = msg;
400 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
401 msg += 9;
402 uevent->firmware = msg;
403 } else if(!strncmp(msg, "MAJOR=", 6)) {
404 msg += 6;
405 uevent->major = atoi(msg);
406 } else if(!strncmp(msg, "MINOR=", 6)) {
407 msg += 6;
408 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700409 } else if(!strncmp(msg, "PARTN=", 6)) {
410 msg += 6;
411 uevent->partition_num = atoi(msg);
412 } else if(!strncmp(msg, "PARTNAME=", 9)) {
413 msg += 9;
414 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700415 } else if(!strncmp(msg, "DEVNAME=", 8)) {
416 msg += 8;
417 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700418 }
419
Wei Zhongf97b8872012-03-23 14:15:34 -0700420 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700421 while(*msg++)
422 ;
423 }
424
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800425 if (LOG_UEVENTS) {
426 INFO("event { '%s', '%s', '%s', '%s', %d, %d }\n",
427 uevent->action, uevent->path, uevent->subsystem,
428 uevent->firmware, uevent->major, uevent->minor);
429 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700430}
431
Benoit Gobyd2278632010-08-03 14:36:02 -0700432static char **get_character_device_symlinks(struct uevent *uevent)
433{
434 const char *parent;
Dan Austin60b976d2016-03-28 14:22:12 -0700435 const char *slash;
Benoit Gobyd2278632010-08-03 14:36:02 -0700436 char **links;
437 int link_num = 0;
438 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800439 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700440
Dima Zavinf395c922013-03-06 16:23:57 -0800441 pdev = find_platform_device(uevent->path);
442 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700443 return NULL;
444
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800445 links = (char**) malloc(sizeof(char *) * 2);
Benoit Gobyd2278632010-08-03 14:36:02 -0700446 if (!links)
447 return NULL;
448 memset(links, 0, sizeof(char *) * 2);
449
450 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800451 parent = strchr(uevent->path + pdev->path_len, '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100452 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700453 goto err;
454
455 if (!strncmp(parent, "/usb", 4)) {
456 /* skip root hub name and device. use device interface */
457 while (*++parent && *parent != '/');
458 if (*parent)
459 while (*++parent && *parent != '/');
460 if (!*parent)
461 goto err;
462 slash = strchr(++parent, '/');
463 if (!slash)
464 goto err;
465 width = slash - parent;
466 if (width <= 0)
467 goto err;
468
469 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
470 link_num++;
471 else
472 links[link_num] = NULL;
473 mkdir("/dev/usb", 0755);
474 }
475 else {
476 goto err;
477 }
478
479 return links;
480err:
481 free(links);
482 return NULL;
483}
484
Andrew Boiea885d042013-09-13 17:41:20 -0700485static char **get_block_device_symlinks(struct uevent *uevent)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700486{
Colin Crossfadb85e2011-03-30 18:32:12 -0700487 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800488 struct platform_node *pdev;
Dan Austin60b976d2016-03-28 14:22:12 -0700489 const char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700490 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700491 char buf[256];
492 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700493 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700494 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700495
Dima Zavinf395c922013-03-06 16:23:57 -0800496 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700497 if (pdev) {
498 device = pdev->name;
499 type = "platform";
500 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
501 device = buf;
502 type = "pci";
503 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800504 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700505 }
Dima Zavinf395c922013-03-06 16:23:57 -0800506
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800507 char **links = (char**) malloc(sizeof(char *) * 4);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700508 if (!links)
509 return NULL;
510 memset(links, 0, sizeof(char *) * 4);
511
Andrew Boiea885d042013-09-13 17:41:20 -0700512 INFO("found %s device %s\n", type, device);
Colin Crossfadb85e2011-03-30 18:32:12 -0700513
Andrew Boiea885d042013-09-13 17:41:20 -0700514 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700515
516 if (uevent->partition_name) {
517 p = strdup(uevent->partition_name);
518 sanitize(p);
Johan Redestig93ca79b2012-04-18 16:41:19 +0200519 if (strcmp(uevent->partition_name, p))
520 NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700521 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
522 link_num++;
523 else
524 links[link_num] = NULL;
525 free(p);
526 }
527
528 if (uevent->partition_num >= 0) {
529 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
530 link_num++;
531 else
532 links[link_num] = NULL;
533 }
534
Dima Zavinf395c922013-03-06 16:23:57 -0800535 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700536 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
537 link_num++;
538 else
539 links[link_num] = NULL;
540
541 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700542}
543
Colin Crosseb5ba832011-03-30 17:37:17 -0700544static void handle_device(const char *action, const char *devpath,
545 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700546{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700547 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700548
Colin Crosseb5ba832011-03-30 17:37:17 -0700549 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400550 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700551 if (links) {
552 for (i = 0; links[i]; i++)
Chris Fries79f33842013-09-05 13:19:21 -0500553 make_link_init(devpath, links[i]);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700554 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700555 }
556
Colin Crosseb5ba832011-03-30 17:37:17 -0700557 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700558 if (links) {
559 for (i = 0; links[i]; i++)
560 remove_link(devpath, links[i]);
561 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700562 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700563 }
564
565 if (links) {
566 for (i = 0; links[i]; i++)
567 free(links[i]);
568 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700569 }
570}
571
Colin Crossfadb85e2011-03-30 18:32:12 -0700572static void handle_platform_device_event(struct uevent *uevent)
573{
Dima Zavinf395c922013-03-06 16:23:57 -0800574 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700575
576 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800577 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700578 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800579 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700580}
581
Colin Crosseb5ba832011-03-30 17:37:17 -0700582static const char *parse_device_name(struct uevent *uevent, unsigned int len)
583{
584 const char *name;
585
586 /* if it's not a /dev device, nothing else to do */
587 if((uevent->major < 0) || (uevent->minor < 0))
588 return NULL;
589
590 /* do we have a name? */
591 name = strrchr(uevent->path, '/');
592 if(!name)
593 return NULL;
594 name++;
595
596 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800597 if(strlen(name) > len) {
598 ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n",
599 name, len);
Colin Crosseb5ba832011-03-30 17:37:17 -0700600 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800601 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700602
603 return name;
604}
605
606static void handle_block_device_event(struct uevent *uevent)
607{
608 const char *base = "/dev/block/";
609 const char *name;
610 char devpath[96];
611 char **links = NULL;
612
613 name = parse_device_name(uevent, 64);
614 if (!name)
615 return;
616
617 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500618 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700619
Dima Zavinf395c922013-03-06 16:23:57 -0800620 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700621 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700622
623 handle_device(uevent->action, devpath, uevent->path, 1,
624 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700625}
626
Greg Hackmann3312aa82013-11-18 15:24:40 -0800627#define DEVPATH_LEN 96
628
629static bool assemble_devpath(char *devpath, const char *dirname,
630 const char *devname)
631{
632 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
633 if (s < 0) {
634 ERROR("failed to assemble device path (%s); ignoring event\n",
635 strerror(errno));
636 return false;
637 } else if (s >= DEVPATH_LEN) {
638 ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n",
639 dirname, devname, DEVPATH_LEN);
640 return false;
641 }
642 return true;
643}
644
645static void mkdir_recursive_for_devpath(const char *devpath)
646{
647 char dir[DEVPATH_LEN];
648 char *slash;
649
650 strcpy(dir, devpath);
651 slash = strrchr(dir, '/');
652 *slash = '\0';
653 mkdir_recursive(dir, 0755);
654}
655
Colin Crosseb5ba832011-03-30 17:37:17 -0700656static void handle_generic_device_event(struct uevent *uevent)
657{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800658 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700659 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800660 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700661 char **links = NULL;
662
663 name = parse_device_name(uevent, 64);
664 if (!name)
665 return;
666
Greg Hackmann3312aa82013-11-18 15:24:40 -0800667 struct ueventd_subsystem *subsystem =
668 ueventd_subsystem_find_by_name(uevent->subsystem);
669
670 if (subsystem) {
671 const char *devname;
672
673 switch (subsystem->devname_src) {
674 case DEVNAME_UEVENT_DEVNAME:
675 devname = uevent->device_name;
676 break;
677
678 case DEVNAME_UEVENT_DEVPATH:
679 devname = name;
680 break;
681
682 default:
683 ERROR("%s subsystem's devpath option is not set; ignoring event\n",
684 uevent->subsystem);
685 return;
686 }
687
688 if (!assemble_devpath(devpath, subsystem->dirname, devname))
689 return;
690 mkdir_recursive_for_devpath(devpath);
691 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700692 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700693 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800694 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800695 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800696 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700697 }
698 else {
699 /* This imitates the file system that would be created
700 * if we were using devfs instead.
701 * Minors are broken up into groups of 128, starting at "001"
702 */
703 int bus_id = uevent->minor / 128 + 1;
704 int device_id = uevent->minor % 128 + 1;
705 /* build directories */
706 make_dir("/dev/bus", 0755);
707 make_dir("/dev/bus/usb", 0755);
708 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
709 make_dir(devpath, 0755);
710 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
711 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700712 } else {
713 /* ignore other USB events */
714 return;
715 }
716 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
717 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500718 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200719 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
720 base = "/dev/dri/";
721 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700722 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
723 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500724 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700725 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
726 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500727 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700728 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
729 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500730 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700731 } else if(!strncmp(uevent->subsystem, "input", 5)) {
732 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500733 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700734 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
735 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500736 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700737 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
738 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500739 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700740 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
741 !strncmp(name, "log_", 4)) {
Elliott Hughesf682b472015-02-06 12:19:48 -0800742 INFO("kernel logger is deprecated\n");
Colin Crosseb5ba832011-03-30 17:37:17 -0700743 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500744 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700745 name += 4;
746 } else
747 base = "/dev/";
748 links = get_character_device_symlinks(uevent);
749
750 if (!devpath[0])
751 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
752
753 handle_device(uevent->action, devpath, uevent->path, 0,
754 uevent->major, uevent->minor, links);
755}
756
757static void handle_device_event(struct uevent *uevent)
758{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700759 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Rob Herring6de783a2016-05-06 10:06:59 -0500760 fixup_sys_perms(uevent->path, uevent->subsystem);
Colin Crosseb5ba832011-03-30 17:37:17 -0700761
762 if (!strncmp(uevent->subsystem, "block", 5)) {
763 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700764 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
765 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700766 } else {
767 handle_generic_device_event(uevent);
768 }
769}
770
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700771static int load_firmware(int fw_fd, int loading_fd, int data_fd)
772{
773 struct stat st;
774 long len_to_copy;
775 int ret = 0;
776
777 if(fstat(fw_fd, &st) < 0)
778 return -1;
779 len_to_copy = st.st_size;
780
781 write(loading_fd, "1", 1); /* start transfer */
782
783 while (len_to_copy > 0) {
784 char buf[PAGE_SIZE];
785 ssize_t nr;
786
787 nr = read(fw_fd, buf, sizeof(buf));
788 if(!nr)
789 break;
790 if(nr < 0) {
791 ret = -1;
792 break;
793 }
Biao Ludc848562016-01-28 16:10:54 +0800794 if (!android::base::WriteFully(data_fd, buf, nr)) {
795 ret = -1;
796 break;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700797 }
Biao Ludc848562016-01-28 16:10:54 +0800798 len_to_copy -= nr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700799 }
800
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700801 if(!ret)
802 write(loading_fd, "0", 1); /* successful end of transfer */
803 else
804 write(loading_fd, "-1", 2); /* abort transfer */
805
806 return ret;
807}
808
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700809static int is_booting(void)
810{
811 return access("/dev/.booting", F_OK) == 0;
812}
813
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700814static void process_firmware_event(struct uevent *uevent)
815{
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700816 char *root, *loading, *data;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700817 int l, loading_fd, data_fd, fw_fd;
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700818 size_t i;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700819 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700820
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700821 INFO("firmware: loading '%s' for '%s'\n",
822 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700823
824 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
825 if (l == -1)
826 return;
827
828 l = asprintf(&loading, "%sloading", root);
829 if (l == -1)
830 goto root_free_out;
831
832 l = asprintf(&data, "%sdata", root);
833 if (l == -1)
834 goto loading_free_out;
835
Nick Kralevich45a884f2015-02-02 14:37:22 -0800836 loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700837 if(loading_fd < 0)
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700838 goto data_free_out;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700839
Nick Kralevich45a884f2015-02-02 14:37:22 -0800840 data_fd = open(data, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700841 if(data_fd < 0)
842 goto loading_close_out;
843
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700844try_loading_again:
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700845 for (i = 0; i < ARRAY_SIZE(firmware_dirs); i++) {
846 char *file = NULL;
847 l = asprintf(&file, "%s/%s", firmware_dirs[i], uevent->firmware);
848 if (l == -1)
849 goto data_free_out;
850 fw_fd = open(file, O_RDONLY|O_CLOEXEC);
851 free(file);
852 if (fw_fd >= 0) {
853 if(!load_firmware(fw_fd, loading_fd, data_fd))
854 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
855 else
856 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
857 break;
Benoit Goby609d8822010-11-09 18:10:24 -0800858 }
Brian Swetland02863b92010-09-19 03:36:39 -0700859 }
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700860 if (fw_fd < 0) {
861 if (booting) {
862 /* If we're not fully booted, we may be missing
863 * filesystems needed for firmware, wait and retry.
864 */
865 usleep(100000);
866 booting = is_booting();
867 goto try_loading_again;
868 }
Elliott Hughescd67f002015-03-20 17:05:56 -0700869 INFO("firmware: could not open '%s': %s\n", uevent->firmware, strerror(errno));
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700870 write(loading_fd, "-1", 2);
871 goto data_close_out;
872 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700873
874 close(fw_fd);
875data_close_out:
876 close(data_fd);
877loading_close_out:
878 close(loading_fd);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700879data_free_out:
880 free(data);
881loading_free_out:
882 free(loading);
883root_free_out:
884 free(root);
885}
886
887static void handle_firmware_event(struct uevent *uevent)
888{
889 pid_t pid;
890
891 if(strcmp(uevent->subsystem, "firmware"))
892 return;
893
894 if(strcmp(uevent->action, "add"))
895 return;
896
897 /* we fork, to avoid making large memory allocations in init proper */
898 pid = fork();
899 if (!pid) {
900 process_firmware_event(uevent);
Kenny Root17baff42014-08-20 16:16:44 -0700901 _exit(EXIT_SUCCESS);
902 } else if (pid < 0) {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800903 ERROR("could not fork to process firmware event: %s\n", strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700904 }
905}
906
Ruchi Kandoic6037202014-06-23 11:22:09 -0700907#define UEVENT_MSG_LEN 2048
Colin Cross0dd7ca62010-04-13 19:25:51 -0700908void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700909{
Vernon Tang3f582e92011-04-25 13:08:17 +1000910 char msg[UEVENT_MSG_LEN+2];
911 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700912 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700913 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700914 continue;
915
916 msg[n] = '\0';
917 msg[n+1] = '\0';
918
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700919 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700920 parse_event(msg, &uevent);
921
Nick Kralevich4d870952015-06-12 22:03:50 -0700922 if (selinux_status_updated() > 0) {
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400923 struct selabel_handle *sehandle2;
924 sehandle2 = selinux_android_file_context_handle();
925 if (sehandle2) {
926 selabel_close(sehandle);
927 sehandle = sehandle2;
928 }
929 }
930
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700931 handle_device_event(&uevent);
932 handle_firmware_event(&uevent);
933 }
934}
935
936/* Coldboot walks parts of the /sys tree and pokes the uevent files
937** to cause the kernel to regenerate device add events that happened
938** before init's device manager was started
939**
940** We drain any pending events from the netlink socket every time
941** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800942** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700943*/
944
Colin Cross0dd7ca62010-04-13 19:25:51 -0700945static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700946{
947 struct dirent *de;
948 int dfd, fd;
949
950 dfd = dirfd(d);
951
952 fd = openat(dfd, "uevent", O_WRONLY);
953 if(fd >= 0) {
954 write(fd, "add\n", 4);
955 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700956 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700957 }
958
959 while((de = readdir(d))) {
960 DIR *d2;
961
962 if(de->d_type != DT_DIR || de->d_name[0] == '.')
963 continue;
964
965 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
966 if(fd < 0)
967 continue;
968
969 d2 = fdopendir(fd);
970 if(d2 == 0)
971 close(fd);
972 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700973 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700974 closedir(d2);
975 }
976 }
977}
978
Colin Cross0dd7ca62010-04-13 19:25:51 -0700979static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700980{
James Hawkins588a2ca2016-02-18 14:52:46 -0800981 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700982 if(d) {
James Hawkins588a2ca2016-02-18 14:52:46 -0800983 do_coldboot(d.get());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700984 }
985}
986
Elliott Hughes74738362015-03-28 10:51:23 -0700987void device_init() {
Nick Kralevich4d870952015-06-12 22:03:50 -0700988 sehandle = selinux_android_file_context_handle();
989 selinux_status_open(true);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700990
Andrew Boied562ca72012-12-04 15:47:20 -0800991 /* is 256K enough? udev uses 16MB! */
992 device_fd = uevent_open_socket(256*1024, true);
Elliott Hughes56a06562015-03-28 11:23:32 -0700993 if (device_fd == -1) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700994 return;
Elliott Hughes56a06562015-03-28 11:23:32 -0700995 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700996 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700997
Elliott Hughes56a06562015-03-28 11:23:32 -0700998 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700999 NOTICE("Skipping coldboot, already done!\n");
Elliott Hughes56a06562015-03-28 11:23:32 -07001000 return;
Colin Crossf83d0b92010-04-21 12:04:20 -07001001 }
Elliott Hughes56a06562015-03-28 11:23:32 -07001002
1003 Timer t;
1004 coldboot("/sys/class");
1005 coldboot("/sys/block");
1006 coldboot("/sys/devices");
1007 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
1008 NOTICE("Coldboot took %.2fs.\n", t.duration());
Colin Cross0dd7ca62010-04-13 19:25:51 -07001009}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001010
Colin Cross0dd7ca62010-04-13 19:25:51 -07001011int get_device_fd()
1012{
1013 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001014}