init: create android::init:: namespace

With some small fixups along the way

Test: Boot bullhead
Test: init unit tests
Change-Id: I7beaa473cfa9397f845f810557d1631b4a462d6a
diff --git a/init/action.cpp b/init/action.cpp
index 206100e..6900391 100644
--- a/init/action.cpp
+++ b/init/action.cpp
@@ -24,6 +24,9 @@
 
 using android::base::Join;
 
+namespace android {
+namespace init {
+
 Command::Command(BuiltinFunction f, const std::vector<std::string>& args, int line)
     : func_(f), args_(args), line_(line) {}
 
@@ -349,3 +352,6 @@
         action_manager_->AddAction(std::move(action_));
     }
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/action.h b/init/action.h
index d006c50..5cb50a7 100644
--- a/init/action.h
+++ b/init/action.h
@@ -27,6 +27,9 @@
 #include "init_parser.h"
 #include "keyword_map.h"
 
+namespace android {
+namespace init {
+
 class Command {
   public:
     Command(BuiltinFunction f, const std::vector<std::string>& args, int line);
@@ -126,4 +129,7 @@
     std::unique_ptr<Action> action_;
 };
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/bootchart.cpp b/init/bootchart.cpp
index 825603a..4727f92 100644
--- a/init/bootchart.cpp
+++ b/init/bootchart.cpp
@@ -40,6 +40,9 @@
 using android::base::StringPrintf;
 using namespace std::chrono_literals;
 
+namespace android {
+namespace init {
+
 static std::thread* g_bootcharting_thread;
 
 static std::mutex g_bootcharting_finished_mutex;
@@ -192,3 +195,6 @@
   if (args[1] == "start") return do_bootchart_start();
   return do_bootchart_stop();
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/bootchart.h b/init/bootchart.h
index 0e3593d..e4f7b59 100644
--- a/init/bootchart.h
+++ b/init/bootchart.h
@@ -20,6 +20,12 @@
 #include <string>
 #include <vector>
 
+namespace android {
+namespace init {
+
 int do_bootchart(const std::vector<std::string>& args);
 
+}  // namespace init
+}  // namespace android
+
 #endif /* _BOOTCHART_H */
diff --git a/init/builtins.cpp b/init/builtins.cpp
index c78bec7..00ffbc3 100644
--- a/init/builtins.cpp
+++ b/init/builtins.cpp
@@ -67,6 +67,9 @@
 
 #define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
 
+namespace android {
+namespace init {
+
 static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s;
 
 static int insmod(const char *filename, const char *options, int flags) {
@@ -924,3 +927,6 @@
     // clang-format on
     return builtin_functions;
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/builtins.h b/init/builtins.h
index e1f0567..b110f61 100644
--- a/init/builtins.h
+++ b/init/builtins.h
@@ -24,6 +24,9 @@
 
 #include "keyword_map.h"
 
+namespace android {
+namespace init {
+
 using BuiltinFunction = std::function<int(const std::vector<std::string>&)>;
 class BuiltinFunctionMap : public KeywordMap<BuiltinFunction> {
   public:
@@ -33,4 +36,7 @@
     const Map& map() const override;
 };
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/capabilities.cpp b/init/capabilities.cpp
index b8a9ec0..53832a4 100644
--- a/init/capabilities.cpp
+++ b/init/capabilities.cpp
@@ -25,6 +25,9 @@
 
 #define CAP_MAP_ENTRY(cap) { #cap, CAP_##cap }
 
+namespace android {
+namespace init {
+
 static const std::map<std::string, int> cap_map = {
     CAP_MAP_ENTRY(CHOWN),
     CAP_MAP_ENTRY(DAC_OVERRIDE),
@@ -192,3 +195,6 @@
     // See http://man7.org/linux/man-pages/man7/capabilities.7.html.
     return SetAmbientCaps(to_keep);
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/capabilities.h b/init/capabilities.h
index abd7fb2..ef507a6 100644
--- a/init/capabilities.h
+++ b/init/capabilities.h
@@ -20,6 +20,9 @@
 #include <bitset>
 #include <string>
 
+namespace android {
+namespace init {
+
 using CapSet = std::bitset<CAP_LAST_CAP + 1>;
 
 int LookupCap(const std::string& cap_name);
@@ -27,4 +30,7 @@
 unsigned int GetLastValidCap();
 bool SetCapsForExec(const CapSet& to_keep);
 
+}  // namespace init
+}  // namespace android
+
 #endif  // _INIT_CAPABILITIES_H
diff --git a/init/descriptors.cpp b/init/descriptors.cpp
index ed65a05..0cb639a 100644
--- a/init/descriptors.cpp
+++ b/init/descriptors.cpp
@@ -31,6 +31,9 @@
 #include "init.h"
 #include "util.h"
 
+namespace android {
+namespace init {
+
 DescriptorInfo::DescriptorInfo(const std::string& name, const std::string& type, uid_t uid,
                                gid_t gid, int perm, const std::string& context)
         : name_(name), type_(type), uid_(uid), gid_(gid), perm_(perm), context_(context) {
@@ -126,3 +129,6 @@
 const std::string FileInfo::key() const {
   return ANDROID_FILE_ENV_PREFIX;
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/descriptors.h b/init/descriptors.h
index ff276fb..3bdddfe 100644
--- a/init/descriptors.h
+++ b/init/descriptors.h
@@ -22,6 +22,9 @@
 
 #include <string>
 
+namespace android {
+namespace init {
+
 class DescriptorInfo {
  public:
   DescriptorInfo(const std::string& name, const std::string& type, uid_t uid,
@@ -75,4 +78,7 @@
   virtual const std::string key() const override;
 };
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/devices.cpp b/init/devices.cpp
index 2943fb7..215d2ea 100644
--- a/init/devices.cpp
+++ b/init/devices.cpp
@@ -37,13 +37,23 @@
 #error "Do not include init.h in files used by ueventd or watchdogd; it will expose init's globals"
 #endif
 
+using android::base::Basename;
+using android::base::Dirname;
+using android::base::Readlink;
+using android::base::Realpath;
+using android::base::StartsWith;
+using android::base::StringPrintf;
+
+namespace android {
+namespace init {
+
 /* Given a path that may start with a PCI device, populate the supplied buffer
  * with the PCI domain/bus number and the peripheral ID and return 0.
  * If it doesn't start with a PCI device, or there is some error, return -1 */
 static bool FindPciDevicePrefix(const std::string& path, std::string* result) {
     result->clear();
 
-    if (!android::base::StartsWith(path, "/devices/pci")) return false;
+    if (!StartsWith(path, "/devices/pci")) return false;
 
     /* Beginning of the prefix is the initial "pci" after "/devices/" */
     std::string::size_type start = 9;
@@ -74,7 +84,7 @@
 static bool FindVbdDevicePrefix(const std::string& path, std::string* result) {
     result->clear();
 
-    if (!android::base::StartsWith(path, "/devices/vbd-")) return false;
+    if (!StartsWith(path, "/devices/vbd-")) return false;
 
     /* Beginning of the prefix is the initial "vbd-" after "/devices/" */
     std::string::size_type start = 13;
@@ -116,14 +126,14 @@
 }
 
 bool Permissions::Match(const std::string& path) const {
-    if (prefix_) return android::base::StartsWith(path, name_.c_str());
+    if (prefix_) return StartsWith(path, name_.c_str());
     if (wildcard_) return fnmatch(name_.c_str(), path.c_str(), FNM_PATHNAME) == 0;
     return path == name_;
 }
 
 bool SysfsPermissions::MatchWithSubsystem(const std::string& path,
                                           const std::string& subsystem) const {
-    std::string path_basename = android::base::Basename(path);
+    std::string path_basename = Basename(path);
     if (name().find(subsystem) != std::string::npos) {
         if (Match("/sys/class/" + subsystem + "/" + path_basename)) return true;
         if (Match("/sys/bus/" + subsystem + "/devices/" + path_basename)) return true;
@@ -156,11 +166,11 @@
     // Uevents don't contain the mount point, so we need to add it here.
     path.insert(0, sysfs_mount_point_);
 
-    std::string directory = android::base::Dirname(path);
+    std::string directory = Dirname(path);
 
     while (directory != "/" && directory != ".") {
         std::string subsystem_link_path;
-        if (android::base::Realpath(directory + "/subsystem", &subsystem_link_path) &&
+        if (Realpath(directory + "/subsystem", &subsystem_link_path) &&
             subsystem_link_path == sysfs_mount_point_ + "/bus/platform") {
             // We need to remove the mount point that we added above before returning.
             directory.erase(0, sysfs_mount_point_.size());
@@ -172,7 +182,7 @@
         if (last_slash == std::string::npos) return false;
 
         path.erase(last_slash);
-        directory = android::base::Dirname(path);
+        directory = Dirname(path);
     }
 
     return false;
@@ -276,7 +286,7 @@
     // skip path to the parent driver
     std::string path = uevent.path.substr(parent_device.length());
 
-    if (!android::base::StartsWith(path, "/usb")) return {};
+    if (!StartsWith(path, "/usb")) return {};
 
     // skip root hub name and device. use device interface
     // skip 3 slashes, including the first / by starting the search at the 1st character, not 0th.
@@ -334,9 +344,9 @@
         static const std::string devices_platform_prefix = "/devices/platform/";
         static const std::string devices_prefix = "/devices/";
 
-        if (android::base::StartsWith(device, devices_platform_prefix.c_str())) {
+        if (StartsWith(device, devices_platform_prefix.c_str())) {
             device = device.substr(devices_platform_prefix.length());
-        } else if (android::base::StartsWith(device, devices_prefix.c_str())) {
+        } else if (StartsWith(device, devices_prefix.c_str())) {
             device = device.substr(devices_prefix.length());
         }
 
@@ -380,8 +390,8 @@
     if (action == "add") {
         MakeDevice(devpath, block, major, minor, links);
         for (const auto& link : links) {
-            if (mkdir_recursive(android::base::Dirname(link), 0755, sehandle_)) {
-                PLOG(ERROR) << "Failed to create directory " << android::base::Dirname(link);
+            if (mkdir_recursive(Dirname(link), 0755, sehandle_)) {
+                PLOG(ERROR) << "Failed to create directory " << Dirname(link);
             }
 
             if (symlink(devpath.c_str(), link.c_str()) && errno != EEXIST) {
@@ -393,7 +403,7 @@
     if (action == "remove") {
         for (const auto& link : links) {
             std::string link_path;
-            if (android::base::Readlink(link, &link_path) && link_path == devpath) {
+            if (Readlink(link, &link_path) && link_path == devpath) {
                 unlink(link.c_str());
             }
         }
@@ -408,11 +418,11 @@
     const char* base = "/dev/block/";
     make_dir(base, 0755, sehandle_);
 
-    std::string name = android::base::Basename(uevent.path);
+    std::string name = Basename(uevent.path);
     std::string devpath = base + name;
 
     std::vector<std::string> links;
-    if (android::base::StartsWith(uevent.path, "/devices")) {
+    if (StartsWith(uevent.path, "/devices")) {
         links = GetBlockDeviceSymlinks(uevent);
     }
 
@@ -425,7 +435,7 @@
 
     std::string devpath;
 
-    if (android::base::StartsWith(uevent.subsystem, "usb")) {
+    if (StartsWith(uevent.subsystem, "usb")) {
         if (uevent.subsystem == "usb") {
             if (!uevent.device_name.empty()) {
                 devpath = "/dev/" + uevent.device_name;
@@ -435,7 +445,7 @@
                 // Minors are broken up into groups of 128, starting at "001"
                 int bus_id = uevent.minor / 128 + 1;
                 int device_id = uevent.minor % 128 + 1;
-                devpath = android::base::StringPrintf("/dev/bus/usb/%03d/%03d", bus_id, device_id);
+                devpath = StringPrintf("/dev/bus/usb/%03d/%03d", bus_id, device_id);
             }
         } else {
             // ignore other USB events
@@ -446,10 +456,10 @@
                subsystem != subsystems_.cend()) {
         devpath = subsystem->ParseDevPath(uevent);
     } else {
-        devpath = "/dev/" + android::base::Basename(uevent.path);
+        devpath = "/dev/" + Basename(uevent.path);
     }
 
-    mkdir_recursive(android::base::Dirname(devpath), 0755, sehandle_);
+    mkdir_recursive(Dirname(devpath), 0755, sehandle_);
 
     auto links = GetCharacterDeviceSymlinks(uevent);
 
@@ -481,3 +491,6 @@
 DeviceHandler::DeviceHandler()
     : DeviceHandler(std::vector<Permissions>{}, std::vector<SysfsPermissions>{},
                     std::vector<Subsystem>{}, false) {}
+
+}  // namespace init
+}  // namespace android
diff --git a/init/devices.h b/init/devices.h
index 362c38c..5105ad7 100644
--- a/init/devices.h
+++ b/init/devices.h
@@ -29,6 +29,9 @@
 
 #include "uevent.h"
 
+namespace android {
+namespace init {
+
 class Permissions {
   public:
     Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid);
@@ -133,4 +136,7 @@
 // Exposed for testing
 void SanitizePartitionName(std::string* string);
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/devices_test.cpp b/init/devices_test.cpp
index e1e4e49..57d8e0f 100644
--- a/init/devices_test.cpp
+++ b/init/devices_test.cpp
@@ -24,6 +24,9 @@
 
 using namespace std::string_literals;
 
+namespace android {
+namespace init {
+
 class DeviceHandlerTester {
   public:
     void TestGetSymlinks(const std::string& platform_device, const Uevent& uevent,
@@ -400,3 +403,6 @@
     EXPECT_EQ(0U, permissions.uid());
     EXPECT_EQ(1001U, permissions.gid());
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/firmware_handler.cpp b/init/firmware_handler.cpp
index 844c605..8cd5cc5 100644
--- a/init/firmware_handler.cpp
+++ b/init/firmware_handler.cpp
@@ -30,10 +30,16 @@
 
 #include "util.h"
 
+using android::base::unique_fd;
+using android::base::WriteFully;
+
+namespace android {
+namespace init {
+
 static void LoadFirmware(const Uevent& uevent, const std::string& root, int fw_fd, size_t fw_size,
                          int loading_fd, int data_fd) {
     // Start transfer.
-    android::base::WriteFully(loading_fd, "1", 1);
+    WriteFully(loading_fd, "1", 1);
 
     // Copy the firmware.
     int rc = sendfile(data_fd, fw_fd, nullptr, fw_size);
@@ -44,7 +50,7 @@
 
     // Tell the firmware whether to abort or commit.
     const char* response = (rc != -1) ? "0" : "-1";
-    android::base::WriteFully(loading_fd, response, strlen(response));
+    WriteFully(loading_fd, response, strlen(response));
 }
 
 static bool IsBooting() {
@@ -60,13 +66,13 @@
     std::string loading = root + "/loading";
     std::string data = root + "/data";
 
-    android::base::unique_fd loading_fd(open(loading.c_str(), O_WRONLY | O_CLOEXEC));
+    unique_fd loading_fd(open(loading.c_str(), O_WRONLY | O_CLOEXEC));
     if (loading_fd == -1) {
         PLOG(ERROR) << "couldn't open firmware loading fd for " << uevent.firmware;
         return;
     }
 
-    android::base::unique_fd data_fd(open(data.c_str(), O_WRONLY | O_CLOEXEC));
+    unique_fd data_fd(open(data.c_str(), O_WRONLY | O_CLOEXEC));
     if (data_fd == -1) {
         PLOG(ERROR) << "couldn't open firmware data fd for " << uevent.firmware;
         return;
@@ -78,7 +84,7 @@
 try_loading_again:
     for (size_t i = 0; i < arraysize(firmware_dirs); i++) {
         std::string file = firmware_dirs[i] + uevent.firmware;
-        android::base::unique_fd fw_fd(open(file.c_str(), O_RDONLY | O_CLOEXEC));
+        unique_fd fw_fd(open(file.c_str(), O_RDONLY | O_CLOEXEC));
         struct stat sb;
         if (fw_fd != -1 && fstat(fw_fd, &sb) != -1) {
             LoadFirmware(uevent, root, fw_fd, sb.st_size, loading_fd, data_fd);
@@ -130,3 +136,6 @@
 
     waitpid(pid, nullptr, 0);
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/firmware_handler.h b/init/firmware_handler.h
index be9daae..e456ac4 100644
--- a/init/firmware_handler.h
+++ b/init/firmware_handler.h
@@ -19,6 +19,12 @@
 
 #include "uevent.h"
 
+namespace android {
+namespace init {
+
 void HandleFirmwareEvent(const Uevent& uevent);
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/import_parser.cpp b/init/import_parser.cpp
index 99275e5..b9fa2ce 100644
--- a/init/import_parser.cpp
+++ b/init/import_parser.cpp
@@ -20,6 +20,9 @@
 
 #include "util.h"
 
+namespace android {
+namespace init {
+
 bool ImportParser::ParseSection(std::vector<std::string>&& args, const std::string& filename,
                                 int line, std::string* err) {
     if (args.size() != 2) {
@@ -50,3 +53,6 @@
         }
     }
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/import_parser.h b/init/import_parser.h
index 45cbfad..b774c57 100644
--- a/init/import_parser.h
+++ b/init/import_parser.h
@@ -22,6 +22,9 @@
 #include <string>
 #include <vector>
 
+namespace android {
+namespace init {
+
 class ImportParser : public SectionParser {
   public:
     ImportParser(Parser* parser) : parser_(parser) {}
@@ -37,4 +40,7 @@
     std::vector<std::pair<std::string, int>> imports_;
 };
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/init.cpp b/init/init.cpp
index d6e9130..0562dad 100644
--- a/init/init.cpp
+++ b/init/init.cpp
@@ -48,7 +48,6 @@
 #include <libavb/libavb.h>
 #include <private/android_filesystem_config.h>
 #include <selinux/android.h>
-#include <selinux/label.h>
 #include <selinux/selinux.h>
 
 #include <fstream>
@@ -75,6 +74,9 @@
 using android::base::boot_clock;
 using android::base::GetProperty;
 
+namespace android {
+namespace init {
+
 struct selabel_handle *sehandle;
 struct selabel_handle *sehandle_prop;
 
@@ -1156,3 +1158,10 @@
 
     return 0;
 }
+
+}  // namespace init
+}  // namespace android
+
+int main(int argc, char** argv) {
+    android::init::main(argc, argv);
+}
diff --git a/init/init.h b/init/init.h
index 6725a70..479b771 100644
--- a/init/init.h
+++ b/init/init.h
@@ -19,6 +19,11 @@
 
 #include <string>
 
+#include <selinux/label.h>
+
+namespace android {
+namespace init {
+
 // Note: These globals are *only* valid in init, so they should not be used in ueventd,
 // watchdogd, or any files that may be included in those, such as devices.cpp and util.cpp.
 // TODO: Have an Init class and remove all globals.
@@ -39,4 +44,7 @@
 
 void DumpState();
 
+}  // namespace init
+}  // namespace android
+
 #endif  /* _INIT_INIT_H */
diff --git a/init/init_first_stage.cpp b/init/init_first_stage.cpp
index 4faca89..1e4654e 100644
--- a/init/init_first_stage.cpp
+++ b/init/init_first_stage.cpp
@@ -38,6 +38,9 @@
 
 using namespace std::chrono_literals;
 
+namespace android {
+namespace init {
+
 // Class Declarations
 // ------------------
 class FirstStageMount {
@@ -493,3 +496,6 @@
     }
     setenv("INIT_AVB_VERSION", avb_handle->avb_version().c_str(), 1);
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/init_first_stage.h b/init/init_first_stage.h
index 170a24c..c7a3867 100644
--- a/init/init_first_stage.h
+++ b/init/init_first_stage.h
@@ -17,7 +17,13 @@
 #ifndef _INIT_FIRST_STAGE_H
 #define _INIT_FIRST_STAGE_H
 
+namespace android {
+namespace init {
+
 bool DoFirstStageMount();
 void SetInitAvbVersionInRecovery();
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/init_parser.cpp b/init/init_parser.cpp
index 1b31cf2..e76d589 100644
--- a/init/init_parser.cpp
+++ b/init/init_parser.cpp
@@ -25,6 +25,9 @@
 #include "parser.h"
 #include "util.h"
 
+namespace android {
+namespace init {
+
 Parser::Parser() {
 }
 
@@ -159,3 +162,6 @@
     }
     return ParseConfigFile(path);
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/init_parser.h b/init/init_parser.h
index 722ebb2..c07a699 100644
--- a/init/init_parser.h
+++ b/init/init_parser.h
@@ -45,6 +45,9 @@
 //    This function is called at the end of the file.
 //    It indicates that the parsing has completed and any relevant objects should be committed.
 
+namespace android {
+namespace init {
+
 class SectionParser {
   public:
     virtual ~SectionParser() {}
@@ -93,4 +96,7 @@
     bool is_odm_etc_init_loaded_ = false;
 };
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/init_parser_test.cpp b/init/init_parser_test.cpp
index 86d60d0..38b8275 100644
--- a/init/init_parser_test.cpp
+++ b/init/init_parser_test.cpp
@@ -24,6 +24,9 @@
 #include <string>
 #include <vector>
 
+namespace android {
+namespace init {
+
 TEST(init_parser, make_exec_oneshot_service_invalid_syntax) {
     ServiceManager& sm = ServiceManager::GetInstance();
     std::vector<std::string> args;
@@ -141,3 +144,6 @@
 TEST(init_parser, make_exec_oneshot_service_with_just_command_no_dash) {
     Test_make_exec_oneshot_service(false, false, false, false, false);
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/init_test.cpp b/init/init_test.cpp
index 7093ba9..0a4071b 100644
--- a/init/init_test.cpp
+++ b/init/init_test.cpp
@@ -27,6 +27,9 @@
 #include "keyword_map.h"
 #include "util.h"
 
+namespace android {
+namespace init {
+
 class TestFunctionMap : public KeywordMap<BuiltinFunction> {
   public:
     // Helper for argument-less functions
@@ -185,3 +188,6 @@
 
     EXPECT_EQ(6, num_executed);
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/keychords.cpp b/init/keychords.cpp
index c572cee..a0d7cc5 100644
--- a/init/keychords.cpp
+++ b/init/keychords.cpp
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include "keychords.h"
+
 #include <fcntl.h>
 #include <stdlib.h>
 #include <sys/stat.h>
@@ -25,7 +27,9 @@
 #include <android-base/properties.h>
 
 #include "init.h"
-#include "service.h"
+
+namespace android {
+namespace init {
 
 static struct input_keychord *keychords = 0;
 static int keychords_count = 0;
@@ -112,3 +116,6 @@
 
     register_epoll_handler(keychord_fd, handle_keychord);
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/keychords.h b/init/keychords.h
index d2723b7..1c34098 100644
--- a/init/keychords.h
+++ b/init/keychords.h
@@ -17,9 +17,15 @@
 #ifndef _INIT_KEYCHORDS_H_
 #define _INIT_KEYCHORDS_H_
 
-struct service;
+#include "service.h"
 
-void add_service_keycodes(service*);
+namespace android {
+namespace init {
+
+void add_service_keycodes(Service* svc);
 void keychord_init();
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/keyword_map.h b/init/keyword_map.h
index 88bad01..481d637 100644
--- a/init/keyword_map.h
+++ b/init/keyword_map.h
@@ -22,6 +22,9 @@
 
 #include <android-base/stringprintf.h>
 
+namespace android {
+namespace init {
+
 template <typename Function>
 class KeywordMap {
   public:
@@ -79,4 +82,7 @@
     virtual const Map& map() const = 0;
 };
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/log.cpp b/init/log.cpp
index 0615730..1830077 100644
--- a/init/log.cpp
+++ b/init/log.cpp
@@ -23,6 +23,9 @@
 #include <android-base/logging.h>
 #include <selinux/selinux.h>
 
+namespace android {
+namespace init {
+
 void InitKernelLogging(char* argv[]) {
     // Make stdin/stdout/stderr all point to /dev/null.
     int fd = open("/sys/fs/selinux/null", O_RDWR);
@@ -55,3 +58,6 @@
     android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf);
     return 0;
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/log.h b/init/log.h
index 29a27af..5a4eba6 100644
--- a/init/log.h
+++ b/init/log.h
@@ -19,8 +19,14 @@
 
 #include <sys/cdefs.h>
 
+namespace android {
+namespace init {
+
 void InitKernelLogging(char* argv[]);
 
 int selinux_klog_callback(int level, const char* fmt, ...) __printflike(2, 3);
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/parser.cpp b/init/parser.cpp
index 0d13cfe..c0fa6d9 100644
--- a/init/parser.cpp
+++ b/init/parser.cpp
@@ -1,5 +1,8 @@
 #include "parser.h"
 
+namespace android {
+namespace init {
+
 int next_token(struct parse_state *state)
 {
     char *x = state->ptr;
@@ -116,3 +119,6 @@
     }
     return T_EOF;
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/parser.h b/init/parser.h
index 3dcc566..86e4c57 100644
--- a/init/parser.h
+++ b/init/parser.h
@@ -21,6 +21,9 @@
 #define T_TEXT 1
 #define T_NEWLINE 2
 
+namespace android {
+namespace init {
+
 struct parse_state
 {
     char *ptr;
@@ -31,4 +34,7 @@
 
 int next_token(struct parse_state *state);
 
+}  // namespace init
+}  // namespace android
+
 #endif /* PARSER_H_ */
diff --git a/init/property_service.cpp b/init/property_service.cpp
index 31e6e48..0af6c21 100644
--- a/init/property_service.cpp
+++ b/init/property_service.cpp
@@ -58,6 +58,9 @@
 #define PERSISTENT_PROPERTY_DIR  "/data/property"
 #define RECOVERY_MOUNT_POINT "/recovery"
 
+namespace android {
+namespace init {
+
 static int persistent_properties_loaded = 0;
 
 static int property_set_fd = -1;
@@ -741,3 +744,6 @@
 
     register_epoll_handler(property_set_fd, handle_property_set_fd);
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/property_service.h b/init/property_service.h
index 9251722..a55e79c 100644
--- a/init/property_service.h
+++ b/init/property_service.h
@@ -21,6 +21,9 @@
 
 #include <string>
 
+namespace android {
+namespace init {
+
 struct property_audit_data {
     ucred *cr;
     const char* name;
@@ -36,5 +39,7 @@
 uint32_t property_set(const std::string& name, const std::string& value);
 bool is_legal_property_name(const std::string& name);
 
+}  // namespace init
+}  // namespace android
 
 #endif  /* _INIT_PROPERTY_H */
diff --git a/init/property_service_test.cpp b/init/property_service_test.cpp
index 4d784aa..3a64e02 100644
--- a/init/property_service_test.cpp
+++ b/init/property_service_test.cpp
@@ -23,6 +23,9 @@
 
 #include <gtest/gtest.h>
 
+namespace android {
+namespace init {
+
 TEST(property_service, very_long_name_35166374) {
   // Connect to the property service directly...
   int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
@@ -46,3 +49,6 @@
   ASSERT_EQ(static_cast<ssize_t>(sizeof(data)), send(fd, &data, sizeof(data), 0));
   ASSERT_EQ(0, close(fd));
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/reboot.cpp b/init/reboot.cpp
index cdfc698..df7912f 100644
--- a/init/reboot.cpp
+++ b/init/reboot.cpp
@@ -53,6 +53,9 @@
 
 using android::base::StringPrintf;
 
+namespace android {
+namespace init {
+
 // represents umount status during reboot / shutdown.
 enum UmountStat {
     /* umount succeeded. */
@@ -468,3 +471,6 @@
     DoReboot(cmd, command, reboot_target, run_fsck);
     return true;
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/reboot.h b/init/reboot.h
index b304b3c..e559540 100644
--- a/init/reboot.h
+++ b/init/reboot.h
@@ -19,6 +19,9 @@
 
 #include <string>
 
+namespace android {
+namespace init {
+
 /* Reboot / shutdown the system.
  * cmd ANDROID_RB_* as defined in android_reboot.h
  * reason Reason string like "reboot", "userrequested"
@@ -32,4 +35,7 @@
 // Parses and handles a setprop sys.powerctl message.
 bool HandlePowerctlMessage(const std::string& command);
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/service.cpp b/init/service.cpp
index d8c6875..f9a452b 100644
--- a/init/service.cpp
+++ b/init/service.cpp
@@ -45,10 +45,16 @@
 #include "util.h"
 
 using android::base::boot_clock;
+using android::base::GetProperty;
+using android::base::Join;
 using android::base::ParseInt;
+using android::base::StartsWith;
 using android::base::StringPrintf;
 using android::base::WriteStringToFile;
 
+namespace android {
+namespace init {
+
 static std::string ComputeContextFromExecutable(std::string& service_name,
                                                 const std::string& service_path) {
     std::string computed_context;
@@ -321,8 +327,8 @@
 
 void Service::DumpState() const {
     LOG(INFO) << "service " << name_;
-    LOG(INFO) << "  class '" << android::base::Join(classnames_, " ") << "'";
-    LOG(INFO) << "  exec "<< android::base::Join(args_, " ");
+    LOG(INFO) << "  class '" << Join(classnames_, " ") << "'";
+    LOG(INFO) << "  exec " << Join(args_, " ");
     std::for_each(descriptors_.begin(), descriptors_.end(),
                   [] (const auto& info) { LOG(INFO) << *info; });
 }
@@ -525,9 +531,8 @@
 
 // name type perm [ uid gid context ]
 bool Service::ParseSocket(const std::vector<std::string>& args, std::string* err) {
-    if (!android::base::StartsWith(args[2], "dgram") &&
-        !android::base::StartsWith(args[2], "stream") &&
-        !android::base::StartsWith(args[2], "seqpacket")) {
+    if (!StartsWith(args[2], "dgram") && !StartsWith(args[2], "stream") &&
+        !StartsWith(args[2], "seqpacket")) {
         *err = "socket type must be 'dgram', 'stream' or 'seqpacket'";
         return false;
     }
@@ -695,13 +700,13 @@
 
         // See if there were "writepid" instructions to write to files under /dev/cpuset/.
         auto cpuset_predicate = [](const std::string& path) {
-            return android::base::StartsWith(path, "/dev/cpuset/");
+            return StartsWith(path, "/dev/cpuset/");
         };
         auto iter = std::find_if(writepid_files_.begin(), writepid_files_.end(), cpuset_predicate);
         if (iter == writepid_files_.end()) {
             // There were no "writepid" instructions for cpusets, check if the system default
             // cpuset is specified to be used for the process.
-            std::string default_cpuset = android::base::GetProperty("ro.cpuset.default", "");
+            std::string default_cpuset = GetProperty("ro.cpuset.default", "");
             if (!default_cpuset.empty()) {
                 // Make sure the cpuset name starts and ends with '/'.
                 // A single '/' means the 'root' cpuset.
@@ -954,8 +959,7 @@
     std::vector<std::string> str_args(args.begin() + command_arg, args.end());
 
     exec_count_++;
-    std::string name =
-        "exec " + std::to_string(exec_count_) + " (" + android::base::Join(str_args, " ") + ")";
+    std::string name = "exec " + std::to_string(exec_count_) + " (" + Join(str_args, " ") + ")";
 
     unsigned flags = SVC_EXEC | SVC_ONESHOT | SVC_TEMPORARY;
     CapSet no_capabilities;
@@ -1093,14 +1097,12 @@
     std::string name;
     std::string wait_string;
     if (svc) {
-        name = android::base::StringPrintf("Service '%s' (pid %d)",
-                                           svc->name().c_str(), pid);
+        name = StringPrintf("Service '%s' (pid %d)", svc->name().c_str(), pid);
         if (svc->flags() & SVC_EXEC) {
-            wait_string =
-                android::base::StringPrintf(" waiting took %f seconds", exec_waiter_->duration_s());
+            wait_string = StringPrintf(" waiting took %f seconds", exec_waiter_->duration_s());
         }
     } else {
-        name = android::base::StringPrintf("Untracked pid %d", pid);
+        name = StringPrintf("Untracked pid %d", pid);
     }
 
     if (WIFEXITED(status)) {
@@ -1175,3 +1177,6 @@
     // the service name to the "ctl.start" and "ctl.stop" properties.)
     return is_legal_property_name("init.svc." + name) && name.size() <= PROP_VALUE_MAX;
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/service.h b/init/service.h
index b9c270a..3c7dc74 100644
--- a/init/service.h
+++ b/init/service.h
@@ -55,8 +55,8 @@
 
 #define NR_SVC_SUPP_GIDS 12    // twelve supplementary groups
 
-class Action;
-class ServiceManager;
+namespace android {
+namespace init {
 
 struct ServiceEnvironmentInfo {
     ServiceEnvironmentInfo();
@@ -236,4 +236,7 @@
     std::unique_ptr<Service> service_;
 };
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/service_test.cpp b/init/service_test.cpp
index b9c4627..44f28a3 100644
--- a/init/service_test.cpp
+++ b/init/service_test.cpp
@@ -23,6 +23,9 @@
 
 #include <gtest/gtest.h>
 
+namespace android {
+namespace init {
+
 TEST(service, pod_initialized) {
     constexpr auto memory_size = sizeof(Service);
     alignas(alignof(Service)) char old_memory[memory_size];
@@ -67,3 +70,6 @@
     EXPECT_EQ(-1000, service_in_old_memory2->oom_score_adjust());
     EXPECT_FALSE(service_in_old_memory->process_cgroup_empty());
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/signal_handler.cpp b/init/signal_handler.cpp
index 183837b..db1bfcf 100644
--- a/init/signal_handler.cpp
+++ b/init/signal_handler.cpp
@@ -25,6 +25,9 @@
 #include "init.h"
 #include "service.h"
 
+namespace android {
+namespace init {
+
 static int signal_write_fd = -1;
 static int signal_read_fd = -1;
 
@@ -64,3 +67,6 @@
 
     register_epoll_handler(signal_read_fd, handle_signal);
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/signal_handler.h b/init/signal_handler.h
index 449b4af..f7881ab 100644
--- a/init/signal_handler.h
+++ b/init/signal_handler.h
@@ -17,6 +17,12 @@
 #ifndef _INIT_SIGNAL_HANDLER_H_
 #define _INIT_SIGNAL_HANDLER_H_
 
+namespace android {
+namespace init {
+
 void signal_handler_init(void);
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/uevent.h b/init/uevent.h
index 1095665..c4fd945 100644
--- a/init/uevent.h
+++ b/init/uevent.h
@@ -19,6 +19,9 @@
 
 #include <string>
 
+namespace android {
+namespace init {
+
 struct Uevent {
     std::string action;
     std::string path;
@@ -31,4 +34,7 @@
     int minor;
 };
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/uevent_listener.cpp b/init/uevent_listener.cpp
index c748984..ac1d7c7 100644
--- a/init/uevent_listener.cpp
+++ b/init/uevent_listener.cpp
@@ -26,6 +26,9 @@
 #include <android-base/logging.h>
 #include <cutils/uevent.h>
 
+namespace android {
+namespace init {
+
 static void ParseEvent(const char* msg, Uevent* uevent) {
     uevent->partition_num = -1;
     uevent->major = -1;
@@ -212,3 +215,6 @@
         }
     }
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/uevent_listener.h b/init/uevent_listener.h
index 0dae102..5b453fe 100644
--- a/init/uevent_listener.h
+++ b/init/uevent_listener.h
@@ -29,6 +29,9 @@
 
 #define UEVENT_MSG_LEN 2048
 
+namespace android {
+namespace init {
+
 enum class ListenerAction {
     kStop = 0,  // Stop regenerating uevents as we've handled the one(s) we're interested in.
     kContinue,  // Continue regenerating uevents as we haven't seen the one(s) we're interested in.
@@ -53,4 +56,7 @@
     android::base::unique_fd device_fd_;
 };
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/ueventd.cpp b/init/ueventd.cpp
index 2fd8058..81a0572 100644
--- a/init/ueventd.cpp
+++ b/init/ueventd.cpp
@@ -99,6 +99,9 @@
 // the uevent listener resumes in polling mode and will handle the uevents that occurred during
 // coldboot.
 
+namespace android {
+namespace init {
+
 class ColdBoot {
   public:
     ColdBoot(UeventListener& uevent_listener, DeviceHandler& device_handler)
@@ -273,3 +276,6 @@
 
     return 0;
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/ueventd.h b/init/ueventd.h
index 1f424d3..51775ec 100644
--- a/init/ueventd.h
+++ b/init/ueventd.h
@@ -17,6 +17,12 @@
 #ifndef _INIT_UEVENTD_H_
 #define _INIT_UEVENTD_H_
 
+namespace android {
+namespace init {
+
 int ueventd_main(int argc, char** argv);
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/ueventd_parser.cpp b/init/ueventd_parser.cpp
index 7156e76..02e0d42 100644
--- a/init/ueventd_parser.cpp
+++ b/init/ueventd_parser.cpp
@@ -21,6 +21,9 @@
 
 #include "keyword_map.h"
 
+namespace android {
+namespace init {
+
 bool ParsePermissionsLine(std::vector<std::string>&& args, std::string* err,
                           std::vector<SysfsPermissions>* out_sysfs_permissions,
                           std::vector<Permissions>* out_dev_permissions) {
@@ -143,3 +146,6 @@
 void SubsystemParser::EndSection() {
     subsystems_->emplace_back(std::move(subsystem_));
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/ueventd_parser.h b/init/ueventd_parser.h
index c1ce976..592df63 100644
--- a/init/ueventd_parser.h
+++ b/init/ueventd_parser.h
@@ -23,6 +23,9 @@
 #include "devices.h"
 #include "init_parser.h"
 
+namespace android {
+namespace init {
+
 class SubsystemParser : public SectionParser {
   public:
     SubsystemParser(std::vector<Subsystem>* subsystems) : subsystems_(subsystems) {}
@@ -43,4 +46,7 @@
                           std::vector<SysfsPermissions>* out_sysfs_permissions,
                           std::vector<Permissions>* out_dev_permissions);
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/util.cpp b/init/util.cpp
index 75f81b9..4b1894f 100644
--- a/init/util.cpp
+++ b/init/util.cpp
@@ -50,6 +50,9 @@
 using android::base::boot_clock;
 using namespace std::literals::string_literals;
 
+namespace android {
+namespace init {
+
 // DecodeUid() - decodes and returns the given string, which can be either the
 // numeric or name representation, into the integer uid or gid. Returns
 // UINT_MAX on error.
@@ -397,3 +400,6 @@
     }
     return false;
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/util.h b/init/util.h
index 1ad6b77..346953f 100644
--- a/init/util.h
+++ b/init/util.h
@@ -35,6 +35,9 @@
 using android::base::boot_clock;
 using namespace std::chrono_literals;
 
+namespace android {
+namespace init {
+
 int CreateSocket(const char* name, int type, bool passcred, mode_t perm, uid_t uid, gid_t gid,
                  const char* socketcon, selabel_handle* sehandle);
 
@@ -78,4 +81,7 @@
 bool read_android_dt_file(const std::string& sub_path, std::string* dt_content);
 bool is_android_dt_value_expected(const std::string& sub_path, const std::string& expected_content);
 
+}  // namespace init
+}  // namespace android
+
 #endif
diff --git a/init/util_test.cpp b/init/util_test.cpp
index 4bb8a83..c16ab74 100644
--- a/init/util_test.cpp
+++ b/init/util_test.cpp
@@ -26,6 +26,9 @@
 
 using namespace std::literals::string_literals;
 
+namespace android {
+namespace init {
+
 TEST(util, ReadFile_ENOENT) {
     std::string s("hello");
     std::string err;
@@ -187,3 +190,6 @@
     std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
     EXPECT_TRUE(is_dir(path1.c_str()));
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/watchdogd.cpp b/init/watchdogd.cpp
index 7baa487..e0164b4 100644
--- a/init/watchdogd.cpp
+++ b/init/watchdogd.cpp
@@ -31,6 +31,9 @@
 
 #define DEV_NAME "/dev/watchdog"
 
+namespace android {
+namespace init {
+
 int watchdogd_main(int argc, char **argv) {
     InitKernelLogging(argv);
 
@@ -73,3 +76,6 @@
         sleep(interval);
     }
 }
+
+}  // namespace init
+}  // namespace android
diff --git a/init/watchdogd.h b/init/watchdogd.h
index 8b48ab8..73f77d5 100644
--- a/init/watchdogd.h
+++ b/init/watchdogd.h
@@ -17,6 +17,12 @@
 #ifndef _INIT_WATCHDOGD_H_
 #define _INIT_WATCHDOGD_H_
 
+namespace android {
+namespace init {
+
 int watchdogd_main(int argc, char **argv);
 
+}  // namespace init
+}  // namespace android
+
 #endif