blob: e89244f13be0dfeb258fe637bc54095991748778 [file] [log] [blame]
Tom Cherry16fad422017-08-04 15:59:03 -07001/*
2 * Copyright (C) 2017 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#include "persistent_properties.h"
18
19#include <dirent.h>
20#include <fcntl.h>
21#include <sys/stat.h>
22#include <sys/system_properties.h>
23#include <sys/types.h>
24
25#include <memory>
26
27#include <android-base/file.h>
28#include <android-base/logging.h>
29#include <android-base/strings.h>
30#include <android-base/unique_fd.h>
31
32#include "util.h"
33
Tom Cherry97437a72019-12-06 11:16:10 -080034using android::base::Dirname;
Tom Cherry16fad422017-08-04 15:59:03 -070035using android::base::ReadFdToString;
36using android::base::StartsWith;
Tom Cherry16fad422017-08-04 15:59:03 -070037using android::base::unique_fd;
Tom Cherry97437a72019-12-06 11:16:10 -080038using android::base::WriteStringToFd;
Tom Cherry16fad422017-08-04 15:59:03 -070039
40namespace android {
41namespace init {
42
43std::string persistent_property_filename = "/data/property/persistent_properties";
44
45namespace {
46
Tom Cherry16fad422017-08-04 15:59:03 -070047constexpr const char kLegacyPersistentPropertyDir[] = "/data/property";
48
Tom Cherrya97faba2017-09-15 15:44:04 -070049Result<PersistentProperties> LoadLegacyPersistentProperties() {
Tom Cherry16fad422017-08-04 15:59:03 -070050 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(kLegacyPersistentPropertyDir), closedir);
51 if (!dir) {
52 return ErrnoError() << "Unable to open persistent property directory \""
53 << kLegacyPersistentPropertyDir << "\"";
54 }
55
Tom Cherrya97faba2017-09-15 15:44:04 -070056 PersistentProperties persistent_properties;
Tom Cherry16fad422017-08-04 15:59:03 -070057 dirent* entry;
58 while ((entry = readdir(dir.get())) != nullptr) {
59 if (!StartsWith(entry->d_name, "persist.")) {
60 continue;
61 }
62 if (entry->d_type != DT_REG) {
63 continue;
64 }
65
Tom Cherry247ffbf2019-07-08 15:09:36 -070066 unique_fd fd(openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
Tom Cherry16fad422017-08-04 15:59:03 -070067 if (fd == -1) {
68 PLOG(ERROR) << "Unable to open persistent property file \"" << entry->d_name << "\"";
69 continue;
70 }
71
72 struct stat sb;
Bart Van Asscheaee2ec82022-12-02 18:48:15 -080073 if (fstat(fd.get(), &sb) == -1) {
Tom Cherry16fad422017-08-04 15:59:03 -070074 PLOG(ERROR) << "fstat on property file \"" << entry->d_name << "\" failed";
75 continue;
76 }
77
78 // File must not be accessible to others, be owned by root/root, and
79 // not be a hard link to any other file.
80 if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || sb.st_uid != 0 || sb.st_gid != 0 ||
81 sb.st_nlink != 1) {
82 PLOG(ERROR) << "skipping insecure property file " << entry->d_name
83 << " (uid=" << sb.st_uid << " gid=" << sb.st_gid << " nlink=" << sb.st_nlink
84 << " mode=" << std::oct << sb.st_mode << ")";
85 continue;
86 }
87
88 std::string value;
89 if (ReadFdToString(fd, &value)) {
Tom Cherrya97faba2017-09-15 15:44:04 -070090 AddPersistentProperty(entry->d_name, value, &persistent_properties);
Tom Cherry16fad422017-08-04 15:59:03 -070091 } else {
92 PLOG(ERROR) << "Unable to read persistent property file " << entry->d_name;
93 }
94 }
95 return persistent_properties;
96}
97
98void RemoveLegacyPersistentPropertyFiles() {
99 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(kLegacyPersistentPropertyDir), closedir);
100 if (!dir) {
101 PLOG(ERROR) << "Unable to open persistent property directory \""
102 << kLegacyPersistentPropertyDir << "\"";
103 return;
104 }
105
106 dirent* entry;
107 while ((entry = readdir(dir.get())) != nullptr) {
108 if (!StartsWith(entry->d_name, "persist.")) {
109 continue;
110 }
111 if (entry->d_type != DT_REG) {
112 continue;
113 }
114 unlinkat(dirfd(dir.get()), entry->d_name, 0);
115 }
116}
117
Tom Cherrya97faba2017-09-15 15:44:04 -0700118Result<std::string> ReadPersistentPropertyFile() {
Tom Cherry16fad422017-08-04 15:59:03 -0700119 const std::string temp_filename = persistent_property_filename + ".tmp";
120 if (access(temp_filename.c_str(), F_OK) == 0) {
121 LOG(INFO)
122 << "Found temporary property file while attempting to persistent system properties"
123 " a previous persistent property write may have failed";
124 unlink(temp_filename.c_str());
125 }
126 auto file_contents = ReadFile(persistent_property_filename);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900127 if (!file_contents.ok()) {
Tom Cherry16fad422017-08-04 15:59:03 -0700128 return Error() << "Unable to read persistent property file: " << file_contents.error();
129 }
Tom Cherrya97faba2017-09-15 15:44:04 -0700130 return *file_contents;
131}
132
Paul Crowleyf7c74692022-08-25 11:38:27 -0700133Result<PersistentProperties> ParsePersistentPropertyFile(const std::string& file_contents) {
134 PersistentProperties persistent_properties;
135 if (!persistent_properties.ParseFromString(file_contents)) {
136 return Error() << "Unable to parse persistent property file: Could not parse protobuf";
137 }
138 for (auto& prop : persistent_properties.properties()) {
Dennis Shen678d2682023-09-18 19:52:52 +0000139 if (!StartsWith(prop.name(), "persist.") && !StartsWith(prop.name(), "next_boot.")) {
Paul Crowleyf7c74692022-08-25 11:38:27 -0700140 return Error() << "Unable to load persistent property file: property '" << prop.name()
Dennis Shen678d2682023-09-18 19:52:52 +0000141 << "' doesn't start with 'persist.' or 'next_boot.'";
Paul Crowleyf7c74692022-08-25 11:38:27 -0700142 }
143 }
144 return persistent_properties;
145}
146
Tom Cherrya97faba2017-09-15 15:44:04 -0700147} // namespace
148
Dennis Shen678d2682023-09-18 19:52:52 +0000149void AddPersistentProperty(const std::string& name, const std::string& value,
150 PersistentProperties* persistent_properties) {
151 auto persistent_property_record = persistent_properties->add_properties();
152 persistent_property_record->set_name(name);
153 persistent_property_record->set_value(value);
154}
155
Tom Cherrya97faba2017-09-15 15:44:04 -0700156Result<PersistentProperties> LoadPersistentPropertyFile() {
157 auto file_contents = ReadPersistentPropertyFile();
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900158 if (!file_contents.ok()) return file_contents.error();
Tom Cherrya97faba2017-09-15 15:44:04 -0700159
Paul Crowleyf7c74692022-08-25 11:38:27 -0700160 auto persistent_properties = ParsePersistentPropertyFile(*file_contents);
161 if (!persistent_properties.ok()) {
162 // If the file cannot be parsed in either format, then we don't have any recovery
163 // mechanisms, so we delete it to allow for future writes to take place successfully.
164 unlink(persistent_property_filename.c_str());
165 }
166 return persistent_properties;
Tom Cherry16fad422017-08-04 15:59:03 -0700167}
168
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700169Result<void> WritePersistentPropertyFile(const PersistentProperties& persistent_properties) {
Tom Cherry16fad422017-08-04 15:59:03 -0700170 const std::string temp_filename = persistent_property_filename + ".tmp";
171 unique_fd fd(TEMP_FAILURE_RETRY(
172 open(temp_filename.c_str(), O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0600)));
173 if (fd == -1) {
174 return ErrnoError() << "Could not open temporary properties file";
175 }
Tom Cherrya97faba2017-09-15 15:44:04 -0700176 std::string serialized_string;
177 if (!persistent_properties.SerializeToString(&serialized_string)) {
178 return Error() << "Unable to serialize properties";
179 }
180 if (!WriteStringToFd(serialized_string, fd)) {
Tom Cherry16fad422017-08-04 15:59:03 -0700181 return ErrnoError() << "Unable to write file contents";
182 }
Bart Van Asscheaee2ec82022-12-02 18:48:15 -0800183 fsync(fd.get());
Tom Cherry16fad422017-08-04 15:59:03 -0700184 fd.reset();
185
186 if (rename(temp_filename.c_str(), persistent_property_filename.c_str())) {
187 int saved_errno = errno;
188 unlink(temp_filename.c_str());
189 return Error(saved_errno) << "Unable to rename persistent property file";
190 }
Tom Cherry97437a72019-12-06 11:16:10 -0800191
192 // rename() is atomic with regards to the kernel's filesystem buffers, but the parent
193 // directories must be fsync()'ed otherwise, the rename is not necessarily written to storage.
194 // Note in this case, that the source and destination directories are the same, so only one
195 // fsync() is required.
196 auto dir = Dirname(persistent_property_filename);
Tom Cherryc99d60c2019-12-09 06:48:37 -0800197 auto dir_fd = unique_fd{open(dir.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC)};
Tom Cherry97437a72019-12-06 11:16:10 -0800198 if (dir_fd < 0) {
199 return ErrnoError() << "Unable to open persistent properties directory for fsync()";
200 }
Bart Van Asscheaee2ec82022-12-02 18:48:15 -0800201 fsync(dir_fd.get());
Tom Cherry97437a72019-12-06 11:16:10 -0800202
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700203 return {};
Tom Cherry16fad422017-08-04 15:59:03 -0700204}
205
Jiyong Parkc7230a12023-10-20 20:42:56 +0900206PersistentProperties LoadPersistentPropertiesFromMemory() {
207 PersistentProperties persistent_properties;
208 __system_property_foreach(
209 [](const prop_info* pi, void* cookie) {
210 __system_property_read_callback(
211 pi,
212 [](void* cookie, const char* name, const char* value, unsigned serial) {
213 if (StartsWith(name, "persist.")) {
214 auto properties = reinterpret_cast<PersistentProperties*>(cookie);
215 AddPersistentProperty(name, value, properties);
216 }
217 },
218 cookie);
219 },
220 &persistent_properties);
221 return persistent_properties;
222}
223
Tom Cherry16fad422017-08-04 15:59:03 -0700224// Persistent properties are not written often, so we rather not keep any data in memory and read
225// then rewrite the persistent property file for each update.
226void WritePersistentProperty(const std::string& name, const std::string& value) {
Tom Cherry9614e4d2017-09-19 10:31:27 -0700227 auto persistent_properties = LoadPersistentPropertyFile();
Tom Cherrya97faba2017-09-15 15:44:04 -0700228
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900229 if (!persistent_properties.ok()) {
Tom Cherry16fad422017-08-04 15:59:03 -0700230 LOG(ERROR) << "Recovering persistent properties from memory: "
Tom Cherry9614e4d2017-09-19 10:31:27 -0700231 << persistent_properties.error();
Tom Cherry16fad422017-08-04 15:59:03 -0700232 persistent_properties = LoadPersistentPropertiesFromMemory();
233 }
Tom Cherry9614e4d2017-09-19 10:31:27 -0700234 auto it = std::find_if(persistent_properties->mutable_properties()->begin(),
235 persistent_properties->mutable_properties()->end(),
Tom Cherrya97faba2017-09-15 15:44:04 -0700236 [&name](const auto& record) { return record.name() == name; });
Tom Cherry9614e4d2017-09-19 10:31:27 -0700237 if (it != persistent_properties->mutable_properties()->end()) {
Tom Cherrya97faba2017-09-15 15:44:04 -0700238 it->set_name(name);
239 it->set_value(value);
Tom Cherry16fad422017-08-04 15:59:03 -0700240 } else {
Tom Cherry9614e4d2017-09-19 10:31:27 -0700241 AddPersistentProperty(name, value, &persistent_properties.value());
Tom Cherry16fad422017-08-04 15:59:03 -0700242 }
243
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900244 if (auto result = WritePersistentPropertyFile(*persistent_properties); !result.ok()) {
Tom Cherry16fad422017-08-04 15:59:03 -0700245 LOG(ERROR) << "Could not store persistent property: " << result.error();
246 }
247}
248
Tom Cherrya97faba2017-09-15 15:44:04 -0700249PersistentProperties LoadPersistentProperties() {
Tom Cherry16fad422017-08-04 15:59:03 -0700250 auto persistent_properties = LoadPersistentPropertyFile();
251
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900252 if (!persistent_properties.ok()) {
Tom Cherry16fad422017-08-04 15:59:03 -0700253 LOG(ERROR) << "Could not load single persistent property file, trying legacy directory: "
254 << persistent_properties.error();
255 persistent_properties = LoadLegacyPersistentProperties();
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900256 if (!persistent_properties.ok()) {
Tom Cherry16fad422017-08-04 15:59:03 -0700257 LOG(ERROR) << "Unable to load legacy persistent properties: "
258 << persistent_properties.error();
259 return {};
260 }
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900261 if (auto result = WritePersistentPropertyFile(*persistent_properties); result.ok()) {
Tom Cherry16fad422017-08-04 15:59:03 -0700262 RemoveLegacyPersistentPropertyFiles();
263 } else {
264 LOG(ERROR) << "Unable to write single persistent property file: " << result.error();
265 // Fall through so that we still set the properties that we've read.
266 }
267 }
268
269 return *persistent_properties;
270}
271
272} // namespace init
273} // namespace android