[1/3] posix: Adapt tst-spawn{2,3} to use libsupport.

Message ID 1494876985-21990-1-git-send-email-adhemerval.zanella@linaro.org
State Accepted
Commit 1a920d9c26bc2794a22604e94a988f27f8b556f8
Headers show
Series
  • [1/3] posix: Adapt tst-spawn{2,3} to use libsupport.
Related show

Commit Message

Adhemerval Zanella May 15, 2017, 7:36 p.m.
Checked on x86_64-linux-gnu.

	* posix/tst-spawn2.c (do_test): Use libsupport.
	* posix/tst-spawn3.c (do_test): Likewise.
---
 ChangeLog          |  5 ++++
 posix/tst-spawn2.c | 26 +++++++++-------
 posix/tst-spawn3.c | 88 ++++++++++++++++--------------------------------------
 3 files changed, 46 insertions(+), 73 deletions(-)

-- 
2.7.4

Comments

Adhemerval Zanella June 28, 2017, 12:47 p.m. | #1
Any objections about this patch? Otherwise I will push it in following hours.

On 15/05/2017 16:36, Adhemerval Zanella wrote:
> Checked on x86_64-linux-gnu.

> 

> 	* posix/tst-spawn2.c (do_test): Use libsupport.

> 	* posix/tst-spawn3.c (do_test): Likewise.

> ---

>  ChangeLog          |  5 ++++

>  posix/tst-spawn2.c | 26 +++++++++-------

>  posix/tst-spawn3.c | 88 ++++++++++++++++--------------------------------------

>  3 files changed, 46 insertions(+), 73 deletions(-)

> 

> diff --git a/posix/tst-spawn2.c b/posix/tst-spawn2.c

> index 73a37b6..3a2e041 100644

> --- a/posix/tst-spawn2.c

> +++ b/posix/tst-spawn2.c

> @@ -23,11 +23,12 @@

>  #include <string.h>

>  #include <unistd.h>

>  #include <sys/wait.h>

> -

>  #include <stdio.h>

>  

> +#include <support/check.h>

> +

>  int

> -posix_spawn_test (void)

> +do_test (void)

>  {

>    /* Check if posix_spawn correctly returns an error and an invalid pid

>       by trying to spawn an invalid binary.  */

> @@ -38,35 +39,40 @@ posix_spawn_test (void)

>  

>    int ret = posix_spawn (&pid, program, 0, 0, args, environ);

>    if (ret != ENOENT)

> -    error (EXIT_FAILURE, errno, "posix_spawn");

> +    {

> +      errno = ret;

> +      FAIL_EXIT1 ("posix_spawn: %m");

> +    }

>  

>    /* POSIX states the value returned on pid variable in case of an error

>       is not specified.  GLIBC will update the value iff the child

>       execution is successful.  */

>    if (pid != -1)

> -    error (EXIT_FAILURE, errno, "posix_spawn returned pid != -1");

> +    FAIL_EXIT1 ("posix_spawn returned pid != -1 (%i)", (int) pid);

>  

>    /* Check if no child is actually created.  */

>    ret = waitpid (-1, NULL, 0);

>    if (ret != -1 || errno != ECHILD)

> -    error (EXIT_FAILURE, errno, "waitpid");

> +    FAIL_EXIT1 ("waitpid: %m)");

>  

>    /* Same as before, but with posix_spawnp.  */

>    char *args2[] = { (char*) program, 0 };

>  

>    ret = posix_spawnp (&pid, args2[0], 0, 0, args2, environ);

>    if (ret != ENOENT)

> -    error (EXIT_FAILURE, errno, "posix_spawnp");

> +    {

> +      errno = ret;

> +      FAIL_EXIT1 ("posix_spawnp: %m");

> +    }

>  

>    if (pid != -1)

> -    error (EXIT_FAILURE, errno, "posix_spawnp returned pid != -1");

> +    FAIL_EXIT1 ("posix_spawnp returned pid != -1 (%i)", (int) pid);

>  

>    ret = waitpid (-1, NULL, 0);

>    if (ret != -1 || errno != ECHILD)

> -    error (EXIT_FAILURE, errno, "waitpid");

> +    FAIL_EXIT1 ("waitpid: %m)");

>  

>    return 0;

>  }

