Remove trailing spaces.
[gnulib.git] / tests / test-posix_spawn2.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 #ifndef WTERMSIG
47 # define WTERMSIG(x) ((x) & 0x7f)
48 #endif
49 #ifndef WCOREDUMP
50 # define WCOREDUMP(x) ((x) & 0x80)
51 #endif
52 #ifndef WEXITSTATUS
53 # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
54 #endif
55 #ifndef WIFSIGNALED
56 # define WIFSIGNALED(x) (WTERMSIG (x) != 0 && WTERMSIG(x) != 0x7f)
57 #endif
58 #ifndef WIFEXITED
59 # define WIFEXITED(x) (WTERMSIG (x) == 0)
60 #endif
61 #ifndef WIFSTOPPED
62 # define WIFSTOPPED(x) (WTERMSIG (x) == 0x7f)
63 #endif
64
65 #define CHILD_PROGRAM_FILENAME "test-posix_spawn2.sh"
66
67 static int
68 fd_safer (int fd)
69 {
70   if (0 <= fd && fd <= 2)
71     {
72       int f = fd_safer (dup (fd));
73       int e = errno;
74       close (fd);
75       errno = e;
76       fd = f;
77     }
78
79   return fd;
80 }
81
82 int
83 main ()
84 {
85   char *argv[3] = { "/bin/sh", CHILD_PROGRAM_FILENAME, NULL };
86   int ofd[2];
87   sigset_t blocked_signals;
88   sigset_t fatal_signal_set;
89   posix_spawn_file_actions_t actions;
90   bool actions_allocated;
91   posix_spawnattr_t attrs;
92   bool attrs_allocated;
93   int err;
94   pid_t child;
95   int fd;
96   FILE *fp;
97   int written;
98   int status;
99   int exitstatus;
100
101   if (pipe (ofd) < 0 || (ofd[1] = fd_safer (ofd[1])) < 0)
102     {
103       perror ("cannot create pipe");
104       exit (1);
105     }
106   sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
107   sigemptyset (&fatal_signal_set);
108   sigaddset (&fatal_signal_set, SIGINT);
109   sigaddset (&fatal_signal_set, SIGTERM);
110   sigaddset (&fatal_signal_set, SIGHUP);
111   sigaddset (&fatal_signal_set, SIGPIPE);
112   sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
113   actions_allocated = false;
114   attrs_allocated = false;
115   if ((err = posix_spawn_file_actions_init (&actions)) != 0
116       || (actions_allocated = true,
117           (err = posix_spawn_file_actions_adddup2 (&actions, ofd[0], STDIN_FILENO)) != 0
118           || (err = posix_spawn_file_actions_addclose (&actions, ofd[0])) != 0
119           || (err = posix_spawn_file_actions_addclose (&actions, ofd[1])) != 0
120           || (err = posix_spawnattr_init (&attrs)) != 0
121           || (attrs_allocated = true,
122               (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
123               || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0)
124           || (err = posix_spawnp (&child, "/bin/sh", &actions, &attrs, argv, environ)) != 0))
125     {
126       if (actions_allocated)
127         posix_spawn_file_actions_destroy (&actions);
128       if (attrs_allocated)
129         posix_spawnattr_destroy (&attrs);
130       sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
131       errno = err;
132       perror ("subprocess failed");
133       exit (1);
134     }
135   posix_spawn_file_actions_destroy (&actions);
136   posix_spawnattr_destroy (&attrs);
137   sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
138   close (ofd[0]);
139   fd = ofd[1];
140   fp = fdopen (fd, "w");
141   if (fp == NULL)
142     {
143       fprintf (stderr, "fdopen() failed\n");
144       exit (1);
145     }
146   written = fwrite ("Halle Potta\n", 1, 12, fp);
147   if (written < 12)
148     {
149       fprintf (stderr, "could not write input\n");
150       exit (1);
151     }
152   fclose (fp);
153   status = 0;
154   while (waitpid (child, &status, 0) != child)
155     ;
156   if (!WIFEXITED (status))
157     {
158       fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
159       exit (1);
160     }
161   exitstatus = WEXITSTATUS (status);
162   if (exitstatus != 0)
163     {
164       fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
165       exit (1);
166     }
167   return 0;
168 }