libpdx_uds: Handle EACCES error when connecting to PDX service
There is a race condition in the way init process creates socket
files before it forks into a child (see system/core/init/util.cpp,
function create_socket()). It first creates a socket, then calls
bind() which creates a file entry, then calls chown/chmod to change
ownership and access permissions of that file object. If a client
process connects to the socket just after bind() was called but before
chmod(), the socket file will have incorrect permissions and the
connect call will be aborted with EACCESS (Permission denied) error.
We should retry connection in case we get EACCESS. A proper fix would
be is to find a way for init to create the file entry with the proper
permissions to start with, and eliminate the race condition altogether.
Bug: 37171113
Test: `lunch sailfish-eng && m -j32` succeeds
Device boots correctly.
Error recovery is handled correctly even after adding sleep(5)
to init process just before calling chmod on the socket.
Change-Id: If97c316daa0c0be5ff7b5aa302a69aa60e9fb237
diff --git a/libs/vr/libpdx_uds/client_channel_factory.cpp b/libs/vr/libpdx_uds/client_channel_factory.cpp
index 323236d..9202cd5 100644
--- a/libs/vr/libpdx_uds/client_channel_factory.cpp
+++ b/libs/vr/libpdx_uds/client_channel_factory.cpp
@@ -65,6 +65,7 @@
auto time_end = now + std::chrono::milliseconds{timeout_ms};
bool connected = false;
+ int max_eaccess = 5; // Max number of times to retry when EACCES returned.
while (!connected) {
int64_t timeout = -1;
if (use_timeout) {
@@ -84,10 +85,14 @@
if (ret == -1) {
ALOGD("ClientChannelFactory: Connect error %d: %s", errno,
strerror(errno));
- if (errno == ECONNREFUSED) {
- // Connection refused can be the result of connecting too early (the
- // service socket is created but not being listened to yet).
- ALOGD("ClientChannelFactory: Connection refused, waiting...");
+ // if |max_eaccess| below reaches zero when errno is EACCES, the control
+ // flows into the next "else if" statement and a permanent error is
+ // returned from this function.
+ if (errno == ECONNREFUSED || (errno == EACCES && max_eaccess-- > 0)) {
+ // Connection refused/Permission denied can be the result of connecting
+ // too early (the service socket is created but its access rights are
+ // not set or not being listened to yet).
+ ALOGD("ClientChannelFactory: %s, waiting...", strerror(errno));
using namespace std::literals::chrono_literals;
std::this_thread::sleep_for(100ms);
} else if (errno != ENOENT && errno != ENOTDIR) {