Avoid the broken posix_spawn function on AIX 5.3 and 6.1.
[gnulib.git] / m4 / posix_spawn.m4
1 # posix_spawn.m4 serial 2
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.
6
7 dnl Tests whether the entire posix_spawn facility is available.
8 AC_DEFUN([gl_POSIX_SPAWN],
9 [
10   AC_REQUIRE([gl_POSIX_SPAWN_BODY])
11 ])
12
13 AC_DEFUN([gl_POSIX_SPAWN_BODY],
14 [
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
40     gl_POSIX_SPAWN_WORKS
41     case "$gl_cv_func_posix_spawn_works" in
42       *yes) ;;
43       *) REPLACE_POSIX_SPAWN=1 ;;
44     esac
45   else
46     HAVE_POSIX_SPAWN=0
47   fi
48 ])
49
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],
57 [
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([[
63 #include <errno.h>
64 #include <fcntl.h>
65 #include <signal.h>
66 #include <spawn.h>
67 #include <stdbool.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <unistd.h>
72 #include <sys/types.h>
73 #include <sys/wait.h>
74
75 extern char **environ;
76
77 #ifndef STDIN_FILENO
78 # define STDIN_FILENO 0
79 #endif
80 #ifndef STDOUT_FILENO
81 # define STDOUT_FILENO 1
82 #endif
83 #ifndef STDERR_FILENO
84 # define STDERR_FILENO 2
85 #endif
86
87 #ifndef WTERMSIG
88 # define WTERMSIG(x) ((x) & 0x7f)
89 #endif
90 #ifndef WCOREDUMP
91 # define WCOREDUMP(x) ((x) & 0x80)
92 #endif
93 #ifndef WEXITSTATUS
94 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
95 #endif
96 #ifndef WIFSIGNALED
97 # define WIFSIGNALED(x) (WTERMSIG (x) != 0 && WTERMSIG(x) != 0x7f)
98 #endif
99 #ifndef WIFEXITED
100 # define WIFEXITED(x) (WTERMSIG (x) == 0)
101 #endif
102 #ifndef WIFSTOPPED
103 # define WIFSTOPPED(x) (WTERMSIG (x) == 0x7f)
104 #endif
105
106 #define CHILD_PROGRAM_FILENAME "/non/exist/ent"
107
108 static int
109 fd_safer (int fd)
110 {
111   if (0 <= fd && fd <= 2)
112     {
113       int f = fd_safer (dup (fd));
114       int e = errno;
115       close (fd);
116       errno = e;
117       fd = f;
118     }
119
120   return fd;
121 }  
122 ]],
123 dnl Now comes the main() function.
124 [[
125   char *argv[2] = { CHILD_PROGRAM_FILENAME, NULL };
126   int ofd[2];
127   sigset_t blocked_signals;
128   sigset_t fatal_signal_set;
129   posix_spawn_file_actions_t actions;
130   bool actions_allocated;
131   posix_spawnattr_t attrs;
132   bool attrs_allocated;
133   int err;
134   pid_t child;
135   int status;
136   int exitstatus;
137
138   setvbuf (stdout, NULL, _IOFBF, 0);
139   puts ("This should be seen only once.");
140   if (pipe (ofd) < 0 || (ofd[1] = fd_safer (ofd[1])) < 0)
141     {
142       perror ("cannot create pipe");
143       exit (1);
144     }
145   sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
146   sigemptyset (&fatal_signal_set);
147   sigaddset (&fatal_signal_set, SIGINT);
148   sigaddset (&fatal_signal_set, SIGTERM);
149   sigaddset (&fatal_signal_set, SIGHUP);
150   sigaddset (&fatal_signal_set, SIGPIPE);
151   sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
152   actions_allocated = false;
153   attrs_allocated = false;
154   if ((err = posix_spawn_file_actions_init (&actions)) != 0
155       || (actions_allocated = true,
156           (err = posix_spawn_file_actions_adddup2 (&actions, ofd[0], STDIN_FILENO)) != 0
157           || (err = posix_spawn_file_actions_addclose (&actions, ofd[0])) != 0
158           || (err = posix_spawn_file_actions_addclose (&actions, ofd[1])) != 0
159           || (err = posix_spawnattr_init (&attrs)) != 0
160           || (attrs_allocated = true,
161               (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
162               || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0)
163           || (err = posix_spawnp (&child, CHILD_PROGRAM_FILENAME, &actions, &attrs, argv, environ)) != 0))
164     {
165       if (actions_allocated)
166         posix_spawn_file_actions_destroy (&actions);
167       if (attrs_allocated)
168         posix_spawnattr_destroy (&attrs);
169       sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
170       errno = err;
171       perror ("subprocess failed");
172       exit (1);
173     }
174   posix_spawn_file_actions_destroy (&actions);
175   posix_spawnattr_destroy (&attrs);
176   sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
177   close (ofd[0]);
178   close (ofd[1]);
179   status = 0;
180   while (waitpid (child, &status, 0) != child)
181     ;
182   if (!WIFEXITED (status))
183     {
184       fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
185       exit (1);
186     }
187   exitstatus = WEXITSTATUS (status);
188   if (exitstatus != 127)
189     {
190       fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
191       exit (1);
192     }
193 ]])],
194          [if test -s conftest$ac_exeext \
195              && ./conftest$ac_exeext > conftest.out \
196              && echo 'This should be seen only once.' > conftest.ok \
197              && cmp conftest.out conftest.ok > /dev/null; then
198             gl_cv_func_posix_spawn_works=yes
199           else
200             gl_cv_func_posix_spawn_works=no
201           fi],
202          [gl_cv_func_posix_spawn_works=no])
203      else
204        case "$host_os" in
205          aix*) gl_cv_func_posix_spawn_works="guessing no";;
206          *)    gl_cv_func_posix_spawn_works="guessing yes";;
207        esac
208      fi
209     ])
210 ])
211
212 AC_DEFUN([gl_POSIX_SPAWN_INTERNAL],
213 [
214   AC_LIBOBJ([spawni])
215   dnl Prerequisites of lib/spawni.c.
216   AC_CHECK_HEADERS([paths.h])
217   AC_CHECK_FUNCS([confstr sched_setparam sched_setscheduler setegid seteuid vfork])
218 ])