]> Savannah Git Hosting - gnulib.git/commitdiff
posix_spawn tests: Add another test.
authorBruno Haible <bruno@clisp.org>
Wed, 23 Dec 2020 22:22:03 +0000 (23:22 +0100)
committerBruno Haible <bruno@clisp.org>
Wed, 23 Dec 2020 22:25:55 +0000 (23:25 +0100)
* tests/test-posix_spawn-open2.c: New file, based on
tests/test-posix_spawn-open1.c.
* modules/posix_spawn-tests (Files): Add it.
(Makefile.am): Compile and run test-posix_spawn-open1.

ChangeLog
modules/posix_spawn-tests
tests/test-posix_spawn-dup2-stdin.c
tests/test-posix_spawn-dup2-stdout.c
tests/test-posix_spawn-open1.c
tests/test-posix_spawn-open2.c [new file with mode: 0644]

index 8bf1adf9395271897c5f046f8ff36167e294b391..23b83e0fd7df068eeaa1192474796fd52af86995 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2020-12-23  Bruno Haible  <bruno@clisp.org>
+
+       posix_spawn tests: Add another test.
+       * tests/test-posix_spawn-open2.c: New file, based on
+       tests/test-posix_spawn-open1.c.
+       * modules/posix_spawn-tests (Files): Add it.
+       (Makefile.am): Compile and run test-posix_spawn-open1.
+
 2020-12-20  Bruno Haible  <bruno@clisp.org>
 
        Remove support for broken <wchar.h> in AIX 3.
index 4b20afa7ce8c5e0dd96a175ef1d52c5c05006bf8..f56ee6328efb88434a0a72e401b69b9dc65f3ba7 100644 (file)
@@ -1,5 +1,6 @@
 Files:
 tests/test-posix_spawn-open1.c
+tests/test-posix_spawn-open2.c
 tests/signature.h
 
 Depends-on:
@@ -23,6 +24,10 @@ AM_CONDITIONAL([POSIX_SPAWN_PORTED], [test $posix_spawn_ported = yes])
 
 Makefile.am:
 if POSIX_SPAWN_PORTED
-TESTS += test-posix_spawn-open1
-check_PROGRAMS += test-posix_spawn-open1
+TESTS += \
+  test-posix_spawn-open1 \
+  test-posix_spawn-open2
+check_PROGRAMS += \
+  test-posix_spawn-open1 \
+  test-posix_spawn-open2
 endif
