Gsid should behave more like a normal service.

This patch makes the following changes to gsid:
 - It now runs on startup and is no longer a one-shot service.
 - /data/gsi is now created via init, with the appropriate permissions.
 - gsi_tool no longer starts/stops gsid.
 - /metadata/gsi is readable by the system group, so system_server can
   read /metadata/gsi/booted.

Bug: 122556707
Test: manual test
Change-Id: I08c0c6e9a155a0dea113c8535e71bda5aeda0ff6
diff --git a/gsi_service.cpp b/gsi_service.cpp
index fae6acb..f24f478 100644
--- a/gsi_service.cpp
+++ b/gsi_service.cpp
@@ -309,10 +309,6 @@
         return INSTALL_ERROR_GENERIC;
     }
 
-    if (!EnsureFolderExists(kGsiDataFolder)) {
-        return INSTALL_ERROR_GENERIC;
-    }
-
     struct statvfs sb;
     if (statvfs(kGsiDataFolder, &sb)) {
         PLOG(ERROR) << "failed to read file system stats";
@@ -724,15 +720,6 @@
     return true;
 }
 
-bool GsiService::EnsureFolderExists(const std::string& path) {
-    if (!mkdir(path.c_str(), 0755) || errno == EEXIST) {
-        return true;
-    }
-
-    LOG(ERROR) << "mkdir: " << strerror(errno) << ": " << path;
-    return false;
-}
-
 void GsiService::RunStartupTasks() {
     if (!IsGsiInstalled()) {
         return;
@@ -750,6 +737,9 @@
             RemoveGsiFiles(true /* wipeUserdata */);
         }
     } else {
+        // NB: When kOnlyAllowSingleBoot is true, init will write "disabled"
+        // into the install_status file, which will cause GetBootAttempts to
+        // return false. Thus, we won't write "ok" here.
         int ignore;
         if (GetBootAttempts(boot_key, &ignore)) {
             // Mark the GSI as having successfully booted.
diff --git a/gsi_service.h b/gsi_service.h
index 55c83b1..f5f8974 100644
--- a/gsi_service.h
+++ b/gsi_service.h
@@ -75,7 +75,6 @@
     bool SetGsiBootable();
     int ReenableGsi();
     bool DisableGsiInstall();
-    bool EnsureFolderExists(const std::string& path);
     bool AddPartitionFiemap(android::fs_mgr::MetadataBuilder* builder,
                             android::fs_mgr::Partition* partition,
                             android::fiemap_writer::FiemapWriter* writer);
diff --git a/gsi_tool.cpp b/gsi_tool.cpp
index b4fad2f..256af85 100644
--- a/gsi_tool.cpp
+++ b/gsi_tool.cpp
@@ -396,13 +396,6 @@
 }
 
 int main(int argc, char** argv) {
-    // Ensure gsid is started.
-    android::base::SetProperty("ctl.start", "gsid");
-    if (!android::base::WaitForProperty("init.svc.gsid", "running", 5s)) {
-        std::cout << "Unable to start gsid";
-        return EX_SOFTWARE;
-    }
-
     auto gsid = getService();
     if (!gsid) {
         std::cout << "Could not connect to the gsid service." << std::endl;
@@ -431,7 +424,5 @@
     }
 
     int rc = iter->second(gsid, argc - 1, argv + 1);
-
-    android::base::SetProperty("ctl.stop", "gsid");
     return rc;
 }
diff --git a/gsid.rc b/gsid.rc
index 7138f27..7038ab3 100644
--- a/gsid.rc
+++ b/gsid.rc
@@ -1,12 +1,13 @@
 service gsid /system/bin/gsid
-    oneshot
-    disabled
     user root
-    group root
+    group root system
 
 on post-fs
-    mkdir /metadata/gsi
-    chmod 0700 /metadata/gsi
+    mkdir /metadata/gsi 0771 root system
+
+on post-fs-data
+    mkdir /data/gsi 0700 root root
 
 on boot
-    exec - gsid gsid -- /system/bin/gsid run-startup-tasks
+    exec - root root -- /system/bin/gsid run-startup-tasks
+    start gsid