37
37
#error "Do not include init.h in files used by ueventd or watchdogd; it will expose init's globals"
38
38
#endif
39
39
40
+ using android::base::Basename;
41
+ using android::base::Dirname;
42
+ using android::base::Readlink;
43
+ using android::base::Realpath;
44
+ using android::base::StartsWith;
45
+ using android::base::StringPrintf;
46
+
47
+ namespace android {
48
+ namespace init {
49
+
40
50
/* Given a path that may start with a PCI device, populate the supplied buffer
41
51
* with the PCI domain/bus number and the peripheral ID and return 0.
42
52
* If it doesn't start with a PCI device, or there is some error, return -1 */
43
53
static bool FindPciDevicePrefix (const std::string& path, std::string* result) {
44
54
result->clear ();
45
55
46
- if (!android::base:: StartsWith (path, " /devices/pci" )) return false ;
56
+ if (!StartsWith (path, " /devices/pci" )) return false ;
47
57
48
58
/* Beginning of the prefix is the initial "pci" after "/devices/" */
49
59
std::string::size_type start = 9 ;
@@ -74,7 +84,7 @@ static bool FindPciDevicePrefix(const std::string& path, std::string* result) {
74
84
static bool FindVbdDevicePrefix (const std::string& path, std::string* result) {
75
85
result->clear ();
76
86
77
- if (!android::base:: StartsWith (path, " /devices/vbd-" )) return false ;
87
+ if (!StartsWith (path, " /devices/vbd-" )) return false ;
78
88
79
89
/* Beginning of the prefix is the initial "vbd-" after "/devices/" */
80
90
std::string::size_type start = 13 ;
@@ -116,14 +126,14 @@ Permissions::Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t
116
126
}
117
127
118
128
bool Permissions::Match (const std::string& path) const {
119
- if (prefix_) return android::base:: StartsWith (path, name_.c_str ());
129
+ if (prefix_) return StartsWith (path, name_.c_str ());
120
130
if (wildcard_) return fnmatch (name_.c_str (), path.c_str (), FNM_PATHNAME) == 0 ;
121
131
return path == name_;
122
132
}
123
133
124
134
bool SysfsPermissions::MatchWithSubsystem (const std::string& path,
125
135
const std::string& subsystem) const {
126
- std::string path_basename = android::base:: Basename (path);
136
+ std::string path_basename = Basename (path);
127
137
if (name ().find (subsystem) != std::string::npos) {
128
138
if (Match (" /sys/class/" + subsystem + " /" + path_basename)) return true ;
129
139
if (Match (" /sys/bus/" + subsystem + " /devices/" + path_basename)) return true ;
@@ -156,11 +166,11 @@ bool DeviceHandler::FindPlatformDevice(std::string path, std::string* platform_d
156
166
// Uevents don't contain the mount point, so we need to add it here.
157
167
path.insert (0 , sysfs_mount_point_);
158
168
159
- std::string directory = android::base:: Dirname (path);
169
+ std::string directory = Dirname (path);
160
170
161
171
while (directory != " /" && directory != " ." ) {
162
172
std::string subsystem_link_path;
163
- if (android::base:: Realpath (directory + " /subsystem" , &subsystem_link_path) &&
173
+ if (Realpath (directory + " /subsystem" , &subsystem_link_path) &&
164
174
subsystem_link_path == sysfs_mount_point_ + " /bus/platform" ) {
165
175
// We need to remove the mount point that we added above before returning.
166
176
directory.erase (0 , sysfs_mount_point_.size ());
@@ -172,7 +182,7 @@ bool DeviceHandler::FindPlatformDevice(std::string path, std::string* platform_d
172
182
if (last_slash == std::string::npos) return false ;
173
183
174
184
path.erase (last_slash);
175
- directory = android::base:: Dirname (path);
185
+ directory = Dirname (path);
176
186
}
177
187
178
188
return false ;
@@ -276,7 +286,7 @@ std::vector<std::string> DeviceHandler::GetCharacterDeviceSymlinks(const Uevent&
276
286
// skip path to the parent driver
277
287
std::string path = uevent.path .substr (parent_device.length ());
278
288
279
- if (!android::base:: StartsWith (path, " /usb" )) return {};
289
+ if (!StartsWith (path, " /usb" )) return {};
280
290
281
291
// skip root hub name and device. use device interface
282
292
// skip 3 slashes, including the first / by starting the search at the 1st character, not 0th.
@@ -334,9 +344,9 @@ std::vector<std::string> DeviceHandler::GetBlockDeviceSymlinks(const Uevent& uev
334
344
static const std::string devices_platform_prefix = " /devices/platform/" ;
335
345
static const std::string devices_prefix = " /devices/" ;
336
346
337
- if (android::base:: StartsWith (device, devices_platform_prefix.c_str ())) {
347
+ if (StartsWith (device, devices_platform_prefix.c_str ())) {
338
348
device = device.substr (devices_platform_prefix.length ());
339
- } else if (android::base:: StartsWith (device, devices_prefix.c_str ())) {
349
+ } else if (StartsWith (device, devices_prefix.c_str ())) {
340
350
device = device.substr (devices_prefix.length ());
341
351
}
342
352
@@ -380,8 +390,8 @@ void DeviceHandler::HandleDevice(const std::string& action, const std::string& d
380
390
if (action == " add" ) {
381
391
MakeDevice (devpath, block, major, minor, links);
382
392
for (const auto & link : links) {
383
- if (mkdir_recursive (android::base:: Dirname (link ), 0755 , sehandle_)) {
384
- PLOG (ERROR) << " Failed to create directory " << android::base:: Dirname (link );
393
+ if (mkdir_recursive (Dirname (link ), 0755 , sehandle_)) {
394
+ PLOG (ERROR) << " Failed to create directory " << Dirname (link );
385
395
}
386
396
387
397
if (symlink (devpath.c_str (), link .c_str ()) && errno != EEXIST) {
@@ -393,7 +403,7 @@ void DeviceHandler::HandleDevice(const std::string& action, const std::string& d
393
403
if (action == " remove" ) {
394
404
for (const auto & link : links) {
395
405
std::string link_path;
396
- if (android::base:: Readlink (link , &link_path) && link_path == devpath) {
406
+ if (Readlink (link , &link_path) && link_path == devpath) {
397
407
unlink (link .c_str ());
398
408
}
399
409
}
@@ -408,11 +418,11 @@ void DeviceHandler::HandleBlockDeviceEvent(const Uevent& uevent) const {
408
418
const char * base = " /dev/block/" ;
409
419
make_dir (base, 0755 , sehandle_);
410
420
411
- std::string name = android::base:: Basename (uevent.path );
421
+ std::string name = Basename (uevent.path );
412
422
std::string devpath = base + name;
413
423
414
424
std::vector<std::string> links;
415
- if (android::base:: StartsWith (uevent.path , " /devices" )) {
425
+ if (StartsWith (uevent.path , " /devices" )) {
416
426
links = GetBlockDeviceSymlinks (uevent);
417
427
}
418
428
@@ -425,7 +435,7 @@ void DeviceHandler::HandleGenericDeviceEvent(const Uevent& uevent) const {
425
435
426
436
std::string devpath;
427
437
428
- if (android::base:: StartsWith (uevent.subsystem , " usb" )) {
438
+ if (StartsWith (uevent.subsystem , " usb" )) {
429
439
if (uevent.subsystem == " usb" ) {
430
440
if (!uevent.device_name .empty ()) {
431
441
devpath = " /dev/" + uevent.device_name ;
@@ -435,7 +445,7 @@ void DeviceHandler::HandleGenericDeviceEvent(const Uevent& uevent) const {
435
445
// Minors are broken up into groups of 128, starting at "001"
436
446
int bus_id = uevent.minor / 128 + 1 ;
437
447
int device_id = uevent.minor % 128 + 1 ;
438
- devpath = android::base:: StringPrintf (" /dev/bus/usb/%03d/%03d" , bus_id, device_id);
448
+ devpath = StringPrintf (" /dev/bus/usb/%03d/%03d" , bus_id, device_id);
439
449
}
440
450
} else {
441
451
// ignore other USB events
@@ -446,10 +456,10 @@ void DeviceHandler::HandleGenericDeviceEvent(const Uevent& uevent) const {
446
456
subsystem != subsystems_.cend ()) {
447
457
devpath = subsystem->ParseDevPath (uevent);
448
458
} else {
449
- devpath = " /dev/" + android::base:: Basename (uevent.path );
459
+ devpath = " /dev/" + Basename (uevent.path );
450
460
}
451
461
452
- mkdir_recursive (android::base:: Dirname (devpath), 0755 , sehandle_);
462
+ mkdir_recursive (Dirname (devpath), 0755 , sehandle_);
453
463
454
464
auto links = GetCharacterDeviceSymlinks (uevent);
455
465
@@ -481,3 +491,6 @@ DeviceHandler::DeviceHandler(std::vector<Permissions> dev_permissions,
481
491
DeviceHandler::DeviceHandler ()
482
492
: DeviceHandler(std::vector<Permissions>{}, std::vector<SysfsPermissions>{},
483
493
std::vector<Subsystem>{}, false ) {}
494
+
495
+ } // namespace init
496
+ } // namespace android
0 commit comments