1 # posix_spawn.m4 serial 5
2 dnl Copyright (C) 2008-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,
18 dnl except posix_spawnattr_{get,set}sched*, are 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_getsigdefault])
31 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_setsigdefault])
32 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_getsigmask])
33 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_setsigmask])
34 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_destroy])
35 if test $ac_cv_func_posix_spawn = yes; then
37 case "$gl_cv_func_posix_spawn_works" in
39 dnl Assume that these functions are available if POSIX_SPAWN_SETSCHEDULER
40 dnl evaluates to nonzero.
41 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_getschedpolicy])
42 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_setschedpolicy])
43 AC_CACHE_CHECK([whether posix_spawnattr_setschedpolicy is supported],
44 [gl_cv_func_spawnattr_setschedpolicy],
45 [AC_EGREP_CPP([POSIX scheduling supported], [
47 #if POSIX_SPAWN_SETSCHEDULER
48 POSIX scheduling supported
51 [gl_cv_func_spawnattr_setschedpolicy=yes],
52 [gl_cv_func_spawnattr_setschedpolicy=no])
54 dnl Assume that these functions are available if POSIX_SPAWN_SETSCHEDPARAM
55 dnl evaluates to nonzero.
56 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_getschedparam])
57 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_setschedparam])
58 AC_CACHE_CHECK([whether posix_spawnattr_setschedparam is supported],
59 [gl_cv_func_spawnattr_setschedparam],
60 [AC_EGREP_CPP([POSIX scheduling supported], [
62 #if POSIX_SPAWN_SETSCHEDPARAM
63 POSIX scheduling supported
66 [gl_cv_func_spawnattr_setschedparam=yes],
67 [gl_cv_func_spawnattr_setschedparam=no])
70 *) REPLACE_POSIX_SPAWN=1 ;;
77 dnl Test whether posix_spawn actually works.
78 dnl posix_spawn on AIX 5.3..6.1 has two bugs:
79 dnl 1) When it fails to execute the program, the child process exits with
80 dnl exit() rather than _exit(), which causes the stdio buffers to be
81 dnl flushed. Reported by Rainer Tammer.
82 dnl 2) The posix_spawn_file_actions_addopen function does not support file
83 dnl names that contain a '*'.
84 dnl posix_spawn on AIX 5.3..6.1 has also a third bug: It does not work
85 dnl when POSIX threads are used. But we don't test against this bug here.
86 AC_DEFUN([gl_POSIX_SPAWN_WORKS],
88 AC_REQUIRE([AC_PROG_CC])
89 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
90 AC_CACHE_CHECK([whether posix_spawn works], [gl_cv_func_posix_spawn_works],
91 [if test $cross_compiling = no; then
92 AC_LINK_IFELSE([AC_LANG_SOURCE([[
102 #include <sys/types.h>
103 #include <sys/wait.h>
105 extern char **environ;
108 # define STDIN_FILENO 0
110 #ifndef STDOUT_FILENO
111 # define STDOUT_FILENO 1
113 #ifndef STDERR_FILENO
114 # define STDERR_FILENO 2
118 # define WTERMSIG(x) ((x) & 0x7f)
121 # define WIFEXITED(x) (WTERMSIG (x) == 0)
124 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
127 #define CHILD_PROGRAM_FILENAME "/non/exist/ent"
132 if (0 <= fd && fd <= 2)
134 int f = fd_safer (dup (fd));
147 char *argv[2] = { CHILD_PROGRAM_FILENAME, NULL };
149 sigset_t blocked_signals;
150 sigset_t fatal_signal_set;
151 posix_spawn_file_actions_t actions;
152 bool actions_allocated;
153 posix_spawnattr_t attrs;
154 bool attrs_allocated;
160 setvbuf (stdout, NULL, _IOFBF, 0);
161 puts ("This should be seen only once.");
162 if (pipe (ofd) < 0 || (ofd[1] = fd_safer (ofd[1])) < 0)
164 perror ("cannot create pipe");
167 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
168 sigemptyset (&fatal_signal_set);
169 sigaddset (&fatal_signal_set, SIGINT);
170 sigaddset (&fatal_signal_set, SIGTERM);
171 sigaddset (&fatal_signal_set, SIGHUP);
172 sigaddset (&fatal_signal_set, SIGPIPE);
173 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
174 actions_allocated = false;
175 attrs_allocated = false;
176 if ((err = posix_spawn_file_actions_init (&actions)) != 0
177 || (actions_allocated = true,
178 (err = posix_spawn_file_actions_adddup2 (&actions, ofd[0], STDIN_FILENO)) != 0
179 || (err = posix_spawn_file_actions_addclose (&actions, ofd[0])) != 0
180 || (err = posix_spawn_file_actions_addclose (&actions, ofd[1])) != 0
181 || (err = posix_spawnattr_init (&attrs)) != 0
182 || (attrs_allocated = true,
183 (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
184 || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0)
185 || (err = posix_spawnp (&child, CHILD_PROGRAM_FILENAME, &actions, &attrs, argv, environ)) != 0))
187 if (actions_allocated)
188 posix_spawn_file_actions_destroy (&actions);
190 posix_spawnattr_destroy (&attrs);
191 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
197 perror ("subprocess failed");
201 posix_spawn_file_actions_destroy (&actions);
202 posix_spawnattr_destroy (&attrs);
203 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
207 while (waitpid (child, &status, 0) != child)
209 if (!WIFEXITED (status))
211 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
214 exitstatus = WEXITSTATUS (status);
215 if (exitstatus != 127)
217 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
223 [if test -s conftest$ac_exeext \
224 && ./conftest$ac_exeext > conftest.out \
225 && echo 'This should be seen only once.' > conftest.ok \
226 && cmp conftest.out conftest.ok > /dev/null; then
227 gl_cv_func_posix_spawn_works=yes
229 gl_cv_func_posix_spawn_works=no
231 [gl_cv_func_posix_spawn_works=no])
232 if test $gl_cv_func_posix_spawn_works = yes; then
233 AC_RUN_IFELSE([AC_LANG_SOURCE([[
234 /* Test whether posix_spawn_file_actions_addopen supports filename arguments
235 that contain special characters such as '*'. */
245 #include <sys/types.h>
246 #include <sys/wait.h>
248 extern char **environ;
251 # define STDIN_FILENO 0
253 #ifndef STDOUT_FILENO
254 # define STDOUT_FILENO 1
256 #ifndef STDERR_FILENO
257 # define STDERR_FILENO 2
261 # define WTERMSIG(x) ((x) & 0x7f)
264 # define WIFEXITED(x) (WTERMSIG (x) == 0)
267 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
270 #define CHILD_PROGRAM_FILENAME "conftest"
271 #define DATA_FILENAME "conftest%=*#?"
277 char *argv[3] = { CHILD_PROGRAM_FILENAME, "-child", NULL };
278 posix_spawn_file_actions_t actions;
279 bool actions_allocated;
285 /* Create a data file with specific contents. */
286 fp = fopen (DATA_FILENAME, "wb");
289 perror ("cannot create data file");
292 fwrite ("Halle Potta", 1, 11, fp);
293 if (fflush (fp) || fclose (fp))
295 perror ("cannot prepare data file");
299 /* Avoid reading from our stdin, as it could block. */
300 freopen ("/dev/null", "rb", stdin);
302 /* Test whether posix_spawn_file_actions_addopen with this file name
303 actually works, but spawning a child that reads from this file. */
304 actions_allocated = false;
305 if ((err = posix_spawn_file_actions_init (&actions)) != 0
306 || (actions_allocated = true,
307 (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, DATA_FILENAME, O_RDONLY, 0600)) != 0
308 || (err = posix_spawn (&child, CHILD_PROGRAM_FILENAME, &actions, NULL, argv, environ)) != 0))
310 if (actions_allocated)
311 posix_spawn_file_actions_destroy (&actions);
313 perror ("subprocess failed");
316 posix_spawn_file_actions_destroy (&actions);
318 while (waitpid (child, &status, 0) != child)
320 if (!WIFEXITED (status))
322 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
325 exitstatus = WEXITSTATUS (status);
328 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
339 /* See if reading from STDIN_FILENO yields the expected contents. */
340 if (fread (buf, 1, sizeof (buf), stdin) == 11
341 && memcmp (buf, "Halle Potta", 11) == 0)
348 cleanup_then_die (int sig)
350 /* Clean up data file. */
351 unlink (DATA_FILENAME);
353 /* Re-raise the signal and die from it. */
354 signal (sig, SIG_DFL);
359 main (int argc, char *argv[])
363 if (!(argc > 1 && strcmp (argv[1], "-child") == 0))
365 /* This is the parent process. */
366 signal (SIGINT, cleanup_then_die);
367 signal (SIGTERM, cleanup_then_die);
369 signal (SIGHUP, cleanup_then_die);
372 exitstatus = parent_main ();
376 /* This is the child process. */
378 exitstatus = child_main ();
380 unlink (DATA_FILENAME);
385 [gl_cv_func_posix_spawn_works=no])
389 aix*) gl_cv_func_posix_spawn_works="guessing no";;
390 *) gl_cv_func_posix_spawn_works="guessing yes";;
396 AC_DEFUN([gl_POSIX_SPAWN_INTERNAL],
399 dnl Prerequisites of lib/spawni.c.
400 AC_CHECK_HEADERS([paths.h])
401 AC_CHECK_FUNCS([confstr sched_setparam sched_setscheduler setegid seteuid vfork])