1 # posix_spawn.m4 serial 15
2 dnl Copyright (C) 2008-2018 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
36 m4_ifdef([gl_FUNC_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR],
37 [dnl Module 'posix_spawn_file_actions_addchdir' is present.
38 AC_CHECK_FUNCS_ONCE([posix_spawn_file_actions_addchdir_np])
39 if test $ac_cv_func_posix_spawn_file_actions_addchdir_np = no; then
40 dnl In order to implement the posix_spawn_file_actions_addchdir
41 dnl function, we need to replace the entire posix_spawn facility.
45 if test $REPLACE_POSIX_SPAWN = 0; then
47 case "$gl_cv_func_posix_spawn_works" in
49 dnl Assume that these functions are available if POSIX_SPAWN_SETSCHEDULER
50 dnl evaluates to nonzero.
51 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_getschedpolicy])
52 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_setschedpolicy])
53 AC_CACHE_CHECK([whether posix_spawnattr_setschedpolicy is supported],
54 [gl_cv_func_spawnattr_setschedpolicy],
55 [AC_EGREP_CPP([POSIX scheduling supported], [
57 #if POSIX_SPAWN_SETSCHEDULER
58 POSIX scheduling supported
61 [gl_cv_func_spawnattr_setschedpolicy=yes],
62 [gl_cv_func_spawnattr_setschedpolicy=no])
64 dnl Assume that these functions are available if POSIX_SPAWN_SETSCHEDPARAM
65 dnl evaluates to nonzero.
66 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_getschedparam])
67 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_setschedparam])
68 AC_CACHE_CHECK([whether posix_spawnattr_setschedparam is supported],
69 [gl_cv_func_spawnattr_setschedparam],
70 [AC_EGREP_CPP([POSIX scheduling supported], [
72 #if POSIX_SPAWN_SETSCHEDPARAM
73 POSIX scheduling supported
76 [gl_cv_func_spawnattr_setschedparam=yes],
77 [gl_cv_func_spawnattr_setschedparam=no])
80 *) REPLACE_POSIX_SPAWN=1 ;;
84 if test $ac_cv_func_posix_spawn != yes || test $REPLACE_POSIX_SPAWN = 1; then
85 AC_DEFINE([REPLACE_POSIX_SPAWN], [1],
86 [Define if gnulib uses its own posix_spawn and posix_spawnp functions.])
90 dnl Test whether posix_spawn actually works.
91 dnl posix_spawn on AIX 5.3..6.1 has two bugs:
92 dnl 1) When it fails to execute the program, the child process exits with
93 dnl exit() rather than _exit(), which causes the stdio buffers to be
94 dnl flushed. Reported by Rainer Tammer.
95 dnl 2) The posix_spawn_file_actions_addopen function does not support file
96 dnl names that contain a '*'.
97 dnl posix_spawn on AIX 5.3..6.1 has also a third bug: It does not work
98 dnl when POSIX threads are used. But we don't test against this bug here.
99 AC_DEFUN([gl_POSIX_SPAWN_WORKS],
101 AC_REQUIRE([AC_PROG_CC])
102 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
103 AC_CACHE_CHECK([whether posix_spawn works], [gl_cv_func_posix_spawn_works],
104 [if test $cross_compiling = no; then
105 AC_LINK_IFELSE([AC_LANG_SOURCE([[
115 #include <sys/types.h>
116 #include <sys/wait.h>
118 extern char **environ;
121 # define STDIN_FILENO 0
123 #ifndef STDOUT_FILENO
124 # define STDOUT_FILENO 1
126 #ifndef STDERR_FILENO
127 # define STDERR_FILENO 2
131 # define WTERMSIG(x) ((x) & 0x7f)
134 # define WIFEXITED(x) (WTERMSIG (x) == 0)
137 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
140 #define CHILD_PROGRAM_FILENAME "/non/exist/ent"
145 if (0 <= fd && fd <= 2)
147 int f = fd_safer (dup (fd));
160 char *argv[2] = { CHILD_PROGRAM_FILENAME, NULL };
162 sigset_t blocked_signals;
163 sigset_t fatal_signal_set;
164 posix_spawn_file_actions_t actions;
165 bool actions_allocated;
166 posix_spawnattr_t attrs;
167 bool attrs_allocated;
173 setvbuf (stdout, NULL, _IOFBF, 0);
174 puts ("This should be seen only once.");
175 if (pipe (ofd) < 0 || (ofd[1] = fd_safer (ofd[1])) < 0)
177 perror ("cannot create pipe");
180 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
181 sigemptyset (&fatal_signal_set);
182 sigaddset (&fatal_signal_set, SIGINT);
183 sigaddset (&fatal_signal_set, SIGTERM);
184 sigaddset (&fatal_signal_set, SIGHUP);
185 sigaddset (&fatal_signal_set, SIGPIPE);
186 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
187 actions_allocated = false;
188 attrs_allocated = false;
189 if ((err = posix_spawn_file_actions_init (&actions)) != 0
190 || (actions_allocated = true,
191 (err = posix_spawn_file_actions_adddup2 (&actions, ofd[0], STDIN_FILENO)) != 0
192 || (err = posix_spawn_file_actions_addclose (&actions, ofd[0])) != 0
193 || (err = posix_spawn_file_actions_addclose (&actions, ofd[1])) != 0
194 || (err = posix_spawnattr_init (&attrs)) != 0
195 || (attrs_allocated = true,
196 (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
197 || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0)
198 || (err = posix_spawnp (&child, CHILD_PROGRAM_FILENAME, &actions, &attrs, argv, environ)) != 0))
200 if (actions_allocated)
201 posix_spawn_file_actions_destroy (&actions);
203 posix_spawnattr_destroy (&attrs);
204 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
210 perror ("subprocess failed");
214 posix_spawn_file_actions_destroy (&actions);
215 posix_spawnattr_destroy (&attrs);
216 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
220 while (waitpid (child, &status, 0) != child)
222 if (!WIFEXITED (status))
224 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
227 exitstatus = WEXITSTATUS (status);
228 if (exitstatus != 127)
230 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
236 [if test -s conftest$ac_exeext \
237 && ./conftest$ac_exeext > conftest.out \
238 && echo 'This should be seen only once.' > conftest.ok \
239 && cmp conftest.out conftest.ok >/dev/null 2>&1; then
240 gl_cv_func_posix_spawn_works=yes
242 gl_cv_func_posix_spawn_works=no
244 [gl_cv_func_posix_spawn_works=no])
245 if test $gl_cv_func_posix_spawn_works = yes; then
246 AC_RUN_IFELSE([AC_LANG_SOURCE([[
247 /* Test whether posix_spawn_file_actions_addopen supports filename arguments
248 that contain special characters such as '*'. */
258 #include <sys/types.h>
259 #include <sys/wait.h>
261 extern char **environ;
264 # define STDIN_FILENO 0
266 #ifndef STDOUT_FILENO
267 # define STDOUT_FILENO 1
269 #ifndef STDERR_FILENO
270 # define STDERR_FILENO 2
274 # define WTERMSIG(x) ((x) & 0x7f)
277 # define WIFEXITED(x) (WTERMSIG (x) == 0)
280 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
283 #define CHILD_PROGRAM_FILENAME "conftest"
284 #define DATA_FILENAME "conftest%=*#?"
290 char *argv[3] = { CHILD_PROGRAM_FILENAME, "-child", NULL };
291 posix_spawn_file_actions_t actions;
292 bool actions_allocated;
298 /* Create a data file with specific contents. */
299 fp = fopen (DATA_FILENAME, "wb");
302 perror ("cannot create data file");
305 fwrite ("Halle Potta", 1, 11, fp);
306 if (fflush (fp) || fclose (fp))
308 perror ("cannot prepare data file");
312 /* Avoid reading from our stdin, as it could block. */
313 freopen ("/dev/null", "rb", stdin);
315 /* Test whether posix_spawn_file_actions_addopen with this file name
316 actually works, but spawning a child that reads from this file. */
317 actions_allocated = false;
318 if ((err = posix_spawn_file_actions_init (&actions)) != 0
319 || (actions_allocated = true,
320 (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, DATA_FILENAME, O_RDONLY, 0600)) != 0
321 || (err = posix_spawn (&child, CHILD_PROGRAM_FILENAME, &actions, NULL, argv, environ)) != 0))
323 if (actions_allocated)
324 posix_spawn_file_actions_destroy (&actions);
326 perror ("subprocess failed");
329 posix_spawn_file_actions_destroy (&actions);
331 while (waitpid (child, &status, 0) != child)
333 if (!WIFEXITED (status))
335 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
338 exitstatus = WEXITSTATUS (status);
341 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
352 /* See if reading from STDIN_FILENO yields the expected contents. */
353 if (fread (buf, 1, sizeof (buf), stdin) == 11
354 && memcmp (buf, "Halle Potta", 11) == 0)
361 cleanup_then_die (int sig)
363 /* Clean up data file. */
364 unlink (DATA_FILENAME);
366 /* Re-raise the signal and die from it. */
367 signal (sig, SIG_DFL);
372 main (int argc, char *argv[])
376 if (!(argc > 1 && strcmp (argv[1], "-child") == 0))
378 /* This is the parent process. */
379 signal (SIGINT, cleanup_then_die);
380 signal (SIGTERM, cleanup_then_die);
382 signal (SIGHUP, cleanup_then_die);
385 exitstatus = parent_main ();
389 /* This is the child process. */
391 exitstatus = child_main ();
393 unlink (DATA_FILENAME);
398 [gl_cv_func_posix_spawn_works=no])
402 aix*) gl_cv_func_posix_spawn_works="guessing no";;
403 *) gl_cv_func_posix_spawn_works="guessing yes";;
409 # Prerequisites of lib/spawni.c.
410 AC_DEFUN([gl_PREREQ_POSIX_SPAWN_INTERNAL],
412 AC_CHECK_HEADERS([paths.h])
413 AC_CHECK_FUNCS([confstr sched_setparam sched_setscheduler setegid seteuid vfork])
416 AC_DEFUN([gl_FUNC_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSE],
418 AC_REQUIRE([gl_SPAWN_H_DEFAULTS])
419 AC_REQUIRE([AC_PROG_CC])
420 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
422 if test $REPLACE_POSIX_SPAWN = 1; then
423 REPLACE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSE=1
425 dnl On Solaris 11.0, posix_spawn_file_actions_addclose succeeds even
426 dnl if the fd argument is out of range.
427 AC_CACHE_CHECK([whether posix_spawn_file_actions_addclose works],
428 [gl_cv_func_posix_spawn_file_actions_addclose_works],
434 posix_spawn_file_actions_t actions;
435 if (posix_spawn_file_actions_init (&actions) != 0)
437 if (posix_spawn_file_actions_addclose (&actions, 10000000) == 0)
441 [gl_cv_func_posix_spawn_file_actions_addclose_works=yes],
442 [gl_cv_func_posix_spawn_file_actions_addclose_works=no],
443 [# Guess no on Solaris, yes otherwise.
445 solaris*) gl_cv_func_posix_spawn_file_actions_addclose_works="guessing no" ;;
446 # Guess no on native Windows.
447 mingw*) gl_cv_func_posix_spawn_file_actions_addclose_works="guessing no" ;;
448 *) gl_cv_func_posix_spawn_file_actions_addclose_works="guessing yes" ;;
452 case "$gl_cv_func_posix_spawn_file_actions_addclose_works" in
454 *) REPLACE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSE=1 ;;
459 AC_DEFUN([gl_FUNC_POSIX_SPAWN_FILE_ACTIONS_ADDDUP2],
461 AC_REQUIRE([gl_SPAWN_H_DEFAULTS])
462 AC_REQUIRE([AC_PROG_CC])
463 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
465 if test $REPLACE_POSIX_SPAWN = 1; then
466 REPLACE_POSIX_SPAWN_FILE_ACTIONS_ADDDUP2=1
468 dnl On Solaris 11.0, posix_spawn_file_actions_adddup2 succeeds even
469 dnl if the fd argument is out of range.
470 AC_CACHE_CHECK([whether posix_spawn_file_actions_adddup2 works],
471 [gl_cv_func_posix_spawn_file_actions_adddup2_works],
477 posix_spawn_file_actions_t actions;
478 if (posix_spawn_file_actions_init (&actions) != 0)
480 if (posix_spawn_file_actions_adddup2 (&actions, 10000000, 2) == 0)
484 [gl_cv_func_posix_spawn_file_actions_adddup2_works=yes],
485 [gl_cv_func_posix_spawn_file_actions_adddup2_works=no],
486 [# Guess no on Solaris, yes otherwise.
488 solaris*) gl_cv_func_posix_spawn_file_actions_adddup2_works="guessing no";;
489 # Guess no on native Windows.
490 mingw*) gl_cv_func_posix_spawn_file_actions_adddup2_works="guessing no" ;;
491 *) gl_cv_func_posix_spawn_file_actions_adddup2_works="guessing yes";;
495 case "$gl_cv_func_posix_spawn_file_actions_adddup2_works" in
497 *) REPLACE_POSIX_SPAWN_FILE_ACTIONS_ADDDUP2=1 ;;
502 AC_DEFUN([gl_FUNC_POSIX_SPAWN_FILE_ACTIONS_ADDOPEN],
504 AC_REQUIRE([gl_SPAWN_H_DEFAULTS])
505 AC_REQUIRE([AC_PROG_CC])
506 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
508 if test $REPLACE_POSIX_SPAWN = 1; then
509 REPLACE_POSIX_SPAWN_FILE_ACTIONS_ADDOPEN=1
511 dnl On Solaris 11.0, posix_spawn_file_actions_addopen succeeds even
512 dnl if the fd argument is out of range.
513 AC_CACHE_CHECK([whether posix_spawn_file_actions_addopen works],
514 [gl_cv_func_posix_spawn_file_actions_addopen_works],
521 posix_spawn_file_actions_t actions;
522 if (posix_spawn_file_actions_init (&actions) != 0)
524 if (posix_spawn_file_actions_addopen (&actions, 10000000, "foo", 0, O_RDONLY)
529 [gl_cv_func_posix_spawn_file_actions_addopen_works=yes],
530 [gl_cv_func_posix_spawn_file_actions_addopen_works=no],
531 [# Guess no on Solaris, yes otherwise.
533 solaris*) gl_cv_func_posix_spawn_file_actions_addopen_works="guessing no";;
534 # Guess no on native Windows.
535 mingw*) gl_cv_func_posix_spawn_file_actions_addopen_works="guessing no" ;;
536 *) gl_cv_func_posix_spawn_file_actions_addopen_works="guessing yes";;
540 case "$gl_cv_func_posix_spawn_file_actions_addopen_works" in
542 *) REPLACE_POSIX_SPAWN_FILE_ACTIONS_ADDOPEN=1 ;;