* [LTP] [PATCH v1 1/9] Remove libclone dependency from sysvipc
2022-02-08 10:09 [LTP] [PATCH v1 0/9] Rewrite sysvipc testing suite using new LTP API Andrea Cervesato
@ 2022-02-08 10:09 ` Andrea Cervesato
2022-02-08 10:09 ` [LTP] [PATCH v1 2/9] Rewrite mesgq_nstest.c using new LTP API Andrea Cervesato
` (7 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Andrea Cervesato @ 2022-02-08 10:09 UTC (permalink / raw)
To: ltp
This is the step 1 before rewriting all sysvipc tests using new
LTP API. Substituted ipcns_helper.h with a common.h header for all
tests. The new common.h file includes some helpers functions, as
well as specific clone functionalities.
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
testcases/kernel/containers/sysvipc/Makefile | 26 +---
testcases/kernel/containers/sysvipc/common.h | 138 ++++++++++++++++++
.../kernel/containers/sysvipc/ipcns_helper.h | 41 ------
3 files changed, 141 insertions(+), 64 deletions(-)
create mode 100644 testcases/kernel/containers/sysvipc/common.h
delete mode 100644 testcases/kernel/containers/sysvipc/ipcns_helper.h
diff --git a/testcases/kernel/containers/sysvipc/Makefile b/testcases/kernel/containers/sysvipc/Makefile
index 00b537f6a..426fe5292 100644
--- a/testcases/kernel/containers/sysvipc/Makefile
+++ b/testcases/kernel/containers/sysvipc/Makefile
@@ -1,28 +1,8 @@
-################################################################################
-## ##
-## 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 ##
-## ##
-################################################################################
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) International Business Machines Corp., 2007
+# Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
top_srcdir ?= ../../../..
include $(top_srcdir)/include/mk/testcases.mk
-include $(abs_srcdir)/../Makefile.inc
-
-LDLIBS := -lclone $(LDLIBS)
-
include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/containers/sysvipc/common.h b/testcases/kernel/containers/sysvipc/common.h
new file mode 100644
index 000000000..019716781
--- /dev/null
+++ b/testcases/kernel/containers/sysvipc/common.h
@@ -0,0 +1,138 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) International Business Machines Corp., 2007
+ * Rishikesh K Rajak <risrajak@in.ibm.com>
+ * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+#ifndef COMMON_H
+#define COMMON_H
+
+#include <stdlib.h>
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+#include "lapi/namespaces_constants.h"
+
+enum {
+ T_CLONE,
+ T_UNSHARE,
+ T_NONE,
+};
+
+static int dummy_child(void *v)
+{
+ (void)v;
+ return 0;
+}
+
+static void check_newipc(void)
+{
+ int pid, status;
+
+ if (tst_kvercmp(2, 6, 19) < 0)
+ tst_brk(TCONF, "CLONE_NEWIPC not supported");
+
+ pid = ltp_clone_quick(CLONE_NEWIPC | SIGCHLD, dummy_child, NULL);
+ if (pid < 0)
+ tst_brk(TCONF | TERRNO, "CLONE_NEWIPC not supported");
+
+ SAFE_WAITPID(pid, &status, 0);
+}
+
+static int clone_test(unsigned long clone_flags, int (*fn1)(void *arg),
+ void *arg1)
+{
+ int ret;
+
+ ret = ltp_clone_quick(clone_flags | SIGCHLD, fn1, arg1);
+
+ if (ret != -1) {
+ /* no errors: we'll get the PID id that means success */
+ ret = 0;
+ }
+
+ return ret;
+}
+
+static int unshare_test(unsigned long clone_flags, int (*fn1)(void *arg),
+ void *arg1)
+{
+ int pid, ret = 0;
+ int retpipe[2];
+ char buf[2];
+
+ SAFE_PIPE(retpipe);
+
+ pid = fork();
+ if (pid < 0) {
+ SAFE_CLOSE(retpipe[0]);
+ SAFE_CLOSE(retpipe[1]);
+ tst_brk(TBROK, "fork");
+ }
+
+ if (!pid) {
+ SAFE_CLOSE(retpipe[0]);
+
+ ret = tst_syscall(SYS_unshare, clone_flags);
+ if (ret == -1) {
+ SAFE_WRITE(1, retpipe[1], "0", 2);
+ SAFE_CLOSE(retpipe[1]);
+ exit(1);
+ } else {
+ SAFE_WRITE(1, retpipe[1], "1", 2);
+ }
+
+ SAFE_CLOSE(retpipe[1]);
+
+ ret = fn1(arg1);
+ exit(ret);
+ }
+
+ SAFE_CLOSE(retpipe[1]);
+ SAFE_READ(1, retpipe[0], &buf, 2);
+ SAFE_CLOSE(retpipe[0]);
+
+ if (*buf == '0')
+ return -1;
+
+ return ret;
+}
+
+static int plain_test(int (*fn1)(void *arg), void *arg1)
+{
+ int ret = 0, pid;
+
+ pid = SAFE_FORK();
+ if (!pid)
+ exit(fn1(arg1));
+
+ return ret;
+}
+
+static void clone_unshare_test(int use_clone, unsigned long clone_flags,
+ int (*fn1)(void *arg), void *arg1)
+{
+ int ret = 0;
+
+ switch (use_clone) {
+ case T_NONE:
+ ret = plain_test(fn1, arg1);
+ break;
+ case T_CLONE:
+ ret = clone_test(clone_flags, fn1, arg1);
+ break;
+ case T_UNSHARE:
+ ret = unshare_test(clone_flags, fn1, arg1);
+ break;
+ default:
+ ret = -1;
+ tst_brk(TBROK, "%s: bad use_clone option: %d", __FUNCTION__,
+ use_clone);
+ break;
+ }
+
+ if (ret == -1)
+ tst_brk(TBROK, "child2 clone failed");
+}
+
+#endif
diff --git a/testcases/kernel/containers/sysvipc/ipcns_helper.h b/testcases/kernel/containers/sysvipc/ipcns_helper.h
deleted file mode 100644
index 01ad0fff6..000000000
--- a/testcases/kernel/containers/sysvipc/ipcns_helper.h
+++ /dev/null
@@ -1,41 +0,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
-*
-* Author: Rishikesh K Rajak <risrajak@in.ibm.com>
-***************************************************************************/
-#include <sched.h>
-#include "../libclone/libclone.h"
-#include "test.h"
-#include "safe_macros.h"
-
-static int dummy_child(void *v)
-{
- (void) v;
- return 0;
-}
-
-static void check_newipc(void)
-{
- int pid, status;
-
- if (tst_kvercmp(2, 6, 19) < 0)
- tst_brkm(TCONF, NULL, "CLONE_NEWIPC not supported");
-
- pid = do_clone_unshare_test(T_CLONE, CLONE_NEWIPC, dummy_child, NULL);
- if (pid == -1)
- tst_brkm(TCONF | TERRNO, NULL, "CLONE_NEWIPC not supported");
-
- SAFE_WAIT(NULL, &status);
-}
--
2.35.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 13+ messages in thread* [LTP] [PATCH v1 2/9] Rewrite mesgq_nstest.c using new LTP API
2022-02-08 10:09 [LTP] [PATCH v1 0/9] Rewrite sysvipc testing suite using new LTP API Andrea Cervesato
2022-02-08 10:09 ` [LTP] [PATCH v1 1/9] Remove libclone dependency from sysvipc Andrea Cervesato
@ 2022-02-08 10:09 ` Andrea Cervesato
2022-03-08 14:42 ` Cyril Hrubis
2022-02-08 10:09 ` [LTP] [PATCH v1 3/9] Rewrite msg_comm.c " Andrea Cervesato
` (6 subsequent siblings)
8 siblings, 1 reply; 13+ messages in thread
From: Andrea Cervesato @ 2022-02-08 10:09 UTC (permalink / raw)
To: ltp
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
.../kernel/containers/sysvipc/mesgq_nstest.c | 234 ++++++++----------
1 file changed, 108 insertions(+), 126 deletions(-)
diff --git a/testcases/kernel/containers/sysvipc/mesgq_nstest.c b/testcases/kernel/containers/sysvipc/mesgq_nstest.c
index dbf311dc8..d0fc5ff34 100644
--- a/testcases/kernel/containers/sysvipc/mesgq_nstest.c
+++ b/testcases/kernel/containers/sysvipc/mesgq_nstest.c
@@ -1,175 +1,157 @@
-/* *************************************************************************
-* Copyright (c) International Business Machines Corp., 2009
-* 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
-*
-* Author: Veerendra C <vechandr@in.ibm.com>
-*
-* In Parent Process , create mesgq with key 154326L
-* Now create container by passing 1 of the flag values..
-* Flag = clone, clone(CLONE_NEWIPC), or unshare(CLONE_NEWIPC)
-* In cloned process, try to access the created mesgq
-* Test PASS: If the mesgq is readable when flag is None.
-* Test FAIL: If the mesgq is readable when flag is Unshare or Clone.
-***************************************************************************/
-
-#define _GNU_SOURCE 1
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) International Business Machines Corp., 2009
+ * Veerendra C <vechandr@in.ibm.com>
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * In Parent Process , create mesgq with key 154326L
+ * Now create container by passing 1 of the flag values..
+ * Flag = clone, clone(CLONE_NEWIPC), or unshare(CLONE_NEWIPC)
+ * In cloned process, try to access the created mesgq
+ * Test PASS: If the mesgq is readable when flag is None.
+ * Test FAIL: If the mesgq is readable when flag is Unshare or Clone.
+ */
+
+#define _GNU_SOURCE
+
#include <sys/ipc.h>
+#include <sys/wait.h>
#include <sys/msg.h>
-#include <libclone.h>
-#include "test.h"
-#include "ipcns_helper.h"
-
-#define KEY_VAL 154326L
-#define UNSHARESTR "unshare"
-#define CLONESTR "clone"
-#define NONESTR "none"
-
-char *TCID = "mesgq_nstest";
-int TST_TOTAL = 1;
-int p1[2];
-int p2[2];
-struct msg_buf {
- long int mtype; /* type of received/sent message */
- char mtext[80]; /* text of the message */
+#include <sys/types.h>
+#include "tst_test.h"
+#include "common.h"
+
+#define KEY_VAL 154326L
+
+static char *str_op = "clone";
+
+static int p1[2];
+static int p2[2];
+
+static struct msg_buf {
+ int mtype; /* type of received/sent message */
+ char mtext[80]; /* text of the message */
} msg;
-void mesgq_read(int id)
+static void mesgq_read(int id)
{
- int READMAX = 80;
int n;
- /* read msg type 5 on the Q; msgtype, flags are last 2 params.. */
- n = msgrcv(id, &msg, READMAX, 5, 0);
- if (n == -1)
- perror("msgrcv"), tst_exit();
+ /* read msg type 5 on the Q; msgtype, flags are last 2 params.. */
+ n = msgrcv(id, &msg, sizeof(struct msg_buf) - sizeof(long), 5, 0);
+ if (n < 0)
+ tst_brk(TBROK, "msgrcv: %s", tst_strerrno(-n));
- tst_resm(TINFO, "Mesg read of %d bytes; Type %ld: Msg: %.*s",
- n, msg.mtype, n, msg.mtext);
+ tst_res(TINFO, "Mesg read of %d bytes; Type %ld: Msg: %.*s", n,
+ msg.mtype, n, msg.mtext);
}
-int check_mesgq(void *vtest)
+static int check_mesgq(LTP_ATTRIBUTE_UNUSED void *vtest)
{
char buf[3];
int id;
- (void) vtest;
+ SAFE_CLOSE(p1[1]);
+ SAFE_CLOSE(p2[0]);
- close(p1[1]);
- close(p2[0]);
+ SAFE_READ(1, p1[0], buf, 3);
- read(p1[0], buf, 3);
id = msgget(KEY_VAL, 0);
- if (id == -1)
- write(p2[1], "notfnd", 7);
+ if (id < 0)
+ SAFE_WRITE(1, p2[1], "notfnd", 7);
else {
- write(p2[1], "exists", 7);
+ SAFE_WRITE(1, p2[1], "exists", 7);
mesgq_read(id);
}
- tst_exit();
-}
-static void setup(void)
-{
- tst_require_root();
- check_newipc();
+ return 0;
}
-int main(int argc, char *argv[])
+static void run(void)
{
- int ret, use_clone = T_NONE, id, n;
- char *tsttype = NONESTR;
+ int use_clone = T_NONE, id, n;
char buf[7];
- setup();
-
- if (argc != 2) {
- tst_resm(TFAIL, "Usage: %s <clone|unshare|none>", argv[0]);
- tst_brkm(TFAIL, NULL, " where clone, unshare, or fork specifies"
- " unshare method.");
- }
-
/* Using PIPE's to sync between container and Parent */
- if (pipe(p1) == -1) {
- perror("pipe");
- exit(EXIT_FAILURE);
- }
- if (pipe(p2) == -1) {
- perror("pipe");
- exit(EXIT_FAILURE);
- }
+ SAFE_PIPE(p1);
+ SAFE_PIPE(p2);
- tsttype = NONESTR;
-
- if (strcmp(argv[1], "clone") == 0) {
+ if (!strcmp(str_op, "clone"))
use_clone = T_CLONE;
- tsttype = CLONESTR;
- } else if (strcmp(argv[1], "unshare") == 0) {
+ else if (!strcmp(str_op, "unshare"))
use_clone = T_UNSHARE;
- tsttype = UNSHARESTR;
- }
id = msgget(KEY_VAL, IPC_CREAT | IPC_EXCL | 0600);
- if (id == -1) {
- perror("msgget");
+ if (id < 0) {
/* Retry without attempting to create the MQ */
id = msgget(KEY_VAL, 0);
- if (id == -1)
- perror("msgget failure"), exit(1);
+ if (id < 0)
+ tst_brk(TBROK, "msgget: %s", tst_strerrno(-id));
}
msg.mtype = 5;
strcpy(msg.mtext, "Message of type 5!");
+
n = msgsnd(id, &msg, strlen(msg.mtext), 0);
- if (n == -1)
- perror("msgsnd"), tst_exit();
+ if (n < 0)
+ tst_brk(TBROK, "msgsnd: %s", tst_strerrno(-n));
- tst_resm(TINFO, "mesgq namespaces test : %s", tsttype);
- /* fire off the test */
- ret = do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_mesgq, NULL);
- if (ret < 0) {
- tst_brkm(TFAIL, NULL, "%s failed", tsttype);
- }
+ tst_res(TINFO, "mesgq namespaces test : %s", str_op);
- close(p1[0]);
- close(p2[1]);
- write(p1[1], "go", 3);
-
- read(p2[0], buf, 7);
- if (strcmp(buf, "exists") == 0) {
- if (use_clone == T_NONE)
- tst_resm(TPASS, "Plain cloned process found mesgq "
- "inside container");
- else
- tst_resm(TFAIL,
- "%s: Container init process found mesgq",
- tsttype);
+ /* fire off the test */
+ clone_unshare_test(use_clone, CLONE_NEWIPC, check_mesgq, NULL);
+
+ SAFE_CLOSE(p1[0]);
+ SAFE_CLOSE(p2[1]);
+
+ SAFE_WRITE(1, p1[1], "go", 3);
+ SAFE_READ(1, p2[0], buf, 7);
+
+ if (!strcmp(buf, "exists")) {
+ if (use_clone == T_NONE) {
+ tst_res(TPASS, "Plain cloned process found mesgq "
+ "inside container");
+ } else {
+ tst_res(TFAIL, "%s: Container init process found mesgq",
+ str_op);
+ }
} else {
- if (use_clone == T_NONE)
- tst_resm(TFAIL,
- "Plain cloned process didn't find mesgq");
- else
- tst_resm(TPASS, "%s: Container didn't find mesgq",
- tsttype);
+ if (use_clone == T_NONE) {
+ tst_res(TFAIL,
+ "Plain cloned process didn't find mesgq");
+ } else {
+ tst_res(TPASS, "%s: Container didn't find mesgq",
+ str_op);
+ }
}
/* Delete the mesgQ */
id = msgget(KEY_VAL, 0);
msgctl(id, IPC_RMID, NULL);
+}
- tst_exit();
+static void setup(void)
+{
+ check_newipc();
+
+ if (strcmp(str_op, "clone") && strcmp(str_op, "unshare") &&
+ strcmp(str_op, "none"))
+ tst_brk(TBROK, "Test execution mode <clone|unshare|none>");
}
+
+static struct tst_test test = {
+ .test_all = run,
+ .setup = setup,
+ .needs_root = 1,
+ .forks_child = 1,
+ .options =
+ (struct tst_option[]){
+ { "m:", &str_op,
+ "Test execution mode <clone|unshare|none>" },
+ {},
+ },
+};
--
2.35.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [LTP] [PATCH v1 2/9] Rewrite mesgq_nstest.c using new LTP API
2022-02-08 10:09 ` [LTP] [PATCH v1 2/9] Rewrite mesgq_nstest.c using new LTP API Andrea Cervesato
@ 2022-03-08 14:42 ` Cyril Hrubis
0 siblings, 0 replies; 13+ messages in thread
From: Cyril Hrubis @ 2022-03-08 14:42 UTC (permalink / raw)
To: Andrea Cervesato; +Cc: ltp
Hi!
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) International Business Machines Corp., 2009
> + * Veerendra C <vechandr@in.ibm.com>
> + * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
> + */
> +
> +/*\
> + * [Description]
> + *
> + * In Parent Process , create mesgq with key 154326L
> + * Now create container by passing 1 of the flag values..
> + * Flag = clone, clone(CLONE_NEWIPC), or unshare(CLONE_NEWIPC)
> + * In cloned process, try to access the created mesgq
> + * Test PASS: If the mesgq is readable when flag is None.
> + * Test FAIL: If the mesgq is readable when flag is Unshare or Clone.
Please reformat this into asciidoc.
> + */
> +
> +#define _GNU_SOURCE
> +
> #include <sys/ipc.h>
> +#include <sys/wait.h>
> #include <sys/msg.h>
> -#include <libclone.h>
> -#include "test.h"
> -#include "ipcns_helper.h"
> -
> -#define KEY_VAL 154326L
> -#define UNSHARESTR "unshare"
> -#define CLONESTR "clone"
> -#define NONESTR "none"
> -
> -char *TCID = "mesgq_nstest";
> -int TST_TOTAL = 1;
> -int p1[2];
> -int p2[2];
> -struct msg_buf {
> - long int mtype; /* type of received/sent message */
> - char mtext[80]; /* text of the message */
> +#include <sys/types.h>
> +#include "tst_test.h"
> +#include "common.h"
> +
> +#define KEY_VAL 154326L
> +
> +static char *str_op = "clone";
> +
> +static int p1[2];
> +static int p2[2];
> +
> +static struct msg_buf {
> + int mtype; /* type of received/sent message */
> + char mtext[80]; /* text of the message */
Just remove the useless comments here.
> } msg;
>
> -void mesgq_read(int id)
> +static void mesgq_read(int id)
> {
> - int READMAX = 80;
> int n;
> - /* read msg type 5 on the Q; msgtype, flags are last 2 params.. */
>
> - n = msgrcv(id, &msg, READMAX, 5, 0);
> - if (n == -1)
> - perror("msgrcv"), tst_exit();
> + /* read msg type 5 on the Q; msgtype, flags are last 2 params.. */
> + n = msgrcv(id, &msg, sizeof(struct msg_buf) - sizeof(long), 5, 0);
> + if (n < 0)
> + tst_brk(TBROK, "msgrcv: %s", tst_strerrno(-n));
>
> - tst_resm(TINFO, "Mesg read of %d bytes; Type %ld: Msg: %.*s",
> - n, msg.mtype, n, msg.mtext);
> + tst_res(TINFO, "Mesg read of %d bytes; Type %ld: Msg: %.*s", n,
> + msg.mtype, n, msg.mtext);
> }
>
> -int check_mesgq(void *vtest)
> +static int check_mesgq(LTP_ATTRIBUTE_UNUSED void *vtest)
> {
> char buf[3];
> int id;
>
> - (void) vtest;
> + SAFE_CLOSE(p1[1]);
> + SAFE_CLOSE(p2[0]);
>
> - close(p1[1]);
> - close(p2[0]);
> + SAFE_READ(1, p1[0], buf, 3);
>
> - read(p1[0], buf, 3);
> id = msgget(KEY_VAL, 0);
> - if (id == -1)
> - write(p2[1], "notfnd", 7);
> + if (id < 0)
> + SAFE_WRITE(1, p2[1], "notfnd", 7);
> else {
> - write(p2[1], "exists", 7);
> + SAFE_WRITE(1, p2[1], "exists", 7);
> mesgq_read(id);
> }
There is absolutely no reason to propagate the test results via pipe in
the new library tests. You can use the tst_res() directly here in the
child process.
Generally there is no need for the pipes at all, the parent should just
send a message and start a child that will attempt to receive it. The
child will either produce PASS or FAIL depending on a value of a global
variable that would be initialized in the setup with the value of the
enum that describes the test type (NONE, CLONE, UNSHARE) and the parent
will just do tst_reap_children() and the remove the message queue.
> - tst_exit();
> -}
>
> -static void setup(void)
> -{
> - tst_require_root();
> - check_newipc();
> + return 0;
> }
>
> -int main(int argc, char *argv[])
> +static void run(void)
> {
> - int ret, use_clone = T_NONE, id, n;
> - char *tsttype = NONESTR;
> + int use_clone = T_NONE, id, n;
> char buf[7];
>
> - setup();
> -
> - if (argc != 2) {
> - tst_resm(TFAIL, "Usage: %s <clone|unshare|none>", argv[0]);
> - tst_brkm(TFAIL, NULL, " where clone, unshare, or fork specifies"
> - " unshare method.");
> - }
> -
> /* Using PIPE's to sync between container and Parent */
> - if (pipe(p1) == -1) {
> - perror("pipe");
> - exit(EXIT_FAILURE);
> - }
> - if (pipe(p2) == -1) {
> - perror("pipe");
> - exit(EXIT_FAILURE);
> - }
> + SAFE_PIPE(p1);
> + SAFE_PIPE(p2);
>
> - tsttype = NONESTR;
> -
> - if (strcmp(argv[1], "clone") == 0) {
> + if (!strcmp(str_op, "clone"))
> use_clone = T_CLONE;
> - tsttype = CLONESTR;
> - } else if (strcmp(argv[1], "unshare") == 0) {
> + else if (!strcmp(str_op, "unshare"))
> use_clone = T_UNSHARE;
> - tsttype = UNSHARESTR;
> - }
>
> id = msgget(KEY_VAL, IPC_CREAT | IPC_EXCL | 0600);
> - if (id == -1) {
> - perror("msgget");
> + if (id < 0) {
> /* Retry without attempting to create the MQ */
> id = msgget(KEY_VAL, 0);
> - if (id == -1)
> - perror("msgget failure"), exit(1);
> + if (id < 0)
> + tst_brk(TBROK, "msgget: %s", tst_strerrno(-id));
> }
>
> msg.mtype = 5;
> strcpy(msg.mtext, "Message of type 5!");
> +
> n = msgsnd(id, &msg, strlen(msg.mtext), 0);
> - if (n == -1)
> - perror("msgsnd"), tst_exit();
> + if (n < 0)
> + tst_brk(TBROK, "msgsnd: %s", tst_strerrno(-n));
>
> - tst_resm(TINFO, "mesgq namespaces test : %s", tsttype);
> - /* fire off the test */
> - ret = do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_mesgq, NULL);
> - if (ret < 0) {
> - tst_brkm(TFAIL, NULL, "%s failed", tsttype);
> - }
> + tst_res(TINFO, "mesgq namespaces test : %s", str_op);
>
> - close(p1[0]);
> - close(p2[1]);
> - write(p1[1], "go", 3);
> -
> - read(p2[0], buf, 7);
> - if (strcmp(buf, "exists") == 0) {
> - if (use_clone == T_NONE)
> - tst_resm(TPASS, "Plain cloned process found mesgq "
> - "inside container");
> - else
> - tst_resm(TFAIL,
> - "%s: Container init process found mesgq",
> - tsttype);
> + /* fire off the test */
> + clone_unshare_test(use_clone, CLONE_NEWIPC, check_mesgq, NULL);
> +
> + SAFE_CLOSE(p1[0]);
> + SAFE_CLOSE(p2[1]);
> +
> + SAFE_WRITE(1, p1[1], "go", 3);
> + SAFE_READ(1, p2[0], buf, 7);
> +
> + if (!strcmp(buf, "exists")) {
> + if (use_clone == T_NONE) {
> + tst_res(TPASS, "Plain cloned process found mesgq "
> + "inside container");
> + } else {
> + tst_res(TFAIL, "%s: Container init process found mesgq",
> + str_op);
> + }
> } else {
> - if (use_clone == T_NONE)
> - tst_resm(TFAIL,
> - "Plain cloned process didn't find mesgq");
> - else
> - tst_resm(TPASS, "%s: Container didn't find mesgq",
> - tsttype);
> + if (use_clone == T_NONE) {
> + tst_res(TFAIL,
> + "Plain cloned process didn't find mesgq");
> + } else {
> + tst_res(TPASS, "%s: Container didn't find mesgq",
> + str_op);
> + }
> }
>
> /* Delete the mesgQ */
> id = msgget(KEY_VAL, 0);
> msgctl(id, IPC_RMID, NULL);
> +}
>
> - tst_exit();
> +static void setup(void)
> +{
> + check_newipc();
Technically this is not required for "none".
> + if (strcmp(str_op, "clone") && strcmp(str_op, "unshare") &&
> + strcmp(str_op, "none"))
> + tst_brk(TBROK, "Test execution mode <clone|unshare|none>");
Can we just add a function to parse the str_op and return the enum into
the common.h instead of checking it here and then converting it in the
run() function we should convert it here in the setup...
> }
> +
> +static struct tst_test test = {
> + .test_all = run,
> + .setup = setup,
> + .needs_root = 1,
> + .forks_child = 1,
> + .options =
> + (struct tst_option[]){
> + { "m:", &str_op,
> + "Test execution mode <clone|unshare|none>" },
> + {},
> + },
> +};
> --
> 2.35.1
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 13+ messages in thread
* [LTP] [PATCH v1 3/9] Rewrite msg_comm.c using new LTP API
2022-02-08 10:09 [LTP] [PATCH v1 0/9] Rewrite sysvipc testing suite using new LTP API Andrea Cervesato
2022-02-08 10:09 ` [LTP] [PATCH v1 1/9] Remove libclone dependency from sysvipc Andrea Cervesato
2022-02-08 10:09 ` [LTP] [PATCH v1 2/9] Rewrite mesgq_nstest.c using new LTP API Andrea Cervesato
@ 2022-02-08 10:09 ` Andrea Cervesato
2022-03-08 14:47 ` Cyril Hrubis
2022-02-08 10:09 ` [LTP] [PATCH v1 4/9] Rewrite sem_comm.c " Andrea Cervesato
` (5 subsequent siblings)
8 siblings, 1 reply; 13+ messages in thread
From: Andrea Cervesato @ 2022-02-08 10:09 UTC (permalink / raw)
To: ltp
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
.../kernel/containers/sysvipc/msg_comm.c | 159 +++++++-----------
1 file changed, 63 insertions(+), 96 deletions(-)
diff --git a/testcases/kernel/containers/sysvipc/msg_comm.c b/testcases/kernel/containers/sysvipc/msg_comm.c
index 0da328997..ec6dbd7fa 100644
--- a/testcases/kernel/containers/sysvipc/msg_comm.c
+++ b/testcases/kernel/containers/sysvipc/msg_comm.c
@@ -1,20 +1,12 @@
-/* Copyright (c) 2014 Red Hat, Inc.
- *
- * 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: msg_comm.c
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2014 Red Hat, Inc.
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
*
- * Description:
* 1. Clones two child processes with CLONE_NEWIPC flag, each child
* gets System V message queue (msg) with the _identical_ key.
* 2. Child1 appends a message with identifier #1 to the message queue.
@@ -27,152 +19,127 @@
*/
#define _GNU_SOURCE
+
#include <sys/ipc.h>
+#include <sys/wait.h>
#include <sys/msg.h>
#include <sys/types.h>
-#include <sys/wait.h>
-#include <stdio.h>
-#include <errno.h>
-#include "ipcns_helper.h"
-#include "test.h"
-#include "safe_macros.h"
+#include "tst_test.h"
+#include "common.h"
#define TESTKEY 124426L
#define MSGSIZE 50
-char *TCID = "msg_comm";
-int TST_TOTAL = 1;
struct sysv_msg {
long mtype;
char mtext[MSGSIZE];
};
-static void cleanup(void)
+static int chld1_msg(LTP_ATTRIBUTE_UNUSED void *arg)
{
- tst_rmdir();
-}
-
-static void setup(void)
-{
- tst_require_root();
- check_newipc();
- tst_tmpdir();
- TST_CHECKPOINT_INIT(tst_rmdir);
-}
-
-int chld1_msg(void *arg)
-{
- int id, n, rval = 0;
+ int id, ret, rval = 0;
struct sysv_msg m;
struct sysv_msg rec;
id = msgget(TESTKEY, IPC_CREAT | 0600);
- if (id == -1) {
- perror("msgget");
- return 2;
- }
+ if (id < 0)
+ tst_brk(TBROK, "msgget: %s", tst_strerrno(-id));
m.mtype = 1;
m.mtext[0] = 'A';
- if (msgsnd(id, &m, sizeof(struct sysv_msg) - sizeof(long), 0) == -1) {
- perror("msgsnd");
+
+ ret = msgsnd(id, &m, sizeof(struct sysv_msg) - sizeof(long), 0);
+ if (ret < 0) {
msgctl(id, IPC_RMID, NULL);
- return 2;
+ tst_brk(TBROK, "msgsnd: %s", tst_strerrno(-ret));
}
/* wait for child2 to write into the message queue */
- TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
+ TST_CHECKPOINT_WAIT(0);
/* if child1 message queue has changed (by child2) report fail */
- n = msgrcv(id, &rec, sizeof(struct sysv_msg) - sizeof(long),
- 2, IPC_NOWAIT);
- if (n == -1 && errno != ENOMSG) {
- perror("msgrcv");
+ ret = msgrcv(id, &rec, sizeof(struct sysv_msg) - sizeof(long), 2,
+ IPC_NOWAIT);
+ if (ret < 0 && errno != ENOMSG) {
msgctl(id, IPC_RMID, NULL);
- return 2;
+ tst_brk(TBROK, "msgrcv: %s", tst_strerrno(-ret));
}
+
/* if mtype #2 was found in the message queue, it is fail */
- if (n > 0) {
+ if (ret > 0)
rval = 1;
- }
/* tell child2 to continue */
- TST_SAFE_CHECKPOINT_WAKE(NULL, 0);
+ TST_CHECKPOINT_WAKE(0);
msgctl(id, IPC_RMID, NULL);
+
return rval;
}
-int chld2_msg(void *arg)
+static int chld2_msg(LTP_ATTRIBUTE_UNUSED void *arg)
{
- int id;
+ int id, ret;
struct sysv_msg m;
id = msgget(TESTKEY, IPC_CREAT | 0600);
- if (id == -1) {
- perror("msgget");
- return 2;
- }
+ if (id < 0)
+ tst_brk(TBROK, "msgget: %s", tst_strerrno(-id));
m.mtype = 2;
m.mtext[0] = 'B';
- if (msgsnd(id, &m, sizeof(struct sysv_msg) - sizeof(long), 0) == -1) {
- perror("msgsnd");
+
+ ret = msgsnd(id, &m, sizeof(struct sysv_msg) - sizeof(long), 0);
+ if (ret < 0) {
msgctl(id, IPC_RMID, NULL);
- return 2;
+ tst_brk(TBROK, "msgsnd: %s", tst_strerrno(-ret));
}
/* tell child1 to continue and wait for it */
- TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(NULL, 0);
+ TST_CHECKPOINT_WAKE_AND_WAIT(0);
msgctl(id, IPC_RMID, NULL);
+
return 0;
}
-static void test(void)
+static void run(void)
{
int status, ret = 0;
- ret = do_clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld1_msg, NULL);
- if (ret == -1)
- tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
-
- ret = do_clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld2_msg, NULL);
- if (ret == -1)
- tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
-
+ clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld1_msg, NULL);
+ clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld2_msg, NULL);
while (wait(&status) > 0) {
if (WIFEXITED(status) && WEXITSTATUS(status) == 1)
ret = 1;
+
if (WIFEXITED(status) && WEXITSTATUS(status) == 2)
- tst_brkm(TBROK | TERRNO, cleanup, "error in child");
+ tst_brk(TBROK, "error in child");
+
if (WIFSIGNALED(status)) {
- tst_resm(TFAIL, "child was killed with signal %s",
- tst_strsig(WTERMSIG(status)));
- return;
+ tst_brk(TBROK, "child was killed with signal %s",
+ tst_strsig(WTERMSIG(status)));
}
}
- if (ret)
- tst_resm(TFAIL, "SysV msg: communication with identical keys"
- " between namespaces");
- else
- tst_resm(TPASS, "SysV msg: communication with identical keys"
- " between namespaces");
+ if (ret) {
+ tst_res(TFAIL, "SysV msg: communication with identical keys"
+ " between namespaces");
+ } else {
+ tst_res(TPASS, "SysV msg: communication with identical keys"
+ " between namespaces");
+ }
}
-int main(int argc, char *argv[])
+static void setup(void)
{
- int lc;
-
- tst_parse_opts(argc, argv, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++)
- test();
-
- cleanup();
- tst_exit();
+ check_newipc();
}
+
+static struct tst_test test = {
+ .test_all = run,
+ .setup = setup,
+ .needs_root = 1,
+ .needs_checkpoints = 1,
+};
--
2.35.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [LTP] [PATCH v1 3/9] Rewrite msg_comm.c using new LTP API
2022-02-08 10:09 ` [LTP] [PATCH v1 3/9] Rewrite msg_comm.c " Andrea Cervesato
@ 2022-03-08 14:47 ` Cyril Hrubis
0 siblings, 0 replies; 13+ messages in thread
From: Cyril Hrubis @ 2022-03-08 14:47 UTC (permalink / raw)
To: Andrea Cervesato; +Cc: ltp
Hi!
Similar here.
> #define _GNU_SOURCE
> +
> #include <sys/ipc.h>
> +#include <sys/wait.h>
> #include <sys/msg.h>
> #include <sys/types.h>
> -#include <sys/wait.h>
> -#include <stdio.h>
> -#include <errno.h>
> -#include "ipcns_helper.h"
> -#include "test.h"
> -#include "safe_macros.h"
> +#include "tst_test.h"
> +#include "common.h"
>
> #define TESTKEY 124426L
> #define MSGSIZE 50
> -char *TCID = "msg_comm";
> -int TST_TOTAL = 1;
>
> struct sysv_msg {
> long mtype;
> char mtext[MSGSIZE];
> };
>
> -static void cleanup(void)
> +static int chld1_msg(LTP_ATTRIBUTE_UNUSED void *arg)
> {
> - tst_rmdir();
> -}
> -
> -static void setup(void)
> -{
> - tst_require_root();
> - check_newipc();
> - tst_tmpdir();
> - TST_CHECKPOINT_INIT(tst_rmdir);
> -}
> -
> -int chld1_msg(void *arg)
> -{
> - int id, n, rval = 0;
> + int id, ret, rval = 0;
> struct sysv_msg m;
> struct sysv_msg rec;
>
> id = msgget(TESTKEY, IPC_CREAT | 0600);
> - if (id == -1) {
> - perror("msgget");
> - return 2;
> - }
> + if (id < 0)
> + tst_brk(TBROK, "msgget: %s", tst_strerrno(-id));
>
> m.mtype = 1;
> m.mtext[0] = 'A';
> - if (msgsnd(id, &m, sizeof(struct sysv_msg) - sizeof(long), 0) == -1) {
> - perror("msgsnd");
> +
> + ret = msgsnd(id, &m, sizeof(struct sysv_msg) - sizeof(long), 0);
> + if (ret < 0) {
> msgctl(id, IPC_RMID, NULL);
> - return 2;
> + tst_brk(TBROK, "msgsnd: %s", tst_strerrno(-ret));
> }
>
> /* wait for child2 to write into the message queue */
> - TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
> + TST_CHECKPOINT_WAIT(0);
>
> /* if child1 message queue has changed (by child2) report fail */
> - n = msgrcv(id, &rec, sizeof(struct sysv_msg) - sizeof(long),
> - 2, IPC_NOWAIT);
> - if (n == -1 && errno != ENOMSG) {
> - perror("msgrcv");
> + ret = msgrcv(id, &rec, sizeof(struct sysv_msg) - sizeof(long), 2,
> + IPC_NOWAIT);
> + if (ret < 0 && errno != ENOMSG) {
> msgctl(id, IPC_RMID, NULL);
> - return 2;
> + tst_brk(TBROK, "msgrcv: %s", tst_strerrno(-ret));
This is wrong in many places, the msg*() calls return -1 and set errno
so you really just need to pass TERRNO along with the TBROK.
> }
> +
> /* if mtype #2 was found in the message queue, it is fail */
> - if (n > 0) {
> + if (ret > 0)
> rval = 1;
> - }
>
> /* tell child2 to continue */
> - TST_SAFE_CHECKPOINT_WAKE(NULL, 0);
> + TST_CHECKPOINT_WAKE(0);
>
> msgctl(id, IPC_RMID, NULL);
> +
> return rval;
> }
>
> -int chld2_msg(void *arg)
> +static int chld2_msg(LTP_ATTRIBUTE_UNUSED void *arg)
> {
> - int id;
> + int id, ret;
> struct sysv_msg m;
>
> id = msgget(TESTKEY, IPC_CREAT | 0600);
> - if (id == -1) {
> - perror("msgget");
> - return 2;
> - }
> + if (id < 0)
> + tst_brk(TBROK, "msgget: %s", tst_strerrno(-id));
>
> m.mtype = 2;
> m.mtext[0] = 'B';
> - if (msgsnd(id, &m, sizeof(struct sysv_msg) - sizeof(long), 0) == -1) {
> - perror("msgsnd");
> +
> + ret = msgsnd(id, &m, sizeof(struct sysv_msg) - sizeof(long), 0);
> + if (ret < 0) {
> msgctl(id, IPC_RMID, NULL);
> - return 2;
> + tst_brk(TBROK, "msgsnd: %s", tst_strerrno(-ret));
> }
>
> /* tell child1 to continue and wait for it */
> - TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(NULL, 0);
> + TST_CHECKPOINT_WAKE_AND_WAIT(0);
>
> msgctl(id, IPC_RMID, NULL);
> +
> return 0;
> }
>
> -static void test(void)
> +static void run(void)
> {
> int status, ret = 0;
>
> - ret = do_clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld1_msg, NULL);
> - if (ret == -1)
> - tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
> -
> - ret = do_clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld2_msg, NULL);
> - if (ret == -1)
> - tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
> -
> + clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld1_msg, NULL);
> + clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld2_msg, NULL);
>
> while (wait(&status) > 0) {
> if (WIFEXITED(status) && WEXITSTATUS(status) == 1)
> ret = 1;
> +
> if (WIFEXITED(status) && WEXITSTATUS(status) == 2)
> - tst_brkm(TBROK | TERRNO, cleanup, "error in child");
> + tst_brk(TBROK, "error in child");
> +
> if (WIFSIGNALED(status)) {
> - tst_resm(TFAIL, "child was killed with signal %s",
> - tst_strsig(WTERMSIG(status)));
> - return;
> + tst_brk(TBROK, "child was killed with signal %s",
> + tst_strsig(WTERMSIG(status)));
> }
> }
>
> - if (ret)
> - tst_resm(TFAIL, "SysV msg: communication with identical keys"
> - " between namespaces");
> - else
> - tst_resm(TPASS, "SysV msg: communication with identical keys"
> - " between namespaces");
> + if (ret) {
> + tst_res(TFAIL, "SysV msg: communication with identical keys"
> + " between namespaces");
> + } else {
> + tst_res(TPASS, "SysV msg: communication with identical keys"
> + " between namespaces");
> + }
No need to propagate anything at all, just do the PASS/FAIL in the
respective tests.
> }
>
> -int main(int argc, char *argv[])
> +static void setup(void)
> {
> - int lc;
> -
> - tst_parse_opts(argc, argv, NULL, NULL);
> -
> - setup();
> -
> - for (lc = 0; TEST_LOOPING(lc); lc++)
> - test();
> -
> - cleanup();
> - tst_exit();
> + check_newipc();
> }
> +
> +static struct tst_test test = {
> + .test_all = run,
> + .setup = setup,
> + .needs_root = 1,
> + .needs_checkpoints = 1,
> +};
> --
> 2.35.1
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 13+ messages in thread
* [LTP] [PATCH v1 4/9] Rewrite sem_comm.c using new LTP API
2022-02-08 10:09 [LTP] [PATCH v1 0/9] Rewrite sysvipc testing suite using new LTP API Andrea Cervesato
` (2 preceding siblings ...)
2022-02-08 10:09 ` [LTP] [PATCH v1 3/9] Rewrite msg_comm.c " Andrea Cervesato
@ 2022-02-08 10:09 ` Andrea Cervesato
2022-03-08 14:51 ` Cyril Hrubis
2022-02-08 10:09 ` [LTP] [PATCH v1 5/9] Rewrite sem_nstest.c " Andrea Cervesato
` (4 subsequent siblings)
8 siblings, 1 reply; 13+ messages in thread
From: Andrea Cervesato @ 2022-02-08 10:09 UTC (permalink / raw)
To: ltp
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
.../kernel/containers/sysvipc/sem_comm.c | 160 +++++++-----------
1 file changed, 65 insertions(+), 95 deletions(-)
diff --git a/testcases/kernel/containers/sysvipc/sem_comm.c b/testcases/kernel/containers/sysvipc/sem_comm.c
index a2c354a08..5ec21bab5 100644
--- a/testcases/kernel/containers/sysvipc/sem_comm.c
+++ b/testcases/kernel/containers/sysvipc/sem_comm.c
@@ -1,20 +1,12 @@
-/* Copyright (c) 2014 Red Hat, Inc.
- *
- * 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: sem_comm.c
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2014 Red Hat, Inc.
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
*
- * Description:
* 1. Clones two child processes with CLONE_NEWIPC flag, each child
* creates System V semaphore (sem) with the _identical_ key.
* 2. Child1 locks the semaphore.
@@ -26,166 +18,144 @@
*/
#define _GNU_SOURCE
+
#include <sys/ipc.h>
-#include <sys/types.h>
#include <sys/wait.h>
-#include <stdio.h>
-#include <errno.h>
-#include "ipcns_helper.h"
-#include "test.h"
-#include "safe_macros.h"
+#include <sys/msg.h>
+#include <sys/types.h>
+#include "tst_test.h"
#include "lapi/sem.h"
+#include "common.h"
#define TESTKEY 124426L
-char *TCID = "sem_comm";
-int TST_TOTAL = 1;
-
-static void cleanup(void)
-{
- tst_rmdir();
-}
-static void setup(void)
+static int chld1_sem(LTP_ATTRIBUTE_UNUSED void *arg)
{
- tst_require_root();
- check_newipc();
- tst_tmpdir();
- TST_CHECKPOINT_INIT(tst_rmdir);
-}
-
-int chld1_sem(void *arg)
-{
- int id;
+ int id, ret;
union semun su;
struct sembuf sm;
id = semget(TESTKEY, 1, IPC_CREAT);
- if (id == -1) {
- perror("semget");
- return 2;
- }
+ if (id < 0)
+ tst_brk(TBROK, "semget: %s", tst_strerrno(-id));
su.val = 1;
- if (semctl(id, 0, SETVAL, su) == -1) {
- perror("semctl");
+
+ ret = semctl(id, 0, SETVAL, su);
+ if (ret < 0) {
semctl(id, 0, IPC_RMID);
- return 2;
+ tst_brk(TBROK, "semctl: %s", tst_strerrno(-ret));
}
/* tell child2 to continue and wait for it to create the semaphore */
- TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(NULL, 0);
+ TST_CHECKPOINT_WAKE_AND_WAIT(0);
sm.sem_num = 0;
sm.sem_op = -1;
sm.sem_flg = IPC_NOWAIT;
- if (semop(id, &sm, 1) == -1) {
- perror("semop");
+
+ ret = semop(id, &sm, 1);
+ if (ret < 0) {
semctl(id, 0, IPC_RMID);
- return 2;
+ tst_brk(TBROK, "semop: %s", tst_strerrno(-ret));
}
/* tell child2 to continue and wait for it to lock the semaphore */
- TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(NULL, 0);
+ TST_CHECKPOINT_WAKE_AND_WAIT(0);
sm.sem_op = 1;
semop(id, &sm, 1);
semctl(id, 0, IPC_RMID);
+
return 0;
}
-int chld2_sem(void *arg)
+static int chld2_sem(LTP_ATTRIBUTE_UNUSED void *arg)
{
- int id, rval = 0;
+ int id, ret, rval = 0;
struct sembuf sm;
union semun su;
/* wait for child1 to create the semaphore */
- TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
+ TST_CHECKPOINT_WAIT(0);
id = semget(TESTKEY, 1, IPC_CREAT);
- if (id == -1) {
- perror("semget");
- return 2;
- }
+ if (id < 0)
+ tst_brk(TBROK, "semget: %s", tst_strerrno(-id));
su.val = 1;
- if (semctl(id, 0, SETVAL, su) == -1) {
- perror("semctl");
+
+ ret = semctl(id, 0, SETVAL, su);
+ if (ret < 0) {
semctl(id, 0, IPC_RMID);
- return 2;
+ tst_brk(TBROK, "semctl: %s", tst_strerrno(-ret));
}
/* tell child1 to continue and wait for it to lock the semaphore */
- TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(NULL, 0);
+ TST_CHECKPOINT_WAKE_AND_WAIT(0);
sm.sem_num = 0;
sm.sem_op = -1;
sm.sem_flg = IPC_NOWAIT;
- if (semop(id, &sm, 1) == -1) {
+
+ ret = semop(id, &sm, 1);
+ if (ret < 0) {
if (errno == EAGAIN) {
rval = 1;
} else {
- perror("semop");
semctl(id, 0, IPC_RMID);
- return 2;
+ tst_brk(TBROK, "semop: %s", tst_strerrno(-ret));
}
}
/* tell child1 to continue */
- TST_SAFE_CHECKPOINT_WAKE(NULL, 0);
+ TST_CHECKPOINT_WAKE(0);
sm.sem_op = 1;
semop(id, &sm, 1);
semctl(id, 0, IPC_RMID);
+
return rval;
}
-static void test(void)
+static void run(void)
{
int status, ret = 0;
- ret = do_clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld1_sem, NULL);
- if (ret == -1)
- tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
-
- ret = do_clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld2_sem, NULL);
- if (ret == -1)
- tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
-
+ clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld1_sem, NULL);
+ clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld2_sem, NULL);
while (wait(&status) > 0) {
if (WIFEXITED(status) && WEXITSTATUS(status) == 1)
ret = 1;
+
if (WIFEXITED(status) && WEXITSTATUS(status) == 2)
- tst_brkm(TBROK | TERRNO, cleanup, "error in child");
+ tst_brk(TBROK, "error in child");
+
if (WIFSIGNALED(status)) {
- tst_resm(TFAIL, "child was killed with signal %s",
- tst_strsig(WTERMSIG(status)));
- return;
+ tst_brk(TBROK, "child was killed with signal %s",
+ tst_strsig(WTERMSIG(status)));
}
}
if (ret)
- tst_resm(TFAIL, "SysV sem: communication with identical keys"
- " between namespaces");
+ tst_res(TFAIL, "SysV sem: communication with identical keys"
+ " between namespaces");
else
- tst_resm(TPASS, "SysV sem: communication with identical keys"
- " between namespaces");
+ tst_res(TPASS, "SysV sem: communication with identical keys"
+ " between namespaces");
}
-int main(int argc, char *argv[])
+static void setup(void)
{
- int lc;
-
- tst_parse_opts(argc, argv, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++)
- test();
-
- cleanup();
- tst_exit();
+ check_newipc();
}
+
+static struct tst_test test = {
+ .test_all = run,
+ .setup = setup,
+ .needs_root = 1,
+ .needs_checkpoints = 1,
+};
--
2.35.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 13+ messages in thread* [LTP] [PATCH v1 5/9] Rewrite sem_nstest.c using new LTP API
2022-02-08 10:09 [LTP] [PATCH v1 0/9] Rewrite sysvipc testing suite using new LTP API Andrea Cervesato
` (3 preceding siblings ...)
2022-02-08 10:09 ` [LTP] [PATCH v1 4/9] Rewrite sem_comm.c " Andrea Cervesato
@ 2022-02-08 10:09 ` Andrea Cervesato
2022-02-08 10:09 ` [LTP] [PATCH v1 6/9] Rewrite semtest_2ns.c " Andrea Cervesato
` (3 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Andrea Cervesato @ 2022-02-08 10:09 UTC (permalink / raw)
To: ltp
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
.../kernel/containers/sysvipc/sem_nstest.c | 221 ++++++++----------
1 file changed, 101 insertions(+), 120 deletions(-)
diff --git a/testcases/kernel/containers/sysvipc/sem_nstest.c b/testcases/kernel/containers/sysvipc/sem_nstest.c
index 72e04fe4f..cf206feb3 100644
--- a/testcases/kernel/containers/sysvipc/sem_nstest.c
+++ b/testcases/kernel/containers/sysvipc/sem_nstest.c
@@ -1,158 +1,139 @@
-/* *************************************************************************
-* Copyright (c) International Business Machines Corp., 2009
-* 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
-*
-* Author: Veerendra C <vechandr@in.ibm.com>
-*
-* In Parent Process , create semaphore with key 154326L
-* Now create container by passing 1 of the below flag values..
-* clone(NONE), clone(CLONE_NEWIPC), or unshare(CLONE_NEWIPC)
-* In cloned process, try to access the created semaphore
-* Test PASS: If the semaphore is readable when flag is None.
-* Test FAIL: If the semaphore is readable when flag is Unshare or Clone.
-***************************************************************************/
-
-#define _GNU_SOURCE 1
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) International Business Machines Corp., 2009
+ * Veerendra C <vechandr@in.ibm.com>
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * In Parent Process , create semaphore with key 154326L
+ * Now create container by passing 1 of the below flag values..
+ * clone(NONE), clone(CLONE_NEWIPC), or unshare(CLONE_NEWIPC)
+ * In cloned process, try to access the created semaphore
+ * Test PASS: If the semaphore is readable when flag is None.
+ * Test FAIL: If the semaphore is readable when flag is Unshare or Clone.
+ */
+
+#define _GNU_SOURCE
+
#include <sys/ipc.h>
+#include <sys/wait.h>
+#include <sys/msg.h>
+#include <sys/types.h>
#include <sys/sem.h>
-#include <libclone.h>
-#include "test.h"
-#include "ipcns_helper.h"
+#include "tst_test.h"
+#include "common.h"
+
+#define MY_KEY 154326L
-#define MY_KEY 154326L
-#define UNSHARESTR "unshare"
-#define CLONESTR "clone"
-#define NONESTR "none"
+static char *str_op = "clone";
-char *TCID = "sem_nstest";
-int TST_TOTAL = 1;
-int p1[2];
-int p2[2];
+static int p1[2];
+static int p2[2];
-int check_semaphore(void *vtest)
+static int check_semaphore(LTP_ATTRIBUTE_UNUSED void *vtest)
{
char buf[3];
int id;
- (void) vtest;
+ SAFE_CLOSE(p1[1]);
+ SAFE_CLOSE(p2[0]);
- close(p1[1]);
- close(p2[0]);
+ SAFE_READ(1, p1[0], buf, 3);
- read(p1[0], buf, 3);
id = semget(MY_KEY, 1, 0);
- if (id == -1)
- write(p2[1], "notfnd", 7);
- else {
- write(p2[1], "exists", 7);
- tst_resm(TINFO, "PID %d: Fetched existing semaphore..id = %d",
- getpid(), id);
+ if (id < 0) {
+ SAFE_WRITE(1, p2[1], "notfnd", 7);
+ } else {
+ SAFE_WRITE(1, p2[1], "exists", 7);
+ tst_res(TINFO, "PID %d: Fetched existing semaphore..id = %d",
+ getpid(), id);
}
- tst_exit();
-}
-static void setup(void)
-{
- tst_require_root();
- check_newipc();
+ return 0;
}
-int main(int argc, char *argv[])
+static void run(void)
{
- int ret, use_clone = T_NONE, id;
- char *tsttype = NONESTR;
+ int use_clone = T_NONE, id;
char buf[7];
- setup();
-
- if (argc != 2) {
- tst_resm(TFAIL, "Usage: %s <clone| unshare| none>", argv[0]);
- tst_brkm(TFAIL, NULL, " where clone, unshare, or fork specifies"
- " unshare method.");
- }
-
/* Using PIPE's to sync between container and Parent */
- if (pipe(p1) == -1) {
- perror("pipe");
- exit(EXIT_FAILURE);
- }
- if (pipe(p2) == -1) {
- perror("pipe");
- exit(EXIT_FAILURE);
- }
+ SAFE_PIPE(p1);
+ SAFE_PIPE(p2);
- if (strcmp(argv[1], "clone") == 0) {
+ if (!strcmp(str_op, "clone"))
use_clone = T_CLONE;
- tsttype = CLONESTR;
- } else if (strcmp(argv[1], "unshare") == 0) {
+ else if (!strcmp(str_op, "unshare"))
use_clone = T_UNSHARE;
- tsttype = UNSHARESTR;
- }
/* 1. Create (or fetch if existing) the binary semaphore */
id = semget(MY_KEY, 1, IPC_CREAT | IPC_EXCL | 0666);
- if (id == -1) {
- perror("Semaphore create");
- if (errno != EEXIST) {
- perror("semget failure");
- tst_brkm(TBROK, NULL, "Semaphore creation failed");
- }
+ if (id < 0) {
+ tst_res(TINFO, "semget failure. Checking existing semaphore");
+
+ if (errno != EEXIST)
+ tst_brk(TBROK, "Semaphore creation failed");
+
id = semget(MY_KEY, 1, 0);
- if (id == -1) {
- perror("Semaphore create");
- tst_brkm(TBROK, NULL, "Semaphore operation failed");
- }
+ if (id < 0)
+ tst_brk(TBROK, "Semaphore operation failed");
}
- tst_resm(TINFO, "Semaphore namespaces Isolation test : %s", tsttype);
- /* fire off the test */
- ret =
- do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_semaphore,
- NULL);
- if (ret < 0) {
- tst_brkm(TFAIL, NULL, "%s failed", tsttype);
- }
+ tst_res(TINFO, "Semaphore namespaces Isolation test : %s", str_op);
- close(p1[0]);
- close(p2[1]);
- write(p1[1], "go", 3);
- read(p2[0], buf, 7);
-
- if (strcmp(buf, "exists") == 0) {
- if (use_clone == T_NONE)
- tst_resm(TPASS, "Plain cloned process found semaphore "
- "inside container");
- else
- tst_resm(TFAIL,
- "%s: Container init process found semaphore",
- tsttype);
+ /* fire off the test */
+ clone_unshare_test(use_clone, CLONE_NEWIPC, check_semaphore, NULL);
+
+ SAFE_CLOSE(p1[0]);
+ SAFE_CLOSE(p2[1]);
+ SAFE_WRITE(1, p1[1], "go", 3);
+ SAFE_READ(1, p2[0], buf, 7);
+
+ if (!strcmp(buf, "exists")) {
+ if (use_clone == T_NONE) {
+ tst_res(TPASS, "Plain cloned process found semaphore "
+ "inside container");
+ } else {
+ tst_res(TFAIL,
+ "%s: Container init process found semaphore",
+ str_op);
+ }
} else {
- if (use_clone == T_NONE)
- tst_resm(TFAIL,
- "Plain cloned process didn't find semaphore");
- else
- tst_resm(TPASS, "%s: Container didn't find semaphore",
- tsttype);
+ if (use_clone == T_NONE) {
+ tst_res(TFAIL,
+ "Plain cloned process didn't find semaphore");
+ } else {
+ tst_res(TPASS, "%s: Container didn't find semaphore",
+ str_op);
+ }
}
/* Delete the semaphore */
id = semget(MY_KEY, 1, 0);
semctl(id, IPC_RMID, 0);
+}
- tst_exit();
+static void setup(void)
+{
+ check_newipc();
+
+ if (strcmp(str_op, "clone") && strcmp(str_op, "unshare") &&
+ strcmp(str_op, "none"))
+ tst_brk(TBROK, "Test execution mode <clone|unshare|none>");
}
+
+static struct tst_test test = {
+ .test_all = run,
+ .setup = setup,
+ .needs_root = 1,
+ .forks_child = 1,
+ .options =
+ (struct tst_option[]){
+ { "m:", &str_op,
+ "Test execution mode <clone|unshare|none>" },
+ {},
+ },
+};
--
2.35.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 13+ messages in thread* [LTP] [PATCH v1 6/9] Rewrite semtest_2ns.c using new LTP API
2022-02-08 10:09 [LTP] [PATCH v1 0/9] Rewrite sysvipc testing suite using new LTP API Andrea Cervesato
` (4 preceding siblings ...)
2022-02-08 10:09 ` [LTP] [PATCH v1 5/9] Rewrite sem_nstest.c " Andrea Cervesato
@ 2022-02-08 10:09 ` Andrea Cervesato
2022-02-08 10:09 ` [LTP] [PATCH v1 7/9] Rewrite shm_comm.c " Andrea Cervesato
` (2 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Andrea Cervesato @ 2022-02-08 10:09 UTC (permalink / raw)
To: ltp
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
.../kernel/containers/sysvipc/semtest_2ns.c | 318 ++++++++----------
1 file changed, 149 insertions(+), 169 deletions(-)
diff --git a/testcases/kernel/containers/sysvipc/semtest_2ns.c b/testcases/kernel/containers/sysvipc/semtest_2ns.c
index c3483b675..9b0d83879 100644
--- a/testcases/kernel/containers/sysvipc/semtest_2ns.c
+++ b/testcases/kernel/containers/sysvipc/semtest_2ns.c
@@ -1,230 +1,210 @@
-/* *************************************************************************
-* Copyright (c) International Business Machines Corp., 2009
-* 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
-*
-* Author: Veerendra C <vechandr@in.ibm.com>
-*
-* Test Assertion:
-* This testcase verifies the semaphore isoloation in 2 diff containers.
-* It tries to create/access a semaphore created with the same KEY.
-*
-* Description:
-* Create 2 'containers' with the below flag value
-* Flag = clone, clone(CLONE_NEWIPC), or unshare(CLONE_NEWIPC)
-* In Cont1, create semaphore with key 124326L
-* In Cont2, try to access the semaphore created in Cont1.
-* PASS :
-* If flag = None and the semaphore is accessible in Cont2.
-* If flag = unshare/clone and the semaphore is not accessible in Cont2.
-* If semaphore is not accessible in Cont2, creates new semaphore with
-* the same key to double check isloation in IPCNS.
-*
-* FAIL :
-* If flag = none and the semaphore is not accessible.
-* If flag = unshare/clone and semaphore is accessible in Cont2.
-* If the new semaphore creation Fails.
-***************************************************************************/
-
-#define _GNU_SOURCE 1
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) International Business Machines Corp., 2009
+ * Veerendra C <vechandr@in.ibm.com>
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Create 2 'containers' with the below flag value
+ * Flag = clone, clone(CLONE_NEWIPC), or unshare(CLONE_NEWIPC)
+ * In Cont1, create semaphore with key 124326L
+ * In Cont2, try to access the semaphore created in Cont1.
+ * PASS :
+ * If flag = None and the semaphore is accessible in Cont2.
+ * If flag = unshare/clone and the semaphore is not accessible in
+ *Cont2. If semaphore is not accessible in Cont2, creates new semaphore with the
+ *same key to double check isloation in IPCNS.
+ *
+ * FAIL :
+ * If flag = none and the semaphore is not accessible.
+ * If flag = unshare/clone and semaphore is accessible in Cont2.
+ * If the new semaphore creation Fails.
+ */
+
+#define _GNU_SOURCE
+
#include <sys/ipc.h>
+#include <sys/wait.h>
+#include <sys/msg.h>
+#include <sys/types.h>
#include <sys/sem.h>
-#include <libclone.h>
-#include "test.h"
-#include "ipcns_helper.h"
-
-#define MY_KEY 124326L
-#define UNSHARESTR "unshare"
-#define CLONESTR "clone"
-#define NONESTR "none"
-
-char *TCID = "semtest_2ns";
-int TST_TOTAL = 1;
-int p1[2];
-int p2[2];
+#include "tst_test.h"
+#include "common.h"
+
+#define MY_KEY 124326L
+
+static char *str_op = "clone";
+
+static int p1[2];
+static int p2[2];
+
static struct sembuf semop_lock[2] = {
/* sem_num, sem_op, flag */
- {0, 0, 0}, /* wait for sem#0 to become 0 */
- {0, 1, SEM_UNDO} /* then increment sem#0 by 1 */
+ { 0, 0, 0 }, /* wait for sem#0 to become 0 */
+ { 0, 1, SEM_UNDO } /* then increment sem#0 by 1 */
};
static struct sembuf semop_unlock[1] = {
/* sem_num, sem_op, flag */
- {0, -1, (IPC_NOWAIT | SEM_UNDO)} /* decrement sem#0 by 1 (sets it to 0) */
+ { 0, -1,
+ (IPC_NOWAIT | SEM_UNDO) } /* decrement sem#0 by 1 (sets it to 0) */
};
/*
* sem_lock() - Locks the semaphore for crit-sec updation, and unlocks it later
*/
-void sem_lock(int id)
+static void sem_lock(int id)
{
+ int ret;
+
/* Checking the semlock and simulating as if the crit-sec is updated */
- if (semop(id, &semop_lock[0], 2) < 0) {
- perror("sem lock error");
- tst_brkm(TBROK, NULL, "semop failed");
- }
- tst_resm(TINFO, "Sem1: File locked, Critical section is updated...");
+ ret = semop(id, &semop_lock[0], 2);
+ if (ret < 0)
+ tst_brk(TBROK, "semop: %s", tst_strerrno(-ret));
+
+ tst_res(TINFO, "Sem1: File locked, Critical section is updated...");
+
sleep(2);
- if (semop(id, &semop_unlock[0], 1) < 0) {
- perror("sem unlock error");
- tst_brkm(TBROK, NULL, "semop failed");
- }
+
+ ret = semop(id, &semop_unlock[0], 1);
+ if (ret < 0)
+ tst_brk(TBROK, "semop: %s", tst_strerrno(-ret));
}
/*
* check_sem1 - does not read -- it writes to check_sem2() when it's done.
*/
-int check_sem1(void *vtest)
+static int check_sem1(LTP_ATTRIBUTE_UNUSED void *vtest)
{
- int id1;
+ SAFE_CLOSE(p1[0]);
- (void) vtest;
-
- close(p1[0]);
/* 1. Create (or fetch if existing) the binary semaphore */
- id1 = semget(MY_KEY, 1, IPC_CREAT | IPC_EXCL | 0666);
- if (id1 == -1) {
- perror("Semaphore create");
- if (errno != EEXIST) {
- perror("semget failure");
- tst_brkm(TBROK, NULL, "semget failure");
- }
- id1 = semget(MY_KEY, 1, 0);
- if (id1 == -1) {
- perror("Semaphore create");
- tst_brkm(TBROK, NULL, "semget failure");
- }
+ TEST(semget(MY_KEY, 1, IPC_CREAT | IPC_EXCL | 0666));
+ if (TST_RET < 0) {
+ tst_res(TINFO, "semget failure. Checking existing semaphore");
+
+ if (TST_ERR != EEXIST)
+ tst_brk(TBROK, "Semaphore creation failed");
+
+ TEST(semget(MY_KEY, 1, 0));
+ if (TST_RET < 0)
+ tst_brk(TBROK, "Semaphore operation failed");
}
- write(p1[1], "go", 3);
- tst_resm(TINFO, "Cont1: Able to create semaphore");
- tst_exit();
+ SAFE_WRITE(1, p1[1], "go", 3);
+
+ tst_res(TINFO, "Cont1: Able to create semaphore");
+
+ return 0;
}
/*
* check_sem2() reads from check_sem1() and writes to main() when it's done.
*/
-
-int check_sem2(void *vtest)
+static int check_sem2(LTP_ATTRIBUTE_UNUSED void *vtest)
{
char buf[3];
int id2;
- (void) vtest;
-
- close(p1[1]);
- close(p2[0]);
- read(p1[0], buf, 3);
+ SAFE_CLOSE(p1[1]);
+ SAFE_CLOSE(p2[0]);
+ SAFE_READ(1, p1[0], buf, 3);
id2 = semget(MY_KEY, 1, 0);
- if (id2 != -1) {
+ if (id2 >= 0) {
sem_lock(id2);
- write(p2[1], "exists", 7);
+ SAFE_WRITE(1, p2[1], "exists", 7);
} else {
- /* Trying to create a new semaphore, if semaphore is not existing */
- id2 = semget(MY_KEY, 1, IPC_CREAT | IPC_EXCL | 0666);
- if (id2 == -1) {
- perror("Semaphore create");
- if (errno != EEXIST) {
- perror("semget failure");
- tst_resm(TBROK, "semget failure");
+ /* Trying to create a new semaphore, if semaphore is not
+ * existing
+ */
+ TEST(semget(MY_KEY, 1, IPC_CREAT | IPC_EXCL | 0666));
+ if (TST_RET < 0) {
+ if (TST_ERR != EEXIST) {
+ tst_brk(TBROK, "semget: %s",
+ tst_strerrno(-TST_RET));
}
- } else
- tst_resm(TINFO,
- "Cont2: Able to create semaphore with sameKey");
+ } else {
+ tst_res(TINFO,
+ "Cont2: Able to create semaphore with sameKey");
+ }
+
/* Passing the pipe Not-found mesg */
- write(p2[1], "notfnd", 7);
+ SAFE_WRITE(1, p2[1], "notfnd", 7);
}
- tst_exit();
-}
-
-static void setup(void)
-{
- tst_require_root();
- check_newipc();
+ return 0;
}
-int main(int argc, char *argv[])
+static void run(void)
{
- int ret, id, use_clone = T_NONE;
- char *tsttype = NONESTR;
+ int id, use_clone = T_NONE;
char buf[7];
- setup();
-
- if (argc != 2) {
- tst_resm(TINFO, "Usage: %s <clone| unshare| none>", argv[0]);
- tst_resm(TINFO, " where clone, unshare, or fork specifies"
- " unshare method.");
- tst_exit();
- }
-
/* Using PIPE's to sync between container and Parent */
- if (pipe(p1) == -1) {
- perror("pipe1");
- tst_exit();
- }
- if (pipe(p2) == -1) {
- perror("pipe2");
- tst_exit();
- }
+ SAFE_PIPE(p1);
+ SAFE_PIPE(p2);
- if (strcmp(argv[1], "clone") == 0) {
+ if (!strcmp(str_op, "clone"))
use_clone = T_CLONE;
- tsttype = CLONESTR;
- } else if (strcmp(argv[1], "unshare") == 0) {
+ else if (!strcmp(str_op, "unshare"))
use_clone = T_UNSHARE;
- tsttype = UNSHARESTR;
- }
- tst_resm(TINFO, "Semaphore Namespaces Test : %s", tsttype);
+ tst_res(TINFO, "Semaphore Namespaces Test : %s", str_op);
/* Create 2 containers */
- ret = do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_sem1, NULL);
- if (ret < 0) {
- tst_brkm(TFAIL, NULL, "clone/unshare failed");
- }
-
- ret = do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_sem2, NULL);
- if (ret < 0) {
- tst_brkm(TFAIL, NULL, "clone/unshare failed");
+ clone_unshare_test(use_clone, CLONE_NEWIPC, check_sem1, NULL);
+ clone_unshare_test(use_clone, CLONE_NEWIPC, check_sem2, NULL);
+
+ SAFE_CLOSE(p2[1]);
+ SAFE_READ(1, p2[0], buf, 7);
+
+ if (!strcmp(buf, "exists"))
+ if (use_clone == T_NONE) {
+ tst_res(TPASS, "Plain cloned process able to access "
+ "the semaphore "
+ "created");
+ } else {
+ tst_res(TFAIL,
+ "%s : In namespace2 found the semaphore "
+ "created in Namespace1",
+ str_op);
+ }
+ else if (use_clone == T_NONE) {
+ tst_res(TFAIL, "Plain cloned process didn't find semaphore");
+ } else {
+ tst_res(TPASS,
+ "%s : In namespace2 unable to access the semaphore "
+ "created in Namespace1",
+ str_op);
}
- close(p2[1]);
- read(p2[0], buf, 7);
-
- if (strcmp(buf, "exists") == 0)
- if (use_clone == T_NONE)
- tst_resm(TPASS,
- "Plain cloned process able to access the semaphore "
- "created");
- else
- tst_resm(TFAIL,
- "%s : In namespace2 found the semaphore "
- "created in Namespace1", tsttype);
- else if (use_clone == T_NONE)
- tst_resm(TFAIL, "Plain cloned process didn't find semaphore");
- else
- tst_resm(TPASS,
- "%s : In namespace2 unable to access the semaphore "
- "created in Namespace1", tsttype);
/* Delete the semaphore */
id = semget(MY_KEY, 1, 0);
semctl(id, IPC_RMID, 0);
- tst_exit();
}
+
+static void setup(void)
+{
+ check_newipc();
+
+ if (strcmp(str_op, "clone") && strcmp(str_op, "unshare") &&
+ strcmp(str_op, "none"))
+ tst_brk(TBROK, "Test execution mode <clone|unshare|none>");
+}
+
+static struct tst_test test = {
+ .test_all = run,
+ .setup = setup,
+ .needs_root = 1,
+ .forks_child = 1,
+ .options =
+ (struct tst_option[]){
+ { "m:", &str_op,
+ "Test execution mode <clone|unshare|none>" },
+ {},
+ },
+};
--
2.35.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 13+ messages in thread* [LTP] [PATCH v1 7/9] Rewrite shm_comm.c using new LTP API
2022-02-08 10:09 [LTP] [PATCH v1 0/9] Rewrite sysvipc testing suite using new LTP API Andrea Cervesato
` (5 preceding siblings ...)
2022-02-08 10:09 ` [LTP] [PATCH v1 6/9] Rewrite semtest_2ns.c " Andrea Cervesato
@ 2022-02-08 10:09 ` Andrea Cervesato
2022-02-08 10:09 ` [LTP] [PATCH v1 8/9] Rewrite shmem_2nstest.c " Andrea Cervesato
2022-02-08 10:09 ` [LTP] [PATCH v1 9/9] Rewrite shmnstest.c " Andrea Cervesato
8 siblings, 0 replies; 13+ messages in thread
From: Andrea Cervesato @ 2022-02-08 10:09 UTC (permalink / raw)
To: ltp
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
.../kernel/containers/sysvipc/shm_comm.c | 144 +++++++-----------
1 file changed, 55 insertions(+), 89 deletions(-)
diff --git a/testcases/kernel/containers/sysvipc/shm_comm.c b/testcases/kernel/containers/sysvipc/shm_comm.c
index 4b3bbfaa8..297486131 100644
--- a/testcases/kernel/containers/sysvipc/shm_comm.c
+++ b/testcases/kernel/containers/sysvipc/shm_comm.c
@@ -1,20 +1,12 @@
-/* Copyright (c) 2014 Red Hat, Inc.
- *
- * 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: shm_comm.c
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2014 Red Hat, Inc.
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
*
- * Description:
* 1. Clones two child processes with CLONE_NEWIPC flag, each child
* allocates System V shared memory segment (shm) with the _identical_
* key and attaches that segment into its address space.
@@ -27,141 +19,115 @@
*/
#define _GNU_SOURCE
+
#include <sys/ipc.h>
#include <sys/shm.h>
-#include <sys/types.h>
#include <sys/wait.h>
-#include <stdio.h>
-#include <errno.h>
-#include "ipcns_helper.h"
-#include "test.h"
-#include "safe_macros.h"
-
+#include <sys/msg.h>
+#include <sys/types.h>
+#include "tst_test.h"
+#include "common.h"
#define TESTKEY 124426L
#define SHMSIZE 50
-char *TCID = "shm_comm";
-int TST_TOTAL = 1;
-
-static void cleanup(void)
-{
- tst_rmdir();
-}
-
-static void setup(void)
-{
- tst_require_root();
- check_newipc();
- tst_tmpdir();
- TST_CHECKPOINT_INIT(tst_rmdir);
-}
-int chld1_shm(void *arg)
+static int chld1_shm(LTP_ATTRIBUTE_UNUSED void *arg)
{
int id, rval = 0;
char *shmem;
id = shmget(TESTKEY, SHMSIZE, IPC_CREAT);
- if (id == -1) {
- perror("shmget");
- return 2;
- }
+ if (id < 0)
+ tst_brk(TBROK, "shmget: %s", tst_strerrno(-id));
- if ((shmem = shmat(id, NULL, 0)) == (char *) -1) {
- perror("shmat");
+ shmem = shmat(id, NULL, 0);
+ if (shmem == (char *)-1) {
shmctl(id, IPC_RMID, NULL);
- return 2;
+ tst_brk(TBROK, "shmem error");
}
*shmem = 'A';
- TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(NULL, 0);
+ TST_CHECKPOINT_WAKE_AND_WAIT(0);
/* if child1 shared segment has changed (by child2) report fail */
if (*shmem != 'A')
rval = 1;
/* tell child2 to continue */
- TST_SAFE_CHECKPOINT_WAKE(NULL, 0);
+ TST_CHECKPOINT_WAKE(0);
shmdt(shmem);
shmctl(id, IPC_RMID, NULL);
+
return rval;
}
-int chld2_shm(void *arg)
+static int chld2_shm(LTP_ATTRIBUTE_UNUSED void *arg)
{
int id;
char *shmem;
id = shmget(TESTKEY, SHMSIZE, IPC_CREAT);
- if (id == -1) {
- perror("shmget");
- return 2;
- }
+ if (id < 0)
+ tst_brk(TBROK, "shmget: %s", tst_strerrno(-id));
- if ((shmem = shmat(id, NULL, 0)) == (char *) -1) {
- perror("shmat");
+ shmem = shmat(id, NULL, 0);
+ if (shmem == (char *)-1) {
shmctl(id, IPC_RMID, NULL);
- return 2;
+ tst_brk(TBROK, "shmem error");
}
/* wait for child1 to write to his segment */
- TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
+ TST_CHECKPOINT_WAIT(0);
*shmem = 'B';
- TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(NULL, 0);
+ TST_CHECKPOINT_WAKE_AND_WAIT(0);
shmdt(shmem);
shmctl(id, IPC_RMID, NULL);
+
return 0;
}
-static void test(void)
+static void run(void)
{
int status, ret = 0;
- ret = do_clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld1_shm, NULL);
- if (ret == -1)
- tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
-
- ret = do_clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld2_shm, NULL);
- if (ret == -1)
- tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
-
+ clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld1_shm, NULL);
+ clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld2_shm, NULL);
while (wait(&status) > 0) {
if (WIFEXITED(status) && WEXITSTATUS(status) == 1)
ret = 1;
+
if (WIFEXITED(status) && WEXITSTATUS(status) == 2)
- tst_brkm(TBROK | TERRNO, cleanup, "error in child");
+ tst_brk(TBROK, "error in child");
+
if (WIFSIGNALED(status)) {
- tst_resm(TFAIL, "child was killed with signal %s",
- tst_strsig(WTERMSIG(status)));
- return;
+ tst_brk(TBROK, "child was killed with signal %s",
+ tst_strsig(WTERMSIG(status)));
}
}
- if (ret)
- tst_resm(TFAIL, "SysV shm: communication with identical keys"
- " between namespaces");
- else
- tst_resm(TPASS, "SysV shm: communication with identical keys"
- " between namespaces");
+ if (ret) {
+ tst_res(TFAIL, "SysV shm: communication with identical keys"
+ " between namespaces");
+ } else {
+ tst_res(TPASS, "SysV shm: communication with identical keys"
+ " between namespaces");
+ }
}
-int main(int argc, char *argv[])
+static void setup(void)
{
- int lc;
-
- tst_parse_opts(argc, argv, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++)
- test();
-
- cleanup();
- tst_exit();
+ check_newipc();
}
+
+static struct tst_test test = {
+ .test_all = run,
+ .setup = setup,
+ .needs_root = 1,
+ .needs_checkpoints = 1,
+};
--
2.35.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 13+ messages in thread* [LTP] [PATCH v1 8/9] Rewrite shmem_2nstest.c using new LTP API
2022-02-08 10:09 [LTP] [PATCH v1 0/9] Rewrite sysvipc testing suite using new LTP API Andrea Cervesato
` (6 preceding siblings ...)
2022-02-08 10:09 ` [LTP] [PATCH v1 7/9] Rewrite shm_comm.c " Andrea Cervesato
@ 2022-02-08 10:09 ` Andrea Cervesato
2022-02-08 10:09 ` [LTP] [PATCH v1 9/9] Rewrite shmnstest.c " Andrea Cervesato
8 siblings, 0 replies; 13+ messages in thread
From: Andrea Cervesato @ 2022-02-08 10:09 UTC (permalink / raw)
To: ltp
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
.../kernel/containers/sysvipc/shmem_2nstest.c | 262 ++++++++----------
1 file changed, 122 insertions(+), 140 deletions(-)
diff --git a/testcases/kernel/containers/sysvipc/shmem_2nstest.c b/testcases/kernel/containers/sysvipc/shmem_2nstest.c
index b172ee07c..8f64f365c 100644
--- a/testcases/kernel/containers/sysvipc/shmem_2nstest.c
+++ b/testcases/kernel/containers/sysvipc/shmem_2nstest.c
@@ -1,187 +1,169 @@
-/* *************************************************************************
-* Copyright (c) International Business Machines Corp., 2009
-* 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
-*
-* Author: Veerendra C <vechandr@in.ibm.com>
-*
-* Test Assertion:
-* This testcase verifies the Shared Memory isoloation in 2 containers.
-* It tries to create/access a Shared Memory created with the same KEY.
-*
-* Description:
-* Create 2 'containers' with the below flag value
-* Flag = clone, clone(CLONE_NEWIPC), or unshare(CLONE_NEWIPC)
-* In Cont1, create Shared Memory segment with key 124426L
-* In Cont2, try to access the MQ created in Cont1.
-* PASS :
-* If flag = None and the shmem seg is accessible in Cont2.
-* If flag = unshare/clone and the shmem seg is not accessible in Cont2.
-* If shmem seg is not accessible in Cont2,
-* creates new shmem with same key to double check isloation in IPCNS.
-*
-* FAIL :
-* If flag = none and the shmem seg is not accessible.
-* If flag = unshare/clone and shmem seg is accessible in Cont2.
-* If the new shmem seg creation Fails.
-***************************************************************************/
-
-#define _GNU_SOURCE 1
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) International Business Machines Corp., 2009
+ * Veerendra C <vechandr@in.ibm.com>
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Create 2 'containers' with the below flag value
+ * Flag = clone, clone(CLONE_NEWIPC), or unshare(CLONE_NEWIPC)
+ * In Cont1, create Shared Memory segment with key 124426L
+ * In Cont2, try to access the MQ created in Cont1.
+ * PASS :
+ * If flag = None and the shmem seg is accessible in Cont2.
+ * If flag = unshare/clone and the shmem seg is not accessible in
+ * Cont2. If shmem seg is not accessible in Cont2, creates new shmem with same
+ * key to double check isloation in IPCNS.
+ *
+ * FAIL :
+ * If flag = none and the shmem seg is not accessible.
+ * If flag = unshare/clone and shmem seg is accessible in Cont2.
+ * If the new shmem seg creation Fails.
+ */
+
+#define _GNU_SOURCE
+
#include <sys/ipc.h>
#include <sys/shm.h>
-#include <libclone.h>
-#include "test.h"
-#include "safe_macros.h"
-#include "ipcns_helper.h"
+#include <sys/wait.h>
+#include <sys/msg.h>
+#include <sys/types.h>
+#include "tst_test.h"
+#include "common.h"
+
+#define TESTKEY 124426L
-#define TESTKEY 124426L
-#define UNSHARESTR "unshare"
-#define CLONESTR "clone"
-#define NONESTR "none"
+static char *str_op = "clone";
-char *TCID = "shmem_2nstest";
-int TST_TOTAL = 1;
-int p2[2];
-int p1[2];
+static int p1[2];
+static int p2[2];
/*
* check_shmem1() does not read -- it writes to check_shmem2() when it's done.
*/
-int check_shmem1(void *vtest)
+static int check_shmem1(LTP_ATTRIBUTE_UNUSED void *vtest)
{
- int id1;
+ SAFE_CLOSE(p1[0]);
- (void) vtest;
+ /* first create the key */
+ TEST(shmget(TESTKEY, 100, IPC_CREAT));
+ if (TST_RET < 0)
+ tst_brk(TBROK, "shmget: %s", tst_strerrno(-TST_ERR));
- close(p1[0]);
+ tst_res(TINFO, "Cont1: Able to create shared mem segment");
- /* first create the key */
- id1 = shmget(TESTKEY, 100, IPC_CREAT);
- if (id1 == -1)
- tst_brkm(TFAIL | TERRNO, NULL, "shmget failed");
+ SAFE_WRITE(1, p1[1], "done", 5);
- tst_resm(TINFO, "Cont1: Able to create shared mem segment");
- write(p1[1], "done", 5);
- tst_exit();
+ return 0;
}
/*
* check_shmem2() reads from check_shmem1() and writes to main() when it's done.
*/
-int check_shmem2(void *vtest)
+static int check_shmem2(LTP_ATTRIBUTE_UNUSED void *vtest)
{
char buf[3];
- int id2;
- (void) vtest;
+ SAFE_CLOSE(p1[1]);
+ SAFE_CLOSE(p2[0]);
- close(p1[1]);
- close(p2[0]);
+ SAFE_READ(1, p1[0], buf, 3);
- read(p1[0], buf, 3);
/* Trying to access shmem, if not existing create new shmem */
- id2 = shmget(TESTKEY, 100, 0);
- if (id2 == -1) {
- id2 = shmget(TESTKEY, 100, IPC_CREAT);
- if (id2 == -1)
- tst_resm(TFAIL | TERRNO, "shmget failed");
- else
- tst_resm(TINFO,
- "Cont2: Able to allocate shmem seg with "
- "the same key");
- write(p2[1], "notfnd", 7);
- } else
- write(p2[1], "exists", 7);
-
- tst_exit();
-}
+ TEST(shmget(TESTKEY, 100, 0));
+ if (TST_RET < 0) {
+ TEST(shmget(TESTKEY, 100, IPC_CREAT));
+
+ if (TST_RET < 0) {
+ tst_brk(TBROK, "shmget: %s", tst_strerrno(-TST_ERR));
+ } else {
+ tst_res(TINFO, "Cont2: Able to allocate shmem seg with "
+ "the same key");
+ }
+
+ SAFE_WRITE(1, p2[1], "notfnd", 7);
+ } else {
+ SAFE_WRITE(1, p2[1], "exists", 7);
+ }
-static void setup(void)
-{
- tst_require_root();
- check_newipc();
+ return 0;
}
-int main(int argc, char *argv[])
+static void run(void)
{
- int ret, use_clone = T_NONE;
- char *tsttype = NONESTR;
+ int use_clone = T_NONE;
char buf[7];
int id;
- setup();
-
- if (argc != 2) {
- tst_resm(TINFO, "Usage: %s <clone| unshare| none>", argv[0]);
- tst_resm(TINFO, " where clone, unshare, or fork specifies"
- " unshare method.");
- tst_exit();
- }
-
- /* Using PIPE's to sync between containers and Parent */
- SAFE_PIPE(NULL, p1);
- SAFE_PIPE(NULL, p2);
+ /* Using PIPE's to sync between container and Parent */
+ SAFE_PIPE(p1);
+ SAFE_PIPE(p2);
- if (strcmp(argv[1], "clone") == 0) {
+ if (!strcmp(str_op, "clone"))
use_clone = T_CLONE;
- tsttype = CLONESTR;
- } else if (strcmp(argv[1], "unshare") == 0) {
+ else if (!strcmp(str_op, "unshare"))
use_clone = T_UNSHARE;
- tsttype = UNSHARESTR;
- }
- tst_resm(TINFO, "Shared Memory namespace test : %s", tsttype);
+ tst_res(TINFO, "Shared Memory namespace test : %s", str_op);
/* Create 2 containers */
- ret =
- do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_shmem1, NULL);
- if (ret < 0)
- tst_brkm(TFAIL, NULL, "clone/unshare failed");
-
- ret =
- do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_shmem2, NULL);
- if (ret < 0)
- tst_brkm(TFAIL, NULL, "clone/unshare failed");
+ clone_unshare_test(use_clone, CLONE_NEWIPC, check_shmem1, NULL);
+ clone_unshare_test(use_clone, CLONE_NEWIPC, check_shmem2, NULL);
- close(p2[1]);
- read(p2[0], buf, 7);
+ SAFE_CLOSE(p2[1]);
+ SAFE_READ(1, p2[0], buf, 7);
if (strcmp(buf, "exists") == 0) {
- if (use_clone == T_NONE)
- tst_resm(TPASS,
- "Plain cloned process able to access shmem "
- "segment created");
- else
- tst_resm(TFAIL,
- "%s : In namespace2 found the shmem segment "
- "created in Namespace1", tsttype);
+ if (use_clone == T_NONE) {
+ tst_res(TPASS,
+ "Plain cloned process able to access shmem "
+ "segment created");
+ } else {
+ tst_res(TFAIL,
+ "%s : In namespace2 found the shmem segment "
+ "created in Namespace1",
+ str_op);
+ }
} else {
- if (use_clone == T_NONE)
- tst_resm(TFAIL,
- "Plain cloned process didn't find shmem seg");
- else
- tst_resm(TPASS,
- "%s : In namespace2 unable to access the shmem seg "
- "created in Namespace1", tsttype);
+ if (use_clone == T_NONE) {
+ tst_res(TFAIL,
+ "Plain cloned process didn't find shmem seg");
+ } else {
+ tst_res(TPASS,
+ "%s : In namespace2 unable to access the "
+ "shmem seg "
+ "created in Namespace1",
+ str_op);
+ }
}
- /* destroy the key */
+ /* destroy the key */
id = shmget(TESTKEY, 100, 0);
shmctl(id, IPC_RMID, NULL);
+}
- tst_exit();
+static void setup(void)
+{
+ check_newipc();
+
+ if (strcmp(str_op, "clone") && strcmp(str_op, "unshare") &&
+ strcmp(str_op, "none"))
+ tst_brk(TBROK, "Test execution mode <clone|unshare|none>");
}
+
+static struct tst_test test = {
+ .test_all = run,
+ .setup = setup,
+ .forks_child = 1,
+ .needs_root = 1,
+ .needs_checkpoints = 1,
+ .options =
+ (struct tst_option[]){
+ { "m:", &str_op,
+ "Test execution mode <clone|unshare|none>" },
+ {},
+ },
+};
--
2.35.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 13+ messages in thread* [LTP] [PATCH v1 9/9] Rewrite shmnstest.c using new LTP API
2022-02-08 10:09 [LTP] [PATCH v1 0/9] Rewrite sysvipc testing suite using new LTP API Andrea Cervesato
` (7 preceding siblings ...)
2022-02-08 10:09 ` [LTP] [PATCH v1 8/9] Rewrite shmem_2nstest.c " Andrea Cervesato
@ 2022-02-08 10:09 ` Andrea Cervesato
8 siblings, 0 replies; 13+ messages in thread
From: Andrea Cervesato @ 2022-02-08 10:09 UTC (permalink / raw)
To: ltp
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de>
---
.../kernel/containers/sysvipc/shmnstest.c | 185 ++++++++----------
1 file changed, 83 insertions(+), 102 deletions(-)
diff --git a/testcases/kernel/containers/sysvipc/shmnstest.c b/testcases/kernel/containers/sysvipc/shmnstest.c
index cf69cab21..55f76a0be 100644
--- a/testcases/kernel/containers/sysvipc/shmnstest.c
+++ b/testcases/kernel/containers/sysvipc/shmnstest.c
@@ -1,144 +1,125 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
-* 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
-*
-* Author: Serge Hallyn <serue@us.ibm.com>
-*
-* Create shm with key 0xEAEAEA
-* clone, clone(CLONE_NEWIPC), or unshare(CLONE_NEWIPC)
-* In cloned process, try to get the created shm
-
-***************************************************************************/
-
-#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>
+ * Copyright (c) International Business Machines Corp., 2007
+ * Serge Hallyn <serue@us.ibm.com>
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Create shm with key 0xEAEAEA and in cloned process try to get the shm.
+ */
+
+#define _GNU_SOURCE
+
#include <sys/ipc.h>
#include <sys/shm.h>
-#include "ipcns_helper.h"
-#include "test.h"
+#include <sys/wait.h>
+#include <sys/msg.h>
+#include <sys/types.h>
+#include "tst_test.h"
+#include "common.h"
-char *TCID = "sysvipc_namespace";
-int TST_TOTAL = 1;
#define TESTKEY 0xEAEAEA
-int p1[2];
-int p2[2];
+static char *str_op = "clone";
+
+static int p1[2];
+static int p2[2];
-int check_shmid(void *vtest)
+static int check_shmid(LTP_ATTRIBUTE_UNUSED void *vtest)
{
char buf[3];
int id;
- (void) vtest;
+ SAFE_CLOSE(p1[1]);
+ SAFE_CLOSE(p2[0]);
- close(p1[1]);
- close(p2[0]);
+ SAFE_READ(1, p1[0], buf, 3);
- read(p1[0], buf, 3);
id = shmget(TESTKEY, 100, 0);
- if (id == -1) {
- write(p2[1], "notfnd", 7);
+ if (id < 0) {
+ SAFE_WRITE(1, p2[1], "notfnd", 7);
} else {
- write(p2[1], "exists", 7);
+ SAFE_WRITE(1, p2[1], "exists", 7);
shmctl(id, IPC_RMID, NULL);
}
- tst_exit();
+ return 0;
}
-static void setup(void)
+static void run(void)
{
- tst_require_root();
- check_newipc();
-}
-
-#define UNSHARESTR "unshare"
-#define CLONESTR "clone"
-#define NONESTR "none"
-int main(int argc, char *argv[])
-{
- int r, use_clone = T_NONE;
+ int use_clone = T_NONE;
int id;
- char *tsttype = NONESTR;
char buf[7];
- setup();
+ /* Using PIPE's to sync between container and Parent */
+ SAFE_PIPE(p1);
+ SAFE_PIPE(p2);
- if (argc != 2) {
- tst_resm(TFAIL, "Usage: %s <clone|unshare|none>", argv[0]);
- tst_brkm(TFAIL,
- NULL,
- " where clone, unshare, or fork specifies unshare method.");
- }
- if (pipe(p1) == -1) {
- perror("pipe");
- exit(EXIT_FAILURE);
- }
- if (pipe(p2) == -1) {
- perror("pipe");
- exit(EXIT_FAILURE);
- }
- tsttype = NONESTR;
- if (strcmp(argv[1], "clone") == 0) {
+ if (!strcmp(str_op, "clone"))
use_clone = T_CLONE;
- tsttype = CLONESTR;
- } else if (strcmp(argv[1], "unshare") == 0) {
+ else if (!strcmp(str_op, "unshare"))
use_clone = T_UNSHARE;
- tsttype = UNSHARESTR;
- }
/* first create the key */
- id = shmget(TESTKEY, 100, IPC_CREAT);
- if (id == -1) {
- perror("shmget");
- tst_brkm(TFAIL, NULL, "shmget failed");
- }
+ TEST(shmget(TESTKEY, 100, IPC_CREAT));
+ if (TST_RET < 0)
+ tst_brk(TBROK, "shmget: %s", tst_strerrno(-TST_ERR));
+
+ id = (int)TST_RET;
+
+ tst_res(TINFO, "shmid namespaces test : %s", str_op);
- tst_resm(TINFO, "shmid namespaces test : %s", tsttype);
/* fire off the test */
- r = do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_shmid, NULL);
- if (r < 0) {
- tst_brkm(TFAIL, NULL, "%s failed", tsttype);
- }
+ clone_unshare_test(use_clone, CLONE_NEWIPC, check_shmid, NULL);
+
+ SAFE_CLOSE(p1[0]);
+ SAFE_CLOSE(p2[1]);
+
+ SAFE_WRITE(1, p1[1], "go", 3);
+ SAFE_READ(1, p2[0], buf, 7);
- close(p1[0]);
- close(p2[1]);
- write(p1[1], "go", 3);
- read(p2[0], buf, 7);
if (strcmp(buf, "exists") == 0) {
if (use_clone == T_NONE)
- tst_resm(TPASS, "plain cloned process found shmid");
+ tst_res(TPASS, "plain cloned process found shmid");
else
- tst_resm(TFAIL, "%s: child process found shmid",
- tsttype);
+ tst_res(TFAIL, "%s: child process found shmid", str_op);
} else {
- if (use_clone == T_NONE)
- tst_resm(TFAIL,
- "plain cloned process didn't find shmid");
- else
- tst_resm(TPASS, "%s: child process didn't find shmid",
- tsttype);
+ if (use_clone == T_NONE) {
+ tst_res(TFAIL,
+ "plain cloned process didn't find shmid");
+ } else {
+ tst_res(TPASS, "%s: child process didn't find shmid",
+ str_op);
+ }
}
/* destroy the key */
shmctl(id, IPC_RMID, NULL);
+}
- tst_exit();
+static void setup(void)
+{
+ check_newipc();
+
+ if (strcmp(str_op, "clone") && strcmp(str_op, "unshare") &&
+ strcmp(str_op, "none"))
+ tst_brk(TBROK, "Test execution mode <clone|unshare|none>");
}
+
+static struct tst_test test = {
+ .test_all = run,
+ .setup = setup,
+ .forks_child = 1,
+ .needs_root = 1,
+ .needs_checkpoints = 1,
+ .options =
+ (struct tst_option[]){
+ { "m:", &str_op,
+ "Test execution mode <clone|unshare|none>" },
+ {},
+ },
+};
--
2.35.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 13+ messages in thread