Use spaces for indentation, not tabs.
[gnulib.git] / tests / test-posix_spawn1.c
1 /* Test of posix_spawn() function.
2    Copyright (C) 2008, 2009 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 #define CHILD_PROGRAM_FILENAME "test-posix_spawn1.sh"
37
38 static int
39 fd_safer (int fd)
40 {
41   if (0 <= fd && fd <= 2)
42     {
43       int f = fd_safer (dup (fd));
44       int e = errno;
45       close (fd);
46       errno = e;
47       fd = f;
48     }
49
50   return fd;
51 }
52
53 int
54 main ()
55 {
56   char *argv[3] = { "/bin/sh", CHILD_PROGRAM_FILENAME, NULL };
57   int ifd[2];
58   sigset_t blocked_signals;
59   sigset_t fatal_signal_set;
60   posix_spawn_file_actions_t actions;
61   bool actions_allocated;
62   posix_spawnattr_t attrs;
63   bool attrs_allocated;
64   int err;
65   pid_t child;
66   int fd;
67   FILE *fp;
68   char line[80];
69   int status;
70   int exitstatus;
71
72   if (pipe (ifd) < 0 || (ifd[0] = fd_safer (ifd[0])) < 0)
73     {
74       perror ("cannot create pipe");
75       exit (1);
76     }
77   sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
78   sigemptyset (&fatal_signal_set);
79   sigaddset (&fatal_signal_set, SIGINT);
80   sigaddset (&fatal_signal_set, SIGTERM);
81   sigaddset (&fatal_signal_set, SIGHUP);
82   sigaddset (&fatal_signal_set, SIGPIPE);
83   sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
84   actions_allocated = false;
85   attrs_allocated = false;
86   if ((err = posix_spawn_file_actions_init (&actions)) != 0
87       || (actions_allocated = true,
88           (err = posix_spawn_file_actions_adddup2 (&actions, ifd[1], STDOUT_FILENO)) != 0
89           || (err = posix_spawn_file_actions_addclose (&actions, ifd[1])) != 0
90           || (err = posix_spawn_file_actions_addclose (&actions, ifd[0])) != 0
91           || (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, "/dev/null", O_RDONLY, 0)) != 0
92           || (err = posix_spawnattr_init (&attrs)) != 0
93           || (attrs_allocated = true,
94               (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
95               || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0)
96           || (err = posix_spawnp (&child, "/bin/sh", &actions, &attrs, argv, environ)) != 0))
97     {
98       if (actions_allocated)
99         posix_spawn_file_actions_destroy (&actions);
100       if (attrs_allocated)
101         posix_spawnattr_destroy (&attrs);
102       sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
103       errno = err;
104       perror ("subprocess failed");
105       exit (1);
106     }
107   posix_spawn_file_actions_destroy (&actions);
108   posix_spawnattr_destroy (&attrs);
109   sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
110   close (ifd[1]);
111   fd = ifd[0];
112   fp = fdopen (fd, "r");
113   if (fp == NULL)
114     {
115       fprintf (stderr, "fdopen() failed\n");
116       exit (1);
117     }
118   if (fread (line, 1, 80, fp) < 12)
119     {
120       fprintf (stderr, "could not read expected output\n");
121       exit (1);
122     }
123   if (memcmp (line, "Halle Potta", 11) != 0)
124     {
125       fprintf (stderr, "read output is not the expected output");
126       exit (1);
127     }
128   fclose (fp);
129   status = 0;
130   while (waitpid (child, &status, 0) != child)
131     ;
132   if (!WIFEXITED (status))
133     {
134       fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
135       exit (1);
136     }
137   exitstatus = WEXITSTATUS (status);
138   if (exitstatus != 0)
139     {
140       fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
141       exit (1);
142     }
143   return 0;
144 }