blob: 4455b2eb1e9c94e4f25c303455e5b01930727bbd [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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
Tom Cherry3f5eaae52017-04-06 16:30:22 -070017#include "util.h"
18
Mark Salyzyn62767fe2016-10-27 07:45:34 -070019#include <ctype.h>
20#include <errno.h>
21#include <fcntl.h>
Mark Salyzyn62767fe2016-10-27 07:45:34 -070022#include <pwd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023#include <stdarg.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#include <stdio.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070025#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026#include <string.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070027#include <sys/socket.h>
28#include <sys/un.h>
Colin Cross504bc512010-04-13 19:35:09 -070029#include <time.h>
Mark Salyzyn62767fe2016-10-27 07:45:34 -070030#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031
Elliott Hughes290a2282016-11-14 17:08:47 -080032#include <thread>
33
Elliott Hughes4f713192015-12-04 22:00:26 -080034#include <android-base/file.h>
Elliott Hughesf86b5a62016-06-24 15:12:21 -070035#include <android-base/logging.h>
Mark Salyzyn62767fe2016-10-27 07:45:34 -070036#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080037#include <android-base/strings.h>
Mark Salyzyndb691072016-11-07 10:16:53 -080038#include <android-base/unique_fd.h>
Elliott Hughes331cf2f2016-11-29 19:20:58 +000039#include <cutils/android_reboot.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include <cutils/sockets.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070041#include <selinux/android.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
Keun-young Park8d01f632017-03-13 11:54:47 -070043#include "reboot.h"
Tom Cherryde6bd502018-02-13 16:50:08 -080044
45#if defined(__ANDROID__)
46#include <android-base/properties.h>
47
Tom Cherry0c8d6d22017-08-10 12:22:44 -070048#include "selinux.h"
Tom Cherryde6bd502018-02-13 16:50:08 -080049#else
50#include "host_init_stubs.h"
51#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052
Tom Cherrye7656b72017-05-01 17:10:09 -070053#ifdef _INIT_INIT_H
54#error "Do not include init.h in files used by ueventd or watchdogd; it will expose init's globals"
55#endif
56
James Hawkinse78ea772017-03-24 11:43:02 -070057using android::base::boot_clock;
Tom Cherry517e1f12017-05-04 17:40:33 -070058using namespace std::literals::string_literals;
James Hawkinse78ea772017-03-24 11:43:02 -070059
Tom Cherry81f5d3e2017-06-22 12:53:17 -070060namespace android {
61namespace init {
62
Yu Ningc01022a2017-07-26 17:54:08 +080063const std::string kDefaultAndroidDtDir("/proc/device-tree/firmware/android/");
64
Tom Cherry517e1f12017-05-04 17:40:33 -070065// DecodeUid() - decodes and returns the given string, which can be either the
Tom Cherry11a3aee2017-08-03 12:54:07 -070066// numeric or name representation, into the integer uid or gid.
67Result<uid_t> DecodeUid(const std::string& name) {
Tom Cherry517e1f12017-05-04 17:40:33 -070068 if (isalpha(name[0])) {
69 passwd* pwd = getpwnam(name.c_str());
Tom Cherry11a3aee2017-08-03 12:54:07 -070070 if (!pwd) return ErrnoError() << "getpwnam failed";
71
72 return pwd->pw_uid;
William Roberts3792e6c2016-04-06 19:18:50 -070073 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074
75 errno = 0;
Tom Cherry517e1f12017-05-04 17:40:33 -070076 uid_t result = static_cast<uid_t>(strtoul(name.c_str(), 0, 0));
Tom Cherry11a3aee2017-08-03 12:54:07 -070077 if (errno) return ErrnoError() << "strtoul failed";
78
79 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080}
81
82/*
Mark Salyzynb066fcc2017-05-05 14:44:35 -070083 * CreateSocket - creates a Unix domain socket in ANDROID_SOCKET_DIR
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084 * ("/dev/socket") as dictated in init.rc. This socket is inherited by the
85 * daemon. We communicate the file descriptor's value via the environment
86 * variable ANDROID_SOCKET_ENV_PREFIX<name> ("ANDROID_SOCKET_foo").
87 */
Mark Salyzynb066fcc2017-05-05 14:44:35 -070088int CreateSocket(const char* name, int type, bool passcred, mode_t perm, uid_t uid, gid_t gid,
Tom Cherry0c8d6d22017-08-10 12:22:44 -070089 const char* socketcon) {
Nick Kralevich83ccb1c2015-11-23 16:26:42 -080090 if (socketcon) {
91 if (setsockcreatecon(socketcon) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070092 PLOG(ERROR) << "setsockcreatecon(\"" << socketcon << "\") failed";
Nick Kralevich83ccb1c2015-11-23 16:26:42 -080093 return -1;
94 }
95 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096
Mark Salyzyndb691072016-11-07 10:16:53 -080097 android::base::unique_fd fd(socket(PF_UNIX, type, 0));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098 if (fd < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070099 PLOG(ERROR) << "Failed to open socket '" << name << "'";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100 return -1;
101 }
102
Mark Salyzyndb691072016-11-07 10:16:53 -0800103 if (socketcon) setsockcreatecon(NULL);
Stephen Smalley8348d272013-05-13 12:37:04 -0400104
Mark Salyzyndb691072016-11-07 10:16:53 -0800105 struct sockaddr_un addr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106 memset(&addr, 0 , sizeof(addr));
107 addr.sun_family = AF_UNIX;
108 snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s",
109 name);
110
Mark Salyzyndb691072016-11-07 10:16:53 -0800111 if ((unlink(addr.sun_path) != 0) && (errno != ENOENT)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700112 PLOG(ERROR) << "Failed to unlink old socket '" << name << "'";
Mark Salyzyndb691072016-11-07 10:16:53 -0800113 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114 }
115
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700116 std::string secontext;
117 if (SelabelLookupFileContext(addr.sun_path, S_IFSOCK, &secontext) && !secontext.empty()) {
118 setfscreatecon(secontext.c_str());
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500119 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500120
Mark Salyzynb066fcc2017-05-05 14:44:35 -0700121 if (passcred) {
122 int on = 1;
123 if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on))) {
124 PLOG(ERROR) << "Failed to set SO_PASSCRED '" << name << "'";
125 return -1;
126 }
127 }
128
Mark Salyzyndb691072016-11-07 10:16:53 -0800129 int ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr));
130 int savederrno = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700132 if (!secontext.empty()) {
133 setfscreatecon(nullptr);
134 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500135
Nick Kralevich9bcfd642016-02-24 15:50:52 -0800136 if (ret) {
Mark Salyzyndb691072016-11-07 10:16:53 -0800137 errno = savederrno;
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700138 PLOG(ERROR) << "Failed to bind socket '" << name << "'";
Nick Kralevich9bcfd642016-02-24 15:50:52 -0800139 goto out_unlink;
140 }
141
Mark Salyzyndb691072016-11-07 10:16:53 -0800142 if (lchown(addr.sun_path, uid, gid)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700143 PLOG(ERROR) << "Failed to lchown socket '" << addr.sun_path << "'";
Nick Kralevich9bcfd642016-02-24 15:50:52 -0800144 goto out_unlink;
145 }
Mark Salyzyndb691072016-11-07 10:16:53 -0800146 if (fchmodat(AT_FDCWD, addr.sun_path, perm, AT_SYMLINK_NOFOLLOW)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700147 PLOG(ERROR) << "Failed to fchmodat socket '" << addr.sun_path << "'";
Nick Kralevich9bcfd642016-02-24 15:50:52 -0800148 goto out_unlink;
149 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700151 LOG(INFO) << "Created socket '" << addr.sun_path << "'"
152 << ", mode " << std::oct << perm << std::dec
153 << ", user " << uid
154 << ", group " << gid;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155
Mark Salyzyndb691072016-11-07 10:16:53 -0800156 return fd.release();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157
158out_unlink:
159 unlink(addr.sun_path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 return -1;
161}
162
Tom Cherry11a3aee2017-08-03 12:54:07 -0700163Result<std::string> ReadFile(const std::string& path) {
Tom Cherry53089aa2017-03-31 15:47:33 -0700164 android::base::unique_fd fd(
165 TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
Elliott Hughesf682b472015-02-06 12:19:48 -0800166 if (fd == -1) {
Tom Cherry11a3aee2017-08-03 12:54:07 -0700167 return ErrnoError() << "open() failed";
Elliott Hughesf682b472015-02-06 12:19:48 -0800168 }
169
170 // For security reasons, disallow world-writable
171 // or group-writable files.
Nick Kralevich38f368c2012-01-18 10:39:01 -0800172 struct stat sb;
Elliott Hughesf682b472015-02-06 12:19:48 -0800173 if (fstat(fd, &sb) == -1) {
Tom Cherry11a3aee2017-08-03 12:54:07 -0700174 return ErrnoError() << "fstat failed()";
Nick Kralevich38f368c2012-01-18 10:39:01 -0800175 }
176 if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
Tom Cherry11a3aee2017-08-03 12:54:07 -0700177 return Error() << "Skipping insecure file";
Nick Kralevich38f368c2012-01-18 10:39:01 -0800178 }
179
Tom Cherry11a3aee2017-08-03 12:54:07 -0700180 std::string content;
181 if (!android::base::ReadFdToString(fd, &content)) {
182 return ErrnoError() << "Unable to read file contents";
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700183 }
Tom Cherry11a3aee2017-08-03 12:54:07 -0700184 return content;
Elliott Hughesf682b472015-02-06 12:19:48 -0800185}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186
Joel Galenson4b591f12017-11-27 14:45:26 -0800187static int OpenFile(const std::string& path, int flags, mode_t mode) {
188 std::string secontext;
189 if (SelabelLookupFileContext(path, mode, &secontext) && !secontext.empty()) {
190 setfscreatecon(secontext.c_str());
191 }
192
193 int rc = open(path.c_str(), flags, mode);
194
195 if (!secontext.empty()) {
196 int save_errno = errno;
197 setfscreatecon(nullptr);
198 errno = save_errno;
199 }
200
201 return rc;
202}
203
Tom Cherry11a3aee2017-08-03 12:54:07 -0700204Result<Success> WriteFile(const std::string& path, const std::string& content) {
Yongqin Liudbe88e72016-12-28 16:06:19 +0800205 android::base::unique_fd fd(TEMP_FAILURE_RETRY(
Joel Galenson4b591f12017-11-27 14:45:26 -0800206 OpenFile(path, O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0600)));
Elliott Hughesf682b472015-02-06 12:19:48 -0800207 if (fd == -1) {
Tom Cherry11a3aee2017-08-03 12:54:07 -0700208 return ErrnoError() << "open() failed";
Elliott Hughesf682b472015-02-06 12:19:48 -0800209 }
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700210 if (!android::base::WriteStringToFd(content, fd)) {
Tom Cherry11a3aee2017-08-03 12:54:07 -0700211 return ErrnoError() << "Unable to write file contents";
Nick Kralevicheedbe812015-04-25 14:10:03 -0700212 }
Tom Cherry11a3aee2017-08-03 12:54:07 -0700213 return Success();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214}
215
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700216bool mkdir_recursive(const std::string& path, mode_t mode) {
Tom Cherry060b74b2017-04-12 14:27:51 -0700217 std::string::size_type slash = 0;
218 while ((slash = path.find('/', slash + 1)) != std::string::npos) {
219 auto directory = path.substr(0, slash);
220 struct stat info;
221 if (stat(directory.c_str(), &info) != 0) {
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700222 auto ret = make_dir(directory, mode);
223 if (!ret && errno != EEXIST) return false;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700224 }
225 }
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700226 auto ret = make_dir(path, mode);
227 if (!ret && errno != EEXIST) return false;
228 return true;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700229}
230
Elliott Hughes9605a942016-11-10 17:43:47 -0800231int wait_for_file(const char* filename, std::chrono::nanoseconds timeout) {
Wei Wang4cea1212017-08-22 12:07:37 -0700232 android::base::Timer t;
233 while (t.duration() < timeout) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800234 struct stat sb;
Wei Wang4cea1212017-08-22 12:07:37 -0700235 if (stat(filename, &sb) != -1) {
236 LOG(INFO) << "wait for '" << filename << "' took " << t;
237 return 0;
238 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800239 std::this_thread::sleep_for(10ms);
Elliott Hughes9605a942016-11-10 17:43:47 -0800240 }
Wei Wang4cea1212017-08-22 12:07:37 -0700241 LOG(WARNING) << "wait for '" << filename << "' timed out and took " << t;
Elliott Hughes9605a942016-11-10 17:43:47 -0800242 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700243}
Colin Crossf83d0b92010-04-21 12:04:20 -0700244
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700245void import_kernel_cmdline(bool in_qemu,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700246 const std::function<void(const std::string&, const std::string&, bool)>& fn) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700247 std::string cmdline;
248 android::base::ReadFileToString("/proc/cmdline", &cmdline);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700249
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700250 for (const auto& entry : android::base::Split(android::base::Trim(cmdline), " ")) {
251 std::vector<std::string> pieces = android::base::Split(entry, "=");
252 if (pieces.size() == 2) {
253 fn(pieces[0], pieces[1], in_qemu);
254 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700255 }
256}
Stephen Smalleye096e362012-06-11 13:37:39 -0400257
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700258bool make_dir(const std::string& path, mode_t mode) {
259 std::string secontext;
260 if (SelabelLookupFileContext(path, mode, &secontext) && !secontext.empty()) {
261 setfscreatecon(secontext.c_str());
Stephen Smalleye096e362012-06-11 13:37:39 -0400262 }
Stephen Smalleye096e362012-06-11 13:37:39 -0400263
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700264 int rc = mkdir(path.c_str(), mode);
Stephen Smalleye096e362012-06-11 13:37:39 -0400265
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700266 if (!secontext.empty()) {
Stephen Smalleye096e362012-06-11 13:37:39 -0400267 int save_errno = errno;
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700268 setfscreatecon(nullptr);
Stephen Smalleye096e362012-06-11 13:37:39 -0400269 errno = save_errno;
270 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700271
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700272 return rc == 0;
Stephen Smalleye096e362012-06-11 13:37:39 -0400273}
274
Andres Moralesdb5f5d42015-05-08 08:30:33 -0700275/*
276 * Writes hex_len hex characters (1/2 byte) to hex from bytes.
277 */
278std::string bytes_to_hex(const uint8_t* bytes, size_t bytes_len) {
279 std::string hex("0x");
280 for (size_t i = 0; i < bytes_len; i++)
281 android::base::StringAppendF(&hex, "%02x", bytes[i]);
282 return hex;
283}
Lee Campbellf13b1b32015-07-24 16:57:14 -0700284
285/*
286 * Returns true is pathname is a directory
287 */
288bool is_dir(const char* pathname) {
289 struct stat info;
290 if (stat(pathname, &info) == -1) {
291 return false;
292 }
293 return S_ISDIR(info.st_mode);
294}
Tom Cherryb7349902015-08-26 11:43:36 -0700295
296bool expand_props(const std::string& src, std::string* dst) {
297 const char* src_ptr = src.c_str();
298
299 if (!dst) {
300 return false;
301 }
302
303 /* - variables can either be $x.y or ${x.y}, in case they are only part
304 * of the string.
305 * - will accept $$ as a literal $.
306 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
307 * bad things will happen
Mark Salyzyn4b561622016-06-07 08:49:01 -0700308 * - ${x.y:-default} will return default value if property empty.
Tom Cherryb7349902015-08-26 11:43:36 -0700309 */
310 while (*src_ptr) {
311 const char* c;
312
313 c = strchr(src_ptr, '$');
314 if (!c) {
315 dst->append(src_ptr);
316 return true;
317 }
318
319 dst->append(src_ptr, c);
320 c++;
321
322 if (*c == '$') {
323 dst->push_back(*(c++));
324 src_ptr = c;
325 continue;
326 } else if (*c == '\0') {
327 return true;
328 }
329
330 std::string prop_name;
Mark Salyzyn4b561622016-06-07 08:49:01 -0700331 std::string def_val;
Tom Cherryb7349902015-08-26 11:43:36 -0700332 if (*c == '{') {
333 c++;
334 const char* end = strchr(c, '}');
335 if (!end) {
336 // failed to find closing brace, abort.
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700337 LOG(ERROR) << "unexpected end of string in '" << src << "', looking for }";
Tom Cherryb7349902015-08-26 11:43:36 -0700338 return false;
339 }
340 prop_name = std::string(c, end);
341 c = end + 1;
Mark Salyzyn4b561622016-06-07 08:49:01 -0700342 size_t def = prop_name.find(":-");
343 if (def < prop_name.size()) {
344 def_val = prop_name.substr(def + 2);
345 prop_name = prop_name.substr(0, def);
346 }
Tom Cherryb7349902015-08-26 11:43:36 -0700347 } else {
348 prop_name = c;
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700349 LOG(ERROR) << "using deprecated syntax for specifying property '" << c << "', use ${name} instead";
Tom Cherryb7349902015-08-26 11:43:36 -0700350 c += prop_name.size();
351 }
352
353 if (prop_name.empty()) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700354 LOG(ERROR) << "invalid zero-length property name in '" << src << "'";
Tom Cherryb7349902015-08-26 11:43:36 -0700355 return false;
356 }
357
Tom Cherryccf23532017-03-28 16:40:41 -0700358 std::string prop_val = android::base::GetProperty(prop_name, "");
Tom Cherryb7349902015-08-26 11:43:36 -0700359 if (prop_val.empty()) {
Mark Salyzyn4b561622016-06-07 08:49:01 -0700360 if (def_val.empty()) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700361 LOG(ERROR) << "property '" << prop_name << "' doesn't exist while expanding '" << src << "'";
Mark Salyzyn4b561622016-06-07 08:49:01 -0700362 return false;
363 }
364 prop_val = def_val;
Tom Cherryb7349902015-08-26 11:43:36 -0700365 }
366
367 dst->append(prop_val);
368 src_ptr = c;
369 }
370
371 return true;
372}
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000373
Yu Ningc01022a2017-07-26 17:54:08 +0800374static std::string init_android_dt_dir() {
375 // Use the standard procfs-based path by default
376 std::string android_dt_dir = kDefaultAndroidDtDir;
377 // The platform may specify a custom Android DT path in kernel cmdline
378 import_kernel_cmdline(false,
379 [&](const std::string& key, const std::string& value, bool in_qemu) {
380 if (key == "androidboot.android_dt_dir") {
381 android_dt_dir = value;
382 }
383 });
384 LOG(INFO) << "Using Android DT directory " << android_dt_dir;
385 return android_dt_dir;
386}
387
388// FIXME: The same logic is duplicated in system/core/fs_mgr/
389const std::string& get_android_dt_dir() {
390 // Set once and saves time for subsequent calls to this function
391 static const std::string kAndroidDtDir = init_android_dt_dir();
392 return kAndroidDtDir;
393}
394
395// Reads the content of device tree file under the platform's Android DT directory.
Bowgo Tsaid2620172017-04-17 22:17:09 +0800396// Returns true if the read is success, false otherwise.
397bool read_android_dt_file(const std::string& sub_path, std::string* dt_content) {
Yu Ningc01022a2017-07-26 17:54:08 +0800398 const std::string file_name = get_android_dt_dir() + sub_path;
Bowgo Tsaid2620172017-04-17 22:17:09 +0800399 if (android::base::ReadFileToString(file_name, dt_content)) {
400 if (!dt_content->empty()) {
401 dt_content->pop_back(); // Trims the trailing '\0' out.
402 return true;
403 }
404 }
405 return false;
406}
407
408bool is_android_dt_value_expected(const std::string& sub_path, const std::string& expected_content) {
409 std::string dt_content;
410 if (read_android_dt_file(sub_path, &dt_content)) {
411 if (dt_content == expected_content) {
412 return true;
413 }
414 }
415 return false;
416}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700417
Tom Cherryde6bd502018-02-13 16:50:08 -0800418bool IsLegalPropertyName(const std::string& name) {
419 size_t namelen = name.size();
420
421 if (namelen < 1) return false;
422 if (name[0] == '.') return false;
423 if (name[namelen - 1] == '.') return false;
424
425 /* Only allow alphanumeric, plus '.', '-', '@', ':', or '_' */
426 /* Don't allow ".." to appear in a property name */
427 for (size_t i = 0; i < namelen; i++) {
428 if (name[i] == '.') {
429 // i=0 is guaranteed to never have a dot. See above.
430 if (name[i - 1] == '.') return false;
431 continue;
432 }
433 if (name[i] == '_' || name[i] == '-' || name[i] == '@' || name[i] == ':') continue;
434 if (name[i] >= 'a' && name[i] <= 'z') continue;
435 if (name[i] >= 'A' && name[i] <= 'Z') continue;
436 if (name[i] >= '0' && name[i] <= '9') continue;
437 return false;
438 }
439
440 return true;
441}
442
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700443} // namespace init
444} // namespace android