Commit 6418284680

xackus <14938807+xackus@users.noreply.github.com>
2020-11-01 12:19:35
std: add {set,get}rlimit bits and improve test
1 parent 36f1788
lib/std/os/bits/darwin.zig
@@ -1496,3 +1496,33 @@ pub const rusage = extern struct {
     nvcsw: isize,
     nivcsw: isize,
 };
+
+pub const rlimit_resource = extern enum(c_int) {
+    CPU = 0,
+    FSIZE = 1,
+    DATA = 2,
+    STACK = 3,
+    CORE = 4,
+    AS = 5,
+    RSS = 5,
+    MEMLOCK = 6,
+    NPROC = 7,
+    NOFILE = 8,
+
+    _,
+};
+
+pub const rlim_t = u64;
+
+/// No limit
+pub const RLIM_INFINITY: rlim_t = (1 << 63) - 1;
+
+pub const RLIM_SAVED_MAX = RLIM_INFINITY;
+pub const RLIM_SAVED_CUR = RLIM_INFINITY;
+
+pub const rlimit = extern struct {
+    /// Soft limit
+    cur: rlim_t,
+    /// Hard limit
+    max: rlim_t,
+};
lib/std/os/bits/dragonfly.zig
@@ -721,3 +721,36 @@ pub const Flock = extern struct {
     l_type: c_short,
     l_whence: c_short,
 };
+
+pub const rlimit_resource = extern enum(c_int) {
+    CPU = 0,
+    FSIZE = 1,
+    DATA = 2,
+    STACK = 3,
+    CORE = 4,
+    RSS = 5,
+    MEMLOCK = 6,
+    NPROC = 7,
+    NOFILE = 8,
+    SBSIZE = 9,
+    AS = 10,
+    VMEM = 10,
+    POSIXLOCKS = 11,
+
+    _,
+};
+
+pub const rlim_t = i64;
+
+/// No limit
+pub const RLIM_INFINITY: rlim_t = (1 << 63) - 1;
+
+pub const RLIM_SAVED_MAX = RLIM_INFINITY;
+pub const RLIM_SAVED_CUR = RLIM_INFINITY;
+
+pub const rlimit = extern struct {
+    /// Soft limit
+    cur: rlim_t,
+    /// Hard limit
+    max: rlim_t,
+};
lib/std/os/bits/freebsd.zig
@@ -1358,3 +1358,39 @@ pub const IPPROTO_RESERVED_253 = 253;
 
 /// Reserved
 pub const IPPROTO_RESERVED_254 = 254;
+
+pub const rlimit_resource = extern enum(c_int) {
+    CPU = 0,
+    FSIZE = 1,
+    DATA = 2,
+    STACK = 3,
+    CORE = 4,
+    RSS = 5,
+    MEMLOCK = 6,
+    NPROC = 7,
+    NOFILE = 8,
+    SBSIZE = 9,
+    VMEM = 10,
+    AS = 10,
+    NPTS = 11,
+    SWAP = 12,
+    KQUEUES = 13,
+    UMTXP = 14,
+
+    _,
+};
+
+pub const rlim_t = i64;
+
+/// No limit
+pub const RLIM_INFINITY: rlim_t = (1 << 63) - 1;
+
+pub const RLIM_SAVED_MAX = RLIM_INFINITY;
+pub const RLIM_SAVED_CUR = RLIM_INFINITY;
+
+pub const rlimit = extern struct {
+    /// Soft limit
+    cur: rlim_t,
+    /// Hard limit
+    max: rlim_t,
+};
lib/std/os/bits/netbsd.zig
@@ -1169,3 +1169,36 @@ pub const IPPROTO_PFSYNC = 240;
 
 /// raw IP packet
 pub const IPPROTO_RAW = 255;
+
+pub const rlimit_resource = extern enum(c_int) {
+    CPU = 0,
+    FSIZE = 1,
+    DATA = 2,
+    STACK = 3,
+    CORE = 4,
+    RSS = 5,
+    MEMLOCK = 6,
+    NPROC = 7,
+    NOFILE = 8,
+    SBSIZE = 9,
+    AS = 10,
+    VMEM = 10,
+    NTHR = 11,
+
+    _,
+};
+
+pub const rlim_t = u64;
+
+/// No limit
+pub const RLIM_INFINITY: rlim_t = (1 << 63) - 1;
+
+pub const RLIM_SAVED_MAX = RLIM_INFINITY;
+pub const RLIM_SAVED_CUR = RLIM_INFINITY;
+
+pub const rlimit = extern struct {
+    /// Soft limit
+    cur: rlim_t,
+    /// Hard limit
+    max: rlim_t,
+};
lib/std/os/bits/openbsd.zig
@@ -1050,3 +1050,32 @@ pub const IPPROTO_PFSYNC = 240;
 
 /// raw IP packet
 pub const IPPROTO_RAW = 255;
+
+pub const rlimit_resource = extern enum(c_int) {
+    CPU,
+    FSIZE,
+    DATA,
+    STACK,
+    CORE,
+    RSS,
+    MEMLOCK,
+    NPROC,
+    NOFILE,
+
+    _,
+};
+
+pub const rlim_t = u64;
+
+/// No limit
+pub const RLIM_INFINITY: rlim_t = (1 << 63) - 1;
+
+pub const RLIM_SAVED_MAX = RLIM_INFINITY;
+pub const RLIM_SAVED_CUR = RLIM_INFINITY;
+
+pub const rlimit = extern struct {
+    /// Soft limit
+    cur: rlim_t,
+    /// Hard limit
+    max: rlim_t,
+};
lib/std/os/test.zig
@@ -593,11 +593,13 @@ test "fsync" {
 }
 
 test "getrlimit and setrlimit" {
-    // TODO enable for other systems when implemented
-    if (builtin.os.tag != .linux) {
+    if (!@hasDecl(os, "rlimit")) {
         return error.SkipZigTest;
     }
 
-    const cpuLimit = try os.getrlimit(.CPU);
-    try os.setrlimit(.CPU, cpuLimit);
+    inline for (std.meta.fields(os.rlimit_resource)) |field| {
+        const resource = @intToEnum(os.rlimit_resource, field.value);
+        const limit = try os.getrlimit(resource);
+        try os.setrlimit(resource, limit);
+    }
 }
lib/std/os.zig
@@ -5663,7 +5663,6 @@ pub fn prctl(option: i32, args: anytype) PrctlError!u31 {
 pub const GetrlimitError = UnexpectedError;
 
 pub fn getrlimit(resource: rlimit_resource) GetrlimitError!rlimit {
-    // TODO implement for systems other than linux and enable test
     var limits: rlimit = undefined;
     const rc = system.getrlimit(resource, &limits);
     switch (errno(rc)) {
@@ -5677,7 +5676,6 @@ pub fn getrlimit(resource: rlimit_resource) GetrlimitError!rlimit {
 pub const SetrlimitError = error{PermissionDenied} || UnexpectedError;
 
 pub fn setrlimit(resource: rlimit_resource, limits: rlimit) SetrlimitError!void {
-    // TODO implement for systems other than linux and enable test
     const rc = system.setrlimit(resource, &limits);
     switch (errno(rc)) {
         0 => return,