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