Improve VDC's logging on failure.

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

Change-Id: I1dece2982f762f4522e17d45b5f04af104b95861
This commit is contained in:
Paul Crowley 2017-10-27 13:37:24 -07:00
parent 26a53888a4
commit 2d64b91823

52
vdc.cpp
View file

@ -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 @@ static android::sp<android::IBinder> getServiceAggressive() {
return res;
}
int main(int argc, char **argv) {
int sock;
int wait;
char *progname;
progname = argv[0];
static void checkStatus(android::binder::Status status) {
if (status.isOk()) return;
LOG(ERROR) << "Failed: " << status.toString8().string();
exit(ENOTTY);
}
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 @@ int main(int argc, char **argv) {
} 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 @@ int main(int argc, char **argv) {
}
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...]";
}