>  

> -#define TEST_FUNCTION  posix_spawn_test ()

> -#include "../test-skeleton.c"

> +#include <support/test-driver.c>

> diff --git a/posix/tst-spawn3.c b/posix/tst-spawn3.c

> index 8577b03..28a4872 100644

> --- a/posix/tst-spawn3.c

> +++ b/posix/tst-spawn3.c

> @@ -25,10 +25,11 @@

>  #include <unistd.h>

>  #include <sys/wait.h>

>  #include <sys/resource.h>

> +#include <fcntl.h>

> +#include <paths.h>

>  

> -static int do_test (void);

> -#define TEST_FUNCTION           do_test ()

> -#include <test-skeleton.c>

> +#include <support/check.h>

> +#include <support/temp_file.h>

>  

>  static int

>  do_test (void)

> @@ -47,25 +48,20 @@ do_test (void)

>  

>    struct rlimit rl;

>    int max_fd = 24;

> +  int ret;

>  

>    /* Set maximum number of file descriptor to a low value to avoid open

>       too many files in environments where RLIMIT_NOFILE is large and to

>       limit the array size to track the opened file descriptors.  */

>  

>    if (getrlimit (RLIMIT_NOFILE, &rl) == -1)

> -    {

> -      printf ("error: getrlimit RLIMIT_NOFILE failed");

> -      exit (EXIT_FAILURE);

> -    }

> +    FAIL_EXIT1 ("getrlimit (RLIMIT_NOFILE): %m");

>  

>    max_fd = (rl.rlim_cur < max_fd ? rl.rlim_cur : max_fd);

>    rl.rlim_cur = max_fd;

>  

>    if (setrlimit (RLIMIT_NOFILE, &rl) == 1)

> -    {

> -      printf ("error: setrlimit RLIMIT_NOFILE to %u failed", max_fd);

> -      exit (EXIT_FAILURE);

> -    }

> +    FAIL_EXIT1 ("setrlimit (RLIMIT_NOFILE): %m");

>  

>    /* Exhauste the file descriptor limit with temporary files.  */

>    int files[max_fd];

> @@ -76,11 +72,7 @@ do_test (void)

>        if (fd == -1)

>  	{

>  	  if (errno != EMFILE)

> -	    {

> -	      printf ("error: create_temp_file returned -1 with "

> -		      "errno != EMFILE\n");

> -	      exit (EXIT_FAILURE);

> -	    }

> +	    FAIL_EXIT1 ("create_temp_file: %m");

>  	  break;

>  	}

>        files[nfiles++] = fd;

> @@ -88,25 +80,16 @@ do_test (void)

>  

>    posix_spawn_file_actions_t a;

>    if (posix_spawn_file_actions_init (&a) != 0)

> -    {

> -      puts ("error: spawn_file_actions_init failed");

> -      exit (EXIT_FAILURE);

> -    }

> +    FAIL_EXIT1 ("posix_spawn_file_actions_init");

>  

>    /* Executes a /bin/sh echo $$ 2>&1 > /tmp/tst-spawn3.pid .  */

>    const char pidfile[] = "/tmp/tst-spawn3.pid";

>    if (posix_spawn_file_actions_addopen (&a, STDOUT_FILENO, pidfile, O_WRONLY |

>  					O_CREAT | O_TRUNC, 0644) != 0)

> -    {

> -      puts ("error: spawn_file_actions_addopen failed");

> -      exit (EXIT_FAILURE);

> -    }

> +    FAIL_EXIT1 ("posix_spawn_file_actions_addopen");

>  

>    if (posix_spawn_file_actions_adddup2 (&a, STDOUT_FILENO, STDERR_FILENO) != 0)

> -    {

> -      puts ("error: spawn_file_actions_addclose");

> -      exit (EXIT_FAILURE);

> -    }

> +    FAIL_EXIT1 ("posix_spawn_file_actions_adddup2");

>  

