lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Tests for spawn. |
| 2 | Copyright (C) 2000-2015 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000. |
| 5 | |
| 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. |
| 10 | |
| 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. |
| 15 | |
| 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/>. */ |
| 19 | |
| 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 <unistd.h> |
| 27 | #include <wait.h> |
| 28 | #include <sys/param.h> |
| 29 | |
| 30 | |
| 31 | /* Nonzero if the program gets called via `exec'. */ |
| 32 | static int restart; |
| 33 | |
| 34 | |
| 35 | #define CMDLINE_OPTIONS \ |
| 36 | { "restart", no_argument, &restart, 1 }, |
| 37 | |
| 38 | /* Prototype for our test function. */ |
| 39 | extern void do_prepare (int argc, char *argv[]); |
| 40 | extern int do_test (int argc, char *argv[]); |
| 41 | |
| 42 | /* We have a preparation function. */ |
| 43 | #define PREPARE do_prepare |
| 44 | |
| 45 | #include "../test-skeleton.c" |
| 46 | |
| 47 | |
| 48 | /* Name of the temporary files. */ |
| 49 | static char *name1; |
| 50 | static char *name2; |
| 51 | static char *name3; |
| 52 | |
| 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"; |
| 57 | |
| 58 | |
| 59 | /* We have a preparation function. */ |
| 60 | void |
| 61 | do_prepare (int argc, char *argv[]) |
| 62 | { |
| 63 | size_t name_len; |
| 64 | |
| 65 | name_len = strlen (test_dir); |
| 66 | name1 = (char *) malloc (name_len + sizeof ("/spawnXXXXXX")); |
| 67 | mempcpy (mempcpy (name1, test_dir, name_len), |
| 68 | "/spawnXXXXXX", sizeof ("/spawnXXXXXX")); |
| 69 | add_temp_file (name1); |
| 70 | |
| 71 | name2 = (char *) malloc (name_len + sizeof ("/spawnXXXXXX")); |
| 72 | mempcpy (mempcpy (name2, test_dir, name_len), |
| 73 | "/spawnXXXXXX", sizeof ("/spawnXXXXXX")); |
| 74 | add_temp_file (name2); |
| 75 | |
| 76 | name3 = (char *) malloc (name_len + sizeof ("/spawnXXXXXX")); |
| 77 | mempcpy (mempcpy (name3, test_dir, name_len), |
| 78 | "/spawnXXXXXX", sizeof ("/spawnXXXXXX")); |
| 79 | add_temp_file (name3); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | static int |
| 84 | handle_restart (const char *fd1s, const char *fd2s, const char *fd3s, |
| 85 | const char *fd4s, const char *name) |
| 86 | { |
| 87 | char buf[100]; |
| 88 | int fd1; |
| 89 | int fd2; |
| 90 | int fd3; |
| 91 | int fd4; |
| 92 | |
| 93 | /* First get the descriptors. */ |
| 94 | fd1 = atol (fd1s); |
| 95 | fd2 = atol (fd2s); |
| 96 | fd3 = atol (fd3s); |
| 97 | fd4 = atol (fd4s); |
| 98 | |
| 99 | /* Sanity check. */ |
| 100 | if (fd1 == fd2) |
| 101 | error (EXIT_FAILURE, 0, "value of fd1 and fd2 is the same"); |
| 102 | if (fd1 == fd3) |
| 103 | error (EXIT_FAILURE, 0, "value of fd1 and fd3 is the same"); |
| 104 | if (fd1 == fd4) |
| 105 | error (EXIT_FAILURE, 0, "value of fd1 and fd4 is the same"); |
| 106 | if (fd2 == fd3) |
| 107 | error (EXIT_FAILURE, 0, "value of fd2 and fd3 is the same"); |
| 108 | if (fd2 == fd4) |
| 109 | error (EXIT_FAILURE, 0, "value of fd2 and fd4 is the same"); |
| 110 | if (fd3 == fd4) |
| 111 | error (EXIT_FAILURE, 0, "value of fd3 and fd4 is the same"); |
| 112 | |
| 113 | /* First the easy part: read from the file descriptor which is |
| 114 | supposed to be open. */ |
| 115 | if (lseek (fd2, 0, SEEK_CUR) != strlen (fd2string)) |
| 116 | error (EXIT_FAILURE, errno, "file 2 not in right position"); |
| 117 | /* The duped descriptor must have the same position. */ |
| 118 | if (lseek (fd4, 0, SEEK_CUR) != strlen (fd2string)) |
| 119 | error (EXIT_FAILURE, errno, "file 4 not in right position"); |
| 120 | if (lseek (fd2, 0, SEEK_SET) != 0) |
| 121 | error (EXIT_FAILURE, 0, "cannot reset position in file 2"); |
| 122 | if (lseek (fd4, 0, SEEK_CUR) != 0) |
| 123 | error (EXIT_FAILURE, errno, "file 4 not set back, too"); |
| 124 | if (read (fd2, buf, sizeof buf) != strlen (fd2string)) |
| 125 | error (EXIT_FAILURE, 0, "cannot read file 2"); |
| 126 | if (memcmp (fd2string, buf, strlen (fd2string)) != 0) |
| 127 | error (EXIT_FAILURE, 0, "file 2 does not match"); |
| 128 | |
| 129 | /* Now read from the third file. */ |
| 130 | if (read (fd3, buf, sizeof buf) != strlen (fd3string)) |
| 131 | error (EXIT_FAILURE, 0, "cannot read file 3"); |
| 132 | if (memcmp (fd3string, buf, strlen (fd3string)) != 0) |
| 133 | error (EXIT_FAILURE, 0, "file 3 does not match"); |
| 134 | /* Try to write to the file. This should not be allowed. */ |
| 135 | if (write (fd3, "boo!", 4) != -1 || errno != EBADF) |
| 136 | error (EXIT_FAILURE, 0, "file 3 is writable"); |
| 137 | |
| 138 | /* Now try to read the first file. First make sure it is not opened. */ |
| 139 | if (lseek (fd1, 0, SEEK_CUR) != (off_t) -1 || errno != EBADF) |
| 140 | error (EXIT_FAILURE, 0, "file 1 (%d) is not closed", fd1); |
| 141 | |
| 142 | /* Now open the file and read it. */ |
| 143 | fd1 = open (name, O_RDONLY); |
| 144 | if (fd1 == -1) |
| 145 | error (EXIT_FAILURE, errno, |
| 146 | "cannot open first file \"%s\" for verification", name); |
| 147 | |
| 148 | if (read (fd1, buf, sizeof buf) != strlen (fd1string)) |
| 149 | error (EXIT_FAILURE, errno, "cannot read file 1"); |
| 150 | if (memcmp (fd1string, buf, strlen (fd1string)) != 0) |
| 151 | error (EXIT_FAILURE, 0, "file 1 does not match"); |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | int |
| 158 | do_test (int argc, char *argv[]) |
| 159 | { |
| 160 | pid_t pid; |
| 161 | int fd1; |
| 162 | int fd2; |
| 163 | int fd3; |
| 164 | int fd4; |
| 165 | int status; |
| 166 | posix_spawn_file_actions_t actions; |
| 167 | char fd1name[18]; |
| 168 | char fd2name[18]; |
| 169 | char fd3name[18]; |
| 170 | char fd4name[18]; |
| 171 | char *name3_copy; |
| 172 | char *spargv[12]; |
| 173 | int i; |
| 174 | |
| 175 | /* We must have |
| 176 | - one or four parameters left if called initially |
| 177 | + path for ld.so optional |
| 178 | + "--library-path" optional |
| 179 | + the library path optional |
| 180 | + the application name |
| 181 | - five parameters left if called through re-execution |
| 182 | + file descriptor number which is supposed to be closed |
| 183 | + the open file descriptor |
| 184 | + the newly opened file descriptor |
| 185 | + thhe duped second descriptor |
| 186 | + the name of the closed descriptor |
| 187 | */ |
| 188 | if (argc != (restart ? 6 : 2) && argc != (restart ? 6 : 5)) |
| 189 | error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc); |
| 190 | |
| 191 | if (restart) |
| 192 | return handle_restart (argv[1], argv[2], argv[3], argv[4], argv[5]); |
| 193 | |
| 194 | /* Prepare the test. We are creating two files: one which file descriptor |
| 195 | will be marked with FD_CLOEXEC, another which is not. */ |
| 196 | |
| 197 | /* Open our test files. */ |
| 198 | fd1 = mkstemp (name1); |
| 199 | if (fd1 == -1) |
| 200 | error (EXIT_FAILURE, errno, "cannot open test file `%s'", name1); |
| 201 | fd2 = mkstemp (name2); |
| 202 | if (fd2 == -1) |
| 203 | error (EXIT_FAILURE, errno, "cannot open test file `%s'", name2); |
| 204 | fd3 = mkstemp (name3); |
| 205 | if (fd3 == -1) |
| 206 | error (EXIT_FAILURE, errno, "cannot open test file `%s'", name3); |
| 207 | |
| 208 | /* Write something in the files. */ |
| 209 | if (write (fd1, fd1string, strlen (fd1string)) != strlen (fd1string)) |
| 210 | error (EXIT_FAILURE, errno, "cannot write to first file"); |
| 211 | if (write (fd2, fd2string, strlen (fd2string)) != strlen (fd2string)) |
| 212 | error (EXIT_FAILURE, errno, "cannot write to second file"); |
| 213 | if (write (fd3, fd3string, strlen (fd3string)) != strlen (fd3string)) |
| 214 | error (EXIT_FAILURE, errno, "cannot write to third file"); |
| 215 | |
| 216 | /* Close the third file. It'll be opened by `spawn'. */ |
| 217 | close (fd3); |
| 218 | |
| 219 | /* Tell `spawn' what to do. */ |
| 220 | if (posix_spawn_file_actions_init (&actions) != 0) |
| 221 | error (EXIT_FAILURE, errno, "posix_spawn_file_actions_init"); |
| 222 | /* Close `fd1'. */ |
| 223 | if (posix_spawn_file_actions_addclose (&actions, fd1) != 0) |
| 224 | error (EXIT_FAILURE, errno, "posix_spawn_file_actions_addclose"); |
| 225 | /* We want to open the third file. */ |
| 226 | name3_copy = strdup (name3); |
| 227 | if (name3_copy == NULL) |
| 228 | error (EXIT_FAILURE, errno, "strdup"); |
| 229 | if (posix_spawn_file_actions_addopen (&actions, fd3, name3_copy, |
| 230 | O_RDONLY, 0666) != 0) |
| 231 | error (EXIT_FAILURE, errno, "posix_spawn_file_actions_addopen"); |
| 232 | /* Overwrite the name to check that a copy has been made. */ |
| 233 | memset (name3_copy, 'X', strlen (name3_copy)); |
| 234 | |
| 235 | /* We dup the second descriptor. */ |
| 236 | fd4 = MAX (2, MAX (fd1, MAX (fd2, fd3))) + 1; |
| 237 | if (posix_spawn_file_actions_adddup2 (&actions, fd2, fd4) != 0) |
| 238 | error (EXIT_FAILURE, errno, "posix_spawn_file_actions_adddup2"); |
| 239 | |
| 240 | /* Now spawn the process. */ |
| 241 | snprintf (fd1name, sizeof fd1name, "%d", fd1); |
| 242 | snprintf (fd2name, sizeof fd2name, "%d", fd2); |
| 243 | snprintf (fd3name, sizeof fd3name, "%d", fd3); |
| 244 | snprintf (fd4name, sizeof fd4name, "%d", fd4); |
| 245 | |
| 246 | for (i = 0; i < (argc == (restart ? 6 : 5) ? 4 : 1); i++) |
| 247 | spargv[i] = argv[i + 1]; |
| 248 | spargv[i++] = (char *) "--direct"; |
| 249 | spargv[i++] = (char *) "--restart"; |
| 250 | spargv[i++] = fd1name; |
| 251 | spargv[i++] = fd2name; |
| 252 | spargv[i++] = fd3name; |
| 253 | spargv[i++] = fd4name; |
| 254 | spargv[i++] = name1; |
| 255 | spargv[i] = NULL; |
| 256 | |
| 257 | if (posix_spawn (&pid, argv[1], &actions, NULL, spargv, environ) != 0) |
| 258 | error (EXIT_FAILURE, errno, "posix_spawn"); |
| 259 | |
| 260 | /* Cleanup. */ |
| 261 | if (posix_spawn_file_actions_destroy (&actions) != 0) |
| 262 | error (EXIT_FAILURE, errno, "posix_spawn_file_actions_destroy"); |
| 263 | free (name3_copy); |
| 264 | |
| 265 | /* Wait for the child. */ |
| 266 | if (waitpid (pid, &status, 0) != pid) |
| 267 | error (EXIT_FAILURE, errno, "wrong child"); |
| 268 | |
| 269 | if (WTERMSIG (status) != 0) |
| 270 | error (EXIT_FAILURE, 0, "Child terminated incorrectly"); |
| 271 | status = WEXITSTATUS (status); |
| 272 | |
| 273 | /* Remove the test files. */ |
| 274 | unlink (name1); |
| 275 | unlink (name2); |
| 276 | unlink (name3); |
| 277 | |
| 278 | return status; |
| 279 | } |