Merge "add kill all for shutdown_timeout of 0"
diff --git a/cpio/mkbootfs.c b/cpio/mkbootfs.c
index b89c395..e52762e 100644
--- a/cpio/mkbootfs.c
+++ b/cpio/mkbootfs.c
@@ -301,6 +301,7 @@
allocated *= 2;
canned_config = (struct fs_config_entry*)realloc(
canned_config, allocated * sizeof(struct fs_config_entry));
+ if (canned_config == NULL) die("failed to reallocate memory");
}
struct fs_config_entry* cc = canned_config + used;
@@ -320,6 +321,7 @@
++allocated;
canned_config = (struct fs_config_entry*)realloc(
canned_config, allocated * sizeof(struct fs_config_entry));
+ if (canned_config == NULL) die("failed to reallocate memory");
}
canned_config[used].name = NULL;
diff --git a/init/README.md b/init/README.md
index 99522b9..709c667 100644
--- a/init/README.md
+++ b/init/README.md
@@ -282,6 +282,11 @@
`copy <src> <dst>`
> Copies a file. Similar to write, but useful for binary/large
amounts of data.
+ Regarding to the src file, copying from symbol link file and world-writable
+ or group-writable files are not allowed.
+ Regarding to the dst file, the default mode created is 0600 if it does not
+ exist. And it will be truncated if dst file is a normal regular file and
+ already exists.
`domainname <name>`
> Set the domain name.
diff --git a/init/builtins.cpp b/init/builtins.cpp
index 24875d5..32e9ef6 100644
--- a/init/builtins.cpp
+++ b/init/builtins.cpp
@@ -704,61 +704,11 @@
}
static int do_copy(const std::vector<std::string>& args) {
- char *buffer = NULL;
- int rc = 0;
- int fd1 = -1, fd2 = -1;
- struct stat info;
- int brtw, brtr;
- char *p;
-
- if (stat(args[1].c_str(), &info) < 0)
- return -1;
-
- if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
- goto out_err;
-
- if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
- goto out_err;
-
- if (!(buffer = (char*) malloc(info.st_size)))
- goto out_err;
-
- p = buffer;
- brtr = info.st_size;
- while(brtr) {
- rc = read(fd1, p, brtr);
- if (rc < 0)
- goto out_err;
- if (rc == 0)
- break;
- p += rc;
- brtr -= rc;
+ std::string data;
+ if (read_file(args[1].c_str(), &data)) {
+ return write_file(args[2].c_str(), data.data()) ? 0 : 1;
}
-
- p = buffer;
- brtw = info.st_size;
- while(brtw) {
- rc = write(fd2, p, brtw);
- if (rc < 0)
- goto out_err;
- if (rc == 0)
- break;
- p += rc;
- brtw -= rc;
- }
-
- rc = 0;
- goto out;
-out_err:
- rc = -1;
-out:
- if (buffer)
- free(buffer);
- if (fd1 >= 0)
- close(fd1);
- if (fd2 >= 0)
- close(fd2);
- return rc;
+ return 1;
}
static int do_chown(const std::vector<std::string>& args) {
diff --git a/init/init.cpp b/init/init.cpp
index 0ce1c05..23448d6 100644
--- a/init/init.cpp
+++ b/init/init.cpp
@@ -1322,22 +1322,24 @@
am.QueueBuiltinAction(queue_property_triggers_action, "queue_property_triggers");
while (true) {
- if (!(waiting_for_exec || waiting_for_prop)) {
- am.ExecuteOneCommand();
- restart_processes();
- }
-
// By default, sleep until something happens.
int epoll_timeout_ms = -1;
- // If there's a process that needs restarting, wake up in time for that.
- if (process_needs_restart_at != 0) {
- epoll_timeout_ms = (process_needs_restart_at - time(nullptr)) * 1000;
- if (epoll_timeout_ms < 0) epoll_timeout_ms = 0;
+ if (!(waiting_for_exec || waiting_for_prop)) {
+ am.ExecuteOneCommand();
}
+ if (!(waiting_for_exec || waiting_for_prop)) {
+ restart_processes();
- // If there's more work to do, wake up again immediately.
- if (am.HasMoreCommands()) epoll_timeout_ms = 0;
+ // If there's a process that needs restarting, wake up in time for that.
+ if (process_needs_restart_at != 0) {
+ epoll_timeout_ms = (process_needs_restart_at - time(nullptr)) * 1000;
+ if (epoll_timeout_ms < 0) epoll_timeout_ms = 0;
+ }
+
+ // If there's more work to do, wake up again immediately.
+ if (am.HasMoreCommands()) epoll_timeout_ms = 0;
+ }
epoll_event ev;
int nr = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd, &ev, 1, epoll_timeout_ms));
diff --git a/init/service.cpp b/init/service.cpp
index 35aaa56..c8d1cb1 100644
--- a/init/service.cpp
+++ b/init/service.cpp
@@ -889,15 +889,10 @@
}
}
- std::unique_ptr<Service> svc_p(new Service(name, "default", flags, uid, gid, supp_gids,
- no_capabilities, namespace_flags, seclabel,
- str_args));
- if (!svc_p) {
- LOG(ERROR) << "Couldn't allocate service for exec of '" << str_args[0] << "'";
- return nullptr;
- }
+ auto svc_p = std::make_unique<Service>(name, "default", flags, uid, gid, supp_gids,
+ no_capabilities, namespace_flags, seclabel, str_args);
Service* svc = svc_p.get();
- services_.push_back(std::move(svc_p));
+ services_.emplace_back(std::move(svc_p));
return svc;
}
diff --git a/init/util.cpp b/init/util.cpp
index b90e5b1..0ba9800 100644
--- a/init/util.cpp
+++ b/init/util.cpp
@@ -185,8 +185,8 @@
}
bool write_file(const char* path, const char* content) {
- android::base::unique_fd fd(
- TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0600)));
+ android::base::unique_fd fd(TEMP_FAILURE_RETRY(
+ open(path, O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0600)));
if (fd == -1) {
PLOG(ERROR) << "write_file: Unable to open '" << path << "'";
return false;
diff --git a/init/util_test.cpp b/init/util_test.cpp
index 24c75c4..4e82e76 100644
--- a/init/util_test.cpp
+++ b/init/util_test.cpp
@@ -17,9 +17,15 @@
#include "util.h"
#include <errno.h>
+#include <fcntl.h>
+
+#include <sys/stat.h>
#include <gtest/gtest.h>
+#include <android-base/stringprintf.h>
+#include <android-base/test_utils.h>
+
TEST(util, read_file_ENOENT) {
std::string s("hello");
errno = 0;
@@ -28,6 +34,35 @@
EXPECT_EQ("", s); // s was cleared.
}
+TEST(util, read_file_group_writeable) {
+ std::string s("hello");
+ TemporaryFile tf;
+ ASSERT_TRUE(tf.fd != -1);
+ EXPECT_TRUE(write_file(tf.path, s.c_str())) << strerror(errno);
+ EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0620, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
+ EXPECT_FALSE(read_file(tf.path, &s)) << strerror(errno);
+ EXPECT_EQ("", s); // s was cleared.
+}
+
+TEST(util, read_file_world_writeable) {
+ std::string s("hello");
+ TemporaryFile tf;
+ ASSERT_TRUE(tf.fd != -1);
+ EXPECT_TRUE(write_file(tf.path, s.c_str())) << strerror(errno);
+ EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0602, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
+ EXPECT_FALSE(read_file(tf.path, &s)) << strerror(errno);
+ EXPECT_EQ("", s); // s was cleared.
+}
+
+TEST(util, read_file_symbol_link) {
+ std::string s("hello");
+ errno = 0;
+ // lrwxrwxrwx 1 root root 13 1970-01-01 00:00 charger -> /sbin/healthd
+ EXPECT_FALSE(read_file("/charger", &s));
+ EXPECT_EQ(ELOOP, errno);
+ EXPECT_EQ("", s); // s was cleared.
+}
+
TEST(util, read_file_success) {
std::string s("hello");
EXPECT_TRUE(read_file("/proc/version", &s));
@@ -37,6 +72,42 @@
EXPECT_STREQ("Linux", s.c_str());
}
+TEST(util, write_file_not_exist) {
+ std::string s("hello");
+ std::string s2("hello");
+ TemporaryDir test_dir;
+ std::string path = android::base::StringPrintf("%s/does-not-exist", test_dir.path);
+ EXPECT_TRUE(write_file(path.c_str(), s.c_str()));
+ EXPECT_TRUE(read_file(path.c_str(), &s2));
+ EXPECT_EQ(s, s2);
+ struct stat sb;
+ int fd = open(path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
+ EXPECT_NE(-1, fd);
+ EXPECT_EQ(0, fstat(fd, &sb));
+ EXPECT_NE(0u, sb.st_mode & S_IRUSR);
+ EXPECT_NE(0u, sb.st_mode & S_IWUSR);
+ EXPECT_EQ(0u, sb.st_mode & S_IXUSR);
+ EXPECT_EQ(0u, sb.st_mode & S_IRGRP);
+ EXPECT_EQ(0u, sb.st_mode & S_IWGRP);
+ EXPECT_EQ(0u, sb.st_mode & S_IXGRP);
+ EXPECT_EQ(0u, sb.st_mode & S_IROTH);
+ EXPECT_EQ(0u, sb.st_mode & S_IWOTH);
+ EXPECT_EQ(0u, sb.st_mode & S_IXOTH);
+ EXPECT_EQ(0, unlink(path.c_str()));
+}
+
+TEST(util, write_file_exist) {
+ std::string s2("");
+ TemporaryFile tf;
+ ASSERT_TRUE(tf.fd != -1);
+ EXPECT_TRUE(write_file(tf.path, "1hello1")) << strerror(errno);
+ EXPECT_TRUE(read_file(tf.path, &s2));
+ EXPECT_STREQ("1hello1", s2.c_str());
+ EXPECT_TRUE(write_file(tf.path, "2hello2"));
+ EXPECT_TRUE(read_file(tf.path, &s2));
+ EXPECT_STREQ("2hello2", s2.c_str());
+}
+
TEST(util, decode_uid) {
EXPECT_EQ(0U, decode_uid("root"));
EXPECT_EQ(UINT_MAX, decode_uid("toot"));
diff --git a/libbacktrace/Backtrace.cpp b/libbacktrace/Backtrace.cpp
index 4354f0b..3545661 100644
--- a/libbacktrace/Backtrace.cpp
+++ b/libbacktrace/Backtrace.cpp
@@ -155,7 +155,7 @@
case BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST:
return "Thread doesn't exist";
case BACKTRACE_UNWIND_ERROR_THREAD_TIMEOUT:
- return "Thread has not repsonded to signal in time";
+ return "Thread has not responded to signal in time";
case BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION:
return "Attempt to use an unsupported feature";
case BACKTRACE_UNWIND_ERROR_NO_CONTEXT: