2 Copyright (C) 2000-2018 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/>. */
28 #include <sys/param.h>
30 #include <support/check.h>
31 #include <support/xunistd.h>
32 #include <support/temp_file.h>
33 #include <support/support.h>
36 /* Nonzero if the program gets called via `exec'. */
40 #define CMDLINE_OPTIONS \
41 { "restart", no_argument, &restart, 1 },
43 /* Name of the temporary files. */
48 /* Descriptors for the temporary files. */
49 static int temp_fd1
= -1;
50 static int temp_fd2
= -1;
51 static int temp_fd3
= -1;
53 /* The contents of our files. */
54 static const char fd1string
[] = "This file should get closed";
55 static const char fd2string
[] = "This file should stay opened";
56 static const char fd3string
[] = "This file will be opened";
59 /* We have a preparation function. */
61 do_prepare (int argc
, char *argv
[])
63 /* We must not open any files in the restart case. */
67 TEST_VERIFY_EXIT ((temp_fd1
= create_temp_file ("spawn", &name1
)) != -1);
68 TEST_VERIFY_EXIT ((temp_fd2
= create_temp_file ("spawn", &name2
)) != -1);
69 TEST_VERIFY_EXIT ((temp_fd3
= create_temp_file ("spawn", &name3
)) != -1);
71 #define PREPARE do_prepare
75 handle_restart (const char *fd1s
, const char *fd2s
, const char *fd3s
,
76 const char *fd4s
, const char *name
)
84 /* First get the descriptors. */
91 TEST_VERIFY_EXIT (fd1
!= fd2
);
92 TEST_VERIFY_EXIT (fd1
!= fd3
);
93 TEST_VERIFY_EXIT (fd1
!= fd4
);
94 TEST_VERIFY_EXIT (fd2
!= fd3
);
95 TEST_VERIFY_EXIT (fd2
!= fd4
);
96 TEST_VERIFY_EXIT (fd3
!= fd4
);
98 /* First the easy part: read from the file descriptor which is
99 supposed to be open. */
100 TEST_COMPARE (xlseek (fd2
, 0, SEEK_CUR
), strlen (fd2string
));
101 /* The duped descriptor must have the same position. */
102 TEST_COMPARE (xlseek (fd4
, 0, SEEK_CUR
), strlen (fd2string
));
103 TEST_COMPARE (xlseek (fd2
, 0, SEEK_SET
), 0);
104 TEST_COMPARE (xlseek (fd4
, 0, SEEK_CUR
), 0);
105 TEST_COMPARE (read (fd2
, buf
, sizeof buf
), strlen (fd2string
));
106 TEST_COMPARE_BLOB (fd2string
, strlen (fd2string
), buf
, strlen (fd2string
));
108 /* Now read from the third file. */
109 TEST_COMPARE (read (fd3
, buf
, sizeof buf
), strlen (fd3string
));
110 TEST_COMPARE_BLOB (fd3string
, strlen (fd3string
), buf
, strlen (fd3string
));
111 /* Try to write to the file. This should not be allowed. */
112 TEST_COMPARE (write (fd3
, "boo!", 4), -1);
113 TEST_COMPARE (errno
, EBADF
);
115 /* Now try to read the first file. First make sure it is not opened. */
116 TEST_COMPARE (lseek (fd1
, 0, SEEK_CUR
), (off_t
) -1);
117 TEST_COMPARE (errno
, EBADF
);
119 /* Now open the file and read it. */
120 fd1
= xopen (name
, O_RDONLY
, 0600);
122 TEST_COMPARE (read (fd1
, buf
, sizeof buf
), strlen (fd1string
));
123 TEST_COMPARE_BLOB (fd1string
, strlen (fd1string
), buf
, strlen (fd1string
));
130 do_test (int argc
, char *argv
[])
135 posix_spawn_file_actions_t actions
;
145 - one or four parameters left if called initially
146 + path for ld.so optional
147 + "--library-path" optional
148 + the library path optional
149 + the application name
150 - five parameters left if called through re-execution
151 + file descriptor number which is supposed to be closed
152 + the open file descriptor
153 + the newly opened file descriptor
154 + thhe duped second descriptor
155 + the name of the closed descriptor
157 if (argc
!= (restart
? 6 : 2) && argc
!= (restart
? 6 : 5))
158 FAIL_EXIT1 ("wrong number of arguments (%d)", argc
);
161 return handle_restart (argv
[1], argv
[2], argv
[3], argv
[4], argv
[5]);
163 /* Prepare the test. We are creating two files: one which file descriptor
164 will be marked with FD_CLOEXEC, another which is not. */
166 /* Write something in the files. */
167 xwrite (temp_fd1
, fd1string
, strlen (fd1string
));
168 xwrite (temp_fd2
, fd2string
, strlen (fd2string
));
169 xwrite (temp_fd3
, fd3string
, strlen (fd3string
));
171 /* Close the third file. It'll be opened by `spawn'. */
174 /* Tell `spawn' what to do. */
175 TEST_COMPARE (posix_spawn_file_actions_init (&actions
), 0);
176 /* Close `temp_fd1'. */
177 TEST_COMPARE (posix_spawn_file_actions_addclose (&actions
, temp_fd1
), 0);
178 /* We want to open the third file. */
179 name3_copy
= xstrdup (name3
);
180 TEST_COMPARE (posix_spawn_file_actions_addopen (&actions
, temp_fd3
,
184 /* Overwrite the name to check that a copy has been made. */
185 memset (name3_copy
, 'X', strlen (name3_copy
));
187 /* We dup the second descriptor. */
188 fd4
= MAX (2, MAX (temp_fd1
, MAX (temp_fd2
, temp_fd3
))) + 1;
189 TEST_COMPARE (posix_spawn_file_actions_adddup2 (&actions
, temp_fd2
, fd4
),
192 /* Now spawn the process. */
193 snprintf (fd1name
, sizeof fd1name
, "%d", temp_fd1
);
194 snprintf (fd2name
, sizeof fd2name
, "%d", temp_fd2
);
195 snprintf (fd3name
, sizeof fd3name
, "%d", temp_fd3
);
196 snprintf (fd4name
, sizeof fd4name
, "%d", fd4
);
198 for (i
= 0; i
< (argc
== (restart
? 6 : 5) ? 4 : 1); i
++)
199 spargv
[i
] = argv
[i
+ 1];
200 spargv
[i
++] = (char *) "--direct";
201 spargv
[i
++] = (char *) "--restart";
202 spargv
[i
++] = fd1name
;
203 spargv
[i
++] = fd2name
;
204 spargv
[i
++] = fd3name
;
205 spargv
[i
++] = fd4name
;
209 TEST_COMPARE (posix_spawn (&pid
, argv
[1], &actions
, NULL
, spargv
, environ
),
212 /* Same test but with a NULL pid argument. */
213 TEST_COMPARE (posix_spawn (NULL
, argv
[1], &actions
, NULL
, spargv
, environ
),
217 TEST_COMPARE (posix_spawn_file_actions_destroy (&actions
), 0);
220 /* Wait for the children. */
221 TEST_COMPARE (xwaitpid (pid
, &status
, 0), pid
);
222 TEST_VERIFY (WIFEXITED (status
));
223 TEST_VERIFY (!WIFSIGNALED (status
));
224 TEST_COMPARE (WEXITSTATUS (status
), 0);
226 xwaitpid (-1, &status
, 0);
227 TEST_VERIFY (WIFEXITED (status
));
228 TEST_VERIFY (!WIFSIGNALED (status
));
229 TEST_COMPARE (WEXITSTATUS (status
), 0);
234 #define TEST_FUNCTION_ARGV do_test
235 #include <support/test-driver.c>