From: Andrea Cervesato via ltp <ltp@lists.linux.it>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v1] Refactor pidns32 test using new LTP API
Date: Thu, 11 Aug 2022 13:36:40 +0200 [thread overview]
Message-ID: <20220811113640.8499-1-andrea.cervesato@suse.com> (raw)
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
testcases/kernel/containers/pidns/pidns32.c | 118 ++++++++------------
1 file changed, 44 insertions(+), 74 deletions(-)
diff --git a/testcases/kernel/containers/pidns/pidns32.c b/testcases/kernel/containers/pidns/pidns32.c
index 316c5d195..cbc91df68 100644
--- a/testcases/kernel/containers/pidns/pidns32.c
+++ b/testcases/kernel/containers/pidns/pidns32.c
@@ -1,101 +1,71 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) Huawei Technologies Co., Ltd., 2015
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- * the GNU General Public License for more details.
+ * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
*/
-/*
- * Verify that:
- * The kernel imposes a limit of 32 nested levels of pid namespaces.
+/*\
+ * [Description]
+ *
+ * Clone a process with CLONE_NEWPID flag and check for the maxium amount of
+ * nested containers.
*/
#define _GNU_SOURCE
-#include <sys/wait.h>
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-#include "pidns_helper.h"
-#include "test.h"
-#define MAXNEST 32
-
-char *TCID = "pidns32";
-int TST_TOTAL = 1;
+#include <sys/mman.h>
+#include "tst_test.h"
+#include "lapi/namespaces_constants.h"
-static void setup(void)
-{
- tst_require_root();
- check_newpid();
- tst_tmpdir();
-}
+#define MAXNEST 32
-static void cleanup(void)
-{
- tst_rmdir();
-}
+static int *level;
-static int child_fn1(void *arg)
+static int child_func(LTP_ATTRIBUTE_UNUSED void *arg)
{
- pid_t cpid1;
- long level = (long)arg;
+ pid_t cpid;
int status;
- if (level == MAXNEST)
+ if (*level == MAXNEST)
return 0;
- cpid1 = ltp_clone_quick(CLONE_NEWPID | SIGCHLD,
- (void *)child_fn1, (void *)(level + 1));
- if (cpid1 < 0) {
- printf("level %ld:unexpected error: (%d) %s\n",
- level, errno, strerror(errno));
- return 1;
- }
- if (waitpid(cpid1, &status, 0) == -1)
- return 1;
-
- if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
- printf("child exited abnormally\n");
- return 1;
- } else if (WIFSIGNALED(status)) {
- printf("child was killed with signal = %d", WTERMSIG(status));
- return 1;
- }
+
+ (*level)++;
+
+ cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, 0);
+ if (cpid < 0)
+ tst_brk(TBROK | TERRNO, "clone failed");
+
+ SAFE_WAITPID(cpid, &status, 0);
+
return 0;
}
-static void test_max_nest(void)
+static void setup(void)
{
- pid_t cpid1;
-
- cpid1 = ltp_clone_quick(CLONE_NEWPID | SIGCHLD,
- (void *)child_fn1, (void *)1);
- if (cpid1 < 0)
- tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
-
- tst_record_childstatus(cleanup, cpid1);
+ level = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ *level = 1;
}
-int main(int argc, char *argv[])
+static void run(void)
{
- int lc;
+ int ret, status;
- setup();
- tst_parse_opts(argc, argv, NULL, NULL);
+ ret = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, 0);
+ if (ret < 0)
+ tst_brk(TBROK | TERRNO, "clone failed");
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- tst_count = 0;
- test_max_nest();
+ SAFE_WAITPID(ret, &status, 0);
+
+ if (*level < MAXNEST) {
+ tst_res(TFAIL, "Nested containers should be %d, but they are %d", MAXNEST, *level);
+ return;
}
- cleanup();
- tst_exit();
+ tst_res(TPASS, "All %d containers have been nested", MAXNEST);
}
+
+static struct tst_test test = {
+ .test_all = run,
+ .needs_root = 1,
+ .setup = setup,
+};
--
2.35.3
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next reply other threads:[~2022-08-11 11:37 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-11 11:36 Andrea Cervesato via ltp [this message]
2022-08-15 14:49 ` [LTP] [PATCH v1] Refactor pidns32 test using new LTP API Cyril Hrubis
-- strict thread matches above, loose matches on Subject: below --
2022-08-10 9:48 Andrea Cervesato via ltp
2022-08-10 14:06 ` Cyril Hrubis
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220811113640.8499-1-andrea.cervesato@suse.com \
--to=ltp@lists.linux.it \
--cc=andrea.cervesato@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox