Skip to content

Commit

Permalink
stdio tweaks (#9726)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner authored Mar 30, 2024
1 parent 5296c26 commit 8ff7ee0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 38 deletions.
34 changes: 34 additions & 0 deletions src/bun.js/bindings/c-bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#else
#include <uv.h>
#include <windows.h>
#include <corecrt_io.h>
#endif // !OS(WINDOWS)
#include <lshpack.h>

Expand Down Expand Up @@ -483,6 +484,39 @@ extern "C" void bun_initialize_process()
sigaction(SIGTERM, &sa, nullptr);
sigaction(SIGINT, &sa, nullptr);
}
#elif OS(WINDOWS)
for (int fd = 0; fd <= 2; ++fd) {
auto handle = reinterpret_cast<HANDLE>(uv_get_osfhandle(fd));
if (handle == INVALID_HANDLE_VALUE || GetFileType(handle) == FILE_TYPE_UNKNOWN) {
// Ignore _close result. If it fails or not depends on used Windows
// version. We will just check _open result.
_close(fd);
if (fd != _open("nul", O_RDWR)) {
RELEASE_ASSERT_NOT_REACHED();
} else {
switch (fd) {
case 0: {
SetStdHandle(STD_INPUT_HANDLE, uv_get_osfhandle(fd));
ASSERT(GetStdHandle(STD_INPUT_HANDLE) == uv_get_osfhandle(fd));
break;
}
case 1: {
SetStdHandle(STD_OUTPUT_HANDLE, uv_get_osfhandle(fd));
ASSERT(GetStdHandle(STD_OUTPUT_HANDLE) == uv_get_osfhandle(fd));
break;
}
case 2: {
SetStdHandle(STD_ERROR_HANDLE, uv_get_osfhandle(fd));
ASSERT(GetStdHandle(STD_ERROR_HANDLE) == uv_get_osfhandle(fd));
break;
}
default: {
ASSERT_NOT_REACHED();
}
}
}
}
}
#endif

atexit(Bun__onExit);
Expand Down
33 changes: 16 additions & 17 deletions src/io/source.zig
Original file line number Diff line number Diff line change
Expand Up @@ -149,43 +149,42 @@ pub const Source = union(enum) {
}

pub fn open(loop: *uv.Loop, fd: bun.FileDescriptor) bun.JSC.Maybe(Source) {
const rc = bun.windows.GetFileType(fd.cast());
log("open(fd: {}, type: {d})", .{ fd, rc });
const rc = bun.windows.libuv.uv_guess_handle(bun.uvfdcast(fd));
log("open(fd: {}, type: {d})", .{ fd, @tagName(rc) });

switch (rc) {
bun.windows.FILE_TYPE_PIPE => {
.named_pipe => {
switch (openPipe(loop, fd)) {
.result => |pipe| return .{ .result = .{ .pipe = pipe } },
.err => |err| return .{ .err = err },
}
},
bun.windows.FILE_TYPE_CHAR => {
.tty => {
switch (openTty(loop, fd)) {
.result => |tty| return .{ .result = .{ .tty = tty } },
.err => |err| return .{ .err = err },
}
},
bun.windows.FILE_TYPE_UNKNOWN => {
.file => {
return .{
.result = .{
.file = openFile(fd),
},
};
},
else => {
const errno = bun.windows.getLastErrno();

if (errno == .SUCCESS) {
// If it's nul, let's pretend its a pipe
// that seems to be the mode that libuv is happiest with.
return switch (openPipe(loop, fd)) {
.result => |pipe| return .{ .result = .{ .pipe = pipe } },
.err => |err| return .{ .err = err },
return .{
.result = .{
.file = openFile(fd),
},
};
}

return .{ .err = bun.sys.Error.fromCode(errno, .open) };
},
else => {
return .{
.result = .{
.file = openFile(fd),
},
};
},
}
}
};
24 changes: 3 additions & 21 deletions src/output.zig
Original file line number Diff line number Diff line change
Expand Up @@ -171,27 +171,9 @@ pub const Source = struct {
pub fn init() void {
bun.windows.libuv.uv_disable_stdio_inheritance();

const peb = std.os.windows.peb();
var stdout = peb.ProcessParameters.hStdOutput;
var stderr = peb.ProcessParameters.hStdError;
var stdin = peb.ProcessParameters.hStdInput;

const handle_identifiers = &.{ std.os.windows.STD_INPUT_HANDLE, std.os.windows.STD_OUTPUT_HANDLE, std.os.windows.STD_ERROR_HANDLE };
const handles = &.{ &stdin, &stdout, &stderr };
inline for (0..3) |fd_i| {
if (handles[fd_i].* == std.os.windows.INVALID_HANDLE_VALUE) {
handles[fd_i].* = bun.windows.CreateFileW(
comptime bun.strings.w("NUL" ++ .{0}).ptr,
if (fd_i > 0) std.os.windows.GENERIC_WRITE else std.os.windows.GENERIC_READ,
0,
null,
std.os.windows.OPEN_EXISTING,
0,
null,
);
_ = SetStdHandle(handle_identifiers[fd_i], handles[fd_i].*);
}
}
const stdin = w.GetStdHandle(w.STD_INPUT_HANDLE) catch bun.windows.INVALID_HANDLE_VALUE;
const stdout = w.GetStdHandle(w.STD_OUTPUT_HANDLE) catch bun.windows.INVALID_HANDLE_VALUE;
const stderr = w.GetStdHandle(w.STD_ERROR_HANDLE) catch bun.windows.INVALID_HANDLE_VALUE;

bun.win32.STDERR_FD = if (stderr != std.os.windows.INVALID_HANDLE_VALUE) bun.toFD(stderr) else bun.invalid_fd;
bun.win32.STDOUT_FD = if (stdout != std.os.windows.INVALID_HANDLE_VALUE) bun.toFD(stdout) else bun.invalid_fd;
Expand Down

0 comments on commit 8ff7ee0

Please sign in to comment.