Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions starboard/android/shared/posix_emu/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
// limitations under the License.

#include <fcntl.h>
#include <sys/uio.h>
#include <unistd.h>

#include <string>
#include <vector>

#include "starboard/android/shared/asset_manager.h"
Expand All @@ -34,6 +37,7 @@ extern "C" {
int __real_close(int fildes);
int __real_open(const char* path, int oflag, ...);
int __real_openat(int dirfd, const char* path, int oflag, ...);
ssize_t __real_writev(int fd, const struct iovec* iov, int iovcnt);

int __wrap_close(int fildes) {
AssetManager* asset_manager = AssetManager::GetInstance();
Expand Down Expand Up @@ -68,4 +72,34 @@ int __wrap_open(const char* path, int oflag, ...) {
return __wrap_openat(AT_FDCWD, path, oflag);
}

// Route stdout/stderr writes, which musl flushes via writev, through SbLogRaw
// so they reach Android logcat.
ssize_t __wrap_writev(int fd, const struct iovec* iov, int iovcnt) {
if (fd == STDOUT_FILENO || fd == STDERR_FILENO) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve writes after stdout/stderr redirection

This check treats file descriptor numbers 1 and 2 as logcat forever, even after code redirects them with dup2/freopen. GoogleTest's capture support does exactly that by dup2(captured_fd, fd_) before reading the temp file back (third_party/googletest/src/googletest/src/gtest-port.cc:1150-1166), so any captured printf/fprintf flush that musl emits via writev will be reported as successfully written but sent to SbLogRaw instead of the capture file. That makes captured stdout/stderr and death-test stderr logs come back empty on the Android Starboard build this wrapper targets.

Useful? React with 👍 / 👎.

ssize_t total = 0;
std::string buffer;
for (int i = 0; i < iovcnt; ++i) {
if (iov[i].iov_base && iov[i].iov_len > 0) {
buffer.append(static_cast<const char*>(iov[i].iov_base),
iov[i].iov_len);
total += iov[i].iov_len;
}
}
// SbLogRaw is line-oriented; split on newlines and drop the terminators.
size_t start = 0, pos;
while ((pos = buffer.find('\n', start)) != std::string::npos) {
buffer[pos] = '\0';
if (pos > start) {
SbLogRaw(buffer.c_str() + start);
}
start = pos + 1;
}
if (start < buffer.size()) {
SbLogRaw(buffer.c_str() + start);
}
return total;
}
return __real_writev(fd, iov, iovcnt);
}

} // extern "C"
1 change: 1 addition & 0 deletions starboard/build/config/starboard_target_type.gni
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ template("starboard_platform_target") {
"-Wl,--wrap=open",
"-Wl,--wrap=openat",
"-Wl,--wrap=stat",
"-Wl,--wrap=writev",
]
}
}
Expand Down
Loading