From: Cyril Hrubis <chrubis@suse.cz>
To: Andrea Cervesato <andrea.cervesato@suse.de>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH v1 2/9] Rewrite mesgq_nstest.c using new LTP API
Date: Tue, 8 Mar 2022 15:42:50 +0100 [thread overview]
Message-ID: <Yidrapuv55RcgOXQ@yuki> (raw)
In-Reply-To: <20220208100948.22913-3-andrea.cervesato@suse.de>
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
next prev parent reply other threads:[~2022-03-08 14:40 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
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-03-08 14:42 ` Cyril Hrubis [this message]
2022-02-08 10:09 ` [LTP] [PATCH v1 3/9] Rewrite msg_comm.c " 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
2022-03-08 14:51 ` Cyril Hrubis
2022-02-08 10:09 ` [LTP] [PATCH v1 5/9] Rewrite sem_nstest.c " Andrea Cervesato
2022-02-08 10:09 ` [LTP] [PATCH v1 6/9] Rewrite semtest_2ns.c " Andrea Cervesato
2022-02-08 10:09 ` [LTP] [PATCH v1 7/9] Rewrite shm_comm.c " 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Yidrapuv55RcgOXQ@yuki \
--to=chrubis@suse.cz \
--cc=andrea.cervesato@suse.de \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is 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.