authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2020-11-01 12:19:35+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-01 15:47:46-05:00
log6418284680920a19fd34ec1a10d79113a5bec75d
tree9c12e2ada9ee871191b9a818c819ad34bb8a28d2
parent36f1788762ef5584bd0ebf5cef85a99318230ac4

std: add {set,get}rlimit bits and improve test


7 files changed, 167 insertions(+), 6 deletions(-)

lib/std/os.zig-2
......@@ -5663,7 +5663,6 @@ pub fn prctl(option: i32, args: anytype) PrctlError!u31 {
56635663pub const GetrlimitError = UnexpectedError;
56645664
56655665pub fn getrlimit(resource: rlimit_resource) GetrlimitError!rlimit {
5666 // TODO implement for systems other than linux and enable test
56675666 var limits: rlimit = undefined;
56685667 const rc = system.getrlimit(resource, &limits);
56695668 switch (errno(rc)) {
......@@ -5677,7 +5676,6 @@ pub fn getrlimit(resource: rlimit_resource) GetrlimitError!rlimit {
56775676pub const SetrlimitError = error{PermissionDenied} || UnexpectedError;
56785677
56795678pub fn setrlimit(resource: rlimit_resource, limits: rlimit) SetrlimitError!void {
5680 // TODO implement for systems other than linux and enable test
56815679 const rc = system.setrlimit(resource, &limits);
56825680 switch (errno(rc)) {
56835681 0 => return,
lib/std/os/bits/darwin.zig+30
......@@ -1496,3 +1496,33 @@ pub const rusage = extern struct {
14961496 nvcsw: isize,
14971497 nivcsw: isize,
14981498};
1499
1500pub const rlimit_resource = extern enum(c_int) {
1501 CPU = 0,
1502 FSIZE = 1,
1503 DATA = 2,
1504 STACK = 3,
1505 CORE = 4,
1506 AS = 5,
1507 RSS = 5,
1508 MEMLOCK = 6,
1509 NPROC = 7,
1510 NOFILE = 8,
1511
1512 _,
1513};
1514
1515pub const rlim_t = u64;
1516
1517/// No limit
1518pub const RLIM_INFINITY: rlim_t = (1 << 63) - 1;
1519
1520pub const RLIM_SAVED_MAX = RLIM_INFINITY;
1521pub const RLIM_SAVED_CUR = RLIM_INFINITY;
1522
1523pub const rlimit = extern struct {
1524 /// Soft limit
1525 cur: rlim_t,
1526 /// Hard limit
1527 max: rlim_t,
1528};
lib/std/os/bits/dragonfly.zig+33
......@@ -721,3 +721,36 @@ pub const Flock = extern struct {
721721 l_type: c_short,
722722 l_whence: c_short,
723723};
724
725pub const rlimit_resource = extern enum(c_int) {
726 CPU = 0,
727 FSIZE = 1,
728 DATA = 2,
729 STACK = 3,
730 CORE = 4,
731 RSS = 5,
732 MEMLOCK = 6,
733 NPROC = 7,
734 NOFILE = 8,
735 SBSIZE = 9,
736 AS = 10,
737 VMEM = 10,
738 POSIXLOCKS = 11,
739
740 _,
741};
742
743pub const rlim_t = i64;
744
745/// No limit
746pub const RLIM_INFINITY: rlim_t = (1 << 63) - 1;
747
748pub const RLIM_SAVED_MAX = RLIM_INFINITY;
749pub const RLIM_SAVED_CUR = RLIM_INFINITY;
750
751pub const rlimit = extern struct {
752 /// Soft limit
753 cur: rlim_t,
754 /// Hard limit
755 max: rlim_t,
756};
lib/std/os/bits/freebsd.zig+36
......@@ -1358,3 +1358,39 @@ pub const IPPROTO_RESERVED_253 = 253;
13581358
13591359/// Reserved
13601360pub const IPPROTO_RESERVED_254 = 254;
1361
1362pub const rlimit_resource = extern enum(c_int) {
1363 CPU = 0,
1364 FSIZE = 1,
1365 DATA = 2,
1366 STACK = 3,
1367 CORE = 4,
1368 RSS = 5,
1369 MEMLOCK = 6,
1370 NPROC = 7,
1371 NOFILE = 8,
1372 SBSIZE = 9,
1373 VMEM = 10,
1374 AS = 10,
1375 NPTS = 11,
1376 SWAP = 12,
1377 KQUEUES = 13,
1378 UMTXP = 14,
1379
1380 _,
1381};
1382
1383pub const rlim_t = i64;
1384
1385/// No limit
1386pub const RLIM_INFINITY: rlim_t = (1 << 63) - 1;
1387
1388pub const RLIM_SAVED_MAX = RLIM_INFINITY;
1389pub const RLIM_SAVED_CUR = RLIM_INFINITY;
1390
1391pub const rlimit = extern struct {
1392 /// Soft limit
1393 cur: rlim_t,
1394 /// Hard limit
1395 max: rlim_t,
1396};
lib/std/os/bits/netbsd.zig+33
......@@ -1169,3 +1169,36 @@ pub const IPPROTO_PFSYNC = 240;
11691169
11701170/// raw IP packet
11711171pub const IPPROTO_RAW = 255;
1172
1173pub const rlimit_resource = extern enum(c_int) {
1174 CPU = 0,
1175 FSIZE = 1,
1176 DATA = 2,
1177 STACK = 3,
1178 CORE = 4,
1179 RSS = 5,
1180 MEMLOCK = 6,
1181 NPROC = 7,
1182 NOFILE = 8,
1183 SBSIZE = 9,
1184 AS = 10,
1185 VMEM = 10,
1186 NTHR = 11,
1187
1188 _,
1189};
1190
1191pub const rlim_t = u64;
1192
1193/// No limit
1194pub const RLIM_INFINITY: rlim_t = (1 << 63) - 1;
1195
1196pub const RLIM_SAVED_MAX = RLIM_INFINITY;
1197pub const RLIM_SAVED_CUR = RLIM_INFINITY;
1198
1199pub const rlimit = extern struct {
1200 /// Soft limit
1201 cur: rlim_t,
1202 /// Hard limit
1203 max: rlim_t,
1204};
lib/std/os/bits/openbsd.zig+29
......@@ -1050,3 +1050,32 @@ pub const IPPROTO_PFSYNC = 240;
10501050
10511051/// raw IP packet
10521052pub const IPPROTO_RAW = 255;
1053
1054pub const rlimit_resource = extern enum(c_int) {
1055 CPU,
1056 FSIZE,
1057 DATA,
1058 STACK,
1059 CORE,
1060 RSS,
1061 MEMLOCK,
1062 NPROC,
1063 NOFILE,
1064
1065 _,
1066};
1067
1068pub const rlim_t = u64;
1069
1070/// No limit
1071pub const RLIM_INFINITY: rlim_t = (1 << 63) - 1;
1072
1073pub const RLIM_SAVED_MAX = RLIM_INFINITY;
1074pub const RLIM_SAVED_CUR = RLIM_INFINITY;
1075
1076pub const rlimit = extern struct {
1077 /// Soft limit
1078 cur: rlim_t,
1079 /// Hard limit
1080 max: rlim_t,
1081};
lib/std/os/test.zig+6-4
......@@ -593,11 +593,13 @@ test "fsync" {
593593}
594594
595595test "getrlimit and setrlimit" {
596 // TODO enable for other systems when implemented
597 if (builtin.os.tag != .linux) {
596 if (!@hasDecl(os, "rlimit")) {
598597 return error.SkipZigTest;
599598 }
600599
601 const cpuLimit = try os.getrlimit(.CPU);
602 try os.setrlimit(.CPU, cpuLimit);
600 inline for (std.meta.fields(os.rlimit_resource)) |field| {
601 const resource = @intToEnum(os.rlimit_resource, field.value);
602 const limit = try os.getrlimit(resource);
603 try os.setrlimit(resource, limit);
604 }
603605}