blob: 2644623b8909281d24ffa9e32ba88c1ef8a36d26 [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
Stephen Smalleye46f9d52012-01-13 08:48:47 -050033#include <selinux/selinux.h>
34#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040035#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050036
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070037#include <private/android_filesystem_config.h>
38#include <sys/time.h>
39#include <asm/page.h>
Colin Cross982a8152010-06-03 12:21:01 -070040#include <sys/wait.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070041
Dima Zavinda04c522011-09-01 17:09:44 -070042#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100043#include <cutils/uevent.h>
44
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070045#include "devices.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070046#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070047#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070048
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070049#define SYSFS_PREFIX "/sys"
Brian Swetland02863b92010-09-19 03:36:39 -070050#define FIRMWARE_DIR1 "/etc/firmware"
51#define FIRMWARE_DIR2 "/vendor/firmware"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070052
Stephen Smalleye096e362012-06-11 13:37:39 -040053extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050054
Colin Cross0dd7ca62010-04-13 19:25:51 -070055static int device_fd = -1;
56
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070057struct uevent {
58 const char *action;
59 const char *path;
60 const char *subsystem;
61 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070062 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070063 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070064 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070065 int major;
66 int minor;
67};
68
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069struct perms_ {
70 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070071 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072 mode_t perm;
73 unsigned int uid;
74 unsigned int gid;
75 unsigned short prefix;
76};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070077
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070078struct perm_node {
79 struct perms_ dp;
80 struct listnode plist;
81};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070082
Colin Crossfadb85e2011-03-30 18:32:12 -070083struct platform_node {
84 char *name;
85 int name_len;
86 struct listnode list;
87};
88
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070089static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070090static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070091static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070092
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070093int add_dev_perms(const char *name, const char *attr,
94 mode_t perm, unsigned int uid, unsigned int gid,
95 unsigned short prefix) {
96 struct perm_node *node = calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070097 if (!node)
98 return -ENOMEM;
99
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700100 node->dp.name = strdup(name);
101 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700102 return -ENOMEM;
103
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700104 if (attr) {
105 node->dp.attr = strdup(attr);
106 if (!node->dp.attr)
107 return -ENOMEM;
108 }
109
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700110 node->dp.perm = perm;
111 node->dp.uid = uid;
112 node->dp.gid = gid;
113 node->dp.prefix = prefix;
114
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700115 if (attr)
116 list_add_tail(&sys_perms, &node->plist);
117 else
118 list_add_tail(&dev_perms, &node->plist);
119
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700120 return 0;
121}
122
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700123void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700124{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700125 char buf[512];
126 struct listnode *node;
127 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700128
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700129 /* upaths omit the "/sys" that paths in this list
130 * contain, so we add 4 when comparing...
131 */
132 list_for_each(node, &sys_perms) {
133 dp = &(node_to_item(node, struct perm_node, plist))->dp;
134 if (dp->prefix) {
135 if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700136 continue;
137 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700138 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700139 continue;
140 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700141
142 if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
143 return;
144
145 sprintf(buf,"/sys%s/%s", upath, dp->attr);
146 INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
147 chown(buf, dp->uid, dp->gid);
148 chmod(buf, dp->perm);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700149 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700150}
151
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700152static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
153{
154 mode_t perm;
Colin Cross44b65d02010-04-20 14:32:50 -0700155 struct listnode *node;
156 struct perm_node *perm_node;
157 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700158
Colin Cross44b65d02010-04-20 14:32:50 -0700159 /* search the perms list in reverse so that ueventd.$hardware can
160 * override ueventd.rc
161 */
162 list_for_each_reverse(node, &dev_perms) {
163 perm_node = node_to_item(node, struct perm_node, plist);
164 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700165
Colin Cross44b65d02010-04-20 14:32:50 -0700166 if (dp->prefix) {
167 if (strncmp(path, dp->name, strlen(dp->name)))
168 continue;
169 } else {
170 if (strcmp(path, dp->name))
171 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700172 }
Colin Cross44b65d02010-04-20 14:32:50 -0700173 *uid = dp->uid;
174 *gid = dp->gid;
175 return dp->perm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700176 }
Colin Cross44b65d02010-04-20 14:32:50 -0700177 /* Default if nothing found. */
178 *uid = 0;
179 *gid = 0;
180 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700181}
182
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700183static void make_device(const char *path,
184 const char *upath,
185 int block, int major, int minor)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700186{
187 unsigned uid;
188 unsigned gid;
189 mode_t mode;
190 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500191 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700192
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700193 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700194
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500195 if (sehandle) {
196 selabel_lookup(sehandle, &secontext, path, mode);
197 setfscreatecon(secontext);
198 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700199
Colin Cross17dcc5c2010-09-03 12:25:34 -0700200 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800201 /* Temporarily change egid to avoid race condition setting the gid of the
202 * device node. Unforunately changing the euid would prevent creation of
203 * some device nodes, so the uid has to be set with chown() and is still
204 * racy. Fixing the gid race at least fixed the issue with system_server
205 * opening dynamic input devices under the AID_INPUT gid. */
206 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700207 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800208 chown(path, uid, -1);
209 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700210
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500211 if (secontext) {
212 freecon(secontext);
213 setfscreatecon(NULL);
214 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700215}
216
Colin Crossfadb85e2011-03-30 18:32:12 -0700217static void add_platform_device(const char *name)
218{
219 int name_len = strlen(name);
220 struct listnode *node;
221 struct platform_node *bus;
222
223 list_for_each_reverse(node, &platform_names) {
224 bus = node_to_item(node, struct platform_node, list);
225 if ((bus->name_len < name_len) &&
226 (name[bus->name_len] == '/') &&
227 !strncmp(name, bus->name, bus->name_len))
228 /* subdevice of an existing platform, ignore it */
229 return;
230 }
231
232 INFO("adding platform device %s\n", name);
233
234 bus = calloc(1, sizeof(struct platform_node));
235 bus->name = strdup(name);
236 bus->name_len = name_len;
237 list_add_tail(&platform_names, &bus->list);
238}
239
240/*
241 * given a name that may start with a platform device, find the length of the
242 * platform device prefix. If it doesn't start with a platform device, return
243 * 0.
244 */
245static const char *find_platform_device(const char *name)
246{
247 int name_len = strlen(name);
248 struct listnode *node;
249 struct platform_node *bus;
250
251 list_for_each_reverse(node, &platform_names) {
252 bus = node_to_item(node, struct platform_node, list);
253 if ((bus->name_len < name_len) &&
254 (name[bus->name_len] == '/') &&
255 !strncmp(name, bus->name, bus->name_len))
256 return bus->name;
257 }
258
259 return NULL;
260}
261
262static void remove_platform_device(const char *name)
263{
264 struct listnode *node;
265 struct platform_node *bus;
266
267 list_for_each_reverse(node, &platform_names) {
268 bus = node_to_item(node, struct platform_node, list);
269 if (!strcmp(name, bus->name)) {
270 INFO("removing platform device %s\n", name);
271 free(bus->name);
272 list_remove(node);
273 free(bus);
274 return;
275 }
276 }
277}
278
Chuck Tuffli1e070842008-12-15 14:26:56 -0800279#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700280
281static inline suseconds_t get_usecs(void)
282{
283 struct timeval tv;
284 gettimeofday(&tv, 0);
285 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
286}
287
288#define log_event_print(x...) INFO(x)
289
290#else
291
292#define log_event_print(fmt, args...) do { } while (0)
293#define get_usecs() 0
294
295#endif
296
297static void parse_event(const char *msg, struct uevent *uevent)
298{
299 uevent->action = "";
300 uevent->path = "";
301 uevent->subsystem = "";
302 uevent->firmware = "";
303 uevent->major = -1;
304 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700305 uevent->partition_name = NULL;
306 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700307 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700308
309 /* currently ignoring SEQNUM */
310 while(*msg) {
311 if(!strncmp(msg, "ACTION=", 7)) {
312 msg += 7;
313 uevent->action = msg;
314 } else if(!strncmp(msg, "DEVPATH=", 8)) {
315 msg += 8;
316 uevent->path = msg;
317 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
318 msg += 10;
319 uevent->subsystem = msg;
320 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
321 msg += 9;
322 uevent->firmware = msg;
323 } else if(!strncmp(msg, "MAJOR=", 6)) {
324 msg += 6;
325 uevent->major = atoi(msg);
326 } else if(!strncmp(msg, "MINOR=", 6)) {
327 msg += 6;
328 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700329 } else if(!strncmp(msg, "PARTN=", 6)) {
330 msg += 6;
331 uevent->partition_num = atoi(msg);
332 } else if(!strncmp(msg, "PARTNAME=", 9)) {
333 msg += 9;
334 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700335 } else if(!strncmp(msg, "DEVNAME=", 8)) {
336 msg += 8;
337 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700338 }
339
Wei Zhongf97b8872012-03-23 14:15:34 -0700340 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700341 while(*msg++)
342 ;
343 }
344
345 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
346 uevent->action, uevent->path, uevent->subsystem,
347 uevent->firmware, uevent->major, uevent->minor);
348}
349
Benoit Gobyd2278632010-08-03 14:36:02 -0700350static char **get_character_device_symlinks(struct uevent *uevent)
351{
352 const char *parent;
353 char *slash;
354 char **links;
355 int link_num = 0;
356 int width;
357
358 if (strncmp(uevent->path, "/devices/platform/", 18))
359 return NULL;
360
361 links = malloc(sizeof(char *) * 2);
362 if (!links)
363 return NULL;
364 memset(links, 0, sizeof(char *) * 2);
365
366 /* skip "/devices/platform/<driver>" */
367 parent = strchr(uevent->path + 18, '/');
368 if (!*parent)
369 goto err;
370
371 if (!strncmp(parent, "/usb", 4)) {
372 /* skip root hub name and device. use device interface */
373 while (*++parent && *parent != '/');
374 if (*parent)
375 while (*++parent && *parent != '/');
376 if (!*parent)
377 goto err;
378 slash = strchr(++parent, '/');
379 if (!slash)
380 goto err;
381 width = slash - parent;
382 if (width <= 0)
383 goto err;
384
385 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
386 link_num++;
387 else
388 links[link_num] = NULL;
389 mkdir("/dev/usb", 0755);
390 }
391 else {
392 goto err;
393 }
394
395 return links;
396err:
397 free(links);
398 return NULL;
399}
400
Colin Crossb0ab94b2010-04-08 16:16:20 -0700401static char **parse_platform_block_device(struct uevent *uevent)
402{
Colin Crossfadb85e2011-03-30 18:32:12 -0700403 const char *device;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700404 const char *path;
405 char *slash;
406 int width;
407 char buf[256];
408 char link_path[256];
409 int fd;
410 int link_num = 0;
411 int ret;
412 char *p;
413 unsigned int size;
414 struct stat info;
415
416 char **links = malloc(sizeof(char *) * 4);
417 if (!links)
418 return NULL;
419 memset(links, 0, sizeof(char *) * 4);
420
421 /* Drop "/devices/platform/" */
422 path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700423 device = path + 18;
424 device = find_platform_device(device);
425 if (!device)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700426 goto err;
427
Colin Crossfadb85e2011-03-30 18:32:12 -0700428 INFO("found platform device %s\n", device);
429
430 snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700431
432 if (uevent->partition_name) {
433 p = strdup(uevent->partition_name);
434 sanitize(p);
435 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
436 link_num++;
437 else
438 links[link_num] = NULL;
439 free(p);
440 }
441
442 if (uevent->partition_num >= 0) {
443 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
444 link_num++;
445 else
446 links[link_num] = NULL;
447 }
448
449 slash = strrchr(path, '/');
450 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
451 link_num++;
452 else
453 links[link_num] = NULL;
454
455 return links;
456
457err:
458 free(links);
459 return NULL;
460}
461
Colin Crosseb5ba832011-03-30 17:37:17 -0700462static void handle_device(const char *action, const char *devpath,
463 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700464{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700465 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700466
Colin Crosseb5ba832011-03-30 17:37:17 -0700467 if(!strcmp(action, "add")) {
468 make_device(devpath, path, block, major, minor);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700469 if (links) {
470 for (i = 0; links[i]; i++)
471 make_link(devpath, links[i]);
472 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700473 }
474
Colin Crosseb5ba832011-03-30 17:37:17 -0700475 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700476 if (links) {
477 for (i = 0; links[i]; i++)
478 remove_link(devpath, links[i]);
479 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700480 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700481 }
482
483 if (links) {
484 for (i = 0; links[i]; i++)
485 free(links[i]);
486 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700487 }
488}
489
Colin Crossfadb85e2011-03-30 18:32:12 -0700490static void handle_platform_device_event(struct uevent *uevent)
491{
492 const char *name = uevent->path + 18; /* length of /devices/platform/ */
493
494 if (!strcmp(uevent->action, "add"))
495 add_platform_device(name);
496 else if (!strcmp(uevent->action, "remove"))
497 remove_platform_device(name);
498}
499
Colin Crosseb5ba832011-03-30 17:37:17 -0700500static const char *parse_device_name(struct uevent *uevent, unsigned int len)
501{
502 const char *name;
503
504 /* if it's not a /dev device, nothing else to do */
505 if((uevent->major < 0) || (uevent->minor < 0))
506 return NULL;
507
508 /* do we have a name? */
509 name = strrchr(uevent->path, '/');
510 if(!name)
511 return NULL;
512 name++;
513
514 /* too-long names would overrun our buffer */
515 if(strlen(name) > len)
516 return NULL;
517
518 return name;
519}
520
521static void handle_block_device_event(struct uevent *uevent)
522{
523 const char *base = "/dev/block/";
524 const char *name;
525 char devpath[96];
526 char **links = NULL;
527
528 name = parse_device_name(uevent, 64);
529 if (!name)
530 return;
531
532 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500533 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700534
535 if (!strncmp(uevent->path, "/devices/platform/", 18))
536 links = parse_platform_block_device(uevent);
537
538 handle_device(uevent->action, devpath, uevent->path, 1,
539 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700540}
541
542static void handle_generic_device_event(struct uevent *uevent)
543{
544 char *base;
545 const char *name;
546 char devpath[96] = {0};
547 char **links = NULL;
548
549 name = parse_device_name(uevent, 64);
550 if (!name)
551 return;
552
553 if (!strncmp(uevent->subsystem, "usb", 3)) {
554 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700555 if (uevent->device_name) {
556 /*
557 * create device node provided by kernel if present
558 * see drivers/base/core.c
559 */
560 char *p = devpath;
561 snprintf(devpath, sizeof(devpath), "/dev/%s", uevent->device_name);
562 /* skip leading /dev/ */
563 p += 5;
564 /* build directories */
565 while (*p) {
566 if (*p == '/') {
567 *p = 0;
568 make_dir(devpath, 0755);
569 *p = '/';
570 }
571 p++;
572 }
573 }
574 else {
575 /* This imitates the file system that would be created
576 * if we were using devfs instead.
577 * Minors are broken up into groups of 128, starting at "001"
578 */
579 int bus_id = uevent->minor / 128 + 1;
580 int device_id = uevent->minor % 128 + 1;
581 /* build directories */
582 make_dir("/dev/bus", 0755);
583 make_dir("/dev/bus/usb", 0755);
584 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
585 make_dir(devpath, 0755);
586 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
587 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700588 } else {
589 /* ignore other USB events */
590 return;
591 }
592 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
593 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500594 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200595 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
596 base = "/dev/dri/";
597 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700598 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
599 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500600 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700601 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
602 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500603 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700604 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
605 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500606 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700607 } else if(!strncmp(uevent->subsystem, "input", 5)) {
608 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500609 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700610 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
611 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500612 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700613 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
614 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500615 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700616 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
617 !strncmp(name, "log_", 4)) {
618 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500619 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700620 name += 4;
621 } else
622 base = "/dev/";
623 links = get_character_device_symlinks(uevent);
624
625 if (!devpath[0])
626 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
627
628 handle_device(uevent->action, devpath, uevent->path, 0,
629 uevent->major, uevent->minor, links);
630}
631
632static void handle_device_event(struct uevent *uevent)
633{
634 if (!strcmp(uevent->action,"add"))
635 fixup_sys_perms(uevent->path);
636
637 if (!strncmp(uevent->subsystem, "block", 5)) {
638 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700639 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
640 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700641 } else {
642 handle_generic_device_event(uevent);
643 }
644}
645
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700646static int load_firmware(int fw_fd, int loading_fd, int data_fd)
647{
648 struct stat st;
649 long len_to_copy;
650 int ret = 0;
651
652 if(fstat(fw_fd, &st) < 0)
653 return -1;
654 len_to_copy = st.st_size;
655
656 write(loading_fd, "1", 1); /* start transfer */
657
658 while (len_to_copy > 0) {
659 char buf[PAGE_SIZE];
660 ssize_t nr;
661
662 nr = read(fw_fd, buf, sizeof(buf));
663 if(!nr)
664 break;
665 if(nr < 0) {
666 ret = -1;
667 break;
668 }
669
670 len_to_copy -= nr;
671 while (nr > 0) {
672 ssize_t nw = 0;
673
674 nw = write(data_fd, buf + nw, nr);
675 if(nw <= 0) {
676 ret = -1;
677 goto out;
678 }
679 nr -= nw;
680 }
681 }
682
683out:
684 if(!ret)
685 write(loading_fd, "0", 1); /* successful end of transfer */
686 else
687 write(loading_fd, "-1", 2); /* abort transfer */
688
689 return ret;
690}
691
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700692static int is_booting(void)
693{
694 return access("/dev/.booting", F_OK) == 0;
695}
696
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700697static void process_firmware_event(struct uevent *uevent)
698{
Brian Swetland02863b92010-09-19 03:36:39 -0700699 char *root, *loading, *data, *file1 = NULL, *file2 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700700 int l, loading_fd, data_fd, fw_fd;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700701 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700702
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700703 INFO("firmware: loading '%s' for '%s'\n",
704 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700705
706 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
707 if (l == -1)
708 return;
709
710 l = asprintf(&loading, "%sloading", root);
711 if (l == -1)
712 goto root_free_out;
713
714 l = asprintf(&data, "%sdata", root);
715 if (l == -1)
716 goto loading_free_out;
717
Brian Swetland02863b92010-09-19 03:36:39 -0700718 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
719 if (l == -1)
720 goto data_free_out;
721
722 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700723 if (l == -1)
724 goto data_free_out;
725
726 loading_fd = open(loading, O_WRONLY);
727 if(loading_fd < 0)
728 goto file_free_out;
729
730 data_fd = open(data, O_WRONLY);
731 if(data_fd < 0)
732 goto loading_close_out;
733
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700734try_loading_again:
Brian Swetland02863b92010-09-19 03:36:39 -0700735 fw_fd = open(file1, O_RDONLY);
736 if(fw_fd < 0) {
737 fw_fd = open(file2, O_RDONLY);
Benoit Goby609d8822010-11-09 18:10:24 -0800738 if (fw_fd < 0) {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700739 if (booting) {
740 /* If we're not fully booted, we may be missing
741 * filesystems needed for firmware, wait and retry.
742 */
743 usleep(100000);
744 booting = is_booting();
745 goto try_loading_again;
746 }
747 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
Benoit Goby609d8822010-11-09 18:10:24 -0800748 write(loading_fd, "-1", 2);
Brian Swetland02863b92010-09-19 03:36:39 -0700749 goto data_close_out;
Benoit Goby609d8822010-11-09 18:10:24 -0800750 }
Brian Swetland02863b92010-09-19 03:36:39 -0700751 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700752
753 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700754 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700755 else
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700756 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700757
758 close(fw_fd);
759data_close_out:
760 close(data_fd);
761loading_close_out:
762 close(loading_fd);
763file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700764 free(file1);
765 free(file2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700766data_free_out:
767 free(data);
768loading_free_out:
769 free(loading);
770root_free_out:
771 free(root);
772}
773
774static void handle_firmware_event(struct uevent *uevent)
775{
776 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700777 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700778
779 if(strcmp(uevent->subsystem, "firmware"))
780 return;
781
782 if(strcmp(uevent->action, "add"))
783 return;
784
785 /* we fork, to avoid making large memory allocations in init proper */
786 pid = fork();
787 if (!pid) {
788 process_firmware_event(uevent);
789 exit(EXIT_SUCCESS);
790 }
791}
792
793#define UEVENT_MSG_LEN 1024
Colin Cross0dd7ca62010-04-13 19:25:51 -0700794void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700795{
Vernon Tang3f582e92011-04-25 13:08:17 +1000796 char msg[UEVENT_MSG_LEN+2];
797 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700798 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700799 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700800 continue;
801
802 msg[n] = '\0';
803 msg[n+1] = '\0';
804
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700805 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700806 parse_event(msg, &uevent);
807
808 handle_device_event(&uevent);
809 handle_firmware_event(&uevent);
810 }
811}
812
813/* Coldboot walks parts of the /sys tree and pokes the uevent files
814** to cause the kernel to regenerate device add events that happened
815** before init's device manager was started
816**
817** We drain any pending events from the netlink socket every time
818** we poke another uevent file to make sure we don't overrun the
819** socket's buffer.
820*/
821
Colin Cross0dd7ca62010-04-13 19:25:51 -0700822static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700823{
824 struct dirent *de;
825 int dfd, fd;
826
827 dfd = dirfd(d);
828
829 fd = openat(dfd, "uevent", O_WRONLY);
830 if(fd >= 0) {
831 write(fd, "add\n", 4);
832 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700833 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700834 }
835
836 while((de = readdir(d))) {
837 DIR *d2;
838
839 if(de->d_type != DT_DIR || de->d_name[0] == '.')
840 continue;
841
842 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
843 if(fd < 0)
844 continue;
845
846 d2 = fdopendir(fd);
847 if(d2 == 0)
848 close(fd);
849 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700850 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700851 closedir(d2);
852 }
853 }
854}
855
Colin Cross0dd7ca62010-04-13 19:25:51 -0700856static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700857{
858 DIR *d = opendir(path);
859 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700860 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700861 closedir(d);
862 }
863}
864
Colin Cross0dd7ca62010-04-13 19:25:51 -0700865void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700866{
867 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -0700868 struct stat info;
869 int fd;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700870
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400871 sehandle = NULL;
872 if (is_selinux_enabled() > 0) {
873 sehandle = selinux_android_file_context_handle();
874 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700875
Dima Zavin2d55e022011-08-31 18:07:36 -0700876 /* is 64K enough? udev uses 16MB! */
877 device_fd = uevent_open_socket(64*1024, true);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700878 if(device_fd < 0)
879 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700880
Colin Cross0dd7ca62010-04-13 19:25:51 -0700881 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
882 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700883
Colin Crossf83d0b92010-04-21 12:04:20 -0700884 if (stat(coldboot_done, &info) < 0) {
885 t0 = get_usecs();
886 coldboot("/sys/class");
887 coldboot("/sys/block");
888 coldboot("/sys/devices");
889 t1 = get_usecs();
890 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
891 close(fd);
892 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
893 } else {
894 log_event_print("skipping coldboot, already done\n");
895 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700896}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700897
Colin Cross0dd7ca62010-04-13 19:25:51 -0700898int get_device_fd()
899{
900 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700901}