/dev/(u)random return eperm on write, update ioctl fionread to linux behavior, cleanup tests

This commit is contained in:
Gavin Hayes 2024-05-03 02:49:44 -04:00
parent 542b817cc8
commit 1c9f462e77
3 changed files with 17 additions and 20 deletions

View file

@ -106,10 +106,10 @@ static int ioctl_fionread(int fd, uint32_t *arg) {
int bytes = CountConsoleInputBytes(); int bytes = CountConsoleInputBytes();
*arg = MAX(0, bytes); *arg = MAX(0, bytes);
return 0; return 0;
} else if (g_fds.p[fd].kind == kFdDevNull || } else if (g_fds.p[fd].kind == kFdDevNull) {
g_fds.p[fd].kind == kFdDevRandom) { return enotty();
*arg = 1; } else if (g_fds.p[fd].kind == kFdDevRandom) {
return 0; return einval();
} else if (GetFileType(handle) == kNtFileTypePipe) { } else if (GetFileType(handle) == kNtFileTypePipe) {
uint32_t avail; uint32_t avail;
if (PeekNamedPipe(handle, 0, 0, 0, &avail, 0)) { if (PeekNamedPipe(handle, 0, 0, 0, &avail, 0)) {

View file

@ -52,9 +52,9 @@ static textwindows ssize_t sys_write_nt_impl(int fd, void *data, size_t size,
struct Fd *f = g_fds.p + fd; struct Fd *f = g_fds.p + fd;
bool isconsole = f->kind == kFdConsole; bool isconsole = f->kind == kFdConsole;
// not implemented // not implemented, XNU returns eperm();
if (f->kind == kFdDevRandom) { if (f->kind == kFdDevRandom) {
return enosys(); return eperm();
} }
// determine win32 handle for writing // determine win32 handle for writing

View file

@ -27,9 +27,12 @@
int pipefd[2]; int pipefd[2];
int stdoutBack; int stdoutBack;
int allowMask;
void SetUpOnce(void) { void SetUpOnce(void) {
testlib_enable_tmp_setup_teardown(); testlib_enable_tmp_setup_teardown();
// qemu-aarch64 defines o_largefile wrong
allowMask = ~(O_LARGEFILE | 0x00008000);
} }
void CaptureStdout(void) { void CaptureStdout(void) {
@ -47,8 +50,7 @@ void RestoreStdout(void) {
TEST(specialfile, devNull) { TEST(specialfile, devNull) {
ASSERT_SYS(0, 3, creat("/dev/null", 0644)); ASSERT_SYS(0, 3, creat("/dev/null", 0644));
// qemu-aarch64 defines o_largefile wrong ASSERT_EQ(O_WRONLY, fcntl(3, F_GETFL) & allowMask);
ASSERT_EQ(O_WRONLY, fcntl(3, F_GETFL) & ~(O_LARGEFILE | 0x00008000));
ASSERT_SYS(0, 2, write(3, "hi", 2)); ASSERT_SYS(0, 2, write(3, "hi", 2));
ASSERT_SYS(0, 2, pwrite(3, "hi", 2, 0)); ASSERT_SYS(0, 2, pwrite(3, "hi", 2, 0));
ASSERT_SYS(0, 2, pwrite(3, "hi", 2, 2)); ASSERT_SYS(0, 2, pwrite(3, "hi", 2, 2));
@ -65,8 +67,7 @@ TEST(specialfile, devNull) {
TEST(specialfile, devNullRead) { TEST(specialfile, devNullRead) {
char buf[8] = {0}; char buf[8] = {0};
ASSERT_SYS(0, 3, open("/dev/null", O_RDONLY)); ASSERT_SYS(0, 3, open("/dev/null", O_RDONLY));
// qemu-aarch64 defines o_largefile wrong ASSERT_EQ(O_RDONLY, fcntl(3, F_GETFL) & allowMask);
ASSERT_EQ(O_RDONLY, fcntl(3, F_GETFL) & ~(O_LARGEFILE | 0x00008000));
ASSERT_SYS(0, 0, read(3, buf, 8)); ASSERT_SYS(0, 0, read(3, buf, 8));
ASSERT_SYS(0, 0, close(3)); ASSERT_SYS(0, 0, close(3));
} }
@ -74,8 +75,7 @@ TEST(specialfile, devNullRead) {
TEST(specialfile, devRandomRead) { TEST(specialfile, devRandomRead) {
char buf[8] = {0}; char buf[8] = {0};
ASSERT_SYS(0, 3, open("/dev/random", O_RDONLY)); ASSERT_SYS(0, 3, open("/dev/random", O_RDONLY));
// qemu-aarch64 defines o_largefile wrong ASSERT_EQ(O_RDONLY, fcntl(3, F_GETFL) & allowMask);
ASSERT_EQ(O_RDONLY, fcntl(3, F_GETFL) & ~(O_LARGEFILE | 0x00008000));
ASSERT_SYS(0, 8, read(3, buf, 8)); ASSERT_SYS(0, 8, read(3, buf, 8));
ASSERT_NE(0, memcmp(buf, " ", 8)); ASSERT_NE(0, memcmp(buf, " ", 8));
ASSERT_SYS(0, 0, close(3)); ASSERT_SYS(0, 0, close(3));
@ -84,8 +84,7 @@ TEST(specialfile, devRandomRead) {
TEST(specialfile, devUrandomRead) { TEST(specialfile, devUrandomRead) {
char buf[8] = {0}; char buf[8] = {0};
ASSERT_SYS(0, 3, open("/dev/urandom", O_RDONLY)); ASSERT_SYS(0, 3, open("/dev/urandom", O_RDONLY));
// qemu-aarch64 defines o_largefile wrong ASSERT_EQ(O_RDONLY, fcntl(3, F_GETFL) & allowMask);
ASSERT_EQ(O_RDONLY, fcntl(3, F_GETFL) & ~(O_LARGEFILE | 0x00008000));
ASSERT_SYS(0, 8, read(3, buf, 8)); ASSERT_SYS(0, 8, read(3, buf, 8));
ASSERT_NE(0, memcmp(buf, " ", 8)); ASSERT_NE(0, memcmp(buf, " ", 8));
ASSERT_SYS(0, 0, close(3)); ASSERT_SYS(0, 0, close(3));
@ -97,9 +96,8 @@ TEST(specialfile, devRandomWrite_fails_on_nt) {
} }
char buf[8] = {0}; char buf[8] = {0};
ASSERT_SYS(0, 3, creat("/dev/random", 0644)); ASSERT_SYS(0, 3, creat("/dev/random", 0644));
// qemu-aarch64 defines o_largefile wrong ASSERT_EQ(O_WRONLY, fcntl(3, F_GETFL) & allowMask);
ASSERT_EQ(O_WRONLY, fcntl(3, F_GETFL) & ~(O_LARGEFILE | 0x00008000)); ASSERT_SYS(EPERM, -1, write(3, buf, 8));
ASSERT_SYS(ENOSYS, -1, write(3, buf, 8));
ASSERT_SYS(0, 0, close(3)); ASSERT_SYS(0, 0, close(3));
} }
@ -109,9 +107,8 @@ TEST(specialfile, devUrandomWrite_fails_on_nt) {
} }
char buf[8] = {0}; char buf[8] = {0};
ASSERT_SYS(0, 3, creat("/dev/urandom", 0644)); ASSERT_SYS(0, 3, creat("/dev/urandom", 0644));
// qemu-aarch64 defines o_largefile wrong ASSERT_EQ(O_WRONLY, fcntl(3, F_GETFL) & allowMask);
ASSERT_EQ(O_WRONLY, fcntl(3, F_GETFL) & ~(O_LARGEFILE | 0x00008000)); ASSERT_SYS(EPERM, -1, write(3, buf, 8));
ASSERT_SYS(ENOSYS, -1, write(3, buf, 8));
ASSERT_SYS(0, 0, close(3)); ASSERT_SYS(0, 0, close(3));
} }