blob: 5e12f25d7cae827095470d268d94dc1a412a2a73 [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>
Dima Zavinda04c522011-09-01 17:09:44 -070047#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100048#include <cutils/uevent.h>
49
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070050#include "devices.h"
Greg Hackmann3312aa82013-11-18 15:24:40 -080051#include "ueventd_parser.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070052#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070053#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070055#define SYSFS_PREFIX "/sys"
Daniel Rosenbergd1d96022015-03-20 15:10:29 -070056static const char *firmware_dirs[] = { "/etc/firmware",
57 "/vendor/firmware",
58 "/firmware/image" };
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070059
Stephen Smalleye096e362012-06-11 13:37:39 -040060extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050061
Colin Cross0dd7ca62010-04-13 19:25:51 -070062static int device_fd = -1;
63
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064struct uevent {
65 const char *action;
66 const char *path;
67 const char *subsystem;
68 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070069 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070070 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070071 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072 int major;
73 int minor;
74};
75
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076struct perms_ {
77 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070078 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070079 mode_t perm;
80 unsigned int uid;
81 unsigned int gid;
82 unsigned short prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070083 unsigned short wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070084};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070085
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070086struct perm_node {
87 struct perms_ dp;
88 struct listnode plist;
89};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070090
Colin Crossfadb85e2011-03-30 18:32:12 -070091struct platform_node {
92 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080093 char *path;
94 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070095 struct listnode list;
96};
97
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070098static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070099static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -0700100static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700101
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700102int add_dev_perms(const char *name, const char *attr,
103 mode_t perm, unsigned int uid, unsigned int gid,
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700104 unsigned short prefix,
105 unsigned short wildcard) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800106 struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700107 if (!node)
108 return -ENOMEM;
109
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700110 node->dp.name = strdup(name);
111 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700112 return -ENOMEM;
113
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700114 if (attr) {
115 node->dp.attr = strdup(attr);
116 if (!node->dp.attr)
117 return -ENOMEM;
118 }
119
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700120 node->dp.perm = perm;
121 node->dp.uid = uid;
122 node->dp.gid = gid;
123 node->dp.prefix = prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700124 node->dp.wildcard = wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700125
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700126 if (attr)
127 list_add_tail(&sys_perms, &node->plist);
128 else
129 list_add_tail(&dev_perms, &node->plist);
130
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700131 return 0;
132}
133
Colin Cross43d537e2014-07-02 13:08:13 -0700134static bool perm_path_matches(const char *path, struct perms_ *dp)
135{
136 if (dp->prefix) {
137 if (strncmp(path, dp->name, strlen(dp->name)) == 0)
138 return true;
139 } else if (dp->wildcard) {
140 if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
141 return true;
142 } else {
143 if (strcmp(path, dp->name) == 0)
144 return true;
145 }
146
147 return false;
148}
149
Rob Herring6de783a2016-05-06 10:06:59 -0500150static bool match_subsystem(perms_* dp, const char* pattern,
151 const char* path, const char* subsystem) {
152 if (!pattern || !subsystem || strstr(dp->name, subsystem) == NULL) {
153 return false;
154 }
Rob Herringe5636a32016-05-06 12:28:48 -0500155
Rob Herring6de783a2016-05-06 10:06:59 -0500156 std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path));
157 return perm_path_matches(subsys_path.c_str(), dp);
158}
Rob Herringe5636a32016-05-06 12:28:48 -0500159
Rob Herring6de783a2016-05-06 10:06:59 -0500160static void fixup_sys_perms(const char* upath, const char* subsystem) {
161 // upaths omit the "/sys" that paths in this list
162 // contain, so we prepend it...
163 std::string path = std::string(SYSFS_PREFIX) + upath;
164
165 listnode* node;
Rob Herringe5636a32016-05-06 12:28:48 -0500166 list_for_each(node, &sys_perms) {
Rob Herring6de783a2016-05-06 10:06:59 -0500167 perms_* dp = &(node_to_item(node, perm_node, plist))->dp;
168 if (match_subsystem(dp, SYSFS_PREFIX "/class/%s/%s", path.c_str(), subsystem)) {
169 ; // matched
170 } else if (match_subsystem(dp, SYSFS_PREFIX "/bus/%s/devices/%s", path.c_str(), subsystem)) {
171 ; // matched
172 } else if (!perm_path_matches(path.c_str(), dp)) {
173 continue;
Rob Herringe5636a32016-05-06 12:28:48 -0500174 }
175
176 std::string attr_file = path + "/" + dp->attr;
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700177 LOG(INFO) << "fixup " << attr_file
178 << " " << dp->uid << " " << dp->gid << " " << std::oct << dp->perm;
Rob Herringe5636a32016-05-06 12:28:48 -0500179 chown(attr_file.c_str(), dp->uid, dp->gid);
180 chmod(attr_file.c_str(), dp->perm);
181 }
182
183 if (access(path.c_str(), F_OK) == 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700184 LOG(INFO) << "restorecon_recursive: " << path;
Rob Herringe5636a32016-05-06 12:28:48 -0500185 restorecon_recursive(path.c_str());
186 }
187}
188
Colin Cross43d537e2014-07-02 13:08:13 -0700189static mode_t get_device_perm(const char *path, const char **links,
190 unsigned *uid, unsigned *gid)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700191{
Colin Cross44b65d02010-04-20 14:32:50 -0700192 struct listnode *node;
193 struct perm_node *perm_node;
194 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700195
Colin Cross44b65d02010-04-20 14:32:50 -0700196 /* search the perms list in reverse so that ueventd.$hardware can
197 * override ueventd.rc
198 */
199 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700200 bool match = false;
201
Colin Cross44b65d02010-04-20 14:32:50 -0700202 perm_node = node_to_item(node, struct perm_node, plist);
203 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700204
Colin Cross43d537e2014-07-02 13:08:13 -0700205 if (perm_path_matches(path, dp)) {
206 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700207 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700208 if (links) {
209 int i;
210 for (i = 0; links[i]; i++) {
211 if (perm_path_matches(links[i], dp)) {
212 match = true;
213 break;
214 }
215 }
216 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700217 }
Colin Cross43d537e2014-07-02 13:08:13 -0700218
219 if (match) {
220 *uid = dp->uid;
221 *gid = dp->gid;
222 return dp->perm;
223 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700224 }
Colin Cross44b65d02010-04-20 14:32:50 -0700225 /* Default if nothing found. */
226 *uid = 0;
227 *gid = 0;
228 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700229}
230
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700231static void make_device(const char *path,
Elliott Hughesf682b472015-02-06 12:19:48 -0800232 const char */*upath*/,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400233 int block, int major, int minor,
234 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700235{
236 unsigned uid;
237 unsigned gid;
238 mode_t mode;
239 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500240 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700241
Colin Cross43d537e2014-07-02 13:08:13 -0700242 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700243
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300244 if (selabel_lookup_best_match(sehandle, &secontext, path, links, mode)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700245 PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300246 return;
247 }
Nick Kralevich4d870952015-06-12 22:03:50 -0700248 setfscreatecon(secontext);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700249
Colin Cross17dcc5c2010-09-03 12:25:34 -0700250 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800251 /* Temporarily change egid to avoid race condition setting the gid of the
252 * device node. Unforunately changing the euid would prevent creation of
253 * some device nodes, so the uid has to be set with chown() and is still
254 * racy. Fixing the gid race at least fixed the issue with system_server
255 * opening dynamic input devices under the AID_INPUT gid. */
256 setegid(gid);
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300257 /* If the node already exists update its SELinux label to handle cases when
258 * it was created with the wrong context during coldboot procedure. */
259 if (mknod(path, mode, dev) && (errno == EEXIST)) {
William Roberts397de142016-06-02 09:53:44 -0700260
261 char* fcon = nullptr;
262 int rc = lgetfilecon(path, &fcon);
263 if (rc < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700264 PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
William Roberts397de142016-06-02 09:53:44 -0700265 goto out;
266 }
267
268 bool different = strcmp(fcon, secontext) != 0;
269 freecon(fcon);
270
271 if (different && lsetfilecon(path, secontext)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700272 PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path << "' device";
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300273 }
274 }
William Roberts397de142016-06-02 09:53:44 -0700275
276out:
Nick Pelly6405c692010-01-21 18:13:39 -0800277 chown(path, uid, -1);
278 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700279
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300280 freecon(secontext);
281 setfscreatecon(NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700282}
283
Dima Zavinf395c922013-03-06 16:23:57 -0800284static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700285{
Dima Zavinf395c922013-03-06 16:23:57 -0800286 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700287 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800288 const char *name = path;
289
290 if (!strncmp(path, "/devices/", 9)) {
291 name += 9;
292 if (!strncmp(name, "platform/", 9))
293 name += 9;
294 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700295
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700296 LOG(INFO) << "adding platform device " << name << " (" << path << ")";
Colin Crossfadb85e2011-03-30 18:32:12 -0700297
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800298 bus = (platform_node*) calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800299 bus->path = strdup(path);
300 bus->path_len = path_len;
301 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700302 list_add_tail(&platform_names, &bus->list);
303}
304
305/*
Dima Zavinf395c922013-03-06 16:23:57 -0800306 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700307 * platform device prefix. If it doesn't start with a platform device, return
308 * 0.
309 */
Dima Zavinf395c922013-03-06 16:23:57 -0800310static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700311{
Dima Zavinf395c922013-03-06 16:23:57 -0800312 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700313 struct listnode *node;
314 struct platform_node *bus;
315
316 list_for_each_reverse(node, &platform_names) {
317 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800318 if ((bus->path_len < path_len) &&
319 (path[bus->path_len] == '/') &&
320 !strncmp(path, bus->path, bus->path_len))
321 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700322 }
323
324 return NULL;
325}
326
Dima Zavinf395c922013-03-06 16:23:57 -0800327static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700328{
329 struct listnode *node;
330 struct platform_node *bus;
331
332 list_for_each_reverse(node, &platform_names) {
333 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800334 if (!strcmp(path, bus->path)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700335 LOG(INFO) << "removing platform device " << bus->name;
Dima Zavinf395c922013-03-06 16:23:57 -0800336 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700337 list_remove(node);
338 free(bus);
339 return;
340 }
341 }
342}
343
Andrew Boiea885d042013-09-13 17:41:20 -0700344/* Given a path that may start with a PCI device, populate the supplied buffer
345 * with the PCI domain/bus number and the peripheral ID and return 0.
346 * If it doesn't start with a PCI device, or there is some error, return -1 */
347static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
348{
349 const char *start, *end;
350
351 if (strncmp(path, "/devices/pci", 12))
352 return -1;
353
354 /* Beginning of the prefix is the initial "pci" after "/devices/" */
355 start = path + 9;
356
357 /* End of the prefix is two path '/' later, capturing the domain/bus number
358 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
359 end = strchr(start, '/');
360 if (!end)
361 return -1;
362 end = strchr(end + 1, '/');
363 if (!end)
364 return -1;
365
366 /* Make sure we have enough room for the string plus null terminator */
367 if (end - start + 1 > buf_sz)
368 return -1;
369
370 strncpy(buf, start, end - start);
371 buf[end - start] = '\0';
372 return 0;
373}
374
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700375static void parse_event(const char *msg, struct uevent *uevent)
376{
377 uevent->action = "";
378 uevent->path = "";
379 uevent->subsystem = "";
380 uevent->firmware = "";
381 uevent->major = -1;
382 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700383 uevent->partition_name = NULL;
384 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700385 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700386
387 /* currently ignoring SEQNUM */
388 while(*msg) {
389 if(!strncmp(msg, "ACTION=", 7)) {
390 msg += 7;
391 uevent->action = msg;
392 } else if(!strncmp(msg, "DEVPATH=", 8)) {
393 msg += 8;
394 uevent->path = msg;
395 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
396 msg += 10;
397 uevent->subsystem = msg;
398 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
399 msg += 9;
400 uevent->firmware = msg;
401 } else if(!strncmp(msg, "MAJOR=", 6)) {
402 msg += 6;
403 uevent->major = atoi(msg);
404 } else if(!strncmp(msg, "MINOR=", 6)) {
405 msg += 6;
406 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700407 } else if(!strncmp(msg, "PARTN=", 6)) {
408 msg += 6;
409 uevent->partition_num = atoi(msg);
410 } else if(!strncmp(msg, "PARTNAME=", 9)) {
411 msg += 9;
412 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700413 } else if(!strncmp(msg, "DEVNAME=", 8)) {
414 msg += 8;
415 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700416 }
417
Wei Zhongf97b8872012-03-23 14:15:34 -0700418 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700419 while(*msg++)
420 ;
421 }
422
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800423 if (LOG_UEVENTS) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700424 LOG(INFO) << android::base::StringPrintf("event { '%s', '%s', '%s', '%s', %d, %d }",
425 uevent->action, uevent->path, uevent->subsystem,
426 uevent->firmware, uevent->major, uevent->minor);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800427 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700428}
429
Benoit Gobyd2278632010-08-03 14:36:02 -0700430static char **get_character_device_symlinks(struct uevent *uevent)
431{
432 const char *parent;
Dan Austin60b976d2016-03-28 14:22:12 -0700433 const char *slash;
Benoit Gobyd2278632010-08-03 14:36:02 -0700434 char **links;
435 int link_num = 0;
436 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800437 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700438
Dima Zavinf395c922013-03-06 16:23:57 -0800439 pdev = find_platform_device(uevent->path);
440 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700441 return NULL;
442
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800443 links = (char**) malloc(sizeof(char *) * 2);
Benoit Gobyd2278632010-08-03 14:36:02 -0700444 if (!links)
445 return NULL;
446 memset(links, 0, sizeof(char *) * 2);
447
448 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800449 parent = strchr(uevent->path + pdev->path_len, '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100450 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700451 goto err;
452
453 if (!strncmp(parent, "/usb", 4)) {
454 /* skip root hub name and device. use device interface */
455 while (*++parent && *parent != '/');
456 if (*parent)
457 while (*++parent && *parent != '/');
458 if (!*parent)
459 goto err;
460 slash = strchr(++parent, '/');
461 if (!slash)
462 goto err;
463 width = slash - parent;
464 if (width <= 0)
465 goto err;
466
467 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
468 link_num++;
469 else
470 links[link_num] = NULL;
471 mkdir("/dev/usb", 0755);
472 }
473 else {
474 goto err;
475 }
476
477 return links;
478err:
479 free(links);
480 return NULL;
481}
482
Andrew Boiea885d042013-09-13 17:41:20 -0700483static char **get_block_device_symlinks(struct uevent *uevent)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700484{
Colin Crossfadb85e2011-03-30 18:32:12 -0700485 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800486 struct platform_node *pdev;
Dan Austin60b976d2016-03-28 14:22:12 -0700487 const char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700488 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700489 char buf[256];
490 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700491 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700492 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700493
Dima Zavinf395c922013-03-06 16:23:57 -0800494 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700495 if (pdev) {
496 device = pdev->name;
497 type = "platform";
498 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
499 device = buf;
500 type = "pci";
501 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800502 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700503 }
Dima Zavinf395c922013-03-06 16:23:57 -0800504
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800505 char **links = (char**) malloc(sizeof(char *) * 4);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700506 if (!links)
507 return NULL;
508 memset(links, 0, sizeof(char *) * 4);
509
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700510 LOG(INFO) << "found " << type << " device " << device;
Colin Crossfadb85e2011-03-30 18:32:12 -0700511
Andrew Boiea885d042013-09-13 17:41:20 -0700512 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700513
514 if (uevent->partition_name) {
515 p = strdup(uevent->partition_name);
516 sanitize(p);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700517 if (strcmp(uevent->partition_name, p)) {
518 LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '" << p << "'";
519 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700520 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
521 link_num++;
522 else
523 links[link_num] = NULL;
524 free(p);
525 }
526
527 if (uevent->partition_num >= 0) {
528 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
529 link_num++;
530 else
531 links[link_num] = NULL;
532 }
533
Dima Zavinf395c922013-03-06 16:23:57 -0800534 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700535 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
536 link_num++;
537 else
538 links[link_num] = NULL;
539
540 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700541}
542
Colin Crosseb5ba832011-03-30 17:37:17 -0700543static void handle_device(const char *action, const char *devpath,
544 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700545{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700546 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700547
Colin Crosseb5ba832011-03-30 17:37:17 -0700548 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400549 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700550 if (links) {
551 for (i = 0; links[i]; i++)
Chris Fries79f33842013-09-05 13:19:21 -0500552 make_link_init(devpath, links[i]);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700553 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700554 }
555
Colin Crosseb5ba832011-03-30 17:37:17 -0700556 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700557 if (links) {
558 for (i = 0; links[i]; i++)
559 remove_link(devpath, links[i]);
560 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700561 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700562 }
563
564 if (links) {
565 for (i = 0; links[i]; i++)
566 free(links[i]);
567 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700568 }
569}
570
Colin Crossfadb85e2011-03-30 18:32:12 -0700571static void handle_platform_device_event(struct uevent *uevent)
572{
Dima Zavinf395c922013-03-06 16:23:57 -0800573 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700574
575 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800576 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700577 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800578 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700579}
580
Colin Crosseb5ba832011-03-30 17:37:17 -0700581static const char *parse_device_name(struct uevent *uevent, unsigned int len)
582{
583 const char *name;
584
585 /* if it's not a /dev device, nothing else to do */
586 if((uevent->major < 0) || (uevent->minor < 0))
587 return NULL;
588
589 /* do we have a name? */
590 name = strrchr(uevent->path, '/');
591 if(!name)
592 return NULL;
593 name++;
594
595 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800596 if(strlen(name) > len) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700597 LOG(ERROR) << "DEVPATH=" << name << " exceeds " << len << "-character limit on filename; ignoring event";
Colin Crosseb5ba832011-03-30 17:37:17 -0700598 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800599 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700600
601 return name;
602}
603
604static void handle_block_device_event(struct uevent *uevent)
605{
606 const char *base = "/dev/block/";
607 const char *name;
608 char devpath[96];
609 char **links = NULL;
610
611 name = parse_device_name(uevent, 64);
612 if (!name)
613 return;
614
615 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500616 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700617
Dima Zavinf395c922013-03-06 16:23:57 -0800618 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700619 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700620
621 handle_device(uevent->action, devpath, uevent->path, 1,
622 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700623}
624
Greg Hackmann3312aa82013-11-18 15:24:40 -0800625#define DEVPATH_LEN 96
626
627static bool assemble_devpath(char *devpath, const char *dirname,
628 const char *devname)
629{
630 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
631 if (s < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700632 PLOG(ERROR) << "failed to assemble device path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800633 return false;
634 } else if (s >= DEVPATH_LEN) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700635 LOG(ERROR) << dirname << "/" << devname
636 << " exceeds " << DEVPATH_LEN << "-character limit on path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800637 return false;
638 }
639 return true;
640}
641
642static void mkdir_recursive_for_devpath(const char *devpath)
643{
644 char dir[DEVPATH_LEN];
645 char *slash;
646
647 strcpy(dir, devpath);
648 slash = strrchr(dir, '/');
649 *slash = '\0';
650 mkdir_recursive(dir, 0755);
651}
652
Colin Crosseb5ba832011-03-30 17:37:17 -0700653static void handle_generic_device_event(struct uevent *uevent)
654{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800655 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700656 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800657 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700658 char **links = NULL;
659
660 name = parse_device_name(uevent, 64);
661 if (!name)
662 return;
663
Greg Hackmann3312aa82013-11-18 15:24:40 -0800664 struct ueventd_subsystem *subsystem =
665 ueventd_subsystem_find_by_name(uevent->subsystem);
666
667 if (subsystem) {
668 const char *devname;
669
670 switch (subsystem->devname_src) {
671 case DEVNAME_UEVENT_DEVNAME:
672 devname = uevent->device_name;
673 break;
674
675 case DEVNAME_UEVENT_DEVPATH:
676 devname = name;
677 break;
678
679 default:
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700680 LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800681 return;
682 }
683
684 if (!assemble_devpath(devpath, subsystem->dirname, devname))
685 return;
686 mkdir_recursive_for_devpath(devpath);
687 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700688 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700689 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800690 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800691 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800692 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700693 }
694 else {
695 /* This imitates the file system that would be created
696 * if we were using devfs instead.
697 * Minors are broken up into groups of 128, starting at "001"
698 */
699 int bus_id = uevent->minor / 128 + 1;
700 int device_id = uevent->minor % 128 + 1;
701 /* build directories */
702 make_dir("/dev/bus", 0755);
703 make_dir("/dev/bus/usb", 0755);
704 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
705 make_dir(devpath, 0755);
706 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
707 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700708 } else {
709 /* ignore other USB events */
710 return;
711 }
712 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
713 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500714 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200715 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
716 base = "/dev/dri/";
717 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700718 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
719 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500720 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700721 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
722 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500723 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700724 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
725 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500726 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700727 } else if(!strncmp(uevent->subsystem, "input", 5)) {
728 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500729 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700730 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
731 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500732 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700733 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
734 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500735 make_dir(base, 0755);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700736 } else if(!strncmp(uevent->subsystem, "misc", 4) && !strncmp(name, "log_", 4)) {
737 LOG(INFO) << "kernel logger is deprecated";
Colin Crosseb5ba832011-03-30 17:37:17 -0700738 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500739 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700740 name += 4;
741 } else
742 base = "/dev/";
743 links = get_character_device_symlinks(uevent);
744
745 if (!devpath[0])
746 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
747
748 handle_device(uevent->action, devpath, uevent->path, 0,
749 uevent->major, uevent->minor, links);
750}
751
752static void handle_device_event(struct uevent *uevent)
753{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700754 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Rob Herring6de783a2016-05-06 10:06:59 -0500755 fixup_sys_perms(uevent->path, uevent->subsystem);
Colin Crosseb5ba832011-03-30 17:37:17 -0700756
757 if (!strncmp(uevent->subsystem, "block", 5)) {
758 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700759 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
760 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700761 } else {
762 handle_generic_device_event(uevent);
763 }
764}
765
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700766static int load_firmware(int fw_fd, int loading_fd, int data_fd)
767{
768 struct stat st;
769 long len_to_copy;
770 int ret = 0;
771
772 if(fstat(fw_fd, &st) < 0)
773 return -1;
774 len_to_copy = st.st_size;
775
776 write(loading_fd, "1", 1); /* start transfer */
777
778 while (len_to_copy > 0) {
779 char buf[PAGE_SIZE];
780 ssize_t nr;
781
782 nr = read(fw_fd, buf, sizeof(buf));
783 if(!nr)
784 break;
785 if(nr < 0) {
786 ret = -1;
787 break;
788 }
Biao Ludc848562016-01-28 16:10:54 +0800789 if (!android::base::WriteFully(data_fd, buf, nr)) {
790 ret = -1;
791 break;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700792 }
Biao Ludc848562016-01-28 16:10:54 +0800793 len_to_copy -= nr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700794 }
795
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700796 if(!ret)
797 write(loading_fd, "0", 1); /* successful end of transfer */
798 else
799 write(loading_fd, "-1", 2); /* abort transfer */
800
801 return ret;
802}
803
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700804static int is_booting(void)
805{
806 return access("/dev/.booting", F_OK) == 0;
807}
808
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700809static void process_firmware_event(struct uevent *uevent)
810{
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700811 char *root, *loading, *data;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700812 int l, loading_fd, data_fd, fw_fd;
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700813 size_t i;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700814 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700815
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700816 LOG(INFO) << "firmware: loading '" << uevent->firmware << "' for '" << uevent->path << "'";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700817
818 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
819 if (l == -1)
820 return;
821
822 l = asprintf(&loading, "%sloading", root);
823 if (l == -1)
824 goto root_free_out;
825
826 l = asprintf(&data, "%sdata", root);
827 if (l == -1)
828 goto loading_free_out;
829
Nick Kralevich45a884f2015-02-02 14:37:22 -0800830 loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700831 if(loading_fd < 0)
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700832 goto data_free_out;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700833
Nick Kralevich45a884f2015-02-02 14:37:22 -0800834 data_fd = open(data, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700835 if(data_fd < 0)
836 goto loading_close_out;
837
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700838try_loading_again:
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700839 for (i = 0; i < arraysize(firmware_dirs); i++) {
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700840 char *file = NULL;
841 l = asprintf(&file, "%s/%s", firmware_dirs[i], uevent->firmware);
842 if (l == -1)
843 goto data_free_out;
844 fw_fd = open(file, O_RDONLY|O_CLOEXEC);
845 free(file);
846 if (fw_fd >= 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700847 if (!load_firmware(fw_fd, loading_fd, data_fd)) {
848 LOG(INFO) << "firmware: copy success { '" << root << "', '" << uevent->firmware << "' }";
849 } else {
850 LOG(ERROR) << "firmware: copy failure { '" << root << "', '" << uevent->firmware << "' }";
851 }
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700852 break;
Benoit Goby609d8822010-11-09 18:10:24 -0800853 }
Brian Swetland02863b92010-09-19 03:36:39 -0700854 }
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700855 if (fw_fd < 0) {
856 if (booting) {
857 /* If we're not fully booted, we may be missing
858 * filesystems needed for firmware, wait and retry.
859 */
860 usleep(100000);
861 booting = is_booting();
862 goto try_loading_again;
863 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700864 PLOG(ERROR) << "firmware: could not open '" << uevent->firmware << "'";
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700865 write(loading_fd, "-1", 2);
866 goto data_close_out;
867 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700868
869 close(fw_fd);
870data_close_out:
871 close(data_fd);
872loading_close_out:
873 close(loading_fd);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700874data_free_out:
875 free(data);
876loading_free_out:
877 free(loading);
878root_free_out:
879 free(root);
880}
881
882static void handle_firmware_event(struct uevent *uevent)
883{
884 pid_t pid;
885
886 if(strcmp(uevent->subsystem, "firmware"))
887 return;
888
889 if(strcmp(uevent->action, "add"))
890 return;
891
892 /* we fork, to avoid making large memory allocations in init proper */
893 pid = fork();
894 if (!pid) {
895 process_firmware_event(uevent);
Kenny Root17baff42014-08-20 16:16:44 -0700896 _exit(EXIT_SUCCESS);
897 } else if (pid < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700898 PLOG(ERROR) << "could not fork to process firmware event";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700899 }
900}
901
Ruchi Kandoic6037202014-06-23 11:22:09 -0700902#define UEVENT_MSG_LEN 2048
Colin Cross0dd7ca62010-04-13 19:25:51 -0700903void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700904{
Vernon Tang3f582e92011-04-25 13:08:17 +1000905 char msg[UEVENT_MSG_LEN+2];
906 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700907 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700908 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700909 continue;
910
911 msg[n] = '\0';
912 msg[n+1] = '\0';
913
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700914 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700915 parse_event(msg, &uevent);
916
Nick Kralevich4d870952015-06-12 22:03:50 -0700917 if (selinux_status_updated() > 0) {
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400918 struct selabel_handle *sehandle2;
919 sehandle2 = selinux_android_file_context_handle();
920 if (sehandle2) {
921 selabel_close(sehandle);
922 sehandle = sehandle2;
923 }
924 }
925
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700926 handle_device_event(&uevent);
927 handle_firmware_event(&uevent);
928 }
929}
930
931/* Coldboot walks parts of the /sys tree and pokes the uevent files
932** to cause the kernel to regenerate device add events that happened
933** before init's device manager was started
934**
935** We drain any pending events from the netlink socket every time
936** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800937** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700938*/
939
Colin Cross0dd7ca62010-04-13 19:25:51 -0700940static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700941{
942 struct dirent *de;
943 int dfd, fd;
944
945 dfd = dirfd(d);
946
947 fd = openat(dfd, "uevent", O_WRONLY);
948 if(fd >= 0) {
949 write(fd, "add\n", 4);
950 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700951 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700952 }
953
954 while((de = readdir(d))) {
955 DIR *d2;
956
957 if(de->d_type != DT_DIR || de->d_name[0] == '.')
958 continue;
959
960 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
961 if(fd < 0)
962 continue;
963
964 d2 = fdopendir(fd);
965 if(d2 == 0)
966 close(fd);
967 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700968 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700969 closedir(d2);
970 }
971 }
972}
973
Colin Cross0dd7ca62010-04-13 19:25:51 -0700974static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700975{
James Hawkins588a2ca2016-02-18 14:52:46 -0800976 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700977 if(d) {
James Hawkins588a2ca2016-02-18 14:52:46 -0800978 do_coldboot(d.get());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700979 }
980}
981
Elliott Hughes74738362015-03-28 10:51:23 -0700982void device_init() {
Nick Kralevich4d870952015-06-12 22:03:50 -0700983 sehandle = selinux_android_file_context_handle();
984 selinux_status_open(true);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700985
Andrew Boied562ca72012-12-04 15:47:20 -0800986 /* is 256K enough? udev uses 16MB! */
987 device_fd = uevent_open_socket(256*1024, true);
Elliott Hughes56a06562015-03-28 11:23:32 -0700988 if (device_fd == -1) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700989 return;
Elliott Hughes56a06562015-03-28 11:23:32 -0700990 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700991 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700992
Elliott Hughes56a06562015-03-28 11:23:32 -0700993 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700994 LOG(VERBOSE) << "Skipping coldboot, already done!";
Elliott Hughes56a06562015-03-28 11:23:32 -0700995 return;
Colin Crossf83d0b92010-04-21 12:04:20 -0700996 }
Elliott Hughes56a06562015-03-28 11:23:32 -0700997
998 Timer t;
999 coldboot("/sys/class");
1000 coldboot("/sys/block");
1001 coldboot("/sys/devices");
1002 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
Elliott Hughesf86b5a62016-06-24 15:12:21 -07001003 LOG(INFO) << "Coldboot took " << t.duration() << "s.";
Colin Cross0dd7ca62010-04-13 19:25:51 -07001004}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001005
Colin Cross0dd7ca62010-04-13 19:25:51 -07001006int get_device_fd()
1007{
1008 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001009}