blob: 830b74cb23ba9bd10aa169245a83eee4041faac3 [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
17#include <errno.h>
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070018#include <fnmatch.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -080019#include <stddef.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070020#include <stdio.h>
21#include <stdlib.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24
25#include <fcntl.h>
26#include <dirent.h>
27#include <unistd.h>
28#include <string.h>
29
30#include <sys/socket.h>
31#include <sys/un.h>
32#include <linux/netlink.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050033
James Hawkins588a2ca2016-02-18 14:52:46 -080034#include <memory>
35
Stephen Smalleye46f9d52012-01-13 08:48:47 -050036#include <selinux/selinux.h>
37#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040038#include <selinux/android.h>
Stephen Smalleye2eb69d2013-04-16 09:30:30 -040039#include <selinux/avc.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050040
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070041#include <private/android_filesystem_config.h>
42#include <sys/time.h>
Colin Cross982a8152010-06-03 12:21:01 -070043#include <sys/wait.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
Colin Crosseb5ba832011-03-30 17:37:17 -0700544static void handle_device(const char *action, const char *devpath,
545 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700546{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700547 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700548
Colin Crosseb5ba832011-03-30 17:37:17 -0700549 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400550 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700551 if (links) {
552 for (i = 0; links[i]; i++)
Chris Fries79f33842013-09-05 13:19:21 -0500553 make_link_init(devpath, links[i]);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700554 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700555 }
556
Colin Crosseb5ba832011-03-30 17:37:17 -0700557 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700558 if (links) {
559 for (i = 0; links[i]; i++)
560 remove_link(devpath, links[i]);
561 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700562 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700563 }
564
565 if (links) {
566 for (i = 0; links[i]; i++)
567 free(links[i]);
568 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700569 }
570}
571
Colin Crossfadb85e2011-03-30 18:32:12 -0700572static void handle_platform_device_event(struct uevent *uevent)
573{
Dima Zavinf395c922013-03-06 16:23:57 -0800574 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700575
576 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800577 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700578 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800579 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700580}
581
Colin Crosseb5ba832011-03-30 17:37:17 -0700582static const char *parse_device_name(struct uevent *uevent, unsigned int len)
583{
584 const char *name;
585
586 /* if it's not a /dev device, nothing else to do */
587 if((uevent->major < 0) || (uevent->minor < 0))
588 return NULL;
589
590 /* do we have a name? */
591 name = strrchr(uevent->path, '/');
592 if(!name)
593 return NULL;
594 name++;
595
596 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800597 if(strlen(name) > len) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700598 LOG(ERROR) << "DEVPATH=" << name << " exceeds " << len << "-character limit on filename; ignoring event";
Colin Crosseb5ba832011-03-30 17:37:17 -0700599 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800600 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700601
602 return name;
603}
604
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800605#define DEVPATH_LEN 96
606#define MAX_DEV_NAME 64
607
Colin Crosseb5ba832011-03-30 17:37:17 -0700608static void handle_block_device_event(struct uevent *uevent)
609{
610 const char *base = "/dev/block/";
611 const char *name;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800612 char devpath[DEVPATH_LEN];
Colin Crosseb5ba832011-03-30 17:37:17 -0700613 char **links = NULL;
614
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800615 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700616 if (!name)
617 return;
618
619 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500620 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700621
Dima Zavinf395c922013-03-06 16:23:57 -0800622 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700623 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700624
625 handle_device(uevent->action, devpath, uevent->path, 1,
626 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700627}
628
Greg Hackmann3312aa82013-11-18 15:24:40 -0800629static bool assemble_devpath(char *devpath, const char *dirname,
630 const char *devname)
631{
632 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
633 if (s < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700634 PLOG(ERROR) << "failed to assemble device path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800635 return false;
636 } else if (s >= DEVPATH_LEN) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700637 LOG(ERROR) << dirname << "/" << devname
638 << " exceeds " << DEVPATH_LEN << "-character limit on path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800639 return false;
640 }
641 return true;
642}
643
644static void mkdir_recursive_for_devpath(const char *devpath)
645{
646 char dir[DEVPATH_LEN];
647 char *slash;
648
649 strcpy(dir, devpath);
650 slash = strrchr(dir, '/');
651 *slash = '\0';
652 mkdir_recursive(dir, 0755);
653}
654
Colin Crosseb5ba832011-03-30 17:37:17 -0700655static void handle_generic_device_event(struct uevent *uevent)
656{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800657 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700658 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800659 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700660 char **links = NULL;
661
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800662 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700663 if (!name)
664 return;
665
Greg Hackmann3312aa82013-11-18 15:24:40 -0800666 struct ueventd_subsystem *subsystem =
667 ueventd_subsystem_find_by_name(uevent->subsystem);
668
669 if (subsystem) {
670 const char *devname;
671
672 switch (subsystem->devname_src) {
673 case DEVNAME_UEVENT_DEVNAME:
674 devname = uevent->device_name;
675 break;
676
677 case DEVNAME_UEVENT_DEVPATH:
678 devname = name;
679 break;
680
681 default:
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700682 LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800683 return;
684 }
685
686 if (!assemble_devpath(devpath, subsystem->dirname, devname))
687 return;
688 mkdir_recursive_for_devpath(devpath);
689 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700690 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700691 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800692 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800693 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800694 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700695 }
696 else {
697 /* This imitates the file system that would be created
698 * if we were using devfs instead.
699 * Minors are broken up into groups of 128, starting at "001"
700 */
701 int bus_id = uevent->minor / 128 + 1;
702 int device_id = uevent->minor % 128 + 1;
703 /* build directories */
704 make_dir("/dev/bus", 0755);
705 make_dir("/dev/bus/usb", 0755);
706 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
707 make_dir(devpath, 0755);
708 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
709 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700710 } else {
711 /* ignore other USB events */
712 return;
713 }
714 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
715 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500716 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200717 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
718 base = "/dev/dri/";
719 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700720 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
721 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500722 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700723 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
724 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500725 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700726 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
727 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500728 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700729 } else if(!strncmp(uevent->subsystem, "input", 5)) {
730 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500731 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700732 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
733 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500734 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700735 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
736 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500737 make_dir(base, 0755);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700738 } else if(!strncmp(uevent->subsystem, "misc", 4) && !strncmp(name, "log_", 4)) {
739 LOG(INFO) << "kernel logger is deprecated";
Colin Crosseb5ba832011-03-30 17:37:17 -0700740 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500741 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700742 name += 4;
743 } else
744 base = "/dev/";
745 links = get_character_device_symlinks(uevent);
746
747 if (!devpath[0])
748 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
749
750 handle_device(uevent->action, devpath, uevent->path, 0,
751 uevent->major, uevent->minor, links);
752}
753
754static void handle_device_event(struct uevent *uevent)
755{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700756 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Rob Herring6de783a2016-05-06 10:06:59 -0500757 fixup_sys_perms(uevent->path, uevent->subsystem);
Colin Crosseb5ba832011-03-30 17:37:17 -0700758
759 if (!strncmp(uevent->subsystem, "block", 5)) {
760 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700761 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
762 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700763 } else {
764 handle_generic_device_event(uevent);
765 }
766}
767
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700768static int load_firmware(int fw_fd, int loading_fd, int data_fd)
769{
770 struct stat st;
771 long len_to_copy;
772 int ret = 0;
773
774 if(fstat(fw_fd, &st) < 0)
775 return -1;
776 len_to_copy = st.st_size;
777
778 write(loading_fd, "1", 1); /* start transfer */
779
780 while (len_to_copy > 0) {
781 char buf[PAGE_SIZE];
782 ssize_t nr;
783
784 nr = read(fw_fd, buf, sizeof(buf));
785 if(!nr)
786 break;
787 if(nr < 0) {
788 ret = -1;
789 break;
790 }
Biao Ludc848562016-01-28 16:10:54 +0800791 if (!android::base::WriteFully(data_fd, buf, nr)) {
792 ret = -1;
793 break;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700794 }
Biao Ludc848562016-01-28 16:10:54 +0800795 len_to_copy -= nr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700796 }
797
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700798 if(!ret)
799 write(loading_fd, "0", 1); /* successful end of transfer */
800 else
801 write(loading_fd, "-1", 2); /* abort transfer */
802
803 return ret;
804}
805
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700806static int is_booting(void)
807{
808 return access("/dev/.booting", F_OK) == 0;
809}
810
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700811static void process_firmware_event(struct uevent *uevent)
812{
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700813 char *root, *loading, *data;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700814 int l, loading_fd, data_fd, fw_fd;
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700815 size_t i;
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
820 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
821 if (l == -1)
822 return;
823
824 l = asprintf(&loading, "%sloading", root);
825 if (l == -1)
826 goto root_free_out;
827
828 l = asprintf(&data, "%sdata", root);
829 if (l == -1)
830 goto loading_free_out;
831
Nick Kralevich45a884f2015-02-02 14:37:22 -0800832 loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700833 if(loading_fd < 0)
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700834 goto data_free_out;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700835
Nick Kralevich45a884f2015-02-02 14:37:22 -0800836 data_fd = open(data, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700837 if(data_fd < 0)
838 goto loading_close_out;
839
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700840try_loading_again:
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700841 for (i = 0; i < arraysize(firmware_dirs); i++) {
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700842 char *file = NULL;
843 l = asprintf(&file, "%s/%s", firmware_dirs[i], uevent->firmware);
844 if (l == -1)
845 goto data_free_out;
846 fw_fd = open(file, O_RDONLY|O_CLOEXEC);
847 free(file);
848 if (fw_fd >= 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700849 if (!load_firmware(fw_fd, loading_fd, data_fd)) {
850 LOG(INFO) << "firmware: copy success { '" << root << "', '" << uevent->firmware << "' }";
851 } else {
852 LOG(ERROR) << "firmware: copy failure { '" << root << "', '" << uevent->firmware << "' }";
853 }
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700854 break;
Benoit Goby609d8822010-11-09 18:10:24 -0800855 }
Brian Swetland02863b92010-09-19 03:36:39 -0700856 }
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700857 if (fw_fd < 0) {
858 if (booting) {
859 /* If we're not fully booted, we may be missing
860 * filesystems needed for firmware, wait and retry.
861 */
862 usleep(100000);
863 booting = is_booting();
864 goto try_loading_again;
865 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700866 PLOG(ERROR) << "firmware: could not open '" << uevent->firmware << "'";
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700867 write(loading_fd, "-1", 2);
868 goto data_close_out;
869 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700870
871 close(fw_fd);
872data_close_out:
873 close(data_fd);
874loading_close_out:
875 close(loading_fd);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700876data_free_out:
877 free(data);
878loading_free_out:
879 free(loading);
880root_free_out:
881 free(root);
882}
883
884static void handle_firmware_event(struct uevent *uevent)
885{
886 pid_t pid;
887
888 if(strcmp(uevent->subsystem, "firmware"))
889 return;
890
891 if(strcmp(uevent->action, "add"))
892 return;
893
894 /* we fork, to avoid making large memory allocations in init proper */
895 pid = fork();
896 if (!pid) {
897 process_firmware_event(uevent);
Kenny Root17baff42014-08-20 16:16:44 -0700898 _exit(EXIT_SUCCESS);
899 } else if (pid < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700900 PLOG(ERROR) << "could not fork to process firmware event";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700901 }
902}
903
Ruchi Kandoic6037202014-06-23 11:22:09 -0700904#define UEVENT_MSG_LEN 2048
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800905
906static inline void handle_device_fd_with(void (handle_uevent)(struct uevent*))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700907{
Vernon Tang3f582e92011-04-25 13:08:17 +1000908 char msg[UEVENT_MSG_LEN+2];
909 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700910 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700911 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700912 continue;
913
914 msg[n] = '\0';
915 msg[n+1] = '\0';
916
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700917 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700918 parse_event(msg, &uevent);
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800919 handle_uevent(&uevent);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700920 }
921}
922
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800923void handle_device_fd()
924{
925 handle_device_fd_with(
926 [](struct uevent *uevent) {
927 if (selinux_status_updated() > 0) {
928 struct selabel_handle *sehandle2;
929 sehandle2 = selinux_android_file_context_handle();
930 if (sehandle2) {
931 selabel_close(sehandle);
932 sehandle = sehandle2;
933 }
934 }
935
936 handle_device_event(uevent);
937 handle_firmware_event(uevent);
938 });
939}
940
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700941/* Coldboot walks parts of the /sys tree and pokes the uevent files
942** to cause the kernel to regenerate device add events that happened
943** before init's device manager was started
944**
945** We drain any pending events from the netlink socket every time
946** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800947** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700948*/
949
Colin Cross0dd7ca62010-04-13 19:25:51 -0700950static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700951{
952 struct dirent *de;
953 int dfd, fd;
954
955 dfd = dirfd(d);
956
957 fd = openat(dfd, "uevent", O_WRONLY);
958 if(fd >= 0) {
959 write(fd, "add\n", 4);
960 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700961 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700962 }
963
964 while((de = readdir(d))) {
965 DIR *d2;
966
967 if(de->d_type != DT_DIR || de->d_name[0] == '.')
968 continue;
969
970 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
971 if(fd < 0)
972 continue;
973
974 d2 = fdopendir(fd);
975 if(d2 == 0)
976 close(fd);
977 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700978 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700979 closedir(d2);
980 }
981 }
982}
983
Colin Cross0dd7ca62010-04-13 19:25:51 -0700984static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700985{
James Hawkins588a2ca2016-02-18 14:52:46 -0800986 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700987 if(d) {
James Hawkins588a2ca2016-02-18 14:52:46 -0800988 do_coldboot(d.get());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700989 }
990}
991
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800992static void early_uevent_handler(struct uevent *uevent, const char *base, bool is_block)
993{
994 const char *name;
995 char devpath[DEVPATH_LEN];
996
997 if (is_block && strncmp(uevent->subsystem, "block", 5))
998 return;
999
1000 name = parse_device_name(uevent, MAX_DEV_NAME);
1001 if (!name) {
1002 LOG(ERROR) << "Failed to parse dev name from uevent: " << uevent->action
1003 << " " << uevent->partition_name << " " << uevent->partition_num
1004 << " " << uevent->major << ":" << uevent->minor;
1005 return;
1006 }
1007
1008 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
1009 make_dir(base, 0755);
1010
1011 dev_t dev = makedev(uevent->major, uevent->minor);
1012 mode_t mode = 0600 | (is_block ? S_IFBLK : S_IFCHR);
1013 mknod(devpath, mode, dev);
1014}
1015
1016void early_create_dev(const std::string& syspath, early_device_type dev_type)
1017{
1018 android::base::unique_fd dfd(open(syspath.c_str(), O_RDONLY));
1019 if (dfd < 0) {
1020 LOG(ERROR) << "Failed to open " << syspath;
1021 return;
1022 }
1023
1024 android::base::unique_fd fd(openat(dfd, "uevent", O_WRONLY));
1025 if (fd < 0) {
1026 LOG(ERROR) << "Failed to open " << syspath << "/uevent";
1027 return;
1028 }
1029
1030 fcntl(device_fd, F_SETFL, O_NONBLOCK);
1031
1032 write(fd, "add\n", 4);
1033 handle_device_fd_with(dev_type == EARLY_BLOCK_DEV ?
1034 [](struct uevent *uevent) {
1035 early_uevent_handler(uevent, "/dev/block/", true);
1036 } :
1037 [](struct uevent *uevent) {
1038 early_uevent_handler(uevent, "/dev/", false);
1039 });
1040}
1041
1042int early_device_socket_open() {
1043 device_fd = uevent_open_socket(256*1024, true);
1044 return device_fd < 0;
1045}
1046
1047void early_device_socket_close() {
1048 close(device_fd);
1049}
1050
Elliott Hughes74738362015-03-28 10:51:23 -07001051void device_init() {
Nick Kralevich4d870952015-06-12 22:03:50 -07001052 sehandle = selinux_android_file_context_handle();
1053 selinux_status_open(true);
Kenny Rootb5982bf2012-10-16 23:07:05 -07001054
Andrew Boied562ca72012-12-04 15:47:20 -08001055 /* is 256K enough? udev uses 16MB! */
1056 device_fd = uevent_open_socket(256*1024, true);
Elliott Hughes56a06562015-03-28 11:23:32 -07001057 if (device_fd == -1) {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001058 return;
Elliott Hughes56a06562015-03-28 11:23:32 -07001059 }
Colin Cross0dd7ca62010-04-13 19:25:51 -07001060 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001061
Elliott Hughes56a06562015-03-28 11:23:32 -07001062 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -07001063 LOG(VERBOSE) << "Skipping coldboot, already done!";
Elliott Hughes56a06562015-03-28 11:23:32 -07001064 return;
Colin Crossf83d0b92010-04-21 12:04:20 -07001065 }
Elliott Hughes56a06562015-03-28 11:23:32 -07001066
1067 Timer t;
1068 coldboot("/sys/class");
1069 coldboot("/sys/block");
1070 coldboot("/sys/devices");
1071 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
Elliott Hughesf86b5a62016-06-24 15:12:21 -07001072 LOG(INFO) << "Coldboot took " << t.duration() << "s.";
Colin Cross0dd7ca62010-04-13 19:25:51 -07001073}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001074
Colin Cross0dd7ca62010-04-13 19:25:51 -07001075int get_device_fd()
1076{
1077 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001078}