]> git.ipfire.org Git - thirdparty/glibc.git/blame - posix/tst-exec.c
regex: simplify by using intprops.h
[thirdparty/glibc.git] / posix / tst-exec.c
CommitLineData
079199de 1/* Tests for exec.
688903eb 2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
41bdb6e2 3 This file is part of the GNU C Library.
079199de
UD
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
079199de
UD
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
41bdb6e2 14 Lesser General Public License for more details.
079199de 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
079199de
UD
19
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>
27
28
29/* Nonzero if the program gets called via `exec'. */
30static int restart;
31
32
33#define CMDLINE_OPTIONS \
34 { "restart", no_argument, &restart, 1 },
35
36/* Prototype for our test function. */
37extern void do_prepare (int argc, char *argv[]);
38extern int do_test (int argc, char *argv[]);
39
40/* We have a preparation function. */
41#define PREPARE do_prepare
42
43#include "../test-skeleton.c"
44
45
46/* Name of the temporary files. */
47static char *name1;
48static char *name2;
49
c22553ef
FW
50/* File descriptors for these temporary files. */
51static int temp_fd1 = -1;
52static int temp_fd2 = -1;
53
079199de
UD
54/* The contents of our files. */
55static const char fd1string[] = "This file should get closed";
56static const char fd2string[] = "This file should stay opened";
57
58
59/* We have a preparation function. */
60void
61do_prepare (int argc, char *argv[])
62{
c22553ef
FW
63 /* We must not open any files in the restart case. */
64 if (restart)
65 return;
66
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);
079199de
UD
71}
72
73
74static int
75handle_restart (const char *fd1s, const char *fd2s, const char *name)
76{
77 char buf[100];
78 int fd1;
79 int fd2;
80
81 /* First get the descriptors. */
82 fd1 = atol (fd1s);
83 fd2 = atol (fd2s);
84
85 /* Sanity check. */
86 if (fd1 == fd2)
87 error (EXIT_FAILURE, 0, "value of fd1 and fd2 is the same");
88
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");
99
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);
103
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);
109
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");
114
115 return 0;
116}
117
118
119int
120do_test (int argc, char *argv[])
121{
122 pid_t pid;
079199de
UD
123 int flags;
124 int status;
125
126 /* We must have
4cf8f209
L
127 - one or four parameters left if called initially
128 + path for ld.so optional
129 + "--library-path" optional
130 + the library path optional
079199de 131 + the application name
726b7b0f 132 - three parameters left if called through re-execution
079199de
UD
133 + file descriptor number which is supposed to be closed
134 + the open file descriptor
135 + the name of the closed desriptor
136 */
079199de
UD
137
138 if (restart)
726b7b0f
UD
139 {
140 if (argc != 4)
141 error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc);
142
143 return handle_restart (argv[1], argv[2], argv[3]);
144 }
145
4cf8f209 146 if (argc != 2 && argc != 5)
726b7b0f 147 error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc);
079199de
UD
148
149 /* Prepare the test. We are creating two files: one which file descriptor
150 will be marked with FD_CLOEXEC, another which is not. */
151
079199de 152 /* Set the bit. */
c22553ef 153 flags = fcntl (temp_fd1, F_GETFD, 0);
079199de
UD
154 if (flags < 0)
155 error (EXIT_FAILURE, errno, "cannot get flags");
156 flags |= FD_CLOEXEC;
c22553ef 157 if (fcntl (temp_fd1, F_SETFD, flags) < 0)
079199de
UD
158 error (EXIT_FAILURE, errno, "cannot set flags");
159
160 /* Write something in the files. */
c22553ef 161 if (write (temp_fd1, fd1string, strlen (fd1string)) != strlen (fd1string))
079199de 162 error (EXIT_FAILURE, errno, "cannot write to first file");
c22553ef 163 if (write (temp_fd2, fd2string, strlen (fd2string)) != strlen (fd2string))
079199de
UD
164 error (EXIT_FAILURE, errno, "cannot write to second file");
165
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)
170 {
171 char fd1name[18];
172 char fd2name[18];
173
c22553ef
FW
174 snprintf (fd1name, sizeof fd1name, "%d", temp_fd1);
175 snprintf (fd2name, sizeof fd2name, "%d", temp_fd2);
079199de
UD
176
177 /* This is the child. Construct the command line. */
4cf8f209
L
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);
079199de
UD
184
185 error (EXIT_FAILURE, errno, "cannot exec");
186 }
187 else if (pid == (pid_t) -1)
188 error (EXIT_FAILURE, errno, "cannot fork");
189
190 /* Wait for the child. */
191 if (waitpid (pid, &status, 0) != pid)
192 error (EXIT_FAILURE, errno, "wrong child");
193
194 if (WTERMSIG (status) != 0)
195 error (EXIT_FAILURE, 0, "Child terminated incorrectly");
196 status = WEXITSTATUS (status);
197
079199de
UD
198 return status;
199}