Add socket communication between recovery and minadbd

This cl adds a socket pair to support the communication between recovery
and minadbd. Therefore, minadbd will be able to issue multiple commands
to recovery and get back the status of each command.

This cl also switches the adb sideload from the recovery menu to use
this protocol; and moves minadbd to a separate binary.

Bug: 130166585
Test: sideload a package
Change-Id: I80d36d5c4e6fe1ae3ea23640907bc50c0dc0d482
diff --git a/minadbd/minadbd_services.cpp b/minadbd/minadbd_services.cpp
index 6fe5c79..79e6fc4 100644
--- a/minadbd/minadbd_services.cpp
+++ b/minadbd/minadbd_services.cpp
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include "minadbd_services.h"
+
 #include <errno.h>
 #include <inttypes.h>
 #include <stdio.h>
@@ -27,38 +29,95 @@
 #include <string_view>
 #include <thread>
 
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/memory.h>
+#include <android-base/stringprintf.h>
+#include <android-base/strings.h>
+
 #include "adb.h"
 #include "adb_unique_fd.h"
 #include "fdevent.h"
 #include "fuse_adb_provider.h"
 #include "fuse_sideload.h"
+#include "minadbd_types.h"
 #include "services.h"
 #include "sysdeps.h"
 
+static int minadbd_socket = -1;
+void SetMinadbdSocketFd(int socket_fd) {
+  minadbd_socket = socket_fd;
+}
+
+static bool WriteCommandToFd(MinadbdCommands cmd, int fd) {
+  char message[kMinadbdMessageSize];
+  memcpy(message, kMinadbdCommandPrefix, strlen(kMinadbdStatusPrefix));
+  android::base::put_unaligned(message + strlen(kMinadbdStatusPrefix), cmd);
+
+  if (!android::base::WriteFully(fd, message, kMinadbdMessageSize)) {
+    PLOG(ERROR) << "Failed to write message " << message;
+    return false;
+  }
+  return true;
+}
+
+// Blocks and reads the command status from |fd|. Returns false if the received message has a
+// format error.
+static bool WaitForCommandStatus(int fd, MinadbdCommandStatus* status) {
+  char buffer[kMinadbdMessageSize];
+  if (!android::base::ReadFully(fd, buffer, kMinadbdMessageSize)) {
+    PLOG(ERROR) << "Failed to response status from socket";
+    exit(kMinadbdSocketIOError);
+  }
+
+  std::string message(buffer, buffer + kMinadbdMessageSize);
+  if (!android::base::StartsWith(message, kMinadbdStatusPrefix)) {
+    LOG(ERROR) << "Failed to parse status in " << message;
+    return false;
+  }
+
+  *status = android::base::get_unaligned<MinadbdCommandStatus>(
+      message.substr(strlen(kMinadbdStatusPrefix)).c_str());
+  return true;
+}
+
 static void sideload_host_service(unique_fd sfd, const std::string& args) {
   int64_t file_size;
   int block_size;
   if ((sscanf(args.c_str(), "%" SCNd64 ":%d", &file_size, &block_size) != 2) || file_size <= 0 ||
       block_size <= 0) {
-    printf("bad sideload-host arguments: %s\n", args.c_str());
-    exit(1);
+    LOG(ERROR) << "bad sideload-host arguments: " << args;
+    exit(kMinadbdPackageSizeError);
   }
 
-  printf("sideload-host file size %" PRId64 " block size %d\n", file_size, block_size);
+  LOG(INFO) << "sideload-host file size " << file_size << ", block size " << block_size;
+
+  if (!WriteCommandToFd(MinadbdCommands::kInstall, minadbd_socket)) {
+    exit(kMinadbdSocketIOError);
+  }
 
   auto adb_data_reader =
       std::make_unique<FuseAdbDataProvider>(std::move(sfd), file_size, block_size);
-  int result = run_fuse_sideload(std::move(adb_data_reader));
+  if (int result = run_fuse_sideload(std::move(adb_data_reader)); result != 0) {
+    LOG(ERROR) << "Failed to start fuse";
+    exit(kMinadbdFuseStartError);
+  }
 
-  printf("sideload_host finished\n");
-  exit(result == 0 ? 0 : 1);
+  MinadbdCommandStatus status;
+  if (!WaitForCommandStatus(minadbd_socket, &status)) {
+    exit(kMinadbdMessageFormatError);
+  }
+  LOG(INFO) << "Got command status: " << static_cast<unsigned int>(status);
+
+  LOG(INFO) << "sideload_host finished";
+  exit(kMinadbdSuccess);
 }
 
 unique_fd daemon_service_to_fd(std::string_view name, atransport* /* transport */) {
   if (name.starts_with("sideload:")) {
     // This exit status causes recovery to print a special error message saying to use a newer adb
     // (that supports sideload-host).
-    exit(3);
+    exit(kMinadbdAdbVersionError);
   } else if (name.starts_with("sideload-host:")) {
     std::string arg(name.substr(strlen("sideload-host:")));
     return create_service_thread("sideload-host",