]> git.ipfire.org Git - thirdparty/glibc.git/blame - test-skeleton.c
2002-09-29 Roland McGrath <roland@redhat.com>
[thirdparty/glibc.git] / test-skeleton.c
CommitLineData
80a18298 1/* Skeleton for test programs.
10e62564 2 Copyright (C) 1998, 2000, 2001, 2002 Free Software Foundation, Inc.
41bdb6e2 3 This file is part of the GNU C Library.
80a18298
UD
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
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.
80a18298
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.
80a18298 15
41bdb6e2
AJ
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. */
80a18298
UD
20
21#include <getopt.h>
b9337b6a 22#include <search.h>
80a18298
UD
23#include <signal.h>
24#include <stdio.h>
25#include <stdlib.h>
4380ef5e 26#include <string.h>
80a18298
UD
27#include <unistd.h>
28#include <sys/resource.h>
29#include <sys/wait.h>
30
31/* The test function is normally called `do_test' and it is called
32 with argc and argv as the arguments. We nevertheless provide the
33 possibility to overwrite this name. */
34#ifndef TEST_FUNCTION
35# define TEST_FUNCTION do_test (argc, argv)
36#endif
37
38
39#define OPT_DIRECT 1000
40#define OPT_TESTDIR 1001
41
42static struct option options[] =
43{
44#ifdef CMDLINE_OPTIONS
45 CMDLINE_OPTIONS
46#endif
47 { "direct", no_argument, NULL, OPT_DIRECT },
48 { "test-dir", required_argument, NULL, OPT_TESTDIR },
49 { NULL, 0, NULL, 0 }
50};
51
52/* PID of the test itself. */
608c5afd 53static pid_t pid;
80a18298
UD
54
55/* Directory to place temporary files in. */
56static const char *test_dir;
57
b9337b6a 58/* List of temporary files. */
51eecc4a 59struct temp_name_list
b9337b6a
UD
60{
61 struct qelem q;
62 const char *name;
51eecc4a 63} *temp_name_list;
b9337b6a
UD
64
65/* Add temporary files in list. */
9c0592ab 66static void
5cd6f8f7 67__attribute__ ((unused))
b9337b6a
UD
68add_temp_file (const char *name)
69{
51eecc4a
AJ
70 struct temp_name_list *newp
71 = (struct temp_name_list *) calloc (sizeof (*newp), 1);
b9337b6a
UD
72 if (newp != NULL)
73 {
74 newp->name = name;
51eecc4a
AJ
75 if (temp_name_list == NULL)
76 temp_name_list = (struct temp_name_list *) &newp->q;
b9337b6a 77 else
51eecc4a 78 insque (newp, temp_name_list);
b9337b6a
UD
79 }
80}
81
82/* Delete all temporary files. */
9c0592ab 83static void
b9337b6a
UD
84delete_temp_files (void)
85{
51eecc4a 86 while (temp_name_list != NULL)
b9337b6a 87 {
51eecc4a
AJ
88 remove (temp_name_list->name);
89 temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw;
b9337b6a
UD
90 }
91}
92
10e62564
UD
93/* Create a temporary file. */
94static int
95__attribute__ ((unused))
96create_temp_file (const char *base, char **filename)
97{
98 char *fname;
99 int fd;
100
101 fname = (char *) malloc (strlen (test_dir) + 1 + strlen (base)
102 + sizeof ("XXXXXX"));
103 if (fname == NULL)
104 {
105 puts ("out of memory");
106 return -1;
107 }
108 strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX");
109
110 fd = mkstemp (fname);
111 if (fd == -1)
112 {
113 printf ("cannot open temporary file '%s': %m\n", fname);
114 free (fname);
115 return -1;
116 }
117
118 add_temp_file (fname);
119 if (filename != NULL)
120 *filename = fname;
121
122 return fd;
123}
124
80a18298 125/* Timeout handler. We kill the child and exit with an error. */
9c0592ab 126static void
8c0b7170 127__attribute__ ((noreturn))
80a18298
UD
128timeout_handler (int sig __attribute__ ((unused)))
129{
130 int killed;
b79e3737 131 int status;
80a18298
UD
132
133 /* Send signal. */
134 kill (pid, SIGKILL);
135
136 /* Wait for it to terminate. */
b79e3737 137 killed = waitpid (pid, &status, WNOHANG|WUNTRACED);
80a18298
UD
138 if (killed != 0 && killed != pid)
139 {
140 perror ("Failed to killed test process");
141 exit (1);
142 }
143
144#ifdef CLEANUP_HANDLER
145 CLEANUP_HANDLER;
146#endif
147
b79e3737
RM
148 if (WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL)
149 fputs ("Timed out: killed the child process\n", stderr);
150 else if (WIFSTOPPED (status))
151 fprintf (stderr, "Timed out: the child process was %s\n",
152 strsignal (WSTOPSIG (status)));
153 else if (WIFSIGNALED (status))
154 fprintf (stderr, "Timed out: the child process got signal %s\n",
155 strsignal (WTERMSIG (status)));
156 else
157 fprintf (stderr, "Timed out: killed the child process but it exited %d\n",
158 WEXITSTATUS (status));
80a18298
UD
159
160 /* Exit with an error. */
161 exit (1);
162}
163
164/* We provide the entry point here. */
165int
166main (int argc, char *argv[])
167{
168 int direct = 0; /* Directly call the test function? */
169 int status;
170 int opt;
608c5afd 171 pid_t termpid;
80a18298 172
e7c036b3
UD
173#ifdef STDOUT_UNBUFFERED
174 setbuf (stdout, NULL);
175#endif
176
80a18298
UD
177 while ((opt = getopt_long (argc, argv, "", options, NULL)) != -1)
178 switch (opt)
179 {
180 case '?':
181 exit (1);
182 case OPT_DIRECT:
183 direct = 1;
184 break;
185 case OPT_TESTDIR:
186 test_dir = optarg;
187 break;
188#ifdef CMDLINE_PROCESS
189 CMDLINE_PROCESS
190#endif
191 }
192
193 /* Set TMPDIR to specified test directory. */
194 if (test_dir != NULL)
195 {
196 setenv ("TMPDIR", test_dir, 1);
197
198 if (chdir (test_dir) < 0)
199 {
200 perror ("chdir");
201 exit (1);
202 }
203 }
b9337b6a
UD
204 else
205 {
206 test_dir = getenv ("TMPDIR");
207 if (test_dir == NULL || test_dir[0] == '\0')
208 test_dir = "/tmp";
209 }
80a18298 210
6b9c2e67
UD
211 /* Make sure we see all message, even those on stdout. */
212 setvbuf (stdout, NULL, _IONBF, 0);
213
310b3460
UD
214 /* make sure temporary files are deleted. */
215 atexit (delete_temp_files);
216
726b7b0f
UD
217 /* Correct for the possible parameters. */
218 argv += optind - 1;
219 argc -= optind - 1;
220
310b3460
UD
221 /* Call the initializing function, if one is available. */
222#ifdef PREPARE
223 PREPARE (argc, argv);
224#endif
225
80a18298
UD
226 /* If we are not expected to fork run the function immediately. */
227 if (direct)
228 return TEST_FUNCTION;
229
230 /* Set up the test environment:
231 - prevent core dumps
232 - set up the timer
233 - fork and execute the function. */
234
235 pid = fork ();
236 if (pid == 0)
237 {
238 /* This is the child. */
239#ifdef RLIMIT_CORE
240 /* Try to avoid dumping core. */
241 struct rlimit core_limit;
242 core_limit.rlim_cur = 0;
243 core_limit.rlim_max = 0;
244 setrlimit (RLIMIT_CORE, &core_limit);
245#endif
246
b79e3737
RM
247 /* We put the test process in its own pgrp so that if it bogusly
248 generates any job control signals, they won't hit the whole build. */
249 setpgid (0, 0);
250
80a18298
UD
251 /* Execute the test function and exit with the return value. */
252 exit (TEST_FUNCTION);
253 }
254 else if (pid < 0)
255 {
256 perror ("Cannot fork test program");
257 exit (1);
258 }
259
260 /* Set timeout. */
261#ifndef TIMEOUT
262 /* Default timeout is two seconds. */
263# define TIMEOUT 2
264#endif
265 alarm (TIMEOUT);
266 signal (SIGALRM, timeout_handler);
267
268 /* Wait for the regular termination. */
608c5afd
UD
269 termpid = waitpid (pid, &status, 0);
270 if (termpid == -1)
80a18298 271 {
608c5afd
UD
272 printf ("Waiting for test program failed: %m\n");
273 exit (1);
274 }
275 if (termpid != pid)
276 {
277 printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
278 (long int) pid, (long int) termpid);
80a18298
UD
279 exit (1);
280 }
281
282#ifndef EXPECTED_SIGNAL
283 /* We don't expect any signal. */
284# define EXPECTED_SIGNAL 0
285#endif
789b13c4 286 if (WTERMSIG (status) != EXPECTED_SIGNAL)
80a18298 287 {
b9337b6a 288 if (EXPECTED_SIGNAL != 0)
3fc65a77
UD
289 {
290 if (WTERMSIG (status) == 0)
291 fprintf (stderr,
292 "Expected signal '%s' from child, got none\n",
293 strsignal (EXPECTED_SIGNAL));
294 else
295 fprintf (stderr,
296 "Incorrect signal from child: got `%s', need `%s'\n",
297 strsignal (WTERMSIG (status)),
298 strsignal (EXPECTED_SIGNAL));
299 }
b9337b6a 300 else
6b9c2e67 301 fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
b9337b6a 302 strsignal (WTERMSIG (status)));
80a18298
UD
303 exit (1);
304 }
305
306 /* Simply exit with the return value of the test. */
307 return WEXITSTATUS (status);
308}