bdc76f
commit c70271662aa53aee0c4794d480dca6d9ba3ccc3d
bdc76f
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
bdc76f
Date:   Wed Sep 19 11:12:05 2018 -0700
bdc76f
bdc76f
    Use libsupport for tst-spawn.c
bdc76f
    
bdc76f
    No function changes is done.  Checked on x86_64-linux-gnu.
bdc76f
    
bdc76f
            * posix/tst-spawn.c (do_prepare, handle_restart, do_test):
bdc76f
            Use libsupport.
bdc76f
bdc76f
diff --git a/posix/tst-spawn.c b/posix/tst-spawn.c
bdc76f
index 7c785c430c..41f1a65243 100644
bdc76f
--- a/posix/tst-spawn.c
bdc76f
+++ b/posix/tst-spawn.c
bdc76f
@@ -17,16 +17,20 @@
bdc76f
    License along with the GNU C Library; if not, see
bdc76f
    <http://www.gnu.org/licenses/>.  */
bdc76f
 
bdc76f
+#include <stdio.h>
bdc76f
+#include <getopt.h>
bdc76f
 #include <errno.h>
bdc76f
 #include <error.h>
bdc76f
 #include <fcntl.h>
bdc76f
 #include <spawn.h>
bdc76f
 #include <stdlib.h>
bdc76f
 #include <string.h>
bdc76f
-#include <wait.h>
bdc76f
 #include <sys/param.h>
bdc76f
+
bdc76f
 #include <support/check.h>
bdc76f
 #include <support/xunistd.h>
bdc76f
+#include <support/temp_file.h>
bdc76f
+#include <support/support.h>
bdc76f
 
bdc76f
 
bdc76f
 /* Nonzero if the program gets called via `exec'.  */
bdc76f
@@ -36,16 +40,6 @@ static int restart;
bdc76f
 #define CMDLINE_OPTIONS \
bdc76f
   { "restart", no_argument, &restart, 1 },
bdc76f
 
bdc76f
-/* Prototype for our test function.  */
bdc76f
-extern void do_prepare (int argc, char *argv[]);
bdc76f
-extern int do_test (int argc, char *argv[]);
bdc76f
-
bdc76f
-/* We have a preparation function.  */
bdc76f
-#define PREPARE do_prepare
bdc76f
-
bdc76f
-#include "../test-skeleton.c"
bdc76f
-
bdc76f
-
bdc76f
 /* Name of the temporary files.  */
bdc76f
 static char *name1;
bdc76f
 static char *name2;
bdc76f
@@ -63,19 +57,18 @@ static const char fd3string[] = "This file will be opened";
bdc76f
 
bdc76f
 
bdc76f
 /* We have a preparation function.  */
bdc76f
-void
bdc76f
+static void
bdc76f
 do_prepare (int argc, char *argv[])
bdc76f
 {
bdc76f
   /* We must not open any files in the restart case.  */
bdc76f
   if (restart)
bdc76f
     return;
bdc76f
 
bdc76f
-  temp_fd1 = create_temp_file ("spawn", &name1);
bdc76f
-  temp_fd2 = create_temp_file ("spawn", &name2);
bdc76f
-  temp_fd3 = create_temp_file ("spawn", &name3);
bdc76f
-  if (temp_fd1 < 0 || temp_fd2 < 0 || temp_fd3 < 0)
bdc76f
-    exit (1);
bdc76f
+  TEST_VERIFY_EXIT ((temp_fd1 = create_temp_file ("spawn", &name1)) != -1);
bdc76f
+  TEST_VERIFY_EXIT ((temp_fd2 = create_temp_file ("spawn", &name2)) != -1);
bdc76f
+  TEST_VERIFY_EXIT ((temp_fd3 = create_temp_file ("spawn", &name3)) != -1);
bdc76f
 }
bdc76f
+#define PREPARE do_prepare
bdc76f
 
bdc76f
 
bdc76f
 static int
