std: add prctl wrapper to std.os

This commit is contained in:
Isaac Freund 2020-09-11 14:10:55 +02:00
parent 7b961a876b
commit 744b73ab46
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11

View File

@ -5418,3 +5418,42 @@ pub fn fdatasync(fd: fd_t) SyncError!void {
else => |err| return std.os.unexpectedErrno(err),
}
}
pub const PrctlError = error{
/// Can only occur with PR_SET_SECCOMP/SECCOMP_MODE_FILTER or
/// PR_SET_MM/PR_SET_MM_EXE_FILE
AccessDenied,
/// Can only occur with PR_SET_MM/PR_SET_MM_EXE_FILE
InvalidFileDescriptor,
InvalidAddress,
/// Can only occur with PR_SET_SPECULATION_CTRL, PR_MPX_ENABLE_MANAGEMENT,
/// or PR_MPX_DISABLE_MANAGEMENT
UnsupportedFeature,
/// Can only occur wih PR_SET_FP_MODE
OperationNotSupported,
PermissionDenied,
} || UnexpectedError;
pub fn prctl(option: i32, args: anytype) PrctlError!u31 {
if (@typeInfo(@TypeOf(args)) != .Struct)
@compileError("Expected tuple or struct argument, found " ++ @typeName(@TypeOf(args)));
if (args.len > 4)
@compileError("prctl takes a maximum of 4 optional arguments");
var buf: [4]usize = undefined;
inline for (args) |arg, i| buf[i] = arg;
const rc = system.prctl(option, buf[0], buf[1], buf[2], buf[3]);
switch (errno(rc)) {
0 => return @intCast(u31, rc),
EACCES => return error.AccessDenied,
EBADF => return error.InvalidFileDescriptor,
EFAULT => return error.InvalidAddress,
EINVAL => unreachable,
ENODEV, ENXIO => return error.UnsupportedFeature,
EOPNOTSUPP => return error.OperationNotSupported,
EPERM, EBUSY => return error.PermissionDenied,
ERANGE => unreachable,
else => |err| return std.os.unexpectedErrno(err),
}
}