2 Copyright (C) 2000 Free Software Foundation, Inc.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
29 /* Nonzero if the program gets called via `exec'. */
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. */
50 /* The contents of our files. */
51 static const char fd1string
[] = "This file should get closed";
52 static const char fd2string
[] = "This file should stay opened";
55 /* We have a preparation function. */
57 do_prepare (int argc
, char *argv
[])
61 name_len
= strlen (test_dir
);
62 name1
= malloc (name_len
+ sizeof ("/execXXXXXX"));
63 mempcpy (mempcpy (name1
, test_dir
, name_len
),
64 "/execXXXXXX", sizeof ("/execXXXXXX"));
65 add_temp_file (name1
);
67 name2
= malloc (name_len
+ sizeof ("/execXXXXXX"));
68 mempcpy (mempcpy (name2
, test_dir
, name_len
),
69 "/execXXXXXX", sizeof ("/execXXXXXX"));
70 add_temp_file (name2
);
75 handle_restart (const char *fd1s
, const char *fd2s
, const char *name
)
81 /* First get the descriptors. */
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
);
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");
120 do_test (int argc
, char *argv
[])
129 - five parameters left of called initially
134 + the application name
135 - five parameters left if called through re-execution
138 + file descriptor number which is supposed to be closed
139 + the open file descriptor
140 + the name of the closed desriptor
143 error (EXIT_FAILURE
, 0, "wrong number of arguments (%d)", argc
);
146 return handle_restart (argv
[3], argv
[4], argv
[5]);
148 /* Prepare the test. We are creating two files: one which file descriptor
149 will be marked with FD_CLOEXEC, another which is not. */
151 /* Open our test files. */
152 fd1
= mkstemp (name1
);
154 error (EXIT_FAILURE
, errno
, "cannot open test file `%s'", name1
);
155 fd2
= mkstemp (name2
);
157 error (EXIT_FAILURE
, errno
, "cannot open test file `%s'", name2
);
160 flags
= fcntl (fd1
, F_GETFD
, 0);
162 error (EXIT_FAILURE
, errno
, "cannot get flags");
164 if (fcntl (fd1
, F_SETFD
, flags
) < 0)
165 error (EXIT_FAILURE
, errno
, "cannot set flags");
167 /* Write something in the files. */
168 if (write (fd1
, fd1string
, strlen (fd1string
)) != strlen (fd1string
))
169 error (EXIT_FAILURE
, errno
, "cannot write to first file");
170 if (write (fd2
, fd2string
, strlen (fd2string
)) != strlen (fd2string
))
171 error (EXIT_FAILURE
, errno
, "cannot write to second file");
173 /* We want to test the `exec' function. To do this we restart the program
174 with an additional parameter. But first create another process. */
181 snprintf (fd1name
, sizeof fd1name
, "%d", fd1
);
182 snprintf (fd2name
, sizeof fd2name
, "%d", fd2
);
184 /* This is the child. Construct the command line. */
185 execl (argv
[2], argv
[2], argv
[3], argv
[4], argv
[5], "--direct",
186 "--restart", fd1name
, fd2name
, name1
, NULL
);
188 error (EXIT_FAILURE
, errno
, "cannot exec");
190 else if (pid
== (pid_t
) -1)
191 error (EXIT_FAILURE
, errno
, "cannot fork");
193 /* Wait for the child. */
194 if (waitpid (pid
, &status
, 0) != pid
)
195 error (EXIT_FAILURE
, errno
, "wrong child");
197 if (WTERMSIG (status
) != 0)
198 error (EXIT_FAILURE
, 0, "Child terminated incorrectly");
199 status
= WEXITSTATUS (status
);
201 /* Remove the test files. */