* [LTP] [PATCH v1 1/2] Refactor pidns03 test using new LTP API
@ 2022-08-04 13:30 Andrea Cervesato via ltp
2022-08-04 13:30 ` [LTP] [PATCH v1 2/2] Refactor pidns04 " Andrea Cervesato via ltp
2022-08-05 13:01 ` [LTP] [PATCH v1 1/2] Refactor pidns03 " Cyril Hrubis
0 siblings, 2 replies; 4+ messages in thread
From: Andrea Cervesato via ltp @ 2022-08-04 13:30 UTC (permalink / raw)
To: ltp
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
testcases/kernel/containers/pidns/pidns03.c | 139 ++++++--------------
1 file changed, 38 insertions(+), 101 deletions(-)
diff --git a/testcases/kernel/containers/pidns/pidns03.c b/testcases/kernel/containers/pidns/pidns03.c
index b735ab36b..b9b38b5d9 100644
--- a/testcases/kernel/containers/pidns/pidns03.c
+++ b/testcases/kernel/containers/pidns/pidns03.c
@@ -1,124 +1,61 @@
-/* Copyright (c) 2014 Red Hat, Inc. All rights reserved.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of version 2 the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * 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.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- ***********************************************************************
- * File: pidns03.c
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2014 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
*
- * Description:
- * Clones a new child process with CLONE_NEWPID flag - the new child
- * process mounts procfs to a "proc" directory and checks if it belongs
- * to a new pid namespace by:
- * 1. reading value of "proc/self", which is symlink
- * to directory named after current pid number
- * 2. comparing read value (PID) with "1"
+ * Clone a process with CLONE_NEWPID flag and check if procfs mounted folder
+ * belongs to the new pid namespace by looking at /proc/self .
*/
-#define _GNU_SOURCE
-#include <sys/wait.h>
#include <sys/mount.h>
-#include <sys/types.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include <errno.h>
-#include "pidns_helper.h"
-#include "test.h"
-#include "safe_macros.h"
+#include "tst_test.h"
+#include "lapi/namespaces_constants.h"
#define PROCDIR "proc"
-char *TCID = "pidns03";
-int TST_TOTAL = 1;
-
-
-static void cleanup(void)
-{
- tst_rmdir();
-}
-
-static void setup(void)
-{
- tst_require_root();
- check_newpid();
- tst_tmpdir();
- SAFE_MKDIR(cleanup, PROCDIR, 0555);
-}
-int child_func(void *arg)
+static int child_func(LTP_ATTRIBUTE_UNUSED void *arg)
{
- ssize_t r;
- char buf[10];
+ char proc_self[10];
- if (mount("none", PROCDIR, "proc", MS_RDONLY, NULL) == -1) {
- perror("mount");
- return 1;
- }
+ SAFE_MOUNT("none", PROCDIR, "proc", MS_RDONLY, NULL);
- /* self is symlink to directory named after current pid number */
- r = readlink(PROCDIR"/self", buf, sizeof(buf)-1);
- if (r == -1) {
- perror("readlink");
- umount(PROCDIR);
- return 1;
- }
+ SAFE_READLINK(PROCDIR"/self", proc_self, sizeof(proc_self) - 1);
- buf[r] = '\0';
+ SAFE_UMOUNT(PROCDIR);
- umount(PROCDIR);
-
- /* child should have PID 1 in a new pid namespace - if true
- * procfs belongs to the new pid namespace */
- if (strcmp(buf, "1")) {
- fprintf(stderr, "%s contains: %s\n", PROCDIR"/self", buf);
- return 1;
- }
+ TST_EXP_PASS(strcmp(proc_self, "1"), PROCDIR"/self contains 1:");
return 0;
}
-static void test(void)
+static void setup(void)
{
- int status;
-
- if (do_clone_tests(CLONE_NEWPID, child_func, NULL, NULL, NULL) == -1)
- tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
-
- SAFE_WAIT(cleanup, &status);
-
- if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
- tst_resm(TPASS, "mounting procfs in a new namespace");
- return;
- }
-
- if (WIFSIGNALED(status)) {
- tst_resm(TFAIL, "child was killed with signal %s",
- tst_strsig(WTERMSIG(status)));
- return;
- }
-
- tst_resm(TFAIL, "mounting procfs in a new namespace");
+ SAFE_MKDIR(PROCDIR, 0555);
}
-int main(int argc, char *argv[])
+static void cleanup(void)
{
- int lc;
-
- tst_parse_opts(argc, argv, NULL, NULL);
-
- setup();
+ if (tst_is_mounted_at_tmpdir(PROCDIR))
+ SAFE_UMOUNT(PROCDIR);
+}
- for (lc = 0; TEST_LOOPING(lc); lc++)
- test();
+static void run(void)
+{
+ int ret;
- cleanup();
- tst_exit();
+ ret = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, NULL);
+ if (ret < 0)
+ tst_brk(TBROK | TERRNO, "clone failed");
}
+
+static struct tst_test test = {
+ .test_all = run,
+ .setup = setup,
+ .cleanup = cleanup,
+ .needs_root = 1,
+ .needs_tmpdir = 1,
+};
--
2.35.3
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [LTP] [PATCH v1 2/2] Refactor pidns04 test using new LTP API
2022-08-04 13:30 [LTP] [PATCH v1 1/2] Refactor pidns03 test using new LTP API Andrea Cervesato via ltp
@ 2022-08-04 13:30 ` Andrea Cervesato via ltp
2022-08-05 13:05 ` Cyril Hrubis
2022-08-05 13:01 ` [LTP] [PATCH v1 1/2] Refactor pidns03 " Cyril Hrubis
1 sibling, 1 reply; 4+ messages in thread
From: Andrea Cervesato via ltp @ 2022-08-04 13:30 UTC (permalink / raw)
To: ltp
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
testcases/kernel/containers/pidns/pidns04.c | 171 +++++---------------
1 file changed, 36 insertions(+), 135 deletions(-)
diff --git a/testcases/kernel/containers/pidns/pidns04.c b/testcases/kernel/containers/pidns/pidns04.c
index 9ac0e5aca..02b3f12a2 100644
--- a/testcases/kernel/containers/pidns/pidns04.c
+++ b/testcases/kernel/containers/pidns/pidns04.c
@@ -1,150 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0
/*
-* Copyright (c) International Business Machines Corp., 2007
-* 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.
-* You should have received a copy of the GNU General Public License
-* along with this program; if not, write to the Free Software
-* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-*
-***************************************************************************
-
-* File: pidns04.c
-*
-* Description:
-* The pidns04.c testcase builds into the ltp framework to verify
-* the basic functionality of PID Namespace.
-*
-* Verify that:
-* 1. When parent clone a process with flag CLONE_NEWPID, the process ID of
-* child should be one.
-*
-* 2. When parent clone a process with flag CLONE_NEWPID, the parent process ID
-* of should be zero.
-*
-* 3. The container init process (one), should not get killed by the SIGKILL in
-* the childNS
-*
-* Total Tests:
-*
-* Test Name: pidns04
-*
-* Test Assertion & Strategy:
-*
-* From main() clone a new child process with passing the clone_flag as
-* CLONE_NEWPID.
-* The container init, should not get killed by the SIGKILL inside the child NS.
-* Usage: <for command-line>
-* pidns04
-*
-* History:
-*
-* FLAG DATE NAME DESCRIPTION
-* 08/10/08 Veerendra C <vechandr@in.ibm.com> Verifies killing of cont init.
-*
-*******************************************************************************/
-#define _GNU_SOURCE 1
-#include <sys/wait.h>
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-#define CLEANUP cleanup
-#include "pidns_helper.h"
-#include "test.h"
-
-#define INIT_PID 1
-#define CHILD_PID 1
-#define PARENT_PID 0
-
-char *TCID = "pidns04";
-int TST_TOTAL = 1;
-int fd[2];
-
-/*
- * child_fn1() - Inside container
-*/
-static int child_fn1(void *ttype)
+ * Copyright (c) International Business Machines Corp., 2007
+ * 08/10/08 Veerendra C <vechandr@in.ibm.com>
+ * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Clone a process with CLONE_NEWPID flag and verify that is not possible to
+ * kill init process by sending SIGKILL from child namespace.
+ */
+
+#include "tst_test.h"
+#include "lapi/namespaces_constants.h"
+
+static int child_func(LTP_ATTRIBUTE_UNUSED void *arg)
{
- int exit_val;
pid_t cpid, ppid;
+
cpid = getpid();
ppid = getppid();
- char mesg[] = "I was not killed !";
- /* Child process closes up read side of pipe */
- close(fd[0]);
- /* Comparing the values to make sure pidns is created correctly */
- if ((cpid == CHILD_PID) && (ppid == PARENT_PID)) {
- printf("PIDNS test is running inside container\n");
- kill(INIT_PID, SIGKILL);
- /* Verifying whether the container init is not killed, "
- If so writing into the pipe created in the parent NS" */
-
- /* Send "mesg" through the write side of pipe */
- write(fd[1], mesg, (strlen(mesg) + 1));
- exit_val = 0;
- } else {
- printf("got unexpected result of cpid=%d ppid=%d\n",
- cpid, ppid);
- exit_val = 1;
+ if (cpid != 1 || ppid != 0) {
+ tst_res(TFAIL, "got unexpected result of cpid=%d ppid=%d", cpid, ppid);
+ return 1;
}
- exit(exit_val);
-}
-static void setup(void)
-{
- tst_require_root();
- check_newpid();
-}
+ tst_res(TINFO, "Sending SIGKILL to container init process from child");
-int main(void)
-{
- int nbytes, status;
- char readbuffer[80];
+ SAFE_KILL(1, SIGKILL);
- setup();
-
- pipe(fd);
- TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, child_fn1, NULL));
- if (TEST_RETURN == -1) {
- tst_brkm(TFAIL | TTERRNO, CLEANUP, "clone failed");
- } else if (wait(&status) == -1) {
- tst_brkm(TFAIL | TERRNO, CLEANUP, "wait failed");
- }
-
- /* Parent process closes up write side of pipe */
- close(fd[1]);
- /* Read in a string from the pipe */
- nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
-
- if (0 <= nbytes) {
- tst_resm(TPASS, "Container init : %s", readbuffer);
- } else {
- tst_brkm(TFAIL, CLEANUP,
- "Container init is killed by SIGKILL !!!");
- }
-
- if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
- tst_resm(TFAIL, "Container init pid exited abnormally");
- } else if (WIFSIGNALED(status)) {
- tst_resm(TFAIL, "Container init pid got killed by signal %d",
- WTERMSIG(status));
- }
- CLEANUP();
-
- tst_exit();
+ tst_res(TPASS, "Child namespace is still alive");
+ return 0;
}
-static void cleanup(void)
+static void run(void)
{
- close(fd[0]);
+ int ret;
+
+ ret = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, NULL);
+ if (ret < 0)
+ tst_brk(TBROK | TERRNO, "clone failed");
}
+
+static struct tst_test test = {
+ .test_all = run,
+ .needs_root = 1,
+};
--
2.35.3
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [LTP] [PATCH v1 1/2] Refactor pidns03 test using new LTP API
2022-08-04 13:30 [LTP] [PATCH v1 1/2] Refactor pidns03 test using new LTP API Andrea Cervesato via ltp
2022-08-04 13:30 ` [LTP] [PATCH v1 2/2] Refactor pidns04 " Andrea Cervesato via ltp
@ 2022-08-05 13:01 ` Cyril Hrubis
1 sibling, 0 replies; 4+ messages in thread
From: Cyril Hrubis @ 2022-08-05 13:01 UTC (permalink / raw)
To: Andrea Cervesato; +Cc: ltp
Hi!
Pushed, thanks.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [LTP] [PATCH v1 2/2] Refactor pidns04 test using new LTP API
2022-08-04 13:30 ` [LTP] [PATCH v1 2/2] Refactor pidns04 " Andrea Cervesato via ltp
@ 2022-08-05 13:05 ` Cyril Hrubis
0 siblings, 0 replies; 4+ messages in thread
From: Cyril Hrubis @ 2022-08-05 13:05 UTC (permalink / raw)
To: Andrea Cervesato; +Cc: ltp
Hi!
> -static void cleanup(void)
> +static void run(void)
> {
> - close(fd[0]);
> + int ret;
> +
> + ret = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, NULL);
> + if (ret < 0)
> + tst_brk(TBROK | TERRNO, "clone failed");
I think that it may be cleaner to wait() the child here since if we
manage to kill the child the test will end up with:
tst_test.c:381: TBROK: Child (26434) killed by signal SIGKILL
so maybe add:
SAFE_WAITPID(ret, &status, 0);
if (WIFEXITTED(status) && WEXITSTATUS(status) == 0)
return;
if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL) {
tst_res(TFAIL, "Child killed by SIGKILL");
return;
}
tst_res(TBROK, "Child %s", tst_strstatus(status));
But maybe the clearer message is not worth the effort here.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-08-05 13:03 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-04 13:30 [LTP] [PATCH v1 1/2] Refactor pidns03 test using new LTP API Andrea Cervesato via ltp
2022-08-04 13:30 ` [LTP] [PATCH v1 2/2] Refactor pidns04 " Andrea Cervesato via ltp
2022-08-05 13:05 ` Cyril Hrubis
2022-08-05 13:01 ` [LTP] [PATCH v1 1/2] Refactor pidns03 " Cyril Hrubis
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.