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/>. */
27 #include <sys/param.h>
28 #include <support/check.h>
29 #include <support/xunistd.h>
32 /* Nonzero if the program gets called via `exec'. */
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. */
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. */
67 do_prepare (int argc
, char *argv
[])
69 /* We must not open any files in the restart case. */
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)
82 handle_restart (const char *fd1s
, const char *fd2s
, const char *fd3s
,
83 const char *fd4s
, const char *name
)
91 /* First get the descriptors. */
99 error (EXIT_FAILURE
, 0, "value of fd1 and fd2 is the same");
101 error (EXIT_FAILURE
, 0, "value of fd1 and fd3 is the same");
103 error (EXIT_FAILURE
, 0, "value of fd1 and fd4 is the same");
105 error (EXIT_FAILURE
, 0, "value of fd2 and fd3 is the same");
107 error (EXIT_FAILURE
, 0, "value of fd2 and fd4 is the same");
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
);
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");
156 do_test (int argc
, char *argv
[])
161 posix_spawn_file_actions_t actions
;
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
);
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'. */
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
;
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");
249 if (posix_spawn_file_actions_destroy (&actions
) != 0)
250 error (EXIT_FAILURE
, errno
, "posix_spawn_file_actions_destroy");
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);