1 /* Test of posix_spawn() function with 'open' action.
2 Copyright (C) 2008-2021 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
19 /* Test whether posix_spawn_file_actions_addopen supports filename arguments
20 that contain special characters such as '*'. */
26 #include "signature.h"
27 SIGNATURE_CHECK (posix_spawn, int, (pid_t *, char const *,
28 posix_spawn_file_actions_t const *,
29 posix_spawnattr_t const *,
30 char *const[], char *const[]));
38 #include <sys/types.h>
41 #define CHILD_PROGRAM_FILENAME "test-posix_spawn-open1"
42 #define DATA_FILENAME "t!#$%&'()*+,-;=?@[\\]^_`{|}~.tmp"
43 /* On Windows (including Cygwin), '*' '?' '\\' '|' cannot be used in file
45 #if defined _WIN32 || defined __CYGWIN__
47 # define DATA_FILENAME "t!#$%&'()+,-;=@[]^_`{}~.tmp"
54 char *argv[3] = { CHILD_PROGRAM_FILENAME, "-child", NULL };
55 posix_spawn_file_actions_t actions;
56 bool actions_allocated;
62 /* Create a data file with specific contents. */
63 fp = fopen (DATA_FILENAME, "wb");
66 perror ("cannot create data file");
69 fwrite ("Halle Potta", 1, 11, fp);
70 if (fflush (fp) || fclose (fp))
72 perror ("cannot prepare data file");
76 /* Avoid reading from our stdin, as it could block. */
77 if (freopen ("/dev/null", "rb", stdin) == NULL)
79 perror ("cannot redirect stdin");
83 /* Test whether posix_spawn_file_actions_addopen with this file name
84 actually works, by spawning a child that reads from this file. */
85 actions_allocated = false;
86 if ((err = posix_spawn_file_actions_init (&actions)) != 0
87 || (actions_allocated = true,
88 (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, DATA_FILENAME, O_RDONLY, 0600)) != 0
89 || (err = posix_spawn (&child, CHILD_PROGRAM_FILENAME, &actions, NULL, argv, environ)) != 0))
91 if (actions_allocated)
92 posix_spawn_file_actions_destroy (&actions);
94 perror ("subprocess failed");
97 posix_spawn_file_actions_destroy (&actions);
99 while (waitpid (child, &status, 0) != child)
101 if (!WIFEXITED (status))
103 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
106 exitstatus = WEXITSTATUS (status);
109 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
120 /* See if reading from STDIN_FILENO yields the expected contents. */
121 if (fread (buf, 1, sizeof (buf), stdin) == 11
122 && memcmp (buf, "Halle Potta", 11) == 0)
129 cleanup_then_die (int sig)
131 /* Clean up data file. */
132 unlink (DATA_FILENAME);
134 /* Re-raise the signal and die from it. */
135 signal (sig, SIG_DFL);
140 main (int argc, char *argv[])
144 if (!(argc > 1 && strcmp (argv[1], "-child") == 0))
146 /* This is the parent process. */
147 signal (SIGINT, cleanup_then_die);
148 signal (SIGTERM, cleanup_then_die);
150 signal (SIGHUP, cleanup_then_die);
153 exitstatus = parent_main ();
157 /* This is the child process. */
159 exitstatus = child_main ();
161 unlink (DATA_FILENAME);