The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 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 Bailly | b93e581 | 2010-11-17 11:47:23 -0800 | [diff] [blame] | 18 | #include <stddef.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | #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 Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 32 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 33 | #include <selinux/selinux.h> |
| 34 | #include <selinux/label.h> |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 35 | #include <selinux/android.h> |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 36 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 37 | #include <private/android_filesystem_config.h> |
| 38 | #include <sys/time.h> |
| 39 | #include <asm/page.h> |
Colin Cross | 982a815 | 2010-06-03 12:21:01 -0700 | [diff] [blame] | 40 | #include <sys/wait.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 41 | |
Dima Zavin | da04c52 | 2011-09-01 17:09:44 -0700 | [diff] [blame] | 42 | #include <cutils/list.h> |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 43 | #include <cutils/uevent.h> |
| 44 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 45 | #include "devices.h" |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 46 | #include "util.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 47 | #include "log.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 48 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 49 | #define SYSFS_PREFIX "/sys" |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 50 | #define FIRMWARE_DIR1 "/etc/firmware" |
| 51 | #define FIRMWARE_DIR2 "/vendor/firmware" |
Iliyan Malchev | 029d44e | 2012-06-08 10:42:26 -0700 | [diff] [blame] | 52 | #define FIRMWARE_DIR3 "/firmware/image" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 53 | |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 54 | extern struct selabel_handle *sehandle; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 55 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 56 | static int device_fd = -1; |
| 57 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 58 | struct uevent { |
| 59 | const char *action; |
| 60 | const char *path; |
| 61 | const char *subsystem; |
| 62 | const char *firmware; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 63 | const char *partition_name; |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 64 | const char *device_name; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 65 | int partition_num; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 66 | int major; |
| 67 | int minor; |
| 68 | }; |
| 69 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 70 | struct perms_ { |
| 71 | char *name; |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 72 | char *attr; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 73 | mode_t perm; |
| 74 | unsigned int uid; |
| 75 | unsigned int gid; |
| 76 | unsigned short prefix; |
| 77 | }; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 78 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 79 | struct perm_node { |
| 80 | struct perms_ dp; |
| 81 | struct listnode plist; |
| 82 | }; |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 83 | |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 84 | struct platform_node { |
| 85 | char *name; |
| 86 | int name_len; |
| 87 | struct listnode list; |
| 88 | }; |
| 89 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 90 | static list_declare(sys_perms); |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 91 | static list_declare(dev_perms); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 92 | static list_declare(platform_names); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 93 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 94 | int add_dev_perms(const char *name, const char *attr, |
| 95 | mode_t perm, unsigned int uid, unsigned int gid, |
| 96 | unsigned short prefix) { |
| 97 | struct perm_node *node = calloc(1, sizeof(*node)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 98 | if (!node) |
| 99 | return -ENOMEM; |
| 100 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 101 | node->dp.name = strdup(name); |
| 102 | if (!node->dp.name) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 103 | return -ENOMEM; |
| 104 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 105 | if (attr) { |
| 106 | node->dp.attr = strdup(attr); |
| 107 | if (!node->dp.attr) |
| 108 | return -ENOMEM; |
| 109 | } |
| 110 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 111 | node->dp.perm = perm; |
| 112 | node->dp.uid = uid; |
| 113 | node->dp.gid = gid; |
| 114 | node->dp.prefix = prefix; |
| 115 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 116 | if (attr) |
| 117 | list_add_tail(&sys_perms, &node->plist); |
| 118 | else |
| 119 | list_add_tail(&dev_perms, &node->plist); |
| 120 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 124 | void fixup_sys_perms(const char *upath) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 125 | { |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 126 | char buf[512]; |
| 127 | struct listnode *node; |
| 128 | struct perms_ *dp; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 129 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 130 | /* upaths omit the "/sys" that paths in this list |
| 131 | * contain, so we add 4 when comparing... |
| 132 | */ |
| 133 | list_for_each(node, &sys_perms) { |
| 134 | dp = &(node_to_item(node, struct perm_node, plist))->dp; |
| 135 | if (dp->prefix) { |
| 136 | if (strncmp(upath, dp->name + 4, strlen(dp->name + 4))) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 137 | continue; |
| 138 | } else { |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 139 | if (strcmp(upath, dp->name + 4)) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 140 | continue; |
| 141 | } |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 142 | |
| 143 | if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf)) |
| 144 | return; |
| 145 | |
| 146 | sprintf(buf,"/sys%s/%s", upath, dp->attr); |
| 147 | INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm); |
| 148 | chown(buf, dp->uid, dp->gid); |
| 149 | chmod(buf, dp->perm); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 150 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 151 | } |
| 152 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 153 | static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid) |
| 154 | { |
| 155 | mode_t perm; |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 156 | struct listnode *node; |
| 157 | struct perm_node *perm_node; |
| 158 | struct perms_ *dp; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 159 | |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 160 | /* search the perms list in reverse so that ueventd.$hardware can |
| 161 | * override ueventd.rc |
| 162 | */ |
| 163 | list_for_each_reverse(node, &dev_perms) { |
| 164 | perm_node = node_to_item(node, struct perm_node, plist); |
| 165 | dp = &perm_node->dp; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 166 | |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 167 | if (dp->prefix) { |
| 168 | if (strncmp(path, dp->name, strlen(dp->name))) |
| 169 | continue; |
| 170 | } else { |
| 171 | if (strcmp(path, dp->name)) |
| 172 | continue; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 173 | } |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 174 | *uid = dp->uid; |
| 175 | *gid = dp->gid; |
| 176 | return dp->perm; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 177 | } |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 178 | /* Default if nothing found. */ |
| 179 | *uid = 0; |
| 180 | *gid = 0; |
| 181 | return 0600; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 184 | static void make_device(const char *path, |
| 185 | const char *upath, |
| 186 | int block, int major, int minor) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 187 | { |
| 188 | unsigned uid; |
| 189 | unsigned gid; |
| 190 | mode_t mode; |
| 191 | dev_t dev; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 192 | char *secontext = NULL; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 193 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 194 | mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR); |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 195 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 196 | if (sehandle) { |
| 197 | selabel_lookup(sehandle, &secontext, path, mode); |
| 198 | setfscreatecon(secontext); |
| 199 | } |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 200 | |
Colin Cross | 17dcc5c | 2010-09-03 12:25:34 -0700 | [diff] [blame] | 201 | dev = makedev(major, minor); |
Nick Pelly | 6405c69 | 2010-01-21 18:13:39 -0800 | [diff] [blame] | 202 | /* Temporarily change egid to avoid race condition setting the gid of the |
| 203 | * device node. Unforunately changing the euid would prevent creation of |
| 204 | * some device nodes, so the uid has to be set with chown() and is still |
| 205 | * racy. Fixing the gid race at least fixed the issue with system_server |
| 206 | * opening dynamic input devices under the AID_INPUT gid. */ |
| 207 | setegid(gid); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 208 | mknod(path, mode, dev); |
Nick Pelly | 6405c69 | 2010-01-21 18:13:39 -0800 | [diff] [blame] | 209 | chown(path, uid, -1); |
| 210 | setegid(AID_ROOT); |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 211 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 212 | if (secontext) { |
| 213 | freecon(secontext); |
| 214 | setfscreatecon(NULL); |
| 215 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 218 | static void add_platform_device(const char *name) |
| 219 | { |
| 220 | int name_len = strlen(name); |
| 221 | struct listnode *node; |
| 222 | struct platform_node *bus; |
| 223 | |
| 224 | list_for_each_reverse(node, &platform_names) { |
| 225 | bus = node_to_item(node, struct platform_node, list); |
| 226 | if ((bus->name_len < name_len) && |
| 227 | (name[bus->name_len] == '/') && |
| 228 | !strncmp(name, bus->name, bus->name_len)) |
| 229 | /* subdevice of an existing platform, ignore it */ |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | INFO("adding platform device %s\n", name); |
| 234 | |
| 235 | bus = calloc(1, sizeof(struct platform_node)); |
| 236 | bus->name = strdup(name); |
| 237 | bus->name_len = name_len; |
| 238 | list_add_tail(&platform_names, &bus->list); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * given a name that may start with a platform device, find the length of the |
| 243 | * platform device prefix. If it doesn't start with a platform device, return |
| 244 | * 0. |
| 245 | */ |
| 246 | static const char *find_platform_device(const char *name) |
| 247 | { |
| 248 | int name_len = strlen(name); |
| 249 | struct listnode *node; |
| 250 | struct platform_node *bus; |
| 251 | |
| 252 | list_for_each_reverse(node, &platform_names) { |
| 253 | bus = node_to_item(node, struct platform_node, list); |
| 254 | if ((bus->name_len < name_len) && |
| 255 | (name[bus->name_len] == '/') && |
| 256 | !strncmp(name, bus->name, bus->name_len)) |
| 257 | return bus->name; |
| 258 | } |
| 259 | |
| 260 | return NULL; |
| 261 | } |
| 262 | |
| 263 | static void remove_platform_device(const char *name) |
| 264 | { |
| 265 | struct listnode *node; |
| 266 | struct platform_node *bus; |
| 267 | |
| 268 | list_for_each_reverse(node, &platform_names) { |
| 269 | bus = node_to_item(node, struct platform_node, list); |
| 270 | if (!strcmp(name, bus->name)) { |
| 271 | INFO("removing platform device %s\n", name); |
| 272 | free(bus->name); |
| 273 | list_remove(node); |
| 274 | free(bus); |
| 275 | return; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
Chuck Tuffli | 1e07084 | 2008-12-15 14:26:56 -0800 | [diff] [blame] | 280 | #if LOG_UEVENTS |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 281 | |
| 282 | static inline suseconds_t get_usecs(void) |
| 283 | { |
| 284 | struct timeval tv; |
| 285 | gettimeofday(&tv, 0); |
| 286 | return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec; |
| 287 | } |
| 288 | |
| 289 | #define log_event_print(x...) INFO(x) |
| 290 | |
| 291 | #else |
| 292 | |
| 293 | #define log_event_print(fmt, args...) do { } while (0) |
| 294 | #define get_usecs() 0 |
| 295 | |
| 296 | #endif |
| 297 | |
| 298 | static void parse_event(const char *msg, struct uevent *uevent) |
| 299 | { |
| 300 | uevent->action = ""; |
| 301 | uevent->path = ""; |
| 302 | uevent->subsystem = ""; |
| 303 | uevent->firmware = ""; |
| 304 | uevent->major = -1; |
| 305 | uevent->minor = -1; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 306 | uevent->partition_name = NULL; |
| 307 | uevent->partition_num = -1; |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 308 | uevent->device_name = NULL; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 309 | |
| 310 | /* currently ignoring SEQNUM */ |
| 311 | while(*msg) { |
| 312 | if(!strncmp(msg, "ACTION=", 7)) { |
| 313 | msg += 7; |
| 314 | uevent->action = msg; |
| 315 | } else if(!strncmp(msg, "DEVPATH=", 8)) { |
| 316 | msg += 8; |
| 317 | uevent->path = msg; |
| 318 | } else if(!strncmp(msg, "SUBSYSTEM=", 10)) { |
| 319 | msg += 10; |
| 320 | uevent->subsystem = msg; |
| 321 | } else if(!strncmp(msg, "FIRMWARE=", 9)) { |
| 322 | msg += 9; |
| 323 | uevent->firmware = msg; |
| 324 | } else if(!strncmp(msg, "MAJOR=", 6)) { |
| 325 | msg += 6; |
| 326 | uevent->major = atoi(msg); |
| 327 | } else if(!strncmp(msg, "MINOR=", 6)) { |
| 328 | msg += 6; |
| 329 | uevent->minor = atoi(msg); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 330 | } else if(!strncmp(msg, "PARTN=", 6)) { |
| 331 | msg += 6; |
| 332 | uevent->partition_num = atoi(msg); |
| 333 | } else if(!strncmp(msg, "PARTNAME=", 9)) { |
| 334 | msg += 9; |
| 335 | uevent->partition_name = msg; |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 336 | } else if(!strncmp(msg, "DEVNAME=", 8)) { |
| 337 | msg += 8; |
| 338 | uevent->device_name = msg; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 341 | /* advance to after the next \0 */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 342 | while(*msg++) |
| 343 | ; |
| 344 | } |
| 345 | |
| 346 | log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n", |
| 347 | uevent->action, uevent->path, uevent->subsystem, |
| 348 | uevent->firmware, uevent->major, uevent->minor); |
| 349 | } |
| 350 | |
Benoit Goby | d227863 | 2010-08-03 14:36:02 -0700 | [diff] [blame] | 351 | static char **get_character_device_symlinks(struct uevent *uevent) |
| 352 | { |
| 353 | const char *parent; |
| 354 | char *slash; |
| 355 | char **links; |
| 356 | int link_num = 0; |
| 357 | int width; |
| 358 | |
| 359 | if (strncmp(uevent->path, "/devices/platform/", 18)) |
| 360 | return NULL; |
| 361 | |
| 362 | links = malloc(sizeof(char *) * 2); |
| 363 | if (!links) |
| 364 | return NULL; |
| 365 | memset(links, 0, sizeof(char *) * 2); |
| 366 | |
| 367 | /* skip "/devices/platform/<driver>" */ |
| 368 | parent = strchr(uevent->path + 18, '/'); |
| 369 | if (!*parent) |
| 370 | goto err; |
| 371 | |
| 372 | if (!strncmp(parent, "/usb", 4)) { |
| 373 | /* skip root hub name and device. use device interface */ |
| 374 | while (*++parent && *parent != '/'); |
| 375 | if (*parent) |
| 376 | while (*++parent && *parent != '/'); |
| 377 | if (!*parent) |
| 378 | goto err; |
| 379 | slash = strchr(++parent, '/'); |
| 380 | if (!slash) |
| 381 | goto err; |
| 382 | width = slash - parent; |
| 383 | if (width <= 0) |
| 384 | goto err; |
| 385 | |
| 386 | if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0) |
| 387 | link_num++; |
| 388 | else |
| 389 | links[link_num] = NULL; |
| 390 | mkdir("/dev/usb", 0755); |
| 391 | } |
| 392 | else { |
| 393 | goto err; |
| 394 | } |
| 395 | |
| 396 | return links; |
| 397 | err: |
| 398 | free(links); |
| 399 | return NULL; |
| 400 | } |
| 401 | |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 402 | static char **parse_platform_block_device(struct uevent *uevent) |
| 403 | { |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 404 | const char *device; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 405 | const char *path; |
| 406 | char *slash; |
| 407 | int width; |
| 408 | char buf[256]; |
| 409 | char link_path[256]; |
| 410 | int fd; |
| 411 | int link_num = 0; |
| 412 | int ret; |
| 413 | char *p; |
| 414 | unsigned int size; |
| 415 | struct stat info; |
| 416 | |
| 417 | char **links = malloc(sizeof(char *) * 4); |
| 418 | if (!links) |
| 419 | return NULL; |
| 420 | memset(links, 0, sizeof(char *) * 4); |
| 421 | |
| 422 | /* Drop "/devices/platform/" */ |
| 423 | path = uevent->path; |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 424 | device = path + 18; |
| 425 | device = find_platform_device(device); |
| 426 | if (!device) |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 427 | goto err; |
| 428 | |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 429 | INFO("found platform device %s\n", device); |
| 430 | |
| 431 | snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 432 | |
| 433 | if (uevent->partition_name) { |
| 434 | p = strdup(uevent->partition_name); |
| 435 | sanitize(p); |
| 436 | if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0) |
| 437 | link_num++; |
| 438 | else |
| 439 | links[link_num] = NULL; |
| 440 | free(p); |
| 441 | } |
| 442 | |
| 443 | if (uevent->partition_num >= 0) { |
| 444 | if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0) |
| 445 | link_num++; |
| 446 | else |
| 447 | links[link_num] = NULL; |
| 448 | } |
| 449 | |
| 450 | slash = strrchr(path, '/'); |
| 451 | if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0) |
| 452 | link_num++; |
| 453 | else |
| 454 | links[link_num] = NULL; |
| 455 | |
| 456 | return links; |
| 457 | |
| 458 | err: |
| 459 | free(links); |
| 460 | return NULL; |
| 461 | } |
| 462 | |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 463 | static void handle_device(const char *action, const char *devpath, |
| 464 | const char *path, int block, int major, int minor, char **links) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 465 | { |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 466 | int i; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 467 | |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 468 | if(!strcmp(action, "add")) { |
| 469 | make_device(devpath, path, block, major, minor); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 470 | if (links) { |
| 471 | for (i = 0; links[i]; i++) |
| 472 | make_link(devpath, links[i]); |
| 473 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 476 | if(!strcmp(action, "remove")) { |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 477 | if (links) { |
| 478 | for (i = 0; links[i]; i++) |
| 479 | remove_link(devpath, links[i]); |
| 480 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 481 | unlink(devpath); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | if (links) { |
| 485 | for (i = 0; links[i]; i++) |
| 486 | free(links[i]); |
| 487 | free(links); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 491 | static void handle_platform_device_event(struct uevent *uevent) |
| 492 | { |
| 493 | const char *name = uevent->path + 18; /* length of /devices/platform/ */ |
| 494 | |
| 495 | if (!strcmp(uevent->action, "add")) |
| 496 | add_platform_device(name); |
| 497 | else if (!strcmp(uevent->action, "remove")) |
| 498 | remove_platform_device(name); |
| 499 | } |
| 500 | |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 501 | static const char *parse_device_name(struct uevent *uevent, unsigned int len) |
| 502 | { |
| 503 | const char *name; |
| 504 | |
| 505 | /* if it's not a /dev device, nothing else to do */ |
| 506 | if((uevent->major < 0) || (uevent->minor < 0)) |
| 507 | return NULL; |
| 508 | |
| 509 | /* do we have a name? */ |
| 510 | name = strrchr(uevent->path, '/'); |
| 511 | if(!name) |
| 512 | return NULL; |
| 513 | name++; |
| 514 | |
| 515 | /* too-long names would overrun our buffer */ |
| 516 | if(strlen(name) > len) |
| 517 | return NULL; |
| 518 | |
| 519 | return name; |
| 520 | } |
| 521 | |
| 522 | static void handle_block_device_event(struct uevent *uevent) |
| 523 | { |
| 524 | const char *base = "/dev/block/"; |
| 525 | const char *name; |
| 526 | char devpath[96]; |
| 527 | char **links = NULL; |
| 528 | |
| 529 | name = parse_device_name(uevent, 64); |
| 530 | if (!name) |
| 531 | return; |
| 532 | |
| 533 | snprintf(devpath, sizeof(devpath), "%s%s", base, name); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 534 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 535 | |
| 536 | if (!strncmp(uevent->path, "/devices/platform/", 18)) |
| 537 | links = parse_platform_block_device(uevent); |
| 538 | |
| 539 | handle_device(uevent->action, devpath, uevent->path, 1, |
| 540 | uevent->major, uevent->minor, links); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | static void handle_generic_device_event(struct uevent *uevent) |
| 544 | { |
| 545 | char *base; |
| 546 | const char *name; |
| 547 | char devpath[96] = {0}; |
| 548 | char **links = NULL; |
| 549 | |
| 550 | name = parse_device_name(uevent, 64); |
| 551 | if (!name) |
| 552 | return; |
| 553 | |
| 554 | if (!strncmp(uevent->subsystem, "usb", 3)) { |
| 555 | if (!strcmp(uevent->subsystem, "usb")) { |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 556 | if (uevent->device_name) { |
| 557 | /* |
| 558 | * create device node provided by kernel if present |
| 559 | * see drivers/base/core.c |
| 560 | */ |
| 561 | char *p = devpath; |
| 562 | snprintf(devpath, sizeof(devpath), "/dev/%s", uevent->device_name); |
| 563 | /* skip leading /dev/ */ |
| 564 | p += 5; |
| 565 | /* build directories */ |
| 566 | while (*p) { |
| 567 | if (*p == '/') { |
| 568 | *p = 0; |
| 569 | make_dir(devpath, 0755); |
| 570 | *p = '/'; |
| 571 | } |
| 572 | p++; |
| 573 | } |
| 574 | } |
| 575 | else { |
| 576 | /* This imitates the file system that would be created |
| 577 | * if we were using devfs instead. |
| 578 | * Minors are broken up into groups of 128, starting at "001" |
| 579 | */ |
| 580 | int bus_id = uevent->minor / 128 + 1; |
| 581 | int device_id = uevent->minor % 128 + 1; |
| 582 | /* build directories */ |
| 583 | make_dir("/dev/bus", 0755); |
| 584 | make_dir("/dev/bus/usb", 0755); |
| 585 | snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id); |
| 586 | make_dir(devpath, 0755); |
| 587 | snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id); |
| 588 | } |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 589 | } else { |
| 590 | /* ignore other USB events */ |
| 591 | return; |
| 592 | } |
| 593 | } else if (!strncmp(uevent->subsystem, "graphics", 8)) { |
| 594 | base = "/dev/graphics/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 595 | make_dir(base, 0755); |
Lukasz Anaczkowski | e6f8d45 | 2011-09-28 15:30:07 +0200 | [diff] [blame] | 596 | } else if (!strncmp(uevent->subsystem, "drm", 3)) { |
| 597 | base = "/dev/dri/"; |
| 598 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 599 | } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) { |
| 600 | base = "/dev/oncrpc/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 601 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 602 | } else if (!strncmp(uevent->subsystem, "adsp", 4)) { |
| 603 | base = "/dev/adsp/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 604 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 605 | } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) { |
| 606 | base = "/dev/msm_camera/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 607 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 608 | } else if(!strncmp(uevent->subsystem, "input", 5)) { |
| 609 | base = "/dev/input/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 610 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 611 | } else if(!strncmp(uevent->subsystem, "mtd", 3)) { |
| 612 | base = "/dev/mtd/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 613 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 614 | } else if(!strncmp(uevent->subsystem, "sound", 5)) { |
| 615 | base = "/dev/snd/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 616 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 617 | } else if(!strncmp(uevent->subsystem, "misc", 4) && |
| 618 | !strncmp(name, "log_", 4)) { |
| 619 | base = "/dev/log/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 620 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 621 | name += 4; |
| 622 | } else |
| 623 | base = "/dev/"; |
| 624 | links = get_character_device_symlinks(uevent); |
| 625 | |
| 626 | if (!devpath[0]) |
| 627 | snprintf(devpath, sizeof(devpath), "%s%s", base, name); |
| 628 | |
| 629 | handle_device(uevent->action, devpath, uevent->path, 0, |
| 630 | uevent->major, uevent->minor, links); |
| 631 | } |
| 632 | |
| 633 | static void handle_device_event(struct uevent *uevent) |
| 634 | { |
Colin Cross | 308bc52 | 2012-07-23 16:31:28 -0700 | [diff] [blame] | 635 | if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change")) |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 636 | fixup_sys_perms(uevent->path); |
| 637 | |
| 638 | if (!strncmp(uevent->subsystem, "block", 5)) { |
| 639 | handle_block_device_event(uevent); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 640 | } else if (!strncmp(uevent->subsystem, "platform", 8)) { |
| 641 | handle_platform_device_event(uevent); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 642 | } else { |
| 643 | handle_generic_device_event(uevent); |
| 644 | } |
| 645 | } |
| 646 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 647 | static int load_firmware(int fw_fd, int loading_fd, int data_fd) |
| 648 | { |
| 649 | struct stat st; |
| 650 | long len_to_copy; |
| 651 | int ret = 0; |
| 652 | |
| 653 | if(fstat(fw_fd, &st) < 0) |
| 654 | return -1; |
| 655 | len_to_copy = st.st_size; |
| 656 | |
| 657 | write(loading_fd, "1", 1); /* start transfer */ |
| 658 | |
| 659 | while (len_to_copy > 0) { |
| 660 | char buf[PAGE_SIZE]; |
| 661 | ssize_t nr; |
| 662 | |
| 663 | nr = read(fw_fd, buf, sizeof(buf)); |
| 664 | if(!nr) |
| 665 | break; |
| 666 | if(nr < 0) { |
| 667 | ret = -1; |
| 668 | break; |
| 669 | } |
| 670 | |
| 671 | len_to_copy -= nr; |
| 672 | while (nr > 0) { |
| 673 | ssize_t nw = 0; |
| 674 | |
| 675 | nw = write(data_fd, buf + nw, nr); |
| 676 | if(nw <= 0) { |
| 677 | ret = -1; |
| 678 | goto out; |
| 679 | } |
| 680 | nr -= nw; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | out: |
| 685 | if(!ret) |
| 686 | write(loading_fd, "0", 1); /* successful end of transfer */ |
| 687 | else |
| 688 | write(loading_fd, "-1", 2); /* abort transfer */ |
| 689 | |
| 690 | return ret; |
| 691 | } |
| 692 | |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 693 | static int is_booting(void) |
| 694 | { |
| 695 | return access("/dev/.booting", F_OK) == 0; |
| 696 | } |
| 697 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 698 | static void process_firmware_event(struct uevent *uevent) |
| 699 | { |
Iliyan Malchev | 029d44e | 2012-06-08 10:42:26 -0700 | [diff] [blame] | 700 | char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 701 | int l, loading_fd, data_fd, fw_fd; |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 702 | int booting = is_booting(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 703 | |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 704 | INFO("firmware: loading '%s' for '%s'\n", |
| 705 | uevent->firmware, uevent->path); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 706 | |
| 707 | l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path); |
| 708 | if (l == -1) |
| 709 | return; |
| 710 | |
| 711 | l = asprintf(&loading, "%sloading", root); |
| 712 | if (l == -1) |
| 713 | goto root_free_out; |
| 714 | |
| 715 | l = asprintf(&data, "%sdata", root); |
| 716 | if (l == -1) |
| 717 | goto loading_free_out; |
| 718 | |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 719 | l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware); |
| 720 | if (l == -1) |
| 721 | goto data_free_out; |
| 722 | |
| 723 | l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 724 | if (l == -1) |
| 725 | goto data_free_out; |
| 726 | |
Iliyan Malchev | 029d44e | 2012-06-08 10:42:26 -0700 | [diff] [blame] | 727 | l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware); |
| 728 | if (l == -1) |
| 729 | goto data_free_out; |
| 730 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 731 | loading_fd = open(loading, O_WRONLY); |
| 732 | if(loading_fd < 0) |
| 733 | goto file_free_out; |
| 734 | |
| 735 | data_fd = open(data, O_WRONLY); |
| 736 | if(data_fd < 0) |
| 737 | goto loading_close_out; |
| 738 | |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 739 | try_loading_again: |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 740 | fw_fd = open(file1, O_RDONLY); |
| 741 | if(fw_fd < 0) { |
| 742 | fw_fd = open(file2, O_RDONLY); |
Benoit Goby | 609d882 | 2010-11-09 18:10:24 -0800 | [diff] [blame] | 743 | if (fw_fd < 0) { |
Iliyan Malchev | 029d44e | 2012-06-08 10:42:26 -0700 | [diff] [blame] | 744 | fw_fd = open(file3, O_RDONLY); |
| 745 | if (fw_fd < 0) { |
| 746 | if (booting) { |
| 747 | /* If we're not fully booted, we may be missing |
| 748 | * filesystems needed for firmware, wait and retry. |
| 749 | */ |
| 750 | usleep(100000); |
| 751 | booting = is_booting(); |
| 752 | goto try_loading_again; |
| 753 | } |
| 754 | INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno); |
| 755 | write(loading_fd, "-1", 2); |
| 756 | goto data_close_out; |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 757 | } |
Benoit Goby | 609d882 | 2010-11-09 18:10:24 -0800 | [diff] [blame] | 758 | } |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 759 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 760 | |
| 761 | if(!load_firmware(fw_fd, loading_fd, data_fd)) |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 762 | INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 763 | else |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 764 | INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 765 | |
| 766 | close(fw_fd); |
| 767 | data_close_out: |
| 768 | close(data_fd); |
| 769 | loading_close_out: |
| 770 | close(loading_fd); |
| 771 | file_free_out: |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 772 | free(file1); |
| 773 | free(file2); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 774 | data_free_out: |
| 775 | free(data); |
| 776 | loading_free_out: |
| 777 | free(loading); |
| 778 | root_free_out: |
| 779 | free(root); |
| 780 | } |
| 781 | |
| 782 | static void handle_firmware_event(struct uevent *uevent) |
| 783 | { |
| 784 | pid_t pid; |
Colin Cross | 982a815 | 2010-06-03 12:21:01 -0700 | [diff] [blame] | 785 | int ret; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 786 | |
| 787 | if(strcmp(uevent->subsystem, "firmware")) |
| 788 | return; |
| 789 | |
| 790 | if(strcmp(uevent->action, "add")) |
| 791 | return; |
| 792 | |
| 793 | /* we fork, to avoid making large memory allocations in init proper */ |
| 794 | pid = fork(); |
| 795 | if (!pid) { |
| 796 | process_firmware_event(uevent); |
| 797 | exit(EXIT_SUCCESS); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | #define UEVENT_MSG_LEN 1024 |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 802 | void handle_device_fd() |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 803 | { |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 804 | char msg[UEVENT_MSG_LEN+2]; |
| 805 | int n; |
Nick Kralevich | 57de8b8 | 2011-05-11 14:58:24 -0700 | [diff] [blame] | 806 | while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) { |
Nick Kralevich | 5f5d5c8 | 2010-07-19 14:31:20 -0700 | [diff] [blame] | 807 | if(n >= UEVENT_MSG_LEN) /* overflow -- discard */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 808 | continue; |
| 809 | |
| 810 | msg[n] = '\0'; |
| 811 | msg[n+1] = '\0'; |
| 812 | |
Nick Kralevich | 5f5d5c8 | 2010-07-19 14:31:20 -0700 | [diff] [blame] | 813 | struct uevent uevent; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 814 | parse_event(msg, &uevent); |
| 815 | |
| 816 | handle_device_event(&uevent); |
| 817 | handle_firmware_event(&uevent); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | /* Coldboot walks parts of the /sys tree and pokes the uevent files |
| 822 | ** to cause the kernel to regenerate device add events that happened |
| 823 | ** before init's device manager was started |
| 824 | ** |
| 825 | ** We drain any pending events from the netlink socket every time |
| 826 | ** we poke another uevent file to make sure we don't overrun the |
| 827 | ** socket's buffer. |
| 828 | */ |
| 829 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 830 | static void do_coldboot(DIR *d) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 831 | { |
| 832 | struct dirent *de; |
| 833 | int dfd, fd; |
| 834 | |
| 835 | dfd = dirfd(d); |
| 836 | |
| 837 | fd = openat(dfd, "uevent", O_WRONLY); |
| 838 | if(fd >= 0) { |
| 839 | write(fd, "add\n", 4); |
| 840 | close(fd); |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 841 | handle_device_fd(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | while((de = readdir(d))) { |
| 845 | DIR *d2; |
| 846 | |
| 847 | if(de->d_type != DT_DIR || de->d_name[0] == '.') |
| 848 | continue; |
| 849 | |
| 850 | fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); |
| 851 | if(fd < 0) |
| 852 | continue; |
| 853 | |
| 854 | d2 = fdopendir(fd); |
| 855 | if(d2 == 0) |
| 856 | close(fd); |
| 857 | else { |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 858 | do_coldboot(d2); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 859 | closedir(d2); |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 864 | static void coldboot(const char *path) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 865 | { |
| 866 | DIR *d = opendir(path); |
| 867 | if(d) { |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 868 | do_coldboot(d); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 869 | closedir(d); |
| 870 | } |
| 871 | } |
| 872 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 873 | void device_init(void) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 874 | { |
| 875 | suseconds_t t0, t1; |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 876 | struct stat info; |
| 877 | int fd; |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 878 | |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 879 | sehandle = NULL; |
| 880 | if (is_selinux_enabled() > 0) { |
| 881 | sehandle = selinux_android_file_context_handle(); |
| 882 | } |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 883 | |
Andrew Boie | d562ca7 | 2012-12-04 15:47:20 -0800 | [diff] [blame^] | 884 | /* is 256K enough? udev uses 16MB! */ |
| 885 | device_fd = uevent_open_socket(256*1024, true); |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 886 | if(device_fd < 0) |
| 887 | return; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 888 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 889 | fcntl(device_fd, F_SETFD, FD_CLOEXEC); |
| 890 | fcntl(device_fd, F_SETFL, O_NONBLOCK); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 891 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 892 | if (stat(coldboot_done, &info) < 0) { |
| 893 | t0 = get_usecs(); |
| 894 | coldboot("/sys/class"); |
| 895 | coldboot("/sys/block"); |
| 896 | coldboot("/sys/devices"); |
| 897 | t1 = get_usecs(); |
| 898 | fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000); |
| 899 | close(fd); |
| 900 | log_event_print("coldboot %ld uS\n", ((long) (t1 - t0))); |
| 901 | } else { |
| 902 | log_event_print("skipping coldboot, already done\n"); |
| 903 | } |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 904 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 905 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 906 | int get_device_fd() |
| 907 | { |
| 908 | return device_fd; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 909 | } |