vdc.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (C) 2008 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <poll.h>
  19. #include <signal.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <sys/select.h>
  25. #include <sys/time.h>
  26. #include <sys/types.h>
  27. #include <sys/un.h>
  28. #include "android/os/IVold.h"
  29. #include <android-base/logging.h>
  30. #include <android-base/parseint.h>
  31. #include <android-base/stringprintf.h>
  32. #include <binder/IServiceManager.h>
  33. #include <binder/Status.h>
  34. #include <private/android_filesystem_config.h>
  35. static void usage(char* progname);
  36. static android::sp<android::IBinder> getServiceAggressive() {
  37. android::sp<android::IBinder> res;
  38. auto sm = android::defaultServiceManager();
  39. auto name = android::String16("vold");
  40. for (int i = 0; i < 5000; i++) {
  41. res = sm->checkService(name);
  42. if (res) {
  43. LOG(VERBOSE) << "Waited " << (i * 10) << "ms for vold";
  44. break;
  45. }
  46. usleep(10000); // 10ms
  47. }
  48. return res;
  49. }
  50. static void checkStatus(android::binder::Status status) {
  51. if (status.isOk()) return;
  52. LOG(ERROR) << "Failed: " << status.toString8().string();
  53. exit(ENOTTY);
  54. }
  55. int main(int argc, char** argv) {
  56. setenv("ANDROID_LOG_TAGS", "*:v", 1);
  57. if (getppid() == 1) {
  58. // If init is calling us then it's during boot and we should log to kmsg
  59. android::base::InitLogging(argv, &android::base::KernelLogger);
  60. } else {
  61. android::base::InitLogging(argv, &android::base::StderrLogger);
  62. }
  63. std::vector<std::string> args(argv + 1, argv + argc);
  64. if (args.size() > 0 && args[0] == "--wait") {
  65. // Just ignore the --wait flag
  66. args.erase(args.begin());
  67. }
  68. if (args.size() < 2) {
  69. usage(argv[0]);
  70. exit(5);
  71. }
  72. android::sp<android::IBinder> binder = getServiceAggressive();
  73. if (!binder) {
  74. LOG(ERROR) << "Failed to obtain vold Binder";
  75. exit(EINVAL);
  76. }
  77. auto vold = android::interface_cast<android::os::IVold>(binder);
  78. if (args[0] == "cryptfs" && args[1] == "enablefilecrypto") {
  79. checkStatus(vold->fbeEnable());
  80. } else if (args[0] == "cryptfs" && args[1] == "init_user0") {
  81. checkStatus(vold->initUser0());
  82. } else if (args[0] == "cryptfs" && args[1] == "enablecrypto") {
  83. int passwordType = android::os::IVold::PASSWORD_TYPE_DEFAULT;
  84. int encryptionFlags = android::os::IVold::ENCRYPTION_FLAG_NO_UI;
  85. checkStatus(vold->fdeEnable(passwordType, "", encryptionFlags));
  86. } else if (args[0] == "cryptfs" && args[1] == "mountdefaultencrypted") {
  87. checkStatus(vold->mountDefaultEncrypted());
  88. } else if (args[0] == "volume" && args[1] == "shutdown") {
  89. checkStatus(vold->shutdown());
  90. } else if (args[0] == "cryptfs" && args[1] == "checkEncryption" && args.size() == 3) {
  91. checkStatus(vold->checkEncryption(args[2]));
  92. } else if (args[0] == "cryptfs" && args[1] == "mountFstab" && args.size() == 4) {
  93. checkStatus(vold->mountFstab(args[2], args[3]));
  94. } else if (args[0] == "cryptfs" && args[1] == "encryptFstab" && args.size() == 4) {
  95. checkStatus(vold->encryptFstab(args[2], args[3]));
  96. } else if (args[0] == "checkpoint" && args[1] == "supportsCheckpoint" && args.size() == 2) {
  97. bool supported = false;
  98. checkStatus(vold->supportsCheckpoint(&supported));
  99. return supported ? 1 : 0;
  100. } else if (args[0] == "checkpoint" && args[1] == "supportsBlockCheckpoint" && args.size() == 2) {
  101. bool supported = false;
  102. checkStatus(vold->supportsBlockCheckpoint(&supported));
  103. return supported ? 1 : 0;
  104. } else if (args[0] == "checkpoint" && args[1] == "supportsFileCheckpoint" && args.size() == 2) {
  105. bool supported = false;
  106. checkStatus(vold->supportsFileCheckpoint(&supported));
  107. return supported ? 1 : 0;
  108. } else if (args[0] == "checkpoint" && args[1] == "startCheckpoint" && args.size() == 3) {
  109. int retry;
  110. if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL);
  111. checkStatus(vold->startCheckpoint(retry));
  112. } else if (args[0] == "checkpoint" && args[1] == "needsCheckpoint" && args.size() == 2) {
  113. bool enabled = false;
  114. checkStatus(vold->needsCheckpoint(&enabled));
  115. return enabled ? 1 : 0;
  116. } else if (args[0] == "checkpoint" && args[1] == "needsRollback" && args.size() == 2) {
  117. bool enabled = false;
  118. checkStatus(vold->needsRollback(&enabled));
  119. return enabled ? 1 : 0;
  120. } else if (args[0] == "checkpoint" && args[1] == "commitChanges" && args.size() == 2) {
  121. checkStatus(vold->commitChanges());
  122. } else if (args[0] == "checkpoint" && args[1] == "prepareCheckpoint" && args.size() == 2) {
  123. checkStatus(vold->prepareCheckpoint());
  124. } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpoint" && args.size() == 3) {
  125. checkStatus(vold->restoreCheckpoint(args[2]));
  126. } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpointPart" && args.size() == 4) {
  127. int count;
  128. if (!android::base::ParseInt(args[3], &count)) exit(EINVAL);
  129. checkStatus(vold->restoreCheckpointPart(args[2], count));
  130. } else if (args[0] == "checkpoint" && args[1] == "markBootAttempt" && args.size() == 2) {
  131. checkStatus(vold->markBootAttempt());
  132. } else if (args[0] == "checkpoint" && args[1] == "abortChanges" && args.size() == 4) {
  133. int retry;
  134. if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL);
  135. checkStatus(vold->abortChanges(args[2], retry != 0));
  136. } else {
  137. LOG(ERROR) << "Raw commands are no longer supported";
  138. exit(EINVAL);
  139. }
  140. return 0;
  141. }
  142. static void usage(char* progname) {
  143. LOG(INFO) << "Usage: " << progname << " [--wait] <system> <subcommand> [args...]";
  144. }