index bec33fc1eaa5a4fa72da6cf363d8e00f4308be88..8af1e3fb8570f892b425ab8d332b8dc058688008 100644 (file)
@@ -1,4 +1,4 @@
-/* Test of posix_spawn() function.
+/* Test of posix_spawn() function: writing to a subprocess.
    Copyright (C) 2008-2020 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
index d639afacc7a16cee803fb912a877dfbff75fbe36..c67e143fb88c88b08093e644aa9f983b26fcb3fb 100644 (file)
@@ -1,4 +1,4 @@
-/* Test of posix_spawn() function.
+/* Test of posix_spawn() function: reading from a subprocess.
    Copyright (C) 2008-2020 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
index 31fee70ecd3385ee46917a35f6e28a487f8e1ffb..e08c1135e38eb7f08eb437129436aa6867122671 100644 (file)
@@ -1,4 +1,4 @@
-/* Test of posix_spawn() function.
+/* Test of posix_spawn() function with 'open' action.
    Copyright (C) 2008-2020 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
diff --git a/tests/test-posix_spawn-open2.c b/tests/test-posix_spawn-open2.c
new file mode 100644 (file)
index 0000000..51dc14b
--- /dev/null
@@ -0,0 +1,164 @@
+/* Test of posix_spawn() function with 'open' action and O_APPEND flag.
+   Copyright (C) 2008-2020 Free Software Foundation, Inc.
+
+   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 3 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, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2020.  */
+
+/* Test whether posix_spawn_file_actions_addopen supports the O_APPEND flag.  */
+
+#include <config.h>
+
+#include <spawn.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#define CHILD_PROGRAM_FILENAME "test-posix_spawn-open2"
+#define DATA_FILENAME "test-posix_spawn-open2-data.tmp"
+
+static int
+parent_main (void)
+{
+  FILE *fp;
+  char *argv[3] = { CHILD_PROGRAM_FILENAME, "-child", NULL };
+  posix_spawn_file_actions_t actions;
+  bool actions_allocated;
+  int err;
+  pid_t child;
+  int status;
+  int exitstatus;
+
+  /* Create a data file with specific contents.  */
+  fp = fopen (DATA_FILENAME, "wb");
+  if (fp == NULL)
+    {
+      perror ("cannot create data file");
+      return 1;
+    }
+  fwrite ("Halle ", 1, 6, fp);
+  if (fflush (fp) || fclose (fp))
+    {
+      perror ("cannot prepare data file");
+      return 1;
+    }
+
+  /* Test whether posix_spawn_file_actions_addopen with O_APPEND flag causes
+     the child to append to this file.  */
+  actions_allocated = false;
+  if ((err = posix_spawn_file_actions_init (&actions)) != 0
+      || (actions_allocated = true,
+          (err = posix_spawn_file_actions_addopen (&actions, STDOUT_FILENO, DATA_FILENAME, O_RDWR | O_APPEND, 0600)) != 0
+          || (err = posix_spawn (&child, CHILD_PROGRAM_FILENAME, &actions, NULL, argv, environ)) != 0))
+    {
+      if (actions_allocated)
+        posix_spawn_file_actions_destroy (&actions);
+      errno = err;
+      perror ("subprocess failed");
+      return 1;
+    }
+  posix_spawn_file_actions_destroy (&actions);
+  status = 0;
+  while (waitpid (child, &status, 0) != child)
+    ;
+  if (!WIFEXITED (status))
+    {
+      fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
+      return 1;
+    }
+  exitstatus = WEXITSTATUS (status);
+  if (exitstatus != 0)
+    {
+      fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
+      return 1;
+    }
+
+  /* Check the contents of the data file.  */
+  fp = fopen (DATA_FILENAME, "rb");
+  if (fp == NULL)
+    {
+      perror ("cannot open data file");
+      return 1;
+    }
+  char buf[1024];
+  int nread = fread (buf, 1, sizeof (buf), fp);
+  if (!(nread == 11 && memcmp (buf, "Halle Potta", 11) == 0))
+    {
+      fprintf (stderr, "data file wrong: has %d bytes, expected %d bytes\n", nread, 11);
+      return 1;
+    }
+  if (fclose (fp))
+    {
+      perror ("cannot close data file");
+      return 1;
+    }
+
+  /* Clean up data file.  */
+  unlink (DATA_FILENAME);
+
+  return 0;
+}
+
+static int
+child_main (void)
+{
+  /* Write to STDOUT_FILENO.  */
+  fwrite ("Potta", 1, 5, stdout);
+  /* No 'fflush (stdout);' is needed.  It is implicit when the child process
+     exits.  */
+
+  return 0;
+}
+
+static void
+cleanup_then_die (int sig)
+{
+  /* Clean up data file.  */
+  unlink (DATA_FILENAME);
+
+  /* Re-raise the signal and die from it.  */
+  signal (sig, SIG_DFL);
+  raise (sig);
+}
+
+int
+main (int argc, char *argv[])
+{
+  int exitstatus;
+
+  if (!(argc > 1 && strcmp (argv[1], "-child") == 0))
+    {
+      /* This is the parent process.  */
+      signal (SIGINT, cleanup_then_die);
+      signal (SIGTERM, cleanup_then_die);
+      #ifdef SIGHUP
+      signal (SIGHUP, cleanup_then_die);
+      #endif
+
+      exitstatus = parent_main ();
+    }
+  else
+    {
+      /* This is the child process.  */
+      exitstatus = child_main ();
+    }
+  return exitstatus;
+}