diff mbox series

[v4,1/2] lib: Add .ulimit

Message ID 20240123021928.9721-2-wegao@suse.com
State Accepted
Headers show
Series Lib add .ulimit setting | expand

Commit Message

Wei Gao Jan. 23, 2024, 2:19 a.m. UTC
Fixs: #530
Signed-off-by: Wei Gao <wegao@suse.com>
---
 doc/C-Test-API.asciidoc | 18 ++++++++++++++++++
 include/tst_test.h      | 11 +++++++++++
 lib/tst_test.c          | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+)

v4->v3:
To fix following warning when make check-tst_test, i rename tst_set_ulimit_ -> set_ulimit_ and add static key word.
tst_test.c:1156:13: warning: LTP-003: Symbol 'tst_set_ulimit_' has the LTP public library prefix, but is static (private).

Comments

Petr Vorel Jan. 23, 2024, 8:36 a.m. UTC | #1
Hi Wei,

> Fixs: #530
^ typo, it should have been

Fixes: #530

Fortunately github formats properly even there is a typo:
https://github.com/pevik/ltp/commit/2e6289d26c325db796cde27765b6765217541d24

But it would not close the ticket.
Also people sometimes do 'git log --grep=Fixes:' to see changes.

...
> diff --git a/doc/C-Test-API.asciidoc b/doc/C-Test-API.asciidoc
> index db16be36e..e3891d5e8 100644
> --- a/doc/C-Test-API.asciidoc
> +++ b/doc/C-Test-API.asciidoc
> @@ -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},
> +		{}
> +	},
> +};
Here is missing separator, which is required to end formatting, otherwise page
would be horribly broken:
-------------------------------------------------------------------------------

Have look at my fork how it looks like and please, next time test formatting of
doc changes yourself in your fork (preview is enough to see the problems):
https://github.com/pevik/ltp/wiki/TEST#143-set-resource-limits

Both issues can be fixed before merge.

Reviewed-by: Petr Vorel <pvorel@suse.cz>

Unless Cyril or Li acks this to be merged before the release, it will be merged
after the release (not a fix).

Kind regards,
Petr

> +
>  2. Common problems
>  ------------------
...
Petr Vorel Jan. 30, 2024, 11:55 a.m. UTC | #2
Hi Wei,

thanks, patchset merged!

Kind regards,
Petr
diff mbox series

Patch

diff --git a/doc/C-Test-API.asciidoc b/doc/C-Test-API.asciidoc
index db16be36e..e3891d5e8 100644
--- a/doc/C-Test-API.asciidoc
+++ b/doc/C-Test-API.asciidoc
@@ -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
 ------------------
 
diff --git a/include/tst_test.h b/include/tst_test.h
index fda696eeb..47b5902f9 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"
@@ -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;
@@ -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.
diff --git a/lib/tst_test.c b/lib/tst_test.c
index bcf2c4555..f9a6f1ae0 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) \
+	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");
@@ -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);