blob: 0de92f56000f69d046c36e774515ea1d631b25e3 [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;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700137
Nick Kralevich3b4c0bd2014-07-03 16:04:34 -0700138 /* upaths omit the "/sys" that paths in this list
139 * contain, so we add 4 when comparing...
140 */
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700141 list_for_each(node, &sys_perms) {
142 dp = &(node_to_item(node, struct perm_node, plist))->dp;
143 if (dp->prefix) {
144 if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700145 continue;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700146 } else if (dp->wildcard) {
147 if (fnmatch(dp->name + 4, upath, FNM_PATHNAME) != 0)
148 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700149 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700150 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700151 continue;
152 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700153
154 if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
Nick Kralevich3b4c0bd2014-07-03 16:04:34 -0700155 break;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700156
157 sprintf(buf,"/sys%s/%s", upath, dp->attr);
158 INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
159 chown(buf, dp->uid, dp->gid);
160 chmod(buf, dp->perm);
Nick Kralevich3b4c0bd2014-07-03 16:04:34 -0700161 }
162
163 // Now fixup SELinux file labels
164 int len = snprintf(buf, sizeof(buf), "/sys%s", upath);
165 if ((len < 0) || ((size_t) len >= sizeof(buf))) {
166 // Overflow
167 return;
168 }
169 if (access(buf, F_OK) == 0) {
170 INFO("restorecon_recursive: %s\n", buf);
171 restorecon_recursive(buf);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700172 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700173}
174
Colin Cross43d537e2014-07-02 13:08:13 -0700175static bool perm_path_matches(const char *path, struct perms_ *dp)
176{
177 if (dp->prefix) {
178 if (strncmp(path, dp->name, strlen(dp->name)) == 0)
179 return true;
180 } else if (dp->wildcard) {
181 if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
182 return true;
183 } else {
184 if (strcmp(path, dp->name) == 0)
185 return true;
186 }
187
188 return false;
189}
190
191static mode_t get_device_perm(const char *path, const char **links,
192 unsigned *uid, unsigned *gid)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700193{
Colin Cross44b65d02010-04-20 14:32:50 -0700194 struct listnode *node;
195 struct perm_node *perm_node;
196 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700197
Colin Cross44b65d02010-04-20 14:32:50 -0700198 /* search the perms list in reverse so that ueventd.$hardware can
199 * override ueventd.rc
200 */
201 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700202 bool match = false;
203
Colin Cross44b65d02010-04-20 14:32:50 -0700204 perm_node = node_to_item(node, struct perm_node, plist);
205 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700206
Colin Cross43d537e2014-07-02 13:08:13 -0700207 if (perm_path_matches(path, dp)) {
208 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700209 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700210 if (links) {
211 int i;
212 for (i = 0; links[i]; i++) {
213 if (perm_path_matches(links[i], dp)) {
214 match = true;
215 break;
216 }
217 }
218 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700219 }
Colin Cross43d537e2014-07-02 13:08:13 -0700220
221 if (match) {
222 *uid = dp->uid;
223 *gid = dp->gid;
224 return dp->perm;
225 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700226 }
Colin Cross44b65d02010-04-20 14:32:50 -0700227 /* Default if nothing found. */
228 *uid = 0;
229 *gid = 0;
230 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700231}
232
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700233static void make_device(const char *path,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800234 const char *upath UNUSED,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400235 int block, int major, int minor,
236 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700237{
238 unsigned uid;
239 unsigned gid;
240 mode_t mode;
241 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500242 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700243
Colin Cross43d537e2014-07-02 13:08:13 -0700244 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700245
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500246 if (sehandle) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400247 selabel_lookup_best_match(sehandle, &secontext, path, links, mode);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500248 setfscreatecon(secontext);
249 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700250
Colin Cross17dcc5c2010-09-03 12:25:34 -0700251 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800252 /* Temporarily change egid to avoid race condition setting the gid of the
253 * device node. Unforunately changing the euid would prevent creation of
254 * some device nodes, so the uid has to be set with chown() and is still
255 * racy. Fixing the gid race at least fixed the issue with system_server
256 * opening dynamic input devices under the AID_INPUT gid. */
257 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700258 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800259 chown(path, uid, -1);
260 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700261
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500262 if (secontext) {
263 freecon(secontext);
264 setfscreatecon(NULL);
265 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700266}
267
Dima Zavinf395c922013-03-06 16:23:57 -0800268static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700269{
Dima Zavinf395c922013-03-06 16:23:57 -0800270 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700271 struct listnode *node;
272 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800273 const char *name = path;
274
275 if (!strncmp(path, "/devices/", 9)) {
276 name += 9;
277 if (!strncmp(name, "platform/", 9))
278 name += 9;
279 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700280
281 list_for_each_reverse(node, &platform_names) {
282 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800283 if ((bus->path_len < path_len) &&
284 (path[bus->path_len] == '/') &&
285 !strncmp(path, bus->path, bus->path_len))
Colin Crossfadb85e2011-03-30 18:32:12 -0700286 /* subdevice of an existing platform, ignore it */
287 return;
288 }
289
Dima Zavinf395c922013-03-06 16:23:57 -0800290 INFO("adding platform device %s (%s)\n", name, path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700291
292 bus = calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800293 bus->path = strdup(path);
294 bus->path_len = path_len;
295 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700296 list_add_tail(&platform_names, &bus->list);
297}
298
299/*
Dima Zavinf395c922013-03-06 16:23:57 -0800300 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700301 * platform device prefix. If it doesn't start with a platform device, return
302 * 0.
303 */
Dima Zavinf395c922013-03-06 16:23:57 -0800304static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700305{
Dima Zavinf395c922013-03-06 16:23:57 -0800306 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700307 struct listnode *node;
308 struct platform_node *bus;
309
310 list_for_each_reverse(node, &platform_names) {
311 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800312 if ((bus->path_len < path_len) &&
313 (path[bus->path_len] == '/') &&
314 !strncmp(path, bus->path, bus->path_len))
315 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700316 }
317
318 return NULL;
319}
320
Dima Zavinf395c922013-03-06 16:23:57 -0800321static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700322{
323 struct listnode *node;
324 struct platform_node *bus;
325
326 list_for_each_reverse(node, &platform_names) {
327 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800328 if (!strcmp(path, bus->path)) {
329 INFO("removing platform device %s\n", bus->name);
330 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700331 list_remove(node);
332 free(bus);
333 return;
334 }
335 }
336}
337
Andrew Boiea885d042013-09-13 17:41:20 -0700338/* Given a path that may start with a PCI device, populate the supplied buffer
339 * with the PCI domain/bus number and the peripheral ID and return 0.
340 * If it doesn't start with a PCI device, or there is some error, return -1 */
341static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
342{
343 const char *start, *end;
344
345 if (strncmp(path, "/devices/pci", 12))
346 return -1;
347
348 /* Beginning of the prefix is the initial "pci" after "/devices/" */
349 start = path + 9;
350
351 /* End of the prefix is two path '/' later, capturing the domain/bus number
352 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
353 end = strchr(start, '/');
354 if (!end)
355 return -1;
356 end = strchr(end + 1, '/');
357 if (!end)
358 return -1;
359
360 /* Make sure we have enough room for the string plus null terminator */
361 if (end - start + 1 > buf_sz)
362 return -1;
363
364 strncpy(buf, start, end - start);
365 buf[end - start] = '\0';
366 return 0;
367}
368
Chuck Tuffli1e070842008-12-15 14:26:56 -0800369#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700370
371static inline suseconds_t get_usecs(void)
372{
373 struct timeval tv;
374 gettimeofday(&tv, 0);
375 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
376}
377
378#define log_event_print(x...) INFO(x)
379
380#else
381
382#define log_event_print(fmt, args...) do { } while (0)
383#define get_usecs() 0
384
385#endif
386
387static void parse_event(const char *msg, struct uevent *uevent)
388{
389 uevent->action = "";
390 uevent->path = "";
391 uevent->subsystem = "";
392 uevent->firmware = "";
393 uevent->major = -1;
394 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700395 uevent->partition_name = NULL;
396 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700397 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700398
399 /* currently ignoring SEQNUM */
400 while(*msg) {
401 if(!strncmp(msg, "ACTION=", 7)) {
402 msg += 7;
403 uevent->action = msg;
404 } else if(!strncmp(msg, "DEVPATH=", 8)) {
405 msg += 8;
406 uevent->path = msg;
407 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
408 msg += 10;
409 uevent->subsystem = msg;
410 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
411 msg += 9;
412 uevent->firmware = msg;
413 } else if(!strncmp(msg, "MAJOR=", 6)) {
414 msg += 6;
415 uevent->major = atoi(msg);
416 } else if(!strncmp(msg, "MINOR=", 6)) {
417 msg += 6;
418 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700419 } else if(!strncmp(msg, "PARTN=", 6)) {
420 msg += 6;
421 uevent->partition_num = atoi(msg);
422 } else if(!strncmp(msg, "PARTNAME=", 9)) {
423 msg += 9;
424 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700425 } else if(!strncmp(msg, "DEVNAME=", 8)) {
426 msg += 8;
427 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700428 }
429
Wei Zhongf97b8872012-03-23 14:15:34 -0700430 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700431 while(*msg++)
432 ;
433 }
434
435 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
436 uevent->action, uevent->path, uevent->subsystem,
437 uevent->firmware, uevent->major, uevent->minor);
438}
439
Benoit Gobyd2278632010-08-03 14:36:02 -0700440static char **get_character_device_symlinks(struct uevent *uevent)
441{
442 const char *parent;
443 char *slash;
444 char **links;
445 int link_num = 0;
446 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800447 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700448
Dima Zavinf395c922013-03-06 16:23:57 -0800449 pdev = find_platform_device(uevent->path);
450 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700451 return NULL;
452
453 links = malloc(sizeof(char *) * 2);
454 if (!links)
455 return NULL;
456 memset(links, 0, sizeof(char *) * 2);
457
458 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800459 parent = strchr(uevent->path + pdev->path_len, '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100460 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700461 goto err;
462
463 if (!strncmp(parent, "/usb", 4)) {
464 /* skip root hub name and device. use device interface */
465 while (*++parent && *parent != '/');
466 if (*parent)
467 while (*++parent && *parent != '/');
468 if (!*parent)
469 goto err;
470 slash = strchr(++parent, '/');
471 if (!slash)
472 goto err;
473 width = slash - parent;
474 if (width <= 0)
475 goto err;
476
477 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
478 link_num++;
479 else
480 links[link_num] = NULL;
481 mkdir("/dev/usb", 0755);
482 }
483 else {
484 goto err;
485 }
486
487 return links;
488err:
489 free(links);
490 return NULL;
491}
492
Andrew Boiea885d042013-09-13 17:41:20 -0700493static char **get_block_device_symlinks(struct uevent *uevent)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700494{
Colin Crossfadb85e2011-03-30 18:32:12 -0700495 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800496 struct platform_node *pdev;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700497 char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700498 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700499 char buf[256];
500 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700501 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700502 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700503
Dima Zavinf395c922013-03-06 16:23:57 -0800504 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700505 if (pdev) {
506 device = pdev->name;
507 type = "platform";
508 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
509 device = buf;
510 type = "pci";
511 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800512 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700513 }
Dima Zavinf395c922013-03-06 16:23:57 -0800514
Colin Crossb0ab94b2010-04-08 16:16:20 -0700515 char **links = malloc(sizeof(char *) * 4);
516 if (!links)
517 return NULL;
518 memset(links, 0, sizeof(char *) * 4);
519
Andrew Boiea885d042013-09-13 17:41:20 -0700520 INFO("found %s device %s\n", type, device);
Colin Crossfadb85e2011-03-30 18:32:12 -0700521
Andrew Boiea885d042013-09-13 17:41:20 -0700522 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700523
524 if (uevent->partition_name) {
525 p = strdup(uevent->partition_name);
526 sanitize(p);
Johan Redestig93ca79b2012-04-18 16:41:19 +0200527 if (strcmp(uevent->partition_name, p))
528 NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700529 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
530 link_num++;
531 else
532 links[link_num] = NULL;
533 free(p);
534 }
535
536 if (uevent->partition_num >= 0) {
537 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
538 link_num++;
539 else
540 links[link_num] = NULL;
541 }
542
Dima Zavinf395c922013-03-06 16:23:57 -0800543 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700544 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
545 link_num++;
546 else
547 links[link_num] = NULL;
548
549 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700550}
551
Colin Crosseb5ba832011-03-30 17:37:17 -0700552static void handle_device(const char *action, const char *devpath,
553 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700554{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700555 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700556
Colin Crosseb5ba832011-03-30 17:37:17 -0700557 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400558 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700559 if (links) {
560 for (i = 0; links[i]; i++)
561 make_link(devpath, links[i]);
562 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700563 }
564
Colin Crosseb5ba832011-03-30 17:37:17 -0700565 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700566 if (links) {
567 for (i = 0; links[i]; i++)
568 remove_link(devpath, links[i]);
569 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700570 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700571 }
572
573 if (links) {
574 for (i = 0; links[i]; i++)
575 free(links[i]);
576 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700577 }
578}
579
Colin Crossfadb85e2011-03-30 18:32:12 -0700580static void handle_platform_device_event(struct uevent *uevent)
581{
Dima Zavinf395c922013-03-06 16:23:57 -0800582 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700583
584 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800585 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700586 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800587 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700588}
589
Colin Crosseb5ba832011-03-30 17:37:17 -0700590static const char *parse_device_name(struct uevent *uevent, unsigned int len)
591{
592 const char *name;
593
594 /* if it's not a /dev device, nothing else to do */
595 if((uevent->major < 0) || (uevent->minor < 0))
596 return NULL;
597
598 /* do we have a name? */
599 name = strrchr(uevent->path, '/');
600 if(!name)
601 return NULL;
602 name++;
603
604 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800605 if(strlen(name) > len) {
606 ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n",
607 name, len);
Colin Crosseb5ba832011-03-30 17:37:17 -0700608 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800609 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700610
611 return name;
612}
613
614static void handle_block_device_event(struct uevent *uevent)
615{
616 const char *base = "/dev/block/";
617 const char *name;
618 char devpath[96];
619 char **links = NULL;
620
621 name = parse_device_name(uevent, 64);
622 if (!name)
623 return;
624
625 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500626 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700627
Dima Zavinf395c922013-03-06 16:23:57 -0800628 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700629 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700630
631 handle_device(uevent->action, devpath, uevent->path, 1,
632 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700633}
634
Greg Hackmann3312aa82013-11-18 15:24:40 -0800635#define DEVPATH_LEN 96
636
637static bool assemble_devpath(char *devpath, const char *dirname,
638 const char *devname)
639{
640 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
641 if (s < 0) {
642 ERROR("failed to assemble device path (%s); ignoring event\n",
643 strerror(errno));
644 return false;
645 } else if (s >= DEVPATH_LEN) {
646 ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n",
647 dirname, devname, DEVPATH_LEN);
648 return false;
649 }
650 return true;
651}
652
653static void mkdir_recursive_for_devpath(const char *devpath)
654{
655 char dir[DEVPATH_LEN];
656 char *slash;
657
658 strcpy(dir, devpath);
659 slash = strrchr(dir, '/');
660 *slash = '\0';
661 mkdir_recursive(dir, 0755);
662}
663
Mark Salyzyn154f4602014-02-20 14:59:07 -0800664static inline void __attribute__((__deprecated__)) kernel_logger()
665{
666 INFO("kernel logger is deprecated\n");
667}
668
Colin Crosseb5ba832011-03-30 17:37:17 -0700669static void handle_generic_device_event(struct uevent *uevent)
670{
671 char *base;
672 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800673 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700674 char **links = NULL;
675
676 name = parse_device_name(uevent, 64);
677 if (!name)
678 return;
679
Greg Hackmann3312aa82013-11-18 15:24:40 -0800680 struct ueventd_subsystem *subsystem =
681 ueventd_subsystem_find_by_name(uevent->subsystem);
682
683 if (subsystem) {
684 const char *devname;
685
686 switch (subsystem->devname_src) {
687 case DEVNAME_UEVENT_DEVNAME:
688 devname = uevent->device_name;
689 break;
690
691 case DEVNAME_UEVENT_DEVPATH:
692 devname = name;
693 break;
694
695 default:
696 ERROR("%s subsystem's devpath option is not set; ignoring event\n",
697 uevent->subsystem);
698 return;
699 }
700
701 if (!assemble_devpath(devpath, subsystem->dirname, devname))
702 return;
703 mkdir_recursive_for_devpath(devpath);
704 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700705 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700706 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800707 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800708 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800709 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700710 }
711 else {
712 /* This imitates the file system that would be created
713 * if we were using devfs instead.
714 * Minors are broken up into groups of 128, starting at "001"
715 */
716 int bus_id = uevent->minor / 128 + 1;
717 int device_id = uevent->minor % 128 + 1;
718 /* build directories */
719 make_dir("/dev/bus", 0755);
720 make_dir("/dev/bus/usb", 0755);
721 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
722 make_dir(devpath, 0755);
723 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
724 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700725 } else {
726 /* ignore other USB events */
727 return;
728 }
729 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
730 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500731 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200732 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
733 base = "/dev/dri/";
734 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700735 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
736 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500737 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700738 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
739 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500740 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700741 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
742 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500743 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700744 } else if(!strncmp(uevent->subsystem, "input", 5)) {
745 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500746 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700747 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
748 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500749 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700750 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
751 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500752 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700753 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
754 !strncmp(name, "log_", 4)) {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800755 kernel_logger();
Colin Crosseb5ba832011-03-30 17:37:17 -0700756 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500757 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700758 name += 4;
759 } else
760 base = "/dev/";
761 links = get_character_device_symlinks(uevent);
762
763 if (!devpath[0])
764 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
765
766 handle_device(uevent->action, devpath, uevent->path, 0,
767 uevent->major, uevent->minor, links);
768}
769
770static void handle_device_event(struct uevent *uevent)
771{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700772 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700773 fixup_sys_perms(uevent->path);
774
775 if (!strncmp(uevent->subsystem, "block", 5)) {
776 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700777 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
778 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700779 } else {
780 handle_generic_device_event(uevent);
781 }
782}
783
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700784static int load_firmware(int fw_fd, int loading_fd, int data_fd)
785{
786 struct stat st;
787 long len_to_copy;
788 int ret = 0;
789
790 if(fstat(fw_fd, &st) < 0)
791 return -1;
792 len_to_copy = st.st_size;
793
794 write(loading_fd, "1", 1); /* start transfer */
795
796 while (len_to_copy > 0) {
797 char buf[PAGE_SIZE];
798 ssize_t nr;
799
800 nr = read(fw_fd, buf, sizeof(buf));
801 if(!nr)
802 break;
803 if(nr < 0) {
804 ret = -1;
805 break;
806 }
807
808 len_to_copy -= nr;
809 while (nr > 0) {
810 ssize_t nw = 0;
811
812 nw = write(data_fd, buf + nw, nr);
813 if(nw <= 0) {
814 ret = -1;
815 goto out;
816 }
817 nr -= nw;
818 }
819 }
820
821out:
822 if(!ret)
823 write(loading_fd, "0", 1); /* successful end of transfer */
824 else
825 write(loading_fd, "-1", 2); /* abort transfer */
826
827 return ret;
828}
829
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700830static int is_booting(void)
831{
832 return access("/dev/.booting", F_OK) == 0;
833}
834
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700835static void process_firmware_event(struct uevent *uevent)
836{
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700837 char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700838 int l, loading_fd, data_fd, fw_fd;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700839 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700840
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700841 INFO("firmware: loading '%s' for '%s'\n",
842 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700843
844 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
845 if (l == -1)
846 return;
847
848 l = asprintf(&loading, "%sloading", root);
849 if (l == -1)
850 goto root_free_out;
851
852 l = asprintf(&data, "%sdata", root);
853 if (l == -1)
854 goto loading_free_out;
855
Brian Swetland02863b92010-09-19 03:36:39 -0700856 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
857 if (l == -1)
858 goto data_free_out;
859
860 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700861 if (l == -1)
862 goto data_free_out;
863
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700864 l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware);
865 if (l == -1)
866 goto data_free_out;
867
Nick Kralevich45a884f2015-02-02 14:37:22 -0800868 loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700869 if(loading_fd < 0)
870 goto file_free_out;
871
Nick Kralevich45a884f2015-02-02 14:37:22 -0800872 data_fd = open(data, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700873 if(data_fd < 0)
874 goto loading_close_out;
875
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700876try_loading_again:
Nick Kralevich45a884f2015-02-02 14:37:22 -0800877 fw_fd = open(file1, O_RDONLY|O_CLOEXEC);
Brian Swetland02863b92010-09-19 03:36:39 -0700878 if(fw_fd < 0) {
Nick Kralevich45a884f2015-02-02 14:37:22 -0800879 fw_fd = open(file2, O_RDONLY|O_CLOEXEC);
Benoit Goby609d8822010-11-09 18:10:24 -0800880 if (fw_fd < 0) {
Nick Kralevich45a884f2015-02-02 14:37:22 -0800881 fw_fd = open(file3, O_RDONLY|O_CLOEXEC);
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700882 if (fw_fd < 0) {
883 if (booting) {
884 /* If we're not fully booted, we may be missing
885 * filesystems needed for firmware, wait and retry.
886 */
887 usleep(100000);
888 booting = is_booting();
889 goto try_loading_again;
890 }
891 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
892 write(loading_fd, "-1", 2);
893 goto data_close_out;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700894 }
Benoit Goby609d8822010-11-09 18:10:24 -0800895 }
Brian Swetland02863b92010-09-19 03:36:39 -0700896 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700897
898 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700899 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700900 else
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700901 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700902
903 close(fw_fd);
904data_close_out:
905 close(data_fd);
906loading_close_out:
907 close(loading_fd);
908file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700909 free(file1);
910 free(file2);
Ajay Dudani76c58892013-06-12 15:55:26 -0700911 free(file3);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700912data_free_out:
913 free(data);
914loading_free_out:
915 free(loading);
916root_free_out:
917 free(root);
918}
919
920static void handle_firmware_event(struct uevent *uevent)
921{
922 pid_t pid;
923
924 if(strcmp(uevent->subsystem, "firmware"))
925 return;
926
927 if(strcmp(uevent->action, "add"))
928 return;
929
930 /* we fork, to avoid making large memory allocations in init proper */
931 pid = fork();
932 if (!pid) {
933 process_firmware_event(uevent);
Kenny Root17baff42014-08-20 16:16:44 -0700934 _exit(EXIT_SUCCESS);
935 } else if (pid < 0) {
936 log_event_print("could not fork to process firmware event: %s\n", strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700937 }
938}
939
Ruchi Kandoic6037202014-06-23 11:22:09 -0700940#define UEVENT_MSG_LEN 2048
Colin Cross0dd7ca62010-04-13 19:25:51 -0700941void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700942{
Vernon Tang3f582e92011-04-25 13:08:17 +1000943 char msg[UEVENT_MSG_LEN+2];
944 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700945 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700946 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700947 continue;
948
949 msg[n] = '\0';
950 msg[n+1] = '\0';
951
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700952 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700953 parse_event(msg, &uevent);
954
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400955 if (sehandle && selinux_status_updated() > 0) {
956 struct selabel_handle *sehandle2;
957 sehandle2 = selinux_android_file_context_handle();
958 if (sehandle2) {
959 selabel_close(sehandle);
960 sehandle = sehandle2;
961 }
962 }
963
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700964 handle_device_event(&uevent);
965 handle_firmware_event(&uevent);
966 }
967}
968
969/* Coldboot walks parts of the /sys tree and pokes the uevent files
970** to cause the kernel to regenerate device add events that happened
971** before init's device manager was started
972**
973** We drain any pending events from the netlink socket every time
974** we poke another uevent file to make sure we don't overrun the
975** socket's buffer.
976*/
977
Colin Cross0dd7ca62010-04-13 19:25:51 -0700978static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700979{
980 struct dirent *de;
981 int dfd, fd;
982
983 dfd = dirfd(d);
984
985 fd = openat(dfd, "uevent", O_WRONLY);
986 if(fd >= 0) {
987 write(fd, "add\n", 4);
988 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700989 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700990 }
991
992 while((de = readdir(d))) {
993 DIR *d2;
994
995 if(de->d_type != DT_DIR || de->d_name[0] == '.')
996 continue;
997
998 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
999 if(fd < 0)
1000 continue;
1001
1002 d2 = fdopendir(fd);
1003 if(d2 == 0)
1004 close(fd);
1005 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001006 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001007 closedir(d2);
1008 }
1009 }
1010}
1011
Colin Cross0dd7ca62010-04-13 19:25:51 -07001012static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001013{
1014 DIR *d = opendir(path);
1015 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001016 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001017 closedir(d);
1018 }
1019}
1020
Colin Cross0dd7ca62010-04-13 19:25:51 -07001021void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001022{
1023 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -07001024 struct stat info;
1025 int fd;
Kenny Rootb5982bf2012-10-16 23:07:05 -07001026
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04001027 sehandle = NULL;
1028 if (is_selinux_enabled() > 0) {
1029 sehandle = selinux_android_file_context_handle();
Stephen Smalleye2eb69d2013-04-16 09:30:30 -04001030 selinux_status_open(true);
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04001031 }
Kenny Rootb5982bf2012-10-16 23:07:05 -07001032
Andrew Boied562ca72012-12-04 15:47:20 -08001033 /* is 256K enough? udev uses 16MB! */
1034 device_fd = uevent_open_socket(256*1024, true);
Colin Cross0dd7ca62010-04-13 19:25:51 -07001035 if(device_fd < 0)
1036 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001037
Colin Cross0dd7ca62010-04-13 19:25:51 -07001038 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
1039 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001040
Andreas Gampea016c422014-11-24 19:52:41 -08001041 if (stat(COLDBOOT_DONE, &info) < 0) {
Colin Crossf83d0b92010-04-21 12:04:20 -07001042 t0 = get_usecs();
1043 coldboot("/sys/class");
1044 coldboot("/sys/block");
1045 coldboot("/sys/devices");
1046 t1 = get_usecs();
Nick Kralevich45a884f2015-02-02 14:37:22 -08001047 fd = open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000);
Colin Crossf83d0b92010-04-21 12:04:20 -07001048 close(fd);
1049 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
Andreas Gampea016c422014-11-24 19:52:41 -08001050 // t0 & t1 are unused if the log isn't doing anything.
1051 (void)t0;
1052 (void)t1;
Colin Crossf83d0b92010-04-21 12:04:20 -07001053 } 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}