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