From 0153b9171b1b4592f27bd5ce664a3b390d5c17f6 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 21 Oct 2008 01:03:08 +0200 Subject: [PATCH] Tests for posix_spawn function. --- ChangeLog | 5 ++ modules/posix_spawn-tests | 16 +++++ tests/test-posix_spawn3.c | 160 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 181 insertions(+) create mode 100644 modules/posix_spawn-tests create mode 100644 tests/test-posix_spawn3.c diff --git a/ChangeLog b/ChangeLog index 2849469c4..54fc51c9c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2008-10-20 Bruno Haible + * modules/posix_spawn-tests: New file. + * tests/test-posix_spawn3.c: New file. + +2008-10-20 Bruno Haible + * modules/posix_spawnp-tests (Depends-on): Add sys_wait. * tests/test-posix_spawn1.c (WTERMSIG, WCOREDUMP, WEXITSTATUS, WIFSIGNALED, WIFEXITED, WIFSTOPPED): Remove fallback definitions. diff --git a/modules/posix_spawn-tests b/modules/posix_spawn-tests new file mode 100644 index 000000000..b34ea1d3d --- /dev/null +++ b/modules/posix_spawn-tests @@ -0,0 +1,16 @@ +Files: +tests/test-posix_spawn3.c + +Depends-on: +posix_spawn_file_actions_init +posix_spawn_file_actions_addopen +posix_spawn_file_actions_destroy +stdbool +unistd +sys_wait + +configure.ac: + +Makefile.am: +TESTS += test-posix_spawn3 +check_PROGRAMS += test-posix_spawn3 diff --git a/tests/test-posix_spawn3.c b/tests/test-posix_spawn3.c new file mode 100644 index 000000000..ec5d46826 --- /dev/null +++ b/tests/test-posix_spawn3.c @@ -0,0 +1,160 @@ +/* Test of posix_spawn() function. + Copyright (C) 2008 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 . */ + +/* Written by Bruno Haible , 2008. */ + +/* Test whether posix_spawn_file_actions_addopen supports filename arguments + that contain special characters such as '*'. */ + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern char **environ; + +#ifndef STDIN_FILENO +# define STDIN_FILENO 0 +#endif +#ifndef STDOUT_FILENO +# define STDOUT_FILENO 1 +#endif +#ifndef STDERR_FILENO +# define STDERR_FILENO 2 +#endif + +#define CHILD_PROGRAM_FILENAME "test-posix_spawn3" +#define DATA_FILENAME "t!#$%&'()*+,-;=?@[\\]^_`{|}~.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 Potta", 1, 11, fp); + if (fflush (fp) || fclose (fp)) + { + perror ("cannot prepare data file"); + return 1; + } + + /* Avoid reading from our stdin, as it could block. */ + freopen ("/dev/null", "rb", stdin); + + /* Test whether posix_spawn_file_actions_addopen with this file name + actually works, but spawning a child that reads from this file. */ + actions_allocated = false; + if ((err = posix_spawn_file_actions_init (&actions)) != 0 + || (actions_allocated = true, + (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, DATA_FILENAME, O_RDONLY, 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; + } + return 0; +} + +static int +child_main (void) +{ + char buf[1024]; + + /* See if reading from STDIN_FILENO yields the expected contents. */ + if (fread (buf, 1, sizeof (buf), stdin) == 11 + && memcmp (buf, "Halle Potta", 11) == 0) + return 0; + else + return 2; +} + +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 (); + } + unlink (DATA_FILENAME); + return exitstatus; +} -- 2.11.0