feat(libkernel): implement tcgetattr and tcsetattr

A title's descriptors are taken as never being terminals: a file or
pipe gets ENOTTY with the termios untouched, a socket gets what its
protocol answers a termios ioctl on FreeBSD (EADDRNOTAVAIL for AF_INET,
EOPNOTSUPP otherwise), and a closed or invalid descriptor EBADF.
tcsetattr gives EINVAL for an action other than TCSANOW, TCSADRAIN or
TCSAFLUSH, with or without TCSASOFT, before anything else, and EFAULT
for a null termios before the descriptor is checked; with TCSASOFT a
null termios throws, where FreeBSD's libc faults copying it. What the
console gives a title as descriptors 0 to 2 is unknown, as recorded in
TechnicalDebt.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Jan Šlégl
2026-10-08 19:46:17 +02:00
committed by Adrià Franch
parent 87b998a99a
commit 14cc6497d2
6 changed files with 188 additions and 0 deletions
+11
View File
@@ -5,6 +5,7 @@ add_library(libkernel SHARED EXCLUDE_FROM_ALL
${CMAKE_CURRENT_SOURCE_DIR}/Equeue/src/EventAccessors.cpp
${CMAKE_CURRENT_SOURCE_DIR}/File/src/DirectoryDescriptor.cpp
${CMAKE_CURRENT_SOURCE_DIR}/File/src/NativeStat.cpp
${CMAKE_CURRENT_SOURCE_DIR}/File/src/Terminal.cpp
${CMAKE_CURRENT_SOURCE_DIR}/File/src/Open.cpp
${CMAKE_CURRENT_SOURCE_DIR}/File/src/Stdio.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Semaphore/src/Semaphore.cpp
@@ -133,6 +134,16 @@ if(BUILD_TESTING)
set_tests_properties(guest_filesystem PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
endif()
add_test_executable(guest_terminal_tests ${LIBS_INCLUDE_DIR}/tests/GuestTerminal.cpp)
target_include_directories(guest_terminal_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_terminal_tests PRIVATE libkernel libc)
configure_windows_unwind(guest_terminal_tests)
add_test(NAME guest_terminal COMMAND guest_terminal_tests)
set_tests_properties(guest_terminal PROPERTIES TIMEOUT 20)
if(WIN32)
set_tests_properties(guest_terminal PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
endif()
add_test_executable(guest_kernel_sync_tests ${LIBS_INCLUDE_DIR}/tests/GuestKernelSync.cpp)
target_include_directories(guest_kernel_sync_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_kernel_sync_tests PRIVATE libkernel libc)
@@ -0,0 +1,72 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libkernel/Socket/include/SocketRuntime.hpp"
#include <cstdint>
#include <stdexcept>
#ifdef _WIN32
#include <io.h>
#include <windows.h>
#else
#include <fcntl.h>
#include <unistd.h>
#endif
extern "C" int* APS5_VABI __error_nid_postfix();
#ifdef _WIN32
extern "C" _invalid_parameter_handler _set_thread_local_invalid_parameter_handler(_invalid_parameter_handler);
#endif
namespace {
constexpr int BadDescriptor = 9;
constexpr int BadAddress = 14;
constexpr int InvalidArgument = 22;
constexpr int NotTerminal = 25;
constexpr int OperationNotSupported = 45;
constexpr int AddressNotAvailable = 49;
constexpr int SetNow = 0;
constexpr int SetDrain = 1;
constexpr int SetFlush = 2;
constexpr int SetSoft = 0x10;
constexpr int InternetFamily = 2;
int Fail(int error) {
*__error_nid_postfix() = error;
return -1;
}
#ifdef _WIN32
void IgnoreInvalidParameter(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, std::uintptr_t) {}
bool HostOpen(int descriptor) {
const auto previous = _set_thread_local_invalid_parameter_handler(IgnoreInvalidParameter);
const auto handle = ::_get_osfhandle(descriptor);
_set_thread_local_invalid_parameter_handler(previous);
return reinterpret_cast<HANDLE>(handle) != INVALID_HANDLE_VALUE;
}
#else
bool HostOpen(int descriptor) { return ::fcntl(descriptor, F_GETFD) != -1; }
#endif
int TerminalError(int descriptor) {
if (descriptor >= GuestSockets::FirstDescriptor) {
const int family = GuestSockets::Family(descriptor);
if (family < 0) return BadDescriptor;
return family == InternetFamily ? AddressNotAvailable : OperationNotSupported;
}
if (descriptor < 0 || !HostOpen(descriptor)) return BadDescriptor;
return NotTerminal;
}
}
extern "C" {
int APS5_VABI tcgetattr_nid_postfix(int descriptor, void* attributes) {
static_cast<void>(attributes);
return Fail(TerminalError(descriptor));
}
int APS5_VABI tcsetattr_nid_postfix(int descriptor, int action, const void* attributes) {
if ((action & SetSoft) != 0 && attributes == nullptr) throw std::invalid_argument("tcsetattr: attributes is null");
const int mode = action & ~SetSoft;
if (mode != SetNow && mode != SetDrain && mode != SetFlush) return Fail(InvalidArgument);
if (attributes == nullptr) return Fail(BadAddress);
return Fail(TerminalError(descriptor));
}
}
@@ -3,4 +3,5 @@ namespace GuestSockets {
constexpr int FirstDescriptor = 0x10000000;
int Close(int descriptor);
bool IsOpen(int descriptor);
int Family(int descriptor);
}
@@ -180,6 +180,11 @@ bool GuestSockets::IsOpen(int descriptor) {
std::lock_guard lock(socketsMutex);
return sockets.contains(descriptor);
}
int GuestSockets::Family(int descriptor) {
std::lock_guard lock(socketsMutex);
const auto found = sockets.find(descriptor);
return found == sockets.end() ? -1 : found->second->family;
}
namespace {
#ifdef _WIN32
+97
View File
@@ -0,0 +1,97 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <string>
extern "C" {
int APS5_VABI tcgetattr_nid_postfix(int, void*);
int APS5_VABI tcsetattr_nid_postfix(int, int, const void*);
int APS5_VABI open_nid_postfix(const char*, int, int);
int APS5_VABI pipe_nid_postfix(int*);
int APS5_VABI socket_nid_postfix(int, int, int);
int APS5_VABI close_nid_postfix(int);
int* APS5_VABI __error_nid_postfix();
}
static void Check(bool value, int line) {
if (!value) {
std::fprintf(stderr, "terminal check failed at line %d, errno %d\n", line, *__error_nid_postfix());
std::abort();
}
}
#define Require(value) Check((value), __LINE__)
constexpr int Now = 0;
constexpr int Drain = 1;
constexpr int Flush = 2;
constexpr int Soft = 0x10;
constexpr int NotTerminal = 25;
constexpr int BadDescriptor = 9;
constexpr int InvalidArgument = 22;
constexpr int BadAddress = 14;
constexpr int AddressNotAvailable = 49;
static bool Fails(int result, int error) { return result == -1 && *__error_nid_postfix() == error; }
static void RequireNotTerminal(int descriptor) {
unsigned char attributes[44];
std::memset(attributes, 0xaa, sizeof(attributes));
unsigned char untouched[44];
std::memset(untouched, 0xaa, sizeof(untouched));
*__error_nid_postfix() = 0;
Require(Fails(tcgetattr_nid_postfix(descriptor, attributes), NotTerminal));
Require(std::memcmp(attributes, untouched, sizeof(attributes)) == 0);
for (const int action : {Now, Drain, Flush, Now | Soft, Flush | Soft}) {
*__error_nid_postfix() = 0;
Require(Fails(tcsetattr_nid_postfix(descriptor, action, attributes), NotTerminal));
}
}
int main() {
const auto name = "anyps5-terminal-test-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count());
const int file = open_nid_postfix(name.c_str(), 0x202, 0644);
Require(file >= 0);
RequireNotTerminal(file);
RequireNotTerminal(0);
RequireNotTerminal(1);
RequireNotTerminal(2);
int ends[2] = {-1, -1};
Require(pipe_nid_postfix(ends) == 0);
RequireNotTerminal(ends[0]);
RequireNotTerminal(ends[1]);
const int socket = socket_nid_postfix(2, 2, 0);
Require(socket >= 0);
unsigned char untouched[44];
std::memset(untouched, 0xaa, sizeof(untouched));
unsigned char probe[44];
std::memset(probe, 0xaa, sizeof(probe));
Require(Fails(tcgetattr_nid_postfix(socket, probe), AddressNotAvailable));
Require(std::memcmp(probe, untouched, sizeof(probe)) == 0);
Require(Fails(tcsetattr_nid_postfix(socket, Flush, probe), AddressNotAvailable));
unsigned char attributes[44]{};
Require(Fails(tcsetattr_nid_postfix(file, 3, attributes), InvalidArgument));
Require(Fails(tcsetattr_nid_postfix(file, -1, attributes), InvalidArgument));
Require(Fails(tcsetattr_nid_postfix(file, Soft | 3, attributes), InvalidArgument));
Require(Fails(tcsetattr_nid_postfix(-1, 3, attributes), InvalidArgument));
Require(Fails(tcsetattr_nid_postfix(file, Now, nullptr), BadAddress));
Require(Fails(tcsetattr_nid_postfix(-1, Now, nullptr), BadAddress));
Require(Fails(tcgetattr_nid_postfix(-1, attributes), BadDescriptor));
Require(Fails(tcsetattr_nid_postfix(-1, Now, attributes), BadDescriptor));
Require(close_nid_postfix(socket) == 0);
Require(Fails(tcgetattr_nid_postfix(socket, attributes), BadDescriptor));
Require(Fails(tcsetattr_nid_postfix(socket, Now, attributes), BadDescriptor));
Require(close_nid_postfix(ends[0]) == 0 && close_nid_postfix(ends[1]) == 0);
Require(close_nid_postfix(file) == 0);
Require(Fails(tcgetattr_nid_postfix(file, attributes), BadDescriptor));
Require(Fails(tcsetattr_nid_postfix(file, Flush, attributes), BadDescriptor));
std::filesystem::remove(name);
return 0;
}
+2
View File
@@ -233,6 +233,8 @@ Throughout the project, every function at every stage either **does exactly what
- [scePlayerReviewDialogOpen, scePlayerReviewDialogGetResult](../../core/libs/prx/libSceCdlgPlayerReview/Export.cpp) (libSceCdlgPlayerReview) - the PS5-only dialog has no public reference: the parameter is not read and the result is assumed to start with the 32-bit common dialog result, as `sceWebBrowserDialogGetResult` assumes
- [sceHttpWaitRequest](../../core/libs/prx/libSceHttp/Export.cpp) (libSceHttp) - the timeout is assumed to be in microseconds and a negative timeout to wait without limit; the invalid-value error for a null handle or event array or a non-positive event count is assumed, not measured. No request is ever pending, so the call waits for the timeout and returns 0 events, and a wait without limit throws
- [tcgetattr, tcsetattr](../../core/libs/prx/libkernel/File/src/Terminal.cpp) (libkernel) - every file and pipe descriptor is taken as not a terminal (`ENOTTY`), and a socket gets `EADDRNOTAVAIL` for `AF_INET` and `EOPNOTSUPP` otherwise, as FreeBSD's `in_control` and `pru_control_notsupp` answer a termios ioctl; what descriptors 0 to 2 of a title are on the console, and whether any of them is a terminal there, is unknown
### Functional
- [libSceOpusDec](../../core/libs/prx/libSceOpusDec/Export.cpp) uses FFmpeg for 48000 Hz mono/stereo signed-16 decoding. The six signatures, 640-byte opaque state allocation and byte-count return follow [public PS5 decoder research](https://github.com/blackbearreloaded/ps5-hardware-audio-decoding/blob/26be5a12f92b2cdc67f8a9e082fd69ae0e9a35a2/docs/DECODING.md#direct-opus-abi) and [ProsperoRadio](https://github.com/blackbearreloaded/ProsperoRadio/blob/2c4029c3a8f43e9e1dfef98374d94d99b14d745d/src/opus_decoder.cpp). Console PCM equivalence and error codes are unverified. Other sample rates, packet loss concealment, invalid inputs and termination with live decoders throw. Context identifiers and host decoder state are private registries; the firmware's opaque state layout and error recovery are unknown.