init: create sockets before forking
There is a race condition with Service::Start and socket creation.
Since socket creation currently happens after the fork(), it's
possible that init can continue executing other commands before the
socket is created. If init starts another service that relies on that
socket, it isn't guaranteed to be available.
Particularly, we've seen this with hwservicemanager starting after
logd, but hwservicemanager's logs sometimes not showing up.
Bug: 140810300
Test: boot and logging functions correctly
Change-Id: Ib2932e836d345830cd38f3b556598508fd953058
diff --git a/init/service.cpp b/init/service.cpp
index 793a2b2..e8b75d4 100644
--- a/init/service.cpp
+++ b/init/service.cpp
@@ -441,6 +441,23 @@
LOG(INFO) << "starting service '" << name_ << "'...";
+ std::vector<Descriptor> descriptors;
+ for (const auto& socket : sockets_) {
+ if (auto result = socket.Create(scon)) {
+ descriptors.emplace_back(std::move(*result));
+ } else {
+ LOG(INFO) << "Could not create socket '" << socket.name << "': " << result.error();
+ }
+ }
+
+ for (const auto& file : files_) {
+ if (auto result = file.Create()) {
+ descriptors.emplace_back(std::move(*result));
+ } else {
+ LOG(INFO) << "Could not open file '" << file.name << "': " << result.error();
+ }
+ }
+
pid_t pid = -1;
if (namespaces_.flags) {
pid = clone(nullptr, nullptr, namespaces_.flags | SIGCHLD, nullptr);
@@ -460,16 +477,8 @@
setenv(key.c_str(), value.c_str(), 1);
}
- for (const auto& socket : sockets_) {
- if (auto result = socket.CreateAndPublish(scon); !result) {
- LOG(INFO) << "Could not create socket '" << socket.name << "': " << result.error();
- }
- }
-
- for (const auto& file : files_) {
- if (auto result = file.CreateAndPublish(); !result) {
- LOG(INFO) << "Could not open file '" << file.name << "': " << result.error();
- }
+ for (const auto& descriptor : descriptors) {
+ descriptor.Publish();
}
if (auto result = WritePidToFiles(&writepid_files_); !result) {