blob: bad04aec162058dc18ae014455381036de27e2ba [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>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070026#include <sys/socket.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070027#include <sys/stat.h>
28#include <sys/time.h>
29#include <sys/types.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070030#include <sys/un.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070031#include <sys/wait.h>
32#include <unistd.h>
33
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070034#include <linux/netlink.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050035
James Hawkins588a2ca2016-02-18 14:52:46 -080036#include <memory>
37
Stephen Smalleye46f9d52012-01-13 08:48:47 -050038#include <selinux/selinux.h>
39#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040040#include <selinux/android.h>
Stephen Smalleye2eb69d2013-04-16 09:30:30 -040041#include <selinux/avc.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050042
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070043#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070044
Biao Ludc848562016-01-28 16:10:54 +080045#include <android-base/file.h>
Rob Herring6de783a2016-05-06 10:06:59 -050046#include <android-base/stringprintf.h>
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +080047#include <android-base/unique_fd.h>
Dima Zavinda04c522011-09-01 17:09:44 -070048#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100049#include <cutils/uevent.h>
50
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070051#include "devices.h"
Greg Hackmann3312aa82013-11-18 15:24:40 -080052#include "ueventd_parser.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070053#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070054#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070055
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070056#define SYSFS_PREFIX "/sys"
Daniel Rosenbergd1d96022015-03-20 15:10:29 -070057static const char *firmware_dirs[] = { "/etc/firmware",
58 "/vendor/firmware",
59 "/firmware/image" };
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060
Stephen Smalleye096e362012-06-11 13:37:39 -040061extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050062
Colin Cross0dd7ca62010-04-13 19:25:51 -070063static int device_fd = -1;
64
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070065struct uevent {
66 const char *action;
67 const char *path;
68 const char *subsystem;
69 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070070 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070071 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070072 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073 int major;
74 int minor;
75};
76
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070077struct perms_ {
78 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070079 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070080 mode_t perm;
81 unsigned int uid;
82 unsigned int gid;
83 unsigned short prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070084 unsigned short wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070085};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070086
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070087struct perm_node {
88 struct perms_ dp;
89 struct listnode plist;
90};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070091
Colin Crossfadb85e2011-03-30 18:32:12 -070092struct platform_node {
93 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080094 char *path;
95 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070096 struct listnode list;
97};
98
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070099static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -0700100static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -0700101static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700102
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700103int add_dev_perms(const char *name, const char *attr,
104 mode_t perm, unsigned int uid, unsigned int gid,
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700105 unsigned short prefix,
106 unsigned short wildcard) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800107 struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700108 if (!node)
109 return -ENOMEM;
110
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700111 node->dp.name = strdup(name);
112 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700113 return -ENOMEM;
114
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700115 if (attr) {
116 node->dp.attr = strdup(attr);
117 if (!node->dp.attr)
118 return -ENOMEM;
119 }
120
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700121 node->dp.perm = perm;
122 node->dp.uid = uid;
123 node->dp.gid = gid;
124 node->dp.prefix = prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700125 node->dp.wildcard = wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700126
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700127 if (attr)
128 list_add_tail(&sys_perms, &node->plist);
129 else
130 list_add_tail(&dev_perms, &node->plist);
131
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700132 return 0;
133}
134
Colin Cross43d537e2014-07-02 13:08:13 -0700135static bool perm_path_matches(const char *path, struct perms_ *dp)
136{
137 if (dp->prefix) {
138 if (strncmp(path, dp->name, strlen(dp->name)) == 0)
139 return true;
140 } else if (dp->wildcard) {
141 if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
142 return true;
143 } else {
144 if (strcmp(path, dp->name) == 0)
145 return true;
146 }
147
148 return false;
149}
150
Rob Herring6de783a2016-05-06 10:06:59 -0500151static bool match_subsystem(perms_* dp, const char* pattern,
152 const char* path, const char* subsystem) {
153 if (!pattern || !subsystem || strstr(dp->name, subsystem) == NULL) {
154 return false;
155 }
Rob Herringe5636a32016-05-06 12:28:48 -0500156
Rob Herring6de783a2016-05-06 10:06:59 -0500157 std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path));
158 return perm_path_matches(subsys_path.c_str(), dp);
159}
Rob Herringe5636a32016-05-06 12:28:48 -0500160
Rob Herring6de783a2016-05-06 10:06:59 -0500161static void fixup_sys_perms(const char* upath, const char* subsystem) {
162 // upaths omit the "/sys" that paths in this list
163 // contain, so we prepend it...
164 std::string path = std::string(SYSFS_PREFIX) + upath;
165
166 listnode* node;
Rob Herringe5636a32016-05-06 12:28:48 -0500167 list_for_each(node, &sys_perms) {
Rob Herring6de783a2016-05-06 10:06:59 -0500168 perms_* dp = &(node_to_item(node, perm_node, plist))->dp;
169 if (match_subsystem(dp, SYSFS_PREFIX "/class/%s/%s", path.c_str(), subsystem)) {
170 ; // matched
171 } else if (match_subsystem(dp, SYSFS_PREFIX "/bus/%s/devices/%s", path.c_str(), subsystem)) {
172 ; // matched
173 } else if (!perm_path_matches(path.c_str(), dp)) {
174 continue;
Rob Herringe5636a32016-05-06 12:28:48 -0500175 }
176
177 std::string attr_file = path + "/" + dp->attr;
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700178 LOG(INFO) << "fixup " << attr_file
179 << " " << dp->uid << " " << dp->gid << " " << std::oct << dp->perm;
Rob Herringe5636a32016-05-06 12:28:48 -0500180 chown(attr_file.c_str(), dp->uid, dp->gid);
181 chmod(attr_file.c_str(), dp->perm);
182 }
183
184 if (access(path.c_str(), F_OK) == 0) {
Dmitry Shmidt7eed4742016-07-28 13:55:39 -0700185 LOG(VERBOSE) << "restorecon_recursive: " << path;
Rob Herringe5636a32016-05-06 12:28:48 -0500186 restorecon_recursive(path.c_str());
187 }
188}
189
Colin Cross43d537e2014-07-02 13:08:13 -0700190static mode_t get_device_perm(const char *path, const char **links,
191 unsigned *uid, unsigned *gid)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700192{
Colin Cross44b65d02010-04-20 14:32:50 -0700193 struct listnode *node;
194 struct perm_node *perm_node;
195 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700196
Colin Cross44b65d02010-04-20 14:32:50 -0700197 /* search the perms list in reverse so that ueventd.$hardware can
198 * override ueventd.rc
199 */
200 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700201 bool match = false;
202
Colin Cross44b65d02010-04-20 14:32:50 -0700203 perm_node = node_to_item(node, struct perm_node, plist);
204 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700205
Colin Cross43d537e2014-07-02 13:08:13 -0700206 if (perm_path_matches(path, dp)) {
207 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700208 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700209 if (links) {
210 int i;
211 for (i = 0; links[i]; i++) {
212 if (perm_path_matches(links[i], dp)) {
213 match = true;
214 break;
215 }
216 }
217 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700218 }
Colin Cross43d537e2014-07-02 13:08:13 -0700219
220 if (match) {
221 *uid = dp->uid;
222 *gid = dp->gid;
223 return dp->perm;
224 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700225 }
Colin Cross44b65d02010-04-20 14:32:50 -0700226 /* Default if nothing found. */
227 *uid = 0;
228 *gid = 0;
229 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700230}
231
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700232static void make_device(const char *path,
Elliott Hughesf682b472015-02-06 12:19:48 -0800233 const char */*upath*/,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400234 int block, int major, int minor,
235 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700236{
237 unsigned uid;
238 unsigned gid;
239 mode_t mode;
240 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500241 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700242
Colin Cross43d537e2014-07-02 13:08:13 -0700243 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700244
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300245 if (selabel_lookup_best_match(sehandle, &secontext, path, links, mode)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700246 PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300247 return;
248 }
Nick Kralevich4d870952015-06-12 22:03:50 -0700249 setfscreatecon(secontext);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700250
Colin Cross17dcc5c2010-09-03 12:25:34 -0700251 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800252 /* Temporarily change egid to avoid race condition setting the gid of the
253 * device node. Unforunately changing the euid would prevent creation of
254 * some device nodes, so the uid has to be set with chown() and is still
255 * racy. Fixing the gid race at least fixed the issue with system_server
256 * opening dynamic input devices under the AID_INPUT gid. */
257 setegid(gid);
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300258 /* If the node already exists update its SELinux label to handle cases when
259 * it was created with the wrong context during coldboot procedure. */
260 if (mknod(path, mode, dev) && (errno == EEXIST)) {
William Roberts397de142016-06-02 09:53:44 -0700261
262 char* fcon = nullptr;
263 int rc = lgetfilecon(path, &fcon);
264 if (rc < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700265 PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
William Roberts397de142016-06-02 09:53:44 -0700266 goto out;
267 }
268
269 bool different = strcmp(fcon, secontext) != 0;
270 freecon(fcon);
271
272 if (different && lsetfilecon(path, secontext)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700273 PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path << "' device";
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300274 }
275 }
William Roberts397de142016-06-02 09:53:44 -0700276
277out:
Nick Pelly6405c692010-01-21 18:13:39 -0800278 chown(path, uid, -1);
279 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700280
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300281 freecon(secontext);
282 setfscreatecon(NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700283}
284
Dima Zavinf395c922013-03-06 16:23:57 -0800285static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700286{
Dima Zavinf395c922013-03-06 16:23:57 -0800287 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700288 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800289 const char *name = path;
290
291 if (!strncmp(path, "/devices/", 9)) {
292 name += 9;
293 if (!strncmp(name, "platform/", 9))
294 name += 9;
295 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700296
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700297 LOG(INFO) << "adding platform device " << name << " (" << path << ")";
Colin Crossfadb85e2011-03-30 18:32:12 -0700298
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800299 bus = (platform_node*) calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800300 bus->path = strdup(path);
301 bus->path_len = path_len;
302 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700303 list_add_tail(&platform_names, &bus->list);
304}
305
306/*
Dima Zavinf395c922013-03-06 16:23:57 -0800307 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700308 * platform device prefix. If it doesn't start with a platform device, return
309 * 0.
310 */
Dima Zavinf395c922013-03-06 16:23:57 -0800311static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700312{
Dima Zavinf395c922013-03-06 16:23:57 -0800313 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700314 struct listnode *node;
315 struct platform_node *bus;
316
317 list_for_each_reverse(node, &platform_names) {
318 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800319 if ((bus->path_len < path_len) &&
320 (path[bus->path_len] == '/') &&
321 !strncmp(path, bus->path, bus->path_len))
322 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700323 }
324
325 return NULL;
326}
327
Dima Zavinf395c922013-03-06 16:23:57 -0800328static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700329{
330 struct listnode *node;
331 struct platform_node *bus;
332
333 list_for_each_reverse(node, &platform_names) {
334 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800335 if (!strcmp(path, bus->path)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700336 LOG(INFO) << "removing platform device " << bus->name;
Dima Zavinf395c922013-03-06 16:23:57 -0800337 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700338 list_remove(node);
339 free(bus);
340 return;
341 }
342 }
343}
344
Andrew Boiea885d042013-09-13 17:41:20 -0700345/* Given a path that may start with a PCI device, populate the supplied buffer
346 * with the PCI domain/bus number and the peripheral ID and return 0.
347 * If it doesn't start with a PCI device, or there is some error, return -1 */
348static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
349{
350 const char *start, *end;
351
352 if (strncmp(path, "/devices/pci", 12))
353 return -1;
354
355 /* Beginning of the prefix is the initial "pci" after "/devices/" */
356 start = path + 9;
357
358 /* End of the prefix is two path '/' later, capturing the domain/bus number
359 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
360 end = strchr(start, '/');
361 if (!end)
362 return -1;
363 end = strchr(end + 1, '/');
364 if (!end)
365 return -1;
366
367 /* Make sure we have enough room for the string plus null terminator */
368 if (end - start + 1 > buf_sz)
369 return -1;
370
371 strncpy(buf, start, end - start);
372 buf[end - start] = '\0';
373 return 0;
374}
375
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700376static void parse_event(const char *msg, struct uevent *uevent)
377{
378 uevent->action = "";
379 uevent->path = "";
380 uevent->subsystem = "";
381 uevent->firmware = "";
382 uevent->major = -1;
383 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700384 uevent->partition_name = NULL;
385 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700386 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700387
388 /* currently ignoring SEQNUM */
389 while(*msg) {
390 if(!strncmp(msg, "ACTION=", 7)) {
391 msg += 7;
392 uevent->action = msg;
393 } else if(!strncmp(msg, "DEVPATH=", 8)) {
394 msg += 8;
395 uevent->path = msg;
396 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
397 msg += 10;
398 uevent->subsystem = msg;
399 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
400 msg += 9;
401 uevent->firmware = msg;
402 } else if(!strncmp(msg, "MAJOR=", 6)) {
403 msg += 6;
404 uevent->major = atoi(msg);
405 } else if(!strncmp(msg, "MINOR=", 6)) {
406 msg += 6;
407 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700408 } else if(!strncmp(msg, "PARTN=", 6)) {
409 msg += 6;
410 uevent->partition_num = atoi(msg);
411 } else if(!strncmp(msg, "PARTNAME=", 9)) {
412 msg += 9;
413 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700414 } else if(!strncmp(msg, "DEVNAME=", 8)) {
415 msg += 8;
416 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700417 }
418
Wei Zhongf97b8872012-03-23 14:15:34 -0700419 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700420 while(*msg++)
421 ;
422 }
423
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800424 if (LOG_UEVENTS) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700425 LOG(INFO) << android::base::StringPrintf("event { '%s', '%s', '%s', '%s', %d, %d }",
426 uevent->action, uevent->path, uevent->subsystem,
427 uevent->firmware, uevent->major, uevent->minor);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800428 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700429}
430
Benoit Gobyd2278632010-08-03 14:36:02 -0700431static char **get_character_device_symlinks(struct uevent *uevent)
432{
433 const char *parent;
Dan Austin60b976d2016-03-28 14:22:12 -0700434 const char *slash;
Benoit Gobyd2278632010-08-03 14:36:02 -0700435 char **links;
436 int link_num = 0;
437 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800438 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700439
Dima Zavinf395c922013-03-06 16:23:57 -0800440 pdev = find_platform_device(uevent->path);
441 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700442 return NULL;
443
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800444 links = (char**) malloc(sizeof(char *) * 2);
Benoit Gobyd2278632010-08-03 14:36:02 -0700445 if (!links)
446 return NULL;
447 memset(links, 0, sizeof(char *) * 2);
448
449 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800450 parent = strchr(uevent->path + pdev->path_len, '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100451 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700452 goto err;
453
454 if (!strncmp(parent, "/usb", 4)) {
455 /* skip root hub name and device. use device interface */
456 while (*++parent && *parent != '/');
457 if (*parent)
458 while (*++parent && *parent != '/');
459 if (!*parent)
460 goto err;
461 slash = strchr(++parent, '/');
462 if (!slash)
463 goto err;
464 width = slash - parent;
465 if (width <= 0)
466 goto err;
467
468 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
469 link_num++;
470 else
471 links[link_num] = NULL;
472 mkdir("/dev/usb", 0755);
473 }
474 else {
475 goto err;
476 }
477
478 return links;
479err:
480 free(links);
481 return NULL;
482}
483
Andrew Boiea885d042013-09-13 17:41:20 -0700484static char **get_block_device_symlinks(struct uevent *uevent)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700485{
Colin Crossfadb85e2011-03-30 18:32:12 -0700486 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800487 struct platform_node *pdev;
Dan Austin60b976d2016-03-28 14:22:12 -0700488 const char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700489 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700490 char buf[256];
491 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700492 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700493 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700494
Dima Zavinf395c922013-03-06 16:23:57 -0800495 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700496 if (pdev) {
497 device = pdev->name;
498 type = "platform";
499 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
500 device = buf;
501 type = "pci";
502 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800503 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700504 }
Dima Zavinf395c922013-03-06 16:23:57 -0800505
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800506 char **links = (char**) malloc(sizeof(char *) * 4);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700507 if (!links)
508 return NULL;
509 memset(links, 0, sizeof(char *) * 4);
510
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700511 LOG(INFO) << "found " << type << " device " << device;
Colin Crossfadb85e2011-03-30 18:32:12 -0700512
Andrew Boiea885d042013-09-13 17:41:20 -0700513 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700514
515 if (uevent->partition_name) {
516 p = strdup(uevent->partition_name);
517 sanitize(p);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700518 if (strcmp(uevent->partition_name, p)) {
519 LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '" << p << "'";
520 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700521 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
522 link_num++;
523 else
524 links[link_num] = NULL;
525 free(p);
526 }
527
528 if (uevent->partition_num >= 0) {
529 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
530 link_num++;
531 else
532 links[link_num] = NULL;
533 }
534
Dima Zavinf395c922013-03-06 16:23:57 -0800535 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700536 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
537 link_num++;
538 else
539 links[link_num] = NULL;
540
541 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700542}
543
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700544static void make_link_init(const char* oldpath, const char* newpath) {
545 const char* slash = strrchr(newpath, '/');
546 if (!slash) return;
547
548 if (mkdir_recursive(dirname(newpath), 0755)) {
549 PLOG(ERROR) << "Failed to create directory " << dirname(newpath);
550 }
551
552 if (symlink(oldpath, newpath) && errno != EEXIST) {
553 PLOG(ERROR) << "Failed to symlink " << oldpath << " to " << newpath;
554 }
555}
556
557static void remove_link(const char* oldpath, const char* newpath) {
558 std::string path;
559 if (android::base::Readlink(newpath, &path) && path == oldpath) unlink(newpath);
560}
561
Colin Crosseb5ba832011-03-30 17:37:17 -0700562static void handle_device(const char *action, const char *devpath,
563 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700564{
Colin Crosseb5ba832011-03-30 17:37:17 -0700565 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400566 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700567 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700568 for (int i = 0; links[i]; i++) {
Chris Fries79f33842013-09-05 13:19:21 -0500569 make_link_init(devpath, links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700570 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700571 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700572 }
573
Colin Crosseb5ba832011-03-30 17:37:17 -0700574 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700575 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700576 for (int i = 0; links[i]; i++) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700577 remove_link(devpath, links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700578 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700579 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700580 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700581 }
582
583 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700584 for (int i = 0; links[i]; i++) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700585 free(links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700586 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700587 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700588 }
589}
590
Colin Crossfadb85e2011-03-30 18:32:12 -0700591static void handle_platform_device_event(struct uevent *uevent)
592{
Dima Zavinf395c922013-03-06 16:23:57 -0800593 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700594
595 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800596 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700597 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800598 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700599}
600
Colin Crosseb5ba832011-03-30 17:37:17 -0700601static const char *parse_device_name(struct uevent *uevent, unsigned int len)
602{
603 const char *name;
604
605 /* if it's not a /dev device, nothing else to do */
606 if((uevent->major < 0) || (uevent->minor < 0))
607 return NULL;
608
609 /* do we have a name? */
610 name = strrchr(uevent->path, '/');
611 if(!name)
612 return NULL;
613 name++;
614
615 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800616 if(strlen(name) > len) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700617 LOG(ERROR) << "DEVPATH=" << name << " exceeds " << len << "-character limit on filename; ignoring event";
Colin Crosseb5ba832011-03-30 17:37:17 -0700618 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800619 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700620
621 return name;
622}
623
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800624#define DEVPATH_LEN 96
625#define MAX_DEV_NAME 64
626
Colin Crosseb5ba832011-03-30 17:37:17 -0700627static void handle_block_device_event(struct uevent *uevent)
628{
629 const char *base = "/dev/block/";
630 const char *name;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800631 char devpath[DEVPATH_LEN];
Colin Crosseb5ba832011-03-30 17:37:17 -0700632 char **links = NULL;
633
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800634 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700635 if (!name)
636 return;
637
638 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500639 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700640
Dima Zavinf395c922013-03-06 16:23:57 -0800641 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700642 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700643
644 handle_device(uevent->action, devpath, uevent->path, 1,
645 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700646}
647
Greg Hackmann3312aa82013-11-18 15:24:40 -0800648static bool assemble_devpath(char *devpath, const char *dirname,
649 const char *devname)
650{
651 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
652 if (s < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700653 PLOG(ERROR) << "failed to assemble device path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800654 return false;
655 } else if (s >= DEVPATH_LEN) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700656 LOG(ERROR) << dirname << "/" << devname
657 << " exceeds " << DEVPATH_LEN << "-character limit on path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800658 return false;
659 }
660 return true;
661}
662
663static void mkdir_recursive_for_devpath(const char *devpath)
664{
665 char dir[DEVPATH_LEN];
666 char *slash;
667
668 strcpy(dir, devpath);
669 slash = strrchr(dir, '/');
670 *slash = '\0';
671 mkdir_recursive(dir, 0755);
672}
673
Colin Crosseb5ba832011-03-30 17:37:17 -0700674static void handle_generic_device_event(struct uevent *uevent)
675{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800676 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700677 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800678 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700679 char **links = NULL;
680
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800681 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700682 if (!name)
683 return;
684
Greg Hackmann3312aa82013-11-18 15:24:40 -0800685 struct ueventd_subsystem *subsystem =
686 ueventd_subsystem_find_by_name(uevent->subsystem);
687
688 if (subsystem) {
689 const char *devname;
690
691 switch (subsystem->devname_src) {
692 case DEVNAME_UEVENT_DEVNAME:
693 devname = uevent->device_name;
694 break;
695
696 case DEVNAME_UEVENT_DEVPATH:
697 devname = name;
698 break;
699
700 default:
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700701 LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800702 return;
703 }
704
705 if (!assemble_devpath(devpath, subsystem->dirname, devname))
706 return;
707 mkdir_recursive_for_devpath(devpath);
708 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700709 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700710 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800711 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800712 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800713 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700714 }
715 else {
716 /* This imitates the file system that would be created
717 * if we were using devfs instead.
718 * Minors are broken up into groups of 128, starting at "001"
719 */
720 int bus_id = uevent->minor / 128 + 1;
721 int device_id = uevent->minor % 128 + 1;
722 /* build directories */
723 make_dir("/dev/bus", 0755);
724 make_dir("/dev/bus/usb", 0755);
725 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
726 make_dir(devpath, 0755);
727 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
728 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700729 } else {
730 /* ignore other USB events */
731 return;
732 }
733 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
734 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500735 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200736 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
737 base = "/dev/dri/";
738 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700739 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
740 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500741 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700742 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
743 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500744 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700745 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
746 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500747 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700748 } else if(!strncmp(uevent->subsystem, "input", 5)) {
749 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500750 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700751 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
752 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500753 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700754 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
755 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500756 make_dir(base, 0755);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700757 } else if(!strncmp(uevent->subsystem, "misc", 4) && !strncmp(name, "log_", 4)) {
758 LOG(INFO) << "kernel logger is deprecated";
Colin Crosseb5ba832011-03-30 17:37:17 -0700759 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500760 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700761 name += 4;
762 } else
763 base = "/dev/";
764 links = get_character_device_symlinks(uevent);
765
766 if (!devpath[0])
767 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
768
769 handle_device(uevent->action, devpath, uevent->path, 0,
770 uevent->major, uevent->minor, links);
771}
772
773static void handle_device_event(struct uevent *uevent)
774{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700775 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Rob Herring6de783a2016-05-06 10:06:59 -0500776 fixup_sys_perms(uevent->path, uevent->subsystem);
Colin Crosseb5ba832011-03-30 17:37:17 -0700777
778 if (!strncmp(uevent->subsystem, "block", 5)) {
779 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700780 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
781 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700782 } else {
783 handle_generic_device_event(uevent);
784 }
785}
786
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700787static int load_firmware(int fw_fd, int loading_fd, int data_fd)
788{
789 struct stat st;
790 long len_to_copy;
791 int ret = 0;
792
793 if(fstat(fw_fd, &st) < 0)
794 return -1;
795 len_to_copy = st.st_size;
796
797 write(loading_fd, "1", 1); /* start transfer */
798
799 while (len_to_copy > 0) {
800 char buf[PAGE_SIZE];
801 ssize_t nr;
802
803 nr = read(fw_fd, buf, sizeof(buf));
804 if(!nr)
805 break;
806 if(nr < 0) {
807 ret = -1;
808 break;
809 }
Biao Ludc848562016-01-28 16:10:54 +0800810 if (!android::base::WriteFully(data_fd, buf, nr)) {
811 ret = -1;
812 break;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700813 }
Biao Ludc848562016-01-28 16:10:54 +0800814 len_to_copy -= nr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700815 }
816
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700817 if(!ret)
818 write(loading_fd, "0", 1); /* successful end of transfer */
819 else
820 write(loading_fd, "-1", 2); /* abort transfer */
821
822 return ret;
823}
824
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700825static int is_booting(void)
826{
827 return access("/dev/.booting", F_OK) == 0;
828}
829
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700830static void process_firmware_event(struct uevent *uevent)
831{
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700832 char *root, *loading, *data;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700833 int l, loading_fd, data_fd, fw_fd;
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700834 size_t i;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700835 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700836
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700837 LOG(INFO) << "firmware: loading '" << uevent->firmware << "' for '" << uevent->path << "'";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700838
839 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
840 if (l == -1)
841 return;
842
843 l = asprintf(&loading, "%sloading", root);
844 if (l == -1)
845 goto root_free_out;
846
847 l = asprintf(&data, "%sdata", root);
848 if (l == -1)
849 goto loading_free_out;
850
Nick Kralevich45a884f2015-02-02 14:37:22 -0800851 loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700852 if(loading_fd < 0)
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700853 goto data_free_out;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700854
Nick Kralevich45a884f2015-02-02 14:37:22 -0800855 data_fd = open(data, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700856 if(data_fd < 0)
857 goto loading_close_out;
858
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700859try_loading_again:
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700860 for (i = 0; i < arraysize(firmware_dirs); i++) {
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700861 char *file = NULL;
862 l = asprintf(&file, "%s/%s", firmware_dirs[i], uevent->firmware);
863 if (l == -1)
864 goto data_free_out;
865 fw_fd = open(file, O_RDONLY|O_CLOEXEC);
866 free(file);
867 if (fw_fd >= 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700868 if (!load_firmware(fw_fd, loading_fd, data_fd)) {
869 LOG(INFO) << "firmware: copy success { '" << root << "', '" << uevent->firmware << "' }";
870 } else {
871 LOG(ERROR) << "firmware: copy failure { '" << root << "', '" << uevent->firmware << "' }";
872 }
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700873 break;
Benoit Goby609d8822010-11-09 18:10:24 -0800874 }
Brian Swetland02863b92010-09-19 03:36:39 -0700875 }
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700876 if (fw_fd < 0) {
877 if (booting) {
878 /* If we're not fully booted, we may be missing
879 * filesystems needed for firmware, wait and retry.
880 */
881 usleep(100000);
882 booting = is_booting();
883 goto try_loading_again;
884 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700885 PLOG(ERROR) << "firmware: could not open '" << uevent->firmware << "'";
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700886 write(loading_fd, "-1", 2);
887 goto data_close_out;
888 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700889
890 close(fw_fd);
891data_close_out:
892 close(data_fd);
893loading_close_out:
894 close(loading_fd);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700895data_free_out:
896 free(data);
897loading_free_out:
898 free(loading);
899root_free_out:
900 free(root);
901}
902
903static void handle_firmware_event(struct uevent *uevent)
904{
905 pid_t pid;
906
907 if(strcmp(uevent->subsystem, "firmware"))
908 return;
909
910 if(strcmp(uevent->action, "add"))
911 return;
912
913 /* we fork, to avoid making large memory allocations in init proper */
914 pid = fork();
915 if (!pid) {
916 process_firmware_event(uevent);
Kenny Root17baff42014-08-20 16:16:44 -0700917 _exit(EXIT_SUCCESS);
918 } else if (pid < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700919 PLOG(ERROR) << "could not fork to process firmware event";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700920 }
921}
922
Ruchi Kandoic6037202014-06-23 11:22:09 -0700923#define UEVENT_MSG_LEN 2048
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800924
925static inline void handle_device_fd_with(void (handle_uevent)(struct uevent*))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700926{
Vernon Tang3f582e92011-04-25 13:08:17 +1000927 char msg[UEVENT_MSG_LEN+2];
928 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700929 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700930 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700931 continue;
932
933 msg[n] = '\0';
934 msg[n+1] = '\0';
935
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700936 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700937 parse_event(msg, &uevent);
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800938 handle_uevent(&uevent);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700939 }
940}
941
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800942void handle_device_fd()
943{
944 handle_device_fd_with(
945 [](struct uevent *uevent) {
946 if (selinux_status_updated() > 0) {
947 struct selabel_handle *sehandle2;
948 sehandle2 = selinux_android_file_context_handle();
949 if (sehandle2) {
950 selabel_close(sehandle);
951 sehandle = sehandle2;
952 }
953 }
954
955 handle_device_event(uevent);
956 handle_firmware_event(uevent);
957 });
958}
959
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700960/* Coldboot walks parts of the /sys tree and pokes the uevent files
961** to cause the kernel to regenerate device add events that happened
962** before init's device manager was started
963**
964** We drain any pending events from the netlink socket every time
965** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800966** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700967*/
968
Colin Cross0dd7ca62010-04-13 19:25:51 -0700969static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700970{
971 struct dirent *de;
972 int dfd, fd;
973
974 dfd = dirfd(d);
975
976 fd = openat(dfd, "uevent", O_WRONLY);
977 if(fd >= 0) {
978 write(fd, "add\n", 4);
979 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700980 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700981 }
982
983 while((de = readdir(d))) {
984 DIR *d2;
985
986 if(de->d_type != DT_DIR || de->d_name[0] == '.')
987 continue;
988
989 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
990 if(fd < 0)
991 continue;
992
993 d2 = fdopendir(fd);
994 if(d2 == 0)
995 close(fd);
996 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700997 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700998 closedir(d2);
999 }
1000 }
1001}
1002
Colin Cross0dd7ca62010-04-13 19:25:51 -07001003static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001004{
James Hawkins588a2ca2016-02-18 14:52:46 -08001005 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001006 if(d) {
James Hawkins588a2ca2016-02-18 14:52:46 -08001007 do_coldboot(d.get());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001008 }
1009}
1010
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +08001011static void early_uevent_handler(struct uevent *uevent, const char *base, bool is_block)
1012{
1013 const char *name;
1014 char devpath[DEVPATH_LEN];
1015
1016 if (is_block && strncmp(uevent->subsystem, "block", 5))
1017 return;
1018
1019 name = parse_device_name(uevent, MAX_DEV_NAME);
1020 if (!name) {
1021 LOG(ERROR) << "Failed to parse dev name from uevent: " << uevent->action
1022 << " " << uevent->partition_name << " " << uevent->partition_num
1023 << " " << uevent->major << ":" << uevent->minor;
1024 return;
1025 }
1026
1027 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
1028 make_dir(base, 0755);
1029
1030 dev_t dev = makedev(uevent->major, uevent->minor);
1031 mode_t mode = 0600 | (is_block ? S_IFBLK : S_IFCHR);
1032 mknod(devpath, mode, dev);
1033}
1034
1035void early_create_dev(const std::string& syspath, early_device_type dev_type)
1036{
1037 android::base::unique_fd dfd(open(syspath.c_str(), O_RDONLY));
1038 if (dfd < 0) {
1039 LOG(ERROR) << "Failed to open " << syspath;
1040 return;
1041 }
1042
1043 android::base::unique_fd fd(openat(dfd, "uevent", O_WRONLY));
1044 if (fd < 0) {
1045 LOG(ERROR) << "Failed to open " << syspath << "/uevent";
1046 return;
1047 }
1048
1049 fcntl(device_fd, F_SETFL, O_NONBLOCK);
1050
1051 write(fd, "add\n", 4);
1052 handle_device_fd_with(dev_type == EARLY_BLOCK_DEV ?
1053 [](struct uevent *uevent) {
1054 early_uevent_handler(uevent, "/dev/block/", true);
1055 } :
1056 [](struct uevent *uevent) {
1057 early_uevent_handler(uevent, "/dev/", false);
1058 });
1059}
1060
1061int early_device_socket_open() {
1062 device_fd = uevent_open_socket(256*1024, true);
1063 return device_fd < 0;
1064}
1065
1066void early_device_socket_close() {
1067 close(device_fd);
1068}
1069
Elliott Hughes74738362015-03-28 10:51:23 -07001070void device_init() {
Nick Kralevich4d870952015-06-12 22:03:50 -07001071 sehandle = selinux_android_file_context_handle();
1072 selinux_status_open(true);
Kenny Rootb5982bf2012-10-16 23:07:05 -07001073
Andrew Boied562ca72012-12-04 15:47:20 -08001074 /* is 256K enough? udev uses 16MB! */
1075 device_fd = uevent_open_socket(256*1024, true);
Elliott Hughes56a06562015-03-28 11:23:32 -07001076 if (device_fd == -1) {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001077 return;
Elliott Hughes56a06562015-03-28 11:23:32 -07001078 }
Colin Cross0dd7ca62010-04-13 19:25:51 -07001079 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001080
Elliott Hughes56a06562015-03-28 11:23:32 -07001081 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -07001082 LOG(VERBOSE) << "Skipping coldboot, already done!";
Elliott Hughes56a06562015-03-28 11:23:32 -07001083 return;
Colin Crossf83d0b92010-04-21 12:04:20 -07001084 }
Elliott Hughes56a06562015-03-28 11:23:32 -07001085
1086 Timer t;
1087 coldboot("/sys/class");
1088 coldboot("/sys/block");
1089 coldboot("/sys/devices");
1090 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
Elliott Hughesf86b5a62016-06-24 15:12:21 -07001091 LOG(INFO) << "Coldboot took " << t.duration() << "s.";
Colin Cross0dd7ca62010-04-13 19:25:51 -07001092}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001093
Colin Cross0dd7ca62010-04-13 19:25:51 -07001094int get_device_fd()
1095{
1096 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001097}