1 # posix_spawn.m4 serial 4
2 dnl Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
7 dnl Tests whether the entire posix_spawn facility is available.
8 AC_DEFUN([gl_POSIX_SPAWN],
10 AC_REQUIRE([gl_POSIX_SPAWN_BODY])
13 AC_DEFUN([gl_POSIX_SPAWN_BODY],
15 AC_REQUIRE([gl_SPAWN_H_DEFAULTS])
16 AC_CHECK_FUNCS_ONCE([posix_spawn])
17 dnl Assume that when the main function exists, all the others are
18 dnl available as well.
19 dnl AC_CHECK_FUNCS_ONCE([posix_spawnp])
20 dnl AC_CHECK_FUNCS_ONCE([posix_spawn_file_actions_init])
21 dnl AC_CHECK_FUNCS_ONCE([posix_spawn_file_actions_addclose])
22 dnl AC_CHECK_FUNCS_ONCE([posix_spawn_file_actions_adddup2])
23 dnl AC_CHECK_FUNCS_ONCE([posix_spawn_file_actions_addopen])
24 dnl AC_CHECK_FUNCS_ONCE([posix_spawn_file_actions_destroy])
25 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_init])
26 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_getflags])
27 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_setflags])
28 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_getpgroup])
29 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_setpgroup])
30 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_getschedparam])
31 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_setschedparam])
32 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_getschedpolicy])
33 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_setschedpolicy])
34 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_getsigdefault])
35 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_setsigdefault])
36 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_getsigmask])
37 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_setsigmask])
38 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_destroy])
39 if test $ac_cv_func_posix_spawn = yes; then
41 case "$gl_cv_func_posix_spawn_works" in
43 *) REPLACE_POSIX_SPAWN=1 ;;
50 dnl Test whether posix_spawn actually works.
51 dnl posix_spawn on AIX 5.3..6.1 has two bugs:
52 dnl 1) When it fails to execute the program, the child process exits with
53 dnl exit() rather than _exit(), which causes the stdio buffers to be
54 dnl flushed. Reported by Rainer Tammer.
55 dnl 2) The posix_spawn_file_actions_addopen function does not support file
56 dnl names that contain a '*'.
57 dnl posix_spawn on AIX 5.3..6.1 has also a third bug: It does not work
58 dnl when POSIX threads are used. But we don't test against this bug here.
59 AC_DEFUN([gl_POSIX_SPAWN_WORKS],
61 AC_REQUIRE([AC_PROG_CC])
62 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
63 AC_CACHE_CHECK([whether posix_spawn works], [gl_cv_func_posix_spawn_works],
64 [if test $cross_compiling = no; then
65 AC_LINK_IFELSE([AC_LANG_SOURCE([[
75 #include <sys/types.h>
78 extern char **environ;
81 # define STDIN_FILENO 0
84 # define STDOUT_FILENO 1
87 # define STDERR_FILENO 2
91 # define WTERMSIG(x) ((x) & 0x7f)
94 # define WIFEXITED(x) (WTERMSIG (x) == 0)
97 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
100 #define CHILD_PROGRAM_FILENAME "/non/exist/ent"
105 if (0 <= fd && fd <= 2)
107 int f = fd_safer (dup (fd));
120 char *argv[2] = { CHILD_PROGRAM_FILENAME, NULL };
122 sigset_t blocked_signals;
123 sigset_t fatal_signal_set;
124 posix_spawn_file_actions_t actions;
125 bool actions_allocated;
126 posix_spawnattr_t attrs;
127 bool attrs_allocated;
133 setvbuf (stdout, NULL, _IOFBF, 0);
134 puts ("This should be seen only once.");
135 if (pipe (ofd) < 0 || (ofd[1] = fd_safer (ofd[1])) < 0)
137 perror ("cannot create pipe");
140 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
141 sigemptyset (&fatal_signal_set);
142 sigaddset (&fatal_signal_set, SIGINT);
143 sigaddset (&fatal_signal_set, SIGTERM);
144 sigaddset (&fatal_signal_set, SIGHUP);
145 sigaddset (&fatal_signal_set, SIGPIPE);
146 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
147 actions_allocated = false;
148 attrs_allocated = false;
149 if ((err = posix_spawn_file_actions_init (&actions)) != 0
150 || (actions_allocated = true,
151 (err = posix_spawn_file_actions_adddup2 (&actions, ofd[0], STDIN_FILENO)) != 0
152 || (err = posix_spawn_file_actions_addclose (&actions, ofd[0])) != 0
153 || (err = posix_spawn_file_actions_addclose (&actions, ofd[1])) != 0
154 || (err = posix_spawnattr_init (&attrs)) != 0
155 || (attrs_allocated = true,
156 (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
157 || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0)
158 || (err = posix_spawnp (&child, CHILD_PROGRAM_FILENAME, &actions, &attrs, argv, environ)) != 0))
160 if (actions_allocated)
161 posix_spawn_file_actions_destroy (&actions);
163 posix_spawnattr_destroy (&attrs);
164 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
170 perror ("subprocess failed");
174 posix_spawn_file_actions_destroy (&actions);
175 posix_spawnattr_destroy (&attrs);
176 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
180 while (waitpid (child, &status, 0) != child)
182 if (!WIFEXITED (status))
184 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
187 exitstatus = WEXITSTATUS (status);
188 if (exitstatus != 127)
190 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
196 [if test -s conftest$ac_exeext \
197 && ./conftest$ac_exeext > conftest.out \
198 && echo 'This should be seen only once.' > conftest.ok \
199 && cmp conftest.out conftest.ok > /dev/null; then
200 gl_cv_func_posix_spawn_works=yes
202 gl_cv_func_posix_spawn_works=no
204 [gl_cv_func_posix_spawn_works=no])
205 if test $gl_cv_func_posix_spawn_works = yes; then
206 AC_RUN_IFELSE([AC_LANG_SOURCE([[
207 /* Test whether posix_spawn_file_actions_addopen supports filename arguments
208 that contain special characters such as '*'. */
218 #include <sys/types.h>
219 #include <sys/wait.h>
221 extern char **environ;
224 # define STDIN_FILENO 0
226 #ifndef STDOUT_FILENO
227 # define STDOUT_FILENO 1
229 #ifndef STDERR_FILENO
230 # define STDERR_FILENO 2
234 # define WTERMSIG(x) ((x) & 0x7f)
237 # define WIFEXITED(x) (WTERMSIG (x) == 0)
240 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
243 #define CHILD_PROGRAM_FILENAME "conftest"
244 #define DATA_FILENAME "conftest%=*#?"
250 char *argv[3] = { CHILD_PROGRAM_FILENAME, "-child", NULL };
251 posix_spawn_file_actions_t actions;
252 bool actions_allocated;
258 /* Create a data file with specific contents. */
259 fp = fopen (DATA_FILENAME, "wb");
262 perror ("cannot create data file");
265 fwrite ("Halle Potta", 1, 11, fp);
266 if (fflush (fp) || fclose (fp))
268 perror ("cannot prepare data file");
272 /* Avoid reading from our stdin, as it could block. */
273 freopen ("/dev/null", "rb", stdin);
275 /* Test whether posix_spawn_file_actions_addopen with this file name
276 actually works, but spawning a child that reads from this file. */
277 actions_allocated = false;
278 if ((err = posix_spawn_file_actions_init (&actions)) != 0
279 || (actions_allocated = true,
280 (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, DATA_FILENAME, O_RDONLY, 0600)) != 0
281 || (err = posix_spawn (&child, CHILD_PROGRAM_FILENAME, &actions, NULL, argv, environ)) != 0))
283 if (actions_allocated)
284 posix_spawn_file_actions_destroy (&actions);
286 perror ("subprocess failed");
289 posix_spawn_file_actions_destroy (&actions);
291 while (waitpid (child, &status, 0) != child)
293 if (!WIFEXITED (status))
295 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
298 exitstatus = WEXITSTATUS (status);
301 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
312 /* See if reading from STDIN_FILENO yields the expected contents. */
313 if (fread (buf, 1, sizeof (buf), stdin) == 11
314 && memcmp (buf, "Halle Potta", 11) == 0)
321 cleanup_then_die (int sig)
323 /* Clean up data file. */
324 unlink (DATA_FILENAME);
326 /* Re-raise the signal and die from it. */
327 signal (sig, SIG_DFL);
332 main (int argc, char *argv[])
336 if (!(argc > 1 && strcmp (argv[1], "-child") == 0))
338 /* This is the parent process. */
339 signal (SIGINT, cleanup_then_die);
340 signal (SIGTERM, cleanup_then_die);
342 signal (SIGHUP, cleanup_then_die);
345 exitstatus = parent_main ();
349 /* This is the child process. */
351 exitstatus = child_main ();
353 unlink (DATA_FILENAME);
358 [gl_cv_func_posix_spawn_works=no])
362 aix*) gl_cv_func_posix_spawn_works="guessing no";;
363 *) gl_cv_func_posix_spawn_works="guessing yes";;
369 AC_DEFUN([gl_POSIX_SPAWN_INTERNAL],
372 dnl Prerequisites of lib/spawni.c.
373 AC_CHECK_HEADERS([paths.h])
374 AC_CHECK_FUNCS([confstr sched_setparam sched_setscheduler setegid seteuid vfork])