blob: 5d7ad3b255de0448dc83756a93bd60736ebec644 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
Mark Salyzyn154f4602014-02-20 14:59:07 -08002 * Copyright (C) 2007-2014 The Android Open Source Project
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
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
Mark Salyzyn154f4602014-02-20 14:59:07 -080050#define UNUSED __attribute__((__unused__))
51
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070052#define SYSFS_PREFIX "/sys"
Brian Swetland02863b92010-09-19 03:36:39 -070053#define FIRMWARE_DIR1 "/etc/firmware"
54#define FIRMWARE_DIR2 "/vendor/firmware"
Iliyan Malchev029d44e2012-06-08 10:42:26 -070055#define FIRMWARE_DIR3 "/firmware/image"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070056
Stephen Smalleye096e362012-06-11 13:37:39 -040057extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050058
Colin Cross0dd7ca62010-04-13 19:25:51 -070059static int device_fd = -1;
60
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070061struct uevent {
62 const char *action;
63 const char *path;
64 const char *subsystem;
65 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070066 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070067 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070068 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069 int major;
70 int minor;
71};
72
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073struct perms_ {
74 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070075 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076 mode_t perm;
77 unsigned int uid;
78 unsigned int gid;
79 unsigned short prefix;
80};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070081
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070082struct perm_node {
83 struct perms_ dp;
84 struct listnode plist;
85};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070086
Colin Crossfadb85e2011-03-30 18:32:12 -070087struct platform_node {
88 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080089 char *path;
90 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070091 struct listnode list;
92};
93
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070094static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070095static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070096static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070097
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070098int add_dev_perms(const char *name, const char *attr,
99 mode_t perm, unsigned int uid, unsigned int gid,
100 unsigned short prefix) {
101 struct perm_node *node = calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700102 if (!node)
103 return -ENOMEM;
104
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700105 node->dp.name = strdup(name);
106 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700107 return -ENOMEM;
108
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700109 if (attr) {
110 node->dp.attr = strdup(attr);
111 if (!node->dp.attr)
112 return -ENOMEM;
113 }
114
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700115 node->dp.perm = perm;
116 node->dp.uid = uid;
117 node->dp.gid = gid;
118 node->dp.prefix = prefix;
119
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700120 if (attr)
121 list_add_tail(&sys_perms, &node->plist);
122 else
123 list_add_tail(&dev_perms, &node->plist);
124
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700125 return 0;
126}
127
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700128void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700129{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700130 char buf[512];
131 struct listnode *node;
132 struct perms_ *dp;
Stephen Smalley5f7b0172013-04-03 13:15:01 -0400133 char *secontext;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700134
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700135 /* upaths omit the "/sys" that paths in this list
136 * contain, so we add 4 when comparing...
137 */
138 list_for_each(node, &sys_perms) {
139 dp = &(node_to_item(node, struct perm_node, plist))->dp;
140 if (dp->prefix) {
141 if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700142 continue;
143 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700144 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700145 continue;
146 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700147
148 if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
149 return;
150
151 sprintf(buf,"/sys%s/%s", upath, dp->attr);
152 INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
153 chown(buf, dp->uid, dp->gid);
154 chmod(buf, dp->perm);
Stephen Smalley5f7b0172013-04-03 13:15:01 -0400155 if (sehandle) {
156 secontext = NULL;
157 selabel_lookup(sehandle, &secontext, buf, 0);
158 if (secontext) {
159 setfilecon(buf, secontext);
160 freecon(secontext);
161 }
162 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700163 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700164}
165
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700166static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
167{
168 mode_t perm;
Colin Cross44b65d02010-04-20 14:32:50 -0700169 struct listnode *node;
170 struct perm_node *perm_node;
171 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700172
Colin Cross44b65d02010-04-20 14:32:50 -0700173 /* search the perms list in reverse so that ueventd.$hardware can
174 * override ueventd.rc
175 */
176 list_for_each_reverse(node, &dev_perms) {
177 perm_node = node_to_item(node, struct perm_node, plist);
178 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700179
Colin Cross44b65d02010-04-20 14:32:50 -0700180 if (dp->prefix) {
181 if (strncmp(path, dp->name, strlen(dp->name)))
182 continue;
183 } else {
184 if (strcmp(path, dp->name))
185 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700186 }
Colin Cross44b65d02010-04-20 14:32:50 -0700187 *uid = dp->uid;
188 *gid = dp->gid;
189 return dp->perm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700190 }
Colin Cross44b65d02010-04-20 14:32:50 -0700191 /* Default if nothing found. */
192 *uid = 0;
193 *gid = 0;
194 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700195}
196
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700197static void make_device(const char *path,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800198 const char *upath UNUSED,
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700199 int block, int major, int minor)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700200{
201 unsigned uid;
202 unsigned gid;
203 mode_t mode;
204 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500205 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700206
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700207 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700208
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500209 if (sehandle) {
210 selabel_lookup(sehandle, &secontext, path, mode);
211 setfscreatecon(secontext);
212 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700213
Colin Cross17dcc5c2010-09-03 12:25:34 -0700214 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800215 /* Temporarily change egid to avoid race condition setting the gid of the
216 * device node. Unforunately changing the euid would prevent creation of
217 * some device nodes, so the uid has to be set with chown() and is still
218 * racy. Fixing the gid race at least fixed the issue with system_server
219 * opening dynamic input devices under the AID_INPUT gid. */
220 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700221 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800222 chown(path, uid, -1);
223 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700224
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500225 if (secontext) {
226 freecon(secontext);
227 setfscreatecon(NULL);
228 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700229}
230
Dima Zavinf395c922013-03-06 16:23:57 -0800231static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700232{
Dima Zavinf395c922013-03-06 16:23:57 -0800233 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700234 struct listnode *node;
235 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800236 const char *name = path;
237
238 if (!strncmp(path, "/devices/", 9)) {
239 name += 9;
240 if (!strncmp(name, "platform/", 9))
241 name += 9;
242 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700243
244 list_for_each_reverse(node, &platform_names) {
245 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800246 if ((bus->path_len < path_len) &&
247 (path[bus->path_len] == '/') &&
248 !strncmp(path, bus->path, bus->path_len))
Colin Crossfadb85e2011-03-30 18:32:12 -0700249 /* subdevice of an existing platform, ignore it */
250 return;
251 }
252
Dima Zavinf395c922013-03-06 16:23:57 -0800253 INFO("adding platform device %s (%s)\n", name, path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700254
255 bus = calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800256 bus->path = strdup(path);
257 bus->path_len = path_len;
258 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700259 list_add_tail(&platform_names, &bus->list);
260}
261
262/*
Dima Zavinf395c922013-03-06 16:23:57 -0800263 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700264 * platform device prefix. If it doesn't start with a platform device, return
265 * 0.
266 */
Dima Zavinf395c922013-03-06 16:23:57 -0800267static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700268{
Dima Zavinf395c922013-03-06 16:23:57 -0800269 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700270 struct listnode *node;
271 struct platform_node *bus;
272
273 list_for_each_reverse(node, &platform_names) {
274 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800275 if ((bus->path_len < path_len) &&
276 (path[bus->path_len] == '/') &&
277 !strncmp(path, bus->path, bus->path_len))
278 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700279 }
280
281 return NULL;
282}
283
Dima Zavinf395c922013-03-06 16:23:57 -0800284static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700285{
286 struct listnode *node;
287 struct platform_node *bus;
288
289 list_for_each_reverse(node, &platform_names) {
290 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800291 if (!strcmp(path, bus->path)) {
292 INFO("removing platform device %s\n", bus->name);
293 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700294 list_remove(node);
295 free(bus);
296 return;
297 }
298 }
299}
300
Chuck Tuffli1e070842008-12-15 14:26:56 -0800301#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700302
303static inline suseconds_t get_usecs(void)
304{
305 struct timeval tv;
306 gettimeofday(&tv, 0);
307 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
308}
309
310#define log_event_print(x...) INFO(x)
311
312#else
313
314#define log_event_print(fmt, args...) do { } while (0)
315#define get_usecs() 0
316
317#endif
318
319static void parse_event(const char *msg, struct uevent *uevent)
320{
321 uevent->action = "";
322 uevent->path = "";
323 uevent->subsystem = "";
324 uevent->firmware = "";
325 uevent->major = -1;
326 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700327 uevent->partition_name = NULL;
328 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700329 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700330
331 /* currently ignoring SEQNUM */
332 while(*msg) {
333 if(!strncmp(msg, "ACTION=", 7)) {
334 msg += 7;
335 uevent->action = msg;
336 } else if(!strncmp(msg, "DEVPATH=", 8)) {
337 msg += 8;
338 uevent->path = msg;
339 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
340 msg += 10;
341 uevent->subsystem = msg;
342 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
343 msg += 9;
344 uevent->firmware = msg;
345 } else if(!strncmp(msg, "MAJOR=", 6)) {
346 msg += 6;
347 uevent->major = atoi(msg);
348 } else if(!strncmp(msg, "MINOR=", 6)) {
349 msg += 6;
350 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700351 } else if(!strncmp(msg, "PARTN=", 6)) {
352 msg += 6;
353 uevent->partition_num = atoi(msg);
354 } else if(!strncmp(msg, "PARTNAME=", 9)) {
355 msg += 9;
356 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700357 } else if(!strncmp(msg, "DEVNAME=", 8)) {
358 msg += 8;
359 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700360 }
361
Wei Zhongf97b8872012-03-23 14:15:34 -0700362 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700363 while(*msg++)
364 ;
365 }
366
367 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
368 uevent->action, uevent->path, uevent->subsystem,
369 uevent->firmware, uevent->major, uevent->minor);
370}
371
Benoit Gobyd2278632010-08-03 14:36:02 -0700372static char **get_character_device_symlinks(struct uevent *uevent)
373{
374 const char *parent;
375 char *slash;
376 char **links;
377 int link_num = 0;
378 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800379 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700380
Dima Zavinf395c922013-03-06 16:23:57 -0800381 pdev = find_platform_device(uevent->path);
382 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700383 return NULL;
384
385 links = malloc(sizeof(char *) * 2);
386 if (!links)
387 return NULL;
388 memset(links, 0, sizeof(char *) * 2);
389
390 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800391 parent = strchr(uevent->path + pdev->path_len, '/');
Benoit Gobyd2278632010-08-03 14:36:02 -0700392 if (!*parent)
393 goto err;
394
395 if (!strncmp(parent, "/usb", 4)) {
396 /* skip root hub name and device. use device interface */
397 while (*++parent && *parent != '/');
398 if (*parent)
399 while (*++parent && *parent != '/');
400 if (!*parent)
401 goto err;
402 slash = strchr(++parent, '/');
403 if (!slash)
404 goto err;
405 width = slash - parent;
406 if (width <= 0)
407 goto err;
408
409 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
410 link_num++;
411 else
412 links[link_num] = NULL;
413 mkdir("/dev/usb", 0755);
414 }
415 else {
416 goto err;
417 }
418
419 return links;
420err:
421 free(links);
422 return NULL;
423}
424
Colin Crossb0ab94b2010-04-08 16:16:20 -0700425static char **parse_platform_block_device(struct uevent *uevent)
426{
Colin Crossfadb85e2011-03-30 18:32:12 -0700427 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800428 struct platform_node *pdev;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700429 char *slash;
430 int width;
431 char buf[256];
432 char link_path[256];
433 int fd;
434 int link_num = 0;
435 int ret;
436 char *p;
437 unsigned int size;
438 struct stat info;
439
Dima Zavinf395c922013-03-06 16:23:57 -0800440 pdev = find_platform_device(uevent->path);
441 if (!pdev)
442 return NULL;
443 device = pdev->name;
444
Colin Crossb0ab94b2010-04-08 16:16:20 -0700445 char **links = malloc(sizeof(char *) * 4);
446 if (!links)
447 return NULL;
448 memset(links, 0, sizeof(char *) * 4);
449
Colin Crossfadb85e2011-03-30 18:32:12 -0700450 INFO("found platform device %s\n", device);
451
452 snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700453
454 if (uevent->partition_name) {
455 p = strdup(uevent->partition_name);
456 sanitize(p);
Johan Redestig93ca79b2012-04-18 16:41:19 +0200457 if (strcmp(uevent->partition_name, p))
458 NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700459 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
460 link_num++;
461 else
462 links[link_num] = NULL;
463 free(p);
464 }
465
466 if (uevent->partition_num >= 0) {
467 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
468 link_num++;
469 else
470 links[link_num] = NULL;
471 }
472
Dima Zavinf395c922013-03-06 16:23:57 -0800473 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700474 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
475 link_num++;
476 else
477 links[link_num] = NULL;
478
479 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700480}
481
Colin Crosseb5ba832011-03-30 17:37:17 -0700482static void handle_device(const char *action, const char *devpath,
483 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700484{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700485 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700486
Colin Crosseb5ba832011-03-30 17:37:17 -0700487 if(!strcmp(action, "add")) {
488 make_device(devpath, path, block, major, minor);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700489 if (links) {
490 for (i = 0; links[i]; i++)
491 make_link(devpath, links[i]);
492 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700493 }
494
Colin Crosseb5ba832011-03-30 17:37:17 -0700495 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700496 if (links) {
497 for (i = 0; links[i]; i++)
498 remove_link(devpath, links[i]);
499 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700500 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700501 }
502
503 if (links) {
504 for (i = 0; links[i]; i++)
505 free(links[i]);
506 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700507 }
508}
509
Colin Crossfadb85e2011-03-30 18:32:12 -0700510static void handle_platform_device_event(struct uevent *uevent)
511{
Dima Zavinf395c922013-03-06 16:23:57 -0800512 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700513
514 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800515 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700516 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800517 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700518}
519
Colin Crosseb5ba832011-03-30 17:37:17 -0700520static const char *parse_device_name(struct uevent *uevent, unsigned int len)
521{
522 const char *name;
523
524 /* if it's not a /dev device, nothing else to do */
525 if((uevent->major < 0) || (uevent->minor < 0))
526 return NULL;
527
528 /* do we have a name? */
529 name = strrchr(uevent->path, '/');
530 if(!name)
531 return NULL;
532 name++;
533
534 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800535 if(strlen(name) > len) {
536 ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n",
537 name, len);
Colin Crosseb5ba832011-03-30 17:37:17 -0700538 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800539 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700540
541 return name;
542}
543
544static void handle_block_device_event(struct uevent *uevent)
545{
546 const char *base = "/dev/block/";
547 const char *name;
548 char devpath[96];
549 char **links = NULL;
550
551 name = parse_device_name(uevent, 64);
552 if (!name)
553 return;
554
555 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500556 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700557
Dima Zavinf395c922013-03-06 16:23:57 -0800558 if (!strncmp(uevent->path, "/devices/", 9))
Colin Crosseb5ba832011-03-30 17:37:17 -0700559 links = parse_platform_block_device(uevent);
560
561 handle_device(uevent->action, devpath, uevent->path, 1,
562 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700563}
564
Greg Hackmann3312aa82013-11-18 15:24:40 -0800565#define DEVPATH_LEN 96
566
567static bool assemble_devpath(char *devpath, const char *dirname,
568 const char *devname)
569{
570 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
571 if (s < 0) {
572 ERROR("failed to assemble device path (%s); ignoring event\n",
573 strerror(errno));
574 return false;
575 } else if (s >= DEVPATH_LEN) {
576 ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n",
577 dirname, devname, DEVPATH_LEN);
578 return false;
579 }
580 return true;
581}
582
583static void mkdir_recursive_for_devpath(const char *devpath)
584{
585 char dir[DEVPATH_LEN];
586 char *slash;
587
588 strcpy(dir, devpath);
589 slash = strrchr(dir, '/');
590 *slash = '\0';
591 mkdir_recursive(dir, 0755);
592}
593
Mark Salyzyn154f4602014-02-20 14:59:07 -0800594static inline void __attribute__((__deprecated__)) kernel_logger()
595{
596 INFO("kernel logger is deprecated\n");
597}
598
Colin Crosseb5ba832011-03-30 17:37:17 -0700599static void handle_generic_device_event(struct uevent *uevent)
600{
601 char *base;
602 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800603 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700604 char **links = NULL;
605
606 name = parse_device_name(uevent, 64);
607 if (!name)
608 return;
609
Greg Hackmann3312aa82013-11-18 15:24:40 -0800610 struct ueventd_subsystem *subsystem =
611 ueventd_subsystem_find_by_name(uevent->subsystem);
612
613 if (subsystem) {
614 const char *devname;
615
616 switch (subsystem->devname_src) {
617 case DEVNAME_UEVENT_DEVNAME:
618 devname = uevent->device_name;
619 break;
620
621 case DEVNAME_UEVENT_DEVPATH:
622 devname = name;
623 break;
624
625 default:
626 ERROR("%s subsystem's devpath option is not set; ignoring event\n",
627 uevent->subsystem);
628 return;
629 }
630
631 if (!assemble_devpath(devpath, subsystem->dirname, devname))
632 return;
633 mkdir_recursive_for_devpath(devpath);
634 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700635 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700636 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800637 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800638 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800639 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700640 }
641 else {
642 /* This imitates the file system that would be created
643 * if we were using devfs instead.
644 * Minors are broken up into groups of 128, starting at "001"
645 */
646 int bus_id = uevent->minor / 128 + 1;
647 int device_id = uevent->minor % 128 + 1;
648 /* build directories */
649 make_dir("/dev/bus", 0755);
650 make_dir("/dev/bus/usb", 0755);
651 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
652 make_dir(devpath, 0755);
653 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
654 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700655 } else {
656 /* ignore other USB events */
657 return;
658 }
659 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
660 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500661 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200662 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
663 base = "/dev/dri/";
664 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700665 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
666 base = "/dev/oncrpc/";
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, "adsp", 4)) {
669 base = "/dev/adsp/";
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, "msm_camera", 10)) {
672 base = "/dev/msm_camera/";
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, "input", 5)) {
675 base = "/dev/input/";
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, "mtd", 3)) {
678 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500679 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700680 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
681 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500682 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700683 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
684 !strncmp(name, "log_", 4)) {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800685 kernel_logger();
Colin Crosseb5ba832011-03-30 17:37:17 -0700686 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500687 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700688 name += 4;
689 } else
690 base = "/dev/";
691 links = get_character_device_symlinks(uevent);
692
693 if (!devpath[0])
694 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
695
696 handle_device(uevent->action, devpath, uevent->path, 0,
697 uevent->major, uevent->minor, links);
698}
699
700static void handle_device_event(struct uevent *uevent)
701{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700702 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700703 fixup_sys_perms(uevent->path);
704
705 if (!strncmp(uevent->subsystem, "block", 5)) {
706 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700707 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
708 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700709 } else {
710 handle_generic_device_event(uevent);
711 }
712}
713
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700714static int load_firmware(int fw_fd, int loading_fd, int data_fd)
715{
716 struct stat st;
717 long len_to_copy;
718 int ret = 0;
719
720 if(fstat(fw_fd, &st) < 0)
721 return -1;
722 len_to_copy = st.st_size;
723
724 write(loading_fd, "1", 1); /* start transfer */
725
726 while (len_to_copy > 0) {
727 char buf[PAGE_SIZE];
728 ssize_t nr;
729
730 nr = read(fw_fd, buf, sizeof(buf));
731 if(!nr)
732 break;
733 if(nr < 0) {
734 ret = -1;
735 break;
736 }
737
738 len_to_copy -= nr;
739 while (nr > 0) {
740 ssize_t nw = 0;
741
742 nw = write(data_fd, buf + nw, nr);
743 if(nw <= 0) {
744 ret = -1;
745 goto out;
746 }
747 nr -= nw;
748 }
749 }
750
751out:
752 if(!ret)
753 write(loading_fd, "0", 1); /* successful end of transfer */
754 else
755 write(loading_fd, "-1", 2); /* abort transfer */
756
757 return ret;
758}
759
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700760static int is_booting(void)
761{
762 return access("/dev/.booting", F_OK) == 0;
763}
764
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700765static void process_firmware_event(struct uevent *uevent)
766{
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700767 char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700768 int l, loading_fd, data_fd, fw_fd;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700769 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700770
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700771 INFO("firmware: loading '%s' for '%s'\n",
772 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700773
774 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
775 if (l == -1)
776 return;
777
778 l = asprintf(&loading, "%sloading", root);
779 if (l == -1)
780 goto root_free_out;
781
782 l = asprintf(&data, "%sdata", root);
783 if (l == -1)
784 goto loading_free_out;
785
Brian Swetland02863b92010-09-19 03:36:39 -0700786 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
787 if (l == -1)
788 goto data_free_out;
789
790 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700791 if (l == -1)
792 goto data_free_out;
793
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700794 l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware);
795 if (l == -1)
796 goto data_free_out;
797
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700798 loading_fd = open(loading, O_WRONLY);
799 if(loading_fd < 0)
800 goto file_free_out;
801
802 data_fd = open(data, O_WRONLY);
803 if(data_fd < 0)
804 goto loading_close_out;
805
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700806try_loading_again:
Brian Swetland02863b92010-09-19 03:36:39 -0700807 fw_fd = open(file1, O_RDONLY);
808 if(fw_fd < 0) {
809 fw_fd = open(file2, O_RDONLY);
Benoit Goby609d8822010-11-09 18:10:24 -0800810 if (fw_fd < 0) {
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700811 fw_fd = open(file3, O_RDONLY);
812 if (fw_fd < 0) {
813 if (booting) {
814 /* If we're not fully booted, we may be missing
815 * filesystems needed for firmware, wait and retry.
816 */
817 usleep(100000);
818 booting = is_booting();
819 goto try_loading_again;
820 }
821 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
822 write(loading_fd, "-1", 2);
823 goto data_close_out;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700824 }
Benoit Goby609d8822010-11-09 18:10:24 -0800825 }
Brian Swetland02863b92010-09-19 03:36:39 -0700826 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700827
828 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700829 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700830 else
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700831 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700832
833 close(fw_fd);
834data_close_out:
835 close(data_fd);
836loading_close_out:
837 close(loading_fd);
838file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700839 free(file1);
840 free(file2);
Ajay Dudani76c58892013-06-12 15:55:26 -0700841 free(file3);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700842data_free_out:
843 free(data);
844loading_free_out:
845 free(loading);
846root_free_out:
847 free(root);
848}
849
850static void handle_firmware_event(struct uevent *uevent)
851{
852 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700853 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700854
855 if(strcmp(uevent->subsystem, "firmware"))
856 return;
857
858 if(strcmp(uevent->action, "add"))
859 return;
860
861 /* we fork, to avoid making large memory allocations in init proper */
862 pid = fork();
863 if (!pid) {
864 process_firmware_event(uevent);
865 exit(EXIT_SUCCESS);
866 }
867}
868
869#define UEVENT_MSG_LEN 1024
Colin Cross0dd7ca62010-04-13 19:25:51 -0700870void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700871{
Vernon Tang3f582e92011-04-25 13:08:17 +1000872 char msg[UEVENT_MSG_LEN+2];
873 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700874 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700875 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700876 continue;
877
878 msg[n] = '\0';
879 msg[n+1] = '\0';
880
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700881 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700882 parse_event(msg, &uevent);
883
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400884 if (sehandle && selinux_status_updated() > 0) {
885 struct selabel_handle *sehandle2;
886 sehandle2 = selinux_android_file_context_handle();
887 if (sehandle2) {
888 selabel_close(sehandle);
889 sehandle = sehandle2;
890 }
891 }
892
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700893 handle_device_event(&uevent);
894 handle_firmware_event(&uevent);
895 }
896}
897
898/* Coldboot walks parts of the /sys tree and pokes the uevent files
899** to cause the kernel to regenerate device add events that happened
900** before init's device manager was started
901**
902** We drain any pending events from the netlink socket every time
903** we poke another uevent file to make sure we don't overrun the
904** socket's buffer.
905*/
906
Colin Cross0dd7ca62010-04-13 19:25:51 -0700907static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700908{
909 struct dirent *de;
910 int dfd, fd;
911
912 dfd = dirfd(d);
913
914 fd = openat(dfd, "uevent", O_WRONLY);
915 if(fd >= 0) {
916 write(fd, "add\n", 4);
917 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700918 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700919 }
920
921 while((de = readdir(d))) {
922 DIR *d2;
923
924 if(de->d_type != DT_DIR || de->d_name[0] == '.')
925 continue;
926
927 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
928 if(fd < 0)
929 continue;
930
931 d2 = fdopendir(fd);
932 if(d2 == 0)
933 close(fd);
934 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700935 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700936 closedir(d2);
937 }
938 }
939}
940
Colin Cross0dd7ca62010-04-13 19:25:51 -0700941static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700942{
943 DIR *d = opendir(path);
944 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700945 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700946 closedir(d);
947 }
948}
949
Colin Cross0dd7ca62010-04-13 19:25:51 -0700950void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700951{
952 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -0700953 struct stat info;
954 int fd;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700955
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400956 sehandle = NULL;
957 if (is_selinux_enabled() > 0) {
958 sehandle = selinux_android_file_context_handle();
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400959 selinux_status_open(true);
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400960 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700961
Andrew Boied562ca72012-12-04 15:47:20 -0800962 /* is 256K enough? udev uses 16MB! */
963 device_fd = uevent_open_socket(256*1024, true);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700964 if(device_fd < 0)
965 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700966
Colin Cross0dd7ca62010-04-13 19:25:51 -0700967 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
968 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700969
Colin Crossf83d0b92010-04-21 12:04:20 -0700970 if (stat(coldboot_done, &info) < 0) {
971 t0 = get_usecs();
972 coldboot("/sys/class");
973 coldboot("/sys/block");
974 coldboot("/sys/devices");
975 t1 = get_usecs();
976 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
977 close(fd);
978 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
979 } else {
980 log_event_print("skipping coldboot, already done\n");
981 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700982}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700983
Colin Cross0dd7ca62010-04-13 19:25:51 -0700984int get_device_fd()
985{
986 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700987}