Improve VDC's logging on failure.

Also refactor.
Bug: 36029169
Test: ensure that a command fails, check logs for failure.

Change-Id: I1dece2982f762f4522e17d45b5f04af104b95861
diff --git a/vdc.cpp b/vdc.cpp
index 4e0c3a9..761d035 100644
--- a/vdc.cpp
+++ b/vdc.cpp
@@ -34,6 +34,7 @@
 #include <android-base/logging.h>
 #include <android-base/stringprintf.h>
 #include <binder/IServiceManager.h>
+#include <binder/Status.h>
 
 #include <private/android_filesystem_config.h>
 
@@ -54,13 +55,13 @@
     return res;
 }
 
-int main(int argc, char **argv) {
-    int sock;
-    int wait;
-    char *progname;
+static void checkStatus(android::binder::Status status) {
+    if (status.isOk()) return;
+    LOG(ERROR) << "Failed: " << status.toString8().string();
+    exit(ENOTTY);
+}
 
-    progname = argv[0];
-
+int main(int argc, char** argv) {
     setenv("ANDROID_LOG_TAGS", "*:v", 1);
     if (getppid() == 1) {
         // If init is calling us then it's during boot and we should log to kmsg
@@ -68,21 +69,17 @@
     } else {
         android::base::InitLogging(argv, &android::base::StderrLogger);
     }
+    std::vector<std::string> args(argv + 1, argv + argc);
 
-    wait = argc > 1 && strcmp(argv[1], "--wait") == 0;
-    if (wait) {
-        argv++;
-        argc--;
+    if (args.size() > 0 && args[0] == "--wait") {
+        // Just ignore the --wait flag
+        args.erase(args.begin());
     }
 
-    if (argc < 3) {
-        usage(progname);
+    if (args.size() < 2) {
+        usage(argv[0]);
         exit(5);
     }
-
-    std::string arg1 = argv[1];
-    std::string arg2 = argv[2];
-
     android::sp<android::IBinder> binder = getServiceAggressive();
     if (!binder) {
         LOG(ERROR) << "Failed to obtain vold Binder";
@@ -90,25 +87,26 @@
     }
     auto vold = android::interface_cast<android::os::IVold>(binder);
 
-    if (arg1 == "cryptfs" && arg2 == "enablefilecrypto") {
-        exit(vold->fbeEnable().isOk() ? 0 : ENOTTY);
-    } else if (arg1 == "cryptfs" && arg2 == "init_user0") {
-        exit(vold->initUser0().isOk() ? 0 : ENOTTY);
-    } else if (arg1 == "cryptfs" && arg2 == "enablecrypto") {
+    if (args[0] == "cryptfs" && args[1] == "enablefilecrypto") {
+        checkStatus(vold->fbeEnable());
+    } else if (args[0] == "cryptfs" && args[1] == "init_user0") {
+        checkStatus(vold->initUser0());
+    } else if (args[0] == "cryptfs" && args[1] == "enablecrypto") {
         int passwordType = android::os::IVold::PASSWORD_TYPE_DEFAULT;
         int encryptionFlags = android::os::IVold::ENCRYPTION_FLAG_IN_PLACE
                 | android::os::IVold::ENCRYPTION_FLAG_NO_UI;
-        exit(vold->fdeEnable(passwordType, "", encryptionFlags).isOk() ? 0 : ENOTTY);
-    } else if (arg1 == "cryptfs" && arg2 == "mountdefaultencrypted") {
-        exit(vold->mountDefaultEncrypted().isOk() ? 0 : ENOTTY);
-    } else if (arg1 == "volume" && arg2 == "shutdown") {
-        exit(vold->shutdown().isOk() ? 0 : ENOTTY);
+        checkStatus(vold->fdeEnable(passwordType, "", encryptionFlags));
+    } else if (args[0] == "cryptfs" && args[1] == "mountdefaultencrypted") {
+        checkStatus(vold->mountDefaultEncrypted());
+    } else if (args[0] == "volume" && args[1] == "shutdown") {
+        checkStatus(vold->shutdown());
     } else {
         LOG(ERROR) << "Raw commands are no longer supported";
         exit(EINVAL);
     }
+    return 0;
 }
 
 static void usage(char *progname) {
-    LOG(INFO) << "Usage: " << progname << " [--wait] <monitor>|<cmd> [arg1] [arg2...]";
+    LOG(INFO) << "Usage: " << progname << " [--wait] <system> <subcommand> [args...]";
 }