Merge "init: print service name when started waiting"
diff --git a/fs_mgr/libdm/dm.cpp b/fs_mgr/libdm/dm.cpp
index e8bae60..5a2dfc6 100644
--- a/fs_mgr/libdm/dm.cpp
+++ b/fs_mgr/libdm/dm.cpp
@@ -14,23 +14,12 @@
  * limitations under the License.
  */
 
-#include <errno.h>
-#include <fcntl.h>
-#include <linux/dm-ioctl.h>
-#include <stdint.h>
+#include "libdm/dm.h"
+
 #include <sys/ioctl.h>
 #include <sys/types.h>
-#include <unistd.h>
 
-#include <memory>
-#include <string>
-#include <vector>
-
-#include <android-base/logging.h>
 #include <android-base/macros.h>
-#include <android-base/unique_fd.h>
-
-#include "dm.h"
 
 namespace android {
 namespace dm {
@@ -105,8 +94,37 @@
     return DmDeviceState::INVALID;
 }
 
-bool DeviceMapper::LoadTableAndActivate(const std::string& /* name */, const DmTable& /* table */) {
-    return false;
+bool DeviceMapper::CreateDevice(const std::string& name, const DmTable& table) {
+    if (!CreateDevice(name)) {
+        return false;
+    }
+    if (!LoadTableAndActivate(name, table)) {
+        DeleteDevice(name);
+        return false;
+    }
+    return true;
+}
+
+bool DeviceMapper::LoadTableAndActivate(const std::string& name, const DmTable& table) {
+    std::string ioctl_buffer(sizeof(struct dm_ioctl), 0);
+    ioctl_buffer += table.Serialize();
+
+    struct dm_ioctl* io = reinterpret_cast<struct dm_ioctl*>(&ioctl_buffer[0]);
+    InitIo(io, name);
+    io->data_size = ioctl_buffer.size();
+    io->data_start = sizeof(struct dm_ioctl);
+    io->target_count = static_cast<uint32_t>(table.num_targets());
+    if (ioctl(fd_, DM_TABLE_LOAD, io)) {
+        PLOG(ERROR) << "DM_TABLE_LOAD failed";
+        return false;
+    }
+
+    InitIo(io, name);
+    if (ioctl(fd_, DM_DEV_SUSPEND, io)) {
+        PLOG(ERROR) << "DM_TABLE_SUSPEND resume failed";
+        return false;
+    }
+    return true;
 }
 
 // Reads all the available device mapper targets and their corresponding
diff --git a/fs_mgr/libdm/dm_table.cpp b/fs_mgr/libdm/dm_table.cpp
index 14b3932..868286e 100644
--- a/fs_mgr/libdm/dm_table.cpp
+++ b/fs_mgr/libdm/dm_table.cpp
@@ -14,14 +14,11 @@
  * limitations under the License.
  */
 
+#include "libdm/dm_table.h"
+
 #include <android-base/logging.h>
 #include <android-base/macros.h>
 
-#include <string>
-#include <vector>
-
-#include "dm_table.h"
-
 namespace android {
 namespace dm {
 
@@ -37,8 +34,8 @@
     return true;
 }
 
-uint64_t DmTable::size() const {
-    return valid() ? size_ : 0;
+uint64_t DmTable::num_sectors() const {
+    return valid() ? num_sectors_ : 0;
 }
 
 // Returns a string represnetation of the table that is ready to be passed
diff --git a/fs_mgr/libdm/dm_target.cpp b/fs_mgr/libdm/dm_target.cpp
index 8bcd526..dbe4fed 100644
--- a/fs_mgr/libdm/dm_target.cpp
+++ b/fs_mgr/libdm/dm_target.cpp
@@ -14,16 +14,11 @@
  * limitations under the License.
  */
 
+#include "libdm/dm_target.h"
+
 #include <android-base/logging.h>
 #include <android-base/macros.h>
 
-#include <stdint.h>
-
-#include <string>
-#include <vector>
-
-#include "dm_target.h"
-
 namespace android {
 namespace dm {}  // namespace dm
 }  // namespace android
diff --git a/fs_mgr/libdm/include/dm.h b/fs_mgr/libdm/include/libdm/dm.h
similarity index 91%
rename from fs_mgr/libdm/include/dm.h
rename to fs_mgr/libdm/include/libdm/dm.h
index 52a9a11..8407774 100644
--- a/fs_mgr/libdm/include/dm.h
+++ b/fs_mgr/libdm/include/libdm/dm.h
@@ -17,18 +17,20 @@
 #ifndef _LIBDM_DM_H_
 #define _LIBDM_DM_H_
 
-#include <errno.h>
 #include <fcntl.h>
 #include <linux/dm-ioctl.h>
 #include <linux/kdev_t.h>
+#include <stdint.h>
 #include <sys/sysmacros.h>
 #include <unistd.h>
 
 #include <memory>
+#include <string>
+#include <vector>
 
 #include <android-base/logging.h>
 
-#include <dm_table.h>
+#include "dm_table.h"
 
 // The minimum expected device mapper major.minor version
 #define DM_VERSION0 (4)
@@ -67,14 +69,6 @@
         uint64_t dev_;
     };
 
-    // Creates a device mapper device with given name.
-    // Return 'true' on success and 'false' on failure to
-    // create OR if a device mapper device with the same name already
-    // exists.
-    // TODO(b/110035986): Make this method private and to be only
-    // called through LoadTableAndActivate() below.
-    bool CreateDevice(const std::string& name);
-
     // Removes a device mapper device with the given name.
     // Returns 'true' on success, false otherwise.
     bool DeleteDevice(const std::string& name);
@@ -88,9 +82,14 @@
     // One of INVALID, SUSPENDED or ACTIVE.
     DmDeviceState state(const std::string& name) const;
 
-    // Loads the device mapper table from parameter into the underlying
-    // device mapper device with given name and activate / resumes the device in the process.
-    // If a device mapper device with the 'name', doesn't exist, it will be created.
+    // Creates a device, loads the given table, and activates it. If the device
+    // is not able to be activated, it is destroyed, and false is returned.
+    bool CreateDevice(const std::string& name, const DmTable& table);
+
+    // Loads the device mapper table from parameter into the underlying device
+    // mapper device with given name and activate / resumes the device in the
+    // process. A device with the given name must already exist.
+    //
     // Returns 'true' on success, false otherwise.
     bool LoadTableAndActivate(const std::string& name, const DmTable& table);
 
@@ -138,6 +137,12 @@
         }
     }
 
+    // Creates a device mapper device with given name.
+    // Return 'true' on success and 'false' on failure to
+    // create OR if a device mapper device with the same name already
+    // exists.
+    bool CreateDevice(const std::string& name);
+
     int fd_;
     // Non-copyable & Non-movable
     DeviceMapper(const DeviceMapper&) = delete;
diff --git a/fs_mgr/libdm/include/dm_table.h b/fs_mgr/libdm/include/libdm/dm_table.h
similarity index 92%
rename from fs_mgr/libdm/include/dm_table.h
rename to fs_mgr/libdm/include/libdm/dm_table.h
index 0b1685d..8a5c624 100644
--- a/fs_mgr/libdm/include/dm_table.h
+++ b/fs_mgr/libdm/include/libdm/dm_table.h
@@ -30,7 +30,7 @@
 
 class DmTable {
   public:
-    DmTable() : size_(0){};
+    DmTable() : num_sectors_(0){};
 
     // Adds a target to the device mapper table for a range specified in the target object.
     // The function will return 'true' if the target was successfully added and doesn't overlap with
@@ -48,9 +48,12 @@
     // table is malformed.
     bool valid() const;
 
+    // Returns the toatl number of targets.
+    size_t num_targets() const { return targets_.size(); }
+
     // Returns the total size represented by the table in terms of number of 512-byte sectors.
     // NOTE: This function will overlook if there are any gaps in the targets added in the table.
-    uint64_t size() const;
+    uint64_t num_sectors() const;
 
     // Returns the string represntation of the table that is ready to be passed into the kernel
     // as part of the DM_TABLE_LOAD ioctl.
@@ -66,7 +69,7 @@
 
     // Total size in terms of # of sectors, as calculated by looking at the last and the first
     // target in 'target_'.
-    uint64_t size_;
+    uint64_t num_sectors_;
 };
 
 }  // namespace dm
