1 # posix_spawn.m4 serial 9
2 dnl Copyright (C) 2008-2011 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_REQUIRE([gl_HAVE_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 ;;
75 dnl Test whether posix_spawn actually works.
76 dnl posix_spawn on AIX 5.3..6.1 has two bugs:
77 dnl 1) When it fails to execute the program, the child process exits with
78 dnl exit() rather than _exit(), which causes the stdio buffers to be
79 dnl flushed. Reported by Rainer Tammer.
80 dnl 2) The posix_spawn_file_actions_addopen function does not support file
81 dnl names that contain a '*'.
82 dnl posix_spawn on AIX 5.3..6.1 has also a third bug: It does not work
83 dnl when POSIX threads are used. But we don't test against this bug here.
84 AC_DEFUN([gl_POSIX_SPAWN_WORKS],
86 AC_REQUIRE([AC_PROG_CC])
87 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
88 AC_CACHE_CHECK([whether posix_spawn works], [gl_cv_func_posix_spawn_works],
89 [if test $cross_compiling = no; then
90 AC_LINK_IFELSE([AC_LANG_SOURCE([[
100 #include <sys/types.h>
101 #include <sys/wait.h>
103 extern char **environ;
106 # define STDIN_FILENO 0
108 #ifndef STDOUT_FILENO
109 # define STDOUT_FILENO 1
111 #ifndef STDERR_FILENO
112 # define STDERR_FILENO 2
116 # define WTERMSIG(x) ((x) & 0x7f)
119 # define WIFEXITED(x) (WTERMSIG (x) == 0)
122 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
125 #define CHILD_PROGRAM_FILENAME "/non/exist/ent"
130 if (0 <= fd && fd <= 2)
132 int f = fd_safer (dup (fd));
145 char *argv[2] = { CHILD_PROGRAM_FILENAME, NULL };
147 sigset_t blocked_signals;
148 sigset_t fatal_signal_set;
149 posix_spawn_file_actions_t actions;
150 bool actions_allocated;
151 posix_spawnattr_t attrs;
152 bool attrs_allocated;
158 setvbuf (stdout, NULL, _IOFBF, 0);
159 puts ("This should be seen only once.");
160 if (pipe (ofd) < 0 || (ofd[1] = fd_safer (ofd[1])) < 0)
162 perror ("cannot create pipe");
165 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
166 sigemptyset (&fatal_signal_set);
167 sigaddset (&fatal_signal_set, SIGINT);
168 sigaddset (&fatal_signal_set, SIGTERM);
169 sigaddset (&fatal_signal_set, SIGHUP);
170 sigaddset (&fatal_signal_set, SIGPIPE);
171 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
172 actions_allocated = false;
173 attrs_allocated = false;
174 if ((err = posix_spawn_file_actions_init (&actions)) != 0
175 || (actions_allocated = true,
176 (err = posix_spawn_file_actions_adddup2 (&actions, ofd[0], STDIN_FILENO)) != 0
177 || (err = posix_spawn_file_actions_addclose (&actions, ofd[0])) != 0
178 || (err = posix_spawn_file_actions_addclose (&actions, ofd[1])) != 0
179 || (err = posix_spawnattr_init (&attrs)) != 0
180 || (attrs_allocated = true,
181 (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
182 || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0)
183 || (err = posix_spawnp (&child, CHILD_PROGRAM_FILENAME, &actions, &attrs, argv, environ)) != 0))
185 if (actions_allocated)
186 posix_spawn_file_actions_destroy (&actions);
188 posix_spawnattr_destroy (&attrs);
189 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
195 perror ("subprocess failed");
199 posix_spawn_file_actions_destroy (&actions);
200 posix_spawnattr_destroy (&attrs);
201 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
205 while (waitpid (child, &status, 0) != child)
207 if (!WIFEXITED (status))
209 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
212 exitstatus = WEXITSTATUS (status);
213 if (exitstatus != 127)
215 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
221 [if test -s conftest$ac_exeext \
222 && ./conftest$ac_exeext > conftest.out \
223 && echo 'This should be seen only once.' > conftest.ok \
224 && cmp conftest.out conftest.ok > /dev/null; then
225 gl_cv_func_posix_spawn_works=yes
227 gl_cv_func_posix_spawn_works=no
229 [gl_cv_func_posix_spawn_works=no])
230 if test $gl_cv_func_posix_spawn_works = yes; then
231 AC_RUN_IFELSE([AC_LANG_SOURCE([[
232 /* Test whether posix_spawn_file_actions_addopen supports filename arguments
233 that contain special characters such as '*'. */
243 #include <sys/types.h>
244 #include <sys/wait.h>
246 extern char **environ;
249 # define STDIN_FILENO 0
251 #ifndef STDOUT_FILENO
252 # define STDOUT_FILENO 1
254 #ifndef STDERR_FILENO
255 # define STDERR_FILENO 2
259 # define WTERMSIG(x) ((x) & 0x7f)
262 # define WIFEXITED(x) (WTERMSIG (x) == 0)
265 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
268 #define CHILD_PROGRAM_FILENAME "conftest"
269 #define DATA_FILENAME "conftest%=*#?"
275 char *argv[3] = { CHILD_PROGRAM_FILENAME, "-child", NULL };
276 posix_spawn_file_actions_t actions;
277 bool actions_allocated;
283 /* Create a data file with specific contents. */
284 fp = fopen (DATA_FILENAME, "wb");
287 perror ("cannot create data file");
290 fwrite ("Halle Potta", 1, 11, fp);
291 if (fflush (fp) || fclose (fp))
293 perror ("cannot prepare data file");
297 /* Avoid reading from our stdin, as it could block. */
298 freopen ("/dev/null", "rb", stdin);
300 /* Test whether posix_spawn_file_actions_addopen with this file name
301 actually works, but spawning a child that reads from this file. */
302 actions_allocated = false;
303 if ((err = posix_spawn_file_actions_init (&actions)) != 0
304 || (actions_allocated = true,
305 (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, DATA_FILENAME, O_RDONLY, 0600)) != 0
306 || (err = posix_spawn (&child, CHILD_PROGRAM_FILENAME, &actions, NULL, argv, environ)) != 0))
308 if (actions_allocated)
309 posix_spawn_file_actions_destroy (&actions);
311 perror ("subprocess failed");
314 posix_spawn_file_actions_destroy (&actions);
316 while (waitpid (child, &status, 0) != child)
318 if (!WIFEXITED (status))
320 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
323 exitstatus = WEXITSTATUS (status);
326 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
337 /* See if reading from STDIN_FILENO yields the expected contents. */
338 if (fread (buf, 1, sizeof (buf), stdin) == 11
339 && memcmp (buf, "Halle Potta", 11) == 0)
346 cleanup_then_die (int sig)
348 /* Clean up data file. */
349 unlink (DATA_FILENAME);
351 /* Re-raise the signal and die from it. */
352 signal (sig, SIG_DFL);
357 main (int argc, char *argv[])
361 if (!(argc > 1 && strcmp (argv[1], "-child") == 0))
363 /* This is the parent process. */
364 signal (SIGINT, cleanup_then_die);
365 signal (SIGTERM, cleanup_then_die);
367 signal (SIGHUP, cleanup_then_die);
370 exitstatus = parent_main ();
374 /* This is the child process. */
376 exitstatus = child_main ();
378 unlink (DATA_FILENAME);
383 [gl_cv_func_posix_spawn_works=no])
387 aix*) gl_cv_func_posix_spawn_works="guessing no";;
388 *) gl_cv_func_posix_spawn_works="guessing yes";;
394 # Prerequisites of lib/spawni.c.
395 AC_DEFUN([gl_PREREQ_POSIX_SPAWN_INTERNAL],
397 AC_CHECK_HEADERS([paths.h])
398 AC_CHECK_FUNCS([confstr sched_setparam sched_setscheduler setegid seteuid vfork])