Use gnulib's sys/wait.h replacement.
[gnulib.git] / tests / test-posix_spawn1.c
1 /* Test of posix_spawn() function.
2    Copyright (C) 2008 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008.  */
18
19 #include <config.h>
20
21 #include <spawn.h>
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33
34 extern char **environ;
35
36 #ifndef STDIN_FILENO
37 # define STDIN_FILENO 0
38 #endif
39 #ifndef STDOUT_FILENO
40 # define STDOUT_FILENO 1
41 #endif
42 #ifndef STDERR_FILENO
43 # define STDERR_FILENO 2
44 #endif
45
46 #define CHILD_PROGRAM_FILENAME "test-posix_spawn1.sh"
47
48 static int
49 fd_safer (int fd)
50 {
51   if (0 <= fd && fd <= 2)
52     {
53       int f = fd_safer (dup (fd));
54       int e = errno;
55       close (fd);
56       errno = e;
57       fd = f;
58     }
59
60   return fd;
61 }
62
63 int
64 main ()
65 {
66   char *argv[3] = { "/bin/sh", CHILD_PROGRAM_FILENAME, NULL };
67   int ifd[2];
68   sigset_t blocked_signals;
69   sigset_t fatal_signal_set;
70   posix_spawn_file_actions_t actions;
71   bool actions_allocated;
72   posix_spawnattr_t attrs;
73   bool attrs_allocated;
74   int err;
75   pid_t child;
76   int fd;
77   FILE *fp;
78   char line[80];
79   int status;
80   int exitstatus;
81
82   if (pipe (ifd) < 0 || (ifd[0] = fd_safer (ifd[0])) < 0)
83     {
84       perror ("cannot create pipe");
85       exit (1);
86     }
87   sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
88   sigemptyset (&fatal_signal_set);
89   sigaddset (&fatal_signal_set, SIGINT);
90   sigaddset (&fatal_signal_set, SIGTERM);
91   sigaddset (&fatal_signal_set, SIGHUP);
92   sigaddset (&fatal_signal_set, SIGPIPE);
93   sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
94   actions_allocated = false;
95   attrs_allocated = false;
96   if ((err = posix_spawn_file_actions_init (&actions)) != 0
97       || (actions_allocated = true,
98           (err = posix_spawn_file_actions_adddup2 (&actions, ifd[1], STDOUT_FILENO)) != 0
99           || (err = posix_spawn_file_actions_addclose (&actions, ifd[1])) != 0
100           || (err = posix_spawn_file_actions_addclose (&actions, ifd[0])) != 0
101           || (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, "/dev/null", O_RDONLY, 0)) != 0
102           || (err = posix_spawnattr_init (&attrs)) != 0
103           || (attrs_allocated = true,
104               (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
105               || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0)
106           || (err = posix_spawnp (&child, "/bin/sh", &actions, &attrs, argv, environ)) != 0))
107     {
108       if (actions_allocated)
109         posix_spawn_file_actions_destroy (&actions);
110       if (attrs_allocated)
111         posix_spawnattr_destroy (&attrs);
112       sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
113       errno = err;
114       perror ("subprocess failed");
115       exit (1);
116     }
117   posix_spawn_file_actions_destroy (&actions);
118   posix_spawnattr_destroy (&attrs);
119   sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
120   close (ifd[1]);
121   fd = ifd[0];
122   fp = fdopen (fd, "r");
123   if (fp == NULL)
124     {
125       fprintf (stderr, "fdopen() failed\n");
126       exit (1);
127     }
128   if (fread (line, 1, 80, fp) < 12)
129     {
130       fprintf (stderr, "could not read expected output\n");
131       exit (1);
132     }
133   if (memcmp (line, "Halle Potta", 11) != 0)
134     {
135       fprintf (stderr, "read output is not the expected output");
136       exit (1);
137     }
138   fclose (fp);
139   status = 0;
140   while (waitpid (child, &status, 0) != child)
141     ;
142   if (!WIFEXITED (status))
143     {
144       fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
145       exit (1);
146     }
147   exitstatus = WEXITSTATUS (status);
148   if (exitstatus != 0)
149     {
150       fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
151       exit (1);
152     }
153   return 0;
154 }