blob: f7df4534089b41f38042d37d5428e550b589f763 [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 Smalleye2eb69d2013-04-16 09:30:30 -040036#include <selinux/avc.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050037
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070038#include <private/android_filesystem_config.h>
39#include <sys/time.h>
40#include <asm/page.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
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 Smalleye096e362012-06-11 13:37:39 -040056extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050057
Colin Cross0dd7ca62010-04-13 19:25:51 -070058static int device_fd = -1;
59
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060struct uevent {
61 const char *action;
62 const char *path;
63 const char *subsystem;
64 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070065 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070066 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070067 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068 int major;
69 int minor;
70};
71
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072struct perms_ {
73 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070074 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075 mode_t perm;
76 unsigned int uid;
77 unsigned int gid;
78 unsigned short prefix;
79};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070080
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070081struct perm_node {
82 struct perms_ dp;
83 struct listnode plist;
84};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070085
Colin Crossfadb85e2011-03-30 18:32:12 -070086struct platform_node {
87 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080088 char *path;
89 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070090 struct listnode list;
91};
92
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070093static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070094static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070095static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070096
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070097int add_dev_perms(const char *name, const char *attr,
98 mode_t perm, unsigned int uid, unsigned int gid,
99 unsigned short prefix) {
100 struct perm_node *node = calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700101 if (!node)
102 return -ENOMEM;
103
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700104 node->dp.name = strdup(name);
105 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700106 return -ENOMEM;
107
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700108 if (attr) {
109 node->dp.attr = strdup(attr);
110 if (!node->dp.attr)
111 return -ENOMEM;
112 }
113
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700114 node->dp.perm = perm;
115 node->dp.uid = uid;
116 node->dp.gid = gid;
117 node->dp.prefix = prefix;
118
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700119 if (attr)
120 list_add_tail(&sys_perms, &node->plist);
121 else
122 list_add_tail(&dev_perms, &node->plist);
123
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700124 return 0;
125}
126
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700127void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700128{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700129 char buf[512];
130 struct listnode *node;
131 struct perms_ *dp;
Stephen Smalley5f7b0172013-04-03 13:15:01 -0400132 char *secontext;
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);
Stephen Smalley5f7b0172013-04-03 13:15:01 -0400154 if (sehandle) {
155 secontext = NULL;
156 selabel_lookup(sehandle, &secontext, buf, 0);
157 if (secontext) {
158 setfilecon(buf, secontext);
159 freecon(secontext);
160 }
161 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700162 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700163}
164
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700165static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
166{
167 mode_t perm;
Colin Cross44b65d02010-04-20 14:32:50 -0700168 struct listnode *node;
169 struct perm_node *perm_node;
170 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700171
Colin Cross44b65d02010-04-20 14:32:50 -0700172 /* search the perms list in reverse so that ueventd.$hardware can
173 * override ueventd.rc
174 */
175 list_for_each_reverse(node, &dev_perms) {
176 perm_node = node_to_item(node, struct perm_node, plist);
177 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700178
Colin Cross44b65d02010-04-20 14:32:50 -0700179 if (dp->prefix) {
180 if (strncmp(path, dp->name, strlen(dp->name)))
181 continue;
182 } else {
183 if (strcmp(path, dp->name))
184 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700185 }
Colin Cross44b65d02010-04-20 14:32:50 -0700186 *uid = dp->uid;
187 *gid = dp->gid;
188 return dp->perm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700189 }
Colin Cross44b65d02010-04-20 14:32:50 -0700190 /* Default if nothing found. */
191 *uid = 0;
192 *gid = 0;
193 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700194}
195
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700196static void make_device(const char *path,
197 const char *upath,
198 int block, int major, int minor)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700199{
200 unsigned uid;
201 unsigned gid;
202 mode_t mode;
203 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500204 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700205
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700206 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700207
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500208 if (sehandle) {
209 selabel_lookup(sehandle, &secontext, path, mode);
210 setfscreatecon(secontext);
211 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700212
Colin Cross17dcc5c2010-09-03 12:25:34 -0700213 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800214 /* Temporarily change egid to avoid race condition setting the gid of the
215 * device node. Unforunately changing the euid would prevent creation of
216 * some device nodes, so the uid has to be set with chown() and is still
217 * racy. Fixing the gid race at least fixed the issue with system_server
218 * opening dynamic input devices under the AID_INPUT gid. */
219 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700220 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800221 chown(path, uid, -1);
222 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700223
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500224 if (secontext) {
225 freecon(secontext);
226 setfscreatecon(NULL);
227 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700228}
229
Dima Zavinf395c922013-03-06 16:23:57 -0800230static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700231{
Dima Zavinf395c922013-03-06 16:23:57 -0800232 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700233 struct listnode *node;
234 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800235 const char *name = path;
236
237 if (!strncmp(path, "/devices/", 9)) {
238 name += 9;
239 if (!strncmp(name, "platform/", 9))
240 name += 9;
241 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700242
243 list_for_each_reverse(node, &platform_names) {
244 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800245 if ((bus->path_len < path_len) &&
246 (path[bus->path_len] == '/') &&
247 !strncmp(path, bus->path, bus->path_len))
Colin Crossfadb85e2011-03-30 18:32:12 -0700248 /* subdevice of an existing platform, ignore it */
249 return;
250 }
251
Dima Zavinf395c922013-03-06 16:23:57 -0800252 INFO("adding platform device %s (%s)\n", name, path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700253
254 bus = calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800255 bus->path = strdup(path);
256 bus->path_len = path_len;
257 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700258 list_add_tail(&platform_names, &bus->list);
259}
260
261/*
Dima Zavinf395c922013-03-06 16:23:57 -0800262 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700263 * platform device prefix. If it doesn't start with a platform device, return
264 * 0.
265 */
Dima Zavinf395c922013-03-06 16:23:57 -0800266static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700267{
Dima Zavinf395c922013-03-06 16:23:57 -0800268 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700269 struct listnode *node;
270 struct platform_node *bus;
271
272 list_for_each_reverse(node, &platform_names) {
273 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800274 if ((bus->path_len < path_len) &&
275 (path[bus->path_len] == '/') &&
276 !strncmp(path, bus->path, bus->path_len))
277 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700278 }
279
280 return NULL;
281}
282
Dima Zavinf395c922013-03-06 16:23:57 -0800283static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700284{
285 struct listnode *node;
286 struct platform_node *bus;
287
288 list_for_each_reverse(node, &platform_names) {
289 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800290 if (!strcmp(path, bus->path)) {
291 INFO("removing platform device %s\n", bus->name);
292 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700293 list_remove(node);
294 free(bus);
295 return;
296 }
297 }
298}
299
Chuck Tuffli1e070842008-12-15 14:26:56 -0800300#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301
302static inline suseconds_t get_usecs(void)
303{
304 struct timeval tv;
305 gettimeofday(&tv, 0);
306 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
307}
308
309#define log_event_print(x...) INFO(x)
310
311#else
312
313#define log_event_print(fmt, args...) do { } while (0)
314#define get_usecs() 0
315
316#endif
317
318static void parse_event(const char *msg, struct uevent *uevent)
319{
320 uevent->action = "";
321 uevent->path = "";
322 uevent->subsystem = "";
323 uevent->firmware = "";
324 uevent->major = -1;
325 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700326 uevent->partition_name = NULL;
327 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700328 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700329
330 /* currently ignoring SEQNUM */
331 while(*msg) {
332 if(!strncmp(msg, "ACTION=", 7)) {
333 msg += 7;
334 uevent->action = msg;
335 } else if(!strncmp(msg, "DEVPATH=", 8)) {
336 msg += 8;
337 uevent->path = msg;
338 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
339 msg += 10;
340 uevent->subsystem = msg;
341 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
342 msg += 9;
343 uevent->firmware = msg;
344 } else if(!strncmp(msg, "MAJOR=", 6)) {
345 msg += 6;
346 uevent->major = atoi(msg);
347 } else if(!strncmp(msg, "MINOR=", 6)) {
348 msg += 6;
349 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700350 } else if(!strncmp(msg, "PARTN=", 6)) {
351 msg += 6;
352 uevent->partition_num = atoi(msg);
353 } else if(!strncmp(msg, "PARTNAME=", 9)) {
354 msg += 9;
355 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700356 } else if(!strncmp(msg, "DEVNAME=", 8)) {
357 msg += 8;
358 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700359 }
360
Wei Zhongf97b8872012-03-23 14:15:34 -0700361 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700362 while(*msg++)
363 ;
364 }
365
366 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
367 uevent->action, uevent->path, uevent->subsystem,
368 uevent->firmware, uevent->major, uevent->minor);
369}
370
Benoit Gobyd2278632010-08-03 14:36:02 -0700371static char **get_character_device_symlinks(struct uevent *uevent)
372{
373 const char *parent;
374 char *slash;
375 char **links;
376 int link_num = 0;
377 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800378 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700379
Dima Zavinf395c922013-03-06 16:23:57 -0800380 pdev = find_platform_device(uevent->path);
381 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700382 return NULL;
383
384 links = malloc(sizeof(char *) * 2);
385 if (!links)
386 return NULL;
387 memset(links, 0, sizeof(char *) * 2);
388
389 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800390 parent = strchr(uevent->path + pdev->path_len, '/');
Benoit Gobyd2278632010-08-03 14:36:02 -0700391 if (!*parent)
392 goto err;
393
394 if (!strncmp(parent, "/usb", 4)) {
395 /* skip root hub name and device. use device interface */
396 while (*++parent && *parent != '/');
397 if (*parent)
398 while (*++parent && *parent != '/');
399 if (!*parent)
400 goto err;
401 slash = strchr(++parent, '/');
402 if (!slash)
403 goto err;
404 width = slash - parent;
405 if (width <= 0)
406 goto err;
407
408 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
409 link_num++;
410 else
411 links[link_num] = NULL;
412 mkdir("/dev/usb", 0755);
413 }
414 else {
415 goto err;
416 }
417
418 return links;
419err:
420 free(links);
421 return NULL;
422}
423
Colin Crossb0ab94b2010-04-08 16:16:20 -0700424static char **parse_platform_block_device(struct uevent *uevent)
425{
Colin Crossfadb85e2011-03-30 18:32:12 -0700426 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800427 struct platform_node *pdev;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700428 char *slash;
429 int width;
430 char buf[256];
431 char link_path[256];
432 int fd;
433 int link_num = 0;
434 int ret;
435 char *p;
436 unsigned int size;
437 struct stat info;
438
Dima Zavinf395c922013-03-06 16:23:57 -0800439 pdev = find_platform_device(uevent->path);
440 if (!pdev)
441 return NULL;
442 device = pdev->name;
443
Colin Crossb0ab94b2010-04-08 16:16:20 -0700444 char **links = malloc(sizeof(char *) * 4);
445 if (!links)
446 return NULL;
447 memset(links, 0, sizeof(char *) * 4);
448
Colin Crossfadb85e2011-03-30 18:32:12 -0700449 INFO("found platform device %s\n", device);
450
451 snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700452
453 if (uevent->partition_name) {
454 p = strdup(uevent->partition_name);
455 sanitize(p);
Johan Redestig93ca79b2012-04-18 16:41:19 +0200456 if (strcmp(uevent->partition_name, p))
457 NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700458 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
459 link_num++;
460 else
461 links[link_num] = NULL;
462 free(p);
463 }
464
465 if (uevent->partition_num >= 0) {
466 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
467 link_num++;
468 else
469 links[link_num] = NULL;
470 }
471
Dima Zavinf395c922013-03-06 16:23:57 -0800472 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700473 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
474 link_num++;
475 else
476 links[link_num] = NULL;
477
478 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700479}
480
Colin Crosseb5ba832011-03-30 17:37:17 -0700481static void handle_device(const char *action, const char *devpath,
482 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700483{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700484 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700485
Colin Crosseb5ba832011-03-30 17:37:17 -0700486 if(!strcmp(action, "add")) {
487 make_device(devpath, path, block, major, minor);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700488 if (links) {
489 for (i = 0; links[i]; i++)
490 make_link(devpath, links[i]);
491 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700492 }
493
Colin Crosseb5ba832011-03-30 17:37:17 -0700494 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700495 if (links) {
496 for (i = 0; links[i]; i++)
497 remove_link(devpath, links[i]);
498 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700499 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700500 }
501
502 if (links) {
503 for (i = 0; links[i]; i++)
504 free(links[i]);
505 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700506 }
507}
508
Colin Crossfadb85e2011-03-30 18:32:12 -0700509static void handle_platform_device_event(struct uevent *uevent)
510{
Dima Zavinf395c922013-03-06 16:23:57 -0800511 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700512
513 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800514 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700515 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800516 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700517}
518
Colin Crosseb5ba832011-03-30 17:37:17 -0700519static const char *parse_device_name(struct uevent *uevent, unsigned int len)
520{
521 const char *name;
522
523 /* if it's not a /dev device, nothing else to do */
524 if((uevent->major < 0) || (uevent->minor < 0))
525 return NULL;
526
527 /* do we have a name? */
528 name = strrchr(uevent->path, '/');
529 if(!name)
530 return NULL;
531 name++;
532
533 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800534 if(strlen(name) > len) {
535 ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n",
536 name, len);
Colin Crosseb5ba832011-03-30 17:37:17 -0700537 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800538 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700539
540 return name;
541}
542
543static void handle_block_device_event(struct uevent *uevent)
544{
545 const char *base = "/dev/block/";
546 const char *name;
547 char devpath[96];
548 char **links = NULL;
549
550 name = parse_device_name(uevent, 64);
551 if (!name)
552 return;
553
554 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500555 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700556
Dima Zavinf395c922013-03-06 16:23:57 -0800557 if (!strncmp(uevent->path, "/devices/", 9))
Colin Crosseb5ba832011-03-30 17:37:17 -0700558 links = parse_platform_block_device(uevent);
559
560 handle_device(uevent->action, devpath, uevent->path, 1,
561 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700562}
563
Greg Hackmann3312aa82013-11-18 15:24:40 -0800564#define DEVPATH_LEN 96
565
566static bool assemble_devpath(char *devpath, const char *dirname,
567 const char *devname)
568{
569 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
570 if (s < 0) {
571 ERROR("failed to assemble device path (%s); ignoring event\n",
572 strerror(errno));
573 return false;
574 } else if (s >= DEVPATH_LEN) {
575 ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n",
576 dirname, devname, DEVPATH_LEN);
577 return false;
578 }
579 return true;
580}
581
582static void mkdir_recursive_for_devpath(const char *devpath)
583{
584 char dir[DEVPATH_LEN];
585 char *slash;
586
587 strcpy(dir, devpath);
588 slash = strrchr(dir, '/');
589 *slash = '\0';
590 mkdir_recursive(dir, 0755);
591}
592
Colin Crosseb5ba832011-03-30 17:37:17 -0700593static void handle_generic_device_event(struct uevent *uevent)
594{
595 char *base;
596 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800597 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700598 char **links = NULL;
599
600 name = parse_device_name(uevent, 64);
601 if (!name)
602 return;
603
Greg Hackmann3312aa82013-11-18 15:24:40 -0800604 struct ueventd_subsystem *subsystem =
605 ueventd_subsystem_find_by_name(uevent->subsystem);
606
607 if (subsystem) {
608 const char *devname;
609
610 switch (subsystem->devname_src) {
611 case DEVNAME_UEVENT_DEVNAME:
612 devname = uevent->device_name;
613 break;
614
615 case DEVNAME_UEVENT_DEVPATH:
616 devname = name;
617 break;
618
619 default:
620 ERROR("%s subsystem's devpath option is not set; ignoring event\n",
621 uevent->subsystem);
622 return;
623 }
624
625 if (!assemble_devpath(devpath, subsystem->dirname, devname))
626 return;
627 mkdir_recursive_for_devpath(devpath);
628 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700629 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700630 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800631 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800632 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800633 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700634 }
635 else {
636 /* This imitates the file system that would be created
637 * if we were using devfs instead.
638 * Minors are broken up into groups of 128, starting at "001"
639 */
640 int bus_id = uevent->minor / 128 + 1;
641 int device_id = uevent->minor % 128 + 1;
642 /* build directories */
643 make_dir("/dev/bus", 0755);
644 make_dir("/dev/bus/usb", 0755);
645 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
646 make_dir(devpath, 0755);
647 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
648 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700649 } else {
650 /* ignore other USB events */
651 return;
652 }
653 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
654 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500655 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200656 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
657 base = "/dev/dri/";
658 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700659 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
660 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500661 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700662 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
663 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500664 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700665 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
666 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500667 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700668 } else if(!strncmp(uevent->subsystem, "input", 5)) {
669 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500670 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700671 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
672 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500673 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700674 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
675 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500676 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700677 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
678 !strncmp(name, "log_", 4)) {
679 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500680 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700681 name += 4;
682 } else
683 base = "/dev/";
684 links = get_character_device_symlinks(uevent);
685
686 if (!devpath[0])
687 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
688
689 handle_device(uevent->action, devpath, uevent->path, 0,
690 uevent->major, uevent->minor, links);
691}
692
693static void handle_device_event(struct uevent *uevent)
694{
Colin Cross308bc522012-07-23 16:31:28 -0700695 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700696 fixup_sys_perms(uevent->path);
697
698 if (!strncmp(uevent->subsystem, "block", 5)) {
699 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700700 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
701 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700702 } else {
703 handle_generic_device_event(uevent);
704 }
705}
706
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700707static int load_firmware(int fw_fd, int loading_fd, int data_fd)
708{
709 struct stat st;
710 long len_to_copy;
711 int ret = 0;
712
713 if(fstat(fw_fd, &st) < 0)
714 return -1;
715 len_to_copy = st.st_size;
716
717 write(loading_fd, "1", 1); /* start transfer */
718
719 while (len_to_copy > 0) {
720 char buf[PAGE_SIZE];
721 ssize_t nr;
722
723 nr = read(fw_fd, buf, sizeof(buf));
724 if(!nr)
725 break;
726 if(nr < 0) {
727 ret = -1;
728 break;
729 }
730
731 len_to_copy -= nr;
732 while (nr > 0) {
733 ssize_t nw = 0;
734
735 nw = write(data_fd, buf + nw, nr);
736 if(nw <= 0) {
737 ret = -1;
738 goto out;
739 }
740 nr -= nw;
741 }
742 }
743
744out:
745 if(!ret)
746 write(loading_fd, "0", 1); /* successful end of transfer */
747 else
748 write(loading_fd, "-1", 2); /* abort transfer */
749
750 return ret;
751}
752
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700753static int is_booting(void)
754{
755 return access("/dev/.booting", F_OK) == 0;
756}
757
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700758static void process_firmware_event(struct uevent *uevent)
759{
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700760 char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700761 int l, loading_fd, data_fd, fw_fd;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700762 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700763
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700764 INFO("firmware: loading '%s' for '%s'\n",
765 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700766
767 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
768 if (l == -1)
769 return;
770
771 l = asprintf(&loading, "%sloading", root);
772 if (l == -1)
773 goto root_free_out;
774
775 l = asprintf(&data, "%sdata", root);
776 if (l == -1)
777 goto loading_free_out;
778
Brian Swetland02863b92010-09-19 03:36:39 -0700779 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
780 if (l == -1)
781 goto data_free_out;
782
783 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700784 if (l == -1)
785 goto data_free_out;
786
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700787 l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware);
788 if (l == -1)
789 goto data_free_out;
790
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700791 loading_fd = open(loading, O_WRONLY);
792 if(loading_fd < 0)
793 goto file_free_out;
794
795 data_fd = open(data, O_WRONLY);
796 if(data_fd < 0)
797 goto loading_close_out;
798
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700799try_loading_again:
Brian Swetland02863b92010-09-19 03:36:39 -0700800 fw_fd = open(file1, O_RDONLY);
801 if(fw_fd < 0) {
802 fw_fd = open(file2, O_RDONLY);
Benoit Goby609d8822010-11-09 18:10:24 -0800803 if (fw_fd < 0) {
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700804 fw_fd = open(file3, O_RDONLY);
805 if (fw_fd < 0) {
806 if (booting) {
807 /* If we're not fully booted, we may be missing
808 * filesystems needed for firmware, wait and retry.
809 */
810 usleep(100000);
811 booting = is_booting();
812 goto try_loading_again;
813 }
814 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
815 write(loading_fd, "-1", 2);
816 goto data_close_out;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700817 }
Benoit Goby609d8822010-11-09 18:10:24 -0800818 }
Brian Swetland02863b92010-09-19 03:36:39 -0700819 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700820
821 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700822 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700823 else
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700824 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700825
826 close(fw_fd);
827data_close_out:
828 close(data_fd);
829loading_close_out:
830 close(loading_fd);
831file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700832 free(file1);
833 free(file2);
Ajay Dudani76c58892013-06-12 15:55:26 -0700834 free(file3);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700835data_free_out:
836 free(data);
837loading_free_out:
838 free(loading);
839root_free_out:
840 free(root);
841}
842
843static void handle_firmware_event(struct uevent *uevent)
844{
845 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700846 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700847
848 if(strcmp(uevent->subsystem, "firmware"))
849 return;
850
851 if(strcmp(uevent->action, "add"))
852 return;
853
854 /* we fork, to avoid making large memory allocations in init proper */
855 pid = fork();
856 if (!pid) {
857 process_firmware_event(uevent);
858 exit(EXIT_SUCCESS);
859 }
860}
861
862#define UEVENT_MSG_LEN 1024
Colin Cross0dd7ca62010-04-13 19:25:51 -0700863void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700864{
Vernon Tang3f582e92011-04-25 13:08:17 +1000865 char msg[UEVENT_MSG_LEN+2];
866 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700867 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700868 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700869 continue;
870
871 msg[n] = '\0';
872 msg[n+1] = '\0';
873
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700874 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700875 parse_event(msg, &uevent);
876
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400877 if (sehandle && selinux_status_updated() > 0) {
878 struct selabel_handle *sehandle2;
879 sehandle2 = selinux_android_file_context_handle();
880 if (sehandle2) {
881 selabel_close(sehandle);
882 sehandle = sehandle2;
883 }
884 }
885
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700886 handle_device_event(&uevent);
887 handle_firmware_event(&uevent);
888 }
889}
890
891/* Coldboot walks parts of the /sys tree and pokes the uevent files
892** to cause the kernel to regenerate device add events that happened
893** before init's device manager was started
894**
895** We drain any pending events from the netlink socket every time
896** we poke another uevent file to make sure we don't overrun the
897** socket's buffer.
898*/
899
Colin Cross0dd7ca62010-04-13 19:25:51 -0700900static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700901{
902 struct dirent *de;
903 int dfd, fd;
904
905 dfd = dirfd(d);
906
907 fd = openat(dfd, "uevent", O_WRONLY);
908 if(fd >= 0) {
909 write(fd, "add\n", 4);
910 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700911 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700912 }
913
914 while((de = readdir(d))) {
915 DIR *d2;
916
917 if(de->d_type != DT_DIR || de->d_name[0] == '.')
918 continue;
919
920 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
921 if(fd < 0)
922 continue;
923
924 d2 = fdopendir(fd);
925 if(d2 == 0)
926 close(fd);
927 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700928 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700929 closedir(d2);
930 }
931 }
932}
933
Colin Cross0dd7ca62010-04-13 19:25:51 -0700934static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700935{
936 DIR *d = opendir(path);
937 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700938 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700939 closedir(d);
940 }
941}
942
Colin Cross0dd7ca62010-04-13 19:25:51 -0700943void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700944{
945 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -0700946 struct stat info;
947 int fd;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700948
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400949 sehandle = NULL;
950 if (is_selinux_enabled() > 0) {
951 sehandle = selinux_android_file_context_handle();
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400952 selinux_status_open(true);
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400953 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700954
Andrew Boied562ca72012-12-04 15:47:20 -0800955 /* is 256K enough? udev uses 16MB! */
956 device_fd = uevent_open_socket(256*1024, true);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700957 if(device_fd < 0)
958 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700959
Colin Cross0dd7ca62010-04-13 19:25:51 -0700960 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
961 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700962
Colin Crossf83d0b92010-04-21 12:04:20 -0700963 if (stat(coldboot_done, &info) < 0) {
964 t0 = get_usecs();
965 coldboot("/sys/class");
966 coldboot("/sys/block");
967 coldboot("/sys/devices");
968 t1 = get_usecs();
969 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
970 close(fd);
971 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
972 } else {
973 log_event_print("skipping coldboot, already done\n");
974 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700975}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700976
Colin Cross0dd7ca62010-04-13 19:25:51 -0700977int get_device_fd()
978{
979 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700980}