Something went wrong. Try again.
syscall wrapper; for when you're using std.c/std.os.linux
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344const 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", .{}));}