bdc76f
@@ -95,64 +88,45 @@ handle_restart (const char *fd1s, const char *fd2s, const char *fd3s,
bdc76f
   fd4 = atol (fd4s);
bdc76f
 
bdc76f
   /* Sanity check.  */
bdc76f
-  if (fd1 == fd2)
bdc76f
-    error (EXIT_FAILURE, 0, "value of fd1 and fd2 is the same");
bdc76f
-  if (fd1 == fd3)
bdc76f
-    error (EXIT_FAILURE, 0, "value of fd1 and fd3 is the same");
bdc76f
-  if (fd1 == fd4)
bdc76f
-    error (EXIT_FAILURE, 0, "value of fd1 and fd4 is the same");
bdc76f
-  if (fd2 == fd3)
bdc76f
-    error (EXIT_FAILURE, 0, "value of fd2 and fd3 is the same");
bdc76f
-  if (fd2 == fd4)
bdc76f
-    error (EXIT_FAILURE, 0, "value of fd2 and fd4 is the same");
bdc76f
-  if (fd3 == fd4)
bdc76f
-    error (EXIT_FAILURE, 0, "value of fd3 and fd4 is the same");
bdc76f
+  TEST_VERIFY_EXIT (fd1 != fd2);
bdc76f
+  TEST_VERIFY_EXIT (fd1 != fd3);
bdc76f
+  TEST_VERIFY_EXIT (fd1 != fd4);
bdc76f
+  TEST_VERIFY_EXIT (fd2 != fd3);
bdc76f
+  TEST_VERIFY_EXIT (fd2 != fd4);
bdc76f
+  TEST_VERIFY_EXIT (fd3 != fd4);
bdc76f
 
bdc76f
   /* First the easy part: read from the file descriptor which is
bdc76f
      supposed to be open.  */
bdc76f
-  if (lseek (fd2, 0, SEEK_CUR) != strlen (fd2string))
bdc76f
-    error (EXIT_FAILURE, errno, "file 2 not in right position");
bdc76f
+  TEST_COMPARE (xlseek (fd2, 0, SEEK_CUR), strlen (fd2string));
bdc76f
   /* The duped descriptor must have the same position.  */
bdc76f
-  if (lseek (fd4, 0, SEEK_CUR) != strlen (fd2string))
bdc76f
-    error (EXIT_FAILURE, errno, "file 4 not in right position");
bdc76f
-  if (lseek (fd2, 0, SEEK_SET) != 0)
bdc76f
-    error (EXIT_FAILURE, 0, "cannot reset position in file 2");
bdc76f
-  if (lseek (fd4, 0, SEEK_CUR) != 0)
bdc76f
-    error (EXIT_FAILURE, errno, "file 4 not set back, too");
bdc76f
-  if (read (fd2, buf, sizeof buf) != strlen (fd2string))
bdc76f
-    error (EXIT_FAILURE, 0, "cannot read file 2");
bdc76f
-  if (memcmp (fd2string, buf, strlen (fd2string)) != 0)
bdc76f
-    error (EXIT_FAILURE, 0, "file 2 does not match");
bdc76f
+  TEST_COMPARE (xlseek (fd4, 0, SEEK_CUR), strlen (fd2string));
bdc76f
+  TEST_COMPARE (xlseek (fd2, 0, SEEK_SET), 0);
bdc76f
+  TEST_COMPARE (xlseek (fd4, 0, SEEK_CUR), 0);
bdc76f
+  TEST_COMPARE (read (fd2, buf, sizeof buf), strlen (fd2string));
bdc76f
+  TEST_COMPARE_BLOB (fd2string, strlen (fd2string), buf, strlen (fd2string));
bdc76f
 
bdc76f
   /* Now read from the third file.  */
bdc76f
-  if (read (fd3, buf, sizeof buf) != strlen (fd3string))
bdc76f
-    error (EXIT_FAILURE, 0, "cannot read file 3");
bdc76f
-  if (memcmp (fd3string, buf, strlen (fd3string)) != 0)
bdc76f
-    error (EXIT_FAILURE, 0, "file 3 does not match");
bdc76f
+  TEST_COMPARE (read (fd3, buf, sizeof buf), strlen (fd3string));
bdc76f
+  TEST_COMPARE_BLOB (fd3string, strlen (fd3string), buf, strlen (fd3string));
bdc76f
   /* Try to write to the file.  This should not be allowed.  */
bdc76f
-  if (write (fd3, "boo!", 4) != -1 || errno != EBADF)
bdc76f
-    error (EXIT_FAILURE, 0, "file 3 is writable");
bdc76f
+  TEST_COMPARE (write (fd3, "boo!", 4), -1);
bdc76f
+  TEST_COMPARE (errno, EBADF);
bdc76f
 
bdc76f
   /* Now try to read the first file.  First make sure it is not opened.  */
bdc76f
-  if (lseek (fd1, 0, SEEK_CUR) != (off_t) -1 || errno != EBADF)
bdc76f
-    error (EXIT_FAILURE, 0, "file 1 (%d) is not closed", fd1);
bdc76f
+  TEST_COMPARE (lseek (fd1, 0, SEEK_CUR), (off_t) -1);
bdc76f
+  TEST_COMPARE (errno, EBADF);
bdc76f
 
bdc76f
   /* Now open the file and read it.  */
bdc76f
-  fd1 = open (name, O_RDONLY);
bdc76f
-  if (fd1 == -1)
bdc76f
-    error (EXIT_FAILURE, errno,
bdc76f
-	   "cannot open first file \"%s\" for verification", name);
bdc76f
+  fd1 = xopen (name, O_RDONLY, 0600);
bdc76f
 
bdc76f
-  if (read (fd1, buf, sizeof buf) != strlen (fd1string))
bdc76f
-    error (EXIT_FAILURE, errno, "cannot read file 1");
bdc76f
-  if (memcmp (fd1string, buf, strlen (fd1string)) != 0)
bdc76f
-    error (EXIT_FAILURE, 0, "file 1 does not match");
bdc76f
+  TEST_COMPARE (read (fd1, buf, sizeof buf), strlen (fd1string));
bdc76f
+  TEST_COMPARE_BLOB (fd1string, strlen (fd1string), buf, strlen (fd1string));
bdc76f
 
bdc76f
   return 0;
bdc76f
 }