>    /* Since execve (called by posix_spawn) might require to open files to

>       actually execute the shell script, setup to close the temporary file

> @@ -114,54 +97,40 @@ do_test (void)

>    for (int i=0; i<nfiles; i++)

>      {

>        if (posix_spawn_file_actions_addclose (&a, files[i]))

> -	{

> -          printf ("error: posix_spawn_file_actions_addclose failed");

> -	  exit (EXIT_FAILURE);

> -	}

> +	FAIL_EXIT1 ("posix_spawn_file_actions_addclose");

>      }

>  

>    char *spawn_argv[] = { (char *) _PATH_BSHELL, (char *) "-c",

>  			 (char *) "echo $$", NULL };

>    pid_t pid;

> -  if (posix_spawn (&pid, _PATH_BSHELL, &a, NULL, spawn_argv, NULL) != 0)

> +  if ((ret = posix_spawn (&pid, _PATH_BSHELL, &a, NULL, spawn_argv, NULL))

> +       != 0)

>      {

> -      puts ("error: posix_spawn failed");

> -      exit (EXIT_FAILURE);

> +      errno = ret;

> +      FAIL_EXIT1 ("posix_spawn: %m");

>      }

>  

>    int status;

>    int err = waitpid (pid, &status, 0);

>    if (err != pid)

> -    {

> -      puts ("error: waitpid failed");

> -      exit (EXIT_FAILURE);

> -    }

> +    FAIL_EXIT1 ("waitpid: %m");

>  

>    /* Close the temporary files descriptor so it can check posix_spawn

>       output.  */

>    for (int i=0; i<nfiles; i++)

>      {

>        if (close (files[i]))

> -	{

> -	  printf ("error: close failed\n");

> -	  exit (EXIT_FAILURE);

> -	}

> +	FAIL_EXIT1 ("close: %m");

>      }

>  

>    int pidfd = open (pidfile, O_RDONLY);

>    if (pidfd == -1)

> -    {

> -      printf ("error: open pidfile failed\n");

> -      exit (EXIT_FAILURE);

> -    }

> +    FAIL_EXIT1 ("open: %m");

>  

>    char buf[64];

>    ssize_t n;

>    if ((n = read (pidfd, buf, sizeof (buf))) < 0)

> -    {

> -      printf ("error: read pidfile failed\n");

> -      exit (EXIT_FAILURE);

> -    }

> +    FAIL_EXIT1 ("read: %m");

>  

>    unlink (pidfile);

>  

> @@ -169,21 +138,14 @@ do_test (void)

>    char *endp;

>    long int rpid = strtol (buf, &endp, 10);

>    if (*endp != '\n')

> -    {

> -      printf ("error: didn't parse whole line: \"%s\"\n", buf);

> -      exit (EXIT_FAILURE);

> -    }

> +    FAIL_EXIT1 ("*endp != \'n\'");

>    if (endp == buf)

> -    {

> -      puts ("error: read empty line");

> -      exit (EXIT_FAILURE);

> -    }

> +    FAIL_EXIT1 ("read empty line");

>  

>    if (rpid != pid)

> -    {

> -      printf ("error: found \"%s\", expected PID %ld\n", buf, (long int) pid);

> -      exit (EXIT_FAILURE);

> -    }

> +    FAIL_EXIT1 ("found \"%s\", expected pid %ld\n", buf, (long int) pid);

>  

>    return 0;

>  }

> +

> +#include <support/test-driver.c>

>

Patch

diff --git a/posix/tst-spawn2.c b/posix/tst-spawn2.c
index 73a37b6..3a2e041 100644
--- a/posix/tst-spawn2.c
+++ b/posix/tst-spawn2.c
@@ -23,11 +23,12 @@ 
 #include <string.h>
 #include <unistd.h>
 #include <sys/wait.h>
-
 #include <stdio.h>
 
+#include <support/check.h>
+
 int
