diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-30 04:37:26 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-30 04:37:26 -0300 |
| commit | ab0946311085f6fa337162302ea2f9f25b1877e2 (patch) | |
| tree | 3cd24953d49f7fbc633a0ce5b0edef68740c0f87 /src/main.zig | |
| parent | 5cea19dfded7027090234bbe41fdaac3d379e012 (diff) | |
| download | pardes-ab0946311085f6fa337162302ea2f9f25b1877e2.tar.gz pardes-ab0946311085f6fa337162302ea2f9f25b1877e2.zip | |
main: tcgetattr and tcsetattr take libc's place on Linux glibc, the kernel's own ioctls on the 36 bytes both layouts begin with: glibc's struct termios is 60 bytes and Zig 0.17's std.c.termios the kernel's 44, so libc's tcgetattr wrote 16 bytes past every Zig caller's (vaxis's makeRaw, the stub's, the GUI's) and a ReleaseSafe build trapped at its first terminal (Debug and ReleaseFast ran on by luck of the stack); a test with a canary past the struct fails without them
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/main.zig b/src/main.zig index b4f97128..ce1c5bba 100644 --- a/src/main.zig +++ b/src/main.zig @@ -315,6 +315,47 @@ const nested_text = // the std.process.Init entry. pub const main = if (is_emscripten) webMain else nativeMain; +// glibc's struct termios is 60 bytes, Zig 0.17's std.c.termios the kernel's +// 44: libc's tcgetattr wrote 16 bytes past every Zig caller's (vaxis's +// makeRaw, the stub's, the GUI's), and a ReleaseSafe build trapped at its +// first terminal. These take libc's place in the process: the kernel's own +// ioctls, which read and write only the 36 bytes both layouts begin with +// (the flags, the line, the kernel's 19 cc), so a caller of either size +// stays within its struct. +comptime { + if (builtin.os.tag == .linux and builtin.abi.isGnu()) { + @export(&kernelTcgetattr, .{ .name = "tcgetattr" }); + @export(&kernelTcsetattr, .{ .name = "tcsetattr" }); + } +} + +fn kernelTcgetattr(fd: c_int, t: *std.os.linux.termios) callconv(.c) c_int { + return libcResult(std.os.linux.tcgetattr(fd, t)); +} + +fn kernelTcsetattr(fd: c_int, how: c_int, t: *const std.os.linux.termios) callconv(.c) c_int { + return libcResult(std.os.linux.tcsetattr(fd, @fromBackingInt(@bitCast(how)), t)); +} + +fn libcResult(rc: usize) c_int { + const e = std.os.linux.errno(rc); + if (e == .SUCCESS) return 0; + std.c._errno().* = @intCast(@backingInt(e)); + return -1; +} + +test "tcgetattr writes nothing past a Zig termios" { + if (comptime !(builtin.os.tag == .linux and builtin.abi.isGnu())) return error.SkipZigTest; + const master = std.c.open("/dev/ptmx", .{ .ACCMODE = .RDWR, .NOCTTY = true, .CLOEXEC = true }, @as(std.c.mode_t, 0)); + if (master < 0) return error.SkipZigTest; + defer _ = std.c.close(master); + var s: extern struct { t: std.c.termios, canary: [32]u8 } = .{ .t = undefined, .canary = @splat(0xAA) }; + try std.testing.expectEqual(0, std.c.tcgetattr(master, &s.t)); + try std.testing.expectEqualSlices(u8, &@as([32]u8, @splat(0xAA)), &s.canary); + try std.testing.expectEqual(0, std.c.tcsetattr(master, .NOW, &s.t)); + try std.testing.expectEqual(-1, std.c.tcgetattr(-1, &s.t)); +} + comptime { if (is_emscripten) @export(&emscriptenMain, .{ .name = "main" }); } |
