mbsnrtowcs: Work around Solaris 11.4 bug.
[gnulib.git] / m4 / posix_spawn.m4
blobf656cfcdf1da1c2a2f5230d0fc047cf1b97b2ac5
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.
42          REPLACE_POSIX_SPAWN=1
43        fi
44       ])
45     if test $REPLACE_POSIX_SPAWN = 0; then
46       gl_POSIX_SPAWN_WORKS
47       case "$gl_cv_func_posix_spawn_works" in
48         *yes)
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], [
56 #include <spawn.h>
57 #if POSIX_SPAWN_SETSCHEDULER
58  POSIX scheduling supported
59 #endif
61                [gl_cv_func_spawnattr_setschedpolicy=yes],
62                [gl_cv_func_spawnattr_setschedpolicy=no])
63             ])
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], [
71 #include <spawn.h>
72 #if POSIX_SPAWN_SETSCHEDPARAM
73  POSIX scheduling supported
74 #endif
76                [gl_cv_func_spawnattr_setschedparam=yes],
77                [gl_cv_func_spawnattr_setschedparam=no])
78             ])
79           ;;
80         *) REPLACE_POSIX_SPAWN=1 ;;
81       esac
82     fi
83   fi
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.])
87   fi
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([[
106 #include <errno.h>
107 #include <fcntl.h>
108 #include <signal.h>
109 #include <spawn.h>
110 #include <stdbool.h>
111 #include <stdio.h>
112 #include <stdlib.h>
113 #include <string.h>
114 #include <unistd.h>
115 #include <sys/types.h>
116 #include <sys/wait.h>
118 extern char **environ;
120 #ifndef STDIN_FILENO
121 # define STDIN_FILENO 0
122 #endif
123 #ifndef STDOUT_FILENO
124 # define STDOUT_FILENO 1
125 #endif
126 #ifndef STDERR_FILENO
127 # define STDERR_FILENO 2
128 #endif
130 #ifndef WTERMSIG
131 # define WTERMSIG(x) ((x) & 0x7f)
132 #endif
133 #ifndef WIFEXITED
134 # define WIFEXITED(x) (WTERMSIG (x) == 0)
135 #endif
136 #ifndef WEXITSTATUS
137 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
138 #endif
140 #define CHILD_PROGRAM_FILENAME "/non/exist/ent"
142 static int
143 fd_safer (int fd)
145   if (0 <= fd && fd <= 2)
146     {
147       int f = fd_safer (dup (fd));
148       int e = errno;
149       close (fd);
150       errno = e;
151       fd = f;
152     }
154   return fd;
158 main ()
160   char *argv[2] = { CHILD_PROGRAM_FILENAME, NULL };
161   int ofd[2];
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;
168   int err;
169   pid_t child;
170   int status;
171   int exitstatus;
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)
176     {
177       perror ("cannot create pipe");
178       exit (1);
179     }
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))
199     {
200       if (actions_allocated)
201         posix_spawn_file_actions_destroy (&actions);
202       if (attrs_allocated)
203         posix_spawnattr_destroy (&attrs);
204       sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
205       if (err == ENOENT)
206         return 0;
207       else
208         {
209           errno = err;
210           perror ("subprocess failed");
211           exit (1);
212         }
213     }
214   posix_spawn_file_actions_destroy (&actions);
215   posix_spawnattr_destroy (&attrs);
216   sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
217   close (ofd[0]);
218   close (ofd[1]);
219   status = 0;
220   while (waitpid (child, &status, 0) != child)
221     ;
222   if (!WIFEXITED (status))
223     {
224       fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
225       exit (1);
226     }
227   exitstatus = WEXITSTATUS (status);
228   if (exitstatus != 127)
229     {
230       fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
231       exit (1);
232     }
233   return 0;
235 ]])],
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
241           else
242             gl_cv_func_posix_spawn_works=no
243           fi],
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 '*'.  */
250 #include <errno.h>
251 #include <fcntl.h>
252 #include <signal.h>
253 #include <spawn.h>
254 #include <stdbool.h>
255 #include <stdio.h>
256 #include <string.h>
257 #include <unistd.h>
258 #include <sys/types.h>
259 #include <sys/wait.h>
261 extern char **environ;
263 #ifndef STDIN_FILENO
264 # define STDIN_FILENO 0
265 #endif
266 #ifndef STDOUT_FILENO
267 # define STDOUT_FILENO 1
268 #endif
269 #ifndef STDERR_FILENO
270 # define STDERR_FILENO 2
271 #endif
273 #ifndef WTERMSIG
274 # define WTERMSIG(x) ((x) & 0x7f)
275 #endif
276 #ifndef WIFEXITED
277 # define WIFEXITED(x) (WTERMSIG (x) == 0)
278 #endif
279 #ifndef WEXITSTATUS
280 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
281 #endif
283 #define CHILD_PROGRAM_FILENAME "conftest"
284 #define DATA_FILENAME "conftest%=*#?"
286 static int
287 parent_main (void)
289   FILE *fp;
290   char *argv[3] = { CHILD_PROGRAM_FILENAME, "-child", NULL };
291   posix_spawn_file_actions_t actions;
292   bool actions_allocated;
293   int err;
294   pid_t child;
295   int status;
296   int exitstatus;
298   /* Create a data file with specific contents.  */
299   fp = fopen (DATA_FILENAME, "wb");
300   if (fp == NULL)
301     {
302       perror ("cannot create data file");
303       return 1;
304     }
305   fwrite ("Halle Potta", 1, 11, fp);
306   if (fflush (fp) || fclose (fp))
307     {
308       perror ("cannot prepare data file");
309       return 2;
310     }
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))
322     {
323       if (actions_allocated)
324         posix_spawn_file_actions_destroy (&actions);
325       errno = err;
326       perror ("subprocess failed");
327       return 3;
328     }
329   posix_spawn_file_actions_destroy (&actions);
330   status = 0;
331   while (waitpid (child, &status, 0) != child)
332     ;
333   if (!WIFEXITED (status))
334     {
335       fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
336       return 4;
337     }
338   exitstatus = WEXITSTATUS (status);
339   if (exitstatus != 0)
340     {
341       fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
342       return 5;
343     }
344   return 0;
347 static int
348 child_main (void)
350   char buf[1024];
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)
355     return 0;
356   else
357     return 8;
360 static void
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);
368   raise (sig);
372 main (int argc, char *argv[])
374   int exitstatus;
376   if (!(argc > 1 && strcmp (argv[1], "-child") == 0))
377     {
378       /* This is the parent process.  */
379       signal (SIGINT, cleanup_then_die);
380       signal (SIGTERM, cleanup_then_die);
381       #ifdef SIGHUP
382       signal (SIGHUP, cleanup_then_die);
383       #endif
385       exitstatus = parent_main ();
386     }
387   else
388     {
389       /* This is the child process.  */
391       exitstatus = child_main ();
392     }
393   unlink (DATA_FILENAME);
394   return exitstatus;
396 ]])],
397            [],
398            [gl_cv_func_posix_spawn_works=no])
399        fi
400      else
401        case "$host_os" in
402          aix*) gl_cv_func_posix_spawn_works="guessing no";;
403          *)    gl_cv_func_posix_spawn_works="guessing yes";;
404        esac
405      fi
406     ])
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
421   gl_POSIX_SPAWN
422   if test $REPLACE_POSIX_SPAWN = 1; then
423     REPLACE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSE=1
424   else
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],
429       [AC_RUN_IFELSE(
430          [AC_LANG_SOURCE([[
431 #include <spawn.h>
432 int main ()
434   posix_spawn_file_actions_t actions;
435   if (posix_spawn_file_actions_init (&actions) != 0)
436     return 1;
437   if (posix_spawn_file_actions_addclose (&actions, 10000000) == 0)
438     return 2;
439   return 0;
440 }]])],
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.
444           case "$host_os" in
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" ;;
449           esac
450          ])
451       ])
452     case "$gl_cv_func_posix_spawn_file_actions_addclose_works" in
453       *yes) ;;
454       *) REPLACE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSE=1 ;;
455     esac
456   fi
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
464   gl_POSIX_SPAWN
465   if test $REPLACE_POSIX_SPAWN = 1; then
466     REPLACE_POSIX_SPAWN_FILE_ACTIONS_ADDDUP2=1
467   else
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],
472       [AC_RUN_IFELSE(
473          [AC_LANG_SOURCE([[
474 #include <spawn.h>
475 int main ()
477   posix_spawn_file_actions_t actions;
478   if (posix_spawn_file_actions_init (&actions) != 0)
479     return 1;
480   if (posix_spawn_file_actions_adddup2 (&actions, 10000000, 2) == 0)
481     return 2;
482   return 0;
483 }]])],
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.
487           case "$host_os" in
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";;
492           esac
493          ])
494       ])
495     case "$gl_cv_func_posix_spawn_file_actions_adddup2_works" in
496       *yes) ;;
497       *) REPLACE_POSIX_SPAWN_FILE_ACTIONS_ADDDUP2=1 ;;
498     esac
499   fi
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
507   gl_POSIX_SPAWN
508   if test $REPLACE_POSIX_SPAWN = 1; then
509     REPLACE_POSIX_SPAWN_FILE_ACTIONS_ADDOPEN=1
510   else
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],
515       [AC_RUN_IFELSE(
516          [AC_LANG_SOURCE([[
517 #include <spawn.h>
518 #include <fcntl.h>
519 int main ()
521   posix_spawn_file_actions_t actions;
522   if (posix_spawn_file_actions_init (&actions) != 0)
523     return 1;
524   if (posix_spawn_file_actions_addopen (&actions, 10000000, "foo", 0, O_RDONLY)
525       == 0)
526     return 2;
527   return 0;
528 }]])],
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.
532           case "$host_os" in
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";;
537           esac
538          ])
539       ])
540     case "$gl_cv_func_posix_spawn_file_actions_addopen_works" in
541       *yes) ;;
542       *) REPLACE_POSIX_SPAWN_FILE_ACTIONS_ADDOPEN=1 ;;
543     esac
544   fi