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