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