1 | /* Tests for exec.
|
---|
2 | Copyright (C) 2000 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, write to the Free
|
---|
18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA. */
|
---|
20 |
|
---|
21 | #include <errno.h>
|
---|
22 | #include <error.h>
|
---|
23 | #include <fcntl.h>
|
---|
24 | #include <stdlib.h>
|
---|
25 | #include <string.h>
|
---|
26 | #include <unistd.h>
|
---|
27 | #include <wait.h>
|
---|
28 |
|
---|
29 |
|
---|
30 | /* Nonzero if the program gets called via `exec'. */
|
---|
31 | static int restart;
|
---|
32 |
|
---|
33 |
|
---|
34 | #define CMDLINE_OPTIONS \
|
---|
35 | { "restart", no_argument, &restart, 1 },
|
---|
36 |
|
---|
37 | /* Prototype for our test function. */
|
---|
38 | extern void do_prepare (int argc, char *argv[]);
|
---|
39 | extern int do_test (int argc, char *argv[]);
|
---|
40 |
|
---|
41 | /* We have a preparation function. */
|
---|
42 | #define PREPARE do_prepare
|
---|
43 |
|
---|
44 | #include "../test-skeleton.c"
|
---|
45 |
|
---|
46 |
|
---|
47 | /* Name of the temporary files. */
|
---|
48 | static char *name1;
|
---|
49 | static char *name2;
|
---|
50 |
|
---|
51 | /* The contents of our files. */
|
---|
52 | static const char fd1string[] = "This file should get closed";
|
---|
53 | static const char fd2string[] = "This file should stay opened";
|
---|
54 |
|
---|
55 |
|
---|
56 | /* We have a preparation function. */
|
---|
57 | void
|
---|
58 | do_prepare (int argc, char *argv[])
|
---|
59 | {
|
---|
60 | char name_len;
|
---|
61 |
|
---|
62 | name_len = strlen (test_dir);
|
---|
63 | name1 = malloc (name_len + sizeof ("/execXXXXXX"));
|
---|
64 | mempcpy (mempcpy (name1, test_dir, name_len),
|
---|
65 | "/execXXXXXX", sizeof ("/execXXXXXX"));
|
---|
66 | add_temp_file (name1);
|
---|
67 |
|
---|
68 | name2 = malloc (name_len + sizeof ("/execXXXXXX"));
|
---|
69 | mempcpy (mempcpy (name2, test_dir, name_len),
|
---|
70 | "/execXXXXXX", sizeof ("/execXXXXXX"));
|
---|
71 | add_temp_file (name2);
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | static int
|
---|
76 | handle_restart (const char *fd1s, const char *fd2s, const char *name)
|
---|
77 | {
|
---|
78 | char buf[100];
|
---|
79 | int fd1;
|
---|
80 | int fd2;
|
---|
81 |
|
---|
82 | /* First get the descriptors. */
|
---|
83 | fd1 = atol (fd1s);
|
---|
84 | fd2 = atol (fd2s);
|
---|
85 |
|
---|
86 | /* Sanity check. */
|
---|
87 | if (fd1 == fd2)
|
---|
88 | error (EXIT_FAILURE, 0, "value of fd1 and fd2 is the same");
|
---|
89 |
|
---|
90 | /* First the easy part: read from the file descriptor which is
|
---|
91 | supposed to be open. */
|
---|
92 | if (lseek (fd2, 0, SEEK_CUR) != strlen (fd2string))
|
---|
93 | error (EXIT_FAILURE, errno, "file 2 not in right position");
|
---|
94 | if (lseek (fd2, 0, SEEK_SET) != 0)
|
---|
95 | error (EXIT_FAILURE, 0, "cannot reset position in file 2");
|
---|
96 | if (read (fd2, buf, sizeof buf) != strlen (fd2string))
|
---|
97 | error (EXIT_FAILURE, 0, "cannot read file 2");
|
---|
98 | if (memcmp (fd2string, buf, strlen (fd2string)) != 0)
|
---|
99 | error (EXIT_FAILURE, 0, "file 2 does not match");
|
---|
100 |
|
---|
101 | /* No try to read the first file. First make sure it is not opened. */
|
---|
102 | if (lseek (fd1, 0, SEEK_CUR) != (off_t) -1 || errno != EBADF)
|
---|
103 | error (EXIT_FAILURE, 0, "file 1 (%d) is not closed", fd1);
|
---|
104 |
|
---|
105 | /* Now open the file and read it. */
|
---|
106 | fd1 = open (name, O_RDONLY);
|
---|
107 | if (fd1 == -1)
|
---|
108 | error (EXIT_FAILURE, errno,
|
---|
109 | "cannot open first file \"%s\" for verification", name);
|
---|
110 |
|
---|
111 | if (read (fd1, buf, sizeof buf) != strlen (fd1string))
|
---|
112 | error (EXIT_FAILURE, errno, "cannot read file 1");
|
---|
113 | if (memcmp (fd1string, buf, strlen (fd1string)) != 0)
|
---|
114 | error (EXIT_FAILURE, 0, "file 1 does not match");
|
---|
115 |
|
---|
116 | return 0;
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | int
|
---|
121 | do_test (int argc, char *argv[])
|
---|
122 | {
|
---|
123 | pid_t pid;
|
---|
124 | int fd1;
|
---|
125 | int fd2;
|
---|
126 | int flags;
|
---|
127 | int status;
|
---|
128 |
|
---|
129 | /* We must have
|
---|
130 | - four parameters left of called initially
|
---|
131 | + path for ld.so
|
---|
132 | + "--library-path"
|
---|
133 | + the library path
|
---|
134 | + the application name
|
---|
135 | - three parameters left if called through re-execution
|
---|
136 | + file descriptor number which is supposed to be closed
|
---|
137 | + the open file descriptor
|
---|
138 | + the name of the closed desriptor
|
---|
139 | */
|
---|
140 |
|
---|
141 | if (restart)
|
---|
142 | {
|
---|
143 | if (argc != 4)
|
---|
144 | error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc);
|
---|
145 |
|
---|
146 | return handle_restart (argv[1], argv[2], argv[3]);
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (argc != 5)
|
---|
150 | error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc);
|
---|
151 |
|
---|
152 | /* Prepare the test. We are creating two files: one which file descriptor
|
---|
153 | will be marked with FD_CLOEXEC, another which is not. */
|
---|
154 |
|
---|
155 | /* Open our test files. */
|
---|
156 | fd1 = mkstemp (name1);
|
---|
157 | if (fd1 == -1)
|
---|
158 | error (EXIT_FAILURE, errno, "cannot open test file `%s'", name1);
|
---|
159 | fd2 = mkstemp (name2);
|
---|
160 | if (fd2 == -1)
|
---|
161 | error (EXIT_FAILURE, errno, "cannot open test file `%s'", name2);
|
---|
162 |
|
---|
163 | /* Set the bit. */
|
---|
164 | flags = fcntl (fd1, F_GETFD, 0);
|
---|
165 | if (flags < 0)
|
---|
166 | error (EXIT_FAILURE, errno, "cannot get flags");
|
---|
167 | flags |= FD_CLOEXEC;
|
---|
168 | if (fcntl (fd1, F_SETFD, flags) < 0)
|
---|
169 | error (EXIT_FAILURE, errno, "cannot set flags");
|
---|
170 |
|
---|
171 | /* Write something in the files. */
|
---|
172 | if (write (fd1, fd1string, strlen (fd1string)) != strlen (fd1string))
|
---|
173 | error (EXIT_FAILURE, errno, "cannot write to first file");
|
---|
174 | if (write (fd2, fd2string, strlen (fd2string)) != strlen (fd2string))
|
---|
175 | error (EXIT_FAILURE, errno, "cannot write to second file");
|
---|
176 |
|
---|
177 | /* We want to test the `exec' function. To do this we restart the program
|
---|
178 | with an additional parameter. But first create another process. */
|
---|
179 | pid = fork ();
|
---|
180 | if (pid == 0)
|
---|
181 | {
|
---|
182 | char fd1name[18];
|
---|
183 | char fd2name[18];
|
---|
184 |
|
---|
185 | snprintf (fd1name, sizeof fd1name, "%d", fd1);
|
---|
186 | snprintf (fd2name, sizeof fd2name, "%d", fd2);
|
---|
187 |
|
---|
188 | /* This is the child. Construct the command line. */
|
---|
189 | execl (argv[1], argv[1], argv[2], argv[3], argv[4], "--direct",
|
---|
190 | "--restart", fd1name, fd2name, name1, NULL);
|
---|
191 |
|
---|
192 | error (EXIT_FAILURE, errno, "cannot exec");
|
---|
193 | }
|
---|
194 | else if (pid == (pid_t) -1)
|
---|
195 | error (EXIT_FAILURE, errno, "cannot fork");
|
---|
196 |
|
---|
197 | /* Wait for the child. */
|
---|
198 | if (waitpid (pid, &status, 0) != pid)
|
---|
199 | error (EXIT_FAILURE, errno, "wrong child");
|
---|
200 |
|
---|
201 | if (WTERMSIG (status) != 0)
|
---|
202 | error (EXIT_FAILURE, 0, "Child terminated incorrectly");
|
---|
203 | status = WEXITSTATUS (status);
|
---|
204 |
|
---|
205 | /* Remove the test files. */
|
---|
206 | unlink (name1);
|
---|
207 | unlink (name2);
|
---|
208 |
|
---|
209 | return status;
|
---|
210 | }
|
---|