main.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. #define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
  17. #include "NetlinkManager.h"
  18. #include "VoldNativeService.h"
  19. #include "VoldUtil.h"
  20. #include "VolumeManager.h"
  21. #include "cryptfs.h"
  22. #include "model/Disk.h"
  23. #include "sehandle.h"
  24. #include <android-base/logging.h>
  25. #include <android-base/properties.h>
  26. #include <android-base/stringprintf.h>
  27. #include <cutils/klog.h>
  28. #include <hidl/HidlTransportSupport.h>
  29. #include <utils/Trace.h>
  30. #include <dirent.h>
  31. #include <errno.h>
  32. #include <fcntl.h>
  33. #include <fs_mgr.h>
  34. #include <getopt.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <sys/stat.h>
  39. #include <sys/types.h>
  40. static int process_config(VolumeManager* vm, bool* has_adoptable, bool* has_quota,
  41. bool* has_reserved);
  42. static void coldboot(const char* path);
  43. static void parse_args(int argc, char** argv);
  44. struct selabel_handle* sehandle;
  45. using android::base::StringPrintf;
  46. using android::fs_mgr::ReadDefaultFstab;
  47. int main(int argc, char** argv) {
  48. atrace_set_tracing_enabled(false);
  49. setenv("ANDROID_LOG_TAGS", "*:d", 1); // Do not submit with verbose logs enabled
  50. android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
  51. LOG(INFO) << "Vold 3.0 (the awakening) firing up";
  52. ATRACE_BEGIN("main");
  53. LOG(DEBUG) << "Detected support for:"
  54. << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "")
  55. << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "")
  56. << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : "");
  57. VolumeManager* vm;
  58. NetlinkManager* nm;
  59. parse_args(argc, argv);
  60. sehandle = selinux_android_file_context_handle();
  61. if (sehandle) {
  62. selinux_android_set_sehandle(sehandle);
  63. }
  64. mkdir("/dev/block/vold", 0755);
  65. /* For when cryptfs checks and mounts an encrypted filesystem */
  66. klog_set_level(6);
  67. /* Create our singleton managers */
  68. if (!(vm = VolumeManager::Instance())) {
  69. LOG(ERROR) << "Unable to create VolumeManager";
  70. exit(1);
  71. }
  72. if (!(nm = NetlinkManager::Instance())) {
  73. LOG(ERROR) << "Unable to create NetlinkManager";
  74. exit(1);
  75. }
  76. if (android::base::GetBoolProperty("vold.debug", false)) {
  77. vm->setDebug(true);
  78. }
  79. if (vm->start()) {
  80. PLOG(ERROR) << "Unable to start VolumeManager";
  81. exit(1);
  82. }
  83. bool has_adoptable;
  84. bool has_quota;
  85. bool has_reserved;
  86. if (process_config(vm, &has_adoptable, &has_quota, &has_reserved)) {
  87. PLOG(ERROR) << "Error reading configuration... continuing anyways";
  88. }
  89. android::hardware::configureRpcThreadpool(1, false /* callerWillJoin */);
  90. ATRACE_BEGIN("VoldNativeService::start");
  91. if (android::vold::VoldNativeService::start() != android::OK) {
  92. LOG(ERROR) << "Unable to start VoldNativeService";
  93. exit(1);
  94. }
  95. ATRACE_END();
  96. LOG(DEBUG) << "VoldNativeService::start() completed OK";
  97. ATRACE_BEGIN("NetlinkManager::start");
  98. if (nm->start()) {
  99. PLOG(ERROR) << "Unable to start NetlinkManager";
  100. exit(1);
  101. }
  102. ATRACE_END();
  103. // This call should go after listeners are started to avoid
  104. // a deadlock between vold and init (see b/34278978 for details)
  105. android::base::SetProperty("vold.has_adoptable", has_adoptable ? "1" : "0");
  106. android::base::SetProperty("vold.has_quota", has_quota ? "1" : "0");
  107. android::base::SetProperty("vold.has_reserved", has_reserved ? "1" : "0");
  108. // Do coldboot here so it won't block booting,
  109. // also the cold boot is needed in case we have flash drive
  110. // connected before Vold launched
  111. coldboot("/sys/block");
  112. ATRACE_END();
  113. android::IPCThreadState::self()->joinThreadPool();
  114. LOG(INFO) << "vold shutting down";
  115. exit(0);
  116. }
  117. static void parse_args(int argc, char** argv) {
  118. static struct option opts[] = {
  119. {"blkid_context", required_argument, 0, 'b'},
  120. {"blkid_untrusted_context", required_argument, 0, 'B'},
  121. {"fsck_context", required_argument, 0, 'f'},
  122. {"fsck_untrusted_context", required_argument, 0, 'F'},
  123. };
  124. int c;
  125. while ((c = getopt_long(argc, argv, "", opts, nullptr)) != -1) {
  126. switch (c) {
  127. // clang-format off
  128. case 'b': android::vold::sBlkidContext = optarg; break;
  129. case 'B': android::vold::sBlkidUntrustedContext = optarg; break;
  130. case 'f': android::vold::sFsckContext = optarg; break;
  131. case 'F': android::vold::sFsckUntrustedContext = optarg; break;
  132. // clang-format on
  133. }
  134. }
  135. CHECK(android::vold::sBlkidContext != nullptr);
  136. CHECK(android::vold::sBlkidUntrustedContext != nullptr);
  137. CHECK(android::vold::sFsckContext != nullptr);
  138. CHECK(android::vold::sFsckUntrustedContext != nullptr);
  139. }
  140. static void do_coldboot(DIR* d, int lvl) {
  141. struct dirent* de;
  142. int dfd, fd;
  143. dfd = dirfd(d);
  144. fd = openat(dfd, "uevent", O_WRONLY | O_CLOEXEC);
  145. if (fd >= 0) {
  146. write(fd, "add\n", 4);
  147. close(fd);
  148. }
  149. while ((de = readdir(d))) {
  150. DIR* d2;
  151. if (de->d_name[0] == '.') continue;
  152. if (de->d_type != DT_DIR && lvl > 0) continue;
  153. fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
  154. if (fd < 0) continue;
  155. d2 = fdopendir(fd);
  156. if (d2 == 0)
  157. close(fd);
  158. else {
  159. do_coldboot(d2, lvl + 1);
  160. closedir(d2);
  161. }
  162. }
  163. }
  164. static void coldboot(const char* path) {
  165. ATRACE_NAME("coldboot");
  166. DIR* d = opendir(path);
  167. if (d) {
  168. do_coldboot(d, 0);
  169. closedir(d);
  170. }
  171. }
  172. static int process_config(VolumeManager* vm, bool* has_adoptable, bool* has_quota,
  173. bool* has_reserved) {
  174. ATRACE_NAME("process_config");
  175. if (!ReadDefaultFstab(&fstab_default)) {
  176. PLOG(ERROR) << "Failed to open default fstab";
  177. return -1;
  178. }
  179. /* Loop through entries looking for ones that vold manages */
  180. *has_adoptable = false;
  181. *has_quota = false;
  182. *has_reserved = false;
  183. for (auto& entry : fstab_default) {
  184. if (entry.fs_mgr_flags.quota) {
  185. *has_quota = true;
  186. }
  187. if (entry.reserved_size > 0) {
  188. *has_reserved = true;
  189. }
  190. /* Make sure logical partitions have an updated blk_device. */
  191. if (entry.fs_mgr_flags.logical && !fs_mgr_update_logical_partition(&entry)) {
  192. PLOG(FATAL) << "could not find logical partition " << entry.blk_device;
  193. }
  194. if (entry.fs_mgr_flags.vold_managed) {
  195. if (entry.fs_mgr_flags.nonremovable) {
  196. LOG(WARNING) << "nonremovable no longer supported; ignoring volume";
  197. continue;
  198. }
  199. std::string sysPattern(entry.blk_device);
  200. std::string nickname(entry.label);
  201. int flags = 0;
  202. if (entry.is_encryptable()) {
  203. flags |= android::vold::Disk::Flags::kAdoptable;
  204. *has_adoptable = true;
  205. }
  206. if (entry.fs_mgr_flags.no_emulated_sd ||
  207. android::base::GetBoolProperty("vold.debug.default_primary", false)) {
  208. flags |= android::vold::Disk::Flags::kDefaultPrimary;
  209. }
  210. vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
  211. new VolumeManager::DiskSource(sysPattern, nickname, flags)));
  212. }
  213. }
  214. return 0;
  215. }