const std = @import("std"); const logger = std.log.scoped(.syscall); pub const Error = error{ SyscallError, UnexpectedType, }; pub fn syscall(comptime T: type, name: []const u8, result: anytype) Error!T { const errno = switch (@TypeOf(result)) { usize => std.posix.system.errno(result), c_int => std.c.errno(result), else => return error.UnexpectedType, }; if (errno != .SUCCESS) { logger.err("syscall {s} failed with errno {t}", .{ name, errno }); return error.SyscallError; } return @intCast(result); } pub fn syscallvoid(name: []const u8, result: anytype) Error!void { const errno = switch (@TypeOf(result)) { usize => std.posix.system.errno(result), c_int => std.c.errno(result), else => return error.UnexpectedType, }; if (errno != .SUCCESS) { logger.err("syscall {s} failed with errno {t}", .{ name, errno }); return error.SyscallError; } } pub fn syscalldefault(comptime T: type, name: []const u8, result: anytype, default: T) T { return syscall(T, name, result) catch { logger.warn("syscall {s} failed", .{name}); return default; }; } test syscall { _ = try syscall(std.c.fd_t, "open", std.c.open("/dev/null", .{})); _ = try syscall(std.posix.system.fd_t, "open", std.posix.system.open("/dev/null", .{})); }