Refactor wait4/wait402.c to the new API
PATCH 1/1 Refactor wait4/wait402.c to the new API
Not major changes in functionality. Man page says, *wait for process to change state, BSD style*. So i kept the _USE_BSD macro, even if test seems to work anyway without it.
--- testcases/kernel/syscalls/wait4/wait402.c | 97 ++++------------------- 1 file changed, 17 insertions(+), 80 deletions(-)
testcases/kernel/syscalls/wait4/wait402.c +17 -80
@@ -1,101 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * * Copyright (c) International Business Machines Corp., 2001 * Copyright (c) 2012 Cyril Hrubis <[email protected]> -* -* 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) 2023 Ioannis Bonatakis <[email protected]> */ -/* -* wait402 - check for ECHILD errno when using an illegal pid value -*/ - -#include "test.h" +/*\ +* [Description] +* +* Check for ECHILD errno when using an illegal pid value +*/ -#include <errno.h> +#include "tst_test.h" #define _USE_BSD -#include <sys/types.h> -#include <sys/resource.h> #include <sys/wait.h> -#include <stdlib.h> -#include <string.h> -char *TCID ="wait402" ; -int TST_TOTAL =1 ; - -static void cleanup (void ); -static void setup (void ); - -static long get_pid_max (void ) -{ -long pid_max; - -SAFE_FILE_SCANF (NULL ,"/proc/sys/kernel/pid_max" ,"%ld" , &pid_max); - -return pid_max; -} +static pid_t pid_max; -int main (int ac,char **av) +static void run (void ) { -int lc; -pid_t epid =get_pid_max () +1 ; -int status =1 ;struct rusage rusage; -tst_parse_opts (ac, av,NULL ,NULL ); - -setup (); - -for (lc =0 ;TEST_LOOPING (lc); lc++) { - tst_count =0 ; - -TEST (wait4 (epid, &status,0 , &rusage)); - -if (TEST_RETURN ==0 ) { -tst_brkm (TFAIL , cleanup, -"call failed to produce expected error - errno = %d - %s" , -TEST_ERRNO ,strerror (TEST_ERRNO )); - } - -switch (TEST_ERRNO ) { -case ECHILD : -tst_resm (TPASS , -"received expected failure - errno = %d - %s" , -TEST_ERRNO ,strerror (TEST_ERRNO )); -break ; -default : -tst_brkm (TFAIL , cleanup, -"call failed to produce expected " -"error - errno = %d - %s" ,TEST_ERRNO , -strerror (TEST_ERRNO )); - } - } - -cleanup (); -tst_exit (); +TST_EXP_FAIL2 (wait4 (pid_max, &status,0 , &rusage),ECHILD , +"wait4 fails with ECHILD" ); }static void setup (void ) { - -tst_sig (FORK ,DEF_HANDLER , cleanup); - -TEST_PAUSE ; +SAFE_FILE_SCANF ("/proc/sys/kernel/pid_max" ,"%d\n" , &pid_max); } -static void cleanup (void ) -{ -} +static struct tst_test test = { + .test_all = run, + .setup = setup, +};
Signature · 5 lines
2.42.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Ioannis,
Quoted 3 lines
Not major changes in functionality. Man page says, *wait for process to change state, BSD style*. So i kept the
_USE_BSDmacro, even if test seems to work anyway without it.
This is certainly wrong. _USE_BSD are glibc specific constants, which we should avoid to use. man wait4(2) states for wait4():
Since glibc 2.19:
_DEFAULT_SOURCE
glibc 2.19 and earlier:
_BSD_SOURCE
_DEFAULT_SOURCE IMHO means it works by default, thus we wouldn't need anything.
musl libc guards it with:
#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
But at least on Alpine (the most used musl based linux distro) also compiles without any definition => let's remove it.
Quoted 1 line
Signed-off-by ybonatakis <[email protected]>
Could you please use your real name?
Quoted 9 lines
--- testcases/kernel/syscalls/wait4/wait402.c | 97 ++++------------------- 1 file changed, 17 insertions(+), 80 deletions(-)testcases/kernel/syscalls/wait4/wait402.c +0
-0
testcases/kernel/syscalls/wait4/wait402.c line 5
Quoted 5 lines
testcases/kernel/syscalls/wait4/wait402.c +1
-0@@ -1,101 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * * Copyright (c) International Business Machines Corp., 2001 * Copyright (c) 2012 Cyril Hrubis <[email protected]>
nit: there should be a single space after *. Also I'd /*
* Copyright (c) International Business Machines Corp., 2001
* Copyright (c) 2012 Cyril Hrubis <[email protected]>
* Copyright (c) Linux Test Project, 2001-2015
* Copyright (c) 2023 Ioannis Bonatakis <[email protected]> */
Quoted 11 lines
testcases/kernel/syscalls/wait4/wait402.c +0
-11-* -* 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. -*
testcases/kernel/syscalls/wait4/wait402.c line 12
Quoted 10 lines
testcases/kernel/syscalls/wait4/wait402.c +5
-3-* 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) 2023 Ioannis Bonatakis <[email protected]> */ +/*\ +* [Description] +* +* Check for ECHILD errno when using an illegal pid value
nit: missing dot at the end. Also I reword it a bit:
* Check for ECHILD errno when call wait4(2) with an invalid pid value.
testcases/kernel/syscalls/wait4/wait402.c line 16
Quoted 5 lines
testcases/kernel/syscalls/wait4/wait402.c +2
-1+*/ -#include <errno.h> +#include "tst_test.h" #define _USE_BSD
Removed (see above).
Quoted 5 lines
testcases/kernel/syscalls/wait4/wait402.c +0
-4-#include <sys/types.h> -#include <sys/resource.h> #include <sys/wait.h> -#include <stdlib.h> -#include <string.h>
...
testcases/kernel/syscalls/wait4/wait402.c line 53
Quoted 7 lines
testcases/kernel/syscalls/wait4/wait402.c +2
-3+static pid_t pid_max; -int main (int ac,char **av) +static void run (void ) { -int lc; -pid_t epid =get_pid_max () +1 ;
NOTE: max PID + 1 is invalid.
testcases/kernel/syscalls/wait4/wait402.c
Quoted 2 lines
testcases/kernel/syscalls/wait4/wait402.c +0
-1-int status =1 ;
There is no need to initialize the variable, it will be set by wait4().
Quoted 23 lines
testcases/kernel/syscalls/wait4/wait402.c +0
-21struct rusage rusage; -tst_parse_opts (ac, av,NULL ,NULL ); - -setup (); - -for (lc =0 ;TEST_LOOPING (lc); lc++) { - tst_count =0 ; - -TEST (wait4 (epid, &status,0 , &rusage)); - -if (TEST_RETURN ==0 ) { -tst_brkm (TFAIL , cleanup, -"call failed to produce expected error - errno = %d - %s" , -TEST_ERRNO ,strerror (TEST_ERRNO )); - } - -switch (TEST_ERRNO ) { -case ECHILD : -tst_resm (TPASS , -"received expected failure - errno = %d - %s" , -TEST_ERRNO ,strerror (TEST_ERRNO )); -break ;
testcases/kernel/syscalls/wait4/wait402.c
Quoted 10 lines
testcases/kernel/syscalls/wait4/wait402.c +2
-7-default : -tst_brkm (TFAIL , cleanup, -"call failed to produce expected " -"error - errno = %d - %s" ,TEST_ERRNO , -strerror (TEST_ERRNO )); - } - } +TST_EXP_FAIL2 (wait4 (pid_max, &status,0 , &rusage),ECHILD , +"wait4 fails with ECHILD" );
TST_EXP_FAIL2(wait4(pid_max + 1, &status, 0, &rusage), ECHILD);
NOTE: use +1, otherwise it would not be test for invalid pid value.
Also no need for "fails with ECHILD".
testcases/kernel/syscalls/wait4/wait402.c line 38
Quoted 11 lines
}
static void setup(void)
{testcases/kernel/syscalls/wait4/wait402.c +5
-0+SAFE_FILE_SCANF ("/proc/sys/kernel/pid_max" ,"%d\n" , &pid_max); } +static struct tst_test test = { + .test_all = run, + .setup = setup, +};
In the end I fixed the issues and merged with the diff below. Thanks for your work.
Kind regards,
Petr
+++ testcases/kernel/syscalls/wait4/wait402.c
@@ -1,30 +1,28 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
- *
- * Copyright (c) International Business Machines Corp., 2001 - * Copyright (c) 2012 Cyril Hrubis <[email protected]> - * Copyright (c) 2023 Ioannis Bonatakis <[email protected]>
+ * Copyright (c) International Business Machines Corp., 2001
+ * Copyright (c) 2012 Cyril Hrubis <[email protected]>
+ * Copyright (c) Linux Test Project, 2001-2015
+ * Copyright (c) 2023 Ioannis Bonatakis <[email protected]> */
/*\
* [Description]
*
- * Check for ECHILD errno when using an illegal pid value
+ * Check for ECHILD errno when call wait4(2) with an illegal pid value. */
#include "tst_test.h"
-#define _USE_BSD
#include <sys/wait.h>
static pid_t pid_max;
static void run(void)
{
- int status = 1;
+ int status;
struct rusage rusage;
- TST_EXP_FAIL2(wait4(pid_max, &status, 0, &rusage), ECHILD,
- "wait4 fails with ECHILD");
+ TST_EXP_FAIL2(wait4(pid_max + 1, &status, 0, &rusage), ECHILD);
}
static void setup(void)
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp