Skip to content

Commit 81f5d3e

Browse files
author
Tom Cherry
committed
init: create android::init:: namespace
With some small fixups along the way Test: Boot bullhead Test: init unit tests Change-Id: I7beaa473cfa9397f845f810557d1631b4a462d6a
1 parent 84c2eeb commit 81f5d3e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+379
-44
lines changed

init/action.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
using android::base::Join;
2626

27+
namespace android {
28+
namespace init {
29+
2730
Command::Command(BuiltinFunction f, const std::vector<std::string>& args, int line)
2831
: func_(f), args_(args), line_(line) {}
2932

@@ -349,3 +352,6 @@ void ActionParser::EndSection() {
349352
action_manager_->AddAction(std::move(action_));
350353
}
351354
}
355+
356+
} // namespace init
357+
} // namespace android

init/action.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
#include "init_parser.h"
2828
#include "keyword_map.h"
2929

30+
namespace android {
31+
namespace init {
32+
3033
class Command {
3134
public:
3235
Command(BuiltinFunction f, const std::vector<std::string>& args, int line);
@@ -126,4 +129,7 @@ class ActionParser : public SectionParser {
126129
std::unique_ptr<Action> action_;
127130
};
128131

132+
} // namespace init
133+
} // namespace android
134+
129135
#endif

init/bootchart.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
using android::base::StringPrintf;
4141
using namespace std::chrono_literals;
4242

43+
namespace android {
44+
namespace init {
45+
4346
static std::thread* g_bootcharting_thread;
4447

4548
static std::mutex g_bootcharting_finished_mutex;
@@ -192,3 +195,6 @@ int do_bootchart(const std::vector<std::string>& args) {
192195
if (args[1] == "start") return do_bootchart_start();
193196
return do_bootchart_stop();
194197
}
198+
199+
} // namespace init
200+
} // namespace android

init/bootchart.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
#include <string>
2121
#include <vector>
2222

23+
namespace android {
24+
namespace init {
25+
2326
int do_bootchart(const std::vector<std::string>& args);
2427

28+
} // namespace init
29+
} // namespace android
30+
2531
#endif /* _BOOTCHART_H */

init/builtins.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ using namespace std::literals::string_literals;
6767

6868
#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
6969

70+
namespace android {
71+
namespace init {
72+
7073
static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s;
7174

7275
static int insmod(const char *filename, const char *options, int flags) {
@@ -924,3 +927,6 @@ const BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
924927
// clang-format on
925928
return builtin_functions;
926929
}
930+
931+
} // namespace init
932+
} // namespace android

init/builtins.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
#include "keyword_map.h"
2626

27+
namespace android {
28+
namespace init {
29+
2730
using BuiltinFunction = std::function<int(const std::vector<std::string>&)>;
2831
class BuiltinFunctionMap : public KeywordMap<BuiltinFunction> {
2932
public:
@@ -33,4 +36,7 @@ class BuiltinFunctionMap : public KeywordMap<BuiltinFunction> {
3336
const Map& map() const override;
3437
};
3538

39+
} // namespace init
40+
} // namespace android
41+
3642
#endif

init/capabilities.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
#define CAP_MAP_ENTRY(cap) { #cap, CAP_##cap }
2727

28+
namespace android {
29+
namespace init {
30+
2831
static const std::map<std::string, int> cap_map = {
2932
CAP_MAP_ENTRY(CHOWN),
3033
CAP_MAP_ENTRY(DAC_OVERRIDE),
@@ -192,3 +195,6 @@ bool SetCapsForExec(const CapSet& to_keep) {
192195
// See http://man7.org/linux/man-pages/man7/capabilities.7.html.
193196
return SetAmbientCaps(to_keep);
194197
}
198+
199+
} // namespace init
200+
} // namespace android

init/capabilities.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@
2020
#include <bitset>
2121
#include <string>
2222

23+
namespace android {
24+
namespace init {
25+
2326
using CapSet = std::bitset<CAP_LAST_CAP + 1>;
2427

2528
int LookupCap(const std::string& cap_name);
2629
bool CapAmbientSupported();
2730
unsigned int GetLastValidCap();
2831
bool SetCapsForExec(const CapSet& to_keep);
2932

33+
} // namespace init
34+
} // namespace android
35+
3036
#endif // _INIT_CAPABILITIES_H

init/descriptors.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#include "init.h"
3232
#include "util.h"
3333

34+
namespace android {
35+
namespace init {
36+
3437
DescriptorInfo::DescriptorInfo(const std::string& name, const std::string& type, uid_t uid,
3538
gid_t gid, int perm, const std::string& context)
3639
: name_(name), type_(type), uid_(uid), gid_(gid), perm_(perm), context_(context) {
@@ -126,3 +129,6 @@ int FileInfo::Create(const std::string&) const {
126129
const std::string FileInfo::key() const {
127130
return ANDROID_FILE_ENV_PREFIX;
128131
}
132+
133+
} // namespace init
134+
} // namespace android

init/descriptors.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
#include <string>
2424

25+
namespace android {
26+
namespace init {
27+
2528
class DescriptorInfo {
2629
public:
2730
DescriptorInfo(const std::string& name, const std::string& type, uid_t uid,
@@ -75,4 +78,7 @@ class FileInfo : public DescriptorInfo {
7578
virtual const std::string key() const override;
7679
};
7780

81+
} // namespace init
82+
} // namespace android
83+
7884
#endif

init/devices.cpp

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,23 @@
3737
#error "Do not include init.h in files used by ueventd or watchdogd; it will expose init's globals"
3838
#endif
3939

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+
4050
/* Given a path that may start with a PCI device, populate the supplied buffer
4151
* with the PCI domain/bus number and the peripheral ID and return 0.
4252
* If it doesn't start with a PCI device, or there is some error, return -1 */
4353
static bool FindPciDevicePrefix(const std::string& path, std::string* result) {
4454
result->clear();
4555

46-
if (!android::base::StartsWith(path, "/devices/pci")) return false;
56+
if (!StartsWith(path, "/devices/pci")) return false;
4757

4858
/* Beginning of the prefix is the initial "pci" after "/devices/" */
4959
std::string::size_type start = 9;
@@ -74,7 +84,7 @@ static bool FindPciDevicePrefix(const std::string& path, std::string* result) {
7484
static bool FindVbdDevicePrefix(const std::string& path, std::string* result) {
7585
result->clear();
7686

77-
if (!android::base::StartsWith(path, "/devices/vbd-")) return false;
87+
if (!StartsWith(path, "/devices/vbd-")) return false;
7888

7989
/* Beginning of the prefix is the initial "vbd-" after "/devices/" */
8090
std::string::size_type start = 13;
@@ -116,14 +126,14 @@ Permissions::Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t
116126
}
117127

118128
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());
120130
if (wildcard_) return fnmatch(name_.c_str(), path.c_str(), FNM_PATHNAME) == 0;
121131
return path == name_;
122132
}
123133

124134
bool SysfsPermissions::MatchWithSubsystem(const std::string& path,
125135
const std::string& subsystem) const {
126-
std::string path_basename = android::base::Basename(path);
136+
std::string path_basename = Basename(path);
127137
if (name().find(subsystem) != std::string::npos) {
128138
if (Match("/sys/class/" + subsystem + "/" + path_basename)) return true;
129139
if (Match("/sys/bus/" + subsystem + "/devices/" + path_basename)) return true;
@@ -156,11 +166,11 @@ bool DeviceHandler::FindPlatformDevice(std::string path, std::string* platform_d
156166
// Uevents don't contain the mount point, so we need to add it here.
157167
path.insert(0, sysfs_mount_point_);
158168

159-
std::string directory = android::base::Dirname(path);
169+
std::string directory = Dirname(path);
160170

161171
while (directory != "/" && directory != ".") {
162172
std::string subsystem_link_path;
163-
if (android::base::Realpath(directory + "/subsystem", &subsystem_link_path) &&
173+
if (Realpath(directory + "/subsystem", &subsystem_link_path) &&
164174
subsystem_link_path == sysfs_mount_point_ + "/bus/platform") {
165175
// We need to remove the mount point that we added above before returning.
166176
directory.erase(0, sysfs_mount_point_.size());
@@ -172,7 +182,7 @@ bool DeviceHandler::FindPlatformDevice(std::string path, std::string* platform_d
172182
if (last_slash == std::string::npos) return false;
173183

174184
path.erase(last_slash);
175-
directory = android::base::Dirname(path);
185+
directory = Dirname(path);
176186
}
177187

178188
return false;
@@ -276,7 +286,7 @@ std::vector<std::string> DeviceHandler::GetCharacterDeviceSymlinks(const Uevent&
276286
// skip path to the parent driver
277287
std::string path = uevent.path.substr(parent_device.length());
278288

279-
if (!android::base::StartsWith(path, "/usb")) return {};
289+
if (!StartsWith(path, "/usb")) return {};
280290

281291
// skip root hub name and device. use device interface
282292
// 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
334344
static const std::string devices_platform_prefix = "/devices/platform/";
335345
static const std::string devices_prefix = "/devices/";
336346

337-
if (android::base::StartsWith(device, devices_platform_prefix.c_str())) {
347+
if (StartsWith(device, devices_platform_prefix.c_str())) {
338348
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())) {
340350
device = device.substr(devices_prefix.length());
341351
}
342352

@@ -380,8 +390,8 @@ void DeviceHandler::HandleDevice(const std::string& action, const std::string& d
380390
if (action == "add") {
381391
MakeDevice(devpath, block, major, minor, links);
382392
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);
385395
}
386396

387397
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
393403
if (action == "remove") {
394404
for (const auto& link : links) {
395405
std::string link_path;
396-
if (android::base::Readlink(link, &link_path) && link_path == devpath) {
406+
if (Readlink(link, &link_path) && link_path == devpath) {
397407
unlink(link.c_str());
398408
}
399409
}
@@ -408,11 +418,11 @@ void DeviceHandler::HandleBlockDeviceEvent(const Uevent& uevent) const {
408418
const char* base = "/dev/block/";
409419
make_dir(base, 0755, sehandle_);
410420

411-
std::string name = android::base::Basename(uevent.path);
421+
std::string name = Basename(uevent.path);
412422
std::string devpath = base + name;
413423

414424
std::vector<std::string> links;
415-
if (android::base::StartsWith(uevent.path, "/devices")) {
425+
if (StartsWith(uevent.path, "/devices")) {
416426
links = GetBlockDeviceSymlinks(uevent);
417427
}
418428

@@ -425,7 +435,7 @@ void DeviceHandler::HandleGenericDeviceEvent(const Uevent& uevent) const {
425435

426436
std::string devpath;
427437

428-
if (android::base::StartsWith(uevent.subsystem, "usb")) {
438+
if (StartsWith(uevent.subsystem, "usb")) {
429439
if (uevent.subsystem == "usb") {
430440
if (!uevent.device_name.empty()) {
431441
devpath = "/dev/" + uevent.device_name;
@@ -435,7 +445,7 @@ void DeviceHandler::HandleGenericDeviceEvent(const Uevent& uevent) const {
435445
// Minors are broken up into groups of 128, starting at "001"
436446
int bus_id = uevent.minor / 128 + 1;
437447
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);
439449
}
440450
} else {
441451
// ignore other USB events
@@ -446,10 +456,10 @@ void DeviceHandler::HandleGenericDeviceEvent(const Uevent& uevent) const {
446456
subsystem != subsystems_.cend()) {
447457
devpath = subsystem->ParseDevPath(uevent);
448458
} else {
449-
devpath = "/dev/" + android::base::Basename(uevent.path);
459+
devpath = "/dev/" + Basename(uevent.path);
450460
}
451461

452-
mkdir_recursive(android::base::Dirname(devpath), 0755, sehandle_);
462+
mkdir_recursive(Dirname(devpath), 0755, sehandle_);
453463

454464
auto links = GetCharacterDeviceSymlinks(uevent);
455465

@@ -481,3 +491,6 @@ DeviceHandler::DeviceHandler(std::vector<Permissions> dev_permissions,
481491
DeviceHandler::DeviceHandler()
482492
: DeviceHandler(std::vector<Permissions>{}, std::vector<SysfsPermissions>{},
483493
std::vector<Subsystem>{}, false) {}
494+
495+
} // namespace init
496+
} // namespace android

init/devices.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
#include "uevent.h"
3131

32+
namespace android {
33+
namespace init {
34+
3235
class Permissions {
3336
public:
3437
Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid);
@@ -133,4 +136,7 @@ class DeviceHandler {
133136
// Exposed for testing
134137
void SanitizePartitionName(std::string* string);
135138

139+
} // namespace init
140+
} // namespace android
141+
136142
#endif

init/devices_test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
using namespace std::string_literals;
2626

27+
namespace android {
28+
namespace init {
29+
2730
class DeviceHandlerTester {
2831
public:
2932
void TestGetSymlinks(const std::string& platform_device, const Uevent& uevent,
@@ -400,3 +403,6 @@ TEST(device_handler, SysfsPermissionsMatchWithSubsystemBus) {
400403
EXPECT_EQ(0U, permissions.uid());
401404
EXPECT_EQ(1001U, permissions.gid());
402405
}
406+
407+
} // namespace init
408+
} // namespace android

0 commit comments

Comments
 (0)