Merge "fs_mgr: fs_mgr_update_verity_state take std::function"
diff --git a/fs_mgr/fs_mgr_fstab.cpp b/fs_mgr/fs_mgr_fstab.cpp
index a14dba3..a5b3fe8 100644
--- a/fs_mgr/fs_mgr_fstab.cpp
+++ b/fs_mgr/fs_mgr_fstab.cpp
@@ -235,41 +235,46 @@
* If not found, the loop exits with fl[i].name being null.
*/
for (i = 0; fl[i].name; i++) {
- if (!strncmp(p, fl[i].name, strlen(fl[i].name))) {
+ auto name = fl[i].name;
+ auto len = strlen(name);
+ auto end = len;
+ if (name[end - 1] == '=') --end;
+ if (!strncmp(p, name, len) && (p[end] == name[end])) {
f |= fl[i].flag;
- if ((fl[i].flag == MF_CRYPT) && flag_vals) {
+ if (!flag_vals) break;
+ if (p[end] != '=') break;
+ char* arg = p + end + 1;
+ auto flag = fl[i].flag;
+ if (flag == MF_CRYPT) {
/* The encryptable flag is followed by an = and the
* location of the keys. Get it and return it.
*/
- flag_vals->key_loc = strdup(strchr(p, '=') + 1);
- } else if ((fl[i].flag == MF_VERIFY) && flag_vals) {
+ flag_vals->key_loc = strdup(arg);
+ } else if (flag == MF_VERIFY) {
/* If the verify flag is followed by an = and the
* location for the verity state, get it and return it.
*/
- char *start = strchr(p, '=');
- if (start) {
- flag_vals->verity_loc = strdup(start + 1);
- }
- } else if ((fl[i].flag == MF_FORCECRYPT) && flag_vals) {
+ flag_vals->verity_loc = strdup(arg);
+ } else if (flag == MF_FORCECRYPT) {
/* The forceencrypt flag is followed by an = and the
* location of the keys. Get it and return it.
*/
- flag_vals->key_loc = strdup(strchr(p, '=') + 1);
- } else if ((fl[i].flag == MF_FORCEFDEORFBE) && flag_vals) {
+ flag_vals->key_loc = strdup(arg);
+ } else if (flag == MF_FORCEFDEORFBE) {
/* The forcefdeorfbe flag is followed by an = and the
* location of the keys. Get it and return it.
*/
- flag_vals->key_loc = strdup(strchr(p, '=') + 1);
+ flag_vals->key_loc = strdup(arg);
flag_vals->file_contents_mode = EM_AES_256_XTS;
flag_vals->file_names_mode = EM_AES_256_CTS;
- } else if ((fl[i].flag == MF_FILEENCRYPTION) && flag_vals) {
+ } else if (flag == MF_FILEENCRYPTION) {
/* The fileencryption flag is followed by an = and
* the mode of contents encryption, then optionally a
* : and the mode of filenames encryption (defaults
* to aes-256-cts). Get it and return it.
*/
- char *mode = strchr(p, '=') + 1;
- char *colon = strchr(mode, ':');
+ auto mode = arg;
+ auto colon = strchr(mode, ':');
if (colon) {
*colon = '\0';
}
@@ -283,33 +288,30 @@
} else {
flag_vals->file_names_mode = EM_AES_256_CTS;
}
- } else if ((fl[i].flag == MF_KEYDIRECTORY) && flag_vals) {
+ } else if (flag == MF_KEYDIRECTORY) {
/* The metadata flag is followed by an = and the
* directory for the keys. Get it and return it.
*/
- flag_vals->key_dir = strdup(strchr(p, '=') + 1);
- } else if ((fl[i].flag == MF_LENGTH) && flag_vals) {
+ flag_vals->key_dir = strdup(arg);
+ } else if (flag == MF_LENGTH) {
/* The length flag is followed by an = and the
* size of the partition. Get it and return it.
*/
- flag_vals->part_length = strtoll(strchr(p, '=') + 1, NULL, 0);
- } else if ((fl[i].flag == MF_VOLDMANAGED) && flag_vals) {
+ flag_vals->part_length = strtoll(arg, NULL, 0);
+ } else if (flag == MF_VOLDMANAGED) {
/* The voldmanaged flag is followed by an = and the
* label, a colon and the partition number or the
* word "auto", e.g.
* voldmanaged=sdcard:3
* Get and return them.
*/
- char *label_start;
- char *label_end;
- char *part_start;
+ auto label_start = arg;
+ auto label_end = strchr(label_start, ':');
- label_start = strchr(p, '=') + 1;
- label_end = strchr(p, ':');
if (label_end) {
flag_vals->label = strndup(label_start,
(int) (label_end - label_start));
- part_start = strchr(p, ':') + 1;
+ auto part_start = label_end + 1;
if (!strcmp(part_start, "auto")) {
flag_vals->partnum = -1;
} else {
@@ -318,41 +320,41 @@
} else {
LERROR << "Warning: voldmanaged= flag malformed";
}
- } else if ((fl[i].flag == MF_SWAPPRIO) && flag_vals) {
- flag_vals->swap_prio = strtoll(strchr(p, '=') + 1, NULL, 0);
- } else if ((fl[i].flag == MF_MAX_COMP_STREAMS) && flag_vals) {
- flag_vals->max_comp_streams = strtoll(strchr(p, '=') + 1, NULL, 0);
- } else if ((fl[i].flag == MF_ZRAMSIZE) && flag_vals) {
- int is_percent = !!strrchr(p, '%');
- unsigned int val = strtoll(strchr(p, '=') + 1, NULL, 0);
+ } else if (flag == MF_SWAPPRIO) {
+ flag_vals->swap_prio = strtoll(arg, NULL, 0);
+ } else if (flag == MF_MAX_COMP_STREAMS) {
+ flag_vals->max_comp_streams = strtoll(arg, NULL, 0);
+ } else if (flag == MF_ZRAMSIZE) {
+ auto is_percent = !!strrchr(arg, '%');
+ auto val = strtoll(arg, NULL, 0);
if (is_percent)
flag_vals->zram_size = calculate_zram_size(val);
else
flag_vals->zram_size = val;
- } else if ((fl[i].flag == MF_RESERVEDSIZE) && flag_vals) {
+ } else if (flag == MF_RESERVEDSIZE) {
/* The reserved flag is followed by an = and the
* reserved size of the partition. Get it and return it.
*/
- flag_vals->reserved_size = parse_size(strchr(p, '=') + 1);
- } else if ((fl[i].flag == MF_ERASEBLKSIZE) && flag_vals) {
+ flag_vals->reserved_size = parse_size(arg);
+ } else if (flag == MF_ERASEBLKSIZE) {
/* The erase block size flag is followed by an = and the flash
* erase block size. Get it, check that it is a power of 2 and
* at least 4096, and return it.
*/
- unsigned int val = strtoul(strchr(p, '=') + 1, NULL, 0);
+ auto val = strtoul(arg, NULL, 0);
if (val >= 4096 && (val & (val - 1)) == 0)
flag_vals->erase_blk_size = val;
- } else if ((fl[i].flag == MF_LOGICALBLKSIZE) && flag_vals) {
+ } else if (flag == MF_LOGICALBLKSIZE) {
/* The logical block size flag is followed by an = and the flash
* logical block size. Get it, check that it is a power of 2 and
* at least 4096, and return it.
*/
- unsigned int val = strtoul(strchr(p, '=') + 1, NULL, 0);
+ auto val = strtoul(arg, NULL, 0);
if (val >= 4096 && (val & (val - 1)) == 0)
flag_vals->logical_blk_size = val;
- } else if ((fl[i].flag == MF_SYSFS) && flag_vals) {
+ } else if (flag == MF_SYSFS) {
/* The path to trigger device gc by idle-maint of vold. */
- flag_vals->sysfs_path = strdup(strchr(p, '=') + 1);
+ flag_vals->sysfs_path = strdup(arg);
}
break;
}
@@ -506,8 +508,7 @@
return false;
}
-static struct fstab *fs_mgr_read_fstab_file(FILE *fstab_file)
-{
+static struct fstab* fs_mgr_read_fstab_file(FILE* fstab_file, bool proc_mounts) {
int cnt, entries;
ssize_t len;
size_t alloc_len = 0;
@@ -607,7 +608,10 @@
fstab->recs[cnt].fs_options = NULL;
}
- if (!(p = strtok_r(NULL, delim, &save_ptr))) {
+ // For /proc/mounts, ignore everything after mnt_freq and mnt_passno
+ if (proc_mounts) {
+ p += strlen(p);
+ } else if (!(p = strtok_r(NULL, delim, &save_ptr))) {
LERROR << "Error parsing fs_mgr_options";
goto err;
}
@@ -739,7 +743,7 @@
return nullptr;
}
- fstab = fs_mgr_read_fstab_file(fstab_file);
+ fstab = fs_mgr_read_fstab_file(fstab_file, !strcmp("/proc/mounts", fstab_path));
if (!fstab) {
LERROR << __FUNCTION__ << "(): failed to load fstab from : '" << fstab_path << "'";
}
@@ -767,7 +771,7 @@
return nullptr;
}
- struct fstab *fstab = fs_mgr_read_fstab_file(fstab_file.get());
+ struct fstab* fstab = fs_mgr_read_fstab_file(fstab_file.get(), false);
if (!fstab) {
LERROR << __FUNCTION__ << "(): failed to load fstab from kernel:"
<< std::endl << fstab_buf;
diff --git a/fs_mgr/tests/fs_mgr_test.cpp b/fs_mgr/tests/fs_mgr_test.cpp
index 2e76752..8b1c55a 100644
--- a/fs_mgr/tests/fs_mgr_test.cpp
+++ b/fs_mgr/tests/fs_mgr_test.cpp
@@ -14,13 +14,18 @@
* limitations under the License.
*/
+#include <linux/fs.h>
+#include <mntent.h>
+
#include <algorithm>
#include <iterator>
+#include <set>
#include <string>
#include <utility>
#include <vector>
#include <android-base/strings.h>
+#include <fstab/fstab.h>
#include <gtest/gtest.h>
#include "../fs_mgr_priv_boot_config.h"
@@ -129,3 +134,70 @@
EXPECT_FALSE(fs_mgr_get_boot_config_from_kernel(cmdline, "nospace", &content));
EXPECT_TRUE(content.empty()) << content;
}
+
+TEST(fs_mgr, fs_mgr_read_fstab_file_proc_mounts) {
+ auto fstab = fs_mgr_read_fstab("/proc/mounts");
+ ASSERT_NE(fstab, nullptr);
+
+ std::unique_ptr<std::FILE, int (*)(std::FILE*)> mounts(setmntent("/proc/mounts", "r"),
+ endmntent);
+ ASSERT_NE(mounts, nullptr);
+
+ mntent* mentry;
+ int i = 0;
+ while ((mentry = getmntent(mounts.get())) != nullptr) {
+ ASSERT_LT(i, fstab->num_entries);
+ auto fsrec = &fstab->recs[i];
+
+ std::string mnt_fsname(mentry->mnt_fsname ?: "nullptr");
+ std::string blk_device(fsrec->blk_device ?: "nullptr");
+ EXPECT_EQ(mnt_fsname, blk_device);
+
+ std::string mnt_dir(mentry->mnt_dir ?: "nullptr");
+ std::string mount_point(fsrec->mount_point ?: "nullptr");
+ EXPECT_EQ(mnt_dir, mount_point);
+
+ std::string mnt_type(mentry->mnt_type ?: "nullptr");
+ std::string fs_type(fsrec->fs_type ?: "nullptr");
+ EXPECT_EQ(mnt_type, fs_type);
+
+ std::set<std::string> mnt_opts;
+ for (auto& s : android::base::Split(mentry->mnt_opts ?: "nullptr", ",")) {
+ mnt_opts.emplace(s);
+ }
+ std::set<std::string> fs_options;
+ for (auto& s : android::base::Split(fsrec->fs_options ?: "nullptr", ",")) {
+ fs_options.emplace(s);
+ }
+ // matches private content in fs_mgr_fstab.c
+ static struct flag_list {
+ const char* name;
+ unsigned int flag;
+ } mount_flags[] = {
+ {"noatime", MS_NOATIME},
+ {"noexec", MS_NOEXEC},
+ {"nosuid", MS_NOSUID},
+ {"nodev", MS_NODEV},
+ {"nodiratime", MS_NODIRATIME},
+ {"ro", MS_RDONLY},
+ {"rw", 0},
+ {"remount", MS_REMOUNT},
+ {"bind", MS_BIND},
+ {"rec", MS_REC},
+ {"unbindable", MS_UNBINDABLE},
+ {"private", MS_PRIVATE},
+ {"slave", MS_SLAVE},
+ {"shared", MS_SHARED},
+ {"defaults", 0},
+ {0, 0},
+ };
+ for (auto f = 0; mount_flags[f].name; ++f) {
+ if (mount_flags[f].flag & fsrec->flags) {
+ fs_options.emplace(mount_flags[f].name);
+ }
+ }
+ if (!(fsrec->flags & MS_RDONLY)) fs_options.emplace("rw");
+ EXPECT_EQ(mnt_opts, fs_options);
+ ++i;
+ }
+}
diff --git a/init/Android.bp b/init/Android.bp
index cf7637f..625fb94 100644
--- a/init/Android.bp
+++ b/init/Android.bp
@@ -179,6 +179,7 @@
"rlimit_parser_test.cpp",
"service_test.cpp",
"subcontext_test.cpp",
+ "tokenizer_test.cpp",
"ueventd_test.cpp",
"util_test.cpp",
],
diff --git a/init/tokenizer_test.cpp b/init/tokenizer_test.cpp
new file mode 100644
index 0000000..acfc7c7
--- /dev/null
+++ b/init/tokenizer_test.cpp
@@ -0,0 +1,163 @@
+//
+// Copyright (C) 2018 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include "tokenizer.h"
+
+#include <string>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+namespace android {
+namespace init {
+
+namespace {
+
+void RunTest(const std::string& data, const std::vector<std::vector<std::string>>& expected_tokens) {
+ auto data_copy = std::string{data};
+ data_copy.push_back('\n'); // TODO: fix tokenizer
+ data_copy.push_back('\0');
+
+ parse_state state;
+ state.line = 0;
+ state.ptr = data_copy.data();
+ state.nexttoken = 0;
+
+ std::vector<std::string> current_line;
+ std::vector<std::vector<std::string>> tokens;
+
+ while (true) {
+ switch (next_token(&state)) {
+ case T_EOF:
+ EXPECT_EQ(expected_tokens, tokens) << data;
+ return;
+ case T_NEWLINE:
+ tokens.emplace_back(std::move(current_line));
+ break;
+ case T_TEXT:
+ current_line.emplace_back(state.text);
+ break;
+ }
+ }
+}
+
+} // namespace
+
+TEST(tokenizer, null) {
+ RunTest("", {{}});
+}
+
+TEST(tokenizer, simple_oneline) {
+ RunTest("one two\tthree\rfour", {{"one", "two", "three", "four"}});
+}
+
+TEST(tokenizer, simple_multiline) {
+ RunTest("1 2 3\n4 5 6\n7 8 9", {{"1", "2", "3"}, {"4", "5", "6"}, {"7", "8", "9"}});
+}
+
+TEST(tokenizer, preceding_space) {
+ // Preceding spaces are ignored.
+ RunTest(" 1 2 3\n\t\t\t\t4 5 6\n\r\r\r\r7 8 9",
+ {{"1", "2", "3"}, {"4", "5", "6"}, {"7", "8", "9"}});
+}
+
+TEST(tokenizer, comments) {
+ // Entirely commented lines still produce a T_NEWLINE token for tracking line count.
+ RunTest("1 2 3\n#4 5 6\n7 8 9", {{"1", "2", "3"}, {}, {"7", "8", "9"}});
+
+ RunTest("#1 2 3\n4 5 6\n7 8 9", {{}, {"4", "5", "6"}, {"7", "8", "9"}});
+
+ RunTest("1 2 3\n4 5 6\n#7 8 9", {{"1", "2", "3"}, {"4", "5", "6"}, {}});
+
+ RunTest("1 2 #3\n4 #5 6\n#7 8 9", {{"1", "2"}, {"4"}, {}});
+}
+
+TEST(tokenizer, control_chars) {
+ // Literal \n, \r, \t, and \\ produce the control characters \n, \r, \t, and \\ respectively.
+ // Literal \? produces ? for all other character '?'
+
+ RunTest(R"(1 token\ntoken 2)", {{"1", "token\ntoken", "2"}});
+ RunTest(R"(1 token\rtoken 2)", {{"1", "token\rtoken", "2"}});
+ RunTest(R"(1 token\ttoken 2)", {{"1", "token\ttoken", "2"}});
+ RunTest(R"(1 token\\token 2)", {{"1", "token\\token", "2"}});
+ RunTest(R"(1 token\btoken 2)", {{"1", "tokenbtoken", "2"}});
+
+ RunTest(R"(1 token\n 2)", {{"1", "token\n", "2"}});
+ RunTest(R"(1 token\r 2)", {{"1", "token\r", "2"}});
+ RunTest(R"(1 token\t 2)", {{"1", "token\t", "2"}});
+ RunTest(R"(1 token\\ 2)", {{"1", "token\\", "2"}});
+ RunTest(R"(1 token\b 2)", {{"1", "tokenb", "2"}});
+
+ RunTest(R"(1 \ntoken 2)", {{"1", "\ntoken", "2"}});
+ RunTest(R"(1 \rtoken 2)", {{"1", "\rtoken", "2"}});
+ RunTest(R"(1 \ttoken 2)", {{"1", "\ttoken", "2"}});
+ RunTest(R"(1 \\token 2)", {{"1", "\\token", "2"}});
+ RunTest(R"(1 \btoken 2)", {{"1", "btoken", "2"}});
+
+ RunTest(R"(1 \n 2)", {{"1", "\n", "2"}});
+ RunTest(R"(1 \r 2)", {{"1", "\r", "2"}});
+ RunTest(R"(1 \t 2)", {{"1", "\t", "2"}});
+ RunTest(R"(1 \\ 2)", {{"1", "\\", "2"}});
+ RunTest(R"(1 \b 2)", {{"1", "b", "2"}});
+}
+
+TEST(tokenizer, cr_lf) {
+ // \ before \n, \r, or \r\n is interpreted as a line continuation
+ // Extra whitespace on the next line is eaten, except \r unlike in the above tests.
+
+ RunTest("lf\\\ncont", {{"lfcont"}});
+ RunTest("lf\\\n \t\t\t\tcont", {{"lfcont"}});
+
+ RunTest("crlf\\\r\ncont", {{"crlfcont"}});
+ RunTest("crlf\\\r\n \t\t\t\tcont", {{"crlfcont"}});
+
+ RunTest("cr\\\rcont", {{"crcont"}});
+
+ RunTest("lfspace \\\ncont", {{"lfspace", "cont"}});
+ RunTest("lfspace \\\n \t\t\t\tcont", {{"lfspace", "cont"}});
+
+ RunTest("crlfspace \\\r\ncont", {{"crlfspace", "cont"}});
+ RunTest("crlfspace \\\r\n \t\t\t\tcont", {{"crlfspace", "cont"}});
+
+ RunTest("crspace \\\rcont", {{"crspace", "cont"}});
+}
+
+TEST(tokenizer, quoted) {
+ RunTest("\"quoted simple string\"", {{"quoted simple string"}});
+
+ // Unterminated quotes just return T_EOF without any T_NEWLINE.
+ RunTest("\"unterminated quoted string", {});
+
+ RunTest("\"1 2 3\"\n \"unterminated quoted string", {{"1 2 3"}});
+
+ // Escaping quotes is not allowed and are treated as an unterminated quoted string.
+ RunTest("\"quoted escaped quote\\\"\"", {});
+ RunTest("\"quoted escaped\\\" quote\"", {});
+ RunTest("\"\\\"quoted escaped quote\"", {});
+
+ RunTest("\"quoted control characters \\n \\r \\t \\\\ \\b \\\r \\\n \r \n\"",
+ {{"quoted control characters \\n \\r \\t \\\\ \\b \\\r \\\n \r \n"}});
+
+ RunTest("\"quoted simple string\" \"second quoted string\"",
+ {{"quoted simple string", "second quoted string"}});
+
+ RunTest("\"# comment quoted string\"", {{"# comment quoted string"}});
+
+ RunTest("\"Adjacent \"\"quoted strings\"", {{"Adjacent quoted strings"}});
+}
+
+} // namespace init
+} // namespace android
diff --git a/libcutils/sockets_unix.cpp b/libcutils/sockets_unix.cpp
index 2849aa8..0cb8a4d 100644
--- a/libcutils/sockets_unix.cpp
+++ b/libcutils/sockets_unix.cpp
@@ -32,10 +32,6 @@
#include "android_get_control_env.h"
-#ifndef TEMP_FAILURE_RETRY
-#define TEMP_FAILURE_RETRY(exp) (exp) // KISS implementation
-#endif
-
#if defined(__ANDROID__)
/* For the socket trust (credentials) check */
#include <private/android_filesystem_config.h>
@@ -102,15 +98,15 @@
// Compare to UNIX domain socket name, must match!
struct sockaddr_un addr;
socklen_t addrlen = sizeof(addr);
- int ret = TEMP_FAILURE_RETRY(getsockname(fd, (struct sockaddr *)&addr, &addrlen));
+ int ret = getsockname(fd, (struct sockaddr*)&addr, &addrlen);
if (ret < 0) return -1;
- char *path = NULL;
- if (asprintf(&path, ANDROID_SOCKET_DIR "/%s", name) < 0) return -1;
- if (!path) return -1;
- int cmp = strcmp(addr.sun_path, path);
- free(path);
- if (cmp != 0) return -1;
- // It is what we think it is
- return fd;
+ constexpr char prefix[] = ANDROID_SOCKET_DIR "/";
+ constexpr size_t prefix_size = sizeof(prefix) - sizeof('\0');
+ if ((strncmp(addr.sun_path, prefix, prefix_size) == 0) &&
+ (strcmp(addr.sun_path + prefix_size, name) == 0)) {
+ // It is what we think it is
+ return fd;
+ }
+ return -1;
}