Skip to content

Commit

Permalink
lib: Add .ulimit
Browse files Browse the repository at this point in the history
Fixs: linux-test-project#530
Signed-off-by: Wei Gao <wegao@suse.com>
  • Loading branch information
Wei Gao via ltp authored and pevik committed Jan 23, 2024
1 parent 4e94000 commit 2e6289d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
18 changes: 18 additions & 0 deletions doc/C-Test-API.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -2426,6 +2426,24 @@ Test can be skipped on various conditions: on enabled SecureBoot
('.skip_in_secureboot = 1'), lockdown ('.skip_in_lockdown = 1') or in 32-bit
compat mode ('.skip_in_compat = 1').

1.43 Set resource limits
~~~~~~~~~~~~~~~~~~~~~~~~

'.ulimit' allows to set resource limits on particular resource. NOTE: It sets 'rlim_max'
only if it's higher than 'rlim_cur'.

[source,c]
-------------------------------------------------------------------------------
#include "tst_test.h"
static struct tst_test test = {
...
.ulimit = (const struct tst_ulimit_val[]) {
{RLIMIT_STACK, RLIM_INFINITY},
{}
},
};
2. Common problems
------------------

Expand Down
11 changes: 11 additions & 0 deletions include/tst_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <sys/resource.h>

#include "tst_common.h"
#include "tst_res_flags.h"
Expand Down Expand Up @@ -150,6 +151,11 @@ extern unsigned int tst_variant;

#define TST_UNLIMITED_RUNTIME (-1)

struct tst_ulimit_val {
int resource;
rlim_t rlim_cur;
};

struct tst_test {
/* number of tests available in test() function */
unsigned int tcnt;
Expand Down Expand Up @@ -308,6 +314,11 @@ struct tst_test {
*/
const struct tst_path_val *save_restore;

/*
* {} terminated array of ulimit resource type and value.
*/
const struct tst_ulimit_val *ulimit;

/*
* NULL terminated array of kernel config options required for the
* test.
Expand Down
32 changes: 32 additions & 0 deletions lib/tst_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,29 @@ static void do_cgroup_requires(void)
tst_cg_init();
}

#define tst_set_ulimit(conf) \
set_ulimit_(__FILE__, __LINE__, (conf))

/*
* Set resource limits.
*/
static void set_ulimit_(const char *file, const int lineno, const struct tst_ulimit_val *conf)
{
struct rlimit rlim;

safe_getrlimit(file, lineno, conf->resource, &rlim);

rlim.rlim_cur = conf->rlim_cur;

if (conf->rlim_cur > rlim.rlim_max)
rlim.rlim_max = conf->rlim_cur;

tst_res_(file, lineno, TINFO, "Set ulimit resource: %d rlim_cur: %lu rlim_max: %lu",
conf->resource, rlim.rlim_cur, rlim.rlim_max);

safe_setrlimit(file, lineno, conf->resource, &rlim);
}

static void do_setup(int argc, char *argv[])
{
char *tdebug_env = getenv("LTP_ENABLE_DEBUG");
Expand Down Expand Up @@ -1252,6 +1275,15 @@ static void do_setup(int argc, char *argv[])
}
}

if (tst_test->ulimit) {
const struct tst_ulimit_val *pvl = tst_test->ulimit;

while (pvl->resource) {
tst_set_ulimit(pvl);
pvl++;
}
}

if (tst_test->mntpoint)
SAFE_MKDIR(tst_test->mntpoint, 0777);

Expand Down

0 comments on commit 2e6289d

Please sign in to comment.