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