posix: Add pidfd_spawn and pidfd_spawnp (BZ 30349)
[glibc.git] / posix / tst-spawn.c
blobc44d90756ae23a32669f21a235fc1d1fbd375837
1 /* Tests for spawn.
2 Copyright (C) 2000-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <stdio.h>
20 #include <getopt.h>
21 #include <errno.h>
22 #include <error.h>
23 #include <fcntl.h>
24 #include <spawn.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/param.h>
28 #include <sys/wait.h>
30 #include <support/check.h>
31 #include <support/xunistd.h>
32 #include <support/temp_file.h>
33 #include <support/support.h>
34 #include <tst-spawn.h>
37 /* Nonzero if the program gets called via `exec'. */
38 static int restart;
41 #define CMDLINE_OPTIONS \
42 { "restart", no_argument, &restart, 1 },
44 /* Name of the temporary files. */
45 static char *name1;
46 static char *name2;
47 static char *name3;
48 static char *name5;
50 /* Descriptors for the temporary files. */
51 static int temp_fd1 = -1;
52 static int temp_fd2 = -1;
53 static int temp_fd3 = -1;
54 static int temp_fd5 = -1;
56 /* The contents of our files. */
57 static const char fd1string[] = "This file should get closed";
58 static const char fd2string[] = "This file should stay opened";
59 static const char fd3string[] = "This file will be opened";
60 static const char fd5string[] = "This file should stay opened (O_CLOEXEC)";
63 /* We have a preparation function. */
64 static void
65 do_prepare (int argc, char *argv[])
67 /* We must not open any files in the restart case. */
68 if (restart)
69 return;
71 TEST_VERIFY_EXIT ((temp_fd1 = create_temp_file ("spawn", &name1)) != -1);
72 TEST_VERIFY_EXIT ((temp_fd2 = create_temp_file ("spawn", &name2)) != -1);
73 TEST_VERIFY_EXIT ((temp_fd3 = create_temp_file ("spawn", &name3)) != -1);
74 TEST_VERIFY_EXIT ((temp_fd5 = create_temp_file ("spawn", &name5)) != -1);
76 int flags;
77 TEST_VERIFY_EXIT ((flags = fcntl (temp_fd5, F_GETFD, &flags)) != -1);
78 TEST_COMPARE (fcntl (temp_fd5, F_SETFD, flags | FD_CLOEXEC), 0);
80 #define PREPARE do_prepare
83 static int
84 handle_restart (const char *fd1s, const char *fd2s, const char *fd3s,
85 const char *fd4s, const char *name, const char *fd5s)
87 char buf[100];
88 int fd1;
89 int fd2;
90 int fd3;
91 int fd4;
92 int fd5;
94 /* First get the descriptors. */
95 fd1 = atol (fd1s);
96 fd2 = atol (fd2s);
97 fd3 = atol (fd3s);
98 fd4 = atol (fd4s);
99 fd5 = atol (fd5s);
101 /* Sanity check. */
102 TEST_VERIFY_EXIT (fd1 != fd2);
103 TEST_VERIFY_EXIT (fd1 != fd3);
104 TEST_VERIFY_EXIT (fd1 != fd4);
105 TEST_VERIFY_EXIT (fd2 != fd3);
106 TEST_VERIFY_EXIT (fd2 != fd4);
107 TEST_VERIFY_EXIT (fd3 != fd4);
108 TEST_VERIFY_EXIT (fd4 != fd5);
110 /* First the easy part: read from the file descriptor which is
111 supposed to be open. */
112 TEST_COMPARE (xlseek (fd2, 0, SEEK_CUR), strlen (fd2string));
113 /* The duped descriptor must have the same position. */
114 TEST_COMPARE (xlseek (fd4, 0, SEEK_CUR), strlen (fd2string));
115 TEST_COMPARE (xlseek (fd2, 0, SEEK_SET), 0);
116 TEST_COMPARE (xlseek (fd4, 0, SEEK_CUR), 0);
117 TEST_COMPARE (read (fd2, buf, sizeof buf), strlen (fd2string));
118 TEST_COMPARE_BLOB (fd2string, strlen (fd2string), buf, strlen (fd2string));
120 /* Now read from the third file. */
121 TEST_COMPARE (read (fd3, buf, sizeof buf), strlen (fd3string));
122 TEST_COMPARE_BLOB (fd3string, strlen (fd3string), buf, strlen (fd3string));
123 /* Try to write to the file. This should not be allowed. */
124 TEST_COMPARE (write (fd3, "boo!", 4), -1);
125 TEST_COMPARE (errno, EBADF);
127 /* Now try to read the first file. First make sure it is not opened. */
128 TEST_COMPARE (lseek (fd1, 0, SEEK_CUR), (off_t) -1);
129 TEST_COMPARE (errno, EBADF);
131 /* Now open the file and read it. */
132 fd1 = xopen (name, O_RDONLY, 0600);
134 TEST_COMPARE (read (fd1, buf, sizeof buf), strlen (fd1string));
135 TEST_COMPARE_BLOB (fd1string, strlen (fd1string), buf, strlen (fd1string));
137 TEST_COMPARE (xlseek (fd5, 0, SEEK_SET), 0);
138 TEST_COMPARE (read (fd5, buf, sizeof buf), strlen (fd5string));
139 TEST_COMPARE_BLOB (fd5string, strlen (fd5string), buf, strlen (fd5string));
141 return 0;
145 static int
146 do_test (int argc, char *argv[])
148 PID_T_TYPE pid;
149 int fd4;
150 siginfo_t sinfo;
151 posix_spawn_file_actions_t actions;
152 char fd1name[18];
153 char fd2name[18];
154 char fd3name[18];
155 char fd4name[18];
156 char fd5name[18];
157 char *name3_copy;
158 char *spargv[13];
159 int i;
161 /* We must have
162 - one or four parameters left if called initially
163 + path for ld.so optional
164 + "--library-path" optional
165 + the library path optional
166 + the application name
167 - six parameters left if called through re-execution
168 + file descriptor number which is supposed to be closed
169 + the open file descriptor
170 + the newly opened file descriptor
171 + the duped second descriptor
172 + the name of the closed descriptor
173 + the duped fourth file descriptor which O_CLOEXEC should be
174 remove by adddup2.
176 if (argc != (restart ? 7 : 2) && argc != (restart ? 7 : 5))
177 FAIL_EXIT1 ("wrong number of arguments (%d)", argc);
179 if (restart)
180 return handle_restart (argv[1], argv[2], argv[3], argv[4], argv[5],
181 argv[6]);
183 /* Prepare the test. We are creating four files: two which file descriptor
184 will be marked with FD_CLOEXEC, another which is not. */
186 /* Write something in the files. */
187 xwrite (temp_fd1, fd1string, strlen (fd1string));
188 xwrite (temp_fd2, fd2string, strlen (fd2string));
189 xwrite (temp_fd3, fd3string, strlen (fd3string));
190 xwrite (temp_fd5, fd5string, strlen (fd5string));
192 /* Close the third file. It'll be opened by `spawn'. */
193 xclose (temp_fd3);
195 /* Tell `spawn' what to do. */
196 TEST_COMPARE (posix_spawn_file_actions_init (&actions), 0);
197 /* Close `temp_fd1'. */
198 TEST_COMPARE (posix_spawn_file_actions_addclose (&actions, temp_fd1), 0);
199 /* We want to open the third file. */
200 name3_copy = xstrdup (name3);
201 TEST_COMPARE (posix_spawn_file_actions_addopen (&actions, temp_fd3,
202 name3_copy,
203 O_RDONLY, 0666),
205 /* Overwrite the name to check that a copy has been made. */
206 memset (name3_copy, 'X', strlen (name3_copy));
208 /* We dup the second descriptor. */
209 fd4 = MAX (2, MAX (temp_fd1, MAX (temp_fd2, MAX (temp_fd3, temp_fd5)))) + 1;
210 TEST_COMPARE (posix_spawn_file_actions_adddup2 (&actions, temp_fd2, fd4),
213 /* We clear the O_CLOEXEC on fourth descriptor, so it should be
214 stay open on child. */
215 TEST_COMPARE (posix_spawn_file_actions_adddup2 (&actions, temp_fd5,
216 temp_fd5),
219 /* Now spawn the process. */
220 snprintf (fd1name, sizeof fd1name, "%d", temp_fd1);
221 snprintf (fd2name, sizeof fd2name, "%d", temp_fd2);
222 snprintf (fd3name, sizeof fd3name, "%d", temp_fd3);
223 snprintf (fd4name, sizeof fd4name, "%d", fd4);
224 snprintf (fd5name, sizeof fd5name, "%d", temp_fd5);
226 for (i = 0; i < (argc == (restart ? 7 : 5) ? 4 : 1); i++)
227 spargv[i] = argv[i + 1];
228 spargv[i++] = (char *) "--direct";
229 spargv[i++] = (char *) "--restart";
230 spargv[i++] = fd1name;
231 spargv[i++] = fd2name;
232 spargv[i++] = fd3name;
233 spargv[i++] = fd4name;
234 spargv[i++] = name1;
235 spargv[i++] = fd5name;
236 spargv[i] = NULL;
238 TEST_COMPARE (POSIX_SPAWN (&pid, argv[1], &actions, NULL, spargv, environ),
241 /* Wait for the children. */
242 TEST_COMPARE (WAITID (P_PID, pid, &sinfo, WEXITED), 0);
243 TEST_COMPARE (sinfo.si_code, CLD_EXITED);
244 TEST_COMPARE (sinfo.si_status, 0);
246 /* Same test but with a NULL pid argument. */
247 TEST_COMPARE (POSIX_SPAWN (NULL, argv[1], &actions, NULL, spargv, environ),
250 /* Cleanup. */
251 TEST_COMPARE (posix_spawn_file_actions_destroy (&actions), 0);
252 free (name3_copy);
254 /* Wait for the children. */
255 TEST_COMPARE (WAITID (P_ALL, 0, &sinfo, WEXITED), 0);
256 TEST_COMPARE (sinfo.si_code, CLD_EXITED);
257 TEST_COMPARE (sinfo.si_status, 0);
259 return 0;
262 #define TEST_FUNCTION_ARGV do_test
263 #include <support/test-driver.c>