From 7235359d2efa838e20143e5e68ba60beb8725744 Mon Sep 17 00:00:00 2001 From: Luis Hector Chavez Date: Fri, 21 Sep 2018 12:27:02 -0700 Subject: [PATCH] init: Cleanly exit subcontext processes upon init's death This change makes the subcontext processes cleanly exit in the event of the init's socket being closed. If that was an accident, init will respawn the process immediately. Otherwise, it will just quietly go away. Bug: 80425914 Test: kill -9 $INIT_PID # Outside of an Android container Change-Id: I664f11d1b3700ea46857abf24857335fe28e92fa --- init/subcontext.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/init/subcontext.cpp b/init/subcontext.cpp index c2a21d45c..092c51ceb 100644 --- a/init/subcontext.cpp +++ b/init/subcontext.cpp @@ -62,7 +62,9 @@ constexpr size_t kBufferSize = 4096; Result ReadMessage(int socket) { char buffer[kBufferSize] = {}; auto result = TEMP_FAILURE_RETRY(recv(socket, buffer, sizeof(buffer), 0)); - if (result <= 0) { + if (result == 0) { + return Error(); + } else if (result < 0) { return ErrnoError(); } return std::string(buffer, result); @@ -175,6 +177,12 @@ void SubcontextProcess::MainLoop() { auto init_message = ReadMessage(init_fd_); if (!init_message) { + if (init_message.error_errno() == 0) { + // If the init file descriptor was closed, let's exit quietly. If + // this was accidental, init will restart us. If init died, this + // avoids calling abort(3) unnecessarily. + return; + } LOG(FATAL) << "Could not read message from init: " << init_message.error(); }