Add sysdeps/ieee754/soft-fp.
[glibc.git] / posix / tst-exec.c
blob12a0f57114e82871d836926aa71e8e0b9a0afbb5
1 /* Tests for exec.
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/>. */
20 #include <errno.h>
21 #include <error.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <wait.h>
29 /* Nonzero if the program gets called via `exec'. */
30 static int restart;
33 #define CMDLINE_OPTIONS \
34 { "restart", no_argument, &restart, 1 },
36 /* Prototype for our test function. */
37 extern void do_prepare (int argc, char *argv[]);
38 extern int do_test (int argc, char *argv[]);
40 /* We have a preparation function. */
41 #define PREPARE do_prepare
43 #include "../test-skeleton.c"
46 /* Name of the temporary files. */
47 static char *name1;
48 static char *name2;
50 /* File descriptors for these temporary files. */
51 static int temp_fd1 = -1;
52 static int temp_fd2 = -1;
54 /* The contents of our files. */
55 static const char fd1string[] = "This file should get closed";
56 static const char fd2string[] = "This file should stay opened";
59 /* We have a preparation function. */
60 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 temp_fd1 = create_temp_file ("exec", &name1);
68 temp_fd2 = create_temp_file ("exec", &name2);
69 if (temp_fd1 < 0 || temp_fd2 < 0)
70 exit (1);
74 static int
75 handle_restart (const char *fd1s, const char *fd2s, const char *name)
77 char buf[100];
78 int fd1;
79 int fd2;
81 /* First get the descriptors. */
82 fd1 = atol (fd1s);
83 fd2 = atol (fd2s);
85 /* Sanity check. */
86 if (fd1 == fd2)
87 error (EXIT_FAILURE, 0, "value of fd1 and fd2 is the same");
89 /* First the easy part: read from the file descriptor which is
90 supposed to be open. */
91 if (lseek (fd2, 0, SEEK_CUR) != strlen (fd2string))
92 error (EXIT_FAILURE, errno, "file 2 not in right position");
93 if (lseek (fd2, 0, SEEK_SET) != 0)
94 error (EXIT_FAILURE, 0, "cannot reset position in file 2");
95 if (read (fd2, buf, sizeof buf) != strlen (fd2string))
96 error (EXIT_FAILURE, 0, "cannot read file 2");
97 if (memcmp (fd2string, buf, strlen (fd2string)) != 0)
98 error (EXIT_FAILURE, 0, "file 2 does not match");
100 /* No try to read the first file. First make sure it is not opened. */
101 if (lseek (fd1, 0, SEEK_CUR) != (off_t) -1 || errno != EBADF)
102 error (EXIT_FAILURE, 0, "file 1 (%d) is not closed", fd1);
104 /* Now open the file and read it. */
105 fd1 = open (name, O_RDONLY);
106 if (fd1 == -1)
107 error (EXIT_FAILURE, errno,
108 "cannot open first file \"%s\" for verification", name);
110 if (read (fd1, buf, sizeof buf) != strlen (fd1string))
111 error (EXIT_FAILURE, errno, "cannot read file 1");
112 if (memcmp (fd1string, buf, strlen (fd1string)) != 0)
113 error (EXIT_FAILURE, 0, "file 1 does not match");
115 return 0;
120 do_test (int argc, char *argv[])
122 pid_t pid;
123 int flags;
124 int status;
126 /* We must have
127 - one or four parameters left if called initially
128 + path for ld.so optional
129 + "--library-path" optional
130 + the library path optional
131 + the application name
132 - three parameters left if called through re-execution
133 + file descriptor number which is supposed to be closed
134 + the open file descriptor
135 + the name of the closed desriptor
138 if (restart)
140 if (argc != 4)
141 error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc);
143 return handle_restart (argv[1], argv[2], argv[3]);
146 if (argc != 2 && argc != 5)
147 error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc);
149 /* Prepare the test. We are creating two files: one which file descriptor
150 will be marked with FD_CLOEXEC, another which is not. */
152 /* Set the bit. */
153 flags = fcntl (temp_fd1, F_GETFD, 0);
154 if (flags < 0)
155 error (EXIT_FAILURE, errno, "cannot get flags");
156 flags |= FD_CLOEXEC;
157 if (fcntl (temp_fd1, F_SETFD, flags) < 0)
158 error (EXIT_FAILURE, errno, "cannot set flags");
160 /* Write something in the files. */
161 if (write (temp_fd1, fd1string, strlen (fd1string)) != strlen (fd1string))
162 error (EXIT_FAILURE, errno, "cannot write to first file");
163 if (write (temp_fd2, fd2string, strlen (fd2string)) != strlen (fd2string))
164 error (EXIT_FAILURE, errno, "cannot write to second file");
166 /* We want to test the `exec' function. To do this we restart the program
167 with an additional parameter. But first create another process. */
168 pid = fork ();
169 if (pid == 0)
171 char fd1name[18];
172 char fd2name[18];
174 snprintf (fd1name, sizeof fd1name, "%d", temp_fd1);
175 snprintf (fd2name, sizeof fd2name, "%d", temp_fd2);
177 /* This is the child. Construct the command line. */
178 if (argc == 5)
179 execl (argv[1], argv[1], argv[2], argv[3], argv[4], "--direct",
180 "--restart", fd1name, fd2name, name1, NULL);
181 else
182 execl (argv[1], argv[1], "--direct",
183 "--restart", fd1name, fd2name, name1, NULL);
185 error (EXIT_FAILURE, errno, "cannot exec");
187 else if (pid == (pid_t) -1)
188 error (EXIT_FAILURE, errno, "cannot fork");
190 /* Wait for the child. */
191 if (waitpid (pid, &status, 0) != pid)
192 error (EXIT_FAILURE, errno, "wrong child");
194 if (WTERMSIG (status) != 0)
195 error (EXIT_FAILURE, 0, "Child terminated incorrectly");
196 status = WEXITSTATUS (status);
198 return status;