update nearly all FSF copyright year lists to include 2010
[gnulib.git] / tests / test-posix_spawn3.c
1 /* Test of posix_spawn() function.
2    Copyright (C) 2008, 2009, 2010 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 /* Test whether posix_spawn_file_actions_addopen supports filename arguments
20    that contain special characters such as '*'.  */
21
22 #include <config.h>
23
24 #include <spawn.h>
25
26 #include "signature.h"
27 SIGNATURE_CHECK (posix_spawn, int, (pid_t *, char const *,
28                                     posix_spawn_file_actions_t const *,
29                                     posix_spawnattr_t const *,
30                                     char *const[], char *const[]));
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <signal.h>
34 #include <stdbool.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <sys/types.h>
39 #include <sys/wait.h>
40
41 extern char **environ;
42
43 #define CHILD_PROGRAM_FILENAME "test-posix_spawn3"
44 #define DATA_FILENAME "t!#$%&'()*+,-;=?@[\\]^_`{|}~.tmp"
45
46 static int
47 parent_main (void)
48 {
49   FILE *fp;
50   char *argv[3] = { CHILD_PROGRAM_FILENAME, "-child", NULL };
51   posix_spawn_file_actions_t actions;
52   bool actions_allocated;
53   int err;
54   pid_t child;
55   int status;
56   int exitstatus;
57
58   /* Create a data file with specific contents.  */
59   fp = fopen (DATA_FILENAME, "wb");
60   if (fp == NULL)
61     {
62       perror ("cannot create data file");
63       return 1;
64     }
65   fwrite ("Halle Potta", 1, 11, fp);
66   if (fflush (fp) || fclose (fp))
67     {
68       perror ("cannot prepare data file");
69       return 1;
70     }
71
72   /* Avoid reading from our stdin, as it could block.  */
73   freopen ("/dev/null", "rb", stdin);
74
75   /* Test whether posix_spawn_file_actions_addopen with this file name
76      actually works, but spawning a child that reads from this file.  */
77   actions_allocated = false;
78   if ((err = posix_spawn_file_actions_init (&actions)) != 0
79       || (actions_allocated = true,
80           (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, DATA_FILENAME, O_RDONLY, 0600)) != 0
81           || (err = posix_spawn (&child, CHILD_PROGRAM_FILENAME, &actions, NULL, argv, environ)) != 0))
82     {
83       if (actions_allocated)
84         posix_spawn_file_actions_destroy (&actions);
85       errno = err;
86       perror ("subprocess failed");
87       return 1;
88     }
89   posix_spawn_file_actions_destroy (&actions);
90   status = 0;
91   while (waitpid (child, &status, 0) != child)
92     ;
93   if (!WIFEXITED (status))
94     {
95       fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
96       return 1;
97     }
98   exitstatus = WEXITSTATUS (status);
99   if (exitstatus != 0)
100     {
101       fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
102       return 1;
103     }
104   return 0;
105 }
106
107 static int
108 child_main (void)
109 {
110   char buf[1024];
111
112   /* See if reading from STDIN_FILENO yields the expected contents.  */
113   if (fread (buf, 1, sizeof (buf), stdin) == 11
114       && memcmp (buf, "Halle Potta", 11) == 0)
115     return 0;
116   else
117     return 2;
118 }
119
120 static void
121 cleanup_then_die (int sig)
122 {
123   /* Clean up data file.  */
124   unlink (DATA_FILENAME);
125
126   /* Re-raise the signal and die from it.  */
127   signal (sig, SIG_DFL);
128   raise (sig);
129 }
130
131 int
132 main (int argc, char *argv[])
133 {
134   int exitstatus;
135
136   if (!(argc > 1 && strcmp (argv[1], "-child") == 0))
137     {
138       /* This is the parent process.  */
139       signal (SIGINT, cleanup_then_die);
140       signal (SIGTERM, cleanup_then_die);
141       #ifdef SIGHUP
142       signal (SIGHUP, cleanup_then_die);
143       #endif
144
145       exitstatus = parent_main ();
146     }
147   else
148     {
149       /* This is the child process.  */
150
151       exitstatus = child_main ();
152     }
153   unlink (DATA_FILENAME);
154   return exitstatus;
155 }