blob: 27a20a435a7cdb0d3ce231d7921efa4641aef60d [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
Mark Salyzyn154f4602014-02-20 14:59:07 -08002 * Copyright (C) 2007-2014 The Android Open Source Project
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07003 *
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
Elliott Hughesf39f7f12016-08-31 14:41:51 -070017#include <dirent.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070018#include <errno.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070019#include <fcntl.h>
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070020#include <fnmatch.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070021#include <libgen.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -080022#include <stddef.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070023#include <stdio.h>
24#include <stdlib.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070025#include <string.h>
Elliott Hughes632e99a2016-11-12 11:44:16 -080026#include <sys/sendfile.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070027#include <sys/socket.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070028#include <sys/stat.h>
29#include <sys/time.h>
30#include <sys/types.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070031#include <sys/un.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070032#include <sys/wait.h>
33#include <unistd.h>
34
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070035#include <linux/netlink.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050036
James Hawkins588a2ca2016-02-18 14:52:46 -080037#include <memory>
Elliott Hughes290a2282016-11-14 17:08:47 -080038#include <thread>
James Hawkins588a2ca2016-02-18 14:52:46 -080039
Stephen Smalleye46f9d52012-01-13 08:48:47 -050040#include <selinux/selinux.h>
41#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040042#include <selinux/android.h>
Stephen Smalleye2eb69d2013-04-16 09:30:30 -040043#include <selinux/avc.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050044
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070045#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070046
Biao Ludc848562016-01-28 16:10:54 +080047#include <android-base/file.h>
Rob Herring6de783a2016-05-06 10:06:59 -050048#include <android-base/stringprintf.h>
Tom Cherryd43b6152017-06-19 17:41:41 -070049#include <android-base/strings.h>
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +080050#include <android-base/unique_fd.h>
Dima Zavinda04c522011-09-01 17:09:44 -070051#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100052#include <cutils/uevent.h>
53
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054#include "devices.h"
Greg Hackmann3312aa82013-11-18 15:24:40 -080055#include "ueventd_parser.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070056#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070057#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070058
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070059#define SYSFS_PREFIX "/sys"
Daniel Rosenbergd1d96022015-03-20 15:10:29 -070060static const char *firmware_dirs[] = { "/etc/firmware",
61 "/vendor/firmware",
62 "/firmware/image" };
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070063
Stephen Smalleye096e362012-06-11 13:37:39 -040064extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050065
Sandeep Patil35403eb2017-02-08 20:27:12 -080066static android::base::unique_fd device_fd;
Colin Cross0dd7ca62010-04-13 19:25:51 -070067
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068struct perms_ {
69 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070070 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070071 mode_t perm;
72 unsigned int uid;
73 unsigned int gid;
74 unsigned short prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070075 unsigned short wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070077
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070078struct perm_node {
79 struct perms_ dp;
80 struct listnode plist;
81};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070082
83static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070084static list_declare(dev_perms);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070085
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070086int add_dev_perms(const char *name, const char *attr,
87 mode_t perm, unsigned int uid, unsigned int gid,
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070088 unsigned short prefix,
89 unsigned short wildcard) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -080090 struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070091 if (!node)
92 return -ENOMEM;
93
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070094 node->dp.name = strdup(name);
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -080095 if (!node->dp.name) {
96 free(node);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070097 return -ENOMEM;
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -080098 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070099
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700100 if (attr) {
101 node->dp.attr = strdup(attr);
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800102 if (!node->dp.attr) {
103 free(node->dp.name);
104 free(node);
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700105 return -ENOMEM;
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800106 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700107 }
108
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700109 node->dp.perm = perm;
110 node->dp.uid = uid;
111 node->dp.gid = gid;
112 node->dp.prefix = prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700113 node->dp.wildcard = wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700114
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700115 if (attr)
116 list_add_tail(&sys_perms, &node->plist);
117 else
118 list_add_tail(&dev_perms, &node->plist);
119
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700120 return 0;
121}
122
Colin Cross43d537e2014-07-02 13:08:13 -0700123static bool perm_path_matches(const char *path, struct perms_ *dp)
124{
125 if (dp->prefix) {
126 if (strncmp(path, dp->name, strlen(dp->name)) == 0)
127 return true;
128 } else if (dp->wildcard) {
129 if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
130 return true;
131 } else {
132 if (strcmp(path, dp->name) == 0)
133 return true;
134 }
135
136 return false;
137}
138
Rob Herring6de783a2016-05-06 10:06:59 -0500139static bool match_subsystem(perms_* dp, const char* pattern,
140 const char* path, const char* subsystem) {
141 if (!pattern || !subsystem || strstr(dp->name, subsystem) == NULL) {
142 return false;
143 }
Rob Herringe5636a32016-05-06 12:28:48 -0500144
Rob Herring6de783a2016-05-06 10:06:59 -0500145 std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path));
146 return perm_path_matches(subsys_path.c_str(), dp);
147}
Rob Herringe5636a32016-05-06 12:28:48 -0500148
Rob Herring6de783a2016-05-06 10:06:59 -0500149static void fixup_sys_perms(const char* upath, const char* subsystem) {
150 // upaths omit the "/sys" that paths in this list
151 // contain, so we prepend it...
152 std::string path = std::string(SYSFS_PREFIX) + upath;
153
154 listnode* node;
Rob Herringe5636a32016-05-06 12:28:48 -0500155 list_for_each(node, &sys_perms) {
Rob Herring6de783a2016-05-06 10:06:59 -0500156 perms_* dp = &(node_to_item(node, perm_node, plist))->dp;
157 if (match_subsystem(dp, SYSFS_PREFIX "/class/%s/%s", path.c_str(), subsystem)) {
158 ; // matched
159 } else if (match_subsystem(dp, SYSFS_PREFIX "/bus/%s/devices/%s", path.c_str(), subsystem)) {
160 ; // matched
161 } else if (!perm_path_matches(path.c_str(), dp)) {
162 continue;
Rob Herringe5636a32016-05-06 12:28:48 -0500163 }
164
165 std::string attr_file = path + "/" + dp->attr;
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700166 LOG(INFO) << "fixup " << attr_file
167 << " " << dp->uid << " " << dp->gid << " " << std::oct << dp->perm;
Rob Herringe5636a32016-05-06 12:28:48 -0500168 chown(attr_file.c_str(), dp->uid, dp->gid);
169 chmod(attr_file.c_str(), dp->perm);
170 }
171
172 if (access(path.c_str(), F_OK) == 0) {
Dmitry Shmidt7eed4742016-07-28 13:55:39 -0700173 LOG(VERBOSE) << "restorecon_recursive: " << path;
Paul Lawrencea8d84342016-11-14 15:40:18 -0800174 restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE);
Rob Herringe5636a32016-05-06 12:28:48 -0500175 }
176}
177
Colin Cross43d537e2014-07-02 13:08:13 -0700178static mode_t get_device_perm(const char *path, const char **links,
179 unsigned *uid, unsigned *gid)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700180{
Colin Cross44b65d02010-04-20 14:32:50 -0700181 struct listnode *node;
182 struct perm_node *perm_node;
183 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700184
Colin Cross44b65d02010-04-20 14:32:50 -0700185 /* search the perms list in reverse so that ueventd.$hardware can
186 * override ueventd.rc
187 */
188 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700189 bool match = false;
190
Colin Cross44b65d02010-04-20 14:32:50 -0700191 perm_node = node_to_item(node, struct perm_node, plist);
192 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700193
Colin Cross43d537e2014-07-02 13:08:13 -0700194 if (perm_path_matches(path, dp)) {
195 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700196 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700197 if (links) {
198 int i;
199 for (i = 0; links[i]; i++) {
200 if (perm_path_matches(links[i], dp)) {
201 match = true;
202 break;
203 }
204 }
205 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700206 }
Colin Cross43d537e2014-07-02 13:08:13 -0700207
208 if (match) {
209 *uid = dp->uid;
210 *gid = dp->gid;
211 return dp->perm;
212 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700213 }
Colin Cross44b65d02010-04-20 14:32:50 -0700214 /* Default if nothing found. */
215 *uid = 0;
216 *gid = 0;
217 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700218}
219
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700220static void make_device(const char *path,
Elliott Hughesf682b472015-02-06 12:19:48 -0800221 const char */*upath*/,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400222 int block, int major, int minor,
223 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700224{
225 unsigned uid;
226 unsigned gid;
227 mode_t mode;
228 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500229 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700230
Colin Cross43d537e2014-07-02 13:08:13 -0700231 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700232
Sandeep Patilea239832017-02-03 07:51:55 -0800233 if (sehandle) {
234 if (selabel_lookup_best_match(sehandle, &secontext, path, links, mode)) {
235 PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
236 return;
237 }
238 setfscreatecon(secontext);
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300239 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700240
Colin Cross17dcc5c2010-09-03 12:25:34 -0700241 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800242 /* Temporarily change egid to avoid race condition setting the gid of the
243 * device node. Unforunately changing the euid would prevent creation of
244 * some device nodes, so the uid has to be set with chown() and is still
245 * racy. Fixing the gid race at least fixed the issue with system_server
246 * opening dynamic input devices under the AID_INPUT gid. */
Tom Cherry0506b182017-02-23 13:46:09 -0800247 if (setegid(gid)) {
248 PLOG(ERROR) << "setegid(" << gid << ") for " << path << " device failed";
249 goto out;
250 }
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300251 /* If the node already exists update its SELinux label to handle cases when
252 * it was created with the wrong context during coldboot procedure. */
Sandeep Patilea239832017-02-03 07:51:55 -0800253 if (mknod(path, mode, dev) && (errno == EEXIST) && secontext) {
William Roberts397de142016-06-02 09:53:44 -0700254
255 char* fcon = nullptr;
256 int rc = lgetfilecon(path, &fcon);
257 if (rc < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700258 PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
William Roberts397de142016-06-02 09:53:44 -0700259 goto out;
260 }
261
262 bool different = strcmp(fcon, secontext) != 0;
263 freecon(fcon);
264
265 if (different && lsetfilecon(path, secontext)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700266 PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path << "' device";
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300267 }
268 }
William Roberts397de142016-06-02 09:53:44 -0700269
270out:
Nick Pelly6405c692010-01-21 18:13:39 -0800271 chown(path, uid, -1);
Tom Cherry0506b182017-02-23 13:46:09 -0800272 if (setegid(AID_ROOT)) {
273 PLOG(FATAL) << "setegid(AID_ROOT) failed";
274 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700275
Sandeep Patilea239832017-02-03 07:51:55 -0800276 if (secontext) {
277 freecon(secontext);
278 setfscreatecon(NULL);
279 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700280}
281
Tom Cherryd43b6152017-06-19 17:41:41 -0700282// Given a path that may start with a platform device, find the parent platform device by finding a
283// parent directory with a 'subsystem' symlink that points to the platform bus.
284// If it doesn't start with a platform device, return false
285bool FindPlatformDevice(std::string path, std::string* platform_device_path) {
286 platform_device_path->clear();
Dima Zavinf395c922013-03-06 16:23:57 -0800287
Tom Cherryd43b6152017-06-19 17:41:41 -0700288 static const std::string kSysfsMountPoint = "/sys";
Colin Crossfadb85e2011-03-30 18:32:12 -0700289
Tom Cherryd43b6152017-06-19 17:41:41 -0700290 // Uevents don't contain the mount point, so we need to add it here.
291 path.insert(0, kSysfsMountPoint);
Colin Crossfadb85e2011-03-30 18:32:12 -0700292
Tom Cherryd43b6152017-06-19 17:41:41 -0700293 std::string directory = android::base::Dirname(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700294
Tom Cherryd43b6152017-06-19 17:41:41 -0700295 while (directory != "/" && directory != ".") {
296 std::string subsystem_link_path;
297 if (android::base::Realpath(directory + "/subsystem", &subsystem_link_path) &&
298 subsystem_link_path == kSysfsMountPoint + "/bus/platform") {
299 // We need to remove the mount point that we added above before returning.
300 directory.erase(0, kSysfsMountPoint.size());
301 *platform_device_path = directory;
302 return true;
Colin Crossfadb85e2011-03-30 18:32:12 -0700303 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700304
Tom Cherryd43b6152017-06-19 17:41:41 -0700305 auto last_slash = path.rfind('/');
306 if (last_slash == std::string::npos) return false;
Sandeep Patil35403eb2017-02-08 20:27:12 -0800307
Tom Cherryd43b6152017-06-19 17:41:41 -0700308 path.erase(last_slash);
309 directory = android::base::Dirname(path);
Sandeep Patil35403eb2017-02-08 20:27:12 -0800310 }
Tom Cherryd43b6152017-06-19 17:41:41 -0700311
312 return false;
Sandeep Patil35403eb2017-02-08 20:27:12 -0800313}
314
Andrew Boiea885d042013-09-13 17:41:20 -0700315/* Given a path that may start with a PCI device, populate the supplied buffer
316 * with the PCI domain/bus number and the peripheral ID and return 0.
317 * If it doesn't start with a PCI device, or there is some error, return -1 */
318static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
319{
320 const char *start, *end;
321
322 if (strncmp(path, "/devices/pci", 12))
323 return -1;
324
325 /* Beginning of the prefix is the initial "pci" after "/devices/" */
326 start = path + 9;
327
328 /* End of the prefix is two path '/' later, capturing the domain/bus number
329 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
330 end = strchr(start, '/');
331 if (!end)
332 return -1;
333 end = strchr(end + 1, '/');
334 if (!end)
335 return -1;
336
337 /* Make sure we have enough room for the string plus null terminator */
338 if (end - start + 1 > buf_sz)
339 return -1;
340
341 strncpy(buf, start, end - start);
342 buf[end - start] = '\0';
343 return 0;
344}
345
Jeremy Compostella937309d2017-03-03 16:27:29 +0100346/* Given a path that may start with a virtual block device, populate
347 * the supplied buffer with the virtual block device ID and return 0.
348 * If it doesn't start with a virtual block device, or there is some
349 * error, return -1 */
350static int find_vbd_device_prefix(const char *path, char *buf, ssize_t buf_sz)
351{
352 const char *start, *end;
353
354 /* Beginning of the prefix is the initial "vbd-" after "/devices/" */
355 if (strncmp(path, "/devices/vbd-", 13))
356 return -1;
357
358 /* End of the prefix is one path '/' later, capturing the
359 virtual block device ID. Example: 768 */
360 start = path + 13;
361 end = strchr(start, '/');
362 if (!end)
363 return -1;
364
365 /* Make sure we have enough room for the string plus null terminator */
366 if (end - start + 1 > buf_sz)
367 return -1;
368
369 strncpy(buf, start, end - start);
370 buf[end - start] = '\0';
371 return 0;
372}
373
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700374static void parse_event(const char *msg, struct uevent *uevent)
375{
376 uevent->action = "";
377 uevent->path = "";
378 uevent->subsystem = "";
379 uevent->firmware = "";
380 uevent->major = -1;
381 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700382 uevent->partition_name = NULL;
383 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700384 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700385
386 /* currently ignoring SEQNUM */
387 while(*msg) {
388 if(!strncmp(msg, "ACTION=", 7)) {
389 msg += 7;
390 uevent->action = msg;
391 } else if(!strncmp(msg, "DEVPATH=", 8)) {
392 msg += 8;
393 uevent->path = msg;
394 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
395 msg += 10;
396 uevent->subsystem = msg;
397 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
398 msg += 9;
399 uevent->firmware = msg;
400 } else if(!strncmp(msg, "MAJOR=", 6)) {
401 msg += 6;
402 uevent->major = atoi(msg);
403 } else if(!strncmp(msg, "MINOR=", 6)) {
404 msg += 6;
405 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700406 } else if(!strncmp(msg, "PARTN=", 6)) {
407 msg += 6;
408 uevent->partition_num = atoi(msg);
409 } else if(!strncmp(msg, "PARTNAME=", 9)) {
410 msg += 9;
411 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700412 } else if(!strncmp(msg, "DEVNAME=", 8)) {
413 msg += 8;
414 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700415 }
416
Wei Zhongf97b8872012-03-23 14:15:34 -0700417 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700418 while(*msg++)
419 ;
420 }
421
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800422 if (LOG_UEVENTS) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700423 LOG(INFO) << android::base::StringPrintf("event { '%s', '%s', '%s', '%s', %d, %d }",
424 uevent->action, uevent->path, uevent->subsystem,
425 uevent->firmware, uevent->major, uevent->minor);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800426 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700427}
428
Benoit Gobyd2278632010-08-03 14:36:02 -0700429static char **get_character_device_symlinks(struct uevent *uevent)
430{
431 const char *parent;
Dan Austin60b976d2016-03-28 14:22:12 -0700432 const char *slash;
Benoit Gobyd2278632010-08-03 14:36:02 -0700433 char **links;
434 int link_num = 0;
435 int width;
436
Tom Cherryd43b6152017-06-19 17:41:41 -0700437 std::string platform_device;
438 if (!FindPlatformDevice(uevent->path, &platform_device)) return nullptr;
Benoit Gobyd2278632010-08-03 14:36:02 -0700439
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800440 links = (char**) malloc(sizeof(char *) * 2);
Benoit Gobyd2278632010-08-03 14:36:02 -0700441 if (!links)
442 return NULL;
443 memset(links, 0, sizeof(char *) * 2);
444
445 /* skip "/devices/platform/<driver>" */
Tom Cherryd43b6152017-06-19 17:41:41 -0700446 parent = strchr(uevent->path + platform_device.size(), '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100447 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700448 goto err;
449
450 if (!strncmp(parent, "/usb", 4)) {
451 /* skip root hub name and device. use device interface */
452 while (*++parent && *parent != '/');
453 if (*parent)
454 while (*++parent && *parent != '/');
455 if (!*parent)
456 goto err;
457 slash = strchr(++parent, '/');
458 if (!slash)
459 goto err;
460 width = slash - parent;
461 if (width <= 0)
462 goto err;
463
464 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
465 link_num++;
466 else
467 links[link_num] = NULL;
468 mkdir("/dev/usb", 0755);
469 }
470 else {
471 goto err;
472 }
473
474 return links;
475err:
476 free(links);
477 return NULL;
478}
479
Bowgo Tsai1fa02512017-05-15 16:34:52 +0800480char** get_block_device_symlinks(struct uevent* uevent) {
Dan Austin60b976d2016-03-28 14:22:12 -0700481 const char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700482 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700483 char buf[256];
484 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700485 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700486 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700487
Tom Cherryd43b6152017-06-19 17:41:41 -0700488 std::string device;
489 if (FindPlatformDevice(uevent->path, &device)) {
490 // Skip /devices/platform or /devices/ if present
491 static const std::string devices_platform_prefix = "/devices/platform/";
492 static const std::string devices_prefix = "/devices/";
493
494 if (android::base::StartsWith(device, devices_platform_prefix.c_str())) {
495 device = device.substr(devices_platform_prefix.length());
496 } else if (android::base::StartsWith(device, devices_prefix.c_str())) {
497 device = device.substr(devices_prefix.length());
498 }
499
Andrew Boiea885d042013-09-13 17:41:20 -0700500 type = "platform";
501 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
502 device = buf;
503 type = "pci";
Jeremy Compostella937309d2017-03-03 16:27:29 +0100504 } else if (!find_vbd_device_prefix(uevent->path, buf, sizeof(buf))) {
505 device = buf;
506 type = "vbd";
Andrew Boiea885d042013-09-13 17:41:20 -0700507 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800508 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700509 }
Dima Zavinf395c922013-03-06 16:23:57 -0800510
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800511 char **links = (char**) malloc(sizeof(char *) * 4);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700512 if (!links)
513 return NULL;
514 memset(links, 0, sizeof(char *) * 4);
515
Sandeep Patil35403eb2017-02-08 20:27:12 -0800516 LOG(VERBOSE) << "found " << type << " device " << device;
Colin Crossfadb85e2011-03-30 18:32:12 -0700517
Tom Cherryd43b6152017-06-19 17:41:41 -0700518 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device.c_str());
Colin Crossb0ab94b2010-04-08 16:16:20 -0700519
520 if (uevent->partition_name) {
521 p = strdup(uevent->partition_name);
522 sanitize(p);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700523 if (strcmp(uevent->partition_name, p)) {
524 LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '" << p << "'";
525 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700526 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
527 link_num++;
528 else
529 links[link_num] = NULL;
530 free(p);
531 }
532
533 if (uevent->partition_num >= 0) {
534 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
535 link_num++;
536 else
537 links[link_num] = NULL;
538 }
539
Dima Zavinf395c922013-03-06 16:23:57 -0800540 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700541 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
542 link_num++;
543 else
544 links[link_num] = NULL;
545
546 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700547}
548
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700549static void make_link_init(const char* oldpath, const char* newpath) {
550 const char* slash = strrchr(newpath, '/');
551 if (!slash) return;
552
553 if (mkdir_recursive(dirname(newpath), 0755)) {
554 PLOG(ERROR) << "Failed to create directory " << dirname(newpath);
555 }
556
557 if (symlink(oldpath, newpath) && errno != EEXIST) {
558 PLOG(ERROR) << "Failed to symlink " << oldpath << " to " << newpath;
559 }
560}
561
562static void remove_link(const char* oldpath, const char* newpath) {
563 std::string path;
564 if (android::base::Readlink(newpath, &path) && path == oldpath) unlink(newpath);
565}
566
Colin Crosseb5ba832011-03-30 17:37:17 -0700567static void handle_device(const char *action, const char *devpath,
568 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700569{
Colin Crosseb5ba832011-03-30 17:37:17 -0700570 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400571 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700572 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700573 for (int i = 0; links[i]; i++) {
Chris Fries79f33842013-09-05 13:19:21 -0500574 make_link_init(devpath, links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700575 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700576 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700577 }
578
Colin Crosseb5ba832011-03-30 17:37:17 -0700579 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700580 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700581 for (int i = 0; links[i]; i++) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700582 remove_link(devpath, links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700583 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700584 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700585 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700586 }
587
588 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700589 for (int i = 0; links[i]; i++) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700590 free(links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700591 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700592 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700593 }
594}
595
Colin Crosseb5ba832011-03-30 17:37:17 -0700596static const char *parse_device_name(struct uevent *uevent, unsigned int len)
597{
598 const char *name;
599
600 /* if it's not a /dev device, nothing else to do */
601 if((uevent->major < 0) || (uevent->minor < 0))
602 return NULL;
603
604 /* do we have a name? */
605 name = strrchr(uevent->path, '/');
606 if(!name)
607 return NULL;
608 name++;
609
610 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800611 if(strlen(name) > len) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700612 LOG(ERROR) << "DEVPATH=" << name << " exceeds " << len << "-character limit on filename; ignoring event";
Colin Crosseb5ba832011-03-30 17:37:17 -0700613 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800614 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700615
616 return name;
617}
618
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800619#define DEVPATH_LEN 96
620#define MAX_DEV_NAME 64
621
Colin Crosseb5ba832011-03-30 17:37:17 -0700622static void handle_block_device_event(struct uevent *uevent)
623{
624 const char *base = "/dev/block/";
625 const char *name;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800626 char devpath[DEVPATH_LEN];
Colin Crosseb5ba832011-03-30 17:37:17 -0700627 char **links = NULL;
628
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800629 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700630 if (!name)
631 return;
632
633 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500634 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700635
Dima Zavinf395c922013-03-06 16:23:57 -0800636 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700637 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700638
639 handle_device(uevent->action, devpath, uevent->path, 1,
640 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700641}
642
Greg Hackmann3312aa82013-11-18 15:24:40 -0800643static bool assemble_devpath(char *devpath, const char *dirname,
644 const char *devname)
645{
646 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
647 if (s < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700648 PLOG(ERROR) << "failed to assemble device path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800649 return false;
650 } else if (s >= DEVPATH_LEN) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700651 LOG(ERROR) << dirname << "/" << devname
652 << " exceeds " << DEVPATH_LEN << "-character limit on path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800653 return false;
654 }
655 return true;
656}
657
658static void mkdir_recursive_for_devpath(const char *devpath)
659{
660 char dir[DEVPATH_LEN];
661 char *slash;
662
663 strcpy(dir, devpath);
664 slash = strrchr(dir, '/');
665 *slash = '\0';
666 mkdir_recursive(dir, 0755);
667}
668
Colin Crosseb5ba832011-03-30 17:37:17 -0700669static void handle_generic_device_event(struct uevent *uevent)
670{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800671 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700672 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800673 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700674 char **links = NULL;
675
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800676 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700677 if (!name)
678 return;
679
Greg Hackmann3312aa82013-11-18 15:24:40 -0800680 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:
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700696 LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800697 return;
698 }
699
700 if (!assemble_devpath(devpath, subsystem->dirname, devname))
701 return;
702 mkdir_recursive_for_devpath(devpath);
703 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700704 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700705 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800706 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800707 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800708 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700709 }
710 else {
711 /* This imitates the file system that would be created
712 * if we were using devfs instead.
713 * Minors are broken up into groups of 128, starting at "001"
714 */
715 int bus_id = uevent->minor / 128 + 1;
716 int device_id = uevent->minor % 128 + 1;
717 /* build directories */
718 make_dir("/dev/bus", 0755);
719 make_dir("/dev/bus/usb", 0755);
720 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
721 make_dir(devpath, 0755);
722 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
723 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700724 } else {
725 /* ignore other USB events */
726 return;
727 }
728 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
729 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500730 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200731 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
732 base = "/dev/dri/";
733 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700734 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
735 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500736 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700737 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
738 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500739 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700740 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
741 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500742 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700743 } else if(!strncmp(uevent->subsystem, "input", 5)) {
744 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500745 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700746 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
747 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500748 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700749 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
750 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500751 make_dir(base, 0755);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700752 } else if(!strncmp(uevent->subsystem, "misc", 4) && !strncmp(name, "log_", 4)) {
753 LOG(INFO) << "kernel logger is deprecated";
Colin Crosseb5ba832011-03-30 17:37:17 -0700754 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500755 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700756 name += 4;
757 } else
758 base = "/dev/";
759 links = get_character_device_symlinks(uevent);
760
761 if (!devpath[0])
762 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
763
764 handle_device(uevent->action, devpath, uevent->path, 0,
765 uevent->major, uevent->minor, links);
766}
767
768static void handle_device_event(struct uevent *uevent)
769{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700770 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Rob Herring6de783a2016-05-06 10:06:59 -0500771 fixup_sys_perms(uevent->path, uevent->subsystem);
Colin Crosseb5ba832011-03-30 17:37:17 -0700772
773 if (!strncmp(uevent->subsystem, "block", 5)) {
774 handle_block_device_event(uevent);
775 } else {
776 handle_generic_device_event(uevent);
777 }
778}
779
Elliott Hughes632e99a2016-11-12 11:44:16 -0800780static void load_firmware(uevent* uevent, const std::string& root,
781 int fw_fd, size_t fw_size,
782 int loading_fd, int data_fd) {
783 // Start transfer.
784 android::base::WriteFully(loading_fd, "1", 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700785
Elliott Hughes632e99a2016-11-12 11:44:16 -0800786 // Copy the firmware.
787 int rc = sendfile(data_fd, fw_fd, nullptr, fw_size);
788 if (rc == -1) {
789 PLOG(ERROR) << "firmware: sendfile failed { '" << root << "', '" << uevent->firmware << "' }";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700790 }
791
Elliott Hughes632e99a2016-11-12 11:44:16 -0800792 // Tell the firmware whether to abort or commit.
793 const char* response = (rc != -1) ? "0" : "-1";
794 android::base::WriteFully(loading_fd, response, strlen(response));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700795}
796
Elliott Hughes632e99a2016-11-12 11:44:16 -0800797static int is_booting() {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700798 return access("/dev/.booting", F_OK) == 0;
799}
800
Elliott Hughes632e99a2016-11-12 11:44:16 -0800801static void process_firmware_event(uevent* uevent) {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700802 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700803
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700804 LOG(INFO) << "firmware: loading '" << uevent->firmware << "' for '" << uevent->path << "'";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700805
Elliott Hughes632e99a2016-11-12 11:44:16 -0800806 std::string root = android::base::StringPrintf("/sys%s", uevent->path);
807 std::string loading = root + "/loading";
808 std::string data = root + "/data";
809
810 android::base::unique_fd loading_fd(open(loading.c_str(), O_WRONLY|O_CLOEXEC));
811 if (loading_fd == -1) {
812 PLOG(ERROR) << "couldn't open firmware loading fd for " << uevent->firmware;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700813 return;
Elliott Hughes632e99a2016-11-12 11:44:16 -0800814 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700815
Elliott Hughes632e99a2016-11-12 11:44:16 -0800816 android::base::unique_fd data_fd(open(data.c_str(), O_WRONLY|O_CLOEXEC));
817 if (data_fd == -1) {
818 PLOG(ERROR) << "couldn't open firmware data fd for " << uevent->firmware;
819 return;
820 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700821
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700822try_loading_again:
Elliott Hughes632e99a2016-11-12 11:44:16 -0800823 for (size_t i = 0; i < arraysize(firmware_dirs); i++) {
824 std::string file = android::base::StringPrintf("%s/%s", firmware_dirs[i], uevent->firmware);
825 android::base::unique_fd fw_fd(open(file.c_str(), O_RDONLY|O_CLOEXEC));
826 struct stat sb;
827 if (fw_fd != -1 && fstat(fw_fd, &sb) != -1) {
828 load_firmware(uevent, root, fw_fd, sb.st_size, loading_fd, data_fd);
829 return;
Benoit Goby609d8822010-11-09 18:10:24 -0800830 }
Brian Swetland02863b92010-09-19 03:36:39 -0700831 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700832
Elliott Hughes632e99a2016-11-12 11:44:16 -0800833 if (booting) {
834 // If we're not fully booted, we may be missing
835 // filesystems needed for firmware, wait and retry.
Elliott Hughes290a2282016-11-14 17:08:47 -0800836 std::this_thread::sleep_for(100ms);
Elliott Hughes632e99a2016-11-12 11:44:16 -0800837 booting = is_booting();
838 goto try_loading_again;
839 }
840
841 LOG(ERROR) << "firmware: could not find firmware for " << uevent->firmware;
842
843 // Write "-1" as our response to the kernel's firmware request, since we have nothing for it.
844 write(loading_fd, "-1", 2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700845}
846
Elliott Hughes632e99a2016-11-12 11:44:16 -0800847static void handle_firmware_event(uevent* uevent) {
848 if (strcmp(uevent->subsystem, "firmware")) return;
849 if (strcmp(uevent->action, "add")) return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700850
Elliott Hughes632e99a2016-11-12 11:44:16 -0800851 // Loading the firmware in a child means we can do that in parallel...
852 // (We ignore SIGCHLD rather than wait for our children.)
853 pid_t pid = fork();
854 if (pid == 0) {
855 Timer t;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700856 process_firmware_event(uevent);
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000857 LOG(INFO) << "loading " << uevent->path << " took " << t;
Kenny Root17baff42014-08-20 16:16:44 -0700858 _exit(EXIT_SUCCESS);
Elliott Hughes632e99a2016-11-12 11:44:16 -0800859 } else if (pid == -1) {
860 PLOG(ERROR) << "could not fork to process firmware event for " << uevent->firmware;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700861 }
862}
863
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800864static bool inline should_stop_coldboot(coldboot_action_t act)
865{
866 return (act == COLDBOOT_STOP || act == COLDBOOT_FINISH);
867}
868
Ruchi Kandoic6037202014-06-23 11:22:09 -0700869#define UEVENT_MSG_LEN 2048
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800870
Sandeep Patil44a3ee22017-02-08 15:49:47 -0800871static inline coldboot_action_t handle_device_fd_with(
872 std::function<coldboot_action_t(uevent* uevent)> handle_uevent)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700873{
Vernon Tang3f582e92011-04-25 13:08:17 +1000874 char msg[UEVENT_MSG_LEN+2];
875 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700876 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700877 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700878 continue;
879
880 msg[n] = '\0';
881 msg[n+1] = '\0';
882
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700883 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700884 parse_event(msg, &uevent);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800885 coldboot_action_t act = handle_uevent(&uevent);
886 if (should_stop_coldboot(act))
887 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700888 }
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800889
890 return COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700891}
892
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800893coldboot_action_t handle_device_fd(coldboot_callback fn)
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800894{
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800895 coldboot_action_t ret = handle_device_fd_with(
896 [&](uevent* uevent) -> coldboot_action_t {
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800897 if (selinux_status_updated() > 0) {
898 struct selabel_handle *sehandle2;
899 sehandle2 = selinux_android_file_context_handle();
900 if (sehandle2) {
901 selabel_close(sehandle);
902 sehandle = sehandle2;
903 }
904 }
905
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800906 // default is to always create the devices
907 coldboot_action_t act = COLDBOOT_CREATE;
908 if (fn) {
909 act = fn(uevent);
910 }
911
912 if (act == COLDBOOT_CREATE || act == COLDBOOT_STOP) {
913 handle_device_event(uevent);
914 handle_firmware_event(uevent);
915 }
916
917 return act;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800918 });
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800919
920 return ret;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800921}
922
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700923/* Coldboot walks parts of the /sys tree and pokes the uevent files
924** to cause the kernel to regenerate device add events that happened
925** before init's device manager was started
926**
927** We drain any pending events from the netlink socket every time
928** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800929** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700930*/
931
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800932static coldboot_action_t do_coldboot(DIR *d, coldboot_callback fn)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700933{
934 struct dirent *de;
935 int dfd, fd;
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800936 coldboot_action_t act = COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700937
938 dfd = dirfd(d);
939
940 fd = openat(dfd, "uevent", O_WRONLY);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800941 if (fd >= 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700942 write(fd, "add\n", 4);
943 close(fd);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800944 act = handle_device_fd(fn);
945 if (should_stop_coldboot(act))
946 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700947 }
948
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800949 while (!should_stop_coldboot(act) && (de = readdir(d))) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700950 DIR *d2;
951
952 if(de->d_type != DT_DIR || de->d_name[0] == '.')
953 continue;
954
955 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
956 if(fd < 0)
957 continue;
958
959 d2 = fdopendir(fd);
960 if(d2 == 0)
961 close(fd);
962 else {
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800963 act = do_coldboot(d2, fn);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700964 closedir(d2);
965 }
966 }
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800967
968 // default is always to continue looking for uevents
969 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700970}
971
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800972static coldboot_action_t coldboot(const char *path, coldboot_callback fn)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700973{
James Hawkins588a2ca2016-02-18 14:52:46 -0800974 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800975 if (d) {
976 return do_coldboot(d.get(), fn);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700977 }
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800978
979 return COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700980}
981
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800982void device_init(const char* path, coldboot_callback fn) {
Sandeep Patil971a4602017-02-15 13:37:52 -0800983 if (!sehandle) {
984 sehandle = selinux_android_file_context_handle();
Elliott Hughes56a06562015-03-28 11:23:32 -0700985 }
Sandeep Patil971a4602017-02-15 13:37:52 -0800986 // open uevent socket and selinux status only if it hasn't been
987 // done before
988 if (device_fd == -1) {
989 /* is 256K enough? udev uses 16MB! */
990 device_fd.reset(uevent_open_socket(256 * 1024, true));
991 if (device_fd == -1) {
992 return;
993 }
994 fcntl(device_fd, F_SETFL, O_NONBLOCK);
995 selinux_status_open(true);
996 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700997
Elliott Hughes56a06562015-03-28 11:23:32 -0700998 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700999 LOG(VERBOSE) << "Skipping coldboot, already done!";
Elliott Hughes56a06562015-03-28 11:23:32 -07001000 return;
Colin Crossf83d0b92010-04-21 12:04:20 -07001001 }
Elliott Hughes56a06562015-03-28 11:23:32 -07001002
1003 Timer t;
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001004 coldboot_action_t act;
1005 if (!path) {
1006 act = coldboot("/sys/class", fn);
1007 if (!should_stop_coldboot(act)) {
1008 act = coldboot("/sys/block", fn);
1009 if (!should_stop_coldboot(act)) {
1010 act = coldboot("/sys/devices", fn);
1011 }
1012 }
1013 } else {
1014 act = coldboot(path, fn);
1015 }
1016
1017 // If we have a callback, then do as it says. If no, then the default is
1018 // to always create COLDBOOT_DONE file.
1019 if (!fn || (act == COLDBOOT_FINISH)) {
1020 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
1021 }
1022
Elliott Hughes331cf2f2016-11-29 19:20:58 +00001023 LOG(INFO) << "Coldboot took " << t;
Colin Cross0dd7ca62010-04-13 19:25:51 -07001024}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001025
Sandeep Patil35403eb2017-02-08 20:27:12 -08001026void device_close() {
Sandeep Patil35403eb2017-02-08 20:27:12 -08001027 device_fd.reset();
Sandeep Patil971a4602017-02-15 13:37:52 -08001028 selinux_status_close();
Sandeep Patil35403eb2017-02-08 20:27:12 -08001029}
1030
Elliott Hughes632e99a2016-11-12 11:44:16 -08001031int get_device_fd() {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001032 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001033}