1 /* Skeleton for test programs.
2 Copyright (C) 1998, 2000, 2001, 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #include <sys/resource.h>
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. */
35 # define TEST_FUNCTION do_test (argc, argv)
39 #define OPT_DIRECT 1000
40 #define OPT_TESTDIR 1001
42 static struct option options
[] =
44 #ifdef CMDLINE_OPTIONS
47 { "direct", no_argument
, NULL
, OPT_DIRECT
},
48 { "test-dir", required_argument
, NULL
, OPT_TESTDIR
},
52 /* PID of the test itself. */
55 /* Directory to place temporary files in. */
56 static const char *test_dir
;
58 /* List of temporary files. */
65 /* Add temporary files in list. */
67 __attribute__ ((unused
))
68 add_temp_file (const char *name
)
70 struct temp_name_list
*newp
71 = (struct temp_name_list
*) calloc (sizeof (*newp
), 1);
75 if (temp_name_list
== NULL
)
76 temp_name_list
= (struct temp_name_list
*) &newp
->q
;
78 insque (newp
, temp_name_list
);
82 /* Delete all temporary files. */
84 delete_temp_files (void)
86 while (temp_name_list
!= NULL
)
88 remove (temp_name_list
->name
);
89 temp_name_list
= (struct temp_name_list
*) temp_name_list
->q
.q_forw
;
93 /* Create a temporary file. */
95 __attribute__ ((unused
))
96 create_temp_file (const char *base
, char **filename
)
101 fname
= (char *) malloc (strlen (test_dir
) + 1 + strlen (base
)
102 + sizeof ("XXXXXX"));
105 puts ("out of memory");
108 strcpy (stpcpy (stpcpy (stpcpy (fname
, test_dir
), "/"), base
), "XXXXXX");
110 fd
= mkstemp (fname
);
113 printf ("cannot open temporary file '%s': %m\n", fname
);
118 add_temp_file (fname
);
119 if (filename
!= NULL
)
125 /* Timeout handler. We kill the child and exit with an error. */
127 __attribute__ ((noreturn
))
128 timeout_handler (int sig
__attribute__ ((unused
)))
136 /* Wait for it to terminate. */
137 killed
= waitpid (pid
, &status
, WNOHANG
|WUNTRACED
);
138 if (killed
!= 0 && killed
!= pid
)
140 perror ("Failed to killed test process");
144 #ifdef CLEANUP_HANDLER
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
)));
157 fprintf (stderr
, "Timed out: killed the child process but it exited %d\n",
158 WEXITSTATUS (status
));
160 /* Exit with an error. */
164 /* We provide the entry point here. */
166 main (int argc
, char *argv
[])
168 int direct
= 0; /* Directly call the test function? */
173 #ifdef STDOUT_UNBUFFERED
174 setbuf (stdout
, NULL
);
177 while ((opt
= getopt_long (argc
, argv
, "", options
, NULL
)) != -1)
188 #ifdef CMDLINE_PROCESS
193 /* Set TMPDIR to specified test directory. */
194 if (test_dir
!= NULL
)
196 setenv ("TMPDIR", test_dir
, 1);
198 if (chdir (test_dir
) < 0)
206 test_dir
= getenv ("TMPDIR");
207 if (test_dir
== NULL
|| test_dir
[0] == '\0')
211 /* Make sure we see all message, even those on stdout. */
212 setvbuf (stdout
, NULL
, _IONBF
, 0);
214 /* make sure temporary files are deleted. */
215 atexit (delete_temp_files
);
217 /* Correct for the possible parameters. */
221 /* Call the initializing function, if one is available. */
223 PREPARE (argc
, argv
);
226 /* If we are not expected to fork run the function immediately. */
228 return TEST_FUNCTION
;
230 /* Set up the test environment:
233 - fork and execute the function. */
238 /* This is the child. */
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
);
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. */
251 /* Execute the test function and exit with the return value. */
252 exit (TEST_FUNCTION
);
256 perror ("Cannot fork test program");
262 /* Default timeout is two seconds. */
266 signal (SIGALRM
, timeout_handler
);
268 /* Wait for the regular termination. */
269 termpid
= waitpid (pid
, &status
, 0);
272 printf ("Waiting for test program failed: %m\n");
277 printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
278 (long int) pid
, (long int) termpid
);
282 #ifndef EXPECTED_SIGNAL
283 /* We don't expect any signal. */
284 # define EXPECTED_SIGNAL 0
286 if (WTERMSIG (status
) != EXPECTED_SIGNAL
)
288 if (EXPECTED_SIGNAL
!= 0)
290 if (WTERMSIG (status
) == 0)
292 "Expected signal '%s' from child, got none\n",
293 strsignal (EXPECTED_SIGNAL
));
296 "Incorrect signal from child: got `%s', need `%s'\n",
297 strsignal (WTERMSIG (status
)),
298 strsignal (EXPECTED_SIGNAL
));
301 fprintf (stderr
, "Didn't expect signal from child: got `%s'\n",
302 strsignal (WTERMSIG (status
)));
306 /* Simply exit with the return value of the test. */
307 return WEXITSTATUS (status
);