summaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig41
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" });
}