blob: 6f866623d4ac4d28489bd98ad0eaa8d20c7b162a [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>
Tom Cherry5f4e8ea2017-06-19 18:08:39 -070022#include <poll.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -080023#include <stddef.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070024#include <stdio.h>
25#include <stdlib.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070026#include <string.h>
Elliott Hughes632e99a2016-11-12 11:44:16 -080027#include <sys/sendfile.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070028#include <sys/socket.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070029#include <sys/stat.h>
30#include <sys/time.h>
31#include <sys/types.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070032#include <sys/un.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070033#include <sys/wait.h>
34#include <unistd.h>
35
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070036#include <linux/netlink.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050037
James Hawkins588a2ca2016-02-18 14:52:46 -080038#include <memory>
Elliott Hughes290a2282016-11-14 17:08:47 -080039#include <thread>
James Hawkins588a2ca2016-02-18 14:52:46 -080040
Stephen Smalleye46f9d52012-01-13 08:48:47 -050041#include <selinux/selinux.h>
42#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040043#include <selinux/android.h>
Stephen Smalleye2eb69d2013-04-16 09:30:30 -040044#include <selinux/avc.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050045
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070046#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070047
Biao Ludc848562016-01-28 16:10:54 +080048#include <android-base/file.h>
Rob Herring6de783a2016-05-06 10:06:59 -050049#include <android-base/stringprintf.h>
Tom Cherryd43b6152017-06-19 17:41:41 -070050#include <android-base/strings.h>
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +080051#include <android-base/unique_fd.h>
Dima Zavinda04c522011-09-01 17:09:44 -070052#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100053#include <cutils/uevent.h>
54
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070055#include "devices.h"
Greg Hackmann3312aa82013-11-18 15:24:40 -080056#include "ueventd_parser.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070057#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070058#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070059
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060#define SYSFS_PREFIX "/sys"
Daniel Rosenbergd1d96022015-03-20 15:10:29 -070061static const char *firmware_dirs[] = { "/etc/firmware",
62 "/vendor/firmware",
63 "/firmware/image" };
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064
Stephen Smalleye096e362012-06-11 13:37:39 -040065extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050066
Sandeep Patil35403eb2017-02-08 20:27:12 -080067static android::base::unique_fd device_fd;
Colin Cross0dd7ca62010-04-13 19:25:51 -070068
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069struct perms_ {
70 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070071 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072 mode_t perm;
73 unsigned int uid;
74 unsigned int gid;
75 unsigned short prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070076 unsigned short wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070077};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070078
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070079struct perm_node {
80 struct perms_ dp;
81 struct listnode plist;
82};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070083
84static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070085static list_declare(dev_perms);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070086
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070087int add_dev_perms(const char *name, const char *attr,
88 mode_t perm, unsigned int uid, unsigned int gid,
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070089 unsigned short prefix,
90 unsigned short wildcard) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -080091 struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070092 if (!node)
93 return -ENOMEM;
94
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070095 node->dp.name = strdup(name);
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -080096 if (!node->dp.name) {
97 free(node);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070098 return -ENOMEM;
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -080099 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700100
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700101 if (attr) {
102 node->dp.attr = strdup(attr);
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800103 if (!node->dp.attr) {
104 free(node->dp.name);
105 free(node);
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700106 return -ENOMEM;
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800107 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700108 }
109
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700110 node->dp.perm = perm;
111 node->dp.uid = uid;
112 node->dp.gid = gid;
113 node->dp.prefix = prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700114 node->dp.wildcard = wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700115
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700116 if (attr)
117 list_add_tail(&sys_perms, &node->plist);
118 else
119 list_add_tail(&dev_perms, &node->plist);
120
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700121 return 0;
122}
123
Colin Cross43d537e2014-07-02 13:08:13 -0700124static bool perm_path_matches(const char *path, struct perms_ *dp)
125{
126 if (dp->prefix) {
127 if (strncmp(path, dp->name, strlen(dp->name)) == 0)
128 return true;
129 } else if (dp->wildcard) {
130 if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
131 return true;
132 } else {
133 if (strcmp(path, dp->name) == 0)
134 return true;
135 }
136
137 return false;
138}
139
Rob Herring6de783a2016-05-06 10:06:59 -0500140static bool match_subsystem(perms_* dp, const char* pattern,
141 const char* path, const char* subsystem) {
142 if (!pattern || !subsystem || strstr(dp->name, subsystem) == NULL) {
143 return false;
144 }
Rob Herringe5636a32016-05-06 12:28:48 -0500145
Rob Herring6de783a2016-05-06 10:06:59 -0500146 std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path));
147 return perm_path_matches(subsys_path.c_str(), dp);
148}
Rob Herringe5636a32016-05-06 12:28:48 -0500149
Rob Herring6de783a2016-05-06 10:06:59 -0500150static void fixup_sys_perms(const char* upath, const char* subsystem) {
151 // upaths omit the "/sys" that paths in this list
152 // contain, so we prepend it...
153 std::string path = std::string(SYSFS_PREFIX) + upath;
154
155 listnode* node;
Rob Herringe5636a32016-05-06 12:28:48 -0500156 list_for_each(node, &sys_perms) {
Rob Herring6de783a2016-05-06 10:06:59 -0500157 perms_* dp = &(node_to_item(node, perm_node, plist))->dp;
158 if (match_subsystem(dp, SYSFS_PREFIX "/class/%s/%s", path.c_str(), subsystem)) {
159 ; // matched
160 } else if (match_subsystem(dp, SYSFS_PREFIX "/bus/%s/devices/%s", path.c_str(), subsystem)) {
161 ; // matched
162 } else if (!perm_path_matches(path.c_str(), dp)) {
163 continue;
Rob Herringe5636a32016-05-06 12:28:48 -0500164 }
165
166 std::string attr_file = path + "/" + dp->attr;
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700167 LOG(INFO) << "fixup " << attr_file
168 << " " << dp->uid << " " << dp->gid << " " << std::oct << dp->perm;
Rob Herringe5636a32016-05-06 12:28:48 -0500169 chown(attr_file.c_str(), dp->uid, dp->gid);
170 chmod(attr_file.c_str(), dp->perm);
171 }
172
173 if (access(path.c_str(), F_OK) == 0) {
Dmitry Shmidt7eed4742016-07-28 13:55:39 -0700174 LOG(VERBOSE) << "restorecon_recursive: " << path;
Paul Lawrencea8d84342016-11-14 15:40:18 -0800175 restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE);
Rob Herringe5636a32016-05-06 12:28:48 -0500176 }
177}
178
Colin Cross43d537e2014-07-02 13:08:13 -0700179static mode_t get_device_perm(const char *path, const char **links,
180 unsigned *uid, unsigned *gid)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700181{
Colin Cross44b65d02010-04-20 14:32:50 -0700182 struct listnode *node;
183 struct perm_node *perm_node;
184 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700185
Colin Cross44b65d02010-04-20 14:32:50 -0700186 /* search the perms list in reverse so that ueventd.$hardware can
187 * override ueventd.rc
188 */
189 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700190 bool match = false;
191
Colin Cross44b65d02010-04-20 14:32:50 -0700192 perm_node = node_to_item(node, struct perm_node, plist);
193 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700194
Colin Cross43d537e2014-07-02 13:08:13 -0700195 if (perm_path_matches(path, dp)) {
196 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700197 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700198 if (links) {
199 int i;
200 for (i = 0; links[i]; i++) {
201 if (perm_path_matches(links[i], dp)) {
202 match = true;
203 break;
204 }
205 }
206 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700207 }
Colin Cross43d537e2014-07-02 13:08:13 -0700208
209 if (match) {
210 *uid = dp->uid;
211 *gid = dp->gid;
212 return dp->perm;
213 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700214 }
Colin Cross44b65d02010-04-20 14:32:50 -0700215 /* Default if nothing found. */
216 *uid = 0;
217 *gid = 0;
218 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700219}
220
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700221static void make_device(const char *path,
Elliott Hughesf682b472015-02-06 12:19:48 -0800222 const char */*upath*/,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400223 int block, int major, int minor,
224 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700225{
226 unsigned uid;
227 unsigned gid;
228 mode_t mode;
229 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500230 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700231
Colin Cross43d537e2014-07-02 13:08:13 -0700232 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700233
Sandeep Patilea239832017-02-03 07:51:55 -0800234 if (sehandle) {
235 if (selabel_lookup_best_match(sehandle, &secontext, path, links, mode)) {
236 PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
237 return;
238 }
239 setfscreatecon(secontext);
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300240 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700241
Colin Cross17dcc5c2010-09-03 12:25:34 -0700242 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800243 /* Temporarily change egid to avoid race condition setting the gid of the
244 * device node. Unforunately changing the euid would prevent creation of
245 * some device nodes, so the uid has to be set with chown() and is still
246 * racy. Fixing the gid race at least fixed the issue with system_server
247 * opening dynamic input devices under the AID_INPUT gid. */
Tom Cherry0506b182017-02-23 13:46:09 -0800248 if (setegid(gid)) {
249 PLOG(ERROR) << "setegid(" << gid << ") for " << path << " device failed";
250 goto out;
251 }
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300252 /* If the node already exists update its SELinux label to handle cases when
253 * it was created with the wrong context during coldboot procedure. */
Sandeep Patilea239832017-02-03 07:51:55 -0800254 if (mknod(path, mode, dev) && (errno == EEXIST) && secontext) {
William Roberts397de142016-06-02 09:53:44 -0700255
256 char* fcon = nullptr;
257 int rc = lgetfilecon(path, &fcon);
258 if (rc < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700259 PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
William Roberts397de142016-06-02 09:53:44 -0700260 goto out;
261 }
262
263 bool different = strcmp(fcon, secontext) != 0;
264 freecon(fcon);
265
266 if (different && lsetfilecon(path, secontext)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700267 PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path << "' device";
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300268 }
269 }
William Roberts397de142016-06-02 09:53:44 -0700270
271out:
Nick Pelly6405c692010-01-21 18:13:39 -0800272 chown(path, uid, -1);
Tom Cherry0506b182017-02-23 13:46:09 -0800273 if (setegid(AID_ROOT)) {
274 PLOG(FATAL) << "setegid(AID_ROOT) failed";
275 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700276
Sandeep Patilea239832017-02-03 07:51:55 -0800277 if (secontext) {
278 freecon(secontext);
279 setfscreatecon(NULL);
280 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700281}
282
Tom Cherryd43b6152017-06-19 17:41:41 -0700283// Given a path that may start with a platform device, find the parent platform device by finding a
284// parent directory with a 'subsystem' symlink that points to the platform bus.
285// If it doesn't start with a platform device, return false
286bool FindPlatformDevice(std::string path, std::string* platform_device_path) {
287 platform_device_path->clear();
Dima Zavinf395c922013-03-06 16:23:57 -0800288
Tom Cherryd43b6152017-06-19 17:41:41 -0700289 static const std::string kSysfsMountPoint = "/sys";
Colin Crossfadb85e2011-03-30 18:32:12 -0700290
Tom Cherryd43b6152017-06-19 17:41:41 -0700291 // Uevents don't contain the mount point, so we need to add it here.
292 path.insert(0, kSysfsMountPoint);
Colin Crossfadb85e2011-03-30 18:32:12 -0700293
Tom Cherryd43b6152017-06-19 17:41:41 -0700294 std::string directory = android::base::Dirname(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700295
Tom Cherryd43b6152017-06-19 17:41:41 -0700296 while (directory != "/" && directory != ".") {
297 std::string subsystem_link_path;
298 if (android::base::Realpath(directory + "/subsystem", &subsystem_link_path) &&
299 subsystem_link_path == kSysfsMountPoint + "/bus/platform") {
300 // We need to remove the mount point that we added above before returning.
301 directory.erase(0, kSysfsMountPoint.size());
302 *platform_device_path = directory;
303 return true;
Colin Crossfadb85e2011-03-30 18:32:12 -0700304 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700305
Tom Cherryd43b6152017-06-19 17:41:41 -0700306 auto last_slash = path.rfind('/');
307 if (last_slash == std::string::npos) return false;
Sandeep Patil35403eb2017-02-08 20:27:12 -0800308
Tom Cherryd43b6152017-06-19 17:41:41 -0700309 path.erase(last_slash);
310 directory = android::base::Dirname(path);
Sandeep Patil35403eb2017-02-08 20:27:12 -0800311 }
Tom Cherryd43b6152017-06-19 17:41:41 -0700312
313 return false;
Sandeep Patil35403eb2017-02-08 20:27:12 -0800314}
315
Andrew Boiea885d042013-09-13 17:41:20 -0700316/* Given a path that may start with a PCI device, populate the supplied buffer
317 * with the PCI domain/bus number and the peripheral ID and return 0.
318 * If it doesn't start with a PCI device, or there is some error, return -1 */
319static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
320{
321 const char *start, *end;
322
323 if (strncmp(path, "/devices/pci", 12))
324 return -1;
325
326 /* Beginning of the prefix is the initial "pci" after "/devices/" */
327 start = path + 9;
328
329 /* End of the prefix is two path '/' later, capturing the domain/bus number
330 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
331 end = strchr(start, '/');
332 if (!end)
333 return -1;
334 end = strchr(end + 1, '/');
335 if (!end)
336 return -1;
337
338 /* Make sure we have enough room for the string plus null terminator */
339 if (end - start + 1 > buf_sz)
340 return -1;
341
342 strncpy(buf, start, end - start);
343 buf[end - start] = '\0';
344 return 0;
345}
346
Jeremy Compostella937309d2017-03-03 16:27:29 +0100347/* Given a path that may start with a virtual block device, populate
348 * the supplied buffer with the virtual block device ID and return 0.
349 * If it doesn't start with a virtual block device, or there is some
350 * error, return -1 */
351static int find_vbd_device_prefix(const char *path, char *buf, ssize_t buf_sz)
352{
353 const char *start, *end;
354
355 /* Beginning of the prefix is the initial "vbd-" after "/devices/" */
356 if (strncmp(path, "/devices/vbd-", 13))
357 return -1;
358
359 /* End of the prefix is one path '/' later, capturing the
360 virtual block device ID. Example: 768 */
361 start = path + 13;
362 end = strchr(start, '/');
363 if (!end)
364 return -1;
365
366 /* Make sure we have enough room for the string plus null terminator */
367 if (end - start + 1 > buf_sz)
368 return -1;
369
370 strncpy(buf, start, end - start);
371 buf[end - start] = '\0';
372 return 0;
373}
374
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700375static void parse_event(const char *msg, struct uevent *uevent)
376{
377 uevent->action = "";
378 uevent->path = "";
379 uevent->subsystem = "";
380 uevent->firmware = "";
381 uevent->major = -1;
382 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700383 uevent->partition_name = NULL;
384 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700385 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700386
387 /* currently ignoring SEQNUM */
388 while(*msg) {
389 if(!strncmp(msg, "ACTION=", 7)) {
390 msg += 7;
391 uevent->action = msg;
392 } else if(!strncmp(msg, "DEVPATH=", 8)) {
393 msg += 8;
394 uevent->path = msg;
395 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
396 msg += 10;
397 uevent->subsystem = msg;
398 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
399 msg += 9;
400 uevent->firmware = msg;
401 } else if(!strncmp(msg, "MAJOR=", 6)) {
402 msg += 6;
403 uevent->major = atoi(msg);
404 } else if(!strncmp(msg, "MINOR=", 6)) {
405 msg += 6;
406 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700407 } else if(!strncmp(msg, "PARTN=", 6)) {
408 msg += 6;
409 uevent->partition_num = atoi(msg);
410 } else if(!strncmp(msg, "PARTNAME=", 9)) {
411 msg += 9;
412 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700413 } else if(!strncmp(msg, "DEVNAME=", 8)) {
414 msg += 8;
415 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700416 }
417
Wei Zhongf97b8872012-03-23 14:15:34 -0700418 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700419 while(*msg++)
420 ;
421 }
422
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800423 if (LOG_UEVENTS) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700424 LOG(INFO) << android::base::StringPrintf("event { '%s', '%s', '%s', '%s', %d, %d }",
425 uevent->action, uevent->path, uevent->subsystem,
426 uevent->firmware, uevent->major, uevent->minor);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800427 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700428}
429
Benoit Gobyd2278632010-08-03 14:36:02 -0700430static char **get_character_device_symlinks(struct uevent *uevent)
431{
432 const char *parent;
Dan Austin60b976d2016-03-28 14:22:12 -0700433 const char *slash;
Benoit Gobyd2278632010-08-03 14:36:02 -0700434 char **links;
435 int link_num = 0;
436 int width;
437
Tom Cherryd43b6152017-06-19 17:41:41 -0700438 std::string platform_device;
439 if (!FindPlatformDevice(uevent->path, &platform_device)) return nullptr;
Benoit Gobyd2278632010-08-03 14:36:02 -0700440
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800441 links = (char**) malloc(sizeof(char *) * 2);
Benoit Gobyd2278632010-08-03 14:36:02 -0700442 if (!links)
443 return NULL;
444 memset(links, 0, sizeof(char *) * 2);
445
446 /* skip "/devices/platform/<driver>" */
Tom Cherryd43b6152017-06-19 17:41:41 -0700447 parent = strchr(uevent->path + platform_device.size(), '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100448 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700449 goto err;
450
451 if (!strncmp(parent, "/usb", 4)) {
452 /* skip root hub name and device. use device interface */
453 while (*++parent && *parent != '/');
454 if (*parent)
455 while (*++parent && *parent != '/');
456 if (!*parent)
457 goto err;
458 slash = strchr(++parent, '/');
459 if (!slash)
460 goto err;
461 width = slash - parent;
462 if (width <= 0)
463 goto err;
464
465 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
466 link_num++;
467 else
468 links[link_num] = NULL;
469 mkdir("/dev/usb", 0755);
470 }
471 else {
472 goto err;
473 }
474
475 return links;
476err:
477 free(links);
478 return NULL;
479}
480
Bowgo Tsai1fa02512017-05-15 16:34:52 +0800481char** get_block_device_symlinks(struct uevent* uevent) {
Dan Austin60b976d2016-03-28 14:22:12 -0700482 const char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700483 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700484 char buf[256];
485 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700486 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700487 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700488
Tom Cherryd43b6152017-06-19 17:41:41 -0700489 std::string device;
490 if (FindPlatformDevice(uevent->path, &device)) {
491 // Skip /devices/platform or /devices/ if present
492 static const std::string devices_platform_prefix = "/devices/platform/";
493 static const std::string devices_prefix = "/devices/";
494
495 if (android::base::StartsWith(device, devices_platform_prefix.c_str())) {
496 device = device.substr(devices_platform_prefix.length());
497 } else if (android::base::StartsWith(device, devices_prefix.c_str())) {
498 device = device.substr(devices_prefix.length());
499 }
500
Andrew Boiea885d042013-09-13 17:41:20 -0700501 type = "platform";
502 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
503 device = buf;
504 type = "pci";
Jeremy Compostella937309d2017-03-03 16:27:29 +0100505 } else if (!find_vbd_device_prefix(uevent->path, buf, sizeof(buf))) {
506 device = buf;
507 type = "vbd";
Andrew Boiea885d042013-09-13 17:41:20 -0700508 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800509 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700510 }
Dima Zavinf395c922013-03-06 16:23:57 -0800511
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800512 char **links = (char**) malloc(sizeof(char *) * 4);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700513 if (!links)
514 return NULL;
515 memset(links, 0, sizeof(char *) * 4);
516
Sandeep Patil35403eb2017-02-08 20:27:12 -0800517 LOG(VERBOSE) << "found " << type << " device " << device;
Colin Crossfadb85e2011-03-30 18:32:12 -0700518
Tom Cherryd43b6152017-06-19 17:41:41 -0700519 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device.c_str());
Colin Crossb0ab94b2010-04-08 16:16:20 -0700520
521 if (uevent->partition_name) {
522 p = strdup(uevent->partition_name);
523 sanitize(p);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700524 if (strcmp(uevent->partition_name, p)) {
525 LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '" << p << "'";
526 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700527 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
528 link_num++;
529 else
530 links[link_num] = NULL;
531 free(p);
532 }
533
534 if (uevent->partition_num >= 0) {
535 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
536 link_num++;
537 else
538 links[link_num] = NULL;
539 }
540
Dima Zavinf395c922013-03-06 16:23:57 -0800541 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700542 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
543 link_num++;
544 else
545 links[link_num] = NULL;
546
547 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700548}
549
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700550static void make_link_init(const char* oldpath, const char* newpath) {
551 const char* slash = strrchr(newpath, '/');
552 if (!slash) return;
553
554 if (mkdir_recursive(dirname(newpath), 0755)) {
555 PLOG(ERROR) << "Failed to create directory " << dirname(newpath);
556 }
557
558 if (symlink(oldpath, newpath) && errno != EEXIST) {
559 PLOG(ERROR) << "Failed to symlink " << oldpath << " to " << newpath;
560 }
561}
562
563static void remove_link(const char* oldpath, const char* newpath) {
564 std::string path;
565 if (android::base::Readlink(newpath, &path) && path == oldpath) unlink(newpath);
566}
567
Colin Crosseb5ba832011-03-30 17:37:17 -0700568static void handle_device(const char *action, const char *devpath,
569 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700570{
Colin Crosseb5ba832011-03-30 17:37:17 -0700571 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400572 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700573 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700574 for (int i = 0; links[i]; i++) {
Chris Fries79f33842013-09-05 13:19:21 -0500575 make_link_init(devpath, links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700576 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700577 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700578 }
579
Colin Crosseb5ba832011-03-30 17:37:17 -0700580 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700581 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700582 for (int i = 0; links[i]; i++) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700583 remove_link(devpath, links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700584 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700585 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700586 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700587 }
588
589 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700590 for (int i = 0; links[i]; i++) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700591 free(links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700592 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700593 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700594 }
595}
596
Colin Crosseb5ba832011-03-30 17:37:17 -0700597static const char *parse_device_name(struct uevent *uevent, unsigned int len)
598{
599 const char *name;
600
601 /* if it's not a /dev device, nothing else to do */
602 if((uevent->major < 0) || (uevent->minor < 0))
603 return NULL;
604
605 /* do we have a name? */
606 name = strrchr(uevent->path, '/');
607 if(!name)
608 return NULL;
609 name++;
610
611 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800612 if(strlen(name) > len) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700613 LOG(ERROR) << "DEVPATH=" << name << " exceeds " << len << "-character limit on filename; ignoring event";
Colin Crosseb5ba832011-03-30 17:37:17 -0700614 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800615 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700616
617 return name;
618}
619
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800620#define DEVPATH_LEN 96
621#define MAX_DEV_NAME 64
622
Colin Crosseb5ba832011-03-30 17:37:17 -0700623static void handle_block_device_event(struct uevent *uevent)
624{
625 const char *base = "/dev/block/";
626 const char *name;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800627 char devpath[DEVPATH_LEN];
Colin Crosseb5ba832011-03-30 17:37:17 -0700628 char **links = NULL;
629
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800630 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700631 if (!name)
632 return;
633
634 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500635 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700636
Dima Zavinf395c922013-03-06 16:23:57 -0800637 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700638 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700639
640 handle_device(uevent->action, devpath, uevent->path, 1,
641 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700642}
643
Greg Hackmann3312aa82013-11-18 15:24:40 -0800644static bool assemble_devpath(char *devpath, const char *dirname,
645 const char *devname)
646{
647 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
648 if (s < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700649 PLOG(ERROR) << "failed to assemble device path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800650 return false;
651 } else if (s >= DEVPATH_LEN) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700652 LOG(ERROR) << dirname << "/" << devname
653 << " exceeds " << DEVPATH_LEN << "-character limit on path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800654 return false;
655 }
656 return true;
657}
658
659static void mkdir_recursive_for_devpath(const char *devpath)
660{
661 char dir[DEVPATH_LEN];
662 char *slash;
663
664 strcpy(dir, devpath);
665 slash = strrchr(dir, '/');
666 *slash = '\0';
667 mkdir_recursive(dir, 0755);
668}
669
Colin Crosseb5ba832011-03-30 17:37:17 -0700670static void handle_generic_device_event(struct uevent *uevent)
671{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800672 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700673 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800674 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700675 char **links = NULL;
676
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800677 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700678 if (!name)
679 return;
680
Greg Hackmann3312aa82013-11-18 15:24:40 -0800681 struct ueventd_subsystem *subsystem =
682 ueventd_subsystem_find_by_name(uevent->subsystem);
683
684 if (subsystem) {
685 const char *devname;
686
687 switch (subsystem->devname_src) {
688 case DEVNAME_UEVENT_DEVNAME:
689 devname = uevent->device_name;
690 break;
691
692 case DEVNAME_UEVENT_DEVPATH:
693 devname = name;
694 break;
695
696 default:
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700697 LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800698 return;
699 }
700
701 if (!assemble_devpath(devpath, subsystem->dirname, devname))
702 return;
703 mkdir_recursive_for_devpath(devpath);
704 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700705 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700706 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800707 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800708 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800709 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700710 }
711 else {
712 /* This imitates the file system that would be created
713 * if we were using devfs instead.
714 * Minors are broken up into groups of 128, starting at "001"
715 */
716 int bus_id = uevent->minor / 128 + 1;
717 int device_id = uevent->minor % 128 + 1;
718 /* build directories */
719 make_dir("/dev/bus", 0755);
720 make_dir("/dev/bus/usb", 0755);
721 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
722 make_dir(devpath, 0755);
723 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
724 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700725 } else {
726 /* ignore other USB events */
727 return;
728 }
729 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
730 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500731 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200732 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
733 base = "/dev/dri/";
734 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700735 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
736 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500737 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700738 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
739 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500740 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700741 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
742 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500743 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700744 } else if(!strncmp(uevent->subsystem, "input", 5)) {
745 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500746 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700747 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
748 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500749 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700750 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
751 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500752 make_dir(base, 0755);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700753 } else if(!strncmp(uevent->subsystem, "misc", 4) && !strncmp(name, "log_", 4)) {
754 LOG(INFO) << "kernel logger is deprecated";
Colin Crosseb5ba832011-03-30 17:37:17 -0700755 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500756 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700757 name += 4;
758 } else
759 base = "/dev/";
760 links = get_character_device_symlinks(uevent);
761
762 if (!devpath[0])
763 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
764
765 handle_device(uevent->action, devpath, uevent->path, 0,
766 uevent->major, uevent->minor, links);
767}
768
769static void handle_device_event(struct uevent *uevent)
770{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700771 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Rob Herring6de783a2016-05-06 10:06:59 -0500772 fixup_sys_perms(uevent->path, uevent->subsystem);
Colin Crosseb5ba832011-03-30 17:37:17 -0700773
774 if (!strncmp(uevent->subsystem, "block", 5)) {
775 handle_block_device_event(uevent);
776 } else {
777 handle_generic_device_event(uevent);
778 }
779}
780
Elliott Hughes632e99a2016-11-12 11:44:16 -0800781static void load_firmware(uevent* uevent, const std::string& root,
782 int fw_fd, size_t fw_size,
783 int loading_fd, int data_fd) {
784 // Start transfer.
785 android::base::WriteFully(loading_fd, "1", 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700786
Elliott Hughes632e99a2016-11-12 11:44:16 -0800787 // Copy the firmware.
788 int rc = sendfile(data_fd, fw_fd, nullptr, fw_size);
789 if (rc == -1) {
790 PLOG(ERROR) << "firmware: sendfile failed { '" << root << "', '" << uevent->firmware << "' }";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700791 }
792
Elliott Hughes632e99a2016-11-12 11:44:16 -0800793 // Tell the firmware whether to abort or commit.
794 const char* response = (rc != -1) ? "0" : "-1";
795 android::base::WriteFully(loading_fd, response, strlen(response));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700796}
797
Elliott Hughes632e99a2016-11-12 11:44:16 -0800798static int is_booting() {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700799 return access("/dev/.booting", F_OK) == 0;
800}
801
Elliott Hughes632e99a2016-11-12 11:44:16 -0800802static void process_firmware_event(uevent* uevent) {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700803 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700804
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700805 LOG(INFO) << "firmware: loading '" << uevent->firmware << "' for '" << uevent->path << "'";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700806
Elliott Hughes632e99a2016-11-12 11:44:16 -0800807 std::string root = android::base::StringPrintf("/sys%s", uevent->path);
808 std::string loading = root + "/loading";
809 std::string data = root + "/data";
810
811 android::base::unique_fd loading_fd(open(loading.c_str(), O_WRONLY|O_CLOEXEC));
812 if (loading_fd == -1) {
813 PLOG(ERROR) << "couldn't open firmware loading fd for " << uevent->firmware;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700814 return;
Elliott Hughes632e99a2016-11-12 11:44:16 -0800815 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700816
Elliott Hughes632e99a2016-11-12 11:44:16 -0800817 android::base::unique_fd data_fd(open(data.c_str(), O_WRONLY|O_CLOEXEC));
818 if (data_fd == -1) {
819 PLOG(ERROR) << "couldn't open firmware data fd for " << uevent->firmware;
820 return;
821 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700822
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700823try_loading_again:
Elliott Hughes632e99a2016-11-12 11:44:16 -0800824 for (size_t i = 0; i < arraysize(firmware_dirs); i++) {
825 std::string file = android::base::StringPrintf("%s/%s", firmware_dirs[i], uevent->firmware);
826 android::base::unique_fd fw_fd(open(file.c_str(), O_RDONLY|O_CLOEXEC));
827 struct stat sb;
828 if (fw_fd != -1 && fstat(fw_fd, &sb) != -1) {
829 load_firmware(uevent, root, fw_fd, sb.st_size, loading_fd, data_fd);
830 return;
Benoit Goby609d8822010-11-09 18:10:24 -0800831 }
Brian Swetland02863b92010-09-19 03:36:39 -0700832 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700833
Elliott Hughes632e99a2016-11-12 11:44:16 -0800834 if (booting) {
835 // If we're not fully booted, we may be missing
836 // filesystems needed for firmware, wait and retry.
Elliott Hughes290a2282016-11-14 17:08:47 -0800837 std::this_thread::sleep_for(100ms);
Elliott Hughes632e99a2016-11-12 11:44:16 -0800838 booting = is_booting();
839 goto try_loading_again;
840 }
841
842 LOG(ERROR) << "firmware: could not find firmware for " << uevent->firmware;
843
844 // Write "-1" as our response to the kernel's firmware request, since we have nothing for it.
845 write(loading_fd, "-1", 2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700846}
847
Elliott Hughes632e99a2016-11-12 11:44:16 -0800848static void handle_firmware_event(uevent* uevent) {
849 if (strcmp(uevent->subsystem, "firmware")) return;
850 if (strcmp(uevent->action, "add")) return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700851
Elliott Hughes632e99a2016-11-12 11:44:16 -0800852 // Loading the firmware in a child means we can do that in parallel...
853 // (We ignore SIGCHLD rather than wait for our children.)
854 pid_t pid = fork();
855 if (pid == 0) {
856 Timer t;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700857 process_firmware_event(uevent);
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000858 LOG(INFO) << "loading " << uevent->path << " took " << t;
Kenny Root17baff42014-08-20 16:16:44 -0700859 _exit(EXIT_SUCCESS);
Elliott Hughes632e99a2016-11-12 11:44:16 -0800860 } else if (pid == -1) {
861 PLOG(ERROR) << "could not fork to process firmware event for " << uevent->firmware;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700862 }
863}
864
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800865static bool inline should_stop_coldboot(coldboot_action_t act)
866{
867 return (act == COLDBOOT_STOP || act == COLDBOOT_FINISH);
868}
869
Ruchi Kandoic6037202014-06-23 11:22:09 -0700870#define UEVENT_MSG_LEN 2048
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800871
Sandeep Patil44a3ee22017-02-08 15:49:47 -0800872static inline coldboot_action_t handle_device_fd_with(
873 std::function<coldboot_action_t(uevent* uevent)> handle_uevent)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700874{
Vernon Tang3f582e92011-04-25 13:08:17 +1000875 char msg[UEVENT_MSG_LEN+2];
876 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700877 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700878 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700879 continue;
880
881 msg[n] = '\0';
882 msg[n+1] = '\0';
883
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700884 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700885 parse_event(msg, &uevent);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800886 coldboot_action_t act = handle_uevent(&uevent);
887 if (should_stop_coldboot(act))
888 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700889 }
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800890
891 return COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700892}
893
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800894coldboot_action_t handle_device_fd(coldboot_callback fn)
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800895{
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800896 coldboot_action_t ret = handle_device_fd_with(
897 [&](uevent* uevent) -> coldboot_action_t {
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800898 if (selinux_status_updated() > 0) {
899 struct selabel_handle *sehandle2;
900 sehandle2 = selinux_android_file_context_handle();
901 if (sehandle2) {
902 selabel_close(sehandle);
903 sehandle = sehandle2;
904 }
905 }
906
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800907 // default is to always create the devices
908 coldboot_action_t act = COLDBOOT_CREATE;
909 if (fn) {
910 act = fn(uevent);
911 }
912
913 if (act == COLDBOOT_CREATE || act == COLDBOOT_STOP) {
914 handle_device_event(uevent);
915 handle_firmware_event(uevent);
916 }
917
918 return act;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800919 });
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800920
921 return ret;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800922}
923
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700924/* 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 Hughesc0e919c2015-02-04 14:46:36 -0800930** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700931*/
932
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800933static coldboot_action_t do_coldboot(DIR *d, coldboot_callback fn)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700934{
935 struct dirent *de;
936 int dfd, fd;
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800937 coldboot_action_t act = COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700938
939 dfd = dirfd(d);
940
941 fd = openat(dfd, "uevent", O_WRONLY);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800942 if (fd >= 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700943 write(fd, "add\n", 4);
944 close(fd);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800945 act = handle_device_fd(fn);
946 if (should_stop_coldboot(act))
947 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700948 }
949
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800950 while (!should_stop_coldboot(act) && (de = readdir(d))) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700951 DIR *d2;
952
953 if(de->d_type != DT_DIR || de->d_name[0] == '.')
954 continue;
955
956 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
957 if(fd < 0)
958 continue;
959
960 d2 = fdopendir(fd);
961 if(d2 == 0)
962 close(fd);
963 else {
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800964 act = do_coldboot(d2, fn);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700965 closedir(d2);
966 }
967 }
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800968
969 // default is always to continue looking for uevents
970 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700971}
972
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800973static coldboot_action_t coldboot(const char *path, coldboot_callback fn)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700974{
James Hawkins588a2ca2016-02-18 14:52:46 -0800975 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800976 if (d) {
977 return do_coldboot(d.get(), fn);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700978 }
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800979
980 return COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700981}
982
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800983void device_init(const char* path, coldboot_callback fn) {
Sandeep Patil971a4602017-02-15 13:37:52 -0800984 if (!sehandle) {
985 sehandle = selinux_android_file_context_handle();
Elliott Hughes56a06562015-03-28 11:23:32 -0700986 }
Sandeep Patil971a4602017-02-15 13:37:52 -0800987 // open uevent socket and selinux status only if it hasn't been
988 // done before
989 if (device_fd == -1) {
990 /* is 256K enough? udev uses 16MB! */
991 device_fd.reset(uevent_open_socket(256 * 1024, true));
992 if (device_fd == -1) {
993 return;
994 }
995 fcntl(device_fd, F_SETFL, O_NONBLOCK);
996 selinux_status_open(true);
997 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700998
Elliott Hughes56a06562015-03-28 11:23:32 -0700999 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -07001000 LOG(VERBOSE) << "Skipping coldboot, already done!";
Elliott Hughes56a06562015-03-28 11:23:32 -07001001 return;
Colin Crossf83d0b92010-04-21 12:04:20 -07001002 }
Elliott Hughes56a06562015-03-28 11:23:32 -07001003
1004 Timer t;
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001005 coldboot_action_t act;
1006 if (!path) {
1007 act = coldboot("/sys/class", fn);
1008 if (!should_stop_coldboot(act)) {
1009 act = coldboot("/sys/block", fn);
1010 if (!should_stop_coldboot(act)) {
1011 act = coldboot("/sys/devices", fn);
1012 }
1013 }
1014 } else {
1015 act = coldboot(path, fn);
1016 }
1017
1018 // If we have a callback, then do as it says. If no, then the default is
1019 // to always create COLDBOOT_DONE file.
1020 if (!fn || (act == COLDBOOT_FINISH)) {
1021 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
1022 }
1023
Elliott Hughes331cf2f2016-11-29 19:20:58 +00001024 LOG(INFO) << "Coldboot took " << t;
Colin Cross0dd7ca62010-04-13 19:25:51 -07001025}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001026
Sandeep Patil35403eb2017-02-08 20:27:12 -08001027void device_close() {
Sandeep Patil35403eb2017-02-08 20:27:12 -08001028 device_fd.reset();
Sandeep Patil971a4602017-02-15 13:37:52 -08001029 selinux_status_close();
Sandeep Patil35403eb2017-02-08 20:27:12 -08001030}
1031
Tom Cherry5f4e8ea2017-06-19 18:08:39 -07001032void device_poll(const coldboot_callback& callback,
1033 const std::optional<std::chrono::milliseconds> relative_timeout) {
1034 using namespace std::chrono;
1035
1036 pollfd ufd;
1037 ufd.events = POLLIN;
1038 ufd.fd = device_fd;
1039
1040 auto start_time = steady_clock::now();
1041
1042 while (true) {
1043 ufd.revents = 0;
1044
1045 int timeout_ms = -1;
1046 if (relative_timeout) {
1047 auto now = steady_clock::now();
1048 auto time_elapsed = duration_cast<milliseconds>(now - start_time);
1049 if (time_elapsed > *relative_timeout) return;
1050
1051 auto remaining_timeout = *relative_timeout - time_elapsed;
1052 timeout_ms = remaining_timeout.count();
1053 }
1054
1055 int nr = poll(&ufd, 1, timeout_ms);
1056 if (nr == 0) return;
1057 if (nr < 0) {
1058 continue;
1059 }
1060 if (ufd.revents & POLLIN) {
1061 auto ret = handle_device_fd(callback);
1062 if (should_stop_coldboot(ret)) return;
1063 }
1064 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001065}