authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2020-09-11 14:10:55+02:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2020-09-11 22:32:23+02:00
log744b73ab46a75140537f9cf705939ecf0a2d229d
treefca57450c53bf64f4bce860eec2b0b6899203221
parent7b961a876b56d5926fe7ba6437a0459da9aa60bc
signaturelock-open Commit is signed but in an unrecognized format.

std: add prctl wrapper to std.os


1 files changed, 39 insertions(+), 0 deletions(-)

lib/std/os.zig+39
...@@ -5418,3 +5418,42 @@ pub fn fdatasync(fd: fd_t) SyncError!void {...@@ -5418,3 +5418,42 @@ pub fn fdatasync(fd: fd_t) SyncError!void {
5418 else => |err| return std.os.unexpectedErrno(err),5418 else => |err| return std.os.unexpectedErrno(err),
5419 }5419 }
5420}5420}
5421
5422pub const PrctlError = error{
5423 /// Can only occur with PR_SET_SECCOMP/SECCOMP_MODE_FILTER or
5424 /// PR_SET_MM/PR_SET_MM_EXE_FILE
5425 AccessDenied,
5426 /// Can only occur with PR_SET_MM/PR_SET_MM_EXE_FILE
5427 InvalidFileDescriptor,
5428 InvalidAddress,
5429 /// Can only occur with PR_SET_SPECULATION_CTRL, PR_MPX_ENABLE_MANAGEMENT,
5430 /// or PR_MPX_DISABLE_MANAGEMENT
5431 UnsupportedFeature,
5432 /// Can only occur wih PR_SET_FP_MODE
5433 OperationNotSupported,
5434 PermissionDenied,
5435} || UnexpectedError;
5436
5437pub fn prctl(option: i32, args: anytype) PrctlError!u31 {
5438 if (@typeInfo(@TypeOf(args)) != .Struct)
5439 @compileError("Expected tuple or struct argument, found " ++ @typeName(@TypeOf(args)));
5440 if (args.len > 4)
5441 @compileError("prctl takes a maximum of 4 optional arguments");
5442
5443 var buf: [4]usize = undefined;
5444 inline for (args) |arg, i| buf[i] = arg;
5445
5446 const rc = system.prctl(option, buf[0], buf[1], buf[2], buf[3]);
5447 switch (errno(rc)) {
5448 0 => return @intCast(u31, rc),
5449 EACCES => return error.AccessDenied,
5450 EBADF => return error.InvalidFileDescriptor,
5451 EFAULT => return error.InvalidAddress,
5452 EINVAL => unreachable,
5453 ENODEV, ENXIO => return error.UnsupportedFeature,
5454 EOPNOTSUPP => return error.OperationNotSupported,
5455 EPERM, EBUSY => return error.PermissionDenied,
5456 ERANGE => unreachable,
5457 else => |err| return std.os.unexpectedErrno(err),
5458 }
5459}