-posix_spawn_test (void)
+do_test (void)
 {
   /* Check if posix_spawn correctly returns an error and an invalid pid
      by trying to spawn an invalid binary.  */
@@ -38,35 +39,40 @@  posix_spawn_test (void)
 
   int ret = posix_spawn (&pid, program, 0, 0, args, environ);
   if (ret != ENOENT)
-    error (EXIT_FAILURE, errno, "posix_spawn");
+    {
+      errno = ret;
+      FAIL_EXIT1 ("posix_spawn: %m");
+    }
 
   /* POSIX states the value returned on pid variable in case of an error
      is not specified.  GLIBC will update the value iff the child
      execution is successful.  */
   if (pid != -1)
-    error (EXIT_FAILURE, errno, "posix_spawn returned pid != -1");
+    FAIL_EXIT1 ("posix_spawn returned pid != -1 (%i)", (int) pid);
 
   /* Check if no child is actually created.  */
   ret = waitpid (-1, NULL, 0);
   if (ret != -1 || errno != ECHILD)
-    error (EXIT_FAILURE, errno, "waitpid");
+    FAIL_EXIT1 ("waitpid: %m)");
 
   /* Same as before, but with posix_spawnp.  */
   char *args2[] = { (char*) program, 0 };
 
   ret = posix_spawnp (&pid, args2[0], 0, 0, args2, environ);
   if (ret != ENOENT)
-    error (EXIT_FAILURE, errno, "posix_spawnp");
+    {
+      errno = ret;
+      FAIL_EXIT1 ("posix_spawnp: %m");
+    }
 
   if (pid != -1)
-    error (EXIT_FAILURE, errno, "posix_spawnp returned pid != -1");
+    FAIL_EXIT1 ("posix_spawnp returned pid != -1 (%i)", (int) pid);
 
   ret = waitpid (-1, NULL, 0);
   if (ret != -1 || errno != ECHILD)
-    error (EXIT_FAILURE, errno, "waitpid");
+    FAIL_EXIT1 ("waitpid: %m)");
 
   return 0;
 }
 
-#define TEST_FUNCTION  posix_spawn_test ()
-#include "../test-skeleton.c"
+#include <support/test-driver.c>
diff --git a/posix/tst-spawn3.c b/posix/tst-spawn3.c
index 8577b03..28a4872 100644
--- a/posix/tst-spawn3.c
+++ b/posix/tst-spawn3.c
@@ -25,10 +25,11 @@ 
 #include <unistd.h>
 #include <sys/wait.h>
 #include <sys/resource.h>
+#include <fcntl.h>
+#include <paths.h>
 
-static int do_test (void);
-#define TEST_FUNCTION           do_test ()
-#include <test-skeleton.c>
+#include <support/check.h>
+#include <support/temp_file.h>
 
 static int
 do_test (void)
@@ -47,25 +48,20 @@  do_test (void)
 
   struct rlimit rl;
   int max_fd = 24;
+  int ret;
 
   /* Set maximum number of file descriptor to a low value to avoid open
      too many files in environments where RLIMIT_NOFILE is large and to
      limit the array size to track the opened file descriptors.  */
 
   if (getrlimit (RLIMIT_NOFILE, &rl) == -1)
-    {
-      printf ("error: getrlimit RLIMIT_NOFILE failed");
-      exit (EXIT_FAILURE);
-    }
+    FAIL_EXIT1 ("getrlimit (RLIMIT_NOFILE): %m");
 
   max_fd = (rl.rlim_cur < max_fd ? rl.rlim_cur : max_fd);
   rl.rlim_cur = max_fd;
 
   if (setrlimit (RLIMIT_NOFILE, &rl) == 1)
-    {
-      printf ("error: setrlimit RLIMIT_NOFILE to %u failed", max_fd);
-      exit (EXIT_FAILURE);
-    }
+    FAIL_EXIT1 ("setrlimit (RLIMIT_NOFILE): %m");
 
   /* Exhauste the file descriptor limit with temporary files.  */
   int files[max_fd];
@@ -76,11 +72,7 @@  do_test (void)
       if (fd == -1)
 	{
 	  if (errno != EMFILE)
-	    {
-	      printf ("error: create_temp_file returned -1 with "
-		      "errno != EMFILE\n");
-	      exit (EXIT_FAILURE);
-	    }
+	    FAIL_EXIT1 ("create_temp_file: %m");
 	  break;
 	}
       files[nfiles++] = fd;
@@ -88,25 +80,16 @@  do_test (void)
 
   posix_spawn_file_actions_t a;
   if (posix_spawn_file_actions_init (&a) != 0)
