blob: 5999548d2b33856f0461f38913a554f17a40c2ce [file] [log] [blame]
Andreas Gampebec63582015-11-20 19:26:51 -08001/*
2 * Copyright (C) 2016 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
17#ifndef ART_RUNTIME_GC_SPACE_IMAGE_SPACE_FS_H_
18#define ART_RUNTIME_GC_SPACE_IMAGE_SPACE_FS_H_
19
20#include <dirent.h>
21#include <dlfcn.h>
22
Andreas Gampe46ee31b2016-12-14 10:11:49 -080023#include "android-base/stringprintf.h"
24
Andreas Gampebec63582015-11-20 19:26:51 -080025#include "base/logging.h"
26#include "base/macros.h"
Andreas Gampebec63582015-11-20 19:26:51 -080027#include "base/unix_file/fd_file.h"
28#include "globals.h"
29#include "os.h"
Andreas Gampea1425a12016-03-11 17:44:04 -080030#include "runtime.h"
Andreas Gampebec63582015-11-20 19:26:51 -080031#include "utils.h"
32
33namespace art {
34namespace gc {
35namespace space {
36
37// This file contains helper code for ImageSpace. It has most of the file-system
38// related code, including handling A/B OTA.
39
40namespace impl {
41
42// Delete the directory and its (regular or link) contents. If the recurse flag is true, delete
43// sub-directories recursively.
44static void DeleteDirectoryContents(const std::string& dir, bool recurse) {
45 if (!OS::DirectoryExists(dir.c_str())) {
46 return;
47 }
48 DIR* c_dir = opendir(dir.c_str());
49 if (c_dir == nullptr) {
50 PLOG(WARNING) << "Unable to open " << dir << " to delete it's contents";
51 return;
52 }
53
54 for (struct dirent* de = readdir(c_dir); de != nullptr; de = readdir(c_dir)) {
55 const char* name = de->d_name;
56 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
57 continue;
58 }
59 // We only want to delete regular files and symbolic links.
Andreas Gampe46ee31b2016-12-14 10:11:49 -080060 std::string file = android::base::StringPrintf("%s/%s", dir.c_str(), name);
Andreas Gampebec63582015-11-20 19:26:51 -080061 if (de->d_type != DT_REG && de->d_type != DT_LNK) {
62 if (de->d_type == DT_DIR) {
63 if (recurse) {
64 DeleteDirectoryContents(file, recurse);
65 // Try to rmdir the directory.
Vladimir Marko66fdcbd2016-04-05 14:19:08 +010066 if (rmdir(file.c_str()) != 0) {
Andreas Gampebec63582015-11-20 19:26:51 -080067 PLOG(ERROR) << "Unable to rmdir " << file;
68 }
69 }
70 } else {
71 LOG(WARNING) << "Unexpected file type of " << std::hex << de->d_type << " encountered.";
72 }
73 } else {
74 // Try to unlink the file.
Vladimir Marko66fdcbd2016-04-05 14:19:08 +010075 if (unlink(file.c_str()) != 0) {
Andreas Gampebec63582015-11-20 19:26:51 -080076 PLOG(ERROR) << "Unable to unlink " << file;
77 }
78 }
79 }
Vladimir Marko66fdcbd2016-04-05 14:19:08 +010080 CHECK_EQ(0, closedir(c_dir)) << "Unable to close directory.";
Andreas Gampebec63582015-11-20 19:26:51 -080081}
82
Andreas Gampebec63582015-11-20 19:26:51 -080083} // namespace impl
84
85
86// We are relocating or generating the core image. We should get rid of everything. It is all
87// out-of-date. We also don't really care if this fails since it is just a convenience.
88// Adapted from prune_dex_cache(const char* subdir) in frameworks/native/cmds/installd/commands.c
89// Note this should only be used during first boot.
90static void PruneDalvikCache(InstructionSet isa) {
91 CHECK_NE(isa, kNone);
92 // Prune the base /data/dalvik-cache.
Richard Uhler55b58b62016-08-12 09:05:13 -070093 // Note: GetDalvikCache may return the empty string if the directory doesn't
94 // exist. It is safe to pass "" to DeleteDirectoryContents, so this is okay.
95 impl::DeleteDirectoryContents(GetDalvikCache("."), false);
Andreas Gampebec63582015-11-20 19:26:51 -080096 // Prune /data/dalvik-cache/<isa>.
Richard Uhler55b58b62016-08-12 09:05:13 -070097 impl::DeleteDirectoryContents(GetDalvikCache(GetInstructionSetString(isa)), false);
Andreas Gampea1425a12016-03-11 17:44:04 -080098
99 // Be defensive. There should be a runtime created here, but this may be called in a test.
100 if (Runtime::Current() != nullptr) {
101 Runtime::Current()->SetPrunedDalvikCache(true);
102 }
Andreas Gampebec63582015-11-20 19:26:51 -0800103}
104
105// We write out an empty file to the zygote's ISA specific cache dir at the start of
106// every zygote boot and delete it when the boot completes. If we find a file already
107// present, it usually means the boot didn't complete. We wipe the entire dalvik
108// cache if that's the case.
109static void MarkZygoteStart(const InstructionSet isa, const uint32_t max_failed_boots) {
Richard Uhler55b58b62016-08-12 09:05:13 -0700110 const std::string isa_subdir = GetDalvikCache(GetInstructionSetString(isa));
111 CHECK(!isa_subdir.empty()) << "Dalvik cache not found";
Andreas Gampebec63582015-11-20 19:26:51 -0800112 const std::string boot_marker = isa_subdir + "/.booting";
113 const char* file_name = boot_marker.c_str();
114
115 uint32_t num_failed_boots = 0;
116 std::unique_ptr<File> file(OS::OpenFileReadWrite(file_name));
117 if (file.get() == nullptr) {
118 file.reset(OS::CreateEmptyFile(file_name));
119
120 if (file.get() == nullptr) {
Andreas Gamped26b6ad2016-08-09 20:19:18 -0700121 int saved_errno = errno;
Andreas Gampebec63582015-11-20 19:26:51 -0800122 PLOG(WARNING) << "Failed to create boot marker.";
Andreas Gamped26b6ad2016-08-09 20:19:18 -0700123 if (saved_errno != ENOSPC) {
124 return;
125 }
126
127 LOG(WARNING) << "Pruning dalvik cache because of low-memory situation.";
128 impl::DeleteDirectoryContents(isa_subdir, false);
129
130 // Try once more.
131 file.reset(OS::OpenFileReadWrite(file_name));
132 if (file == nullptr) {
133 PLOG(WARNING) << "Failed to create boot marker.";
134 return;
135 }
Andreas Gampebec63582015-11-20 19:26:51 -0800136 }
137 } else {
138 if (!file->ReadFully(&num_failed_boots, sizeof(num_failed_boots))) {
139 PLOG(WARNING) << "Failed to read boot marker.";
140 file->Erase();
141 return;
142 }
143 }
144
145 if (max_failed_boots != 0 && num_failed_boots > max_failed_boots) {
146 LOG(WARNING) << "Incomplete boot detected. Pruning dalvik cache";
147 impl::DeleteDirectoryContents(isa_subdir, false);
148 }
149
150 ++num_failed_boots;
151 VLOG(startup) << "Number of failed boots on : " << boot_marker << " = " << num_failed_boots;
152
153 if (lseek(file->Fd(), 0, SEEK_SET) == -1) {
154 PLOG(WARNING) << "Failed to write boot marker.";
155 file->Erase();
156 return;
157 }
158
159 if (!file->WriteFully(&num_failed_boots, sizeof(num_failed_boots))) {
160 PLOG(WARNING) << "Failed to write boot marker.";
161 file->Erase();
162 return;
163 }
164
165 if (file->FlushCloseOrErase() != 0) {
166 PLOG(WARNING) << "Failed to flush boot marker.";
167 }
168}
169
Andreas Gampebec63582015-11-20 19:26:51 -0800170} // namespace space
171} // namespace gc
172} // namespace art
173
174#endif // ART_RUNTIME_GC_SPACE_IMAGE_SPACE_FS_H_