1 /* Test of posix_spawn() function.
2 Copyright (C) 2008-2018 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 extern char **environ
;
43 #define CHILD_PROGRAM_FILENAME "test-posix_spawn3"
44 #define DATA_FILENAME "t!#$%&'()*+,-;=?@[\\]^_`{|}~.tmp"
45 /* On Cygwin, '*' '?' '\\' '|' cannot be used in file names. */
46 #if defined __CYGWIN__
48 # define DATA_FILENAME "t!#$%&'()+,-;=@[]^_`{}~.tmp"
55 char *argv
[3] = { CHILD_PROGRAM_FILENAME
, "-child", NULL
};
56 posix_spawn_file_actions_t actions
;
57 bool actions_allocated
;
63 /* Create a data file with specific contents. */
64 fp
= fopen (DATA_FILENAME
, "wb");
67 perror ("cannot create data file");
70 fwrite ("Halle Potta", 1, 11, fp
);
71 if (fflush (fp
) || fclose (fp
))
73 perror ("cannot prepare data file");
77 /* Avoid reading from our stdin, as it could block. */
78 if (freopen ("/dev/null", "rb", stdin
) == NULL
)
80 perror ("cannot redirect stdin");
84 /* Test whether posix_spawn_file_actions_addopen with this file name
85 actually works, by spawning a child that reads from this file. */
86 actions_allocated
= false;
87 if ((err
= posix_spawn_file_actions_init (&actions
)) != 0
88 || (actions_allocated
= true,
89 (err
= posix_spawn_file_actions_addopen (&actions
, STDIN_FILENO
, DATA_FILENAME
, O_RDONLY
, 0600)) != 0
90 || (err
= posix_spawn (&child
, CHILD_PROGRAM_FILENAME
, &actions
, NULL
, argv
, environ
)) != 0))
92 if (actions_allocated
)
93 posix_spawn_file_actions_destroy (&actions
);
95 perror ("subprocess failed");
98 posix_spawn_file_actions_destroy (&actions
);
100 while (waitpid (child
, &status
, 0) != child
)
102 if (!WIFEXITED (status
))
104 fprintf (stderr
, "subprocess terminated with unexpected wait status %d\n", status
);
107 exitstatus
= WEXITSTATUS (status
);
110 fprintf (stderr
, "subprocess terminated with unexpected exit status %d\n", exitstatus
);
121 /* See if reading from STDIN_FILENO yields the expected contents. */
122 if (fread (buf
, 1, sizeof (buf
), stdin
) == 11
123 && memcmp (buf
, "Halle Potta", 11) == 0)
130 cleanup_then_die (int sig
)
132 /* Clean up data file. */
133 unlink (DATA_FILENAME
);
135 /* Re-raise the signal and die from it. */
136 signal (sig
, SIG_DFL
);
141 main (int argc
, char *argv
[])
145 if (!(argc
> 1 && strcmp (argv
[1], "-child") == 0))
147 /* This is the parent process. */
148 signal (SIGINT
, cleanup_then_die
);
149 signal (SIGTERM
, cleanup_then_die
);
151 signal (SIGHUP
, cleanup_then_die
);
154 exitstatus
= parent_main ();
158 /* This is the child process. */
160 exitstatus
= child_main ();
162 unlink (DATA_FILENAME
);