New module 'ansi-c++-opt'.
[pspp] / m4 / posix_spawn.m4
1 # posix_spawn.m4 serial 5
2 dnl Copyright (C) 2008-2010 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,
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     gl_POSIX_SPAWN_WORKS
37     case "$gl_cv_func_posix_spawn_works" in
38       *yes)
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], [
46 #include <spawn.h>
47 #if POSIX_SPAWN_SETSCHEDULER
48  POSIX scheduling supported
49 #endif
50 ],
51              [gl_cv_func_spawnattr_setschedpolicy=yes],
52              [gl_cv_func_spawnattr_setschedpolicy=no])
53           ])
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], [
61 #include <spawn.h>
62 #if POSIX_SPAWN_SETSCHEDPARAM
63  POSIX scheduling supported
64 #endif
65 ],
66              [gl_cv_func_spawnattr_setschedparam=yes],
67              [gl_cv_func_spawnattr_setschedparam=no])
68           ])
69         ;;
70       *) REPLACE_POSIX_SPAWN=1 ;;
71     esac
72   else
73     HAVE_POSIX_SPAWN=0
74   fi
75 ])
76
77 dnl Test whether posix_spawn actually works.
78 dnl posix_spawn on AIX 5.3..6.1 has two bugs:
79 dnl 1) When it fails to execute the program, the child process exits with
80 dnl    exit() rather than _exit(), which causes the stdio buffers to be
81 dnl    flushed. Reported by Rainer Tammer.
82 dnl 2) The posix_spawn_file_actions_addopen function does not support file
83 dnl    names that contain a '*'.
84 dnl posix_spawn on AIX 5.3..6.1 has also a third bug: It does not work
85 dnl when POSIX threads are used. But we don't test against this bug here.
86 AC_DEFUN([gl_POSIX_SPAWN_WORKS],
87 [
88   AC_REQUIRE([AC_PROG_CC])
89   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
90   AC_CACHE_CHECK([whether posix_spawn works], [gl_cv_func_posix_spawn_works],
91     [if test $cross_compiling = no; then
92        AC_LINK_IFELSE([AC_LANG_SOURCE([[
93 #include <errno.h>
94 #include <fcntl.h>
95 #include <signal.h>
96 #include <spawn.h>
97 #include <stdbool.h>
98 #include <stdio.h>
99 #include <stdlib.h>
100 #include <string.h>
101 #include <unistd.h>
102 #include <sys/types.h>
103 #include <sys/wait.h>
104
105 extern char **environ;
106
107 #ifndef STDIN_FILENO
108 # define STDIN_FILENO 0
109 #endif
110 #ifndef STDOUT_FILENO
111 # define STDOUT_FILENO 1
112 #endif
113 #ifndef STDERR_FILENO
114 # define STDERR_FILENO 2
115 #endif
116
117 #ifndef WTERMSIG
118 # define WTERMSIG(x) ((x) & 0x7f)
119 #endif
120 #ifndef WIFEXITED
121 # define WIFEXITED(x) (WTERMSIG (x) == 0)
122 #endif
123 #ifndef WEXITSTATUS
124 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
125 #endif
126
127 #define CHILD_PROGRAM_FILENAME "/non/exist/ent"
128
129 static int
130 fd_safer (int fd)
131 {
132   if (0 <= fd && fd <= 2)
133     {
134       int f = fd_safer (dup (fd));
135       int e = errno;
136       close (fd);
137       errno = e;
138       fd = f;
139     }
140
141   return fd;
142 }
143
144 int
145 main ()
146 {
147   char *argv[2] = { CHILD_PROGRAM_FILENAME, NULL };
148   int ofd[2];
149   sigset_t blocked_signals;
150   sigset_t fatal_signal_set;
151   posix_spawn_file_actions_t actions;
152   bool actions_allocated;
153   posix_spawnattr_t attrs;
154   bool attrs_allocated;
155   int err;
156   pid_t child;
157   int status;
158   int exitstatus;
159
160   setvbuf (stdout, NULL, _IOFBF, 0);
161   puts ("This should be seen only once.");
162   if (pipe (ofd) < 0 || (ofd[1] = fd_safer (ofd[1])) < 0)
163     {
164       perror ("cannot create pipe");
165       exit (1);
166     }
167   sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
168   sigemptyset (&fatal_signal_set);
169   sigaddset (&fatal_signal_set, SIGINT);
170   sigaddset (&fatal_signal_set, SIGTERM);
171   sigaddset (&fatal_signal_set, SIGHUP);
172   sigaddset (&fatal_signal_set, SIGPIPE);
173   sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
174   actions_allocated = false;
175   attrs_allocated = false;
176   if ((err = posix_spawn_file_actions_init (&actions)) != 0
177       || (actions_allocated = true,
178           (err = posix_spawn_file_actions_adddup2 (&actions, ofd[0], STDIN_FILENO)) != 0
179           || (err = posix_spawn_file_actions_addclose (&actions, ofd[0])) != 0
180           || (err = posix_spawn_file_actions_addclose (&actions, ofd[1])) != 0
181           || (err = posix_spawnattr_init (&attrs)) != 0
182           || (attrs_allocated = true,
183               (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
184               || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0)
185           || (err = posix_spawnp (&child, CHILD_PROGRAM_FILENAME, &actions, &attrs, argv, environ)) != 0))
186     {
187       if (actions_allocated)
188         posix_spawn_file_actions_destroy (&actions);
189       if (attrs_allocated)
190         posix_spawnattr_destroy (&attrs);
191       sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
192       if (err == ENOENT)
193         return 0;
194       else
195         {
196           errno = err;
197           perror ("subprocess failed");
198           exit (1);
199         }
200     }
201   posix_spawn_file_actions_destroy (&actions);
202   posix_spawnattr_destroy (&attrs);
203   sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
204   close (ofd[0]);
205   close (ofd[1]);
206   status = 0;
207   while (waitpid (child, &status, 0) != child)
208     ;
209   if (!WIFEXITED (status))
210     {
211       fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
212       exit (1);
213     }
214   exitstatus = WEXITSTATUS (status);
215   if (exitstatus != 127)
216     {
217       fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
218       exit (1);
219     }
220   return 0;
221 }
222 ]])],
223          [if test -s conftest$ac_exeext \
224              && ./conftest$ac_exeext > conftest.out \
225              && echo 'This should be seen only once.' > conftest.ok \
226              && cmp conftest.out conftest.ok > /dev/null; then
227             gl_cv_func_posix_spawn_works=yes
228           else
229             gl_cv_func_posix_spawn_works=no
230           fi],
231          [gl_cv_func_posix_spawn_works=no])
232        if test $gl_cv_func_posix_spawn_works = yes; then
233          AC_RUN_IFELSE([AC_LANG_SOURCE([[
234 /* Test whether posix_spawn_file_actions_addopen supports filename arguments
235    that contain special characters such as '*'.  */
236
237 #include <errno.h>
238 #include <fcntl.h>
239 #include <signal.h>
240 #include <spawn.h>
241 #include <stdbool.h>
242 #include <stdio.h>
243 #include <string.h>
244 #include <unistd.h>
245 #include <sys/types.h>
246 #include <sys/wait.h>
247
248 extern char **environ;
249
250 #ifndef STDIN_FILENO
251 # define STDIN_FILENO 0
252 #endif
253 #ifndef STDOUT_FILENO
254 # define STDOUT_FILENO 1
255 #endif
256 #ifndef STDERR_FILENO
257 # define STDERR_FILENO 2
258 #endif
259
260 #ifndef WTERMSIG
261 # define WTERMSIG(x) ((x) & 0x7f)
262 #endif
263 #ifndef WIFEXITED
264 # define WIFEXITED(x) (WTERMSIG (x) == 0)
265 #endif
266 #ifndef WEXITSTATUS
267 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
268 #endif
269
270 #define CHILD_PROGRAM_FILENAME "conftest"
271 #define DATA_FILENAME "conftest%=*#?"
272
273 static int
274 parent_main (void)
275 {
276   FILE *fp;
277   char *argv[3] = { CHILD_PROGRAM_FILENAME, "-child", NULL };
278   posix_spawn_file_actions_t actions;
279   bool actions_allocated;
280   int err;
281   pid_t child;
282   int status;
283   int exitstatus;
284
285   /* Create a data file with specific contents.  */
286   fp = fopen (DATA_FILENAME, "wb");
287   if (fp == NULL)
288     {
289       perror ("cannot create data file");
290       return 1;
291     }
292   fwrite ("Halle Potta", 1, 11, fp);
293   if (fflush (fp) || fclose (fp))
294     {
295       perror ("cannot prepare data file");
296       return 1;
297     }
298
299   /* Avoid reading from our stdin, as it could block.  */
300   freopen ("/dev/null", "rb", stdin);
301
302   /* Test whether posix_spawn_file_actions_addopen with this file name
303      actually works, but spawning a child that reads from this file.  */
304   actions_allocated = false;
305   if ((err = posix_spawn_file_actions_init (&actions)) != 0
306       || (actions_allocated = true,
307           (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, DATA_FILENAME, O_RDONLY, 0600)) != 0
308           || (err = posix_spawn (&child, CHILD_PROGRAM_FILENAME, &actions, NULL, argv, environ)) != 0))
309     {
310       if (actions_allocated)
311         posix_spawn_file_actions_destroy (&actions);
312       errno = err;
313       perror ("subprocess failed");
314       return 1;
315     }
316   posix_spawn_file_actions_destroy (&actions);
317   status = 0;
318   while (waitpid (child, &status, 0) != child)
319     ;
320   if (!WIFEXITED (status))
321     {
322       fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
323       return 1;
324     }
325   exitstatus = WEXITSTATUS (status);
326   if (exitstatus != 0)
327     {
328       fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
329       return 1;
330     }
331   return 0;
332 }
333
334 static int
335 child_main (void)
336 {
337   char buf[1024];
338
339   /* See if reading from STDIN_FILENO yields the expected contents.  */
340   if (fread (buf, 1, sizeof (buf), stdin) == 11
341       && memcmp (buf, "Halle Potta", 11) == 0)
342     return 0;
343   else
344     return 2;
345 }
346
347 static void
348 cleanup_then_die (int sig)
349 {
350   /* Clean up data file.  */
351   unlink (DATA_FILENAME);
352
353   /* Re-raise the signal and die from it.  */
354   signal (sig, SIG_DFL);
355   raise (sig);
356 }
357
358 int
359 main (int argc, char *argv[])
360 {
361   int exitstatus;
362
363   if (!(argc > 1 && strcmp (argv[1], "-child") == 0))
364     {
365       /* This is the parent process.  */
366       signal (SIGINT, cleanup_then_die);
367       signal (SIGTERM, cleanup_then_die);
368       #ifdef SIGHUP
369       signal (SIGHUP, cleanup_then_die);
370       #endif
371
372       exitstatus = parent_main ();
373     }
374   else
375     {
376       /* This is the child process.  */
377
378       exitstatus = child_main ();
379     }
380   unlink (DATA_FILENAME);
381   return exitstatus;
382 }
383 ]])],
384            [],
385            [gl_cv_func_posix_spawn_works=no])
386        fi
387      else
388        case "$host_os" in
389          aix*) gl_cv_func_posix_spawn_works="guessing no";;
390          *)    gl_cv_func_posix_spawn_works="guessing yes";;
391        esac
392      fi
393     ])
394 ])
395
396 AC_DEFUN([gl_POSIX_SPAWN_INTERNAL],
397 [
398   AC_LIBOBJ([spawni])
399   dnl Prerequisites of lib/spawni.c.
400   AC_CHECK_HEADERS([paths.h])
401   AC_CHECK_FUNCS([confstr sched_setparam sched_setscheduler setegid seteuid vfork])
402 ])