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