All of lore.kernel.org
 help / color / mirror / Atom feed
From: xuyang2018.jy@fujitsu.com <xuyang2018.jy@fujitsu.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH 2/5] syscalls/dup2/dup202: Convert dup202 to the new API
Date: Tue, 7 Sep 2021 09:14:12 +0000	[thread overview]
Message-ID: <61372D6C.5020003@fujitsu.com> (raw)
In-Reply-To: <20210902115849.72382-3-qi.fuli@fujitsu.com>

Hi Qi
> From: QI Fuli<qi.fuli@fujitsu.com>
>
> Signed-off-by: QI Fuli<qi.fuli@fujitsu.com>
> ---
>   testcases/kernel/syscalls/dup2/dup202.c | 175 +++++++-----------------
>   1 file changed, 48 insertions(+), 127 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/dup2/dup202.c b/testcases/kernel/syscalls/dup2/dup202.c
> index c87769fa9..16f8b2add 100644
> --- a/testcases/kernel/syscalls/dup2/dup202.c
> +++ b/testcases/kernel/syscalls/dup2/dup202.c
> @@ -1,65 +1,22 @@
> -/*
> - *
> - *   Copyright (c) International Business Machines  Corp., 2001
> - *
> - *   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
>
>   /*
> - * NAME
> - *	dup202.c
> + * Copyright (c) International Business Machines  Corp., 2001
>    *
> - * DESCRIPTION
> + * DESCRIPTION:
>    *	Is the access mode the same for both file descriptors?
>    *		0: read only ?	"0444"
>    *		1: write only ? "0222"
>    *		2: read/write ? "0666"
>    *
> - * ALGORITHM
> - *	Creat a file with each access mode; dup each file descriptor;
> - *	stat each file descriptor and compare modes of each pair
> - *
> - * USAGE:<for command-line>
> - *  dup202 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
> - *     where,  -c n : Run n copies concurrently.
> - *             -f   : Turn off functionality Testing.
> - *             -i n : Execute test n times.
> - *             -I x : Execute test for x seconds.
> - *             -P x : Pause for x seconds between iterations.
> - *             -t   : Turn on syscall timing.
> - *
> - * HISTORY
> + * HISTORY:
>    *	07/2001 Ported by Wayne Boyer
> - *
> - * RESTRICTIONS
> - *	None
>    */
>
> -#include<sys/types.h>
> -#include<sys/stat.h>
>   #include<errno.h>
> -#include<fcntl.h>
>   #include<stdio.h>
> -#include "test.h"
> -#include "safe_macros.h"
> -
> -char *TCID = "dup202";
> -int TST_TOTAL = 3;
> -
> -void setup(void);
> -void cleanup(void);
> +#include "tst_test.h"
> +#include "tst_safe_macros.h"
>
>   char testfile[40];
>   int fail;
> @@ -68,100 +25,64 @@ int newfd;
>   /* set these to a known index into our local file descriptor table */
>   int duprdo = 10, dupwro = 20, duprdwr = 30;
please use static prefix.
>
> -struct test_case_t {
> +static struct tcase {
>   	int *nfd;
>   	mode_t mode;
> -} TC[] = {
> +} tcases[]= {
>   	/* The first test creat(es) a file with mode 0444 */
These commets are useless because we have said it in the top commet.
> -	{
> -	&duprdo, (S_IRUSR | S_IRGRP | S_IROTH)},
> +	{&duprdo, (S_IRUSR | S_IRGRP | S_IROTH)},
>   	    /* The second test creat(es) a file with mode 0222 */
> -	{
> -	&dupwro, (S_IWUSR | S_IWGRP | S_IWOTH)},
> +	{&dupwro, (S_IWUSR | S_IWGRP | S_IWOTH)},
>   	    /* The third test creat(es) a file with mode 0666 */
> -	{
> -	&duprdwr,
> -		    (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH)}
> +	{&duprdwr, (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH)},
>   };
>
> -int main(int ac, char **av)
> +void setup(void)
>   {
> -	int lc;
> -	int i, ofd;
> -	struct stat oldbuf, newbuf;
> -
> -	tst_parse_opts(ac, av, NULL, NULL);
> -
> -	setup();
> -
> -	for (lc = 0; TEST_LOOPING(lc); lc++) {
> -
> -		tst_count = 0;
> -
> -		/* loop through the test cases */
> -		for (i = 0; i<  TST_TOTAL; i++) {
> -
> -			if ((ofd = creat(testfile, TC[i].mode)) == -1)
> -				tst_brkm(TBROK | TERRNO, cleanup,
> -					 "creat failed");
> -
> -			TEST(dup2(ofd, *TC[i].nfd));
> -
> -			if (TEST_RETURN == -1) {
> -				tst_resm(TFAIL | TTERRNO,
> -					 "call failed unexpectedly");
> -				continue;
> -			}
> +	(void)umask(0);
umask(0) is enough.
> +	sprintf(testfile, "dup202.%d", getpid());
> +}
>
> -			/* stat the original file */
> -			SAFE_FSTAT(cleanup, ofd,&oldbuf);
> +static void run(unsigned int i)
> +{
> +	int ofd;
> +	struct stat oldbuf, newbuf;
> +	struct tcase *tc = tcases + i;
>
> -			/* stat the duped file */
> -			SAFE_FSTAT(cleanup, *TC[i].nfd,&newbuf);
> +	ofd = creat(testfile, tc->mode);
> +	if (ofd == -1)
> +		tst_brk(TBROK | TERRNO, "creat failed");
>
> -			if (oldbuf.st_mode != newbuf.st_mode)
> -				tst_resm(TFAIL, "original and dup "
> -					 "modes do not match");
> -			else
> -				tst_resm(TPASS, "fstat shows new and "
> -					 "old modes are the same");
> +	TEST(dup2(ofd, *tc->nfd));
>
> -			/* remove the file so that we can use it again */
> -			if (close(*TC[i].nfd) == -1)
> -				perror("close failed");
> -			if (close(ofd) == -1)
> -				perror("close failed");
> -			if (unlink(testfile) == -1)
> -				perror("unlink failed");
> -		}
> +	if (TST_RET == -1) {
> +		tst_res(TFAIL | TTERRNO, "call failed unexpectedly");
> +		return;
>   	}
We can use SAFE_DUP2 here.

Best Regards
Yang Xu
>
> -	cleanup();
> -	tst_exit();
> -}
> -
> -/*
> - * setup() - performs all ONE TIME setup for this test.
> - */
> -void setup(void)
> -{
> -
> -	tst_sig(NOFORK, DEF_HANDLER, cleanup);
> +	/* stat the original file */
> +	SAFE_FSTAT(ofd,&oldbuf);
>
> -	TEST_PAUSE;
> +	/* stat the duped file */
> +	SAFE_FSTAT(*tc->nfd,&newbuf);
>
> -	tst_tmpdir();
> +	if (oldbuf.st_mode != newbuf.st_mode)
> +		tst_res(TFAIL, "original and dup modes do not match");
> +	else
> +		tst_res(TPASS, "fstat shows new and old modes are the same");
>
> -	(void)umask(0);
> -
> -	sprintf(testfile, "dup202.%d", getpid());
> +	/* remove the file so that we can use it again */
> +	if (close(*tc->nfd) == -1)
> +		perror("close failed");
> +	if (close(ofd) == -1)
> +		perror("close failed");
> +	if (unlink(testfile) == -1)
> +		perror("unlink failed");
>   }
>
> -/*
> - * cleanup() - performs all ONE TIME cleanup for this test at
> - *	       completion or premature exit.
> - */
> -void cleanup(void)
> -{
> -	tst_rmdir();
> -}
> +static struct tst_test test = {
> +	.needs_tmpdir = 1,
> +	.tcnt = ARRAY_SIZE(tcases),
> +	.test = run,
> +	.setup = setup,
> +};

WARNING: multiple messages have this Message-ID (diff)
From: "xuyang2018.jy@fujitsu.com" <xuyang2018.jy@fujitsu.com>
To: QI Fuli <fukuri.sai@gmail.com>
Cc: "qi.fuli@fujitsu.com" <qi.fuli@fujitsu.com>,
	"ltp@lists.linux.it" <ltp@lists.linux.it>
Subject: Re: [LTP] [PATCH 2/5] syscalls/dup2/dup202: Convert dup202 to the new API
Date: Tue, 7 Sep 2021 09:14:12 +0000	[thread overview]
Message-ID: <61372D6C.5020003@fujitsu.com> (raw)
Message-ID: <20210907091412.gCtBc7LJdQnP0LLyby1tWf1w0lluAih04_U1wxHL_lw@z> (raw)
In-Reply-To: <20210902115849.72382-3-qi.fuli@fujitsu.com>

Hi Qi
> From: QI Fuli<qi.fuli@fujitsu.com>
>
> Signed-off-by: QI Fuli<qi.fuli@fujitsu.com>
> ---
>   testcases/kernel/syscalls/dup2/dup202.c | 175 +++++++-----------------
>   1 file changed, 48 insertions(+), 127 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/dup2/dup202.c b/testcases/kernel/syscalls/dup2/dup202.c
> index c87769fa9..16f8b2add 100644
> --- a/testcases/kernel/syscalls/dup2/dup202.c
> +++ b/testcases/kernel/syscalls/dup2/dup202.c
> @@ -1,65 +1,22 @@
> -/*
> - *
> - *   Copyright (c) International Business Machines  Corp., 2001
> - *
> - *   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
>
>   /*
> - * NAME
> - *	dup202.c
> + * Copyright (c) International Business Machines  Corp., 2001
>    *
> - * DESCRIPTION
> + * DESCRIPTION:
>    *	Is the access mode the same for both file descriptors?
>    *		0: read only ?	"0444"
>    *		1: write only ? "0222"
>    *		2: read/write ? "0666"
>    *
> - * ALGORITHM
> - *	Creat a file with each access mode; dup each file descriptor;
> - *	stat each file descriptor and compare modes of each pair
> - *
> - * USAGE:<for command-line>
> - *  dup202 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
> - *     where,  -c n : Run n copies concurrently.
> - *             -f   : Turn off functionality Testing.
> - *             -i n : Execute test n times.
> - *             -I x : Execute test for x seconds.
> - *             -P x : Pause for x seconds between iterations.
> - *             -t   : Turn on syscall timing.
> - *
> - * HISTORY
> + * HISTORY:
>    *	07/2001 Ported by Wayne Boyer
> - *
> - * RESTRICTIONS
> - *	None
>    */
>
> -#include<sys/types.h>
> -#include<sys/stat.h>
>   #include<errno.h>
> -#include<fcntl.h>
>   #include<stdio.h>
> -#include "test.h"
> -#include "safe_macros.h"
> -
> -char *TCID = "dup202";
> -int TST_TOTAL = 3;
> -
> -void setup(void);
> -void cleanup(void);
> +#include "tst_test.h"
> +#include "tst_safe_macros.h"
>
>   char testfile[40];
>   int fail;
> @@ -68,100 +25,64 @@ int newfd;
>   /* set these to a known index into our local file descriptor table */
>   int duprdo = 10, dupwro = 20, duprdwr = 30;
please use static prefix.
>
> -struct test_case_t {
> +static struct tcase {
>   	int *nfd;
>   	mode_t mode;
> -} TC[] = {
> +} tcases[]= {
>   	/* The first test creat(es) a file with mode 0444 */
These commets are useless because we have said it in the top commet.
> -	{
> -	&duprdo, (S_IRUSR | S_IRGRP | S_IROTH)},
> +	{&duprdo, (S_IRUSR | S_IRGRP | S_IROTH)},
>   	    /* The second test creat(es) a file with mode 0222 */
> -	{
> -	&dupwro, (S_IWUSR | S_IWGRP | S_IWOTH)},
> +	{&dupwro, (S_IWUSR | S_IWGRP | S_IWOTH)},
>   	    /* The third test creat(es) a file with mode 0666 */
> -	{
> -	&duprdwr,
> -		    (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH)}
> +	{&duprdwr, (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH)},
>   };
>
> -int main(int ac, char **av)
> +void setup(void)
>   {
> -	int lc;
> -	int i, ofd;
> -	struct stat oldbuf, newbuf;
> -
> -	tst_parse_opts(ac, av, NULL, NULL);
> -
> -	setup();
> -
> -	for (lc = 0; TEST_LOOPING(lc); lc++) {
> -
> -		tst_count = 0;
> -
> -		/* loop through the test cases */
> -		for (i = 0; i<  TST_TOTAL; i++) {
> -
> -			if ((ofd = creat(testfile, TC[i].mode)) == -1)
> -				tst_brkm(TBROK | TERRNO, cleanup,
> -					 "creat failed");
> -
> -			TEST(dup2(ofd, *TC[i].nfd));
> -
> -			if (TEST_RETURN == -1) {
> -				tst_resm(TFAIL | TTERRNO,
> -					 "call failed unexpectedly");
> -				continue;
> -			}
> +	(void)umask(0);
umask(0) is enough.
> +	sprintf(testfile, "dup202.%d", getpid());
> +}
>
> -			/* stat the original file */
> -			SAFE_FSTAT(cleanup, ofd,&oldbuf);
> +static void run(unsigned int i)
> +{
> +	int ofd;
> +	struct stat oldbuf, newbuf;
> +	struct tcase *tc = tcases + i;
>
> -			/* stat the duped file */
> -			SAFE_FSTAT(cleanup, *TC[i].nfd,&newbuf);
> +	ofd = creat(testfile, tc->mode);
> +	if (ofd == -1)
> +		tst_brk(TBROK | TERRNO, "creat failed");
>
> -			if (oldbuf.st_mode != newbuf.st_mode)
> -				tst_resm(TFAIL, "original and dup "
> -					 "modes do not match");
> -			else
> -				tst_resm(TPASS, "fstat shows new and "
> -					 "old modes are the same");
> +	TEST(dup2(ofd, *tc->nfd));
>
> -			/* remove the file so that we can use it again */
> -			if (close(*TC[i].nfd) == -1)
> -				perror("close failed");
> -			if (close(ofd) == -1)
> -				perror("close failed");
> -			if (unlink(testfile) == -1)
> -				perror("unlink failed");
> -		}
> +	if (TST_RET == -1) {
> +		tst_res(TFAIL | TTERRNO, "call failed unexpectedly");
> +		return;
>   	}
We can use SAFE_DUP2 here.

Best Regards
Yang Xu
>
> -	cleanup();
> -	tst_exit();
> -}
> -
> -/*
> - * setup() - performs all ONE TIME setup for this test.
> - */
> -void setup(void)
> -{
> -
> -	tst_sig(NOFORK, DEF_HANDLER, cleanup);
> +	/* stat the original file */
> +	SAFE_FSTAT(ofd,&oldbuf);
>
> -	TEST_PAUSE;
> +	/* stat the duped file */
> +	SAFE_FSTAT(*tc->nfd,&newbuf);
>
> -	tst_tmpdir();
> +	if (oldbuf.st_mode != newbuf.st_mode)
> +		tst_res(TFAIL, "original and dup modes do not match");
> +	else
> +		tst_res(TPASS, "fstat shows new and old modes are the same");
>
> -	(void)umask(0);
> -
> -	sprintf(testfile, "dup202.%d", getpid());
> +	/* remove the file so that we can use it again */
> +	if (close(*tc->nfd) == -1)
> +		perror("close failed");
> +	if (close(ofd) == -1)
> +		perror("close failed");
> +	if (unlink(testfile) == -1)
> +		perror("unlink failed");
>   }
>
> -/*
> - * cleanup() - performs all ONE TIME cleanup for this test at
> - *	       completion or premature exit.
> - */
> -void cleanup(void)
> -{
> -	tst_rmdir();
> -}
> +static struct tst_test test = {
> +	.needs_tmpdir = 1,
> +	.tcnt = ARRAY_SIZE(tcases),
> +	.test = run,
> +	.setup = setup,
> +};

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  parent reply	other threads:[~2021-09-07  9:14 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-02 11:58 [LTP] [PATCH 0/5] Convert syscalls/dup2/dup2{01...05} to the new API QI Fuli
2021-09-02 11:58 ` [LTP] [PATCH 1/5] syscalls/dup2/dup201: Convert dup201 " QI Fuli
2021-09-06  9:29   ` Xiao Yang
2021-09-06  9:29     ` Xiao Yang
2021-09-07  0:42     ` qi.fuli
2021-09-07  0:42       ` qi.fuli
2021-09-07  8:28   ` xuyang2018.jy
2021-09-07  8:28     ` xuyang2018.jy
2021-09-02 11:58 ` [LTP] [PATCH 2/5] syscalls/dup2/dup202: Convert dup202 " QI Fuli
2021-09-07  1:46   ` Xiao Yang
2021-09-07  1:46     ` Xiao Yang
2021-09-07  9:14   ` xuyang2018.jy [this message]
2021-09-07  9:14     ` xuyang2018.jy
2021-09-02 11:58 ` [LTP] [PATCH 3/5] syscalls/dup2/dup203: Convert dup203 " QI Fuli
2021-09-07  9:27   ` xuyang2018.jy
2021-09-07  9:27     ` xuyang2018.jy
2021-09-02 11:58 ` [LTP] [PATCH 4/5] syscalls/dup2/dup204: Convert dup204 " QI Fuli
2021-09-07  9:36   ` xuyang2018.jy
2021-09-07  9:36     ` xuyang2018.jy
2021-09-02 11:58 ` [LTP] [PATCH 5/5] syscalls/dup2/dup205: Convert dup205 " QI Fuli
2021-09-07 10:06   ` xuyang2018.jy
2021-09-07 10:06     ` xuyang2018.jy

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=61372D6C.5020003@fujitsu.com \
    --to=xuyang2018.jy@fujitsu.com \
    --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.