Merge "Explain /dev/socket/heapprofd SELinux domain issue."
diff --git a/src/profiling/memory/heapprofd_producer.cc b/src/profiling/memory/heapprofd_producer.cc
index 187499f5..ca00947 100644
--- a/src/profiling/memory/heapprofd_producer.cc
+++ b/src/profiling/memory/heapprofd_producer.cc
@@ -137,32 +137,9 @@
       mode_(mode),
       unwinding_workers_(MakeUnwindingWorkers(this, kUnwinderThreads)),
       socket_delegate_(this),
-      weak_factory_(this) {
-  if (mode == HeapprofdMode::kCentral) {
-    listening_socket_ = MakeListeningSocket();
-  }
-}
+      weak_factory_(this) {}
 
-HeapprofdProducer::~HeapprofdProducer() {
-  // We only borrowed this from the environment variable.
-  // UnixSocket always owns the socket, so we need to manually release it
-  // here.
-  if (mode_ == HeapprofdMode::kCentral && bool(listening_socket_))
-    listening_socket_->ReleaseSocket().ReleaseFd().release();
-}
-
-std::unique_ptr<base::UnixSocket> HeapprofdProducer::MakeListeningSocket() {
-  const char* sock_fd = getenv(kHeapprofdSocketEnvVar);
-  if (sock_fd == nullptr)
-    PERFETTO_FATAL("Did not inherit socket from init.");
-  char* end;
-  int raw_fd = static_cast<int>(strtol(sock_fd, &end, 10));
-  if (*end != '\0')
-    PERFETTO_FATAL("Invalid %s. Expected decimal integer.",
-                   kHeapprofdSocketEnvVar);
-  return base::UnixSocket::Listen(base::ScopedFile(raw_fd), &socket_delegate_,
-                                  task_runner_);
-}
+HeapprofdProducer::~HeapprofdProducer() = default;
 
 void HeapprofdProducer::SetTargetProcess(pid_t target_pid,
                                          std::string target_cmdline,
diff --git a/src/profiling/memory/heapprofd_producer.h b/src/profiling/memory/heapprofd_producer.h
index a84e462..6493294 100644
--- a/src/profiling/memory/heapprofd_producer.h
+++ b/src/profiling/memory/heapprofd_producer.h
@@ -153,6 +153,10 @@
   void SetProducerEndpoint(
       std::unique_ptr<TracingService::ProducerEndpoint> endpoint);
 
+  base::UnixSocket::EventListener& socket_delegate() {
+    return socket_delegate_;
+  }
+
  private:
   // State of the connection to tracing service (traced).
   enum State {
@@ -220,9 +224,6 @@
   void SetStartupProperties(DataSource* data_source);
   void SignalRunningProcesses(DataSource* data_source);
 
-  // Specific to mode_ == kCentral
-  std::unique_ptr<base::UnixSocket> MakeListeningSocket();
-
   // Specific to mode_ == kChild
   void TerminateProcess(int exit_status);
   // Specific to mode_ == kChild
@@ -263,9 +264,6 @@
   std::map<DataSourceInstanceID, DataSource> data_sources_;
   std::vector<UnwindingWorker> unwinding_workers_;
 
-  // Specific to mode_ == kCentral
-  std::unique_ptr<base::UnixSocket> listening_socket_;
-
   // Specific to mode_ == kChild
   Process target_process_{base::kInvalidPid, ""};
   // This is a valid FD only between SetTargetProcess and
diff --git a/src/profiling/memory/main.cc b/src/profiling/memory/main.cc
index a66c6aa..e536652 100644
--- a/src/profiling/memory/main.cc
+++ b/src/profiling/memory/main.cc
@@ -45,6 +45,18 @@
                         base::ScopedFile inherited_sock_fd);
 int StartCentralHeapprofd();
 
+int GetListeningSocket() {
+  const char* sock_fd = getenv(kHeapprofdSocketEnvVar);
+  if (sock_fd == nullptr)
+    PERFETTO_FATAL("Did not inherit socket from init.");
+  char* end;
+  int raw_fd = static_cast<int>(strtol(sock_fd, &end, 10));
+  if (*end != '\0')
+    PERFETTO_FATAL("Invalid %s. Expected decimal integer.",
+                   kHeapprofdSocketEnvVar);
+  return raw_fd;
+}
+
 base::Event* g_dump_evt = nullptr;
 
 int HeapprofdMain(int argc, char** argv) {
@@ -145,6 +157,11 @@
   base::Watchdog::GetInstance()->Start();  // crash on exceedingly long tasks
   HeapprofdProducer producer(HeapprofdMode::kCentral, &task_runner);
 
+  int listening_raw_socket = GetListeningSocket();
+  auto listening_socket =
+      base::UnixSocket::Listen(base::ScopedFile(listening_raw_socket),
+                               &producer.socket_delegate(), &task_runner);
+
   struct sigaction action = {};
   action.sa_handler = [](int) { g_dump_evt->Notify(); };
   // Allow to trigger a full dump by sending SIGUSR1 to heapprofd.