Skip to content

Commit c134dd1

Browse files
committed
Implement (m|mun)lockall for Linux and *nix libcs
1 parent 8e7d9af commit c134dd1

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

lib/std/c.zig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,20 @@ pub const clock_gettime = switch (native_os) {
15161516
else => private.clock_gettime,
15171517
};
15181518

1519+
// All the Unix-likes except darwin support the mlockall call. However, the
1520+
// flags values are consistent everywhere except Linux. which has an extra flag
1521+
// MCL_ONFAULT and has non-standard values for the MCL constants on ppc, ppc64,
1522+
// and sparc.
1523+
pub extern "c" fn mlockall(flags: c_int) c_int;
1524+
pub extern "c" fn munlockall() c_int;
1525+
pub const MCL = switch (native_os) {
1526+
.linux => std.os.linux.MCL,
1527+
else => struct {
1528+
pub const CURRENT = 0x01;
1529+
pub const FUTURE = 0x02;
1530+
},
1531+
};
1532+
15191533
pub const fstat = switch (native_os) {
15201534
.macos => switch (native_arch) {
15211535
.x86_64 => private.@"fstat$INODE64",

lib/std/os.zig

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub const Kevent = system.Kevent;
100100
pub const LOCK = system.LOCK;
101101
pub const MADV = system.MADV;
102102
pub const MAP = system.MAP;
103+
pub const MCL = system.MCL;
103104
pub const MSF = system.MSF;
104105
pub const MAX_ADDR_LEN = system.MAX_ADDR_LEN;
105106
pub const MFD = system.MFD;
@@ -7505,6 +7506,37 @@ pub fn madvise(ptr: [*]align(mem.page_size) u8, length: usize, advice: u32) Madv
75057506
}
75067507
}
75077508

7509+
pub const MlockallError = error{
7510+
InvalidArgument,
7511+
LockedMemoryLimitExceeded,
7512+
PermissionDenied,
7513+
SystemResources,
7514+
} || UnexpectedError;
7515+
7516+
pub fn mlockall(flags: i32) MlockallError!void {
7517+
switch (errno(system.mlockall(flags))) {
7518+
.SUCCESS => return,
7519+
.INVAL => return error.InvalidArgument,
7520+
.NOMEM => return error.LockedMemoryLimitExceeded,
7521+
.PERM => return error.PermissionDenied,
7522+
// Solaris and (Free|Net|Open)BSD, presumably no room to fault in locked stuff:
7523+
.AGAIN => return error.SystemResources,
7524+
else => |err| return unexpectedErrno(err),
7525+
}
7526+
}
7527+
7528+
pub const MunlockallError = error{
7529+
PermissionDenied,
7530+
} || UnexpectedError;
7531+
7532+
pub fn munlockall() MunlockallError!void {
7533+
switch (errno(system.munlockall())) {
7534+
.SUCCESS => return,
7535+
.EPERM => return error.PermissionDenied, // Solaris, possibly BSDs as well
7536+
else => |err| return unexpectedErrno(err),
7537+
}
7538+
}
7539+
75087540
pub const PerfEventOpenError = error{
75097541
/// Returned if the perf_event_attr size value is too small (smaller
75107542
/// than PERF_ATTR_SIZE_VER0), too big (larger than the page size),

lib/std/os/linux.zig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,6 +2092,14 @@ pub fn madvise(address: [*]u8, len: usize, advice: u32) usize {
20922092
return syscall3(.madvise, @intFromPtr(address), len, advice);
20932093
}
20942094

2095+
pub fn mlockall(flags: i32) usize {
2096+
return syscall1(.mlockall, @as(usize, @bitCast(@as(isize, flags))));
2097+
}
2098+
2099+
pub fn munlockall() usize {
2100+
return syscall0(.munlockall);
2101+
}
2102+
20952103
pub fn pidfd_open(pid: pid_t, flags: u32) usize {
20962104
return syscall2(.pidfd_open, @as(usize, @bitCast(@as(isize, pid))), flags);
20972105
}
@@ -5454,6 +5462,16 @@ pub const MADV = struct {
54545462
pub const SOFT_OFFLINE = 101;
54555463
};
54565464

5465+
pub const MCL = if (is_ppc or is_ppc64 or is_sparc) struct {
5466+
pub const CURRENT = 0x2000;
5467+
pub const FUTURE = 0x4000;
5468+
pub const ONFAULT = 0x8000;
5469+
} else struct {
5470+
pub const CURRENT = 0x01;
5471+
pub const FUTURE = 0x02;
5472+
pub const ONFAULT = 0x04;
5473+
};
5474+
54575475
pub const POSIX_FADV = switch (native_arch) {
54585476
.s390x => if (@typeInfo(usize).Int.bits == 64) struct {
54595477
pub const NORMAL = 0;

lib/std/os/test.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,3 +1284,14 @@ test "fchmodat smoke test" {
12841284
try expectMode(tmp.dir.fd, "symlink", 0o600);
12851285
try expectMode(tmp.dir.fd, "regfile", 0o640);
12861286
}
1287+
1288+
test "mlockall basic check" {
1289+
// We can't test a "real" mlockall or munlockall because of various system
1290+
// perms/limits on locked memory and/or possible adverse effects on the
1291+
// testing environment itself. What we can do safely, I think, is execute
1292+
// mlockall() with a very-invalid (across all known platforms) flags value
1293+
// and expect EINVAL, so that we at least exercise the code path and
1294+
// confirm the system or libc call really exists and is callable.
1295+
if (!@hasDecl(os.system, "mlockall")) return error.SkipZigTest;
1296+
try expectError(error.InvalidArgument, os.mlockall(0b0001111111111000));
1297+
}

0 commit comments

Comments
 (0)