blob: c30303f4b8adbf5a9620996242f616df192abdc8 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
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>
Olivier Baillyb93e5812010-11-17 11:47:23 -080018#include <stddef.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070019#include <stdio.h>
20#include <stdlib.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23
24#include <fcntl.h>
25#include <dirent.h>
26#include <unistd.h>
27#include <string.h>
28
29#include <sys/socket.h>
30#include <sys/un.h>
31#include <linux/netlink.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050032
33#ifdef HAVE_SELINUX
34#include <selinux/selinux.h>
35#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040036#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050037#endif
38
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070039#include <private/android_filesystem_config.h>
40#include <sys/time.h>
41#include <asm/page.h>
Colin Cross982a8152010-06-03 12:21:01 -070042#include <sys/wait.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070043
Dima Zavinda04c522011-09-01 17:09:44 -070044#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100045#include <cutils/uevent.h>
46
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070047#include "devices.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070048#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070049#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070050
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070051#define SYSFS_PREFIX "/sys"
Brian Swetland02863b92010-09-19 03:36:39 -070052#define FIRMWARE_DIR1 "/etc/firmware"
53#define FIRMWARE_DIR2 "/vendor/firmware"
Iliyan Malchev029d44e2012-06-08 10:42:26 -070054#define FIRMWARE_DIR3 "/firmware/image"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070055
Stephen Smalleye46f9d52012-01-13 08:48:47 -050056#ifdef HAVE_SELINUX
Stephen Smalleye096e362012-06-11 13:37:39 -040057extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050058#endif
59
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;
81};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070082
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070083struct perm_node {
84 struct perms_ dp;
85 struct listnode plist;
86};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070087
Colin Crossfadb85e2011-03-30 18:32:12 -070088struct platform_node {
89 char *name;
90 int name_len;
91 struct listnode list;
92};
93
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070094static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070095static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070096static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070097
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070098int add_dev_perms(const char *name, const char *attr,
99 mode_t perm, unsigned int uid, unsigned int gid,
100 unsigned short prefix) {
101 struct perm_node *node = calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700102 if (!node)
103 return -ENOMEM;
104
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700105 node->dp.name = strdup(name);
106 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700107 return -ENOMEM;
108
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700109 if (attr) {
110 node->dp.attr = strdup(attr);
111 if (!node->dp.attr)
112 return -ENOMEM;
113 }
114
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700115 node->dp.perm = perm;
116 node->dp.uid = uid;
117 node->dp.gid = gid;
118 node->dp.prefix = prefix;
119
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700120 if (attr)
121 list_add_tail(&sys_perms, &node->plist);
122 else
123 list_add_tail(&dev_perms, &node->plist);
124
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700125 return 0;
126}
127
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700128void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700129{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700130 char buf[512];
131 struct listnode *node;
132 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700133
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700134 /* upaths omit the "/sys" that paths in this list
135 * contain, so we add 4 when comparing...
136 */
137 list_for_each(node, &sys_perms) {
138 dp = &(node_to_item(node, struct perm_node, plist))->dp;
139 if (dp->prefix) {
140 if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700141 continue;
142 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700143 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700144 continue;
145 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700146
147 if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
148 return;
149
150 sprintf(buf,"/sys%s/%s", upath, dp->attr);
151 INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
152 chown(buf, dp->uid, dp->gid);
153 chmod(buf, dp->perm);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700154 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700155}
156
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700157static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
158{
159 mode_t perm;
Colin Cross44b65d02010-04-20 14:32:50 -0700160 struct listnode *node;
161 struct perm_node *perm_node;
162 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700163
Colin Cross44b65d02010-04-20 14:32:50 -0700164 /* search the perms list in reverse so that ueventd.$hardware can
165 * override ueventd.rc
166 */
167 list_for_each_reverse(node, &dev_perms) {
168 perm_node = node_to_item(node, struct perm_node, plist);
169 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700170
Colin Cross44b65d02010-04-20 14:32:50 -0700171 if (dp->prefix) {
172 if (strncmp(path, dp->name, strlen(dp->name)))
173 continue;
174 } else {
175 if (strcmp(path, dp->name))
176 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700177 }
Colin Cross44b65d02010-04-20 14:32:50 -0700178 *uid = dp->uid;
179 *gid = dp->gid;
180 return dp->perm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700181 }
Colin Cross44b65d02010-04-20 14:32:50 -0700182 /* Default if nothing found. */
183 *uid = 0;
184 *gid = 0;
185 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700186}
187
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700188static void make_device(const char *path,
189 const char *upath,
190 int block, int major, int minor)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700191{
192 unsigned uid;
193 unsigned gid;
194 mode_t mode;
195 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500196#ifdef HAVE_SELINUX
197 char *secontext = NULL;
198#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700199
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700200 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500201#ifdef HAVE_SELINUX
202 if (sehandle) {
203 selabel_lookup(sehandle, &secontext, path, mode);
204 setfscreatecon(secontext);
205 }
206#endif
Colin Cross17dcc5c2010-09-03 12:25:34 -0700207 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800208 /* Temporarily change egid to avoid race condition setting the gid of the
209 * device node. Unforunately changing the euid would prevent creation of
210 * some device nodes, so the uid has to be set with chown() and is still
211 * racy. Fixing the gid race at least fixed the issue with system_server
212 * opening dynamic input devices under the AID_INPUT gid. */
213 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700214 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800215 chown(path, uid, -1);
216 setegid(AID_ROOT);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500217#ifdef HAVE_SELINUX
218 if (secontext) {
219 freecon(secontext);
220 setfscreatecon(NULL);
221 }
222#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700223}
224
Colin Crossfadb85e2011-03-30 18:32:12 -0700225static void add_platform_device(const char *name)
226{
227 int name_len = strlen(name);
228 struct listnode *node;
229 struct platform_node *bus;
230
231 list_for_each_reverse(node, &platform_names) {
232 bus = node_to_item(node, struct platform_node, list);
233 if ((bus->name_len < name_len) &&
234 (name[bus->name_len] == '/') &&
235 !strncmp(name, bus->name, bus->name_len))
236 /* subdevice of an existing platform, ignore it */
237 return;
238 }
239
240 INFO("adding platform device %s\n", name);
241
242 bus = calloc(1, sizeof(struct platform_node));
243 bus->name = strdup(name);
244 bus->name_len = name_len;
245 list_add_tail(&platform_names, &bus->list);
246}
247
248/*
249 * given a name that may start with a platform device, find the length of the
250 * platform device prefix. If it doesn't start with a platform device, return
251 * 0.
252 */
253static const char *find_platform_device(const char *name)
254{
255 int name_len = strlen(name);
256 struct listnode *node;
257 struct platform_node *bus;
258
259 list_for_each_reverse(node, &platform_names) {
260 bus = node_to_item(node, struct platform_node, list);
261 if ((bus->name_len < name_len) &&
262 (name[bus->name_len] == '/') &&
263 !strncmp(name, bus->name, bus->name_len))
264 return bus->name;
265 }
266
267 return NULL;
268}
269
270static void remove_platform_device(const char *name)
271{
272 struct listnode *node;
273 struct platform_node *bus;
274
275 list_for_each_reverse(node, &platform_names) {
276 bus = node_to_item(node, struct platform_node, list);
277 if (!strcmp(name, bus->name)) {
278 INFO("removing platform device %s\n", name);
279 free(bus->name);
280 list_remove(node);
281 free(bus);
282 return;
283 }
284 }
285}
286
Chuck Tuffli1e070842008-12-15 14:26:56 -0800287#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700288
289static inline suseconds_t get_usecs(void)
290{
291 struct timeval tv;
292 gettimeofday(&tv, 0);
293 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
294}
295
296#define log_event_print(x...) INFO(x)
297
298#else
299
300#define log_event_print(fmt, args...) do { } while (0)
301#define get_usecs() 0
302
303#endif
304
305static void parse_event(const char *msg, struct uevent *uevent)
306{
307 uevent->action = "";
308 uevent->path = "";
309 uevent->subsystem = "";
310 uevent->firmware = "";
311 uevent->major = -1;
312 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700313 uevent->partition_name = NULL;
314 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700315 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700316
317 /* currently ignoring SEQNUM */
318 while(*msg) {
319 if(!strncmp(msg, "ACTION=", 7)) {
320 msg += 7;
321 uevent->action = msg;
322 } else if(!strncmp(msg, "DEVPATH=", 8)) {
323 msg += 8;
324 uevent->path = msg;
325 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
326 msg += 10;
327 uevent->subsystem = msg;
328 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
329 msg += 9;
330 uevent->firmware = msg;
331 } else if(!strncmp(msg, "MAJOR=", 6)) {
332 msg += 6;
333 uevent->major = atoi(msg);
334 } else if(!strncmp(msg, "MINOR=", 6)) {
335 msg += 6;
336 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700337 } else if(!strncmp(msg, "PARTN=", 6)) {
338 msg += 6;
339 uevent->partition_num = atoi(msg);
340 } else if(!strncmp(msg, "PARTNAME=", 9)) {
341 msg += 9;
342 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700343 } else if(!strncmp(msg, "DEVNAME=", 8)) {
344 msg += 8;
345 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700346 }
347
Wei Zhongf97b8872012-03-23 14:15:34 -0700348 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700349 while(*msg++)
350 ;
351 }
352
353 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
354 uevent->action, uevent->path, uevent->subsystem,
355 uevent->firmware, uevent->major, uevent->minor);
356}
357
Benoit Gobyd2278632010-08-03 14:36:02 -0700358static char **get_character_device_symlinks(struct uevent *uevent)
359{
360 const char *parent;
361 char *slash;
362 char **links;
363 int link_num = 0;
364 int width;
365
366 if (strncmp(uevent->path, "/devices/platform/", 18))
367 return NULL;
368
369 links = malloc(sizeof(char *) * 2);
370 if (!links)
371 return NULL;
372 memset(links, 0, sizeof(char *) * 2);
373
374 /* skip "/devices/platform/<driver>" */
375 parent = strchr(uevent->path + 18, '/');
376 if (!*parent)
377 goto err;
378
379 if (!strncmp(parent, "/usb", 4)) {
380 /* skip root hub name and device. use device interface */
381 while (*++parent && *parent != '/');
382 if (*parent)
383 while (*++parent && *parent != '/');
384 if (!*parent)
385 goto err;
386 slash = strchr(++parent, '/');
387 if (!slash)
388 goto err;
389 width = slash - parent;
390 if (width <= 0)
391 goto err;
392
393 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
394 link_num++;
395 else
396 links[link_num] = NULL;
397 mkdir("/dev/usb", 0755);
398 }
399 else {
400 goto err;
401 }
402
403 return links;
404err:
405 free(links);
406 return NULL;
407}
408
Colin Crossb0ab94b2010-04-08 16:16:20 -0700409static char **parse_platform_block_device(struct uevent *uevent)
410{
Colin Crossfadb85e2011-03-30 18:32:12 -0700411 const char *device;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700412 const char *path;
413 char *slash;
414 int width;
415 char buf[256];
416 char link_path[256];
417 int fd;
418 int link_num = 0;
419 int ret;
420 char *p;
421 unsigned int size;
422 struct stat info;
423
424 char **links = malloc(sizeof(char *) * 4);
425 if (!links)
426 return NULL;
427 memset(links, 0, sizeof(char *) * 4);
428
429 /* Drop "/devices/platform/" */
430 path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700431 device = path + 18;
432 device = find_platform_device(device);
433 if (!device)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700434 goto err;
435
Colin Crossfadb85e2011-03-30 18:32:12 -0700436 INFO("found platform device %s\n", device);
437
438 snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700439
440 if (uevent->partition_name) {
441 p = strdup(uevent->partition_name);
442 sanitize(p);
443 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
444 link_num++;
445 else
446 links[link_num] = NULL;
447 free(p);
448 }
449
450 if (uevent->partition_num >= 0) {
451 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
452 link_num++;
453 else
454 links[link_num] = NULL;
455 }
456
457 slash = strrchr(path, '/');
458 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
459 link_num++;
460 else
461 links[link_num] = NULL;
462
463 return links;
464
465err:
466 free(links);
467 return NULL;
468}
469
Colin Crosseb5ba832011-03-30 17:37:17 -0700470static void handle_device(const char *action, const char *devpath,
471 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700472{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700473 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700474
Colin Crosseb5ba832011-03-30 17:37:17 -0700475 if(!strcmp(action, "add")) {
476 make_device(devpath, path, block, major, minor);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700477 if (links) {
478 for (i = 0; links[i]; i++)
479 make_link(devpath, links[i]);
480 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700481 }
482
Colin Crosseb5ba832011-03-30 17:37:17 -0700483 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700484 if (links) {
485 for (i = 0; links[i]; i++)
486 remove_link(devpath, links[i]);
487 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700488 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700489 }
490
491 if (links) {
492 for (i = 0; links[i]; i++)
493 free(links[i]);
494 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700495 }
496}
497
Colin Crossfadb85e2011-03-30 18:32:12 -0700498static void handle_platform_device_event(struct uevent *uevent)
499{
500 const char *name = uevent->path + 18; /* length of /devices/platform/ */
501
502 if (!strcmp(uevent->action, "add"))
503 add_platform_device(name);
504 else if (!strcmp(uevent->action, "remove"))
505 remove_platform_device(name);
506}
507
Colin Crosseb5ba832011-03-30 17:37:17 -0700508static const char *parse_device_name(struct uevent *uevent, unsigned int len)
509{
510 const char *name;
511
512 /* if it's not a /dev device, nothing else to do */
513 if((uevent->major < 0) || (uevent->minor < 0))
514 return NULL;
515
516 /* do we have a name? */
517 name = strrchr(uevent->path, '/');
518 if(!name)
519 return NULL;
520 name++;
521
522 /* too-long names would overrun our buffer */
523 if(strlen(name) > len)
524 return NULL;
525
526 return name;
527}
528
529static void handle_block_device_event(struct uevent *uevent)
530{
531 const char *base = "/dev/block/";
532 const char *name;
533 char devpath[96];
534 char **links = NULL;
535
536 name = parse_device_name(uevent, 64);
537 if (!name)
538 return;
539
540 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500541 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700542
543 if (!strncmp(uevent->path, "/devices/platform/", 18))
544 links = parse_platform_block_device(uevent);
545
546 handle_device(uevent->action, devpath, uevent->path, 1,
547 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700548}
549
550static void handle_generic_device_event(struct uevent *uevent)
551{
552 char *base;
553 const char *name;
554 char devpath[96] = {0};
555 char **links = NULL;
556
557 name = parse_device_name(uevent, 64);
558 if (!name)
559 return;
560
561 if (!strncmp(uevent->subsystem, "usb", 3)) {
562 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700563 if (uevent->device_name) {
564 /*
565 * create device node provided by kernel if present
566 * see drivers/base/core.c
567 */
568 char *p = devpath;
569 snprintf(devpath, sizeof(devpath), "/dev/%s", uevent->device_name);
570 /* skip leading /dev/ */
571 p += 5;
572 /* build directories */
573 while (*p) {
574 if (*p == '/') {
575 *p = 0;
576 make_dir(devpath, 0755);
577 *p = '/';
578 }
579 p++;
580 }
581 }
582 else {
583 /* This imitates the file system that would be created
584 * if we were using devfs instead.
585 * Minors are broken up into groups of 128, starting at "001"
586 */
587 int bus_id = uevent->minor / 128 + 1;
588 int device_id = uevent->minor % 128 + 1;
589 /* build directories */
590 make_dir("/dev/bus", 0755);
591 make_dir("/dev/bus/usb", 0755);
592 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
593 make_dir(devpath, 0755);
594 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
595 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700596 } else {
597 /* ignore other USB events */
598 return;
599 }
600 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
601 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500602 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200603 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
604 base = "/dev/dri/";
605 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700606 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
607 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500608 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700609 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
610 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500611 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700612 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
613 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500614 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700615 } else if(!strncmp(uevent->subsystem, "input", 5)) {
616 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500617 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700618 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
619 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500620 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700621 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
622 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500623 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700624 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
625 !strncmp(name, "log_", 4)) {
626 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500627 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700628 name += 4;
629 } else
630 base = "/dev/";
631 links = get_character_device_symlinks(uevent);
632
633 if (!devpath[0])
634 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
635
636 handle_device(uevent->action, devpath, uevent->path, 0,
637 uevent->major, uevent->minor, links);
638}
639
640static void handle_device_event(struct uevent *uevent)
641{
Colin Cross308bc522012-07-23 16:31:28 -0700642 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700643 fixup_sys_perms(uevent->path);
644
645 if (!strncmp(uevent->subsystem, "block", 5)) {
646 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700647 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
648 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700649 } else {
650 handle_generic_device_event(uevent);
651 }
652}
653
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700654static int load_firmware(int fw_fd, int loading_fd, int data_fd)
655{
656 struct stat st;
657 long len_to_copy;
658 int ret = 0;
659
660 if(fstat(fw_fd, &st) < 0)
661 return -1;
662 len_to_copy = st.st_size;
663
664 write(loading_fd, "1", 1); /* start transfer */
665
666 while (len_to_copy > 0) {
667 char buf[PAGE_SIZE];
668 ssize_t nr;
669
670 nr = read(fw_fd, buf, sizeof(buf));
671 if(!nr)
672 break;
673 if(nr < 0) {
674 ret = -1;
675 break;
676 }
677
678 len_to_copy -= nr;
679 while (nr > 0) {
680 ssize_t nw = 0;
681
682 nw = write(data_fd, buf + nw, nr);
683 if(nw <= 0) {
684 ret = -1;
685 goto out;
686 }
687 nr -= nw;
688 }
689 }
690
691out:
692 if(!ret)
693 write(loading_fd, "0", 1); /* successful end of transfer */
694 else
695 write(loading_fd, "-1", 2); /* abort transfer */
696
697 return ret;
698}
699
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700700static int is_booting(void)
701{
702 return access("/dev/.booting", F_OK) == 0;
703}
704
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700705static void process_firmware_event(struct uevent *uevent)
706{
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700707 char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700708 int l, loading_fd, data_fd, fw_fd;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700709 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700710
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700711 INFO("firmware: loading '%s' for '%s'\n",
712 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700713
714 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
715 if (l == -1)
716 return;
717
718 l = asprintf(&loading, "%sloading", root);
719 if (l == -1)
720 goto root_free_out;
721
722 l = asprintf(&data, "%sdata", root);
723 if (l == -1)
724 goto loading_free_out;
725
Brian Swetland02863b92010-09-19 03:36:39 -0700726 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
727 if (l == -1)
728 goto data_free_out;
729
730 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700731 if (l == -1)
732 goto data_free_out;
733
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700734 l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware);
735 if (l == -1)
736 goto data_free_out;
737
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700738 loading_fd = open(loading, O_WRONLY);
739 if(loading_fd < 0)
740 goto file_free_out;
741
742 data_fd = open(data, O_WRONLY);
743 if(data_fd < 0)
744 goto loading_close_out;
745
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700746try_loading_again:
Brian Swetland02863b92010-09-19 03:36:39 -0700747 fw_fd = open(file1, O_RDONLY);
748 if(fw_fd < 0) {
749 fw_fd = open(file2, O_RDONLY);
Benoit Goby609d8822010-11-09 18:10:24 -0800750 if (fw_fd < 0) {
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700751 fw_fd = open(file3, O_RDONLY);
752 if (fw_fd < 0) {
753 if (booting) {
754 /* If we're not fully booted, we may be missing
755 * filesystems needed for firmware, wait and retry.
756 */
757 usleep(100000);
758 booting = is_booting();
759 goto try_loading_again;
760 }
761 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
762 write(loading_fd, "-1", 2);
763 goto data_close_out;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700764 }
Benoit Goby609d8822010-11-09 18:10:24 -0800765 }
Brian Swetland02863b92010-09-19 03:36:39 -0700766 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700767
768 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700769 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700770 else
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700771 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700772
773 close(fw_fd);
774data_close_out:
775 close(data_fd);
776loading_close_out:
777 close(loading_fd);
778file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700779 free(file1);
780 free(file2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700781data_free_out:
782 free(data);
783loading_free_out:
784 free(loading);
785root_free_out:
786 free(root);
787}
788
789static void handle_firmware_event(struct uevent *uevent)
790{
791 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700792 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700793
794 if(strcmp(uevent->subsystem, "firmware"))
795 return;
796
797 if(strcmp(uevent->action, "add"))
798 return;
799
800 /* we fork, to avoid making large memory allocations in init proper */
801 pid = fork();
802 if (!pid) {
803 process_firmware_event(uevent);
804 exit(EXIT_SUCCESS);
805 }
806}
807
808#define UEVENT_MSG_LEN 1024
Colin Cross0dd7ca62010-04-13 19:25:51 -0700809void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700810{
Vernon Tang3f582e92011-04-25 13:08:17 +1000811 char msg[UEVENT_MSG_LEN+2];
812 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700813 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700814 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700815 continue;
816
817 msg[n] = '\0';
818 msg[n+1] = '\0';
819
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700820 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700821 parse_event(msg, &uevent);
822
823 handle_device_event(&uevent);
824 handle_firmware_event(&uevent);
825 }
826}
827
828/* Coldboot walks parts of the /sys tree and pokes the uevent files
829** to cause the kernel to regenerate device add events that happened
830** before init's device manager was started
831**
832** We drain any pending events from the netlink socket every time
833** we poke another uevent file to make sure we don't overrun the
834** socket's buffer.
835*/
836
Colin Cross0dd7ca62010-04-13 19:25:51 -0700837static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700838{
839 struct dirent *de;
840 int dfd, fd;
841
842 dfd = dirfd(d);
843
844 fd = openat(dfd, "uevent", O_WRONLY);
845 if(fd >= 0) {
846 write(fd, "add\n", 4);
847 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700848 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700849 }
850
851 while((de = readdir(d))) {
852 DIR *d2;
853
854 if(de->d_type != DT_DIR || de->d_name[0] == '.')
855 continue;
856
857 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
858 if(fd < 0)
859 continue;
860
861 d2 = fdopendir(fd);
862 if(d2 == 0)
863 close(fd);
864 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700865 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700866 closedir(d2);
867 }
868 }
869}
870
Colin Cross0dd7ca62010-04-13 19:25:51 -0700871static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700872{
873 DIR *d = opendir(path);
874 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700875 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700876 closedir(d);
877 }
878}
879
Colin Cross0dd7ca62010-04-13 19:25:51 -0700880void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700881{
882 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -0700883 struct stat info;
884 int fd;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500885#ifdef HAVE_SELINUX
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400886 sehandle = NULL;
887 if (is_selinux_enabled() > 0) {
888 sehandle = selinux_android_file_context_handle();
889 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500890#endif
Dima Zavin2d55e022011-08-31 18:07:36 -0700891 /* is 64K enough? udev uses 16MB! */
892 device_fd = uevent_open_socket(64*1024, true);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700893 if(device_fd < 0)
894 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700895
Colin Cross0dd7ca62010-04-13 19:25:51 -0700896 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
897 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700898
Colin Crossf83d0b92010-04-21 12:04:20 -0700899 if (stat(coldboot_done, &info) < 0) {
900 t0 = get_usecs();
901 coldboot("/sys/class");
902 coldboot("/sys/block");
903 coldboot("/sys/devices");
904 t1 = get_usecs();
905 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
906 close(fd);
907 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
908 } else {
909 log_event_print("skipping coldboot, already done\n");
910 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700911}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700912
Colin Cross0dd7ca62010-04-13 19:25:51 -0700913int get_device_fd()
914{
915 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700916}