blob: 2452c1d39756b17d80e889277f6f7465b2a4c80c [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>
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +080049#include <android-base/unique_fd.h>
Dima Zavinda04c522011-09-01 17:09:44 -070050#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100051#include <cutils/uevent.h>
52
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053#include "devices.h"
Greg Hackmann3312aa82013-11-18 15:24:40 -080054#include "ueventd_parser.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070055#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070056#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070057
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070058#define SYSFS_PREFIX "/sys"
Daniel Rosenbergd1d96022015-03-20 15:10:29 -070059static const char *firmware_dirs[] = { "/etc/firmware",
60 "/vendor/firmware",
61 "/firmware/image" };
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070062
Stephen Smalleye096e362012-06-11 13:37:39 -040063extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050064
Colin Cross0dd7ca62010-04-13 19:25:51 -070065static int device_fd = -1;
66
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070067struct uevent {
68 const char *action;
69 const char *path;
70 const char *subsystem;
71 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070072 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070073 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070074 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075 int major;
76 int minor;
77};
78
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070079struct perms_ {
80 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070081 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070082 mode_t perm;
83 unsigned int uid;
84 unsigned int gid;
85 unsigned short prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070086 unsigned short wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070087};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070088
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070089struct perm_node {
90 struct perms_ dp;
91 struct listnode plist;
92};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070093
Colin Crossfadb85e2011-03-30 18:32:12 -070094struct platform_node {
95 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080096 char *path;
97 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070098 struct listnode list;
99};
100
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700101static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -0700102static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -0700103static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700104
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700105int add_dev_perms(const char *name, const char *attr,
106 mode_t perm, unsigned int uid, unsigned int gid,
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700107 unsigned short prefix,
108 unsigned short wildcard) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800109 struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700110 if (!node)
111 return -ENOMEM;
112
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700113 node->dp.name = strdup(name);
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800114 if (!node->dp.name) {
115 free(node);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700116 return -ENOMEM;
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800117 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700118
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700119 if (attr) {
120 node->dp.attr = strdup(attr);
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800121 if (!node->dp.attr) {
122 free(node->dp.name);
123 free(node);
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700124 return -ENOMEM;
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800125 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700126 }
127
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700128 node->dp.perm = perm;
129 node->dp.uid = uid;
130 node->dp.gid = gid;
131 node->dp.prefix = prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700132 node->dp.wildcard = wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700133
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700134 if (attr)
135 list_add_tail(&sys_perms, &node->plist);
136 else
137 list_add_tail(&dev_perms, &node->plist);
138
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700139 return 0;
140}
141
Colin Cross43d537e2014-07-02 13:08:13 -0700142static bool perm_path_matches(const char *path, struct perms_ *dp)
143{
144 if (dp->prefix) {
145 if (strncmp(path, dp->name, strlen(dp->name)) == 0)
146 return true;
147 } else if (dp->wildcard) {
148 if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
149 return true;
150 } else {
151 if (strcmp(path, dp->name) == 0)
152 return true;
153 }
154
155 return false;
156}
157
Rob Herring6de783a2016-05-06 10:06:59 -0500158static bool match_subsystem(perms_* dp, const char* pattern,
159 const char* path, const char* subsystem) {
160 if (!pattern || !subsystem || strstr(dp->name, subsystem) == NULL) {
161 return false;
162 }
Rob Herringe5636a32016-05-06 12:28:48 -0500163
Rob Herring6de783a2016-05-06 10:06:59 -0500164 std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path));
165 return perm_path_matches(subsys_path.c_str(), dp);
166}
Rob Herringe5636a32016-05-06 12:28:48 -0500167
Rob Herring6de783a2016-05-06 10:06:59 -0500168static void fixup_sys_perms(const char* upath, const char* subsystem) {
169 // upaths omit the "/sys" that paths in this list
170 // contain, so we prepend it...
171 std::string path = std::string(SYSFS_PREFIX) + upath;
172
173 listnode* node;
Rob Herringe5636a32016-05-06 12:28:48 -0500174 list_for_each(node, &sys_perms) {
Rob Herring6de783a2016-05-06 10:06:59 -0500175 perms_* dp = &(node_to_item(node, perm_node, plist))->dp;
176 if (match_subsystem(dp, SYSFS_PREFIX "/class/%s/%s", path.c_str(), subsystem)) {
177 ; // matched
178 } else if (match_subsystem(dp, SYSFS_PREFIX "/bus/%s/devices/%s", path.c_str(), subsystem)) {
179 ; // matched
180 } else if (!perm_path_matches(path.c_str(), dp)) {
181 continue;
Rob Herringe5636a32016-05-06 12:28:48 -0500182 }
183
184 std::string attr_file = path + "/" + dp->attr;
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700185 LOG(INFO) << "fixup " << attr_file
186 << " " << dp->uid << " " << dp->gid << " " << std::oct << dp->perm;
Rob Herringe5636a32016-05-06 12:28:48 -0500187 chown(attr_file.c_str(), dp->uid, dp->gid);
188 chmod(attr_file.c_str(), dp->perm);
189 }
190
191 if (access(path.c_str(), F_OK) == 0) {
Dmitry Shmidt7eed4742016-07-28 13:55:39 -0700192 LOG(VERBOSE) << "restorecon_recursive: " << path;
Rob Herringe5636a32016-05-06 12:28:48 -0500193 restorecon_recursive(path.c_str());
194 }
195}
196
Colin Cross43d537e2014-07-02 13:08:13 -0700197static mode_t get_device_perm(const char *path, const char **links,
198 unsigned *uid, unsigned *gid)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700199{
Colin Cross44b65d02010-04-20 14:32:50 -0700200 struct listnode *node;
201 struct perm_node *perm_node;
202 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700203
Colin Cross44b65d02010-04-20 14:32:50 -0700204 /* search the perms list in reverse so that ueventd.$hardware can
205 * override ueventd.rc
206 */
207 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700208 bool match = false;
209
Colin Cross44b65d02010-04-20 14:32:50 -0700210 perm_node = node_to_item(node, struct perm_node, plist);
211 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700212
Colin Cross43d537e2014-07-02 13:08:13 -0700213 if (perm_path_matches(path, dp)) {
214 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700215 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700216 if (links) {
217 int i;
218 for (i = 0; links[i]; i++) {
219 if (perm_path_matches(links[i], dp)) {
220 match = true;
221 break;
222 }
223 }
224 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700225 }
Colin Cross43d537e2014-07-02 13:08:13 -0700226
227 if (match) {
228 *uid = dp->uid;
229 *gid = dp->gid;
230 return dp->perm;
231 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700232 }
Colin Cross44b65d02010-04-20 14:32:50 -0700233 /* Default if nothing found. */
234 *uid = 0;
235 *gid = 0;
236 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700237}
238
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700239static void make_device(const char *path,
Elliott Hughesf682b472015-02-06 12:19:48 -0800240 const char */*upath*/,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400241 int block, int major, int minor,
242 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700243{
244 unsigned uid;
245 unsigned gid;
246 mode_t mode;
247 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500248 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700249
Colin Cross43d537e2014-07-02 13:08:13 -0700250 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700251
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300252 if (selabel_lookup_best_match(sehandle, &secontext, path, links, mode)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700253 PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300254 return;
255 }
Nick Kralevich4d870952015-06-12 22:03:50 -0700256 setfscreatecon(secontext);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700257
Colin Cross17dcc5c2010-09-03 12:25:34 -0700258 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800259 /* Temporarily change egid to avoid race condition setting the gid of the
260 * device node. Unforunately changing the euid would prevent creation of
261 * some device nodes, so the uid has to be set with chown() and is still
262 * racy. Fixing the gid race at least fixed the issue with system_server
263 * opening dynamic input devices under the AID_INPUT gid. */
264 setegid(gid);
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300265 /* If the node already exists update its SELinux label to handle cases when
266 * it was created with the wrong context during coldboot procedure. */
267 if (mknod(path, mode, dev) && (errno == EEXIST)) {
William Roberts397de142016-06-02 09:53:44 -0700268
269 char* fcon = nullptr;
270 int rc = lgetfilecon(path, &fcon);
271 if (rc < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700272 PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
William Roberts397de142016-06-02 09:53:44 -0700273 goto out;
274 }
275
276 bool different = strcmp(fcon, secontext) != 0;
277 freecon(fcon);
278
279 if (different && lsetfilecon(path, secontext)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700280 PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path << "' device";
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300281 }
282 }
William Roberts397de142016-06-02 09:53:44 -0700283
284out:
Nick Pelly6405c692010-01-21 18:13:39 -0800285 chown(path, uid, -1);
286 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700287
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300288 freecon(secontext);
289 setfscreatecon(NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700290}
291
Dima Zavinf395c922013-03-06 16:23:57 -0800292static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700293{
Dima Zavinf395c922013-03-06 16:23:57 -0800294 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700295 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800296 const char *name = path;
297
298 if (!strncmp(path, "/devices/", 9)) {
299 name += 9;
300 if (!strncmp(name, "platform/", 9))
301 name += 9;
302 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700303
Wei Wanga285dac2016-10-04 14:05:39 -0700304 LOG(VERBOSE) << "adding platform device " << name << " (" << path << ")";
Colin Crossfadb85e2011-03-30 18:32:12 -0700305
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800306 bus = (platform_node*) calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800307 bus->path = strdup(path);
308 bus->path_len = path_len;
309 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700310 list_add_tail(&platform_names, &bus->list);
311}
312
313/*
Dima Zavinf395c922013-03-06 16:23:57 -0800314 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700315 * platform device prefix. If it doesn't start with a platform device, return
316 * 0.
317 */
Dima Zavinf395c922013-03-06 16:23:57 -0800318static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700319{
Dima Zavinf395c922013-03-06 16:23:57 -0800320 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700321 struct listnode *node;
322 struct platform_node *bus;
323
324 list_for_each_reverse(node, &platform_names) {
325 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800326 if ((bus->path_len < path_len) &&
327 (path[bus->path_len] == '/') &&
328 !strncmp(path, bus->path, bus->path_len))
329 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700330 }
331
332 return NULL;
333}
334
Dima Zavinf395c922013-03-06 16:23:57 -0800335static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700336{
337 struct listnode *node;
338 struct platform_node *bus;
339
340 list_for_each_reverse(node, &platform_names) {
341 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800342 if (!strcmp(path, bus->path)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700343 LOG(INFO) << "removing platform device " << bus->name;
Dima Zavinf395c922013-03-06 16:23:57 -0800344 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700345 list_remove(node);
346 free(bus);
347 return;
348 }
349 }
350}
351
Andrew Boiea885d042013-09-13 17:41:20 -0700352/* Given a path that may start with a PCI device, populate the supplied buffer
353 * with the PCI domain/bus number and the peripheral ID and return 0.
354 * If it doesn't start with a PCI device, or there is some error, return -1 */
355static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
356{
357 const char *start, *end;
358
359 if (strncmp(path, "/devices/pci", 12))
360 return -1;
361
362 /* Beginning of the prefix is the initial "pci" after "/devices/" */
363 start = path + 9;
364
365 /* End of the prefix is two path '/' later, capturing the domain/bus number
366 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
367 end = strchr(start, '/');
368 if (!end)
369 return -1;
370 end = strchr(end + 1, '/');
371 if (!end)
372 return -1;
373
374 /* Make sure we have enough room for the string plus null terminator */
375 if (end - start + 1 > buf_sz)
376 return -1;
377
378 strncpy(buf, start, end - start);
379 buf[end - start] = '\0';
380 return 0;
381}
382
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700383static void parse_event(const char *msg, struct uevent *uevent)
384{
385 uevent->action = "";
386 uevent->path = "";
387 uevent->subsystem = "";
388 uevent->firmware = "";
389 uevent->major = -1;
390 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700391 uevent->partition_name = NULL;
392 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700393 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700394
395 /* currently ignoring SEQNUM */
396 while(*msg) {
397 if(!strncmp(msg, "ACTION=", 7)) {
398 msg += 7;
399 uevent->action = msg;
400 } else if(!strncmp(msg, "DEVPATH=", 8)) {
401 msg += 8;
402 uevent->path = msg;
403 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
404 msg += 10;
405 uevent->subsystem = msg;
406 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
407 msg += 9;
408 uevent->firmware = msg;
409 } else if(!strncmp(msg, "MAJOR=", 6)) {
410 msg += 6;
411 uevent->major = atoi(msg);
412 } else if(!strncmp(msg, "MINOR=", 6)) {
413 msg += 6;
414 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700415 } else if(!strncmp(msg, "PARTN=", 6)) {
416 msg += 6;
417 uevent->partition_num = atoi(msg);
418 } else if(!strncmp(msg, "PARTNAME=", 9)) {
419 msg += 9;
420 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700421 } else if(!strncmp(msg, "DEVNAME=", 8)) {
422 msg += 8;
423 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700424 }
425
Wei Zhongf97b8872012-03-23 14:15:34 -0700426 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700427 while(*msg++)
428 ;
429 }
430
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800431 if (LOG_UEVENTS) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700432 LOG(INFO) << android::base::StringPrintf("event { '%s', '%s', '%s', '%s', %d, %d }",
433 uevent->action, uevent->path, uevent->subsystem,
434 uevent->firmware, uevent->major, uevent->minor);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800435 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700436}
437
Benoit Gobyd2278632010-08-03 14:36:02 -0700438static char **get_character_device_symlinks(struct uevent *uevent)
439{
440 const char *parent;
Dan Austin60b976d2016-03-28 14:22:12 -0700441 const char *slash;
Benoit Gobyd2278632010-08-03 14:36:02 -0700442 char **links;
443 int link_num = 0;
444 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800445 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700446
Dima Zavinf395c922013-03-06 16:23:57 -0800447 pdev = find_platform_device(uevent->path);
448 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700449 return NULL;
450
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800451 links = (char**) malloc(sizeof(char *) * 2);
Benoit Gobyd2278632010-08-03 14:36:02 -0700452 if (!links)
453 return NULL;
454 memset(links, 0, sizeof(char *) * 2);
455
456 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800457 parent = strchr(uevent->path + pdev->path_len, '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100458 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700459 goto err;
460
461 if (!strncmp(parent, "/usb", 4)) {
462 /* skip root hub name and device. use device interface */
463 while (*++parent && *parent != '/');
464 if (*parent)
465 while (*++parent && *parent != '/');
466 if (!*parent)
467 goto err;
468 slash = strchr(++parent, '/');
469 if (!slash)
470 goto err;
471 width = slash - parent;
472 if (width <= 0)
473 goto err;
474
475 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
476 link_num++;
477 else
478 links[link_num] = NULL;
479 mkdir("/dev/usb", 0755);
480 }
481 else {
482 goto err;
483 }
484
485 return links;
486err:
487 free(links);
488 return NULL;
489}
490
Andrew Boiea885d042013-09-13 17:41:20 -0700491static char **get_block_device_symlinks(struct uevent *uevent)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700492{
Colin Crossfadb85e2011-03-30 18:32:12 -0700493 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800494 struct platform_node *pdev;
Dan Austin60b976d2016-03-28 14:22:12 -0700495 const char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700496 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700497 char buf[256];
498 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700499 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700500 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700501
Dima Zavinf395c922013-03-06 16:23:57 -0800502 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700503 if (pdev) {
504 device = pdev->name;
505 type = "platform";
506 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
507 device = buf;
508 type = "pci";
509 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800510 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700511 }
Dima Zavinf395c922013-03-06 16:23:57 -0800512
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800513 char **links = (char**) malloc(sizeof(char *) * 4);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700514 if (!links)
515 return NULL;
516 memset(links, 0, sizeof(char *) * 4);
517
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700518 LOG(INFO) << "found " << type << " device " << device;
Colin Crossfadb85e2011-03-30 18:32:12 -0700519
Andrew Boiea885d042013-09-13 17:41:20 -0700520 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700521
522 if (uevent->partition_name) {
523 p = strdup(uevent->partition_name);
524 sanitize(p);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700525 if (strcmp(uevent->partition_name, p)) {
526 LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '" << p << "'";
527 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700528 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
529 link_num++;
530 else
531 links[link_num] = NULL;
532 free(p);
533 }
534
535 if (uevent->partition_num >= 0) {
536 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
537 link_num++;
538 else
539 links[link_num] = NULL;
540 }
541
Dima Zavinf395c922013-03-06 16:23:57 -0800542 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700543 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
544 link_num++;
545 else
546 links[link_num] = NULL;
547
548 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700549}
550
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700551static void make_link_init(const char* oldpath, const char* newpath) {
552 const char* slash = strrchr(newpath, '/');
553 if (!slash) return;
554
555 if (mkdir_recursive(dirname(newpath), 0755)) {
556 PLOG(ERROR) << "Failed to create directory " << dirname(newpath);
557 }
558
559 if (symlink(oldpath, newpath) && errno != EEXIST) {
560 PLOG(ERROR) << "Failed to symlink " << oldpath << " to " << newpath;
561 }
562}
563
564static void remove_link(const char* oldpath, const char* newpath) {
565 std::string path;
566 if (android::base::Readlink(newpath, &path) && path == oldpath) unlink(newpath);
567}
568
Colin Crosseb5ba832011-03-30 17:37:17 -0700569static void handle_device(const char *action, const char *devpath,
570 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700571{
Colin Crosseb5ba832011-03-30 17:37:17 -0700572 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400573 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700574 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700575 for (int i = 0; links[i]; i++) {
Chris Fries79f33842013-09-05 13:19:21 -0500576 make_link_init(devpath, links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700577 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700578 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700579 }
580
Colin Crosseb5ba832011-03-30 17:37:17 -0700581 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700582 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700583 for (int i = 0; links[i]; i++) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700584 remove_link(devpath, links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700585 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700586 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700587 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700588 }
589
590 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700591 for (int i = 0; links[i]; i++) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700592 free(links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700593 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700594 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700595 }
596}
597
Colin Crossfadb85e2011-03-30 18:32:12 -0700598static void handle_platform_device_event(struct uevent *uevent)
599{
Dima Zavinf395c922013-03-06 16:23:57 -0800600 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700601
602 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800603 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700604 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800605 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700606}
607
Colin Crosseb5ba832011-03-30 17:37:17 -0700608static const char *parse_device_name(struct uevent *uevent, unsigned int len)
609{
610 const char *name;
611
612 /* if it's not a /dev device, nothing else to do */
613 if((uevent->major < 0) || (uevent->minor < 0))
614 return NULL;
615
616 /* do we have a name? */
617 name = strrchr(uevent->path, '/');
618 if(!name)
619 return NULL;
620 name++;
621
622 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800623 if(strlen(name) > len) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700624 LOG(ERROR) << "DEVPATH=" << name << " exceeds " << len << "-character limit on filename; ignoring event";
Colin Crosseb5ba832011-03-30 17:37:17 -0700625 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800626 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700627
628 return name;
629}
630
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800631#define DEVPATH_LEN 96
632#define MAX_DEV_NAME 64
633
Colin Crosseb5ba832011-03-30 17:37:17 -0700634static void handle_block_device_event(struct uevent *uevent)
635{
636 const char *base = "/dev/block/";
637 const char *name;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800638 char devpath[DEVPATH_LEN];
Colin Crosseb5ba832011-03-30 17:37:17 -0700639 char **links = NULL;
640
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800641 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700642 if (!name)
643 return;
644
645 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500646 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700647
Dima Zavinf395c922013-03-06 16:23:57 -0800648 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700649 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700650
651 handle_device(uevent->action, devpath, uevent->path, 1,
652 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700653}
654
Greg Hackmann3312aa82013-11-18 15:24:40 -0800655static bool assemble_devpath(char *devpath, const char *dirname,
656 const char *devname)
657{
658 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
659 if (s < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700660 PLOG(ERROR) << "failed to assemble device path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800661 return false;
662 } else if (s >= DEVPATH_LEN) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700663 LOG(ERROR) << dirname << "/" << devname
664 << " exceeds " << DEVPATH_LEN << "-character limit on path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800665 return false;
666 }
667 return true;
668}
669
670static void mkdir_recursive_for_devpath(const char *devpath)
671{
672 char dir[DEVPATH_LEN];
673 char *slash;
674
675 strcpy(dir, devpath);
676 slash = strrchr(dir, '/');
677 *slash = '\0';
678 mkdir_recursive(dir, 0755);
679}
680
Colin Crosseb5ba832011-03-30 17:37:17 -0700681static void handle_generic_device_event(struct uevent *uevent)
682{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800683 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700684 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800685 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700686 char **links = NULL;
687
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800688 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700689 if (!name)
690 return;
691
Greg Hackmann3312aa82013-11-18 15:24:40 -0800692 struct ueventd_subsystem *subsystem =
693 ueventd_subsystem_find_by_name(uevent->subsystem);
694
695 if (subsystem) {
696 const char *devname;
697
698 switch (subsystem->devname_src) {
699 case DEVNAME_UEVENT_DEVNAME:
700 devname = uevent->device_name;
701 break;
702
703 case DEVNAME_UEVENT_DEVPATH:
704 devname = name;
705 break;
706
707 default:
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700708 LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800709 return;
710 }
711
712 if (!assemble_devpath(devpath, subsystem->dirname, devname))
713 return;
714 mkdir_recursive_for_devpath(devpath);
715 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700716 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700717 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800718 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800719 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800720 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700721 }
722 else {
723 /* This imitates the file system that would be created
724 * if we were using devfs instead.
725 * Minors are broken up into groups of 128, starting at "001"
726 */
727 int bus_id = uevent->minor / 128 + 1;
728 int device_id = uevent->minor % 128 + 1;
729 /* build directories */
730 make_dir("/dev/bus", 0755);
731 make_dir("/dev/bus/usb", 0755);
732 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
733 make_dir(devpath, 0755);
734 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
735 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700736 } else {
737 /* ignore other USB events */
738 return;
739 }
740 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
741 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500742 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200743 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
744 base = "/dev/dri/";
745 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700746 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
747 base = "/dev/oncrpc/";
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, "adsp", 4)) {
750 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500751 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700752 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
753 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500754 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700755 } else if(!strncmp(uevent->subsystem, "input", 5)) {
756 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500757 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700758 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
759 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500760 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700761 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
762 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500763 make_dir(base, 0755);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700764 } else if(!strncmp(uevent->subsystem, "misc", 4) && !strncmp(name, "log_", 4)) {
765 LOG(INFO) << "kernel logger is deprecated";
Colin Crosseb5ba832011-03-30 17:37:17 -0700766 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500767 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700768 name += 4;
769 } else
770 base = "/dev/";
771 links = get_character_device_symlinks(uevent);
772
773 if (!devpath[0])
774 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
775
776 handle_device(uevent->action, devpath, uevent->path, 0,
777 uevent->major, uevent->minor, links);
778}
779
780static void handle_device_event(struct uevent *uevent)
781{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700782 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Rob Herring6de783a2016-05-06 10:06:59 -0500783 fixup_sys_perms(uevent->path, uevent->subsystem);
Colin Crosseb5ba832011-03-30 17:37:17 -0700784
785 if (!strncmp(uevent->subsystem, "block", 5)) {
786 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700787 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
788 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700789 } else {
790 handle_generic_device_event(uevent);
791 }
792}
793
Elliott Hughes632e99a2016-11-12 11:44:16 -0800794static void load_firmware(uevent* uevent, const std::string& root,
795 int fw_fd, size_t fw_size,
796 int loading_fd, int data_fd) {
797 // Start transfer.
798 android::base::WriteFully(loading_fd, "1", 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700799
Elliott Hughes632e99a2016-11-12 11:44:16 -0800800 // Copy the firmware.
801 int rc = sendfile(data_fd, fw_fd, nullptr, fw_size);
802 if (rc == -1) {
803 PLOG(ERROR) << "firmware: sendfile failed { '" << root << "', '" << uevent->firmware << "' }";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700804 }
805
Elliott Hughes632e99a2016-11-12 11:44:16 -0800806 // Tell the firmware whether to abort or commit.
807 const char* response = (rc != -1) ? "0" : "-1";
808 android::base::WriteFully(loading_fd, response, strlen(response));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700809}
810
Elliott Hughes632e99a2016-11-12 11:44:16 -0800811static int is_booting() {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700812 return access("/dev/.booting", F_OK) == 0;
813}
814
Elliott Hughes632e99a2016-11-12 11:44:16 -0800815static void process_firmware_event(uevent* uevent) {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700816 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700817
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700818 LOG(INFO) << "firmware: loading '" << uevent->firmware << "' for '" << uevent->path << "'";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700819
Elliott Hughes632e99a2016-11-12 11:44:16 -0800820 std::string root = android::base::StringPrintf("/sys%s", uevent->path);
821 std::string loading = root + "/loading";
822 std::string data = root + "/data";
823
824 android::base::unique_fd loading_fd(open(loading.c_str(), O_WRONLY|O_CLOEXEC));
825 if (loading_fd == -1) {
826 PLOG(ERROR) << "couldn't open firmware loading fd for " << uevent->firmware;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700827 return;
Elliott Hughes632e99a2016-11-12 11:44:16 -0800828 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700829
Elliott Hughes632e99a2016-11-12 11:44:16 -0800830 android::base::unique_fd data_fd(open(data.c_str(), O_WRONLY|O_CLOEXEC));
831 if (data_fd == -1) {
832 PLOG(ERROR) << "couldn't open firmware data fd for " << uevent->firmware;
833 return;
834 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700835
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700836try_loading_again:
Elliott Hughes632e99a2016-11-12 11:44:16 -0800837 for (size_t i = 0; i < arraysize(firmware_dirs); i++) {
838 std::string file = android::base::StringPrintf("%s/%s", firmware_dirs[i], uevent->firmware);
839 android::base::unique_fd fw_fd(open(file.c_str(), O_RDONLY|O_CLOEXEC));
840 struct stat sb;
841 if (fw_fd != -1 && fstat(fw_fd, &sb) != -1) {
842 load_firmware(uevent, root, fw_fd, sb.st_size, loading_fd, data_fd);
843 return;
Benoit Goby609d8822010-11-09 18:10:24 -0800844 }
Brian Swetland02863b92010-09-19 03:36:39 -0700845 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700846
Elliott Hughes632e99a2016-11-12 11:44:16 -0800847 if (booting) {
848 // If we're not fully booted, we may be missing
849 // filesystems needed for firmware, wait and retry.
Elliott Hughes290a2282016-11-14 17:08:47 -0800850 std::this_thread::sleep_for(100ms);
Elliott Hughes632e99a2016-11-12 11:44:16 -0800851 booting = is_booting();
852 goto try_loading_again;
853 }
854
855 LOG(ERROR) << "firmware: could not find firmware for " << uevent->firmware;
856
857 // Write "-1" as our response to the kernel's firmware request, since we have nothing for it.
858 write(loading_fd, "-1", 2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700859}
860
Elliott Hughes632e99a2016-11-12 11:44:16 -0800861static void handle_firmware_event(uevent* uevent) {
862 if (strcmp(uevent->subsystem, "firmware")) return;
863 if (strcmp(uevent->action, "add")) return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700864
Elliott Hughes632e99a2016-11-12 11:44:16 -0800865 // Loading the firmware in a child means we can do that in parallel...
866 // (We ignore SIGCHLD rather than wait for our children.)
867 pid_t pid = fork();
868 if (pid == 0) {
869 Timer t;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700870 process_firmware_event(uevent);
Elliott Hughes632e99a2016-11-12 11:44:16 -0800871 LOG(INFO) << "loading " << uevent->path << " took " << t.duration() << "s";
Kenny Root17baff42014-08-20 16:16:44 -0700872 _exit(EXIT_SUCCESS);
Elliott Hughes632e99a2016-11-12 11:44:16 -0800873 } else if (pid == -1) {
874 PLOG(ERROR) << "could not fork to process firmware event for " << uevent->firmware;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700875 }
876}
877
Ruchi Kandoic6037202014-06-23 11:22:09 -0700878#define UEVENT_MSG_LEN 2048
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800879
880static inline void handle_device_fd_with(void (handle_uevent)(struct uevent*))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700881{
Vernon Tang3f582e92011-04-25 13:08:17 +1000882 char msg[UEVENT_MSG_LEN+2];
883 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700884 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700885 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700886 continue;
887
888 msg[n] = '\0';
889 msg[n+1] = '\0';
890
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700891 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700892 parse_event(msg, &uevent);
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800893 handle_uevent(&uevent);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700894 }
895}
896
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800897void handle_device_fd()
898{
899 handle_device_fd_with(
900 [](struct uevent *uevent) {
901 if (selinux_status_updated() > 0) {
902 struct selabel_handle *sehandle2;
903 sehandle2 = selinux_android_file_context_handle();
904 if (sehandle2) {
905 selabel_close(sehandle);
906 sehandle = sehandle2;
907 }
908 }
909
910 handle_device_event(uevent);
911 handle_firmware_event(uevent);
912 });
913}
914
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700915/* Coldboot walks parts of the /sys tree and pokes the uevent files
916** to cause the kernel to regenerate device add events that happened
917** before init's device manager was started
918**
919** We drain any pending events from the netlink socket every time
920** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800921** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700922*/
923
Colin Cross0dd7ca62010-04-13 19:25:51 -0700924static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700925{
926 struct dirent *de;
927 int dfd, fd;
928
929 dfd = dirfd(d);
930
931 fd = openat(dfd, "uevent", O_WRONLY);
932 if(fd >= 0) {
933 write(fd, "add\n", 4);
934 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700935 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700936 }
937
938 while((de = readdir(d))) {
939 DIR *d2;
940
941 if(de->d_type != DT_DIR || de->d_name[0] == '.')
942 continue;
943
944 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
945 if(fd < 0)
946 continue;
947
948 d2 = fdopendir(fd);
949 if(d2 == 0)
950 close(fd);
951 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700952 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700953 closedir(d2);
954 }
955 }
956}
957
Colin Cross0dd7ca62010-04-13 19:25:51 -0700958static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700959{
James Hawkins588a2ca2016-02-18 14:52:46 -0800960 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700961 if(d) {
James Hawkins588a2ca2016-02-18 14:52:46 -0800962 do_coldboot(d.get());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700963 }
964}
965
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800966static void early_uevent_handler(struct uevent *uevent, const char *base, bool is_block)
967{
968 const char *name;
969 char devpath[DEVPATH_LEN];
970
971 if (is_block && strncmp(uevent->subsystem, "block", 5))
972 return;
973
974 name = parse_device_name(uevent, MAX_DEV_NAME);
975 if (!name) {
976 LOG(ERROR) << "Failed to parse dev name from uevent: " << uevent->action
977 << " " << uevent->partition_name << " " << uevent->partition_num
978 << " " << uevent->major << ":" << uevent->minor;
979 return;
980 }
981
982 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
983 make_dir(base, 0755);
984
985 dev_t dev = makedev(uevent->major, uevent->minor);
986 mode_t mode = 0600 | (is_block ? S_IFBLK : S_IFCHR);
987 mknod(devpath, mode, dev);
988}
989
990void early_create_dev(const std::string& syspath, early_device_type dev_type)
991{
992 android::base::unique_fd dfd(open(syspath.c_str(), O_RDONLY));
993 if (dfd < 0) {
994 LOG(ERROR) << "Failed to open " << syspath;
995 return;
996 }
997
998 android::base::unique_fd fd(openat(dfd, "uevent", O_WRONLY));
999 if (fd < 0) {
1000 LOG(ERROR) << "Failed to open " << syspath << "/uevent";
1001 return;
1002 }
1003
1004 fcntl(device_fd, F_SETFL, O_NONBLOCK);
1005
1006 write(fd, "add\n", 4);
1007 handle_device_fd_with(dev_type == EARLY_BLOCK_DEV ?
1008 [](struct uevent *uevent) {
1009 early_uevent_handler(uevent, "/dev/block/", true);
1010 } :
1011 [](struct uevent *uevent) {
1012 early_uevent_handler(uevent, "/dev/", false);
1013 });
1014}
1015
1016int early_device_socket_open() {
1017 device_fd = uevent_open_socket(256*1024, true);
1018 return device_fd < 0;
1019}
1020
1021void early_device_socket_close() {
1022 close(device_fd);
1023}
1024
Elliott Hughes74738362015-03-28 10:51:23 -07001025void device_init() {
Nick Kralevich4d870952015-06-12 22:03:50 -07001026 sehandle = selinux_android_file_context_handle();
1027 selinux_status_open(true);
Kenny Rootb5982bf2012-10-16 23:07:05 -07001028
Andrew Boied562ca72012-12-04 15:47:20 -08001029 /* is 256K enough? udev uses 16MB! */
1030 device_fd = uevent_open_socket(256*1024, true);
Elliott Hughes56a06562015-03-28 11:23:32 -07001031 if (device_fd == -1) {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001032 return;
Elliott Hughes56a06562015-03-28 11:23:32 -07001033 }
Colin Cross0dd7ca62010-04-13 19:25:51 -07001034 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001035
Elliott Hughes56a06562015-03-28 11:23:32 -07001036 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -07001037 LOG(VERBOSE) << "Skipping coldboot, already done!";
Elliott Hughes56a06562015-03-28 11:23:32 -07001038 return;
Colin Crossf83d0b92010-04-21 12:04:20 -07001039 }
Elliott Hughes56a06562015-03-28 11:23:32 -07001040
1041 Timer t;
1042 coldboot("/sys/class");
1043 coldboot("/sys/block");
1044 coldboot("/sys/devices");
1045 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
Elliott Hughesf86b5a62016-06-24 15:12:21 -07001046 LOG(INFO) << "Coldboot took " << t.duration() << "s.";
Colin Cross0dd7ca62010-04-13 19:25:51 -07001047}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001048
Elliott Hughes632e99a2016-11-12 11:44:16 -08001049int get_device_fd() {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001050 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001051}