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; |
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 | |
| 292 | bus = 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 | |
Chuck Tuffli | 1e07084 | 2008-12-15 14:26:56 -0800 | [diff] [blame] | 369 | #if LOG_UEVENTS |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 370 | |
| 371 | static inline suseconds_t get_usecs(void) |
| 372 | { |
| 373 | struct timeval tv; |
| 374 | gettimeofday(&tv, 0); |
| 375 | return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec; |
| 376 | } |
| 377 | |
| 378 | #define log_event_print(x...) INFO(x) |
| 379 | |
| 380 | #else |
| 381 | |
| 382 | #define log_event_print(fmt, args...) do { } while (0) |
| 383 | #define get_usecs() 0 |
| 384 | |
| 385 | #endif |
| 386 | |
| 387 | static void parse_event(const char *msg, struct uevent *uevent) |
| 388 | { |
| 389 | uevent->action = ""; |
| 390 | uevent->path = ""; |
| 391 | uevent->subsystem = ""; |
| 392 | uevent->firmware = ""; |
| 393 | uevent->major = -1; |
| 394 | uevent->minor = -1; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 395 | uevent->partition_name = NULL; |
| 396 | uevent->partition_num = -1; |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 397 | uevent->device_name = NULL; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 398 | |
| 399 | /* currently ignoring SEQNUM */ |
| 400 | while(*msg) { |
| 401 | if(!strncmp(msg, "ACTION=", 7)) { |
| 402 | msg += 7; |
| 403 | uevent->action = msg; |
| 404 | } else if(!strncmp(msg, "DEVPATH=", 8)) { |
| 405 | msg += 8; |
| 406 | uevent->path = msg; |
| 407 | } else if(!strncmp(msg, "SUBSYSTEM=", 10)) { |
| 408 | msg += 10; |
| 409 | uevent->subsystem = msg; |
| 410 | } else if(!strncmp(msg, "FIRMWARE=", 9)) { |
| 411 | msg += 9; |
| 412 | uevent->firmware = msg; |
| 413 | } else if(!strncmp(msg, "MAJOR=", 6)) { |
| 414 | msg += 6; |
| 415 | uevent->major = atoi(msg); |
| 416 | } else if(!strncmp(msg, "MINOR=", 6)) { |
| 417 | msg += 6; |
| 418 | uevent->minor = atoi(msg); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 419 | } else if(!strncmp(msg, "PARTN=", 6)) { |
| 420 | msg += 6; |
| 421 | uevent->partition_num = atoi(msg); |
| 422 | } else if(!strncmp(msg, "PARTNAME=", 9)) { |
| 423 | msg += 9; |
| 424 | uevent->partition_name = msg; |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 425 | } else if(!strncmp(msg, "DEVNAME=", 8)) { |
| 426 | msg += 8; |
| 427 | uevent->device_name = msg; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 430 | /* advance to after the next \0 */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 431 | while(*msg++) |
| 432 | ; |
| 433 | } |
| 434 | |
| 435 | log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n", |
| 436 | uevent->action, uevent->path, uevent->subsystem, |
| 437 | uevent->firmware, uevent->major, uevent->minor); |
| 438 | } |
| 439 | |
Benoit Goby | d227863 | 2010-08-03 14:36:02 -0700 | [diff] [blame] | 440 | static char **get_character_device_symlinks(struct uevent *uevent) |
| 441 | { |
| 442 | const char *parent; |
| 443 | char *slash; |
| 444 | char **links; |
| 445 | int link_num = 0; |
| 446 | int width; |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 447 | struct platform_node *pdev; |
Benoit Goby | d227863 | 2010-08-03 14:36:02 -0700 | [diff] [blame] | 448 | |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 449 | pdev = find_platform_device(uevent->path); |
| 450 | if (!pdev) |
Benoit Goby | d227863 | 2010-08-03 14:36:02 -0700 | [diff] [blame] | 451 | return NULL; |
| 452 | |
| 453 | links = malloc(sizeof(char *) * 2); |
| 454 | if (!links) |
| 455 | return NULL; |
| 456 | memset(links, 0, sizeof(char *) * 2); |
| 457 | |
| 458 | /* skip "/devices/platform/<driver>" */ |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 459 | parent = strchr(uevent->path + pdev->path_len, '/'); |
Tomasz Kondel | fca58f4 | 2013-11-05 13:17:45 +0100 | [diff] [blame] | 460 | if (!parent) |
Benoit Goby | d227863 | 2010-08-03 14:36:02 -0700 | [diff] [blame] | 461 | goto err; |
| 462 | |
| 463 | if (!strncmp(parent, "/usb", 4)) { |
| 464 | /* skip root hub name and device. use device interface */ |
| 465 | while (*++parent && *parent != '/'); |
| 466 | if (*parent) |
| 467 | while (*++parent && *parent != '/'); |
| 468 | if (!*parent) |
| 469 | goto err; |
| 470 | slash = strchr(++parent, '/'); |
| 471 | if (!slash) |
| 472 | goto err; |
| 473 | width = slash - parent; |
| 474 | if (width <= 0) |
| 475 | goto err; |
| 476 | |
| 477 | if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0) |
| 478 | link_num++; |
| 479 | else |
| 480 | links[link_num] = NULL; |
| 481 | mkdir("/dev/usb", 0755); |
| 482 | } |
| 483 | else { |
| 484 | goto err; |
| 485 | } |
| 486 | |
| 487 | return links; |
| 488 | err: |
| 489 | free(links); |
| 490 | return NULL; |
| 491 | } |
| 492 | |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 493 | static char **get_block_device_symlinks(struct uevent *uevent) |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 494 | { |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 495 | const char *device; |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 496 | struct platform_node *pdev; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 497 | char *slash; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 498 | const char *type; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 499 | char buf[256]; |
| 500 | char link_path[256]; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 501 | int link_num = 0; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 502 | char *p; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 503 | |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 504 | pdev = find_platform_device(uevent->path); |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 505 | if (pdev) { |
| 506 | device = pdev->name; |
| 507 | type = "platform"; |
| 508 | } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) { |
| 509 | device = buf; |
| 510 | type = "pci"; |
| 511 | } else { |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 512 | return NULL; |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 513 | } |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 514 | |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 515 | char **links = malloc(sizeof(char *) * 4); |
| 516 | if (!links) |
| 517 | return NULL; |
| 518 | memset(links, 0, sizeof(char *) * 4); |
| 519 | |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 520 | INFO("found %s device %s\n", type, device); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 521 | |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 522 | snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 523 | |
| 524 | if (uevent->partition_name) { |
| 525 | p = strdup(uevent->partition_name); |
| 526 | sanitize(p); |
Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 527 | if (strcmp(uevent->partition_name, p)) |
| 528 | NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 529 | if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0) |
| 530 | link_num++; |
| 531 | else |
| 532 | links[link_num] = NULL; |
| 533 | free(p); |
| 534 | } |
| 535 | |
| 536 | if (uevent->partition_num >= 0) { |
| 537 | if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0) |
| 538 | link_num++; |
| 539 | else |
| 540 | links[link_num] = NULL; |
| 541 | } |
| 542 | |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 543 | slash = strrchr(uevent->path, '/'); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 544 | if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0) |
| 545 | link_num++; |
| 546 | else |
| 547 | links[link_num] = NULL; |
| 548 | |
| 549 | return links; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 552 | static void handle_device(const char *action, const char *devpath, |
| 553 | 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] | 554 | { |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 555 | int i; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 556 | |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 557 | if(!strcmp(action, "add")) { |
Stephen Smalley | b4c5200 | 2014-06-12 12:29:14 -0400 | [diff] [blame] | 558 | make_device(devpath, path, block, major, minor, (const char **)links); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 559 | if (links) { |
| 560 | for (i = 0; links[i]; i++) |
| 561 | make_link(devpath, links[i]); |
| 562 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 565 | if(!strcmp(action, "remove")) { |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 566 | if (links) { |
| 567 | for (i = 0; links[i]; i++) |
| 568 | remove_link(devpath, links[i]); |
| 569 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 570 | unlink(devpath); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | if (links) { |
| 574 | for (i = 0; links[i]; i++) |
| 575 | free(links[i]); |
| 576 | free(links); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 580 | static void handle_platform_device_event(struct uevent *uevent) |
| 581 | { |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 582 | const char *path = uevent->path; |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 583 | |
| 584 | if (!strcmp(uevent->action, "add")) |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 585 | add_platform_device(path); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 586 | else if (!strcmp(uevent->action, "remove")) |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 587 | remove_platform_device(path); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 590 | static const char *parse_device_name(struct uevent *uevent, unsigned int len) |
| 591 | { |
| 592 | const char *name; |
| 593 | |
| 594 | /* if it's not a /dev device, nothing else to do */ |
| 595 | if((uevent->major < 0) || (uevent->minor < 0)) |
| 596 | return NULL; |
| 597 | |
| 598 | /* do we have a name? */ |
| 599 | name = strrchr(uevent->path, '/'); |
| 600 | if(!name) |
| 601 | return NULL; |
| 602 | name++; |
| 603 | |
| 604 | /* too-long names would overrun our buffer */ |
Greg Hackmann | f6e009e | 2013-11-21 13:26:48 -0800 | [diff] [blame] | 605 | if(strlen(name) > len) { |
| 606 | ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n", |
| 607 | name, len); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 608 | return NULL; |
Greg Hackmann | f6e009e | 2013-11-21 13:26:48 -0800 | [diff] [blame] | 609 | } |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 610 | |
| 611 | return name; |
| 612 | } |
| 613 | |
| 614 | static void handle_block_device_event(struct uevent *uevent) |
| 615 | { |
| 616 | const char *base = "/dev/block/"; |
| 617 | const char *name; |
| 618 | char devpath[96]; |
| 619 | char **links = NULL; |
| 620 | |
| 621 | name = parse_device_name(uevent, 64); |
| 622 | if (!name) |
| 623 | return; |
| 624 | |
| 625 | snprintf(devpath, sizeof(devpath), "%s%s", base, name); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 626 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 627 | |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 628 | if (!strncmp(uevent->path, "/devices/", 9)) |
Andrew Boie | a885d04 | 2013-09-13 17:41:20 -0700 | [diff] [blame] | 629 | links = get_block_device_symlinks(uevent); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 630 | |
| 631 | handle_device(uevent->action, devpath, uevent->path, 1, |
| 632 | uevent->major, uevent->minor, links); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 635 | #define DEVPATH_LEN 96 |
| 636 | |
| 637 | static bool assemble_devpath(char *devpath, const char *dirname, |
| 638 | const char *devname) |
| 639 | { |
| 640 | int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname); |
| 641 | if (s < 0) { |
| 642 | ERROR("failed to assemble device path (%s); ignoring event\n", |
| 643 | strerror(errno)); |
| 644 | return false; |
| 645 | } else if (s >= DEVPATH_LEN) { |
| 646 | ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n", |
| 647 | dirname, devname, DEVPATH_LEN); |
| 648 | return false; |
| 649 | } |
| 650 | return true; |
| 651 | } |
| 652 | |
| 653 | static void mkdir_recursive_for_devpath(const char *devpath) |
| 654 | { |
| 655 | char dir[DEVPATH_LEN]; |
| 656 | char *slash; |
| 657 | |
| 658 | strcpy(dir, devpath); |
| 659 | slash = strrchr(dir, '/'); |
| 660 | *slash = '\0'; |
| 661 | mkdir_recursive(dir, 0755); |
| 662 | } |
| 663 | |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 664 | static inline void __attribute__((__deprecated__)) kernel_logger() |
| 665 | { |
| 666 | INFO("kernel logger is deprecated\n"); |
| 667 | } |
| 668 | |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 669 | static void handle_generic_device_event(struct uevent *uevent) |
| 670 | { |
| 671 | char *base; |
| 672 | const char *name; |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 673 | char devpath[DEVPATH_LEN] = {0}; |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 674 | char **links = NULL; |
| 675 | |
| 676 | name = parse_device_name(uevent, 64); |
| 677 | if (!name) |
| 678 | return; |
| 679 | |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 680 | struct ueventd_subsystem *subsystem = |
| 681 | ueventd_subsystem_find_by_name(uevent->subsystem); |
| 682 | |
| 683 | if (subsystem) { |
| 684 | const char *devname; |
| 685 | |
| 686 | switch (subsystem->devname_src) { |
| 687 | case DEVNAME_UEVENT_DEVNAME: |
| 688 | devname = uevent->device_name; |
| 689 | break; |
| 690 | |
| 691 | case DEVNAME_UEVENT_DEVPATH: |
| 692 | devname = name; |
| 693 | break; |
| 694 | |
| 695 | default: |
| 696 | ERROR("%s subsystem's devpath option is not set; ignoring event\n", |
| 697 | uevent->subsystem); |
| 698 | return; |
| 699 | } |
| 700 | |
| 701 | if (!assemble_devpath(devpath, subsystem->dirname, devname)) |
| 702 | return; |
| 703 | mkdir_recursive_for_devpath(devpath); |
| 704 | } else if (!strncmp(uevent->subsystem, "usb", 3)) { |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 705 | if (!strcmp(uevent->subsystem, "usb")) { |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 706 | if (uevent->device_name) { |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 707 | if (!assemble_devpath(devpath, "/dev", uevent->device_name)) |
Greg Hackmann | f6e009e | 2013-11-21 13:26:48 -0800 | [diff] [blame] | 708 | return; |
Greg Hackmann | 3312aa8 | 2013-11-18 15:24:40 -0800 | [diff] [blame] | 709 | mkdir_recursive_for_devpath(devpath); |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 710 | } |
| 711 | else { |
| 712 | /* This imitates the file system that would be created |
| 713 | * if we were using devfs instead. |
| 714 | * Minors are broken up into groups of 128, starting at "001" |
| 715 | */ |
| 716 | int bus_id = uevent->minor / 128 + 1; |
| 717 | int device_id = uevent->minor % 128 + 1; |
| 718 | /* build directories */ |
| 719 | make_dir("/dev/bus", 0755); |
| 720 | make_dir("/dev/bus/usb", 0755); |
| 721 | snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id); |
| 722 | make_dir(devpath, 0755); |
| 723 | snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id); |
| 724 | } |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 725 | } else { |
| 726 | /* ignore other USB events */ |
| 727 | return; |
| 728 | } |
| 729 | } else if (!strncmp(uevent->subsystem, "graphics", 8)) { |
| 730 | base = "/dev/graphics/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 731 | make_dir(base, 0755); |
Lukasz Anaczkowski | e6f8d45 | 2011-09-28 15:30:07 +0200 | [diff] [blame] | 732 | } else if (!strncmp(uevent->subsystem, "drm", 3)) { |
| 733 | base = "/dev/dri/"; |
| 734 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 735 | } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) { |
| 736 | base = "/dev/oncrpc/"; |
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, "adsp", 4)) { |
| 739 | base = "/dev/adsp/"; |
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, "msm_camera", 10)) { |
| 742 | base = "/dev/msm_camera/"; |
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, "input", 5)) { |
| 745 | base = "/dev/input/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 746 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 747 | } else if(!strncmp(uevent->subsystem, "mtd", 3)) { |
| 748 | base = "/dev/mtd/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 749 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 750 | } else if(!strncmp(uevent->subsystem, "sound", 5)) { |
| 751 | base = "/dev/snd/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 752 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 753 | } else if(!strncmp(uevent->subsystem, "misc", 4) && |
| 754 | !strncmp(name, "log_", 4)) { |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 755 | kernel_logger(); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 756 | base = "/dev/log/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 757 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 758 | name += 4; |
| 759 | } else |
| 760 | base = "/dev/"; |
| 761 | links = get_character_device_symlinks(uevent); |
| 762 | |
| 763 | if (!devpath[0]) |
| 764 | snprintf(devpath, sizeof(devpath), "%s%s", base, name); |
| 765 | |
| 766 | handle_device(uevent->action, devpath, uevent->path, 0, |
| 767 | uevent->major, uevent->minor, links); |
| 768 | } |
| 769 | |
| 770 | static void handle_device_event(struct uevent *uevent) |
| 771 | { |
Ruchi Kandoi | 75b287b | 2014-04-29 19:14:37 -0700 | [diff] [blame] | 772 | 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] | 773 | fixup_sys_perms(uevent->path); |
| 774 | |
| 775 | if (!strncmp(uevent->subsystem, "block", 5)) { |
| 776 | handle_block_device_event(uevent); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 777 | } else if (!strncmp(uevent->subsystem, "platform", 8)) { |
| 778 | handle_platform_device_event(uevent); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 779 | } else { |
| 780 | handle_generic_device_event(uevent); |
| 781 | } |
| 782 | } |
| 783 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 784 | static int load_firmware(int fw_fd, int loading_fd, int data_fd) |
| 785 | { |
| 786 | struct stat st; |
| 787 | long len_to_copy; |
| 788 | int ret = 0; |
| 789 | |
| 790 | if(fstat(fw_fd, &st) < 0) |
| 791 | return -1; |
| 792 | len_to_copy = st.st_size; |
| 793 | |
| 794 | write(loading_fd, "1", 1); /* start transfer */ |
| 795 | |
| 796 | while (len_to_copy > 0) { |
| 797 | char buf[PAGE_SIZE]; |
| 798 | ssize_t nr; |
| 799 | |
| 800 | nr = read(fw_fd, buf, sizeof(buf)); |
| 801 | if(!nr) |
| 802 | break; |
| 803 | if(nr < 0) { |
| 804 | ret = -1; |
| 805 | break; |
| 806 | } |
| 807 | |
| 808 | len_to_copy -= nr; |
| 809 | while (nr > 0) { |
| 810 | ssize_t nw = 0; |
| 811 | |
| 812 | nw = write(data_fd, buf + nw, nr); |
| 813 | if(nw <= 0) { |
| 814 | ret = -1; |
| 815 | goto out; |
| 816 | } |
| 817 | nr -= nw; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | out: |
| 822 | if(!ret) |
| 823 | write(loading_fd, "0", 1); /* successful end of transfer */ |
| 824 | else |
| 825 | write(loading_fd, "-1", 2); /* abort transfer */ |
| 826 | |
| 827 | return ret; |
| 828 | } |
| 829 | |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 830 | static int is_booting(void) |
| 831 | { |
| 832 | return access("/dev/.booting", F_OK) == 0; |
| 833 | } |
| 834 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 835 | static void process_firmware_event(struct uevent *uevent) |
| 836 | { |
Iliyan Malchev | 029d44e | 2012-06-08 10:42:26 -0700 | [diff] [blame] | 837 | 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] | 838 | int l, loading_fd, data_fd, fw_fd; |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 839 | int booting = is_booting(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 840 | |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 841 | INFO("firmware: loading '%s' for '%s'\n", |
| 842 | uevent->firmware, uevent->path); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 843 | |
| 844 | l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path); |
| 845 | if (l == -1) |
| 846 | return; |
| 847 | |
| 848 | l = asprintf(&loading, "%sloading", root); |
| 849 | if (l == -1) |
| 850 | goto root_free_out; |
| 851 | |
| 852 | l = asprintf(&data, "%sdata", root); |
| 853 | if (l == -1) |
| 854 | goto loading_free_out; |
| 855 | |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 856 | l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware); |
| 857 | if (l == -1) |
| 858 | goto data_free_out; |
| 859 | |
| 860 | l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 861 | if (l == -1) |
| 862 | goto data_free_out; |
| 863 | |
Iliyan Malchev | 029d44e | 2012-06-08 10:42:26 -0700 | [diff] [blame] | 864 | l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware); |
| 865 | if (l == -1) |
| 866 | goto data_free_out; |
| 867 | |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 868 | loading_fd = open(loading, O_WRONLY|O_CLOEXEC); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 869 | if(loading_fd < 0) |
| 870 | goto file_free_out; |
| 871 | |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 872 | data_fd = open(data, O_WRONLY|O_CLOEXEC); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 873 | if(data_fd < 0) |
| 874 | goto loading_close_out; |
| 875 | |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 876 | try_loading_again: |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 877 | fw_fd = open(file1, O_RDONLY|O_CLOEXEC); |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 878 | if(fw_fd < 0) { |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 879 | fw_fd = open(file2, O_RDONLY|O_CLOEXEC); |
Benoit Goby | 609d882 | 2010-11-09 18:10:24 -0800 | [diff] [blame] | 880 | if (fw_fd < 0) { |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 881 | fw_fd = open(file3, O_RDONLY|O_CLOEXEC); |
Iliyan Malchev | 029d44e | 2012-06-08 10:42:26 -0700 | [diff] [blame] | 882 | if (fw_fd < 0) { |
| 883 | if (booting) { |
| 884 | /* If we're not fully booted, we may be missing |
| 885 | * filesystems needed for firmware, wait and retry. |
| 886 | */ |
| 887 | usleep(100000); |
| 888 | booting = is_booting(); |
| 889 | goto try_loading_again; |
| 890 | } |
| 891 | INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno); |
| 892 | write(loading_fd, "-1", 2); |
| 893 | goto data_close_out; |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 894 | } |
Benoit Goby | 609d882 | 2010-11-09 18:10:24 -0800 | [diff] [blame] | 895 | } |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 896 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 897 | |
| 898 | if(!load_firmware(fw_fd, loading_fd, data_fd)) |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 899 | 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] | 900 | else |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 901 | 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] | 902 | |
| 903 | close(fw_fd); |
| 904 | data_close_out: |
| 905 | close(data_fd); |
| 906 | loading_close_out: |
| 907 | close(loading_fd); |
| 908 | file_free_out: |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 909 | free(file1); |
| 910 | free(file2); |
Ajay Dudani | 76c5889 | 2013-06-12 15:55:26 -0700 | [diff] [blame] | 911 | free(file3); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 912 | data_free_out: |
| 913 | free(data); |
| 914 | loading_free_out: |
| 915 | free(loading); |
| 916 | root_free_out: |
| 917 | free(root); |
| 918 | } |
| 919 | |
| 920 | static void handle_firmware_event(struct uevent *uevent) |
| 921 | { |
| 922 | pid_t pid; |
| 923 | |
| 924 | if(strcmp(uevent->subsystem, "firmware")) |
| 925 | return; |
| 926 | |
| 927 | if(strcmp(uevent->action, "add")) |
| 928 | return; |
| 929 | |
| 930 | /* we fork, to avoid making large memory allocations in init proper */ |
| 931 | pid = fork(); |
| 932 | if (!pid) { |
| 933 | process_firmware_event(uevent); |
Kenny Root | 17baff4 | 2014-08-20 16:16:44 -0700 | [diff] [blame] | 934 | _exit(EXIT_SUCCESS); |
| 935 | } else if (pid < 0) { |
| 936 | log_event_print("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] | 937 | } |
| 938 | } |
| 939 | |
Ruchi Kandoi | c603720 | 2014-06-23 11:22:09 -0700 | [diff] [blame] | 940 | #define UEVENT_MSG_LEN 2048 |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 941 | void handle_device_fd() |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 942 | { |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 943 | char msg[UEVENT_MSG_LEN+2]; |
| 944 | int n; |
Nick Kralevich | 57de8b8 | 2011-05-11 14:58:24 -0700 | [diff] [blame] | 945 | 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] | 946 | if(n >= UEVENT_MSG_LEN) /* overflow -- discard */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 947 | continue; |
| 948 | |
| 949 | msg[n] = '\0'; |
| 950 | msg[n+1] = '\0'; |
| 951 | |
Nick Kralevich | 5f5d5c8 | 2010-07-19 14:31:20 -0700 | [diff] [blame] | 952 | struct uevent uevent; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 953 | parse_event(msg, &uevent); |
| 954 | |
Stephen Smalley | e2eb69d | 2013-04-16 09:30:30 -0400 | [diff] [blame] | 955 | if (sehandle && selinux_status_updated() > 0) { |
| 956 | struct selabel_handle *sehandle2; |
| 957 | sehandle2 = selinux_android_file_context_handle(); |
| 958 | if (sehandle2) { |
| 959 | selabel_close(sehandle); |
| 960 | sehandle = sehandle2; |
| 961 | } |
| 962 | } |
| 963 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 964 | handle_device_event(&uevent); |
| 965 | handle_firmware_event(&uevent); |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | /* Coldboot walks parts of the /sys tree and pokes the uevent files |
| 970 | ** to cause the kernel to regenerate device add events that happened |
| 971 | ** before init's device manager was started |
| 972 | ** |
| 973 | ** We drain any pending events from the netlink socket every time |
| 974 | ** we poke another uevent file to make sure we don't overrun the |
| 975 | ** socket's buffer. |
| 976 | */ |
| 977 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 978 | static void do_coldboot(DIR *d) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 979 | { |
| 980 | struct dirent *de; |
| 981 | int dfd, fd; |
| 982 | |
| 983 | dfd = dirfd(d); |
| 984 | |
| 985 | fd = openat(dfd, "uevent", O_WRONLY); |
| 986 | if(fd >= 0) { |
| 987 | write(fd, "add\n", 4); |
| 988 | close(fd); |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 989 | handle_device_fd(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | while((de = readdir(d))) { |
| 993 | DIR *d2; |
| 994 | |
| 995 | if(de->d_type != DT_DIR || de->d_name[0] == '.') |
| 996 | continue; |
| 997 | |
| 998 | fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); |
| 999 | if(fd < 0) |
| 1000 | continue; |
| 1001 | |
| 1002 | d2 = fdopendir(fd); |
| 1003 | if(d2 == 0) |
| 1004 | close(fd); |
| 1005 | else { |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 1006 | do_coldboot(d2); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1007 | closedir(d2); |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 1012 | static void coldboot(const char *path) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1013 | { |
| 1014 | DIR *d = opendir(path); |
| 1015 | if(d) { |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 1016 | do_coldboot(d); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1017 | closedir(d); |
| 1018 | } |
| 1019 | } |
| 1020 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 1021 | void device_init(void) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1022 | { |
| 1023 | suseconds_t t0, t1; |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 1024 | struct stat info; |
| 1025 | int fd; |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 1026 | |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 1027 | sehandle = NULL; |
| 1028 | if (is_selinux_enabled() > 0) { |
| 1029 | sehandle = selinux_android_file_context_handle(); |
Stephen Smalley | e2eb69d | 2013-04-16 09:30:30 -0400 | [diff] [blame] | 1030 | selinux_status_open(true); |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 1031 | } |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 1032 | |
Andrew Boie | d562ca7 | 2012-12-04 15:47:20 -0800 | [diff] [blame] | 1033 | /* is 256K enough? udev uses 16MB! */ |
| 1034 | device_fd = uevent_open_socket(256*1024, true); |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 1035 | if(device_fd < 0) |
| 1036 | return; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1037 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 1038 | fcntl(device_fd, F_SETFD, FD_CLOEXEC); |
| 1039 | fcntl(device_fd, F_SETFL, O_NONBLOCK); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1040 | |
Andreas Gampe | a016c42 | 2014-11-24 19:52:41 -0800 | [diff] [blame] | 1041 | if (stat(COLDBOOT_DONE, &info) < 0) { |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 1042 | t0 = get_usecs(); |
| 1043 | coldboot("/sys/class"); |
| 1044 | coldboot("/sys/block"); |
| 1045 | coldboot("/sys/devices"); |
| 1046 | t1 = get_usecs(); |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 1047 | fd = open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000); |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 1048 | close(fd); |
| 1049 | log_event_print("coldboot %ld uS\n", ((long) (t1 - t0))); |
Andreas Gampe | a016c42 | 2014-11-24 19:52:41 -0800 | [diff] [blame] | 1050 | // t0 & t1 are unused if the log isn't doing anything. |
| 1051 | (void)t0; |
| 1052 | (void)t1; |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 1053 | } else { |
| 1054 | log_event_print("skipping coldboot, already done\n"); |
| 1055 | } |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 1056 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1057 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 1058 | int get_device_fd() |
| 1059 | { |
| 1060 | return device_fd; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1061 | } |