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_utils.h b/init/service_utils.h
index befce25..d2e69d9 100644
--- a/init/service_utils.h
+++ b/init/service_utils.h
@@ -22,6 +22,7 @@
#include <string>
#include <vector>
+#include <android-base/unique_fd.h>
#include <cutils/iosched_policy.h>
#include "result.h"
@@ -29,6 +30,18 @@
namespace android {
namespace init {
+class Descriptor {
+ public:
+ Descriptor(const std::string& name, android::base::unique_fd fd)
+ : name_(name), fd_(std::move(fd)){};
+
+ void Publish() const;
+
+ private:
+ std::string name_;
+ android::base::unique_fd fd_;
+};
+
struct SocketDescriptor {
std::string name;
int type = 0;
@@ -38,14 +51,14 @@
std::string context;
bool passcred = false;
- Result<void> CreateAndPublish(const std::string& global_context) const;
+ Result<Descriptor> Create(const std::string& global_context) const;
};
struct FileDescriptor {
std::string name;
std::string type;
- Result<void> CreateAndPublish() const;
+ Result<Descriptor> Create() const;
};
struct NamespaceInfo {