blob: 58b2704b489af560b98d488ab6f126b8438c76a1 [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
Mark Salyzyn154f4602014-02-20 14:59:07 -080051#define UNUSED __attribute__((__unused__))
52
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053#define SYSFS_PREFIX "/sys"
Brian Swetland02863b92010-09-19 03:36:39 -070054#define FIRMWARE_DIR1 "/etc/firmware"
55#define FIRMWARE_DIR2 "/vendor/firmware"
Iliyan Malchev029d44e2012-06-08 10:42:26 -070056#define FIRMWARE_DIR3 "/firmware/image"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070057
Stephen Smalleye096e362012-06-11 13:37:39 -040058extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050059
Colin Cross0dd7ca62010-04-13 19:25:51 -070060static int device_fd = -1;
61
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070062struct uevent {
63 const char *action;
64 const char *path;
65 const char *subsystem;
66 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070067 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070068 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070069 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070 int major;
71 int minor;
72};
73
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070074struct perms_ {
75 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070076 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070077 mode_t perm;
78 unsigned int uid;
79 unsigned int gid;
80 unsigned short prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070081 unsigned short wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070082};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070083
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070084struct perm_node {
85 struct perms_ dp;
86 struct listnode plist;
87};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070088
Colin Crossfadb85e2011-03-30 18:32:12 -070089struct platform_node {
90 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080091 char *path;
92 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070093 struct listnode list;
94};
95
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070096static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070097static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070098static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070099
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700100int add_dev_perms(const char *name, const char *attr,
101 mode_t perm, unsigned int uid, unsigned int gid,
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700102 unsigned short prefix,
103 unsigned short wildcard) {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700104 struct perm_node *node = calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700105 if (!node)
106 return -ENOMEM;
107
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700108 node->dp.name = strdup(name);
109 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700110 return -ENOMEM;
111
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700112 if (attr) {
113 node->dp.attr = strdup(attr);
114 if (!node->dp.attr)
115 return -ENOMEM;
116 }
117
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700118 node->dp.perm = perm;
119 node->dp.uid = uid;
120 node->dp.gid = gid;
121 node->dp.prefix = prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700122 node->dp.wildcard = wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700123
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700124 if (attr)
125 list_add_tail(&sys_perms, &node->plist);
126 else
127 list_add_tail(&dev_perms, &node->plist);
128
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700129 return 0;
130}
131
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700132void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700133{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700134 char buf[512];
135 struct listnode *node;
136 struct perms_ *dp;
Stephen Smalley5f7b0172013-04-03 13:15:01 -0400137 char *secontext;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700138
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700139 /* upaths omit the "/sys" that paths in this list
140 * contain, so we add 4 when comparing...
141 */
142 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))
156 return;
157
158 sprintf(buf,"/sys%s/%s", upath, dp->attr);
159 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);
Stephen Smalley5f7b0172013-04-03 13:15:01 -0400162 if (sehandle) {
163 secontext = NULL;
164 selabel_lookup(sehandle, &secontext, buf, 0);
165 if (secontext) {
166 setfilecon(buf, secontext);
167 freecon(secontext);
168 }
169 }
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{
192 mode_t perm;
Colin Cross44b65d02010-04-20 14:32:50 -0700193 struct listnode *node;
194 struct perm_node *perm_node;
195 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700196
Colin Cross44b65d02010-04-20 14:32:50 -0700197 /* search the perms list in reverse so that ueventd.$hardware can
198 * override ueventd.rc
199 */
200 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700201 bool match = false;
202
Colin Cross44b65d02010-04-20 14:32:50 -0700203 perm_node = node_to_item(node, struct perm_node, plist);
204 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700205
Colin Cross43d537e2014-07-02 13:08:13 -0700206 if (perm_path_matches(path, dp)) {
207 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700208 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700209 if (links) {
210 int i;
211 for (i = 0; links[i]; i++) {
212 if (perm_path_matches(links[i], dp)) {
213 match = true;
214 break;
215 }
216 }
217 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700218 }
Colin Cross43d537e2014-07-02 13:08:13 -0700219
220 if (match) {
221 *uid = dp->uid;
222 *gid = dp->gid;
223 return dp->perm;
224 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700225 }
Colin Cross44b65d02010-04-20 14:32:50 -0700226 /* Default if nothing found. */
227 *uid = 0;
228 *gid = 0;
229 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700230}
231
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700232static void make_device(const char *path,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800233 const char *upath UNUSED,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400234 int block, int major, int minor,
235 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700236{
237 unsigned uid;
238 unsigned gid;
239 mode_t mode;
240 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500241 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700242
Colin Cross43d537e2014-07-02 13:08:13 -0700243 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700244
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500245 if (sehandle) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400246 selabel_lookup_best_match(sehandle, &secontext, path, links, mode);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500247 setfscreatecon(secontext);
248 }
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);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700257 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800258 chown(path, uid, -1);
259 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700260
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500261 if (secontext) {
262 freecon(secontext);
263 setfscreatecon(NULL);
264 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700265}
266
Dima Zavinf395c922013-03-06 16:23:57 -0800267static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700268{
Dima Zavinf395c922013-03-06 16:23:57 -0800269 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700270 struct listnode *node;
271 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800272 const char *name = path;
273
274 if (!strncmp(path, "/devices/", 9)) {
275 name += 9;
276 if (!strncmp(name, "platform/", 9))
277 name += 9;
278 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700279
280 list_for_each_reverse(node, &platform_names) {
281 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800282 if ((bus->path_len < path_len) &&
283 (path[bus->path_len] == '/') &&
284 !strncmp(path, bus->path, bus->path_len))
Colin Crossfadb85e2011-03-30 18:32:12 -0700285 /* subdevice of an existing platform, ignore it */
286 return;
287 }
288
Dima Zavinf395c922013-03-06 16:23:57 -0800289 INFO("adding platform device %s (%s)\n", name, path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700290
291 bus = calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800292 bus->path = strdup(path);
293 bus->path_len = path_len;
294 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700295 list_add_tail(&platform_names, &bus->list);
296}
297
298/*
Dima Zavinf395c922013-03-06 16:23:57 -0800299 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700300 * platform device prefix. If it doesn't start with a platform device, return
301 * 0.
302 */
Dima Zavinf395c922013-03-06 16:23:57 -0800303static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700304{
Dima Zavinf395c922013-03-06 16:23:57 -0800305 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700306 struct listnode *node;
307 struct platform_node *bus;
308
309 list_for_each_reverse(node, &platform_names) {
310 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800311 if ((bus->path_len < path_len) &&
312 (path[bus->path_len] == '/') &&
313 !strncmp(path, bus->path, bus->path_len))
314 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700315 }
316
317 return NULL;
318}
319
Dima Zavinf395c922013-03-06 16:23:57 -0800320static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700321{
322 struct listnode *node;
323 struct platform_node *bus;
324
325 list_for_each_reverse(node, &platform_names) {
326 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800327 if (!strcmp(path, bus->path)) {
328 INFO("removing platform device %s\n", bus->name);
329 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700330 list_remove(node);
331 free(bus);
332 return;
333 }
334 }
335}
336
Andrew Boiea885d042013-09-13 17:41:20 -0700337/* Given a path that may start with a PCI device, populate the supplied buffer
338 * with the PCI domain/bus number and the peripheral ID and return 0.
339 * If it doesn't start with a PCI device, or there is some error, return -1 */
340static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
341{
342 const char *start, *end;
343
344 if (strncmp(path, "/devices/pci", 12))
345 return -1;
346
347 /* Beginning of the prefix is the initial "pci" after "/devices/" */
348 start = path + 9;
349
350 /* End of the prefix is two path '/' later, capturing the domain/bus number
351 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
352 end = strchr(start, '/');
353 if (!end)
354 return -1;
355 end = strchr(end + 1, '/');
356 if (!end)
357 return -1;
358
359 /* Make sure we have enough room for the string plus null terminator */
360 if (end - start + 1 > buf_sz)
361 return -1;
362
363 strncpy(buf, start, end - start);
364 buf[end - start] = '\0';
365 return 0;
366}
367
Chuck Tuffli1e070842008-12-15 14:26:56 -0800368#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700369
370static inline suseconds_t get_usecs(void)
371{
372 struct timeval tv;
373 gettimeofday(&tv, 0);
374 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
375}
376
377#define log_event_print(x...) INFO(x)
378
379#else
380
381#define log_event_print(fmt, args...) do { } while (0)
382#define get_usecs() 0
383
384#endif
385
386static void parse_event(const char *msg, struct uevent *uevent)
387{
388 uevent->action = "";
389 uevent->path = "";
390 uevent->subsystem = "";
391 uevent->firmware = "";
392 uevent->major = -1;
393 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700394 uevent->partition_name = NULL;
395 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700396 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700397
398 /* currently ignoring SEQNUM */
399 while(*msg) {
400 if(!strncmp(msg, "ACTION=", 7)) {
401 msg += 7;
402 uevent->action = msg;
403 } else if(!strncmp(msg, "DEVPATH=", 8)) {
404 msg += 8;
405 uevent->path = msg;
406 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
407 msg += 10;
408 uevent->subsystem = msg;
409 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
410 msg += 9;
411 uevent->firmware = msg;
412 } else if(!strncmp(msg, "MAJOR=", 6)) {
413 msg += 6;
414 uevent->major = atoi(msg);
415 } else if(!strncmp(msg, "MINOR=", 6)) {
416 msg += 6;
417 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700418 } else if(!strncmp(msg, "PARTN=", 6)) {
419 msg += 6;
420 uevent->partition_num = atoi(msg);
421 } else if(!strncmp(msg, "PARTNAME=", 9)) {
422 msg += 9;
423 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700424 } else if(!strncmp(msg, "DEVNAME=", 8)) {
425 msg += 8;
426 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700427 }
428
Wei Zhongf97b8872012-03-23 14:15:34 -0700429 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700430 while(*msg++)
431 ;
432 }
433
434 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
435 uevent->action, uevent->path, uevent->subsystem,
436 uevent->firmware, uevent->major, uevent->minor);
437}
438
Benoit Gobyd2278632010-08-03 14:36:02 -0700439static char **get_character_device_symlinks(struct uevent *uevent)
440{
441 const char *parent;
442 char *slash;
443 char **links;
444 int link_num = 0;
445 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800446 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700447
Dima Zavinf395c922013-03-06 16:23:57 -0800448 pdev = find_platform_device(uevent->path);
449 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700450 return NULL;
451
452 links = malloc(sizeof(char *) * 2);
453 if (!links)
454 return NULL;
455 memset(links, 0, sizeof(char *) * 2);
456
457 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800458 parent = strchr(uevent->path + pdev->path_len, '/');
Benoit Gobyd2278632010-08-03 14:36:02 -0700459 if (!*parent)
460 goto err;
461
462 if (!strncmp(parent, "/usb", 4)) {
463 /* skip root hub name and device. use device interface */
464 while (*++parent && *parent != '/');
465 if (*parent)
466 while (*++parent && *parent != '/');
467 if (!*parent)
468 goto err;
469 slash = strchr(++parent, '/');
470 if (!slash)
471 goto err;
472 width = slash - parent;
473 if (width <= 0)
474 goto err;
475
476 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
477 link_num++;
478 else
479 links[link_num] = NULL;
480 mkdir("/dev/usb", 0755);
481 }
482 else {
483 goto err;
484 }
485
486 return links;
487err:
488 free(links);
489 return NULL;
490}
491
Andrew Boiea885d042013-09-13 17:41:20 -0700492static char **get_block_device_symlinks(struct uevent *uevent)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700493{
Colin Crossfadb85e2011-03-30 18:32:12 -0700494 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800495 struct platform_node *pdev;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700496 char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700497 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700498 int width;
499 char buf[256];
500 char link_path[256];
501 int fd;
502 int link_num = 0;
503 int ret;
504 char *p;
505 unsigned int size;
506 struct stat info;
507
Dima Zavinf395c922013-03-06 16:23:57 -0800508 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700509 if (pdev) {
510 device = pdev->name;
511 type = "platform";
512 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
513 device = buf;
514 type = "pci";
515 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800516 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700517 }
Dima Zavinf395c922013-03-06 16:23:57 -0800518
Colin Crossb0ab94b2010-04-08 16:16:20 -0700519 char **links = malloc(sizeof(char *) * 4);
520 if (!links)
521 return NULL;
522 memset(links, 0, sizeof(char *) * 4);
523
Andrew Boiea885d042013-09-13 17:41:20 -0700524 INFO("found %s device %s\n", type, device);
Colin Crossfadb85e2011-03-30 18:32:12 -0700525
Andrew Boiea885d042013-09-13 17:41:20 -0700526 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700527
528 if (uevent->partition_name) {
529 p = strdup(uevent->partition_name);
530 sanitize(p);
Johan Redestig93ca79b2012-04-18 16:41:19 +0200531 if (strcmp(uevent->partition_name, p))
532 NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700533 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
534 link_num++;
535 else
536 links[link_num] = NULL;
537 free(p);
538 }
539
540 if (uevent->partition_num >= 0) {
541 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
542 link_num++;
543 else
544 links[link_num] = NULL;
545 }
546
Dima Zavinf395c922013-03-06 16:23:57 -0800547 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700548 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
549 link_num++;
550 else
551 links[link_num] = NULL;
552
553 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700554}
555
Colin Crosseb5ba832011-03-30 17:37:17 -0700556static void handle_device(const char *action, const char *devpath,
557 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700558{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700559 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700560
Colin Crosseb5ba832011-03-30 17:37:17 -0700561 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400562 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700563 if (links) {
564 for (i = 0; links[i]; i++)
565 make_link(devpath, links[i]);
566 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700567 }
568
Colin Crosseb5ba832011-03-30 17:37:17 -0700569 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700570 if (links) {
571 for (i = 0; links[i]; i++)
572 remove_link(devpath, links[i]);
573 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700574 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700575 }
576
577 if (links) {
578 for (i = 0; links[i]; i++)
579 free(links[i]);
580 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700581 }
582}
583
Colin Crossfadb85e2011-03-30 18:32:12 -0700584static void handle_platform_device_event(struct uevent *uevent)
585{
Dima Zavinf395c922013-03-06 16:23:57 -0800586 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700587
588 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800589 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700590 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800591 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700592}
593
Colin Crosseb5ba832011-03-30 17:37:17 -0700594static const char *parse_device_name(struct uevent *uevent, unsigned int len)
595{
596 const char *name;
597
598 /* if it's not a /dev device, nothing else to do */
599 if((uevent->major < 0) || (uevent->minor < 0))
600 return NULL;
601
602 /* do we have a name? */
603 name = strrchr(uevent->path, '/');
604 if(!name)
605 return NULL;
606 name++;
607
608 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800609 if(strlen(name) > len) {
610 ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n",
611 name, len);
Colin Crosseb5ba832011-03-30 17:37:17 -0700612 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800613 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700614
615 return name;
616}
617
618static void handle_block_device_event(struct uevent *uevent)
619{
620 const char *base = "/dev/block/";
621 const char *name;
622 char devpath[96];
623 char **links = NULL;
624
625 name = parse_device_name(uevent, 64);
626 if (!name)
627 return;
628
629 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500630 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700631
Dima Zavinf395c922013-03-06 16:23:57 -0800632 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700633 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700634
635 handle_device(uevent->action, devpath, uevent->path, 1,
636 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700637}
638
Greg Hackmann3312aa82013-11-18 15:24:40 -0800639#define DEVPATH_LEN 96
640
641static bool assemble_devpath(char *devpath, const char *dirname,
642 const char *devname)
643{
644 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
645 if (s < 0) {
646 ERROR("failed to assemble device path (%s); ignoring event\n",
647 strerror(errno));
648 return false;
649 } else if (s >= DEVPATH_LEN) {
650 ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n",
651 dirname, devname, DEVPATH_LEN);
652 return false;
653 }
654 return true;
655}
656
657static void mkdir_recursive_for_devpath(const char *devpath)
658{
659 char dir[DEVPATH_LEN];
660 char *slash;
661
662 strcpy(dir, devpath);
663 slash = strrchr(dir, '/');
664 *slash = '\0';
665 mkdir_recursive(dir, 0755);
666}
667
Mark Salyzyn154f4602014-02-20 14:59:07 -0800668static inline void __attribute__((__deprecated__)) kernel_logger()
669{
670 INFO("kernel logger is deprecated\n");
671}
672
Colin Crosseb5ba832011-03-30 17:37:17 -0700673static void handle_generic_device_event(struct uevent *uevent)
674{
675 char *base;
676 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800677 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700678 char **links = NULL;
679
680 name = parse_device_name(uevent, 64);
681 if (!name)
682 return;
683
Greg Hackmann3312aa82013-11-18 15:24:40 -0800684 struct ueventd_subsystem *subsystem =
685 ueventd_subsystem_find_by_name(uevent->subsystem);
686
687 if (subsystem) {
688 const char *devname;
689
690 switch (subsystem->devname_src) {
691 case DEVNAME_UEVENT_DEVNAME:
692 devname = uevent->device_name;
693 break;
694
695 case DEVNAME_UEVENT_DEVPATH:
696 devname = name;
697 break;
698
699 default:
700 ERROR("%s subsystem's devpath option is not set; ignoring event\n",
701 uevent->subsystem);
702 return;
703 }
704
705 if (!assemble_devpath(devpath, subsystem->dirname, devname))
706 return;
707 mkdir_recursive_for_devpath(devpath);
708 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700709 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700710 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800711 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800712 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800713 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700714 }
715 else {
716 /* This imitates the file system that would be created
717 * if we were using devfs instead.
718 * Minors are broken up into groups of 128, starting at "001"
719 */
720 int bus_id = uevent->minor / 128 + 1;
721 int device_id = uevent->minor % 128 + 1;
722 /* build directories */
723 make_dir("/dev/bus", 0755);
724 make_dir("/dev/bus/usb", 0755);
725 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
726 make_dir(devpath, 0755);
727 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
728 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700729 } else {
730 /* ignore other USB events */
731 return;
732 }
733 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
734 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500735 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200736 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
737 base = "/dev/dri/";
738 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700739 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
740 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500741 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700742 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
743 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500744 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700745 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
746 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500747 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700748 } else if(!strncmp(uevent->subsystem, "input", 5)) {
749 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500750 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700751 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
752 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500753 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700754 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
755 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500756 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700757 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
758 !strncmp(name, "log_", 4)) {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800759 kernel_logger();
Colin Crosseb5ba832011-03-30 17:37:17 -0700760 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500761 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700762 name += 4;
763 } else
764 base = "/dev/";
765 links = get_character_device_symlinks(uevent);
766
767 if (!devpath[0])
768 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
769
770 handle_device(uevent->action, devpath, uevent->path, 0,
771 uevent->major, uevent->minor, links);
772}
773
774static void handle_device_event(struct uevent *uevent)
775{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700776 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700777 fixup_sys_perms(uevent->path);
778
779 if (!strncmp(uevent->subsystem, "block", 5)) {
780 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700781 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
782 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700783 } else {
784 handle_generic_device_event(uevent);
785 }
786}
787
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700788static int load_firmware(int fw_fd, int loading_fd, int data_fd)
789{
790 struct stat st;
791 long len_to_copy;
792 int ret = 0;
793
794 if(fstat(fw_fd, &st) < 0)
795 return -1;
796 len_to_copy = st.st_size;
797
798 write(loading_fd, "1", 1); /* start transfer */
799
800 while (len_to_copy > 0) {
801 char buf[PAGE_SIZE];
802 ssize_t nr;
803
804 nr = read(fw_fd, buf, sizeof(buf));
805 if(!nr)
806 break;
807 if(nr < 0) {
808 ret = -1;
809 break;
810 }
811
812 len_to_copy -= nr;
813 while (nr > 0) {
814 ssize_t nw = 0;
815
816 nw = write(data_fd, buf + nw, nr);
817 if(nw <= 0) {
818 ret = -1;
819 goto out;
820 }
821 nr -= nw;
822 }
823 }
824
825out:
826 if(!ret)
827 write(loading_fd, "0", 1); /* successful end of transfer */
828 else
829 write(loading_fd, "-1", 2); /* abort transfer */
830
831 return ret;
832}
833
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700834static int is_booting(void)
835{
836 return access("/dev/.booting", F_OK) == 0;
837}
838
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700839static void process_firmware_event(struct uevent *uevent)
840{
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700841 char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700842 int l, loading_fd, data_fd, fw_fd;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700843 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700844
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700845 INFO("firmware: loading '%s' for '%s'\n",
846 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700847
848 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
849 if (l == -1)
850 return;
851
852 l = asprintf(&loading, "%sloading", root);
853 if (l == -1)
854 goto root_free_out;
855
856 l = asprintf(&data, "%sdata", root);
857 if (l == -1)
858 goto loading_free_out;
859
Brian Swetland02863b92010-09-19 03:36:39 -0700860 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
861 if (l == -1)
862 goto data_free_out;
863
864 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700865 if (l == -1)
866 goto data_free_out;
867
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700868 l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware);
869 if (l == -1)
870 goto data_free_out;
871
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700872 loading_fd = open(loading, O_WRONLY);
873 if(loading_fd < 0)
874 goto file_free_out;
875
876 data_fd = open(data, O_WRONLY);
877 if(data_fd < 0)
878 goto loading_close_out;
879
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700880try_loading_again:
Brian Swetland02863b92010-09-19 03:36:39 -0700881 fw_fd = open(file1, O_RDONLY);
882 if(fw_fd < 0) {
883 fw_fd = open(file2, O_RDONLY);
Benoit Goby609d8822010-11-09 18:10:24 -0800884 if (fw_fd < 0) {
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700885 fw_fd = open(file3, O_RDONLY);
886 if (fw_fd < 0) {
887 if (booting) {
888 /* If we're not fully booted, we may be missing
889 * filesystems needed for firmware, wait and retry.
890 */
891 usleep(100000);
892 booting = is_booting();
893 goto try_loading_again;
894 }
895 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
896 write(loading_fd, "-1", 2);
897 goto data_close_out;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700898 }
Benoit Goby609d8822010-11-09 18:10:24 -0800899 }
Brian Swetland02863b92010-09-19 03:36:39 -0700900 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700901
902 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700903 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700904 else
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700905 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700906
907 close(fw_fd);
908data_close_out:
909 close(data_fd);
910loading_close_out:
911 close(loading_fd);
912file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700913 free(file1);
914 free(file2);
Ajay Dudani76c58892013-06-12 15:55:26 -0700915 free(file3);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700916data_free_out:
917 free(data);
918loading_free_out:
919 free(loading);
920root_free_out:
921 free(root);
922}
923
924static void handle_firmware_event(struct uevent *uevent)
925{
926 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700927 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700928
929 if(strcmp(uevent->subsystem, "firmware"))
930 return;
931
932 if(strcmp(uevent->action, "add"))
933 return;
934
935 /* we fork, to avoid making large memory allocations in init proper */
936 pid = fork();
937 if (!pid) {
938 process_firmware_event(uevent);
939 exit(EXIT_SUCCESS);
940 }
941}
942
943#define UEVENT_MSG_LEN 1024
Colin Cross0dd7ca62010-04-13 19:25:51 -0700944void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700945{
Vernon Tang3f582e92011-04-25 13:08:17 +1000946 char msg[UEVENT_MSG_LEN+2];
947 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700948 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700949 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700950 continue;
951
952 msg[n] = '\0';
953 msg[n+1] = '\0';
954
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700955 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700956 parse_event(msg, &uevent);
957
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400958 if (sehandle && selinux_status_updated() > 0) {
959 struct selabel_handle *sehandle2;
960 sehandle2 = selinux_android_file_context_handle();
961 if (sehandle2) {
962 selabel_close(sehandle);
963 sehandle = sehandle2;
964 }
965 }
966
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700967 handle_device_event(&uevent);
968 handle_firmware_event(&uevent);
969 }
970}
971
972/* Coldboot walks parts of the /sys tree and pokes the uevent files
973** to cause the kernel to regenerate device add events that happened
974** before init's device manager was started
975**
976** We drain any pending events from the netlink socket every time
977** we poke another uevent file to make sure we don't overrun the
978** socket's buffer.
979*/
980
Colin Cross0dd7ca62010-04-13 19:25:51 -0700981static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700982{
983 struct dirent *de;
984 int dfd, fd;
985
986 dfd = dirfd(d);
987
988 fd = openat(dfd, "uevent", O_WRONLY);
989 if(fd >= 0) {
990 write(fd, "add\n", 4);
991 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700992 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700993 }
994
995 while((de = readdir(d))) {
996 DIR *d2;
997
998 if(de->d_type != DT_DIR || de->d_name[0] == '.')
999 continue;
1000
1001 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
1002 if(fd < 0)
1003 continue;
1004
1005 d2 = fdopendir(fd);
1006 if(d2 == 0)
1007 close(fd);
1008 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001009 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001010 closedir(d2);
1011 }
1012 }
1013}
1014
Colin Cross0dd7ca62010-04-13 19:25:51 -07001015static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001016{
1017 DIR *d = opendir(path);
1018 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001019 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001020 closedir(d);
1021 }
1022}
1023
Colin Cross0dd7ca62010-04-13 19:25:51 -07001024void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001025{
1026 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -07001027 struct stat info;
1028 int fd;
Kenny Rootb5982bf2012-10-16 23:07:05 -07001029
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04001030 sehandle = NULL;
1031 if (is_selinux_enabled() > 0) {
1032 sehandle = selinux_android_file_context_handle();
Stephen Smalleye2eb69d2013-04-16 09:30:30 -04001033 selinux_status_open(true);
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04001034 }
Kenny Rootb5982bf2012-10-16 23:07:05 -07001035
Andrew Boied562ca72012-12-04 15:47:20 -08001036 /* is 256K enough? udev uses 16MB! */
1037 device_fd = uevent_open_socket(256*1024, true);
Colin Cross0dd7ca62010-04-13 19:25:51 -07001038 if(device_fd < 0)
1039 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001040
Colin Cross0dd7ca62010-04-13 19:25:51 -07001041 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
1042 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001043
Colin Crossf83d0b92010-04-21 12:04:20 -07001044 if (stat(coldboot_done, &info) < 0) {
1045 t0 = get_usecs();
1046 coldboot("/sys/class");
1047 coldboot("/sys/block");
1048 coldboot("/sys/devices");
1049 t1 = get_usecs();
1050 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
1051 close(fd);
1052 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
1053 } else {
1054 log_event_print("skipping coldboot, already done\n");
1055 }
Colin Cross0dd7ca62010-04-13 19:25:51 -07001056}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001057
Colin Cross0dd7ca62010-04-13 19:25:51 -07001058int get_device_fd()
1059{
1060 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001061}