Add sysdeps/ieee754/soft-fp.
[glibc.git] / posix / tst-spawn.c
blob4e5e76351cd4e43204b6a4703cb37b165441eaaa
1 /* Tests for spawn.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <errno.h>
21 #include <error.h>
22 #include <fcntl.h>
23 #include <spawn.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <wait.h>
27 #include <sys/param.h>
28 #include <support/check.h>
29 #include <support/xunistd.h>
32 /* Nonzero if the program gets called via `exec'. */
33 static int restart;
36 #define CMDLINE_OPTIONS \
37 { "restart", no_argument, &restart, 1 },
39 /* Prototype for our test function. */
40 extern void do_prepare (int argc, char *argv[]);
41 extern int do_test (int argc, char *argv[]);
43 /* We have a preparation function. */
44 #define PREPARE do_prepare
46 #include "../test-skeleton.c"
49 /* Name of the temporary files. */
50 static char *name1;
51 static char *name2;
52 static char *name3;
54 /* Descriptors for the temporary files. */
55 static int temp_fd1 = -1;
56 static int temp_fd2 = -1;
57 static int temp_fd3 = -1;
59 /* The contents of our files. */
60 static const char fd1string[] = "This file should get closed";
61 static const char fd2string[] = "This file should stay opened";
62 static const char fd3string[] = "This file will be opened";
65 /* We have a preparation function. */
66 void
67 do_prepare (int argc, char *argv[])
69 /* We must not open any files in the restart case. */
70 if (restart)
71 return;
73 temp_fd1 = create_temp_file ("spawn", &name1);
74 temp_fd2 = create_temp_file ("spawn", &name2);
75 temp_fd3 = create_temp_file ("spawn", &name3);
76 if (temp_fd1 < 0 || temp_fd2 < 0 || temp_fd3 < 0)
77 exit (1);
81 static int
82 handle_restart (const char *fd1s, const char *fd2s, const char *fd3s,
83 const char *fd4s, const char *name)
85 char buf[100];
86 int fd1;
87 int fd2;
88 int fd3;
89 int fd4;
91 /* First get the descriptors. */
92 fd1 = atol (fd1s);
93 fd2 = atol (fd2s);
94 fd3 = atol (fd3s);
95 fd4 = atol (fd4s);
97 /* Sanity check. */
98 if (fd1 == fd2)
99 error (EXIT_FAILURE, 0, "value of fd1 and fd2 is the same");
100 if (fd1 == fd3)
101 error (EXIT_FAILURE, 0, "value of fd1 and fd3 is the same");
102 if (fd1 == fd4)
103 error (EXIT_FAILURE, 0, "value of fd1 and fd4 is the same");
104 if (fd2 == fd3)
105 error (EXIT_FAILURE, 0, "value of fd2 and fd3 is the same");
106 if (fd2 == fd4)
107 error (EXIT_FAILURE, 0, "value of fd2 and fd4 is the same");
108 if (fd3 == fd4)
109 error (EXIT_FAILURE, 0, "value of fd3 and fd4 is the same");
111 /* First the easy part: read from the file descriptor which is
112 supposed to be open. */
113 if (lseek (fd2, 0, SEEK_CUR) != strlen (fd2string))
114 error (EXIT_FAILURE, errno, "file 2 not in right position");
115 /* The duped descriptor must have the same position. */
116 if (lseek (fd4, 0, SEEK_CUR) != strlen (fd2string))
117 error (EXIT_FAILURE, errno, "file 4 not in right position");
118 if (lseek (fd2, 0, SEEK_SET) != 0)
119 error (EXIT_FAILURE, 0, "cannot reset position in file 2");
120 if (lseek (fd4, 0, SEEK_CUR) != 0)
121 error (EXIT_FAILURE, errno, "file 4 not set back, too");
122 if (read (fd2, buf, sizeof buf) != strlen (fd2string))
123 error (EXIT_FAILURE, 0, "cannot read file 2");
124 if (memcmp (fd2string, buf, strlen (fd2string)) != 0)
125 error (EXIT_FAILURE, 0, "file 2 does not match");
127 /* Now read from the third file. */
128 if (read (fd3, buf, sizeof buf) != strlen (fd3string))
129 error (EXIT_FAILURE, 0, "cannot read file 3");
130 if (memcmp (fd3string, buf, strlen (fd3string)) != 0)
131 error (EXIT_FAILURE, 0, "file 3 does not match");
132 /* Try to write to the file. This should not be allowed. */
133 if (write (fd3, "boo!", 4) != -1 || errno != EBADF)
134 error (EXIT_FAILURE, 0, "file 3 is writable");
136 /* Now try to read the first file. First make sure it is not opened. */
137 if (lseek (fd1, 0, SEEK_CUR) != (off_t) -1 || errno != EBADF)
138 error (EXIT_FAILURE, 0, "file 1 (%d) is not closed", fd1);
140 /* Now open the file and read it. */
141 fd1 = open (name, O_RDONLY);
142 if (fd1 == -1)
143 error (EXIT_FAILURE, errno,
144 "cannot open first file \"%s\" for verification", name);
146 if (read (fd1, buf, sizeof buf) != strlen (fd1string))
147 error (EXIT_FAILURE, errno, "cannot read file 1");
148 if (memcmp (fd1string, buf, strlen (fd1string)) != 0)
149 error (EXIT_FAILURE, 0, "file 1 does not match");
151 return 0;
156 do_test (int argc, char *argv[])
158 pid_t pid;
159 int fd4;
160 int status;
161 posix_spawn_file_actions_t actions;
162 char fd1name[18];
163 char fd2name[18];
164 char fd3name[18];
165 char fd4name[18];
166 char *name3_copy;
167 char *spargv[12];
168 int i;
170 /* We must have
171 - one or four parameters left if called initially
172 + path for ld.so optional
173 + "--library-path" optional
174 + the library path optional
175 + the application name
176 - five parameters left if called through re-execution
177 + file descriptor number which is supposed to be closed
178 + the open file descriptor
179 + the newly opened file descriptor
180 + thhe duped second descriptor
181 + the name of the closed descriptor
183 if (argc != (restart ? 6 : 2) && argc != (restart ? 6 : 5))
184 error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc);
186 if (restart)
187 return handle_restart (argv[1], argv[2], argv[3], argv[4], argv[5]);
189 /* Prepare the test. We are creating two files: one which file descriptor
190 will be marked with FD_CLOEXEC, another which is not. */
192 /* Write something in the files. */
193 if (write (temp_fd1, fd1string, strlen (fd1string)) != strlen (fd1string))
194 error (EXIT_FAILURE, errno, "cannot write to first file");
195 if (write (temp_fd2, fd2string, strlen (fd2string)) != strlen (fd2string))
196 error (EXIT_FAILURE, errno, "cannot write to second file");
197 if (write (temp_fd3, fd3string, strlen (fd3string)) != strlen (fd3string))
198 error (EXIT_FAILURE, errno, "cannot write to third file");
200 /* Close the third file. It'll be opened by `spawn'. */
201 close (temp_fd3);
203 /* Tell `spawn' what to do. */
204 if (posix_spawn_file_actions_init (&actions) != 0)
205 error (EXIT_FAILURE, errno, "posix_spawn_file_actions_init");
206 /* Close `temp_fd1'. */
207 if (posix_spawn_file_actions_addclose (&actions, temp_fd1) != 0)
208 error (EXIT_FAILURE, errno, "posix_spawn_file_actions_addclose");
209 /* We want to open the third file. */
210 name3_copy = strdup (name3);
211 if (name3_copy == NULL)
212 error (EXIT_FAILURE, errno, "strdup");
213 if (posix_spawn_file_actions_addopen (&actions, temp_fd3, name3_copy,
214 O_RDONLY, 0666) != 0)
215 error (EXIT_FAILURE, errno, "posix_spawn_file_actions_addopen");
216 /* Overwrite the name to check that a copy has been made. */
217 memset (name3_copy, 'X', strlen (name3_copy));
219 /* We dup the second descriptor. */
220 fd4 = MAX (2, MAX (temp_fd1, MAX (temp_fd2, temp_fd3))) + 1;
221 if (posix_spawn_file_actions_adddup2 (&actions, temp_fd2, fd4) != 0)
222 error (EXIT_FAILURE, errno, "posix_spawn_file_actions_adddup2");
224 /* Now spawn the process. */
225 snprintf (fd1name, sizeof fd1name, "%d", temp_fd1);
226 snprintf (fd2name, sizeof fd2name, "%d", temp_fd2);
227 snprintf (fd3name, sizeof fd3name, "%d", temp_fd3);
228 snprintf (fd4name, sizeof fd4name, "%d", fd4);
230 for (i = 0; i < (argc == (restart ? 6 : 5) ? 4 : 1); i++)
231 spargv[i] = argv[i + 1];
232 spargv[i++] = (char *) "--direct";
233 spargv[i++] = (char *) "--restart";
234 spargv[i++] = fd1name;
235 spargv[i++] = fd2name;
236 spargv[i++] = fd3name;
237 spargv[i++] = fd4name;
238 spargv[i++] = name1;
239 spargv[i] = NULL;
241 if (posix_spawn (&pid, argv[1], &actions, NULL, spargv, environ) != 0)
242 error (EXIT_FAILURE, errno, "posix_spawn");
244 /* Same test but with a NULL pid argument. */
245 if (posix_spawn (NULL, argv[1], &actions, NULL, spargv, environ) != 0)
246 error (EXIT_FAILURE, errno, "posix_spawn");
248 /* Cleanup. */
249 if (posix_spawn_file_actions_destroy (&actions) != 0)
250 error (EXIT_FAILURE, errno, "posix_spawn_file_actions_destroy");
251 free (name3_copy);
253 /* Wait for the children. */
254 TEST_VERIFY (xwaitpid (pid, &status, 0) == pid);
255 TEST_VERIFY (WIFEXITED (status));
256 TEST_VERIFY (!WIFSIGNALED (status));
257 TEST_VERIFY (WEXITSTATUS (status) == 0);
259 xwaitpid (-1, &status, 0);
260 TEST_VERIFY (WIFEXITED (status));
261 TEST_VERIFY (!WIFSIGNALED (status));
262 TEST_VERIFY (WEXITSTATUS (status) == 0);
264 return 0;