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> |
| 32 | #include <private/android_filesystem_config.h> |
| 33 | #include <sys/time.h> |
| 34 | #include <asm/page.h> |
Colin Cross | 982a815 | 2010-06-03 12:21:01 -0700 | [diff] [blame] | 35 | #include <sys/wait.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 36 | |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 37 | #include <cutils/uevent.h> |
| 38 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 39 | #include "devices.h" |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 40 | #include "util.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 41 | #include "log.h" |
| 42 | #include "list.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 43 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 44 | #define SYSFS_PREFIX "/sys" |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 45 | #define FIRMWARE_DIR1 "/etc/firmware" |
| 46 | #define FIRMWARE_DIR2 "/vendor/firmware" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 47 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 48 | static int device_fd = -1; |
| 49 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 50 | struct uevent { |
| 51 | const char *action; |
| 52 | const char *path; |
| 53 | const char *subsystem; |
| 54 | const char *firmware; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 55 | const char *partition_name; |
| 56 | int partition_num; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 57 | int major; |
| 58 | int minor; |
| 59 | }; |
| 60 | |
| 61 | static int open_uevent_socket(void) |
| 62 | { |
| 63 | struct sockaddr_nl addr; |
| 64 | int sz = 64*1024; // XXX larger? udev uses 16MB! |
Nick Kralevich | 5f5d5c8 | 2010-07-19 14:31:20 -0700 | [diff] [blame] | 65 | int on = 1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 66 | int s; |
| 67 | |
| 68 | memset(&addr, 0, sizeof(addr)); |
| 69 | addr.nl_family = AF_NETLINK; |
| 70 | addr.nl_pid = getpid(); |
| 71 | addr.nl_groups = 0xffffffff; |
| 72 | |
| 73 | s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); |
| 74 | if(s < 0) |
| 75 | return -1; |
| 76 | |
| 77 | setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)); |
Nick Kralevich | 5f5d5c8 | 2010-07-19 14:31:20 -0700 | [diff] [blame] | 78 | setsockopt(s, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 79 | |
| 80 | if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 81 | close(s); |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | return s; |
| 86 | } |
| 87 | |
| 88 | struct perms_ { |
| 89 | char *name; |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 90 | char *attr; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 91 | mode_t perm; |
| 92 | unsigned int uid; |
| 93 | unsigned int gid; |
| 94 | unsigned short prefix; |
| 95 | }; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 96 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 97 | struct perm_node { |
| 98 | struct perms_ dp; |
| 99 | struct listnode plist; |
| 100 | }; |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 101 | |
| 102 | static list_declare(sys_perms); |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 103 | static list_declare(dev_perms); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 104 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 105 | int add_dev_perms(const char *name, const char *attr, |
| 106 | mode_t perm, unsigned int uid, unsigned int gid, |
| 107 | unsigned short prefix) { |
| 108 | struct perm_node *node = calloc(1, sizeof(*node)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 109 | if (!node) |
| 110 | return -ENOMEM; |
| 111 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 112 | node->dp.name = strdup(name); |
| 113 | if (!node->dp.name) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 114 | return -ENOMEM; |
| 115 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 116 | if (attr) { |
| 117 | node->dp.attr = strdup(attr); |
| 118 | if (!node->dp.attr) |
| 119 | return -ENOMEM; |
| 120 | } |
| 121 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 122 | node->dp.perm = perm; |
| 123 | node->dp.uid = uid; |
| 124 | node->dp.gid = gid; |
| 125 | node->dp.prefix = prefix; |
| 126 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 127 | if (attr) |
| 128 | list_add_tail(&sys_perms, &node->plist); |
| 129 | else |
| 130 | list_add_tail(&dev_perms, &node->plist); |
| 131 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 135 | void fixup_sys_perms(const char *upath) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 136 | { |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 137 | char buf[512]; |
| 138 | struct listnode *node; |
| 139 | struct perms_ *dp; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 140 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 141 | /* upaths omit the "/sys" that paths in this list |
| 142 | * contain, so we add 4 when comparing... |
| 143 | */ |
| 144 | list_for_each(node, &sys_perms) { |
| 145 | dp = &(node_to_item(node, struct perm_node, plist))->dp; |
| 146 | if (dp->prefix) { |
| 147 | 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] | 148 | continue; |
| 149 | } else { |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 150 | if (strcmp(upath, dp->name + 4)) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 151 | continue; |
| 152 | } |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 153 | |
| 154 | if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf)) |
| 155 | return; |
| 156 | |
| 157 | sprintf(buf,"/sys%s/%s", upath, dp->attr); |
| 158 | INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm); |
| 159 | chown(buf, dp->uid, dp->gid); |
| 160 | chmod(buf, dp->perm); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 161 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 162 | } |
| 163 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 164 | static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid) |
| 165 | { |
| 166 | mode_t perm; |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 167 | struct listnode *node; |
| 168 | struct perm_node *perm_node; |
| 169 | struct perms_ *dp; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 170 | |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 171 | /* 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 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 | 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 Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 184 | } |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 185 | *uid = dp->uid; |
| 186 | *gid = dp->gid; |
| 187 | return dp->perm; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 188 | } |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 189 | /* Default if nothing found. */ |
| 190 | *uid = 0; |
| 191 | *gid = 0; |
| 192 | return 0600; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 195 | static void make_device(const char *path, |
| 196 | const char *upath, |
| 197 | int block, int major, int minor) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 198 | { |
| 199 | unsigned uid; |
| 200 | unsigned gid; |
| 201 | mode_t mode; |
| 202 | dev_t dev; |
| 203 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 204 | mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR); |
Colin Cross | 17dcc5c | 2010-09-03 12:25:34 -0700 | [diff] [blame] | 205 | dev = makedev(major, minor); |
Nick Pelly | 6405c69 | 2010-01-21 18:13:39 -0800 | [diff] [blame] | 206 | /* Temporarily change egid to avoid race condition setting the gid of the |
| 207 | * device node. Unforunately changing the euid would prevent creation of |
| 208 | * some device nodes, so the uid has to be set with chown() and is still |
| 209 | * racy. Fixing the gid race at least fixed the issue with system_server |
| 210 | * opening dynamic input devices under the AID_INPUT gid. */ |
| 211 | setegid(gid); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 212 | mknod(path, mode, dev); |
Nick Pelly | 6405c69 | 2010-01-21 18:13:39 -0800 | [diff] [blame] | 213 | chown(path, uid, -1); |
| 214 | setegid(AID_ROOT); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Chuck Tuffli | 1e07084 | 2008-12-15 14:26:56 -0800 | [diff] [blame] | 217 | #if LOG_UEVENTS |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 218 | |
| 219 | static inline suseconds_t get_usecs(void) |
| 220 | { |
| 221 | struct timeval tv; |
| 222 | gettimeofday(&tv, 0); |
| 223 | return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec; |
| 224 | } |
| 225 | |
| 226 | #define log_event_print(x...) INFO(x) |
| 227 | |
| 228 | #else |
| 229 | |
| 230 | #define log_event_print(fmt, args...) do { } while (0) |
| 231 | #define get_usecs() 0 |
| 232 | |
| 233 | #endif |
| 234 | |
| 235 | static void parse_event(const char *msg, struct uevent *uevent) |
| 236 | { |
| 237 | uevent->action = ""; |
| 238 | uevent->path = ""; |
| 239 | uevent->subsystem = ""; |
| 240 | uevent->firmware = ""; |
| 241 | uevent->major = -1; |
| 242 | uevent->minor = -1; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 243 | uevent->partition_name = NULL; |
| 244 | uevent->partition_num = -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 245 | |
| 246 | /* currently ignoring SEQNUM */ |
| 247 | while(*msg) { |
| 248 | if(!strncmp(msg, "ACTION=", 7)) { |
| 249 | msg += 7; |
| 250 | uevent->action = msg; |
| 251 | } else if(!strncmp(msg, "DEVPATH=", 8)) { |
| 252 | msg += 8; |
| 253 | uevent->path = msg; |
| 254 | } else if(!strncmp(msg, "SUBSYSTEM=", 10)) { |
| 255 | msg += 10; |
| 256 | uevent->subsystem = msg; |
| 257 | } else if(!strncmp(msg, "FIRMWARE=", 9)) { |
| 258 | msg += 9; |
| 259 | uevent->firmware = msg; |
| 260 | } else if(!strncmp(msg, "MAJOR=", 6)) { |
| 261 | msg += 6; |
| 262 | uevent->major = atoi(msg); |
| 263 | } else if(!strncmp(msg, "MINOR=", 6)) { |
| 264 | msg += 6; |
| 265 | uevent->minor = atoi(msg); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 266 | } else if(!strncmp(msg, "PARTN=", 6)) { |
| 267 | msg += 6; |
| 268 | uevent->partition_num = atoi(msg); |
| 269 | } else if(!strncmp(msg, "PARTNAME=", 9)) { |
| 270 | msg += 9; |
| 271 | uevent->partition_name = msg; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /* advance to after the next \0 */ |
| 275 | while(*msg++) |
| 276 | ; |
| 277 | } |
| 278 | |
| 279 | log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n", |
| 280 | uevent->action, uevent->path, uevent->subsystem, |
| 281 | uevent->firmware, uevent->major, uevent->minor); |
| 282 | } |
| 283 | |
Benoit Goby | d227863 | 2010-08-03 14:36:02 -0700 | [diff] [blame] | 284 | static char **get_character_device_symlinks(struct uevent *uevent) |
| 285 | { |
| 286 | const char *parent; |
| 287 | char *slash; |
| 288 | char **links; |
| 289 | int link_num = 0; |
| 290 | int width; |
| 291 | |
| 292 | if (strncmp(uevent->path, "/devices/platform/", 18)) |
| 293 | return NULL; |
| 294 | |
| 295 | links = malloc(sizeof(char *) * 2); |
| 296 | if (!links) |
| 297 | return NULL; |
| 298 | memset(links, 0, sizeof(char *) * 2); |
| 299 | |
| 300 | /* skip "/devices/platform/<driver>" */ |
| 301 | parent = strchr(uevent->path + 18, '/'); |
| 302 | if (!*parent) |
| 303 | goto err; |
| 304 | |
| 305 | if (!strncmp(parent, "/usb", 4)) { |
| 306 | /* skip root hub name and device. use device interface */ |
| 307 | while (*++parent && *parent != '/'); |
| 308 | if (*parent) |
| 309 | while (*++parent && *parent != '/'); |
| 310 | if (!*parent) |
| 311 | goto err; |
| 312 | slash = strchr(++parent, '/'); |
| 313 | if (!slash) |
| 314 | goto err; |
| 315 | width = slash - parent; |
| 316 | if (width <= 0) |
| 317 | goto err; |
| 318 | |
| 319 | if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0) |
| 320 | link_num++; |
| 321 | else |
| 322 | links[link_num] = NULL; |
| 323 | mkdir("/dev/usb", 0755); |
| 324 | } |
| 325 | else { |
| 326 | goto err; |
| 327 | } |
| 328 | |
| 329 | return links; |
| 330 | err: |
| 331 | free(links); |
| 332 | return NULL; |
| 333 | } |
| 334 | |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 335 | static char **parse_platform_block_device(struct uevent *uevent) |
| 336 | { |
| 337 | const char *driver; |
| 338 | const char *path; |
| 339 | char *slash; |
| 340 | int width; |
| 341 | char buf[256]; |
| 342 | char link_path[256]; |
| 343 | int fd; |
| 344 | int link_num = 0; |
| 345 | int ret; |
| 346 | char *p; |
| 347 | unsigned int size; |
| 348 | struct stat info; |
| 349 | |
| 350 | char **links = malloc(sizeof(char *) * 4); |
| 351 | if (!links) |
| 352 | return NULL; |
| 353 | memset(links, 0, sizeof(char *) * 4); |
| 354 | |
| 355 | /* Drop "/devices/platform/" */ |
| 356 | path = uevent->path; |
| 357 | driver = path + 18; |
| 358 | slash = strchr(driver, '/'); |
| 359 | if (!slash) |
| 360 | goto err; |
| 361 | width = slash - driver; |
| 362 | if (width <= 0) |
| 363 | goto err; |
| 364 | |
| 365 | snprintf(link_path, sizeof(link_path), "/dev/block/platform/%.*s", |
| 366 | width, driver); |
| 367 | |
| 368 | if (uevent->partition_name) { |
| 369 | p = strdup(uevent->partition_name); |
| 370 | sanitize(p); |
| 371 | if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0) |
| 372 | link_num++; |
| 373 | else |
| 374 | links[link_num] = NULL; |
| 375 | free(p); |
| 376 | } |
| 377 | |
| 378 | if (uevent->partition_num >= 0) { |
| 379 | if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0) |
| 380 | link_num++; |
| 381 | else |
| 382 | links[link_num] = NULL; |
| 383 | } |
| 384 | |
| 385 | slash = strrchr(path, '/'); |
| 386 | if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0) |
| 387 | link_num++; |
| 388 | else |
| 389 | links[link_num] = NULL; |
| 390 | |
| 391 | return links; |
| 392 | |
| 393 | err: |
| 394 | free(links); |
| 395 | return NULL; |
| 396 | } |
| 397 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 398 | static void handle_device_event(struct uevent *uevent) |
| 399 | { |
| 400 | char devpath[96]; |
Mike Lockwood | 93ac155 | 2010-05-06 13:30:09 -0400 | [diff] [blame] | 401 | int devpath_ready = 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 402 | char *base, *name; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 403 | char **links = NULL; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 404 | int block; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 405 | int i; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 406 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 407 | if (!strcmp(uevent->action,"add")) |
| 408 | fixup_sys_perms(uevent->path); |
| 409 | |
| 410 | /* if it's not a /dev device, nothing else to do */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 411 | if((uevent->major < 0) || (uevent->minor < 0)) |
| 412 | return; |
| 413 | |
| 414 | /* do we have a name? */ |
| 415 | name = strrchr(uevent->path, '/'); |
| 416 | if(!name) |
| 417 | return; |
| 418 | name++; |
| 419 | |
| 420 | /* too-long names would overrun our buffer */ |
| 421 | if(strlen(name) > 64) |
| 422 | return; |
| 423 | |
| 424 | /* are we block or char? where should we live? */ |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 425 | if(!strncmp(uevent->subsystem, "block", 5)) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 426 | block = 1; |
| 427 | base = "/dev/block/"; |
| 428 | mkdir(base, 0755); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 429 | if (!strncmp(uevent->path, "/devices/platform/", 18)) |
| 430 | links = parse_platform_block_device(uevent); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 431 | } else { |
| 432 | block = 0; |
| 433 | /* this should probably be configurable somehow */ |
Mike Lockwood | 93ac155 | 2010-05-06 13:30:09 -0400 | [diff] [blame] | 434 | if (!strncmp(uevent->subsystem, "usb", 3)) { |
| 435 | if (!strcmp(uevent->subsystem, "usb")) { |
| 436 | /* This imitates the file system that would be created |
| 437 | * if we were using devfs instead. |
| 438 | * Minors are broken up into groups of 128, starting at "001" |
| 439 | */ |
| 440 | int bus_id = uevent->minor / 128 + 1; |
| 441 | int device_id = uevent->minor % 128 + 1; |
| 442 | /* build directories */ |
| 443 | mkdir("/dev/bus", 0755); |
| 444 | mkdir("/dev/bus/usb", 0755); |
| 445 | snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id); |
| 446 | mkdir(devpath, 0755); |
| 447 | snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id); |
| 448 | devpath_ready = 1; |
| 449 | } else { |
| 450 | /* ignore other USB events */ |
| 451 | return; |
| 452 | } |
| 453 | } else if (!strncmp(uevent->subsystem, "graphics", 8)) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 454 | base = "/dev/graphics/"; |
| 455 | mkdir(base, 0755); |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 456 | } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 457 | base = "/dev/oncrpc/"; |
| 458 | mkdir(base, 0755); |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 459 | } else if (!strncmp(uevent->subsystem, "adsp", 4)) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 460 | base = "/dev/adsp/"; |
| 461 | mkdir(base, 0755); |
Iliyan Malchev | fc0182e | 2009-05-01 10:25:05 -0700 | [diff] [blame] | 462 | } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) { |
| 463 | base = "/dev/msm_camera/"; |
| 464 | mkdir(base, 0755); |
| 465 | } else if(!strncmp(uevent->subsystem, "input", 5)) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 466 | base = "/dev/input/"; |
| 467 | mkdir(base, 0755); |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 468 | } else if(!strncmp(uevent->subsystem, "mtd", 3)) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 469 | base = "/dev/mtd/"; |
| 470 | mkdir(base, 0755); |
Xiaopeng Yang | 5bb44c8 | 2008-11-25 16:20:07 -0800 | [diff] [blame] | 471 | } else if(!strncmp(uevent->subsystem, "sound", 5)) { |
| 472 | base = "/dev/snd/"; |
| 473 | mkdir(base, 0755); |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 474 | } else if(!strncmp(uevent->subsystem, "misc", 4) && |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 475 | !strncmp(name, "log_", 4)) { |
| 476 | base = "/dev/log/"; |
| 477 | mkdir(base, 0755); |
| 478 | name += 4; |
| 479 | } else |
| 480 | base = "/dev/"; |
Benoit Goby | d227863 | 2010-08-03 14:36:02 -0700 | [diff] [blame] | 481 | links = get_character_device_symlinks(uevent); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Mike Lockwood | 93ac155 | 2010-05-06 13:30:09 -0400 | [diff] [blame] | 484 | if (!devpath_ready) |
| 485 | snprintf(devpath, sizeof(devpath), "%s%s", base, name); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 486 | |
| 487 | if(!strcmp(uevent->action, "add")) { |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 488 | make_device(devpath, uevent->path, block, uevent->major, uevent->minor); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 489 | if (links) { |
| 490 | for (i = 0; links[i]; i++) |
| 491 | make_link(devpath, links[i]); |
| 492 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | if(!strcmp(uevent->action, "remove")) { |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 496 | if (links) { |
| 497 | for (i = 0; links[i]; i++) |
| 498 | remove_link(devpath, links[i]); |
| 499 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 500 | unlink(devpath); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | if (links) { |
| 504 | for (i = 0; links[i]; i++) |
| 505 | free(links[i]); |
| 506 | free(links); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
| 510 | static int load_firmware(int fw_fd, int loading_fd, int data_fd) |
| 511 | { |
| 512 | struct stat st; |
| 513 | long len_to_copy; |
| 514 | int ret = 0; |
| 515 | |
| 516 | if(fstat(fw_fd, &st) < 0) |
| 517 | return -1; |
| 518 | len_to_copy = st.st_size; |
| 519 | |
| 520 | write(loading_fd, "1", 1); /* start transfer */ |
| 521 | |
| 522 | while (len_to_copy > 0) { |
| 523 | char buf[PAGE_SIZE]; |
| 524 | ssize_t nr; |
| 525 | |
| 526 | nr = read(fw_fd, buf, sizeof(buf)); |
| 527 | if(!nr) |
| 528 | break; |
| 529 | if(nr < 0) { |
| 530 | ret = -1; |
| 531 | break; |
| 532 | } |
| 533 | |
| 534 | len_to_copy -= nr; |
| 535 | while (nr > 0) { |
| 536 | ssize_t nw = 0; |
| 537 | |
| 538 | nw = write(data_fd, buf + nw, nr); |
| 539 | if(nw <= 0) { |
| 540 | ret = -1; |
| 541 | goto out; |
| 542 | } |
| 543 | nr -= nw; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | out: |
| 548 | if(!ret) |
| 549 | write(loading_fd, "0", 1); /* successful end of transfer */ |
| 550 | else |
| 551 | write(loading_fd, "-1", 2); /* abort transfer */ |
| 552 | |
| 553 | return ret; |
| 554 | } |
| 555 | |
| 556 | static void process_firmware_event(struct uevent *uevent) |
| 557 | { |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 558 | char *root, *loading, *data, *file1 = NULL, *file2 = NULL; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 559 | int l, loading_fd, data_fd, fw_fd; |
| 560 | |
| 561 | log_event_print("firmware event { '%s', '%s' }\n", |
| 562 | uevent->path, uevent->firmware); |
| 563 | |
| 564 | l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path); |
| 565 | if (l == -1) |
| 566 | return; |
| 567 | |
| 568 | l = asprintf(&loading, "%sloading", root); |
| 569 | if (l == -1) |
| 570 | goto root_free_out; |
| 571 | |
| 572 | l = asprintf(&data, "%sdata", root); |
| 573 | if (l == -1) |
| 574 | goto loading_free_out; |
| 575 | |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 576 | l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware); |
| 577 | if (l == -1) |
| 578 | goto data_free_out; |
| 579 | |
| 580 | l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 581 | if (l == -1) |
| 582 | goto data_free_out; |
| 583 | |
| 584 | loading_fd = open(loading, O_WRONLY); |
| 585 | if(loading_fd < 0) |
| 586 | goto file_free_out; |
| 587 | |
| 588 | data_fd = open(data, O_WRONLY); |
| 589 | if(data_fd < 0) |
| 590 | goto loading_close_out; |
| 591 | |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 592 | fw_fd = open(file1, O_RDONLY); |
| 593 | if(fw_fd < 0) { |
| 594 | fw_fd = open(file2, O_RDONLY); |
Benoit Goby | 609d882 | 2010-11-09 18:10:24 -0800 | [diff] [blame] | 595 | if (fw_fd < 0) { |
| 596 | write(loading_fd, "-1", 2); |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 597 | goto data_close_out; |
Benoit Goby | 609d882 | 2010-11-09 18:10:24 -0800 | [diff] [blame] | 598 | } |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 599 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 600 | |
| 601 | if(!load_firmware(fw_fd, loading_fd, data_fd)) |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 602 | log_event_print("firmware copy success { '%s', '%s' }\n", root, uevent->firmware); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 603 | else |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 604 | log_event_print("firmware copy failure { '%s', '%s' }\n", root, uevent->firmware); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 605 | |
| 606 | close(fw_fd); |
| 607 | data_close_out: |
| 608 | close(data_fd); |
| 609 | loading_close_out: |
| 610 | close(loading_fd); |
| 611 | file_free_out: |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 612 | free(file1); |
| 613 | free(file2); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 614 | data_free_out: |
| 615 | free(data); |
| 616 | loading_free_out: |
| 617 | free(loading); |
| 618 | root_free_out: |
| 619 | free(root); |
| 620 | } |
| 621 | |
| 622 | static void handle_firmware_event(struct uevent *uevent) |
| 623 | { |
| 624 | pid_t pid; |
Colin Cross | 982a815 | 2010-06-03 12:21:01 -0700 | [diff] [blame] | 625 | int status; |
| 626 | int ret; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 627 | |
| 628 | if(strcmp(uevent->subsystem, "firmware")) |
| 629 | return; |
| 630 | |
| 631 | if(strcmp(uevent->action, "add")) |
| 632 | return; |
| 633 | |
| 634 | /* we fork, to avoid making large memory allocations in init proper */ |
| 635 | pid = fork(); |
| 636 | if (!pid) { |
| 637 | process_firmware_event(uevent); |
| 638 | exit(EXIT_SUCCESS); |
Colin Cross | 982a815 | 2010-06-03 12:21:01 -0700 | [diff] [blame] | 639 | } else { |
| 640 | do { |
| 641 | ret = waitpid(pid, &status, 0); |
| 642 | } while (ret == -1 && errno == EINTR); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | |
| 646 | #define UEVENT_MSG_LEN 1024 |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 647 | void handle_device_fd() |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 648 | { |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 649 | char msg[UEVENT_MSG_LEN+2]; |
| 650 | int n; |
| 651 | while ((n = uevent_checked_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) { |
Nick Kralevich | 5f5d5c8 | 2010-07-19 14:31:20 -0700 | [diff] [blame] | 652 | if(n >= UEVENT_MSG_LEN) /* overflow -- discard */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 653 | continue; |
| 654 | |
| 655 | msg[n] = '\0'; |
| 656 | msg[n+1] = '\0'; |
| 657 | |
Nick Kralevich | 5f5d5c8 | 2010-07-19 14:31:20 -0700 | [diff] [blame] | 658 | struct uevent uevent; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 659 | parse_event(msg, &uevent); |
| 660 | |
| 661 | handle_device_event(&uevent); |
| 662 | handle_firmware_event(&uevent); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | /* Coldboot walks parts of the /sys tree and pokes the uevent files |
| 667 | ** to cause the kernel to regenerate device add events that happened |
| 668 | ** before init's device manager was started |
| 669 | ** |
| 670 | ** We drain any pending events from the netlink socket every time |
| 671 | ** we poke another uevent file to make sure we don't overrun the |
| 672 | ** socket's buffer. |
| 673 | */ |
| 674 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 675 | static void do_coldboot(DIR *d) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 676 | { |
| 677 | struct dirent *de; |
| 678 | int dfd, fd; |
| 679 | |
| 680 | dfd = dirfd(d); |
| 681 | |
| 682 | fd = openat(dfd, "uevent", O_WRONLY); |
| 683 | if(fd >= 0) { |
| 684 | write(fd, "add\n", 4); |
| 685 | close(fd); |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 686 | handle_device_fd(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | while((de = readdir(d))) { |
| 690 | DIR *d2; |
| 691 | |
| 692 | if(de->d_type != DT_DIR || de->d_name[0] == '.') |
| 693 | continue; |
| 694 | |
| 695 | fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); |
| 696 | if(fd < 0) |
| 697 | continue; |
| 698 | |
| 699 | d2 = fdopendir(fd); |
| 700 | if(d2 == 0) |
| 701 | close(fd); |
| 702 | else { |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 703 | do_coldboot(d2); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 704 | closedir(d2); |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 709 | static void coldboot(const char *path) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 710 | { |
| 711 | DIR *d = opendir(path); |
| 712 | if(d) { |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 713 | do_coldboot(d); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 714 | closedir(d); |
| 715 | } |
| 716 | } |
| 717 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 718 | void device_init(void) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 719 | { |
| 720 | suseconds_t t0, t1; |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 721 | struct stat info; |
| 722 | int fd; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 723 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 724 | device_fd = open_uevent_socket(); |
| 725 | if(device_fd < 0) |
| 726 | return; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 727 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 728 | fcntl(device_fd, F_SETFD, FD_CLOEXEC); |
| 729 | fcntl(device_fd, F_SETFL, O_NONBLOCK); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 730 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 731 | if (stat(coldboot_done, &info) < 0) { |
| 732 | t0 = get_usecs(); |
| 733 | coldboot("/sys/class"); |
| 734 | coldboot("/sys/block"); |
| 735 | coldboot("/sys/devices"); |
| 736 | t1 = get_usecs(); |
| 737 | fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000); |
| 738 | close(fd); |
| 739 | log_event_print("coldboot %ld uS\n", ((long) (t1 - t0))); |
| 740 | } else { |
| 741 | log_event_print("skipping coldboot, already done\n"); |
| 742 | } |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 743 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 744 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 745 | int get_device_fd() |
| 746 | { |
| 747 | return device_fd; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 748 | } |