Skip to content

Commit b6e40a3

Browse files
authored
Add /dev/(u)random on NT (#1163)
1 parent 8f6bc9d commit b6e40a3

File tree

17 files changed

+97
-22
lines changed

17 files changed

+97
-22
lines changed

libc/calls/fcntl-nt.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ textwindows int sys_fcntl_nt(int fd, int cmd, uintptr_t arg) {
332332
if (__isfdkind(fd, kFdFile) || //
333333
__isfdkind(fd, kFdSocket) || //
334334
__isfdkind(fd, kFdConsole) || //
335-
__isfdkind(fd, kFdDevNull)) {
335+
__isfdkind(fd, kFdDevNull) || //
336+
__isfdkind(fd, kFdDevRandom)) {
336337
if (cmd == F_GETFL) {
337338
rc = g_fds.p[fd].flags & (O_ACCMODE | _O_APPEND | _O_DIRECT |
338339
_O_NONBLOCK | _O_RANDOM | _O_SEQUENTIAL);

libc/calls/fstat-nt.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ textwindows int sys_fstat_nt(int fd, struct stat *st) {
104104
return ebadf();
105105
case kFdConsole:
106106
case kFdDevNull:
107+
case kFdDevRandom:
107108
return sys_fstat_nt_special(g_fds.p[fd].kind, st);
108109
case kFdSocket:
109110
return sys_fstat_nt_socket(g_fds.p[fd].kind, st);

libc/calls/fstatat-nt.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ textwindows int sys_fstatat_nt(int dirfd, const char *path, struct stat *st,
5959
return sys_fstat_nt_special(kFdConsole, st);
6060
} else if (!strcmp(path + 5, "null")) {
6161
return sys_fstat_nt_special(kFdDevNull, st);
62+
} else if (!strcmp(path + 5, "random") || !strcmp(path + 5, "urandom")) {
63+
return sys_fstat_nt_special(kFdDevRandom, st);
6264
} else if (!strcmp(path + 5, "stdin")) {
6365
return sys_fstat_nt(STDIN_FILENO, st);
6466
} else if (!strcmp(path + 5, "stdout")) {

libc/calls/ioctl.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "libc/calls/termios.h"
2525
#include "libc/dce.h"
2626
#include "libc/errno.h"
27-
#include "libc/serialize.h"
2827
#include "libc/intrin/cmpxchg.h"
2928
#include "libc/intrin/strace.internal.h"
3029
#include "libc/intrin/weaken.h"
@@ -42,6 +41,7 @@
4241
#include "libc/nt/winsock.h"
4342
#include "libc/runtime/runtime.h"
4443
#include "libc/runtime/stack.h"
44+
#include "libc/serialize.h"
4545
#include "libc/sock/internal.h"
4646
#include "libc/sock/struct/ifconf.h"
4747
#include "libc/sock/struct/ifreq.h"
@@ -66,7 +66,7 @@ static struct HostAdapterInfoNode {
6666
struct sockaddr netmask;
6767
struct sockaddr broadcast;
6868
short flags;
69-
} *__hostInfo;
69+
} * __hostInfo;
7070

7171
static int ioctl_default(int fd, unsigned long request, void *arg) {
7272
int rc;
@@ -107,8 +107,9 @@ static int ioctl_fionread(int fd, uint32_t *arg) {
107107
*arg = MAX(0, bytes);
108108
return 0;
109109
} else if (g_fds.p[fd].kind == kFdDevNull) {
110-
*arg = 1;
111-
return 0;
110+
return enotty();
111+
} else if (g_fds.p[fd].kind == kFdDevRandom) {
112+
return einval();
112113
} else if (GetFileType(handle) == kNtFileTypePipe) {
113114
uint32_t avail;
114115
if (PeekNamedPipe(handle, 0, 0, 0, &avail, 0)) {

libc/calls/ischardev.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ bool32 ischardev(int fd) {
5555
}
5656
} else {
5757
return __isfdkind(fd, kFdConsole) || __isfdkind(fd, kFdDevNull) ||
58+
__isfdkind(fd, kFdDevRandom) ||
5859
(__isfdkind(fd, kFdFile) &&
5960
GetFileType(g_fds.p[fd].handle) == kNtFileTypeChar);
6061
}

libc/calls/lseek-nt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static textwindows int64_t Seek(struct Fd *f, int64_t offset, int whence) {
6262
}
6363

6464
textwindows int64_t sys_lseek_nt(int fd, int64_t offset, int whence) {
65-
if (__isfdkind(fd, kFdDevNull)) {
65+
if (__isfdkind(fd, kFdDevNull) || __isfdkind(fd, kFdDevRandom)) {
6666
return offset;
6767
} else if (__isfdkind(fd, kFdFile)) {
6868
struct Fd *f = g_fds.p + fd;

libc/calls/open-nt.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ static textwindows int sys_open_nt_special(int fd, int flags, int mode,
159159
return fd;
160160
}
161161

162+
static textwindows int sys_open_nt_no_handle(int fd, int flags, int mode,
163+
int kind) {
164+
g_fds.p[fd].kind = kind;
165+
g_fds.p[fd].mode = mode;
166+
g_fds.p[fd].flags = flags;
167+
g_fds.p[fd].handle = -1;
168+
return fd;
169+
}
170+
162171
static textwindows int sys_open_nt_dup(int fd, int flags, int mode, int oldfd) {
163172
int64_t handle;
164173
if (!__isfdopen(oldfd)) {
@@ -211,6 +220,8 @@ textwindows int sys_open_nt(int dirfd, const char *file, uint32_t flags,
211220
rc = sys_open_nt_special(fd, flags, mode, kFdConsole, u"CONIN$");
212221
} else if (!strcmp(file + 5, "null")) {
213222
rc = sys_open_nt_special(fd, flags, mode, kFdDevNull, u"NUL");
223+
} else if (!strcmp(file + 5, "urandom") || !strcmp(file + 5, "random")) {
224+
rc = sys_open_nt_no_handle(fd, flags, mode, kFdDevRandom);
214225
} else if (!strcmp(file + 5, "stdin")) {
215226
rc = sys_open_nt_dup(fd, flags, mode, STDIN_FILENO);
216227
} else if (!strcmp(file + 5, "stdout")) {

libc/calls/pread.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ ssize_t pread(int fd, void *buf, size_t size, int64_t offset) {
7272
rc = sys_pread(fd, buf, size, offset, offset);
7373
} else if (__isfdkind(fd, kFdSocket)) {
7474
rc = espipe();
75-
} else if (__isfdkind(fd, kFdFile) || __isfdkind(fd, kFdDevNull)) {
75+
} else if (__isfdkind(fd, kFdFile) || __isfdkind(fd, kFdDevNull) ||
76+
__isfdkind(fd, kFdDevRandom)) {
7677
rc = sys_read_nt(fd, (struct iovec[]){{buf, size}}, 1, offset);
7778
} else {
7879
rc = ebadf();

libc/calls/printfds.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ static const char *__fdkind2str(int x) {
4040
return "kFdZip";
4141
case kFdEpoll:
4242
return "kFdEpoll";
43+
case kFdDevRandom:
44+
return "kFdRandom";
4345
default:
4446
return "kFdWut";
4547
}

libc/calls/pwrite.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ ssize_t pwrite(int fd, const void *buf, size_t size, int64_t offset) {
6565
rc = sys_pwrite(fd, buf, size, offset, offset);
6666
} else if (__isfdkind(fd, kFdSocket)) {
6767
rc = espipe();
68-
} else if (__isfdkind(fd, kFdFile) || __isfdkind(fd, kFdDevNull)) {
68+
} else if (__isfdkind(fd, kFdFile) || __isfdkind(fd, kFdDevNull) ||
69+
__isfdkind(fd, kFdDevRandom)) {
6970
rc = sys_write_nt(fd, (struct iovec[]){{(void *)buf, size}}, 1, offset);
7071
} else {
7172
return ebadf();

0 commit comments

Comments
 (0)