Use common bits/shm.h for more architectures.
[glibc.git] / posix / tst-spawn.c
blob41f1a65243128ab514d6a5070ed43ff5a15aa97d
1 /* Tests for spawn.
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/>. */
20 #include <stdio.h>
21 #include <getopt.h>
22 #include <errno.h>
23 #include <error.h>
24 #include <fcntl.h>
25 #include <spawn.h>
26 #include <stdlib.h>
27 #include <string.h>
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'. */
37 static int restart;
40 #define CMDLINE_OPTIONS \
41 { "restart", no_argument, &restart, 1 },
43 /* Name of the temporary files. */
44 static char *name1;
45 static char *name2;
46 static char *name3;
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. */
60 static void
61 do_prepare (int argc, char *argv[])
63 /* We must not open any files in the restart case. */
64 if (restart)
65 return;
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
74 static int
75 handle_restart (const char *fd1s, const char *fd2s, const char *fd3s,
76 const char *fd4s, const char *name)
78 char buf[100];
79 int fd1;
80 int fd2;
81 int fd3;
82 int fd4;
84 /* First get the descriptors. */
85 fd1 = atol (fd1s);
86 fd2 = atol (fd2s);
87 fd3 = atol (fd3s);
88 fd4 = atol (fd4s);
90 /* Sanity check. */
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));
125 return 0;
129 static int
130 do_test (int argc, char *argv[])
132 pid_t pid;
133 int fd4;
134 int status;
135 posix_spawn_file_actions_t actions;
136 char fd1name[18];
137 char fd2name[18];
138 char fd3name[18];
139 char fd4name[18];
140 char *name3_copy;
141 char *spargv[12];
142 int i;
144 /* We must have
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);
160 if (restart)
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'. */
172 xclose (temp_fd3);
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,
181 name3_copy,
182 O_RDONLY, 0666),
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;
206 spargv[i++] = name1;
207 spargv[i] = NULL;
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),
216 /* Cleanup. */
217 TEST_COMPARE (posix_spawn_file_actions_destroy (&actions), 0);
218 free (name3_copy);
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);
231 return 0;
234 #define TEST_FUNCTION_ARGV do_test
235 #include <support/test-driver.c>