skip to content
==> tests/limits.c <==
1/* native.rs's limit_bits_and_macos_refusals, plus the probe and the cgroup check on this machine. */
2#include "mesh/check.h"
3#include "mesh/native_limits.h"
4
5#include "mesh/abi/control_block.h"
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <unistd.h>
11
12int main(void) {
13    mesh_native_limits l = {200, 1u << 30, 5, true};
14    CHECK_U(mesh_native_limits_bits(&l), MESH_LIMIT_CPU | MESH_LIMIT_MEM | MESH_LIMIT_NICE | MESH_LIMIT_BG);
15    char *d = mesh_native_limits_debug(&l);
16    CHECK_S(d, "Limits { cpu_pct: 200, mem: 1073741824, nice: 5, background: true }");
17    free(d);
18    char *err = NULL;
19    mesh_native_limits nice_only = {0, 0, 3, false};
20    CHECK(mesh_native_limits_available(&nice_only, &err));
21    /* A scoped limit: works where systemd's user manager delegates, else refuses with why. */
22    mesh_native_limits mem = {0, 1u << 30, 0, false};
23    if (mesh_native_limits_available(&mem, &err)) printf("systemd-run --user --scope works here\n");
24    else {
25        CHECK(strncmp(err, "load: limits: ", 14) == 0);
26        printf("scoped limits unavailable here: %s\n", err);
27    }
28    /* This test process is not in a scope with those limits. */
29    char *e2 = NULL;
30    bool in_scope = mesh_native_check_cgroup(getpid(), &mem, &e2);
31    CHECK(!in_scope || e2 == NULL);
32    if (!in_scope) CHECK(strncmp(e2, "limits: ", 8) == 0);
33    free(e2);
34    free(err);
35    return mesh_check_done();
36}