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