1 # posix_spawn.m4 serial 3
2 dnl Copyright (C) 2008 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 a bug: When it fails to execute the
52 dnl program, the child process exits with exit() rather than _exit(),
53 dnl which causes the stdio buffers to be flushed. Reported by Rainer Tammer.
54 dnl posix_spawn on AIX 5.3..6.1 has also a second bug: It does not work
55 dnl when POSIX threads are used. But we don't test against this bug here.
56 AC_DEFUN([gl_POSIX_SPAWN_WORKS],
58 AC_REQUIRE([AC_PROG_CC])
59 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
60 AC_CACHE_CHECK([whether posix_spawn works], [gl_cv_func_posix_spawn_works],
61 [if test $cross_compiling = no; then
62 AC_LINK_IFELSE([AC_LANG_PROGRAM([[
72 #include <sys/types.h>
75 extern char **environ;
78 # define STDIN_FILENO 0
81 # define STDOUT_FILENO 1
84 # define STDERR_FILENO 2
88 # define WTERMSIG(x) ((x) & 0x7f)
91 # define WIFEXITED(x) (WTERMSIG (x) == 0)
94 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
97 #define CHILD_PROGRAM_FILENAME "/non/exist/ent"
102 if (0 <= fd && fd <= 2)
104 int f = fd_safer (dup (fd));
114 dnl Now comes the main() function.
116 char *argv[2] = { CHILD_PROGRAM_FILENAME, NULL };
118 sigset_t blocked_signals;
119 sigset_t fatal_signal_set;
120 posix_spawn_file_actions_t actions;
121 bool actions_allocated;
122 posix_spawnattr_t attrs;
123 bool attrs_allocated;
129 setvbuf (stdout, NULL, _IOFBF, 0);
130 puts ("This should be seen only once.");
131 if (pipe (ofd) < 0 || (ofd[1] = fd_safer (ofd[1])) < 0)
133 perror ("cannot create pipe");
136 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
137 sigemptyset (&fatal_signal_set);
138 sigaddset (&fatal_signal_set, SIGINT);
139 sigaddset (&fatal_signal_set, SIGTERM);
140 sigaddset (&fatal_signal_set, SIGHUP);
141 sigaddset (&fatal_signal_set, SIGPIPE);
142 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
143 actions_allocated = false;
144 attrs_allocated = false;
145 if ((err = posix_spawn_file_actions_init (&actions)) != 0
146 || (actions_allocated = true,
147 (err = posix_spawn_file_actions_adddup2 (&actions, ofd[0], STDIN_FILENO)) != 0
148 || (err = posix_spawn_file_actions_addclose (&actions, ofd[0])) != 0
149 || (err = posix_spawn_file_actions_addclose (&actions, ofd[1])) != 0
150 || (err = posix_spawnattr_init (&attrs)) != 0
151 || (attrs_allocated = true,
152 (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
153 || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0)
154 || (err = posix_spawnp (&child, CHILD_PROGRAM_FILENAME, &actions, &attrs, argv, environ)) != 0))
156 if (actions_allocated)
157 posix_spawn_file_actions_destroy (&actions);
159 posix_spawnattr_destroy (&attrs);
160 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
162 perror ("subprocess failed");
165 posix_spawn_file_actions_destroy (&actions);
166 posix_spawnattr_destroy (&attrs);
167 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
171 while (waitpid (child, &status, 0) != child)
173 if (!WIFEXITED (status))
175 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
178 exitstatus = WEXITSTATUS (status);
179 if (exitstatus != 127)
181 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
185 [if test -s conftest$ac_exeext \
186 && ./conftest$ac_exeext > conftest.out \
187 && echo 'This should be seen only once.' > conftest.ok \
188 && cmp conftest.out conftest.ok > /dev/null; then
189 gl_cv_func_posix_spawn_works=yes
191 gl_cv_func_posix_spawn_works=no
193 [gl_cv_func_posix_spawn_works=no])
196 aix*) gl_cv_func_posix_spawn_works="guessing no";;
197 *) gl_cv_func_posix_spawn_works="guessing yes";;
203 AC_DEFUN([gl_POSIX_SPAWN_INTERNAL],
206 dnl Prerequisites of lib/spawni.c.
207 AC_CHECK_HEADERS([paths.h])
208 AC_CHECK_FUNCS([confstr sched_setparam sched_setscheduler setegid seteuid vfork])