diff --git a/fs_mgr/libdm/include/dm_target.h b/fs_mgr/libdm/include/libdm/dm_target.h
similarity index 99%
rename from fs_mgr/libdm/include/dm_target.h
rename to fs_mgr/libdm/include/libdm/dm_target.h
index 31b0cb6..e3058a8 100644
--- a/fs_mgr/libdm/include/dm_target.h
+++ b/fs_mgr/libdm/include/libdm/dm_target.h
@@ -20,10 +20,10 @@
 #include <linux/dm-ioctl.h>
 #include <stdint.h>
 
-#include <android-base/logging.h>
-
 #include <string>
 
+#include <android-base/logging.h>
+
 namespace android {
 namespace dm {
 
diff --git a/fs_mgr/tools/dmctl.cpp b/fs_mgr/tools/dmctl.cpp
index e82c718..d637dc6 100644
--- a/fs_mgr/tools/dmctl.cpp
+++ b/fs_mgr/tools/dmctl.cpp
@@ -23,7 +23,7 @@
 #include <unistd.h>
 
 #include <android-base/unique_fd.h>
-#include <dm.h>
+#include <libdm/dm.h>
 
 #include <functional>
 #include <iomanip>
@@ -34,6 +34,7 @@
 #include <vector>
 
 using DeviceMapper = ::android::dm::DeviceMapper;
+using DmTable = ::android::dm::DmTable;
 using DmTarget = ::android::dm::DmTarget;
 using DmBlockDevice = ::android::dm::DeviceMapper::DmBlockDevice;
 
@@ -53,9 +54,11 @@
         return -EINVAL;
     }
 
+    DmTable table;
+
     std::string name = argv[0];
     DeviceMapper& dm = DeviceMapper::Instance();
-    if (!dm.CreateDevice(name)) {
+    if (!dm.CreateDevice(name, table)) {
         std::cerr << "Failed to create device-mapper device with name: " << name << std::endl;
         return -EIO;
     }
diff --git a/init/Android.bp b/init/Android.bp
index da0895f..cf7637f 100644
--- a/init/Android.bp
+++ b/init/Android.bp
@@ -227,41 +227,39 @@
         "liblog",
         "libcutils",
     ],
+    srcs: [
+        "action.cpp",
+        "action_manager.cpp",
+        "action_parser.cpp",
+        "capabilities.cpp",
+        "descriptors.cpp",
+        "epoll.cpp",
+        "keychords.cpp",
+        "import_parser.cpp",
+        "host_import_parser.cpp",
+        "host_init_verifier.cpp",
+        "host_init_stubs.cpp",
+        "parser.cpp",
+        "rlimit_parser.cpp",
+        "tokenizer.cpp",
+        "service.cpp",
+        "subcontext.cpp",
+        "subcontext.proto",
+        "util.cpp",
+    ],
     proto: {
         type: "lite",
     },
+    generated_headers: [
+        "generated_stub_builtin_function_map",
+        "generated_android_ids"
+    ],
     target: {
-        linux: {
-            srcs: [
-                "action.cpp",
-                "action_manager.cpp",
-                "action_parser.cpp",
-                "capabilities.cpp",
-                "descriptors.cpp",
-                "epoll.cpp",
-                "keychords.cpp",
-                "import_parser.cpp",
-                "host_import_parser.cpp",
-                "host_init_verifier.cpp",
-                "host_init_stubs.cpp",
-                "parser.cpp",
-                "rlimit_parser.cpp",
-                "tokenizer.cpp",
-                "service.cpp",
-                "subcontext.cpp",
-                "subcontext.proto",
-                "util.cpp",
-            ],
-            generated_headers: [
-                "generated_stub_builtin_function_map",
-                "generated_android_ids",
-            ],
-        },
         android: {
             enabled: false,
         },
         darwin: {
-            srcs: ["mac_init_verifier.cpp"],
+            enabled: false,
         },
     },
 }
