Re: [LTP] [PATCH v3 1/2] lib: Add .ulimit

Cyril Hrubis
2024-01-19T16:08:43Z

Hi!
> +1.43 Set resource limits
> +~~~~~~~~~~~~~~~~~~~~~~~~
> +
> +'.ulimit' allows to set resource limits on particular resource. NOTE: It sets 'rlim_cur'
^
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
> ------------------
>
> diff --git a/include/tst_test.h b/include/tst_test.h
> index 0c3171e5b..374a8615c 100644
> --- a/include/tst_test.h
> +++ b/include/tst_test.h
> @@ -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"
> @@ -149,6 +150,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;
> @@ -307,6 +313,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.
> @@ -393,6 +404,11 @@ int tst_validate_children_(const char *file, const int lineno,
> #define tst_validate_children(child_count) \
> tst_validate_children_(__FILE__, __LINE__, (child_count))
>
> +/*
> + * Set system resource limits
> + */
> +void tst_set_ulimit_(const char *file, const int lineno, const struct tst_ulimit_val *conf);
> +
> #ifndef TST_NO_DEFAULT_MAIN
>
> static struct tst_test test;
> diff --git a/lib/tst_test.c b/lib/tst_test.c
> index bcf2c4555..f5037330a 100644
> --- a/lib/tst_test.c
> +++ b/lib/tst_test.c
> @@ -1147,6 +1147,29 @@ static void do_cgroup_requires(void)
> tst_cg_init();
> }
>
> +#define tst_set_ulimit(conf) \
> + tst_set_ulimit_(__FILE__, __LINE__, (conf))

If we want this to be part of the API, i.e. allow tests to call this
function directly this should be in the tst_test.h header.

If not, the function should be static and only used in the tst_test.c

> +/*
> + * Set resource limits.
> + */
> +void tst_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");
> @@ -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);

The rest looks good.

--
Cyril Hrubis
chrubis@suse.cz