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