diff mbox series

[4/4] ipc/semget05: Convert into new api

Message ID 1679293822-19378-4-git-send-email-xuyang2018.jy@fujitsu.com
State Accepted
Headers show
Series [1/4] lapi/sem.h: Add SEMMSL macro | expand

Commit Message

Yang Xu March 20, 2023, 6:30 a.m. UTC
Use the same logic as shmget03 and msget02 to trigger ENOSPC error.

Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 testcases/kernel/syscalls/ipc/semget/Makefile |   5 +-
 .../kernel/syscalls/ipc/semget/semget05.c     | 187 ++++++------------
 2 files changed, 59 insertions(+), 133 deletions(-)

Comments

Li Wang April 13, 2023, 9:46 a.m. UTC | #1
Hi Xu,

Patch generally looks good, but I can't apply it to
main tree because of some code conflicting.

Can you help check the patch and resend another version?


On Mon, Mar 20, 2023 at 2:31 PM Yang Xu <xuyang2018.jy@fujitsu.com> wrote:

> Use the same logic as shmget03 and msget02 to trigger ENOSPC error.
>
> Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
> ---
>  testcases/kernel/syscalls/ipc/semget/Makefile |   5 +-
>  .../kernel/syscalls/ipc/semget/semget05.c     | 187 ++++++------------
>  2 files changed, 59 insertions(+), 133 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/ipc/semget/Makefile
> b/testcases/kernel/syscalls/ipc/semget/Makefile
> index 2f98c36ca..b1201281d 100644
> --- a/testcases/kernel/syscalls/ipc/semget/Makefile
> +++ b/testcases/kernel/syscalls/ipc/semget/Makefile
> @@ -3,11 +3,10 @@
>
>  top_srcdir              ?= ../../../../..
>
> -LTPLIBS = ltpipc ltpnewipc
> +LTPLIBS = ltpnewipc
>
>  include $(top_srcdir)/include/mk/testcases.mk
>
> -semget01 semget02: LTPLDLIBS = -lltpnewipc
> -semget05: LTPLDLIBS = -lltpipc
> +LTPLDLIBS = -lltpnewipc
>
>  include $(top_srcdir)/include/mk/generic_leaf_target.mk
> diff --git a/testcases/kernel/syscalls/ipc/semget/semget05.c
> b/testcases/kernel/syscalls/ipc/semget/semget05.c
> index f801cb8ed..38b0fad3c 100644
> --- a/testcases/kernel/syscalls/ipc/semget/semget05.c
> +++ b/testcases/kernel/syscalls/ipc/semget/semget05.c
> @@ -1,152 +1,79 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
>  /*
> - *
> - *   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
> + * Copyright (c) International Business Machines Corp., 2001
>   */
>
> -/*
> - * NAME
> - *     semget05.c
> +/*\
> + * [Description]
>   *
> - * DESCRIPTION
> - *     semget05 - test for ENOSPC error
> + * Test for ENOSPC error.
>   *
> - * ALGORITHM
> - *     create semaphore sets in a loop until the system limit is reached
> - *     loop if that option was specified
> - *     attempt to create yet another semaphore set
> - *     check the errno value
> - *       issue a PASS message if we get ENOSPC
> - *     otherwise, the tests fails
> - *       issue a FAIL message
> - *     call cleanup
> - *
> - * USAGE:  <for command-line>
> - * HISTORY
> - *     03/2001 - Written by Wayne Boyer
> - *      07/2006 - Changes By Michael Reed
> - *                - Changed the value of MAXIDS for the specific machine
> by reading
> - *                  the system limit for SEMMNI - The maximum number of
> sempahore sets
> - *      03/2008 - Matthieu Fertré  (mfertre@irisa.fr)
> - *                - Fix concurrency issue. Create private semaphores to
> - *                  avoid conflict with concurrent processes.
> - *
> - * RESTRICTIONS
> - *     none
> + * ENOSPC - a semaphore set exceed the maximum number of semaphore
> sets(SEMMNI)
>   */
>
> -#include "ipcsem.h"
> -
> -char *TCID = "semget05";
> -int TST_TOTAL = 1;
> -
> -/*
> - * The MAXIDS value is somewhat arbitrary and may need to be increased
> - * depending on the system being tested.
> - */
> -
> -int MAXIDS = 2048;
> -
> -int *sem_id_arr = NULL;
> -int num_sems = 0;              /* count the semaphores created */
> -
> -int main(int ac, char **av)
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <unistd.h>
> +#include <sys/types.h>
> +#include <sys/ipc.h>
> +#include "lapi/sem.h"
> +#include "tst_test.h"
> +#include "libnewipc.h"
> +#include "tst_safe_sysv_ipc.h"
> +
> +static int *sem_id_arr;
> +static int maxsems, array_cnt, used_cnt;
> +static key_t semkey;
> +
> +static void verify_semget(void)
>  {
> -       int lc;
> -       FILE *fp;
> -
> -       tst_parse_opts(ac, av, NULL, NULL);
> -
> -       /* Set the MAXIDS for the specific machine by reading the system
> limit
> -        * for SEMMNI - The maximum number of sempahore sets
> -        */
> -       fp = fopen("/proc/sys/kernel/sem", "r");
> -       if (fp != NULL) {
> -               int getmaxid;
> -               if (fscanf(fp, "%*d %*d %*d %d", &getmaxid) == 1)
> -                       MAXIDS = getmaxid + 1;
> -               fclose(fp);
> -       }
> -
> -       sem_id_arr = malloc(sizeof(int) * MAXIDS);
> -       if (sem_id_arr == NULL)
> -               tst_brkm(TBROK, cleanup, "malloc failed");
> -
> -       setup();
> -
> -       for (lc = 0; TEST_LOOPING(lc); lc++) {
> -               tst_count = 0;
> -
> -
> -               TEST(semget(IPC_PRIVATE, PSEMS, IPC_CREAT | IPC_EXCL |
> SEM_RA));
> -               if (TEST_RETURN != -1) {
> -                       tst_resm(TFAIL, "call succeeded when error
> expected");
> -                       continue;
> -               }
> -
> -               switch (TEST_ERRNO) {
> -               case ENOSPC:
> -                       tst_resm(TPASS, "expected failure - errno "
> -                                "= %d : %s", TEST_ERRNO,
> strerror(TEST_ERRNO));
> -                       break;
> -               default:
> -                       tst_resm(TFAIL, "unexpected error - %d : %s",
> -                                TEST_ERRNO, strerror(TEST_ERRNO));
> -                       break;
> -               }
> -       }
> -
> -       cleanup();
> -
> -       tst_exit();
> +       TST_EXP_FAIL2(semget(semkey + maxsems, PSEMS, IPC_CREAT | IPC_EXCL
> | SEM_RA),
> +               ENOSPC, "semget(%i, %i, %i)", semkey + maxsems, PSEMS,
> +               IPC_CREAT | IPC_EXCL | SEM_RA);
>  }
>
> -void setup(void)
> +static void setup(void)
>  {
> -       int sem_q;
> -
> -       tst_sig(NOFORK, DEF_HANDLER, cleanup);
> +       int res, num;
>
> -       TEST_PAUSE;
> +       semkey = GETIPCKEY();
> +       used_cnt = GET_USED_ARRAYS();
> +       tst_res(TINFO, "Current environment %d semaphore arrays are
> already in use",
> +               used_cnt);
> +       SAFE_FILE_SCANF("/proc/sys/kernel/sem", "%*d %*d %*d %d",
> &maxsems);
>
> -       tst_tmpdir();
> -
> -       while ((sem_q = semget(IPC_PRIVATE, PSEMS, IPC_CREAT | IPC_EXCL))
> != -1) {
> -               sem_id_arr[num_sems++] = sem_q;
> -               if (num_sems == MAXIDS) {
> -                       tst_brkm(TBROK, cleanup, "The maximum number of "
> -                                "semaphore ID's has been\n\t reached.
> Please "
> -                                "increase the MAXIDS value in the test.");
> -               }
> -       }
> +       sem_id_arr = SAFE_MALLOC((maxsems - used_cnt) * sizeof(int));
> +       for (num = 0; num < maxsems - used_cnt; num++) {
> +               res = semget(semkey + num, PSEMS, IPC_CREAT | IPC_EXCL |
> SEM_RA);
> +               if (res == -1)
> +                       tst_brk(TBROK | TERRNO, "semget failed
> unexpectedly");
>
> -       if (errno != ENOSPC) {
> -               tst_brkm(TBROK, cleanup, "Didn't get ENOSPC in test setup"
> -                        " - errno = %d : %s", errno, strerror(errno));
> +               sem_id_arr[array_cnt++] = res;
>         }
> +       tst_res(TINFO, "The maximum number of semaphore arrays (%d) has
> been reached",
> +               maxsems);
>  }
>
> -void cleanup(void)
> +static void cleanup(void)
>  {
> -       int i;
> +       int num;
>
> -       for (i = 0; i < num_sems; i++) {
> -               rm_sema(sem_id_arr[i]);
> -       }
> +       if (!sem_id_arr)
> +               return;
> +
> +       for (num = 0; num < array_cnt; num++)
> +               SAFE_SEMCTL(sem_id_arr[num], PSEMS, IPC_RMID);
>
>         free(sem_id_arr);
> -       tst_rmdir();
>  }
> +
> +static struct tst_test test = {
> +       .needs_tmpdir = 1,
> +       .setup = setup,
> +       .cleanup = cleanup,
> +       .test_all = verify_semget,
> +       .save_restore = (const struct tst_path_val[]){
> +               {"/proc/sys/kernel/sem", NULL, TST_SR_TCONF},
> +               {}
> +       }
> +};
> --
> 2.39.1
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
>
Yang Xu April 14, 2023, 5:25 a.m. UTC | #2
Hi Li

> Hi Xu,
> 
> Patch generally looks good, but I can't apply it to
> main tree because of some code conflicting.
> 

In fact, I don't know the reason.
I use my local patch, it can be merged.

> Can you help check the patch and resend another version?

I see garbled code for Matthieu Fertré  "é" character when I download 
this patch from patchwork by using mbox format.

Also for this patch, the Content-Transfer-Encoding: 8bit, the previous 
patch is Content-Transfer-Encoding: 7bit.

Also when I send patch, I can see a mention that
The following files are 8bit, but do not declare a 
Content-Transfer-Encoding.
     0001-ipc-semget05-Convert-into-new-api.patch
Which 8bit encoding should I declare [UTF-8]?

I have resend this patch, but I don't see it on patchwork.

Best Regards
Yang Xu
Yang Xu April 14, 2023, 6:47 a.m. UTC | #3
Hi LI

In fact, this patch still can not be merged after resend.

I search Content-Transfer-Encoding in the following url:

https://www.w3.org/Protocols/rfc1341/5_Content-Transfer-Encoding.html

Content-Transfer-Encoding: 7 bit means that the data is all represented 
as short lines of US-ASCII data

Content-Transfer-Encoding: 8 bit  means that the lines are short, but 
there may be non-ASCII characters (octets with the high-order bit set).


I seen these from the above url
"
As of the publication of this document, there are no standardized 
Internet transports for which it is legitimate to include unencoded 
8-bit or binary data in mail bodies. Thus there are no circumstances in 
which the "8bit" or "binary" Content-Transfer-Encoding is actually legal 
on the Internet. However, in the event that 8-bit or binary mail 
transport becomes a reality in Internet mail, or when this document is 
used in conjunction with any other 8-bit or binary-capable transport 
mechanism, 8-bit or binary bodies should be labeled as such using this 
mechanism."


I guess maybe I merged these 3/4 4/4 patch with your reviewed-by by 
myself? So we can avoid this problem. What do you think about it?

Best Regards
Yang Xu
> Use the same logic as shmget03 and msget03 to trigger ENOSPC error.
> 
> Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
> ---
>   testcases/kernel/syscalls/ipc/semget/Makefile |   5 +-
>   .../kernel/syscalls/ipc/semget/semget05.c     | 187 ++++++------------
>   2 files changed, 59 insertions(+), 133 deletions(-)
> 
> diff --git a/testcases/kernel/syscalls/ipc/semget/Makefile b/testcases/kernel/syscalls/ipc/semget/Makefile
> index 2f98c36ca..b1201281d 100644
> --- a/testcases/kernel/syscalls/ipc/semget/Makefile
> +++ b/testcases/kernel/syscalls/ipc/semget/Makefile
> @@ -3,11 +3,10 @@
>   
>   top_srcdir              ?= ../../../../..
>   
> -LTPLIBS = ltpipc ltpnewipc
> +LTPLIBS = ltpnewipc
>   
>   include $(top_srcdir)/include/mk/testcases.mk
>   
> -semget01 semget02: LTPLDLIBS = -lltpnewipc
> -semget05: LTPLDLIBS = -lltpipc
> +LTPLDLIBS = -lltpnewipc
>   
>   include $(top_srcdir)/include/mk/generic_leaf_target.mk
> diff --git a/testcases/kernel/syscalls/ipc/semget/semget05.c b/testcases/kernel/syscalls/ipc/semget/semget05.c
> index f801cb8ed..38b0fad3c 100644
> --- a/testcases/kernel/syscalls/ipc/semget/semget05.c
> +++ b/testcases/kernel/syscalls/ipc/semget/semget05.c
> @@ -1,152 +1,79 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
>   /*
> - *
> - *   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
> + * Copyright (c) International Business Machines Corp., 2001
>    */
>   
> -/*
> - * NAME
> - *	semget05.c
> +/*\
> + * [Description]
>    *
> - * DESCRIPTION
> - *	semget05 - test for ENOSPC error
> + * Test for ENOSPC error.
>    *
> - * ALGORITHM
> - *	create semaphore sets in a loop until the system limit is reached
> - *	loop if that option was specified
> - *	attempt to create yet another semaphore set
> - *	check the errno value
> - *	  issue a PASS message if we get ENOSPC
> - *	otherwise, the tests fails
> - *	  issue a FAIL message
> - *	call cleanup
> - *
> - * USAGE:  <for command-line>
> - * HISTORY
> - *	03/2001 - Written by Wayne Boyer
> - *      07/2006 - Changes By Michael Reed
> - *                - Changed the value of MAXIDS for the specific machine by reading
> - *                  the system limit for SEMMNI - The maximum number of sempahore sets
> - *      03/2008 - Matthieu Fertr�  (mfertre@irisa.fr)
> - *                - Fix concurrency issue. Create private semaphores to
> - *                  avoid conflict with concurrent processes.
> - *
> - * RESTRICTIONS
> - *	none
> + * ENOSPC - a semaphore set exceed the maximum number of semaphore sets(SEMMNI)
>    */
>   
> -#include "ipcsem.h"
> -
> -char *TCID = "semget05";
> -int TST_TOTAL = 1;
> -
> -/*
> - * The MAXIDS value is somewhat arbitrary and may need to be increased
> - * depending on the system being tested.
> - */
> -
> -int MAXIDS = 2048;
> -
> -int *sem_id_arr = NULL;
> -int num_sems = 0;		/* count the semaphores created */
> -
> -int main(int ac, char **av)
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <unistd.h>
> +#include <sys/types.h>
> +#include <sys/ipc.h>
> +#include "lapi/sem.h"
> +#include "tst_test.h"
> +#include "libnewipc.h"
> +#include "tst_safe_sysv_ipc.h"
> +
> +static int *sem_id_arr;
> +static int maxsems, array_cnt, used_cnt;
> +static key_t semkey;
> +
> +static void verify_semget(void)
>   {
> -	int lc;
> -	FILE *fp;
> -
> -	tst_parse_opts(ac, av, NULL, NULL);
> -
> -	/* Set the MAXIDS for the specific machine by reading the system limit
> -	 * for SEMMNI - The maximum number of sempahore sets
> -	 */
> -	fp = fopen("/proc/sys/kernel/sem", "r");
> -	if (fp != NULL) {
> -		int getmaxid;
> -		if (fscanf(fp, "%*d %*d %*d %d", &getmaxid) == 1)
> -			MAXIDS = getmaxid + 1;
> -		fclose(fp);
> -	}
> -
> -	sem_id_arr = malloc(sizeof(int) * MAXIDS);
> -	if (sem_id_arr == NULL)
> -		tst_brkm(TBROK, cleanup, "malloc failed");
> -
> -	setup();
> -
> -	for (lc = 0; TEST_LOOPING(lc); lc++) {
> -		tst_count = 0;
> -
> -
> -		TEST(semget(IPC_PRIVATE, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA));
> -		if (TEST_RETURN != -1) {
> -			tst_resm(TFAIL, "call succeeded when error expected");
> -			continue;
> -		}
> -
> -		switch (TEST_ERRNO) {
> -		case ENOSPC:
> -			tst_resm(TPASS, "expected failure - errno "
> -				 "= %d : %s", TEST_ERRNO, strerror(TEST_ERRNO));
> -			break;
> -		default:
> -			tst_resm(TFAIL, "unexpected error - %d : %s",
> -				 TEST_ERRNO, strerror(TEST_ERRNO));
> -			break;
> -		}
> -	}
> -
> -	cleanup();
> -
> -	tst_exit();
> +	TST_EXP_FAIL2(semget(semkey + maxsems, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA),
> +		ENOSPC, "semget(%i, %i, %i)", semkey + maxsems, PSEMS,
> +		IPC_CREAT | IPC_EXCL | SEM_RA);
>   }
>   
> -void setup(void)
> +static void setup(void)
>   {
> -	int sem_q;
> -
> -	tst_sig(NOFORK, DEF_HANDLER, cleanup);
> +	int res, num;
>   
> -	TEST_PAUSE;
> +	semkey = GETIPCKEY();
> +	used_cnt = GET_USED_ARRAYS();
> +	tst_res(TINFO, "Current environment %d semaphore arrays are already in use",
> +		used_cnt);
> +	SAFE_FILE_SCANF("/proc/sys/kernel/sem", "%*d %*d %*d %d", &maxsems);
>   
> -	tst_tmpdir();
> -
> -	while ((sem_q = semget(IPC_PRIVATE, PSEMS, IPC_CREAT | IPC_EXCL)) != -1) {
> -		sem_id_arr[num_sems++] = sem_q;
> -		if (num_sems == MAXIDS) {
> -			tst_brkm(TBROK, cleanup, "The maximum number of "
> -				 "semaphore ID's has been\n\t reached.  Please "
> -				 "increase the MAXIDS value in the test.");
> -		}
> -	}
> +	sem_id_arr = SAFE_MALLOC((maxsems - used_cnt) * sizeof(int));
> +	for (num = 0; num < maxsems - used_cnt; num++) {
> +		res = semget(semkey + num, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA);
> +		if (res == -1)
> +			tst_brk(TBROK | TERRNO, "semget failed unexpectedly");
>   
> -	if (errno != ENOSPC) {
> -		tst_brkm(TBROK, cleanup, "Didn't get ENOSPC in test setup"
> -			 " - errno = %d : %s", errno, strerror(errno));
> +		sem_id_arr[array_cnt++] = res;
>   	}
> +	tst_res(TINFO, "The maximum number of semaphore arrays (%d) has been reached",
> +		maxsems);
>   }
>   
> -void cleanup(void)
> +static void cleanup(void)
>   {
> -	int i;
> +	int num;
>   
> -	for (i = 0; i < num_sems; i++) {
> -		rm_sema(sem_id_arr[i]);
> -	}
> +	if (!sem_id_arr)
> +		return;
> +
> +	for (num = 0; num < array_cnt; num++)
> +		SAFE_SEMCTL(sem_id_arr[num], PSEMS, IPC_RMID);
>   
>   	free(sem_id_arr);
> -	tst_rmdir();
>   }
> +
> +static struct tst_test test = {
> +	.needs_tmpdir = 1,
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.test_all = verify_semget,
> +	.save_restore = (const struct tst_path_val[]){
> +		{"/proc/sys/kernel/sem", NULL, TST_SR_TCONF},
> +		{}
> +	}
> +};
Yang Xu April 17, 2023, 2:39 a.m. UTC | #4
HI Li

I merged this patch into my own ltp fork, so you can review this patch 
on my ltp fork.

https://github.com/xuyang0410/ltp/commits/master

Best Regards
Yang Xu

> Hi Li
> 
>> Hi Xu,
>>
>> Patch generally looks good, but I can't apply it to
>> main tree because of some code conflicting.
>>
> 
> In fact, I don't know the reason.
> I use my local patch, it can be merged.
> 
>> Can you help check the patch and resend another version?
> 
> I see garbled code for Matthieu Fertré  "é" character when I download
> this patch from patchwork by using mbox format.
> 
> Also for this patch, the Content-Transfer-Encoding: 8bit, the previous
> patch is Content-Transfer-Encoding: 7bit.
> 
> Also when I send patch, I can see a mention that
> The following files are 8bit, but do not declare a
> Content-Transfer-Encoding.
>       0001-ipc-semget05-Convert-into-new-api.patch
> Which 8bit encoding should I declare [UTF-8]?
> 
> I have resend this patch, but I don't see it on patchwork.
> 
> Best Regards
> Yang Xu
>
Li Wang April 17, 2023, 7:59 a.m. UTC | #5
On Mon, Apr 17, 2023 at 10:40 AM Yang Xu (Fujitsu) <
xuyang2018.jy@fujitsu.com> wrote:

> HI Li
>
> I merged this patch into my own ltp fork, so you can review this patch
> on my ltp fork.
>

Yes, the patch looks good, feel free add my reviewd-by and push.

Reviewed-by: Li Wang <liwang@redhat.com>
Yang Xu April 17, 2023, 8:16 a.m. UTC | #6
HI Li

> 
> 
> On Mon, Apr 17, 2023 at 10:40 AM Yang Xu (Fujitsu) 
> <xuyang2018.jy@fujitsu.com <mailto:xuyang2018.jy@fujitsu.com>> wrote:
> 
>     HI Li
> 
>     I merged this patch into my own ltp fork, so you can review this patch
>     on my ltp fork.
> 
> 
> Yes, the patch looks good, feel free add my reviewd-by and push.
> 
> Reviewed-by: Li Wang <liwang@redhat.com <mailto:liwang@redhat.com>>

Thanks, merged!

Best Regards
Yang Xu
> 
> 
> -- 
> Regards,
> Li Wang
diff mbox series

Patch

diff --git a/testcases/kernel/syscalls/ipc/semget/Makefile b/testcases/kernel/syscalls/ipc/semget/Makefile
index 2f98c36ca..b1201281d 100644
--- a/testcases/kernel/syscalls/ipc/semget/Makefile
+++ b/testcases/kernel/syscalls/ipc/semget/Makefile
@@ -3,11 +3,10 @@ 
 
 top_srcdir              ?= ../../../../..
 
-LTPLIBS = ltpipc ltpnewipc
+LTPLIBS = ltpnewipc
 
 include $(top_srcdir)/include/mk/testcases.mk
 
-semget01 semget02: LTPLDLIBS = -lltpnewipc
-semget05: LTPLDLIBS = -lltpipc
+LTPLDLIBS = -lltpnewipc
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/ipc/semget/semget05.c b/testcases/kernel/syscalls/ipc/semget/semget05.c
index f801cb8ed..38b0fad3c 100644
--- a/testcases/kernel/syscalls/ipc/semget/semget05.c
+++ b/testcases/kernel/syscalls/ipc/semget/semget05.c
@@ -1,152 +1,79 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *
- *   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
+ * Copyright (c) International Business Machines Corp., 2001
  */
 
-/*
- * NAME
- *	semget05.c
+/*\
+ * [Description]
  *
- * DESCRIPTION
- *	semget05 - test for ENOSPC error
+ * Test for ENOSPC error.
  *
- * ALGORITHM
- *	create semaphore sets in a loop until the system limit is reached
- *	loop if that option was specified
- *	attempt to create yet another semaphore set
- *	check the errno value
- *	  issue a PASS message if we get ENOSPC
- *	otherwise, the tests fails
- *	  issue a FAIL message
- *	call cleanup
- *
- * USAGE:  <for command-line>
- * HISTORY
- *	03/2001 - Written by Wayne Boyer
- *      07/2006 - Changes By Michael Reed
- *                - Changed the value of MAXIDS for the specific machine by reading
- *                  the system limit for SEMMNI - The maximum number of sempahore sets
- *      03/2008 - Matthieu Fertré  (mfertre@irisa.fr)
- *                - Fix concurrency issue. Create private semaphores to
- *                  avoid conflict with concurrent processes.
- *
- * RESTRICTIONS
- *	none
+ * ENOSPC - a semaphore set exceed the maximum number of semaphore sets(SEMMNI)
  */
 
-#include "ipcsem.h"
-
-char *TCID = "semget05";
-int TST_TOTAL = 1;
-
-/*
- * The MAXIDS value is somewhat arbitrary and may need to be increased
- * depending on the system being tested.
- */
-
-int MAXIDS = 2048;
-
-int *sem_id_arr = NULL;
-int num_sems = 0;		/* count the semaphores created */
-
-int main(int ac, char **av)
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include "lapi/sem.h"
+#include "tst_test.h"
+#include "libnewipc.h"
+#include "tst_safe_sysv_ipc.h"
+
+static int *sem_id_arr;
+static int maxsems, array_cnt, used_cnt;
+static key_t semkey;
+
+static void verify_semget(void)
 {
-	int lc;
-	FILE *fp;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	/* Set the MAXIDS for the specific machine by reading the system limit
-	 * for SEMMNI - The maximum number of sempahore sets
-	 */
-	fp = fopen("/proc/sys/kernel/sem", "r");
-	if (fp != NULL) {
-		int getmaxid;
-		if (fscanf(fp, "%*d %*d %*d %d", &getmaxid) == 1)
-			MAXIDS = getmaxid + 1;
-		fclose(fp);
-	}
-
-	sem_id_arr = malloc(sizeof(int) * MAXIDS);
-	if (sem_id_arr == NULL)
-		tst_brkm(TBROK, cleanup, "malloc failed");
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-
-		TEST(semget(IPC_PRIVATE, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA));
-		if (TEST_RETURN != -1) {
-			tst_resm(TFAIL, "call succeeded when error expected");
-			continue;
-		}
-
-		switch (TEST_ERRNO) {
-		case ENOSPC:
-			tst_resm(TPASS, "expected failure - errno "
-				 "= %d : %s", TEST_ERRNO, strerror(TEST_ERRNO));
-			break;
-		default:
-			tst_resm(TFAIL, "unexpected error - %d : %s",
-				 TEST_ERRNO, strerror(TEST_ERRNO));
-			break;
-		}
-	}
-
-	cleanup();
-
-	tst_exit();
+	TST_EXP_FAIL2(semget(semkey + maxsems, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA),
+		ENOSPC, "semget(%i, %i, %i)", semkey + maxsems, PSEMS,
+		IPC_CREAT | IPC_EXCL | SEM_RA);
 }
 
-void setup(void)
+static void setup(void)
 {
-	int sem_q;
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
+	int res, num;
 
-	TEST_PAUSE;
+	semkey = GETIPCKEY();
+	used_cnt = GET_USED_ARRAYS();
+	tst_res(TINFO, "Current environment %d semaphore arrays are already in use",
+		used_cnt);
+	SAFE_FILE_SCANF("/proc/sys/kernel/sem", "%*d %*d %*d %d", &maxsems);
 
-	tst_tmpdir();
-
-	while ((sem_q = semget(IPC_PRIVATE, PSEMS, IPC_CREAT | IPC_EXCL)) != -1) {
-		sem_id_arr[num_sems++] = sem_q;
-		if (num_sems == MAXIDS) {
-			tst_brkm(TBROK, cleanup, "The maximum number of "
-				 "semaphore ID's has been\n\t reached.  Please "
-				 "increase the MAXIDS value in the test.");
-		}
-	}
+	sem_id_arr = SAFE_MALLOC((maxsems - used_cnt) * sizeof(int));
+	for (num = 0; num < maxsems - used_cnt; num++) {
+		res = semget(semkey + num, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA);
+		if (res == -1)
+			tst_brk(TBROK | TERRNO, "semget failed unexpectedly");
 
-	if (errno != ENOSPC) {
-		tst_brkm(TBROK, cleanup, "Didn't get ENOSPC in test setup"
-			 " - errno = %d : %s", errno, strerror(errno));
+		sem_id_arr[array_cnt++] = res;
 	}
+	tst_res(TINFO, "The maximum number of semaphore arrays (%d) has been reached",
+		maxsems);
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
-	int i;
+	int num;
 
-	for (i = 0; i < num_sems; i++) {
-		rm_sema(sem_id_arr[i]);
-	}
+	if (!sem_id_arr)
+		return;
+
+	for (num = 0; num < array_cnt; num++)
+		SAFE_SEMCTL(sem_id_arr[num], PSEMS, IPC_RMID);
 
 	free(sem_id_arr);
-	tst_rmdir();
 }
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_semget,
+	.save_restore = (const struct tst_path_val[]){
+		{"/proc/sys/kernel/sem", NULL, TST_SR_TCONF},
+		{}
+	}
+};