blob: d422ba79206ebc9d86db5a8ce0f85df84babe490 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
Mark Salyzyn154f4602014-02-20 14:59:07 -08002 * Copyright (C) 2007-2014 The Android Open Source Project
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughesf39f7f12016-08-31 14:41:51 -070017#include <dirent.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070018#include <errno.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070019#include <fcntl.h>
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070020#include <fnmatch.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070021#include <libgen.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -080022#include <stddef.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070023#include <stdio.h>
24#include <stdlib.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070025#include <string.h>
Elliott Hughes632e99a2016-11-12 11:44:16 -080026#include <sys/sendfile.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070027#include <sys/socket.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070028#include <sys/stat.h>
29#include <sys/time.h>
30#include <sys/types.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070031#include <sys/un.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070032#include <sys/wait.h>
33#include <unistd.h>
34
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070035#include <linux/netlink.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050036
James Hawkins588a2ca2016-02-18 14:52:46 -080037#include <memory>
38
Stephen Smalleye46f9d52012-01-13 08:48:47 -050039#include <selinux/selinux.h>
40#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040041#include <selinux/android.h>
Stephen Smalleye2eb69d2013-04-16 09:30:30 -040042#include <selinux/avc.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050043
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070044#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070045
Biao Ludc848562016-01-28 16:10:54 +080046#include <android-base/file.h>
Rob Herring6de783a2016-05-06 10:06:59 -050047#include <android-base/stringprintf.h>
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +080048#include <android-base/unique_fd.h>
Dima Zavinda04c522011-09-01 17:09:44 -070049#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100050#include <cutils/uevent.h>
51
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070052#include "devices.h"
Greg Hackmann3312aa82013-11-18 15:24:40 -080053#include "ueventd_parser.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070054#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070055#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070056
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070057#define SYSFS_PREFIX "/sys"
Daniel Rosenbergd1d96022015-03-20 15:10:29 -070058static const char *firmware_dirs[] = { "/etc/firmware",
59 "/vendor/firmware",
60 "/firmware/image" };
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070061
Stephen Smalleye096e362012-06-11 13:37:39 -040062extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050063
Colin Cross0dd7ca62010-04-13 19:25:51 -070064static int device_fd = -1;
65
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070066struct uevent {
67 const char *action;
68 const char *path;
69 const char *subsystem;
70 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070071 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070072 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070073 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070074 int major;
75 int minor;
76};
77
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070078struct perms_ {
79 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070080 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070081 mode_t perm;
82 unsigned int uid;
83 unsigned int gid;
84 unsigned short prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070085 unsigned short wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070086};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070087
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070088struct perm_node {
89 struct perms_ dp;
90 struct listnode plist;
91};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070092
Colin Crossfadb85e2011-03-30 18:32:12 -070093struct platform_node {
94 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080095 char *path;
96 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070097 struct listnode list;
98};
99
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700100static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -0700101static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -0700102static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700103
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700104int add_dev_perms(const char *name, const char *attr,
105 mode_t perm, unsigned int uid, unsigned int gid,
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700106 unsigned short prefix,
107 unsigned short wildcard) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800108 struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700109 if (!node)
110 return -ENOMEM;
111
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700112 node->dp.name = strdup(name);
113 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700114 return -ENOMEM;
115
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700116 if (attr) {
117 node->dp.attr = strdup(attr);
118 if (!node->dp.attr)
119 return -ENOMEM;
120 }
121
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700122 node->dp.perm = perm;
123 node->dp.uid = uid;
124 node->dp.gid = gid;
125 node->dp.prefix = prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700126 node->dp.wildcard = wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700127
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700128 if (attr)
129 list_add_tail(&sys_perms, &node->plist);
130 else
131 list_add_tail(&dev_perms, &node->plist);
132
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700133 return 0;
134}
135
Colin Cross43d537e2014-07-02 13:08:13 -0700136static bool perm_path_matches(const char *path, struct perms_ *dp)
137{
138 if (dp->prefix) {
139 if (strncmp(path, dp->name, strlen(dp->name)) == 0)
140 return true;
141 } else if (dp->wildcard) {
142 if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
143 return true;
144 } else {
145 if (strcmp(path, dp->name) == 0)
146 return true;
147 }
148
149 return false;
150}
151
Rob Herring6de783a2016-05-06 10:06:59 -0500152static bool match_subsystem(perms_* dp, const char* pattern,
153 const char* path, const char* subsystem) {
154 if (!pattern || !subsystem || strstr(dp->name, subsystem) == NULL) {
155 return false;
156 }
Rob Herringe5636a32016-05-06 12:28:48 -0500157
Rob Herring6de783a2016-05-06 10:06:59 -0500158 std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path));
159 return perm_path_matches(subsys_path.c_str(), dp);
160}
Rob Herringe5636a32016-05-06 12:28:48 -0500161
Rob Herring6de783a2016-05-06 10:06:59 -0500162static void fixup_sys_perms(const char* upath, const char* subsystem) {
163 // upaths omit the "/sys" that paths in this list
164 // contain, so we prepend it...
165 std::string path = std::string(SYSFS_PREFIX) + upath;
166
167 listnode* node;
Rob Herringe5636a32016-05-06 12:28:48 -0500168 list_for_each(node, &sys_perms) {
Rob Herring6de783a2016-05-06 10:06:59 -0500169 perms_* dp = &(node_to_item(node, perm_node, plist))->dp;
170 if (match_subsystem(dp, SYSFS_PREFIX "/class/%s/%s", path.c_str(), subsystem)) {
171 ; // matched
172 } else if (match_subsystem(dp, SYSFS_PREFIX "/bus/%s/devices/%s", path.c_str(), subsystem)) {
173 ; // matched
174 } else if (!perm_path_matches(path.c_str(), dp)) {
175 continue;
Rob Herringe5636a32016-05-06 12:28:48 -0500176 }
177
178 std::string attr_file = path + "/" + dp->attr;
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700179 LOG(INFO) << "fixup " << attr_file
180 << " " << dp->uid << " " << dp->gid << " " << std::oct << dp->perm;
Rob Herringe5636a32016-05-06 12:28:48 -0500181 chown(attr_file.c_str(), dp->uid, dp->gid);
182 chmod(attr_file.c_str(), dp->perm);
183 }
184
185 if (access(path.c_str(), F_OK) == 0) {
Dmitry Shmidt7eed4742016-07-28 13:55:39 -0700186 LOG(VERBOSE) << "restorecon_recursive: " << path;
Rob Herringe5636a32016-05-06 12:28:48 -0500187 restorecon_recursive(path.c_str());
188 }
189}
190
Colin Cross43d537e2014-07-02 13:08:13 -0700191static mode_t get_device_perm(const char *path, const char **links,
192 unsigned *uid, unsigned *gid)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700193{
Colin Cross44b65d02010-04-20 14:32:50 -0700194 struct listnode *node;
195 struct perm_node *perm_node;
196 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700197
Colin Cross44b65d02010-04-20 14:32:50 -0700198 /* search the perms list in reverse so that ueventd.$hardware can
199 * override ueventd.rc
200 */
201 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700202 bool match = false;
203
Colin Cross44b65d02010-04-20 14:32:50 -0700204 perm_node = node_to_item(node, struct perm_node, plist);
205 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700206
Colin Cross43d537e2014-07-02 13:08:13 -0700207 if (perm_path_matches(path, dp)) {
208 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700209 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700210 if (links) {
211 int i;
212 for (i = 0; links[i]; i++) {
213 if (perm_path_matches(links[i], dp)) {
214 match = true;
215 break;
216 }
217 }
218 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700219 }
Colin Cross43d537e2014-07-02 13:08:13 -0700220
221 if (match) {
222 *uid = dp->uid;
223 *gid = dp->gid;
224 return dp->perm;
225 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700226 }
Colin Cross44b65d02010-04-20 14:32:50 -0700227 /* Default if nothing found. */
228 *uid = 0;
229 *gid = 0;
230 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700231}
232
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700233static void make_device(const char *path,
Elliott Hughesf682b472015-02-06 12:19:48 -0800234 const char */*upath*/,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400235 int block, int major, int minor,
236 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700237{
238 unsigned uid;
239 unsigned gid;
240 mode_t mode;
241 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500242 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700243
Colin Cross43d537e2014-07-02 13:08:13 -0700244 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700245
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300246 if (selabel_lookup_best_match(sehandle, &secontext, path, links, mode)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700247 PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300248 return;
249 }
Nick Kralevich4d870952015-06-12 22:03:50 -0700250 setfscreatecon(secontext);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700251
Colin Cross17dcc5c2010-09-03 12:25:34 -0700252 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800253 /* Temporarily change egid to avoid race condition setting the gid of the
254 * device node. Unforunately changing the euid would prevent creation of
255 * some device nodes, so the uid has to be set with chown() and is still
256 * racy. Fixing the gid race at least fixed the issue with system_server
257 * opening dynamic input devices under the AID_INPUT gid. */
258 setegid(gid);
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300259 /* If the node already exists update its SELinux label to handle cases when
260 * it was created with the wrong context during coldboot procedure. */
261 if (mknod(path, mode, dev) && (errno == EEXIST)) {
William Roberts397de142016-06-02 09:53:44 -0700262
263 char* fcon = nullptr;
264 int rc = lgetfilecon(path, &fcon);
265 if (rc < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700266 PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
William Roberts397de142016-06-02 09:53:44 -0700267 goto out;
268 }
269
270 bool different = strcmp(fcon, secontext) != 0;
271 freecon(fcon);
272
273 if (different && lsetfilecon(path, secontext)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700274 PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path << "' device";
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300275 }
276 }
William Roberts397de142016-06-02 09:53:44 -0700277
278out:
Nick Pelly6405c692010-01-21 18:13:39 -0800279 chown(path, uid, -1);
280 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700281
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300282 freecon(secontext);
283 setfscreatecon(NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700284}
285
Dima Zavinf395c922013-03-06 16:23:57 -0800286static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700287{
Dima Zavinf395c922013-03-06 16:23:57 -0800288 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700289 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800290 const char *name = path;
291
292 if (!strncmp(path, "/devices/", 9)) {
293 name += 9;
294 if (!strncmp(name, "platform/", 9))
295 name += 9;
296 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700297
Wei Wanga285dac2016-10-04 14:05:39 -0700298 LOG(VERBOSE) << "adding platform device " << name << " (" << path << ")";
Colin Crossfadb85e2011-03-30 18:32:12 -0700299
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800300 bus = (platform_node*) calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800301 bus->path = strdup(path);
302 bus->path_len = path_len;
303 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700304 list_add_tail(&platform_names, &bus->list);
305}
306
307/*
Dima Zavinf395c922013-03-06 16:23:57 -0800308 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700309 * platform device prefix. If it doesn't start with a platform device, return
310 * 0.
311 */
Dima Zavinf395c922013-03-06 16:23:57 -0800312static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700313{
Dima Zavinf395c922013-03-06 16:23:57 -0800314 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700315 struct listnode *node;
316 struct platform_node *bus;
317
318 list_for_each_reverse(node, &platform_names) {
319 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800320 if ((bus->path_len < path_len) &&
321 (path[bus->path_len] == '/') &&
322 !strncmp(path, bus->path, bus->path_len))
323 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700324 }
325
326 return NULL;
327}
328
Dima Zavinf395c922013-03-06 16:23:57 -0800329static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700330{
331 struct listnode *node;
332 struct platform_node *bus;
333
334 list_for_each_reverse(node, &platform_names) {
335 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800336 if (!strcmp(path, bus->path)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700337 LOG(INFO) << "removing platform device " << bus->name;
Dima Zavinf395c922013-03-06 16:23:57 -0800338 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700339 list_remove(node);
340 free(bus);
341 return;
342 }
343 }
344}
345
Andrew Boiea885d042013-09-13 17:41:20 -0700346/* Given a path that may start with a PCI device, populate the supplied buffer
347 * with the PCI domain/bus number and the peripheral ID and return 0.
348 * If it doesn't start with a PCI device, or there is some error, return -1 */
349static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
350{
351 const char *start, *end;
352
353 if (strncmp(path, "/devices/pci", 12))
354 return -1;
355
356 /* Beginning of the prefix is the initial "pci" after "/devices/" */
357 start = path + 9;
358
359 /* End of the prefix is two path '/' later, capturing the domain/bus number
360 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
361 end = strchr(start, '/');
362 if (!end)
363 return -1;
364 end = strchr(end + 1, '/');
365 if (!end)
366 return -1;
367
368 /* Make sure we have enough room for the string plus null terminator */
369 if (end - start + 1 > buf_sz)
370 return -1;
371
372 strncpy(buf, start, end - start);
373 buf[end - start] = '\0';
374 return 0;
375}
376
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700377static void parse_event(const char *msg, struct uevent *uevent)
378{
379 uevent->action = "";
380 uevent->path = "";
381 uevent->subsystem = "";
382 uevent->firmware = "";
383 uevent->major = -1;
384 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700385 uevent->partition_name = NULL;
386 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700387 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700388
389 /* currently ignoring SEQNUM */
390 while(*msg) {
391 if(!strncmp(msg, "ACTION=", 7)) {
392 msg += 7;
393 uevent->action = msg;
394 } else if(!strncmp(msg, "DEVPATH=", 8)) {
395 msg += 8;
396 uevent->path = msg;
397 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
398 msg += 10;
399 uevent->subsystem = msg;
400 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
401 msg += 9;
402 uevent->firmware = msg;
403 } else if(!strncmp(msg, "MAJOR=", 6)) {
404 msg += 6;
405 uevent->major = atoi(msg);
406 } else if(!strncmp(msg, "MINOR=", 6)) {
407 msg += 6;
408 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700409 } else if(!strncmp(msg, "PARTN=", 6)) {
410 msg += 6;
411 uevent->partition_num = atoi(msg);
412 } else if(!strncmp(msg, "PARTNAME=", 9)) {
413 msg += 9;
414 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700415 } else if(!strncmp(msg, "DEVNAME=", 8)) {
416 msg += 8;
417 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700418 }
419
Wei Zhongf97b8872012-03-23 14:15:34 -0700420 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700421 while(*msg++)
422 ;
423 }
424
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800425 if (LOG_UEVENTS) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700426 LOG(INFO) << android::base::StringPrintf("event { '%s', '%s', '%s', '%s', %d, %d }",
427 uevent->action, uevent->path, uevent->subsystem,
428 uevent->firmware, uevent->major, uevent->minor);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800429 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700430}
431
Benoit Gobyd2278632010-08-03 14:36:02 -0700432static char **get_character_device_symlinks(struct uevent *uevent)
433{
434 const char *parent;
Dan Austin60b976d2016-03-28 14:22:12 -0700435 const char *slash;
Benoit Gobyd2278632010-08-03 14:36:02 -0700436 char **links;
437 int link_num = 0;
438 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800439 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700440
Dima Zavinf395c922013-03-06 16:23:57 -0800441 pdev = find_platform_device(uevent->path);
442 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700443 return NULL;
444
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800445 links = (char**) malloc(sizeof(char *) * 2);
Benoit Gobyd2278632010-08-03 14:36:02 -0700446 if (!links)
447 return NULL;
448 memset(links, 0, sizeof(char *) * 2);
449
450 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800451 parent = strchr(uevent->path + pdev->path_len, '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100452 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700453 goto err;
454
455 if (!strncmp(parent, "/usb", 4)) {
456 /* skip root hub name and device. use device interface */
457 while (*++parent && *parent != '/');
458 if (*parent)
459 while (*++parent && *parent != '/');
460 if (!*parent)
461 goto err;
462 slash = strchr(++parent, '/');
463 if (!slash)
464 goto err;
465 width = slash - parent;
466 if (width <= 0)
467 goto err;
468
469 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
470 link_num++;
471 else
472 links[link_num] = NULL;
473 mkdir("/dev/usb", 0755);
474 }
475 else {
476 goto err;
477 }
478
479 return links;
480err:
481 free(links);
482 return NULL;
483}
484
Andrew Boiea885d042013-09-13 17:41:20 -0700485static char **get_block_device_symlinks(struct uevent *uevent)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700486{
Colin Crossfadb85e2011-03-30 18:32:12 -0700487 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800488 struct platform_node *pdev;
Dan Austin60b976d2016-03-28 14:22:12 -0700489 const char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700490 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700491 char buf[256];
492 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700493 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700494 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700495
Dima Zavinf395c922013-03-06 16:23:57 -0800496 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700497 if (pdev) {
498 device = pdev->name;
499 type = "platform";
500 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
501 device = buf;
502 type = "pci";
503 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800504 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700505 }
Dima Zavinf395c922013-03-06 16:23:57 -0800506
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800507 char **links = (char**) malloc(sizeof(char *) * 4);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700508 if (!links)
509 return NULL;
510 memset(links, 0, sizeof(char *) * 4);
511
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700512 LOG(INFO) << "found " << type << " device " << device;
Colin Crossfadb85e2011-03-30 18:32:12 -0700513
Andrew Boiea885d042013-09-13 17:41:20 -0700514 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700515
516 if (uevent->partition_name) {
517 p = strdup(uevent->partition_name);
518 sanitize(p);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700519 if (strcmp(uevent->partition_name, p)) {
520 LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '" << p << "'";
521 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700522 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
523 link_num++;
524 else
525 links[link_num] = NULL;
526 free(p);
527 }
528
529 if (uevent->partition_num >= 0) {
530 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
531 link_num++;
532 else
533 links[link_num] = NULL;
534 }
535
Dima Zavinf395c922013-03-06 16:23:57 -0800536 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700537 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
538 link_num++;
539 else
540 links[link_num] = NULL;
541
542 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700543}
544
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700545static void make_link_init(const char* oldpath, const char* newpath) {
546 const char* slash = strrchr(newpath, '/');
547 if (!slash) return;
548
549 if (mkdir_recursive(dirname(newpath), 0755)) {
550 PLOG(ERROR) << "Failed to create directory " << dirname(newpath);
551 }
552
553 if (symlink(oldpath, newpath) && errno != EEXIST) {
554 PLOG(ERROR) << "Failed to symlink " << oldpath << " to " << newpath;
555 }
556}
557
558static void remove_link(const char* oldpath, const char* newpath) {
559 std::string path;
560 if (android::base::Readlink(newpath, &path) && path == oldpath) unlink(newpath);
561}
562
Colin Crosseb5ba832011-03-30 17:37:17 -0700563static void handle_device(const char *action, const char *devpath,
564 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700565{
Colin Crosseb5ba832011-03-30 17:37:17 -0700566 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400567 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700568 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700569 for (int i = 0; links[i]; i++) {
Chris Fries79f33842013-09-05 13:19:21 -0500570 make_link_init(devpath, links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700571 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700572 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700573 }
574
Colin Crosseb5ba832011-03-30 17:37:17 -0700575 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700576 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700577 for (int i = 0; links[i]; i++) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700578 remove_link(devpath, links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700579 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700580 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700581 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700582 }
583
584 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700585 for (int i = 0; links[i]; i++) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700586 free(links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700587 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700588 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700589 }
590}
591
Colin Crossfadb85e2011-03-30 18:32:12 -0700592static void handle_platform_device_event(struct uevent *uevent)
593{
Dima Zavinf395c922013-03-06 16:23:57 -0800594 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700595
596 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800597 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700598 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800599 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700600}
601
Colin Crosseb5ba832011-03-30 17:37:17 -0700602static const char *parse_device_name(struct uevent *uevent, unsigned int len)
603{
604 const char *name;
605
606 /* if it's not a /dev device, nothing else to do */
607 if((uevent->major < 0) || (uevent->minor < 0))
608 return NULL;
609
610 /* do we have a name? */
611 name = strrchr(uevent->path, '/');
612 if(!name)
613 return NULL;
614 name++;
615
616 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800617 if(strlen(name) > len) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700618 LOG(ERROR) << "DEVPATH=" << name << " exceeds " << len << "-character limit on filename; ignoring event";
Colin Crosseb5ba832011-03-30 17:37:17 -0700619 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800620 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700621
622 return name;
623}
624
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800625#define DEVPATH_LEN 96
626#define MAX_DEV_NAME 64
627
Colin Crosseb5ba832011-03-30 17:37:17 -0700628static void handle_block_device_event(struct uevent *uevent)
629{
630 const char *base = "/dev/block/";
631 const char *name;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800632 char devpath[DEVPATH_LEN];
Colin Crosseb5ba832011-03-30 17:37:17 -0700633 char **links = NULL;
634
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800635 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700636 if (!name)
637 return;
638
639 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500640 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700641
Dima Zavinf395c922013-03-06 16:23:57 -0800642 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700643 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700644
645 handle_device(uevent->action, devpath, uevent->path, 1,
646 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700647}
648
Greg Hackmann3312aa82013-11-18 15:24:40 -0800649static bool assemble_devpath(char *devpath, const char *dirname,
650 const char *devname)
651{
652 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
653 if (s < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700654 PLOG(ERROR) << "failed to assemble device path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800655 return false;
656 } else if (s >= DEVPATH_LEN) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700657 LOG(ERROR) << dirname << "/" << devname
658 << " exceeds " << DEVPATH_LEN << "-character limit on path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800659 return false;
660 }
661 return true;
662}
663
664static void mkdir_recursive_for_devpath(const char *devpath)
665{
666 char dir[DEVPATH_LEN];
667 char *slash;
668
669 strcpy(dir, devpath);
670 slash = strrchr(dir, '/');
671 *slash = '\0';
672 mkdir_recursive(dir, 0755);
673}
674
Colin Crosseb5ba832011-03-30 17:37:17 -0700675static void handle_generic_device_event(struct uevent *uevent)
676{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800677 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700678 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800679 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700680 char **links = NULL;
681
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800682 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700683 if (!name)
684 return;
685
Greg Hackmann3312aa82013-11-18 15:24:40 -0800686 struct ueventd_subsystem *subsystem =
687 ueventd_subsystem_find_by_name(uevent->subsystem);
688
689 if (subsystem) {
690 const char *devname;
691
692 switch (subsystem->devname_src) {
693 case DEVNAME_UEVENT_DEVNAME:
694 devname = uevent->device_name;
695 break;
696
697 case DEVNAME_UEVENT_DEVPATH:
698 devname = name;
699 break;
700
701 default:
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700702 LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800703 return;
704 }
705
706 if (!assemble_devpath(devpath, subsystem->dirname, devname))
707 return;
708 mkdir_recursive_for_devpath(devpath);
709 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700710 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700711 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800712 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800713 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800714 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700715 }
716 else {
717 /* This imitates the file system that would be created
718 * if we were using devfs instead.
719 * Minors are broken up into groups of 128, starting at "001"
720 */
721 int bus_id = uevent->minor / 128 + 1;
722 int device_id = uevent->minor % 128 + 1;
723 /* build directories */
724 make_dir("/dev/bus", 0755);
725 make_dir("/dev/bus/usb", 0755);
726 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
727 make_dir(devpath, 0755);
728 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
729 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700730 } else {
731 /* ignore other USB events */
732 return;
733 }
734 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
735 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500736 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200737 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
738 base = "/dev/dri/";
739 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700740 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
741 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500742 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700743 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
744 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500745 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700746 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
747 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500748 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700749 } else if(!strncmp(uevent->subsystem, "input", 5)) {
750 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500751 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700752 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
753 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500754 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700755 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
756 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500757 make_dir(base, 0755);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700758 } else if(!strncmp(uevent->subsystem, "misc", 4) && !strncmp(name, "log_", 4)) {
759 LOG(INFO) << "kernel logger is deprecated";
Colin Crosseb5ba832011-03-30 17:37:17 -0700760 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500761 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700762 name += 4;
763 } else
764 base = "/dev/";
765 links = get_character_device_symlinks(uevent);
766
767 if (!devpath[0])
768 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
769
770 handle_device(uevent->action, devpath, uevent->path, 0,
771 uevent->major, uevent->minor, links);
772}
773
774static void handle_device_event(struct uevent *uevent)
775{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700776 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Rob Herring6de783a2016-05-06 10:06:59 -0500777 fixup_sys_perms(uevent->path, uevent->subsystem);
Colin Crosseb5ba832011-03-30 17:37:17 -0700778
779 if (!strncmp(uevent->subsystem, "block", 5)) {
780 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700781 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
782 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700783 } else {
784 handle_generic_device_event(uevent);
785 }
786}
787
Elliott Hughes632e99a2016-11-12 11:44:16 -0800788static void load_firmware(uevent* uevent, const std::string& root,
789 int fw_fd, size_t fw_size,
790 int loading_fd, int data_fd) {
791 // Start transfer.
792 android::base::WriteFully(loading_fd, "1", 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700793
Elliott Hughes632e99a2016-11-12 11:44:16 -0800794 // Copy the firmware.
795 int rc = sendfile(data_fd, fw_fd, nullptr, fw_size);
796 if (rc == -1) {
797 PLOG(ERROR) << "firmware: sendfile failed { '" << root << "', '" << uevent->firmware << "' }";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700798 }
799
Elliott Hughes632e99a2016-11-12 11:44:16 -0800800 // Tell the firmware whether to abort or commit.
801 const char* response = (rc != -1) ? "0" : "-1";
802 android::base::WriteFully(loading_fd, response, strlen(response));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700803}
804
Elliott Hughes632e99a2016-11-12 11:44:16 -0800805static int is_booting() {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700806 return access("/dev/.booting", F_OK) == 0;
807}
808
Elliott Hughes632e99a2016-11-12 11:44:16 -0800809static void process_firmware_event(uevent* uevent) {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700810 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700811
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700812 LOG(INFO) << "firmware: loading '" << uevent->firmware << "' for '" << uevent->path << "'";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700813
Elliott Hughes632e99a2016-11-12 11:44:16 -0800814 std::string root = android::base::StringPrintf("/sys%s", uevent->path);
815 std::string loading = root + "/loading";
816 std::string data = root + "/data";
817
818 android::base::unique_fd loading_fd(open(loading.c_str(), O_WRONLY|O_CLOEXEC));
819 if (loading_fd == -1) {
820 PLOG(ERROR) << "couldn't open firmware loading fd for " << uevent->firmware;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700821 return;
Elliott Hughes632e99a2016-11-12 11:44:16 -0800822 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700823
Elliott Hughes632e99a2016-11-12 11:44:16 -0800824 android::base::unique_fd data_fd(open(data.c_str(), O_WRONLY|O_CLOEXEC));
825 if (data_fd == -1) {
826 PLOG(ERROR) << "couldn't open firmware data fd for " << uevent->firmware;
827 return;
828 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700829
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700830try_loading_again:
Elliott Hughes632e99a2016-11-12 11:44:16 -0800831 for (size_t i = 0; i < arraysize(firmware_dirs); i++) {
832 std::string file = android::base::StringPrintf("%s/%s", firmware_dirs[i], uevent->firmware);
833 android::base::unique_fd fw_fd(open(file.c_str(), O_RDONLY|O_CLOEXEC));
834 struct stat sb;
835 if (fw_fd != -1 && fstat(fw_fd, &sb) != -1) {
836 load_firmware(uevent, root, fw_fd, sb.st_size, loading_fd, data_fd);
837 return;
Benoit Goby609d8822010-11-09 18:10:24 -0800838 }
Brian Swetland02863b92010-09-19 03:36:39 -0700839 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700840
Elliott Hughes632e99a2016-11-12 11:44:16 -0800841 if (booting) {
842 // If we're not fully booted, we may be missing
843 // filesystems needed for firmware, wait and retry.
844 usleep(100000);
845 booting = is_booting();
846 goto try_loading_again;
847 }
848
849 LOG(ERROR) << "firmware: could not find firmware for " << uevent->firmware;
850
851 // Write "-1" as our response to the kernel's firmware request, since we have nothing for it.
852 write(loading_fd, "-1", 2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700853}
854
Elliott Hughes632e99a2016-11-12 11:44:16 -0800855static void handle_firmware_event(uevent* uevent) {
856 if (strcmp(uevent->subsystem, "firmware")) return;
857 if (strcmp(uevent->action, "add")) return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700858
Elliott Hughes632e99a2016-11-12 11:44:16 -0800859 // Loading the firmware in a child means we can do that in parallel...
860 // (We ignore SIGCHLD rather than wait for our children.)
861 pid_t pid = fork();
862 if (pid == 0) {
863 Timer t;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700864 process_firmware_event(uevent);
Elliott Hughes632e99a2016-11-12 11:44:16 -0800865 LOG(INFO) << "loading " << uevent->path << " took " << t.duration() << "s";
Kenny Root17baff42014-08-20 16:16:44 -0700866 _exit(EXIT_SUCCESS);
Elliott Hughes632e99a2016-11-12 11:44:16 -0800867 } else if (pid == -1) {
868 PLOG(ERROR) << "could not fork to process firmware event for " << uevent->firmware;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700869 }
870}
871
Ruchi Kandoic6037202014-06-23 11:22:09 -0700872#define UEVENT_MSG_LEN 2048
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800873
874static inline void handle_device_fd_with(void (handle_uevent)(struct uevent*))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700875{
Vernon Tang3f582e92011-04-25 13:08:17 +1000876 char msg[UEVENT_MSG_LEN+2];
877 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700878 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700879 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700880 continue;
881
882 msg[n] = '\0';
883 msg[n+1] = '\0';
884
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700885 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700886 parse_event(msg, &uevent);
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800887 handle_uevent(&uevent);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700888 }
889}
890
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800891void handle_device_fd()
892{
893 handle_device_fd_with(
894 [](struct uevent *uevent) {
895 if (selinux_status_updated() > 0) {
896 struct selabel_handle *sehandle2;
897 sehandle2 = selinux_android_file_context_handle();
898 if (sehandle2) {
899 selabel_close(sehandle);
900 sehandle = sehandle2;
901 }
902 }
903
904 handle_device_event(uevent);
905 handle_firmware_event(uevent);
906 });
907}
908
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700909/* Coldboot walks parts of the /sys tree and pokes the uevent files
910** to cause the kernel to regenerate device add events that happened
911** before init's device manager was started
912**
913** We drain any pending events from the netlink socket every time
914** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800915** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700916*/
917
Colin Cross0dd7ca62010-04-13 19:25:51 -0700918static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700919{
920 struct dirent *de;
921 int dfd, fd;
922
923 dfd = dirfd(d);
924
925 fd = openat(dfd, "uevent", O_WRONLY);
926 if(fd >= 0) {
927 write(fd, "add\n", 4);
928 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700929 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700930 }
931
932 while((de = readdir(d))) {
933 DIR *d2;
934
935 if(de->d_type != DT_DIR || de->d_name[0] == '.')
936 continue;
937
938 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
939 if(fd < 0)
940 continue;
941
942 d2 = fdopendir(fd);
943 if(d2 == 0)
944 close(fd);
945 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700946 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700947 closedir(d2);
948 }
949 }
950}
951
Colin Cross0dd7ca62010-04-13 19:25:51 -0700952static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700953{
James Hawkins588a2ca2016-02-18 14:52:46 -0800954 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700955 if(d) {
James Hawkins588a2ca2016-02-18 14:52:46 -0800956 do_coldboot(d.get());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700957 }
958}
959
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800960static void early_uevent_handler(struct uevent *uevent, const char *base, bool is_block)
961{
962 const char *name;
963 char devpath[DEVPATH_LEN];
964
965 if (is_block && strncmp(uevent->subsystem, "block", 5))
966 return;
967
968 name = parse_device_name(uevent, MAX_DEV_NAME);
969 if (!name) {
970 LOG(ERROR) << "Failed to parse dev name from uevent: " << uevent->action
971 << " " << uevent->partition_name << " " << uevent->partition_num
972 << " " << uevent->major << ":" << uevent->minor;
973 return;
974 }
975
976 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
977 make_dir(base, 0755);
978
979 dev_t dev = makedev(uevent->major, uevent->minor);
980 mode_t mode = 0600 | (is_block ? S_IFBLK : S_IFCHR);
981 mknod(devpath, mode, dev);
982}
983
984void early_create_dev(const std::string& syspath, early_device_type dev_type)
985{
986 android::base::unique_fd dfd(open(syspath.c_str(), O_RDONLY));
987 if (dfd < 0) {
988 LOG(ERROR) << "Failed to open " << syspath;
989 return;
990 }
991
992 android::base::unique_fd fd(openat(dfd, "uevent", O_WRONLY));
993 if (fd < 0) {
994 LOG(ERROR) << "Failed to open " << syspath << "/uevent";
995 return;
996 }
997
998 fcntl(device_fd, F_SETFL, O_NONBLOCK);
999
1000 write(fd, "add\n", 4);
1001 handle_device_fd_with(dev_type == EARLY_BLOCK_DEV ?
1002 [](struct uevent *uevent) {
1003 early_uevent_handler(uevent, "/dev/block/", true);
1004 } :
1005 [](struct uevent *uevent) {
1006 early_uevent_handler(uevent, "/dev/", false);
1007 });
1008}
1009
1010int early_device_socket_open() {
1011 device_fd = uevent_open_socket(256*1024, true);
1012 return device_fd < 0;
1013}
1014
1015void early_device_socket_close() {
1016 close(device_fd);
1017}
1018
Elliott Hughes74738362015-03-28 10:51:23 -07001019void device_init() {
Nick Kralevich4d870952015-06-12 22:03:50 -07001020 sehandle = selinux_android_file_context_handle();
1021 selinux_status_open(true);
Kenny Rootb5982bf2012-10-16 23:07:05 -07001022
Andrew Boied562ca72012-12-04 15:47:20 -08001023 /* is 256K enough? udev uses 16MB! */
1024 device_fd = uevent_open_socket(256*1024, true);
Elliott Hughes56a06562015-03-28 11:23:32 -07001025 if (device_fd == -1) {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001026 return;
Elliott Hughes56a06562015-03-28 11:23:32 -07001027 }
Colin Cross0dd7ca62010-04-13 19:25:51 -07001028 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001029
Elliott Hughes56a06562015-03-28 11:23:32 -07001030 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -07001031 LOG(VERBOSE) << "Skipping coldboot, already done!";
Elliott Hughes56a06562015-03-28 11:23:32 -07001032 return;
Colin Crossf83d0b92010-04-21 12:04:20 -07001033 }
Elliott Hughes56a06562015-03-28 11:23:32 -07001034
1035 Timer t;
1036 coldboot("/sys/class");
1037 coldboot("/sys/block");
1038 coldboot("/sys/devices");
1039 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
Elliott Hughesf86b5a62016-06-24 15:12:21 -07001040 LOG(INFO) << "Coldboot took " << t.duration() << "s.";
Colin Cross0dd7ca62010-04-13 19:25:51 -07001041}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001042
Elliott Hughes632e99a2016-11-12 11:44:16 -08001043int get_device_fd() {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001044 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001045}