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