bdc76f
 
bdc76f
 
bdc76f
-int
bdc76f
+static int
bdc76f
 do_test (int argc, char *argv[])
bdc76f
 {
bdc76f
   pid_t pid;
bdc76f
@@ -181,7 +155,7 @@ do_test (int argc, char *argv[])
bdc76f
        + the name of the closed descriptor
bdc76f
   */
bdc76f
   if (argc != (restart ? 6 : 2) && argc != (restart ? 6 : 5))
bdc76f
-    error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc);
bdc76f
+    FAIL_EXIT1 ("wrong number of arguments (%d)", argc);
bdc76f
 
bdc76f
   if (restart)
bdc76f
     return handle_restart (argv[1], argv[2], argv[3], argv[4], argv[5]);
bdc76f
@@ -189,77 +163,73 @@ do_test (int argc, char *argv[])
bdc76f
   /* Prepare the test.  We are creating two files: one which file descriptor
bdc76f
      will be marked with FD_CLOEXEC, another which is not.  */
bdc76f
 
bdc76f
-   /* Write something in the files.  */
bdc76f
-   if (write (temp_fd1, fd1string, strlen (fd1string)) != strlen (fd1string))
bdc76f
-     error (EXIT_FAILURE, errno, "cannot write to first file");
bdc76f
-   if (write (temp_fd2, fd2string, strlen (fd2string)) != strlen (fd2string))
bdc76f
-     error (EXIT_FAILURE, errno, "cannot write to second file");
bdc76f
-   if (write (temp_fd3, fd3string, strlen (fd3string)) != strlen (fd3string))
bdc76f
-     error (EXIT_FAILURE, errno, "cannot write to third file");
bdc76f
-
bdc76f
-   /* Close the third file.  It'll be opened by `spawn'.  */
bdc76f
-   close (temp_fd3);
bdc76f
-
bdc76f
-   /* Tell `spawn' what to do.  */
bdc76f
-   if (posix_spawn_file_actions_init (&actions) != 0)
bdc76f
-     error (EXIT_FAILURE, errno, "posix_spawn_file_actions_init");
bdc76f
-   /* Close `temp_fd1'.  */
bdc76f
-   if (posix_spawn_file_actions_addclose (&actions, temp_fd1) != 0)
bdc76f
-     error (EXIT_FAILURE, errno, "posix_spawn_file_actions_addclose");
bdc76f
-   /* We want to open the third file.  */
bdc76f
-   name3_copy = strdup (name3);
bdc76f
-   if (name3_copy == NULL)
bdc76f
-     error (EXIT_FAILURE, errno, "strdup");
bdc76f
-   if (posix_spawn_file_actions_addopen (&actions, temp_fd3, name3_copy,
bdc76f
-					 O_RDONLY, 0666) != 0)
bdc76f
-     error (EXIT_FAILURE, errno, "posix_spawn_file_actions_addopen");
bdc76f
-   /* Overwrite the name to check that a copy has been made.  */
bdc76f
-   memset (name3_copy, 'X', strlen (name3_copy));
bdc76f
-
bdc76f
-   /* We dup the second descriptor.  */
bdc76f
-   fd4 = MAX (2, MAX (temp_fd1, MAX (temp_fd2, temp_fd3))) + 1;
bdc76f
-   if (posix_spawn_file_actions_adddup2 (&actions, temp_fd2, fd4) != 0)
bdc76f
-     error (EXIT_FAILURE, errno, "posix_spawn_file_actions_adddup2");
bdc76f
-
bdc76f
-   /* Now spawn the process.  */
bdc76f
-   snprintf (fd1name, sizeof fd1name, "%d", temp_fd1);
bdc76f
-   snprintf (fd2name, sizeof fd2name, "%d", temp_fd2);
bdc76f
-   snprintf (fd3name, sizeof fd3name, "%d", temp_fd3);
bdc76f
-   snprintf (fd4name, sizeof fd4name, "%d", fd4);
bdc76f
-
bdc76f
-   for (i = 0; i < (argc == (restart ? 6 : 5) ? 4 : 1); i++)
bdc76f
-     spargv[i] = argv[i + 1];
bdc76f
-   spargv[i++] = (char *) "--direct";
bdc76f
-   spargv[i++] = (char *) "--restart";
bdc76f
-   spargv[i++] = fd1name;
bdc76f
-   spargv[i++] = fd2name;
bdc76f
-   spargv[i++] = fd3name;
bdc76f
-   spargv[i++] = fd4name;
bdc76f
-   spargv[i++] = name1;
bdc76f
-   spargv[i] = NULL;
bdc76f
-
bdc76f
-   if (posix_spawn (&pid, argv[1], &actions, NULL, spargv, environ) != 0)
bdc76f
-     error (EXIT_FAILURE, errno, "posix_spawn");
bdc76f
-
bdc76f
-   /* Same test but with a NULL pid argument.  */
bdc76f
-   if (posix_spawn (NULL, argv[1], &actions, NULL, spargv, environ) != 0)
bdc76f
-     error (EXIT_FAILURE, errno, "posix_spawn");
bdc76f
-
bdc76f
-   /* Cleanup.  */
bdc76f
-   if (posix_spawn_file_actions_destroy (&actions) != 0)
bdc76f
-     error (EXIT_FAILURE, errno, "posix_spawn_file_actions_destroy");
bdc76f
-   free (name3_copy);
bdc76f
+  /* Write something in the files.  */
bdc76f
+  xwrite (temp_fd1, fd1string, strlen (fd1string));
bdc76f
+  xwrite (temp_fd2, fd2string, strlen (fd2string));
bdc76f
+  xwrite (temp_fd3, fd3string, strlen (fd3string));
bdc76f
+
bdc76f
+  /* Close the third file.  It'll be opened by `spawn'.  */
bdc76f
+  xclose (temp_fd3);
bdc76f
+
bdc76f
+  /* Tell `spawn' what to do.  */
bdc76f
+  TEST_COMPARE (posix_spawn_file_actions_init (&actions), 0);
bdc76f
+  /* Close `temp_fd1'.  */
bdc76f
+  TEST_COMPARE (posix_spawn_file_actions_addclose (&actions, temp_fd1), 0);
bdc76f
+  /* We want to open the third file.  */
bdc76f
+  name3_copy = xstrdup (name3);
bdc76f
+  TEST_COMPARE (posix_spawn_file_actions_addopen (&actions, temp_fd3,
bdc76f
+						  name3_copy,
bdc76f
+						  O_RDONLY, 0666),
bdc76f
+		0);
bdc76f
+  /* Overwrite the name to check that a copy has been made.  */
bdc76f
+  memset (name3_copy, 'X', strlen (name3_copy));
bdc76f
+
bdc76f
+  /* We dup the second descriptor.  */
bdc76f
+  fd4 = MAX (2, MAX (temp_fd1, MAX (temp_fd2, temp_fd3))) + 1;
bdc76f
+  TEST_COMPARE (posix_spawn_file_actions_adddup2 (&actions, temp_fd2, fd4),
bdc76f
+	        0);
bdc76f
+
bdc76f
+  /* Now spawn the process.  */
bdc76f
+  snprintf (fd1name, sizeof fd1name, "%d", temp_fd1);
bdc76f
+  snprintf (fd2name, sizeof fd2name, "%d", temp_fd2);
bdc76f
+  snprintf (fd3name, sizeof fd3name, "%d", temp_fd3);
bdc76f
+  snprintf (fd4name, sizeof fd4name, "%d", fd4);
bdc76f
+
bdc76f
+  for (i = 0; i < (argc == (restart ? 6 : 5) ? 4 : 1); i++)
bdc76f
+    spargv[i] = argv[i + 1];
bdc76f
+  spargv[i++] = (char *) "--direct";
bdc76f
+  spargv[i++] = (char *) "--restart";
bdc76f
+  spargv[i++] = fd1name;
bdc76f
+  spargv[i++] = fd2name;
bdc76f
+  spargv[i++] = fd3name;
bdc76f
+  spargv[i++] = fd4name;
bdc76f
+  spargv[i++] = name1;
bdc76f
+  spargv[i] = NULL;
bdc76f
+
bdc76f
+  TEST_COMPARE (posix_spawn (&pid, argv[1], &actions, NULL, spargv, environ),
bdc76f
+		0);
bdc76f
+
bdc76f
+  /* Same test but with a NULL pid argument.  */
bdc76f
+  TEST_COMPARE (posix_spawn (NULL, argv[1], &actions, NULL, spargv, environ),
bdc76f
+		0);
bdc76f
+
bdc76f
+  /* Cleanup.  */
bdc76f
+  TEST_COMPARE (posix_spawn_file_actions_destroy (&actions), 0);
bdc76f
+  free (name3_copy);
bdc76f
 
bdc76f
   /* Wait for the children.  */
bdc76f
-  TEST_VERIFY (xwaitpid (pid, &status, 0) == pid);
bdc76f
+  TEST_COMPARE (xwaitpid (pid, &status, 0), pid);
bdc76f
   TEST_VERIFY (WIFEXITED (status));
bdc76f
   TEST_VERIFY (!WIFSIGNALED (status));
bdc76f
-  TEST_VERIFY (WEXITSTATUS (status) == 0);
bdc76f
+  TEST_COMPARE (WEXITSTATUS (status), 0);
bdc76f
 
bdc76f
   xwaitpid (-1, &status, 0);
bdc76f
   TEST_VERIFY (WIFEXITED (status));
bdc76f
   TEST_VERIFY (!WIFSIGNALED (status));
bdc76f
-  TEST_VERIFY (WEXITSTATUS (status) == 0);
bdc76f
+  TEST_COMPARE (WEXITSTATUS (status), 0);
bdc76f
 
bdc76f
   return 0;
bdc76f
 }
bdc76f
+
bdc76f
+#define TEST_FUNCTION_ARGV do_test
bdc76f
+#include <support/test-driver.c>