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