blob: a70c61dd018ea01f8bff6aa6c8ddc59ec900a89f [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>
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"
Greg Hackmann3312aa82013-11-18 15:24:40 -080046#include "ueventd_parser.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070047#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070048#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070049
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070050#define SYSFS_PREFIX "/sys"
Brian Swetland02863b92010-09-19 03:36:39 -070051#define FIRMWARE_DIR1 "/etc/firmware"
52#define FIRMWARE_DIR2 "/vendor/firmware"
Iliyan Malchev029d44e2012-06-08 10:42:26 -070053#define FIRMWARE_DIR3 "/firmware/image"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054
Stephen Smalleye096e362012-06-11 13:37:39 -040055extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050056
Colin Cross0dd7ca62010-04-13 19:25:51 -070057static int device_fd = -1;
58
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070059struct uevent {
60 const char *action;
61 const char *path;
62 const char *subsystem;
63 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070064 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070065 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070066 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070067 int major;
68 int minor;
69};
70
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070071struct perms_ {
72 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070073 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070074 mode_t perm;
75 unsigned int uid;
76 unsigned int gid;
77 unsigned short prefix;
78};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070079
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070080struct perm_node {
81 struct perms_ dp;
82 struct listnode plist;
83};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070084
Colin Crossfadb85e2011-03-30 18:32:12 -070085struct platform_node {
86 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080087 char *path;
88 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070089 struct listnode list;
90};
91
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070092static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070093static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070094static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070095
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070096int add_dev_perms(const char *name, const char *attr,
97 mode_t perm, unsigned int uid, unsigned int gid,
98 unsigned short prefix) {
99 struct perm_node *node = calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700100 if (!node)
101 return -ENOMEM;
102
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700103 node->dp.name = strdup(name);
104 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700105 return -ENOMEM;
106
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700107 if (attr) {
108 node->dp.attr = strdup(attr);
109 if (!node->dp.attr)
110 return -ENOMEM;
111 }
112
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700113 node->dp.perm = perm;
114 node->dp.uid = uid;
115 node->dp.gid = gid;
116 node->dp.prefix = prefix;
117
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700118 if (attr)
119 list_add_tail(&sys_perms, &node->plist);
120 else
121 list_add_tail(&dev_perms, &node->plist);
122
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700123 return 0;
124}
125
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700126void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700127{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700128 char buf[512];
129 struct listnode *node;
130 struct perms_ *dp;
Stephen Smalley5f7b0172013-04-03 13:15:01 -0400131 char *secontext;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700132
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700133 /* upaths omit the "/sys" that paths in this list
134 * contain, so we add 4 when comparing...
135 */
136 list_for_each(node, &sys_perms) {
137 dp = &(node_to_item(node, struct perm_node, plist))->dp;
138 if (dp->prefix) {
139 if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700140 continue;
141 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700142 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700143 continue;
144 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700145
146 if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
147 return;
148
149 sprintf(buf,"/sys%s/%s", upath, dp->attr);
150 INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
151 chown(buf, dp->uid, dp->gid);
152 chmod(buf, dp->perm);
Stephen Smalley5f7b0172013-04-03 13:15:01 -0400153 if (sehandle) {
154 secontext = NULL;
155 selabel_lookup(sehandle, &secontext, buf, 0);
156 if (secontext) {
157 setfilecon(buf, secontext);
158 freecon(secontext);
159 }
160 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700161 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700162}
163
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700164static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
165{
166 mode_t perm;
Colin Cross44b65d02010-04-20 14:32:50 -0700167 struct listnode *node;
168 struct perm_node *perm_node;
169 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700170
Colin Cross44b65d02010-04-20 14:32:50 -0700171 /* search the perms list in reverse so that ueventd.$hardware can
172 * override ueventd.rc
173 */
174 list_for_each_reverse(node, &dev_perms) {
175 perm_node = node_to_item(node, struct perm_node, plist);
176 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700177
Colin Cross44b65d02010-04-20 14:32:50 -0700178 if (dp->prefix) {
179 if (strncmp(path, dp->name, strlen(dp->name)))
180 continue;
181 } else {
182 if (strcmp(path, dp->name))
183 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700184 }
Colin Cross44b65d02010-04-20 14:32:50 -0700185 *uid = dp->uid;
186 *gid = dp->gid;
187 return dp->perm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700188 }
Colin Cross44b65d02010-04-20 14:32:50 -0700189 /* Default if nothing found. */
190 *uid = 0;
191 *gid = 0;
192 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700193}
194
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700195static void make_device(const char *path,
196 const char *upath,
197 int block, int major, int minor)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700198{
199 unsigned uid;
200 unsigned gid;
201 mode_t mode;
202 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500203 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700204
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700205 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700206
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500207 if (sehandle) {
208 selabel_lookup(sehandle, &secontext, path, mode);
209 setfscreatecon(secontext);
210 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700211
Colin Cross17dcc5c2010-09-03 12:25:34 -0700212 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800213 /* Temporarily change egid to avoid race condition setting the gid of the
214 * device node. Unforunately changing the euid would prevent creation of
215 * some device nodes, so the uid has to be set with chown() and is still
216 * racy. Fixing the gid race at least fixed the issue with system_server
217 * opening dynamic input devices under the AID_INPUT gid. */
218 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700219 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800220 chown(path, uid, -1);
221 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700222
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500223 if (secontext) {
224 freecon(secontext);
225 setfscreatecon(NULL);
226 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700227}
228
Dima Zavinf395c922013-03-06 16:23:57 -0800229static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700230{
Dima Zavinf395c922013-03-06 16:23:57 -0800231 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700232 struct listnode *node;
233 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800234 const char *name = path;
235
236 if (!strncmp(path, "/devices/", 9)) {
237 name += 9;
238 if (!strncmp(name, "platform/", 9))
239 name += 9;
240 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700241
242 list_for_each_reverse(node, &platform_names) {
243 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800244 if ((bus->path_len < path_len) &&
245 (path[bus->path_len] == '/') &&
246 !strncmp(path, bus->path, bus->path_len))
Colin Crossfadb85e2011-03-30 18:32:12 -0700247 /* subdevice of an existing platform, ignore it */
248 return;
249 }
250
Dima Zavinf395c922013-03-06 16:23:57 -0800251 INFO("adding platform device %s (%s)\n", name, path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700252
253 bus = calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800254 bus->path = strdup(path);
255 bus->path_len = path_len;
256 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700257 list_add_tail(&platform_names, &bus->list);
258}
259
260/*
Dima Zavinf395c922013-03-06 16:23:57 -0800261 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700262 * platform device prefix. If it doesn't start with a platform device, return
263 * 0.
264 */
Dima Zavinf395c922013-03-06 16:23:57 -0800265static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700266{
Dima Zavinf395c922013-03-06 16:23:57 -0800267 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700268 struct listnode *node;
269 struct platform_node *bus;
270
271 list_for_each_reverse(node, &platform_names) {
272 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800273 if ((bus->path_len < path_len) &&
274 (path[bus->path_len] == '/') &&
275 !strncmp(path, bus->path, bus->path_len))
276 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700277 }
278
279 return NULL;
280}
281
Dima Zavinf395c922013-03-06 16:23:57 -0800282static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700283{
284 struct listnode *node;
285 struct platform_node *bus;
286
287 list_for_each_reverse(node, &platform_names) {
288 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800289 if (!strcmp(path, bus->path)) {
290 INFO("removing platform device %s\n", bus->name);
291 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700292 list_remove(node);
293 free(bus);
294 return;
295 }
296 }
297}
298
Chuck Tuffli1e070842008-12-15 14:26:56 -0800299#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700300
301static inline suseconds_t get_usecs(void)
302{
303 struct timeval tv;
304 gettimeofday(&tv, 0);
305 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
306}
307
308#define log_event_print(x...) INFO(x)
309
310#else
311
312#define log_event_print(fmt, args...) do { } while (0)
313#define get_usecs() 0
314
315#endif
316
317static void parse_event(const char *msg, struct uevent *uevent)
318{
319 uevent->action = "";
320 uevent->path = "";
321 uevent->subsystem = "";
322 uevent->firmware = "";
323 uevent->major = -1;
324 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700325 uevent->partition_name = NULL;
326 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700327 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700328
329 /* currently ignoring SEQNUM */
330 while(*msg) {
331 if(!strncmp(msg, "ACTION=", 7)) {
332 msg += 7;
333 uevent->action = msg;
334 } else if(!strncmp(msg, "DEVPATH=", 8)) {
335 msg += 8;
336 uevent->path = msg;
337 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
338 msg += 10;
339 uevent->subsystem = msg;
340 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
341 msg += 9;
342 uevent->firmware = msg;
343 } else if(!strncmp(msg, "MAJOR=", 6)) {
344 msg += 6;
345 uevent->major = atoi(msg);
346 } else if(!strncmp(msg, "MINOR=", 6)) {
347 msg += 6;
348 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700349 } else if(!strncmp(msg, "PARTN=", 6)) {
350 msg += 6;
351 uevent->partition_num = atoi(msg);
352 } else if(!strncmp(msg, "PARTNAME=", 9)) {
353 msg += 9;
354 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700355 } else if(!strncmp(msg, "DEVNAME=", 8)) {
356 msg += 8;
357 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700358 }
359
Wei Zhongf97b8872012-03-23 14:15:34 -0700360 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700361 while(*msg++)
362 ;
363 }
364
365 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
366 uevent->action, uevent->path, uevent->subsystem,
367 uevent->firmware, uevent->major, uevent->minor);
368}
369
Benoit Gobyd2278632010-08-03 14:36:02 -0700370static char **get_character_device_symlinks(struct uevent *uevent)
371{
372 const char *parent;
373 char *slash;
374 char **links;
375 int link_num = 0;
376 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800377 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700378
Dima Zavinf395c922013-03-06 16:23:57 -0800379 pdev = find_platform_device(uevent->path);
380 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700381 return NULL;
382
383 links = malloc(sizeof(char *) * 2);
384 if (!links)
385 return NULL;
386 memset(links, 0, sizeof(char *) * 2);
387
388 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800389 parent = strchr(uevent->path + pdev->path_len, '/');
Benoit Gobyd2278632010-08-03 14:36:02 -0700390 if (!*parent)
391 goto err;
392
393 if (!strncmp(parent, "/usb", 4)) {
394 /* skip root hub name and device. use device interface */
395 while (*++parent && *parent != '/');
396 if (*parent)
397 while (*++parent && *parent != '/');
398 if (!*parent)
399 goto err;
400 slash = strchr(++parent, '/');
401 if (!slash)
402 goto err;
403 width = slash - parent;
404 if (width <= 0)
405 goto err;
406
407 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
408 link_num++;
409 else
410 links[link_num] = NULL;
411 mkdir("/dev/usb", 0755);
412 }
413 else {
414 goto err;
415 }
416
417 return links;
418err:
419 free(links);
420 return NULL;
421}
422
Colin Crossb0ab94b2010-04-08 16:16:20 -0700423static char **parse_platform_block_device(struct uevent *uevent)
424{
Colin Crossfadb85e2011-03-30 18:32:12 -0700425 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800426 struct platform_node *pdev;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700427 char *slash;
428 int width;
429 char buf[256];
430 char link_path[256];
431 int fd;
432 int link_num = 0;
433 int ret;
434 char *p;
435 unsigned int size;
436 struct stat info;
437
Dima Zavinf395c922013-03-06 16:23:57 -0800438 pdev = find_platform_device(uevent->path);
439 if (!pdev)
440 return NULL;
441 device = pdev->name;
442
Colin Crossb0ab94b2010-04-08 16:16:20 -0700443 char **links = malloc(sizeof(char *) * 4);
444 if (!links)
445 return NULL;
446 memset(links, 0, sizeof(char *) * 4);
447
Colin Crossfadb85e2011-03-30 18:32:12 -0700448 INFO("found platform device %s\n", device);
449
450 snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700451
452 if (uevent->partition_name) {
453 p = strdup(uevent->partition_name);
454 sanitize(p);
Johan Redestig93ca79b2012-04-18 16:41:19 +0200455 if (strcmp(uevent->partition_name, p))
456 NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700457 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
458 link_num++;
459 else
460 links[link_num] = NULL;
461 free(p);
462 }
463
464 if (uevent->partition_num >= 0) {
465 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
466 link_num++;
467 else
468 links[link_num] = NULL;
469 }
470
Dima Zavinf395c922013-03-06 16:23:57 -0800471 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700472 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
473 link_num++;
474 else
475 links[link_num] = NULL;
476
477 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700478}
479
Colin Crosseb5ba832011-03-30 17:37:17 -0700480static void handle_device(const char *action, const char *devpath,
481 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700482{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700483 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700484
Colin Crosseb5ba832011-03-30 17:37:17 -0700485 if(!strcmp(action, "add")) {
486 make_device(devpath, path, block, major, minor);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700487 if (links) {
488 for (i = 0; links[i]; i++)
489 make_link(devpath, links[i]);
490 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700491 }
492
Colin Crosseb5ba832011-03-30 17:37:17 -0700493 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700494 if (links) {
495 for (i = 0; links[i]; i++)
496 remove_link(devpath, links[i]);
497 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700498 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700499 }
500
501 if (links) {
502 for (i = 0; links[i]; i++)
503 free(links[i]);
504 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700505 }
506}
507
Colin Crossfadb85e2011-03-30 18:32:12 -0700508static void handle_platform_device_event(struct uevent *uevent)
509{
Dima Zavinf395c922013-03-06 16:23:57 -0800510 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700511
512 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800513 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700514 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800515 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700516}
517
Colin Crosseb5ba832011-03-30 17:37:17 -0700518static const char *parse_device_name(struct uevent *uevent, unsigned int len)
519{
520 const char *name;
521
522 /* if it's not a /dev device, nothing else to do */
523 if((uevent->major < 0) || (uevent->minor < 0))
524 return NULL;
525
526 /* do we have a name? */
527 name = strrchr(uevent->path, '/');
528 if(!name)
529 return NULL;
530 name++;
531
532 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800533 if(strlen(name) > len) {
534 ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n",
535 name, len);
Colin Crosseb5ba832011-03-30 17:37:17 -0700536 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800537 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700538
539 return name;
540}
541
542static void handle_block_device_event(struct uevent *uevent)
543{
544 const char *base = "/dev/block/";
545 const char *name;
546 char devpath[96];
547 char **links = NULL;
548
549 name = parse_device_name(uevent, 64);
550 if (!name)
551 return;
552
553 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500554 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700555
Dima Zavinf395c922013-03-06 16:23:57 -0800556 if (!strncmp(uevent->path, "/devices/", 9))
Colin Crosseb5ba832011-03-30 17:37:17 -0700557 links = parse_platform_block_device(uevent);
558
559 handle_device(uevent->action, devpath, uevent->path, 1,
560 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700561}
562
Greg Hackmann3312aa82013-11-18 15:24:40 -0800563#define DEVPATH_LEN 96
564
565static bool assemble_devpath(char *devpath, const char *dirname,
566 const char *devname)
567{
568 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
569 if (s < 0) {
570 ERROR("failed to assemble device path (%s); ignoring event\n",
571 strerror(errno));
572 return false;
573 } else if (s >= DEVPATH_LEN) {
574 ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n",
575 dirname, devname, DEVPATH_LEN);
576 return false;
577 }
578 return true;
579}
580
581static void mkdir_recursive_for_devpath(const char *devpath)
582{
583 char dir[DEVPATH_LEN];
584 char *slash;
585
586 strcpy(dir, devpath);
587 slash = strrchr(dir, '/');
588 *slash = '\0';
589 mkdir_recursive(dir, 0755);
590}
591
Colin Crosseb5ba832011-03-30 17:37:17 -0700592static void handle_generic_device_event(struct uevent *uevent)
593{
594 char *base;
595 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800596 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700597 char **links = NULL;
598
599 name = parse_device_name(uevent, 64);
600 if (!name)
601 return;
602
Greg Hackmann3312aa82013-11-18 15:24:40 -0800603 struct ueventd_subsystem *subsystem =
604 ueventd_subsystem_find_by_name(uevent->subsystem);
605
606 if (subsystem) {
607 const char *devname;
608
609 switch (subsystem->devname_src) {
610 case DEVNAME_UEVENT_DEVNAME:
611 devname = uevent->device_name;
612 break;
613
614 case DEVNAME_UEVENT_DEVPATH:
615 devname = name;
616 break;
617
618 default:
619 ERROR("%s subsystem's devpath option is not set; ignoring event\n",
620 uevent->subsystem);
621 return;
622 }
623
624 if (!assemble_devpath(devpath, subsystem->dirname, devname))
625 return;
626 mkdir_recursive_for_devpath(devpath);
627 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700628 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700629 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800630 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800631 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800632 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700633 }
634 else {
635 /* This imitates the file system that would be created
636 * if we were using devfs instead.
637 * Minors are broken up into groups of 128, starting at "001"
638 */
639 int bus_id = uevent->minor / 128 + 1;
640 int device_id = uevent->minor % 128 + 1;
641 /* build directories */
642 make_dir("/dev/bus", 0755);
643 make_dir("/dev/bus/usb", 0755);
644 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
645 make_dir(devpath, 0755);
646 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
647 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700648 } else {
649 /* ignore other USB events */
650 return;
651 }
652 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
653 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500654 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200655 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
656 base = "/dev/dri/";
657 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700658 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
659 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500660 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700661 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
662 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500663 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700664 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
665 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500666 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700667 } else if(!strncmp(uevent->subsystem, "input", 5)) {
668 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500669 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700670 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
671 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500672 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700673 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
674 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500675 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700676 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
677 !strncmp(name, "log_", 4)) {
678 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500679 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700680 name += 4;
681 } else
682 base = "/dev/";
683 links = get_character_device_symlinks(uevent);
684
685 if (!devpath[0])
686 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
687
688 handle_device(uevent->action, devpath, uevent->path, 0,
689 uevent->major, uevent->minor, links);
690}
691
692static void handle_device_event(struct uevent *uevent)
693{
Colin Cross308bc522012-07-23 16:31:28 -0700694 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700695 fixup_sys_perms(uevent->path);
696
697 if (!strncmp(uevent->subsystem, "block", 5)) {
698 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700699 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
700 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700701 } else {
702 handle_generic_device_event(uevent);
703 }
704}
705
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700706static int load_firmware(int fw_fd, int loading_fd, int data_fd)
707{
708 struct stat st;
709 long len_to_copy;
710 int ret = 0;
711
712 if(fstat(fw_fd, &st) < 0)
713 return -1;
714 len_to_copy = st.st_size;
715
716 write(loading_fd, "1", 1); /* start transfer */
717
718 while (len_to_copy > 0) {
719 char buf[PAGE_SIZE];
720 ssize_t nr;
721
722 nr = read(fw_fd, buf, sizeof(buf));
723 if(!nr)
724 break;
725 if(nr < 0) {
726 ret = -1;
727 break;
728 }
729
730 len_to_copy -= nr;
731 while (nr > 0) {
732 ssize_t nw = 0;
733
734 nw = write(data_fd, buf + nw, nr);
735 if(nw <= 0) {
736 ret = -1;
737 goto out;
738 }
739 nr -= nw;
740 }
741 }
742
743out:
744 if(!ret)
745 write(loading_fd, "0", 1); /* successful end of transfer */
746 else
747 write(loading_fd, "-1", 2); /* abort transfer */
748
749 return ret;
750}
751
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700752static int is_booting(void)
753{
754 return access("/dev/.booting", F_OK) == 0;
755}
756
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700757static void process_firmware_event(struct uevent *uevent)
758{
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700759 char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700760 int l, loading_fd, data_fd, fw_fd;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700761 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700762
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700763 INFO("firmware: loading '%s' for '%s'\n",
764 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700765
766 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
767 if (l == -1)
768 return;
769
770 l = asprintf(&loading, "%sloading", root);
771 if (l == -1)
772 goto root_free_out;
773
774 l = asprintf(&data, "%sdata", root);
775 if (l == -1)
776 goto loading_free_out;
777
Brian Swetland02863b92010-09-19 03:36:39 -0700778 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
779 if (l == -1)
780 goto data_free_out;
781
782 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700783 if (l == -1)
784 goto data_free_out;
785
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700786 l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware);
787 if (l == -1)
788 goto data_free_out;
789
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700790 loading_fd = open(loading, O_WRONLY);
791 if(loading_fd < 0)
792 goto file_free_out;
793
794 data_fd = open(data, O_WRONLY);
795 if(data_fd < 0)
796 goto loading_close_out;
797
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700798try_loading_again:
Brian Swetland02863b92010-09-19 03:36:39 -0700799 fw_fd = open(file1, O_RDONLY);
800 if(fw_fd < 0) {
801 fw_fd = open(file2, O_RDONLY);
Benoit Goby609d8822010-11-09 18:10:24 -0800802 if (fw_fd < 0) {
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700803 fw_fd = open(file3, O_RDONLY);
804 if (fw_fd < 0) {
805 if (booting) {
806 /* If we're not fully booted, we may be missing
807 * filesystems needed for firmware, wait and retry.
808 */
809 usleep(100000);
810 booting = is_booting();
811 goto try_loading_again;
812 }
813 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
814 write(loading_fd, "-1", 2);
815 goto data_close_out;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700816 }
Benoit Goby609d8822010-11-09 18:10:24 -0800817 }
Brian Swetland02863b92010-09-19 03:36:39 -0700818 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700819
820 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700821 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700822 else
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700823 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700824
825 close(fw_fd);
826data_close_out:
827 close(data_fd);
828loading_close_out:
829 close(loading_fd);
830file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700831 free(file1);
832 free(file2);
Ajay Dudani76c58892013-06-12 15:55:26 -0700833 free(file3);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700834data_free_out:
835 free(data);
836loading_free_out:
837 free(loading);
838root_free_out:
839 free(root);
840}
841
842static void handle_firmware_event(struct uevent *uevent)
843{
844 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700845 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700846
847 if(strcmp(uevent->subsystem, "firmware"))
848 return;
849
850 if(strcmp(uevent->action, "add"))
851 return;
852
853 /* we fork, to avoid making large memory allocations in init proper */
854 pid = fork();
855 if (!pid) {
856 process_firmware_event(uevent);
857 exit(EXIT_SUCCESS);
858 }
859}
860
861#define UEVENT_MSG_LEN 1024
Colin Cross0dd7ca62010-04-13 19:25:51 -0700862void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700863{
Vernon Tang3f582e92011-04-25 13:08:17 +1000864 char msg[UEVENT_MSG_LEN+2];
865 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700866 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700867 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700868 continue;
869
870 msg[n] = '\0';
871 msg[n+1] = '\0';
872
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700873 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700874 parse_event(msg, &uevent);
875
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400876 if (sehandle && selinux_status_updated() > 0) {
877 struct selabel_handle *sehandle2;
878 sehandle2 = selinux_android_file_context_handle();
879 if (sehandle2) {
880 selabel_close(sehandle);
881 sehandle = sehandle2;
882 }
883 }
884
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700885 handle_device_event(&uevent);
886 handle_firmware_event(&uevent);
887 }
888}
889
890/* Coldboot walks parts of the /sys tree and pokes the uevent files
891** to cause the kernel to regenerate device add events that happened
892** before init's device manager was started
893**
894** We drain any pending events from the netlink socket every time
895** we poke another uevent file to make sure we don't overrun the
896** socket's buffer.
897*/
898
Colin Cross0dd7ca62010-04-13 19:25:51 -0700899static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700900{
901 struct dirent *de;
902 int dfd, fd;
903
904 dfd = dirfd(d);
905
906 fd = openat(dfd, "uevent", O_WRONLY);
907 if(fd >= 0) {
908 write(fd, "add\n", 4);
909 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700910 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700911 }
912
913 while((de = readdir(d))) {
914 DIR *d2;
915
916 if(de->d_type != DT_DIR || de->d_name[0] == '.')
917 continue;
918
919 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
920 if(fd < 0)
921 continue;
922
923 d2 = fdopendir(fd);
924 if(d2 == 0)
925 close(fd);
926 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700927 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700928 closedir(d2);
929 }
930 }
931}
932
Colin Cross0dd7ca62010-04-13 19:25:51 -0700933static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700934{
935 DIR *d = opendir(path);
936 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700937 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700938 closedir(d);
939 }
940}
941
Colin Cross0dd7ca62010-04-13 19:25:51 -0700942void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700943{
944 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -0700945 struct stat info;
946 int fd;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700947
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400948 sehandle = NULL;
949 if (is_selinux_enabled() > 0) {
950 sehandle = selinux_android_file_context_handle();
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400951 selinux_status_open(true);
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400952 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700953
Andrew Boied562ca72012-12-04 15:47:20 -0800954 /* is 256K enough? udev uses 16MB! */
955 device_fd = uevent_open_socket(256*1024, true);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700956 if(device_fd < 0)
957 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700958
Colin Cross0dd7ca62010-04-13 19:25:51 -0700959 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
960 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700961
Colin Crossf83d0b92010-04-21 12:04:20 -0700962 if (stat(coldboot_done, &info) < 0) {
963 t0 = get_usecs();
964 coldboot("/sys/class");
965 coldboot("/sys/block");
966 coldboot("/sys/devices");
967 t1 = get_usecs();
968 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
969 close(fd);
970 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
971 } else {
972 log_event_print("skipping coldboot, already done\n");
973 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700974}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700975
Colin Cross0dd7ca62010-04-13 19:25:51 -0700976int get_device_fd()
977{
978 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700979}