pdx: Rework error reporting when transfering file and channel handles
There is a lot of confusion about reporting errors when passing file
and channel handles over PDX transport between client and service.
Methods like Message::PushFileHandle return an integer which means
both a file handle reference value (if positive) and a possible error
code (if negative). But file handles could contain negative values too
(when they are empty). This is used frequently when passing buffer
fences around (when a fence is not being used, its fd is set to -1).
This results in a special case of when PushFileHandle is called with
a file handle with value of -1, the return value is actually "-errno"
which becomes dependent on a global state (errno is not set by
PushFileHandle itself in case file handle value is negative) and results
in unpredicted behavior (sometimes errno is 0, sometimes its >0).
Cleaned this all up by using Status<T> everywhere we used an int to
pass value payload along with possible error code.
Now the semantics of the calls are more clear.
Bug: 36866492
Test: `m -j32` for sailfish-eng succeeds
Ran unit tests on device (pdx_tests, libpdx_uds_tests, bufferhub_tests,
buffer_hub_queue-test, buffer_hub_queue_producer-test), all pass
Ran CubeSea, NativeTreasureHunt and Ithaca on Sailfish with vrflinger
enabled, was able to use controller and screen rendered correctly.
Change-Id: I0f40c3f356fcba8bc217d5219a0ddf9685e57fd7
diff --git a/libs/vr/libpdx/client.cpp b/libs/vr/libpdx/client.cpp
index c318628..bfa2d87 100644
--- a/libs/vr/libpdx/client.cpp
+++ b/libs/vr/libpdx/client.cpp
@@ -4,7 +4,6 @@
#include <log/log.h>
#include <pdx/trace.h>
-#include "errno_guard.h"
namespace android {
namespace pdx {
@@ -84,7 +83,6 @@
Status<void> Client::SendImpulse(int opcode) {
PDX_TRACE_NAME("Client::SendImpulse");
- ErrnoGuard errno_guard;
auto status = CheckReconnect();
if (!status)
@@ -98,7 +96,6 @@
Status<void> Client::SendImpulse(int opcode, const void* buffer,
size_t length) {
PDX_TRACE_NAME("Client::SendImpulse");
- ErrnoGuard errno_guard;
auto status = CheckReconnect();
if (!status)
@@ -110,7 +107,6 @@
}
void Client::Close(int error) {
- ErrnoGuard errno_guard;
channel_.reset();
// Normalize error codes to negative integer space.
error_ = error <= 0 ? error : -error;
@@ -228,37 +224,38 @@
CheckDisconnect(*ret);
}
-FileReference Transaction::PushFileHandle(const LocalHandle& handle) {
- return client_.CheckReconnect() && EnsureStateAllocated()
- ? client_.GetChannel()->PushFileHandle(state_, handle)
- : -1;
+Status<FileReference> Transaction::PushFileHandle(const LocalHandle& handle) {
+ if (client_.CheckReconnect() && EnsureStateAllocated())
+ return client_.GetChannel()->PushFileHandle(state_, handle);
+ return ErrorStatus{ESHUTDOWN};
}
-FileReference Transaction::PushFileHandle(const BorrowedHandle& handle) {
- return client_.CheckReconnect() && EnsureStateAllocated()
- ? client_.GetChannel()->PushFileHandle(state_, handle)
- : -1;
+Status<FileReference> Transaction::PushFileHandle(
+ const BorrowedHandle& handle) {
+ if (client_.CheckReconnect() && EnsureStateAllocated())
+ return client_.GetChannel()->PushFileHandle(state_, handle);
+ return ErrorStatus{ESHUTDOWN};
}
-FileReference Transaction::PushFileHandle(const RemoteHandle& handle) {
+Status<FileReference> Transaction::PushFileHandle(const RemoteHandle& handle) {
return handle.Get();
}
-ChannelReference Transaction::PushChannelHandle(
+Status<ChannelReference> Transaction::PushChannelHandle(
const LocalChannelHandle& handle) {
- return client_.CheckReconnect() && EnsureStateAllocated()
- ? client_.GetChannel()->PushChannelHandle(state_, handle)
- : -1;
+ if (client_.CheckReconnect() && EnsureStateAllocated())
+ return client_.GetChannel()->PushChannelHandle(state_, handle);
+ return ErrorStatus{ESHUTDOWN};
}
-ChannelReference Transaction::PushChannelHandle(
+Status<ChannelReference> Transaction::PushChannelHandle(
const BorrowedChannelHandle& handle) {
- return client_.CheckReconnect() && EnsureStateAllocated()
- ? client_.GetChannel()->PushChannelHandle(state_, handle)
- : -1;
+ if (client_.CheckReconnect() && EnsureStateAllocated())
+ return client_.GetChannel()->PushChannelHandle(state_, handle);
+ return ErrorStatus{ESHUTDOWN};
}
-ChannelReference Transaction::PushChannelHandle(
+Status<ChannelReference> Transaction::PushChannelHandle(
const RemoteChannelHandle& handle) {
return handle.value();
}