3 dnl Copyright (C) 2008-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 dnl Tests whether the entire posix_spawn facility is available.
9 AC_DEFUN([gl_POSIX_SPAWN],
11 AC_REQUIRE([gl_POSIX_SPAWN_BODY])
14 AC_DEFUN([gl_POSIX_SPAWN_BODY],
16 AC_REQUIRE([gl_SPAWN_H_DEFAULTS])
17 AC_REQUIRE([gl_HAVE_POSIX_SPAWN])
18 dnl Assume that when the main function exists, all the others,
19 dnl except posix_spawnattr_{get,set}sched*, are available as well.
20 dnl AC_CHECK_FUNCS_ONCE([posix_spawnp])
21 dnl AC_CHECK_FUNCS_ONCE([posix_spawn_file_actions_init])
22 dnl AC_CHECK_FUNCS_ONCE([posix_spawn_file_actions_addclose])
23 dnl AC_CHECK_FUNCS_ONCE([posix_spawn_file_actions_adddup2])
24 dnl AC_CHECK_FUNCS_ONCE([posix_spawn_file_actions_addopen])
25 dnl AC_CHECK_FUNCS_ONCE([posix_spawn_file_actions_destroy])
26 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_init])
27 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_getflags])
28 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_setflags])
29 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_getpgroup])
30 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_setpgroup])
31 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_getsigdefault])
32 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_setsigdefault])
33 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_getsigmask])
34 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_setsigmask])
35 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_destroy])
36 AC_CHECK_DECLS([posix_spawn], , , [[#include <spawn.h>]])
37 if test $ac_cv_func_posix_spawn = yes; then
38 m4_ifdef([gl_FUNC_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR],
39 [dnl Module 'posix_spawn_file_actions_addchdir' is present.
40 gl_CHECK_FUNCS_ANDROID([posix_spawn_file_actions_addchdir_np],
41 [[#include <spawn.h>]])
42 if test $ac_cv_func_posix_spawn_file_actions_addchdir_np = no; then
43 dnl In order to implement the posix_spawn_file_actions_addchdir
44 dnl function, we need to replace the entire posix_spawn facility.
48 m4_ifdef([gl_FUNC_POSIX_SPAWN_FILE_ACTIONS_ADDFCHDIR],
49 [dnl Module 'posix_spawn_file_actions_addfchdir' is present.
50 gl_CHECK_FUNCS_ANDROID([posix_spawn_file_actions_addfchdir_np],
51 [[#include <spawn.h>]])
52 if test $ac_cv_func_posix_spawn_file_actions_addfchdir_np = no; then
53 dnl In order to implement the posix_spawn_file_actions_addfchdir
54 dnl function, we need to replace the entire posix_spawn facility.
58 if test $REPLACE_POSIX_SPAWN = 0; then
60 case "$gl_cv_func_posix_spawn_works" in
62 *) REPLACE_POSIX_SPAWN=1 ;;
65 if test $REPLACE_POSIX_SPAWN = 0; then
67 case "$gl_cv_func_posix_spawn_secure_exec" in
69 *) REPLACE_POSIX_SPAWN=1 ;;
71 case "$gl_cv_func_posix_spawnp_secure_exec" in
73 *) REPLACE_POSIX_SPAWN=1 ;;
76 if test $REPLACE_POSIX_SPAWN = 0; then
77 dnl Assume that these functions are available if POSIX_SPAWN_SETSCHEDULER
78 dnl evaluates to nonzero.
79 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_getschedpolicy])
80 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_setschedpolicy])
81 AC_CACHE_CHECK([whether posix_spawnattr_setschedpolicy is supported],
82 [gl_cv_func_spawnattr_setschedpolicy],
83 [AC_EGREP_CPP([POSIX scheduling supported], [
85 #if POSIX_SPAWN_SETSCHEDULER
86 POSIX scheduling supported
89 [gl_cv_func_spawnattr_setschedpolicy=yes],
90 [gl_cv_func_spawnattr_setschedpolicy=no])
92 dnl Assume that these functions are available if POSIX_SPAWN_SETSCHEDPARAM
93 dnl evaluates to nonzero.
94 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_getschedparam])
95 dnl AC_CHECK_FUNCS_ONCE([posix_spawnattr_setschedparam])
96 AC_CACHE_CHECK([whether posix_spawnattr_setschedparam is supported],
97 [gl_cv_func_spawnattr_setschedparam],
98 [AC_EGREP_CPP([POSIX scheduling supported], [
100 #if POSIX_SPAWN_SETSCHEDPARAM
101 POSIX scheduling supported
104 [gl_cv_func_spawnattr_setschedparam=yes],
105 [gl_cv_func_spawnattr_setschedparam=no])
109 dnl The system does not have the main function. Therefore we have to
110 dnl provide our own implementation. This implies to define our own
111 dnl posix_spawn_file_actions_t and posix_spawnattr_t types.
112 if test $ac_cv_have_decl_posix_spawn = yes; then
113 dnl The system declares posix_spawn() already. This declaration uses
114 dnl the original posix_spawn_file_actions_t and posix_spawnattr_t types.
115 dnl But we need a declaration with our own posix_spawn_file_actions_t and
116 dnl posix_spawnattr_t types.
117 REPLACE_POSIX_SPAWN=1
120 if test $ac_cv_func_posix_spawn != yes || test $REPLACE_POSIX_SPAWN = 1; then
121 AC_DEFINE([REPLACE_POSIX_SPAWN], [1],
122 [Define if gnulib uses its own posix_spawn and posix_spawnp functions.])
126 dnl Test whether posix_spawn actually works.
127 dnl posix_spawn on AIX 5.3..6.1 has two bugs:
128 dnl 1) When it fails to execute the program, the child process exits with
129 dnl exit() rather than _exit(), which causes the stdio buffers to be
130 dnl flushed. Reported by Rainer Tammer.
131 dnl 2) The posix_spawn_file_actions_addopen function does not support file
132 dnl names that contain a '*'.
133 dnl posix_spawn on AIX 5.3..6.1 has also a third bug: It does not work
134 dnl when POSIX threads are used. But we don't test against this bug here.
135 AC_DEFUN([gl_POSIX_SPAWN_WORKS],
137 AC_REQUIRE([AC_PROG_CC])
138 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
139 AC_CACHE_CHECK([whether posix_spawn works], [gl_cv_func_posix_spawn_works],
140 [if test $cross_compiling = no; then
141 AC_LINK_IFELSE([AC_LANG_SOURCE([[
151 #include <sys/types.h>
152 #include <sys/wait.h>
155 extern char **environ;
158 # define STDIN_FILENO 0
160 #ifndef STDOUT_FILENO
161 # define STDOUT_FILENO 1
163 #ifndef STDERR_FILENO
164 # define STDERR_FILENO 2
168 # define WTERMSIG(x) ((x) & 0x7f)
171 # define WIFEXITED(x) (WTERMSIG (x) == 0)
174 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
177 #define CHILD_PROGRAM_FILENAME "/non/exist/ent"
182 if (0 <= fd && fd <= 2)
184 int f = fd_safer (dup (fd));
197 char *argv[2] = { CHILD_PROGRAM_FILENAME, NULL };
199 sigset_t blocked_signals;
200 sigset_t fatal_signal_set;
201 posix_spawn_file_actions_t actions;
202 bool actions_allocated;
203 posix_spawnattr_t attrs;
204 bool attrs_allocated;
210 setvbuf (stdout, NULL, _IOFBF, 0);
211 puts ("This should be seen only once.");
212 if (pipe (ofd) < 0 || (ofd[1] = fd_safer (ofd[1])) < 0)
214 perror ("cannot create pipe");
217 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
218 sigemptyset (&fatal_signal_set);
219 sigaddset (&fatal_signal_set, SIGINT);
220 sigaddset (&fatal_signal_set, SIGTERM);
221 sigaddset (&fatal_signal_set, SIGHUP);
222 sigaddset (&fatal_signal_set, SIGPIPE);
223 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
224 actions_allocated = false;
225 attrs_allocated = false;
226 if ((err = posix_spawn_file_actions_init (&actions)) != 0
227 || (actions_allocated = true,
228 (err = posix_spawn_file_actions_adddup2 (&actions, ofd[0], STDIN_FILENO)) != 0
229 || (err = posix_spawn_file_actions_addclose (&actions, ofd[0])) != 0
230 || (err = posix_spawn_file_actions_addclose (&actions, ofd[1])) != 0
231 || (err = posix_spawnattr_init (&attrs)) != 0
232 || (attrs_allocated = true,
233 (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
234 || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0)
235 || (err = posix_spawnp (&child, CHILD_PROGRAM_FILENAME, &actions, &attrs, argv, environ)) != 0))
237 if (actions_allocated)
238 posix_spawn_file_actions_destroy (&actions);
240 posix_spawnattr_destroy (&attrs);
241 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
247 perror ("subprocess failed");
251 posix_spawn_file_actions_destroy (&actions);
252 posix_spawnattr_destroy (&attrs);
253 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
257 while (waitpid (child, &status, 0) != child)
259 if (!WIFEXITED (status))
261 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
264 exitstatus = WEXITSTATUS (status);
265 if (exitstatus != 127)
267 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
273 [if test -s conftest$ac_exeext \
274 && ./conftest$ac_exeext > conftest.out \
275 && echo 'This should be seen only once.' > conftest.ok \
276 && cmp conftest.out conftest.ok >/dev/null 2>&1; then
277 gl_cv_func_posix_spawn_works=yes
279 gl_cv_func_posix_spawn_works=no
281 [gl_cv_func_posix_spawn_works=no])
282 if test $gl_cv_func_posix_spawn_works = yes; then
283 AC_RUN_IFELSE([AC_LANG_SOURCE([[
284 /* Test whether posix_spawn_file_actions_addopen supports filename arguments
285 that contain special characters such as '*'. */
295 #include <sys/types.h>
296 #include <sys/wait.h>
299 extern char **environ;
302 # define STDIN_FILENO 0
304 #ifndef STDOUT_FILENO
305 # define STDOUT_FILENO 1
307 #ifndef STDERR_FILENO
308 # define STDERR_FILENO 2
312 # define WTERMSIG(x) ((x) & 0x7f)
315 # define WIFEXITED(x) (WTERMSIG (x) == 0)
318 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
321 #define CHILD_PROGRAM_FILENAME "conftest"
322 #define DATA_FILENAME "conftest%=*#?"
328 char *argv[3] = { CHILD_PROGRAM_FILENAME, "-child", NULL };
329 posix_spawn_file_actions_t actions;
330 bool actions_allocated;
336 /* Create a data file with specific contents. */
337 fp = fopen (DATA_FILENAME, "wb");
340 perror ("cannot create data file");
343 fwrite ("Halle Potta", 1, 11, fp);
344 if (fflush (fp) || fclose (fp))
346 perror ("cannot prepare data file");
350 /* Avoid reading from our stdin, as it could block. */
351 freopen ("/dev/null", "rb", stdin);
353 /* Test whether posix_spawn_file_actions_addopen with this file name
354 actually works, but spawning a child that reads from this file. */
355 actions_allocated = false;
356 if ((err = posix_spawn_file_actions_init (&actions)) != 0
357 || (actions_allocated = true,
358 (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, DATA_FILENAME, O_RDONLY, 0600)) != 0
359 || (err = posix_spawn (&child, CHILD_PROGRAM_FILENAME, &actions, NULL, argv, environ)) != 0))
361 if (actions_allocated)
362 posix_spawn_file_actions_destroy (&actions);
364 perror ("subprocess failed");
367 posix_spawn_file_actions_destroy (&actions);
369 while (waitpid (child, &status, 0) != child)
371 if (!WIFEXITED (status))
373 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
376 exitstatus = WEXITSTATUS (status);
379 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
390 /* See if reading from STDIN_FILENO yields the expected contents. */
391 if (fread (buf, 1, sizeof (buf), stdin) == 11
392 && memcmp (buf, "Halle Potta", 11) == 0)
399 cleanup_then_die (int sig)
401 /* Clean up data file. */
402 unlink (DATA_FILENAME);
404 /* Re-raise the signal and die from it. */
405 signal (sig, SIG_DFL);
410 main (int argc, char *argv[])
414 if (!(argc > 1 && strcmp (argv[1], "-child") == 0))
416 /* This is the parent process. */
417 signal (SIGINT, cleanup_then_die);
418 signal (SIGTERM, cleanup_then_die);
420 signal (SIGHUP, cleanup_then_die);
423 exitstatus = parent_main ();
427 /* This is the child process. */
429 exitstatus = child_main ();
431 unlink (DATA_FILENAME);
436 [gl_cv_func_posix_spawn_works=no])
440 aix*) gl_cv_func_posix_spawn_works="guessing no";;
441 *) gl_cv_func_posix_spawn_works="guessing yes";;
447 dnl Test whether posix_spawn and posix_spawnp are secure.
448 AC_DEFUN([gl_POSIX_SPAWN_SECURE],
450 AC_REQUIRE([AC_PROG_CC])
451 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
452 dnl On many platforms, posix_spawn or posix_spawnp allow executing a
453 dnl script without a '#!' marker as a shell script. This is unsecure.
454 AC_CACHE_CHECK([whether posix_spawn rejects scripts without shebang],
455 [gl_cv_func_posix_spawn_secure_exec],
456 [echo ':' > conftest.scr
457 chmod a+x conftest.scr
458 AC_RUN_IFELSE([AC_LANG_SOURCE([[
462 #include <sys/types.h>
463 #include <sys/wait.h>
467 const char *prog_path = "./conftest.scr";
468 const char *prog_argv[2] = { prog_path, NULL };
469 const char *environment[2] = { "PATH=.", NULL };
472 int err = posix_spawn (&child, prog_path, NULL, NULL,
473 (char **) prog_argv, (char **) environment);
479 while (waitpid (child, &status, 0) != child)
481 if (!WIFEXITED (status))
483 if (WEXITSTATUS (status) != 127)
488 [gl_cv_func_posix_spawn_secure_exec=yes],
489 [gl_cv_func_posix_spawn_secure_exec=no],
491 # Guess no on GNU/Hurd.
493 gl_cv_func_posix_spawn_secure_exec="guessing no" ;;
494 # Guess yes on all other platforms.
496 gl_cv_func_posix_spawn_secure_exec="guessing yes" ;;
501 AC_CACHE_CHECK([whether posix_spawnp rejects scripts without shebang],
502 [gl_cv_func_posix_spawnp_secure_exec],
503 [echo ':' > conftest.scr
504 chmod a+x conftest.scr
505 AC_RUN_IFELSE([AC_LANG_SOURCE([[
509 #include <sys/types.h>
510 #include <sys/wait.h>
514 const char *prog_path = "./conftest.scr";
515 const char *prog_argv[2] = { prog_path, NULL };
516 const char *environment[2] = { "PATH=.", NULL };
519 int err = posix_spawnp (&child, prog_path, NULL, NULL,
520 (char **) prog_argv, (char **) environment);
526 while (waitpid (child, &status, 0) != child)
528 if (!WIFEXITED (status))
530 if (WEXITSTATUS (status) != 127)
535 [gl_cv_func_posix_spawnp_secure_exec=yes],
536 [gl_cv_func_posix_spawnp_secure_exec=no],
538 # Guess yes on glibc systems (glibc >= 2.15 actually) except GNU/Hurd,
540 *-gnu* | *-musl* | midipix* | netbsd*)
541 gl_cv_func_posix_spawnp_secure_exec="guessing yes" ;;
542 # Guess no on GNU/Hurd, macOS, FreeBSD, OpenBSD, AIX, Solaris, Cygwin.
543 gnu* | darwin* | freebsd* | dragonfly* | midnightbsd* | openbsd* | \
544 aix* | solaris* | cygwin*)
545 gl_cv_func_posix_spawnp_secure_exec="guessing no" ;;
546 # If we don't know, obey --enable-cross-guesses.
548 gl_cv_func_posix_spawnp_secure_exec="$gl_cross_guess_normal" ;;
555 # Prerequisites of lib/spawni.c.
556 AC_DEFUN([gl_PREREQ_POSIX_SPAWN_INTERNAL],
558 AC_CHECK_HEADERS([paths.h])
559 AC_CHECK_FUNCS([confstr sched_setparam sched_setscheduler setegid seteuid vfork])
562 AC_DEFUN([gl_FUNC_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSE],
564 AC_REQUIRE([gl_SPAWN_H_DEFAULTS])
565 AC_REQUIRE([AC_PROG_CC])
566 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
568 if test $REPLACE_POSIX_SPAWN = 1; then
569 REPLACE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSE=1
571 dnl On musl libc, posix_spawn_file_actions_addclose succeeds even if the fd
572 dnl argument is negative.
573 AC_CACHE_CHECK([whether posix_spawn_file_actions_addclose works],
574 [gl_cv_func_posix_spawn_file_actions_addclose_works],
580 posix_spawn_file_actions_t actions;
581 if (posix_spawn_file_actions_init (&actions) != 0)
583 if (posix_spawn_file_actions_addclose (&actions, -5) == 0)
587 [gl_cv_func_posix_spawn_file_actions_addclose_works=yes],
588 [gl_cv_func_posix_spawn_file_actions_addclose_works=no],
589 [# Guess no on musl libc and Solaris, yes otherwise.
591 *-musl* | midipix*) gl_cv_func_posix_spawn_file_actions_addclose_works="guessing no" ;;
592 solaris*) gl_cv_func_posix_spawn_file_actions_addclose_works="guessing no" ;;
593 # Guess no on native Windows.
594 mingw* | windows*) gl_cv_func_posix_spawn_file_actions_addclose_works="guessing no" ;;
595 *) gl_cv_func_posix_spawn_file_actions_addclose_works="guessing yes" ;;
599 case "$gl_cv_func_posix_spawn_file_actions_addclose_works" in
601 *) REPLACE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSE=1 ;;
606 AC_DEFUN([gl_FUNC_POSIX_SPAWN_FILE_ACTIONS_ADDDUP2],
608 AC_REQUIRE([gl_SPAWN_H_DEFAULTS])
609 AC_REQUIRE([AC_PROG_CC])
610 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
612 if test $REPLACE_POSIX_SPAWN = 1; then
613 REPLACE_POSIX_SPAWN_FILE_ACTIONS_ADDDUP2=1
615 dnl On musl libc and Solaris 11.0, posix_spawn_file_actions_adddup2
616 dnl succeeds even if the fd argument is out of range.
617 AC_CACHE_CHECK([whether posix_spawn_file_actions_adddup2 works],
618 [gl_cv_func_posix_spawn_file_actions_adddup2_works],
624 posix_spawn_file_actions_t actions;
625 if (posix_spawn_file_actions_init (&actions) != 0)
627 if (posix_spawn_file_actions_adddup2 (&actions, 10000000, 2) == 0)
631 [gl_cv_func_posix_spawn_file_actions_adddup2_works=yes],
632 [gl_cv_func_posix_spawn_file_actions_adddup2_works=no],
633 [# Guess no on musl libc and Solaris, yes otherwise.
635 *-musl* | midipix*) gl_cv_func_posix_spawn_file_actions_adddup2_works="guessing no";;
636 solaris*) gl_cv_func_posix_spawn_file_actions_adddup2_works="guessing no";;
637 # Guess no on native Windows.
638 mingw* | windows*) gl_cv_func_posix_spawn_file_actions_adddup2_works="guessing no" ;;
639 *) gl_cv_func_posix_spawn_file_actions_adddup2_works="guessing yes";;
643 case "$gl_cv_func_posix_spawn_file_actions_adddup2_works" in
645 *) REPLACE_POSIX_SPAWN_FILE_ACTIONS_ADDDUP2=1 ;;
650 AC_DEFUN([gl_FUNC_POSIX_SPAWN_FILE_ACTIONS_ADDOPEN],
652 AC_REQUIRE([gl_SPAWN_H_DEFAULTS])
653 AC_REQUIRE([AC_PROG_CC])
654 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
656 if test $REPLACE_POSIX_SPAWN = 1; then
657 REPLACE_POSIX_SPAWN_FILE_ACTIONS_ADDOPEN=1
659 dnl On musl libc and Solaris 11.0, posix_spawn_file_actions_addopen
660 dnl succeeds even if the fd argument is out of range.
661 AC_CACHE_CHECK([whether posix_spawn_file_actions_addopen works],
662 [gl_cv_func_posix_spawn_file_actions_addopen_works],
669 posix_spawn_file_actions_t actions;
670 if (posix_spawn_file_actions_init (&actions) != 0)
672 if (posix_spawn_file_actions_addopen (&actions, 10000000, "foo", 0, O_RDONLY)
677 [gl_cv_func_posix_spawn_file_actions_addopen_works=yes],
678 [gl_cv_func_posix_spawn_file_actions_addopen_works=no],
679 [# Guess no on musl libc and Solaris, yes otherwise.
681 *-musl* | midipix*) gl_cv_func_posix_spawn_file_actions_addopen_works="guessing no";;
682 solaris*) gl_cv_func_posix_spawn_file_actions_addopen_works="guessing no";;
683 # Guess no on native Windows.
684 mingw* | windows*) gl_cv_func_posix_spawn_file_actions_addopen_works="guessing no" ;;
685 *) gl_cv_func_posix_spawn_file_actions_addopen_works="guessing yes";;
689 case "$gl_cv_func_posix_spawn_file_actions_addopen_works" in
691 *) REPLACE_POSIX_SPAWN_FILE_ACTIONS_ADDOPEN=1 ;;