-    {
-      puts ("error: spawn_file_actions_init failed");
-      exit (EXIT_FAILURE);
-    }
+    FAIL_EXIT1 ("posix_spawn_file_actions_init");
 
   /* Executes a /bin/sh echo $$ 2>&1 > /tmp/tst-spawn3.pid .  */
   const char pidfile[] = "/tmp/tst-spawn3.pid";
   if (posix_spawn_file_actions_addopen (&a, STDOUT_FILENO, pidfile, O_WRONLY |
 					O_CREAT | O_TRUNC, 0644) != 0)
-    {
-      puts ("error: spawn_file_actions_addopen failed");
-      exit (EXIT_FAILURE);
-    }
+    FAIL_EXIT1 ("posix_spawn_file_actions_addopen");
 
   if (posix_spawn_file_actions_adddup2 (&a, STDOUT_FILENO, STDERR_FILENO) != 0)
-    {
-      puts ("error: spawn_file_actions_addclose");
-      exit (EXIT_FAILURE);
-    }
+    FAIL_EXIT1 ("posix_spawn_file_actions_adddup2");
 
   /* Since execve (called by posix_spawn) might require to open files to
      actually execute the shell script, setup to close the temporary file
@@ -114,54 +97,40 @@  do_test (void)
   for (int i=0; i<nfiles; i++)
     {
       if (posix_spawn_file_actions_addclose (&a, files[i]))
-	{
-          printf ("error: posix_spawn_file_actions_addclose failed");
-	  exit (EXIT_FAILURE);
-	}
+	FAIL_EXIT1 ("posix_spawn_file_actions_addclose");
     }
 
   char *spawn_argv[] = { (char *) _PATH_BSHELL, (char *) "-c",
 			 (char *) "echo $$", NULL };
   pid_t pid;
-  if (posix_spawn (&pid, _PATH_BSHELL, &a, NULL, spawn_argv, NULL) != 0)
+  if ((ret = posix_spawn (&pid, _PATH_BSHELL, &a, NULL, spawn_argv, NULL))
+       != 0)
     {
-      puts ("error: posix_spawn failed");
-      exit (EXIT_FAILURE);
+      errno = ret;
+      FAIL_EXIT1 ("posix_spawn: %m");
     }
 
   int status;
   int err = waitpid (pid, &status, 0);
   if (err != pid)
-    {
-      puts ("error: waitpid failed");
-      exit (EXIT_FAILURE);
-    }
+    FAIL_EXIT1 ("waitpid: %m");
 
   /* Close the temporary files descriptor so it can check posix_spawn
      output.  */
   for (int i=0; i<nfiles; i++)
     {
       if (close (files[i]))
-	{
-	  printf ("error: close failed\n");
-	  exit (EXIT_FAILURE);
-	}
+	FAIL_EXIT1 ("close: %m");
     }
 
   int pidfd = open (pidfile, O_RDONLY);
   if (pidfd == -1)
-    {
-      printf ("error: open pidfile failed\n");
-      exit (EXIT_FAILURE);
-    }
+    FAIL_EXIT1 ("open: %m");
 
   char buf[64];
   ssize_t n;
   if ((n = read (pidfd, buf, sizeof (buf))) < 0)
-    {
-      printf ("error: read pidfile failed\n");
-      exit (EXIT_FAILURE);
-    }
+    FAIL_EXIT1 ("read: %m");
 
   unlink (pidfile);
 
@@ -169,21 +138,14 @@  do_test (void)
   char *endp;
   long int rpid = strtol (buf, &endp, 10);
   if (*endp != '\n')
-    {
-      printf ("error: didn't parse whole line: \"%s\"\n", buf);
-      exit (EXIT_FAILURE);
-    }
+    FAIL_EXIT1 ("*endp != \'n\'");
   if (endp == buf)
-    {
-      puts ("error: read empty line");
-      exit (EXIT_FAILURE);
-    }
+    FAIL_EXIT1 ("read empty line");
 
   if (rpid != pid)
-    {
-      printf ("error: found \"%s\", expected PID %ld\n", buf, (long int) pid);
-      exit (EXIT_FAILURE);
-    }
+    FAIL_EXIT1 ("found \"%s\", expected pid %ld\n", buf, (long int) pid);
 
   return 0;
 }
+
+#include <support/test-driver.c>