diff --git a/init/mac_init_verifier.cpp b/init/mac_init_verifier.cpp
deleted file mode 100644
index 646509e..0000000
--- a/init/mac_init_verifier.cpp
+++ /dev/null
@@ -1,19 +0,0 @@
-//
-// Copyright (C) 2018 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//      http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-
-int main() {
-    return 0;
-}
diff --git a/shell_and_utilities/Android.bp b/shell_and_utilities/Android.bp
index dfb88f7..7834dd5 100644
--- a/shell_and_utilities/Android.bp
+++ b/shell_and_utilities/Android.bp
@@ -1,25 +1,46 @@
 phony {
     name: "shell_and_utilities",
     required: [
+        "shell_and_utilities_system",
+        "shell_and_utilities_recovery",
+        "shell_and_utilities_vendor",
+    ],
+}
+
+phony {
+    name: "shell_and_utilities_system",
+    required: [
         "awk",
-        "awk_vendor",
         "bzip2",
         "grep",
-        "grep_vendor",
         "logwrapper",
-        "logwrapper_vendor",
         "mkshrc",
-        "mkshrc_vendor",
         "newfs_msdos",
         "reboot",
         "sh",
-        "sh.recovery",
-        "sh_vendor",
         "toolbox",
-        "toolbox.recovery",
-        "toolbox_vendor",
         "toybox",
+    ],
+}
+
+phony {
+    name: "shell_and_utilities_recovery",
+    required: [
+        "sh.recovery",
+        "toolbox.recovery",
         "toybox.recovery",
+    ],
+}
+
+phony {
+    name: "shell_and_utilities_vendor",
+    required: [
+        "awk_vendor",
+        "grep_vendor",
+        "logwrapper_vendor",
+        "mkshrc_vendor",
+        "sh_vendor",
+        "toolbox_vendor",
         "toybox_vendor",
     ],
 }
diff --git a/shell_and_utilities/README.md b/shell_and_utilities/README.md
index b15be1f..e310e6b 100644
--- a/shell_and_utilities/README.md
+++ b/shell_and_utilities/README.md
@@ -193,13 +193,15 @@
 Android Q
 ---------
 
+BSD: fsck\_msdos newfs\_msdos
+
 bzip2: bzcat bzip2 bunzip2
 
 one-true-awk: awk
 
 PCRE: egrep fgrep grep
 
-toolbox: getevent getprop newfs\_msdos
+toolbox: getevent getprop
 
 toybox: acpi base64 basename blockdev cal cat chcon chgrp chmod chown
 chroot chrt cksum clear cmp comm cp cpio cut date dd df diff dirname