1998-03-18 20:22:11 +08:00
|
|
|
/* Skeleton for test programs.
|
2000-04-02 05:49:24 +08:00
|
|
|
Copyright (C) 1998, 2000 Free Software Foundation, Inc.
|
2001-07-06 12:58:11 +08:00
|
|
|
This file is part of the GNU C Library.
|
1998-03-18 20:22:11 +08:00
|
|
|
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 12:58:11 +08:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
1998-03-18 20:22:11 +08:00
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2001-07-06 12:58:11 +08:00
|
|
|
Lesser General Public License for more details.
|
1998-03-18 20:22:11 +08:00
|
|
|
|
2001-07-06 12:58:11 +08:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
02111-1307 USA. */
|
1998-03-18 20:22:11 +08:00
|
|
|
|
|
|
|
#include <getopt.h>
|
1998-04-11 17:51:01 +08:00
|
|
|
#include <search.h>
|
1998-03-18 20:22:11 +08:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2000-04-02 05:49:24 +08:00
|
|
|
#include <string.h>
|
1998-03-18 20:22:11 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
/* The test function is normally called `do_test' and it is called
|
|
|
|
with argc and argv as the arguments. We nevertheless provide the
|
|
|
|
possibility to overwrite this name. */
|
|
|
|
#ifndef TEST_FUNCTION
|
|
|
|
# define TEST_FUNCTION do_test (argc, argv)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#define OPT_DIRECT 1000
|
|
|
|
#define OPT_TESTDIR 1001
|
|
|
|
|
|
|
|
static struct option options[] =
|
|
|
|
{
|
|
|
|
#ifdef CMDLINE_OPTIONS
|
|
|
|
CMDLINE_OPTIONS
|
|
|
|
#endif
|
|
|
|
{ "direct", no_argument, NULL, OPT_DIRECT },
|
|
|
|
{ "test-dir", required_argument, NULL, OPT_TESTDIR },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
/* PID of the test itself. */
|
|
|
|
static int pid;
|
|
|
|
|
|
|
|
/* Directory to place temporary files in. */
|
|
|
|
static const char *test_dir;
|
|
|
|
|
1998-04-11 17:51:01 +08:00
|
|
|
/* List of temporary files. */
|
2000-12-19 00:40:40 +08:00
|
|
|
struct temp_name_list
|
1998-04-11 17:51:01 +08:00
|
|
|
{
|
|
|
|
struct qelem q;
|
|
|
|
const char *name;
|
2000-12-19 00:40:40 +08:00
|
|
|
} *temp_name_list;
|
1998-04-11 17:51:01 +08:00
|
|
|
|
|
|
|
/* Add temporary files in list. */
|
2000-11-25 07:39:37 +08:00
|
|
|
static void
|
1998-04-11 17:51:01 +08:00
|
|
|
add_temp_file (const char *name)
|
|
|
|
{
|
2000-12-19 00:40:40 +08:00
|
|
|
struct temp_name_list *newp
|
|
|
|
= (struct temp_name_list *) calloc (sizeof (*newp), 1);
|
1998-04-11 17:51:01 +08:00
|
|
|
if (newp != NULL)
|
|
|
|
{
|
|
|
|
newp->name = name;
|
2000-12-19 00:40:40 +08:00
|
|
|
if (temp_name_list == NULL)
|
|
|
|
temp_name_list = (struct temp_name_list *) &newp->q;
|
1998-04-11 17:51:01 +08:00
|
|
|
else
|
2000-12-19 00:40:40 +08:00
|
|
|
insque (newp, temp_name_list);
|
1998-04-11 17:51:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete all temporary files. */
|
2000-11-25 07:39:37 +08:00
|
|
|
static void
|
1998-04-11 17:51:01 +08:00
|
|
|
delete_temp_files (void)
|
|
|
|
{
|
2000-12-19 00:40:40 +08:00
|
|
|
while (temp_name_list != NULL)
|
1998-04-11 17:51:01 +08:00
|
|
|
{
|
2000-12-19 00:40:40 +08:00
|
|
|
remove (temp_name_list->name);
|
|
|
|
temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw;
|
1998-04-11 17:51:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-03-18 20:22:11 +08:00
|
|
|
/* Timeout handler. We kill the child and exit with an error. */
|
2000-11-25 07:39:37 +08:00
|
|
|
static void
|
2000-11-28 15:10:56 +08:00
|
|
|
__attribute__ ((noreturn))
|
1998-03-18 20:22:11 +08:00
|
|
|
timeout_handler (int sig __attribute__ ((unused)))
|
|
|
|
{
|
|
|
|
int killed;
|
|
|
|
|
|
|
|
/* Send signal. */
|
|
|
|
kill (pid, SIGKILL);
|
|
|
|
|
|
|
|
/* Wait for it to terminate. */
|
|
|
|
killed = waitpid (pid, NULL, WNOHANG);
|
|
|
|
if (killed != 0 && killed != pid)
|
|
|
|
{
|
|
|
|
perror ("Failed to killed test process");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CLEANUP_HANDLER
|
|
|
|
CLEANUP_HANDLER;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fputs ("Timed out: killed the child process\n", stderr);
|
|
|
|
|
|
|
|
/* Exit with an error. */
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We provide the entry point here. */
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int direct = 0; /* Directly call the test function? */
|
|
|
|
int status;
|
|
|
|
int opt;
|
|
|
|
|
2000-04-12 00:27:38 +08:00
|
|
|
#ifdef STDOUT_UNBUFFERED
|
|
|
|
setbuf (stdout, NULL);
|
|
|
|
#endif
|
|
|
|
|
1998-03-18 20:22:11 +08:00
|
|
|
while ((opt = getopt_long (argc, argv, "", options, NULL)) != -1)
|
|
|
|
switch (opt)
|
|
|
|
{
|
|
|
|
case '?':
|
|
|
|
exit (1);
|
|
|
|
case OPT_DIRECT:
|
|
|
|
direct = 1;
|
|
|
|
break;
|
|
|
|
case OPT_TESTDIR:
|
|
|
|
test_dir = optarg;
|
|
|
|
break;
|
|
|
|
#ifdef CMDLINE_PROCESS
|
|
|
|
CMDLINE_PROCESS
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set TMPDIR to specified test directory. */
|
|
|
|
if (test_dir != NULL)
|
|
|
|
{
|
|
|
|
setenv ("TMPDIR", test_dir, 1);
|
|
|
|
|
|
|
|
if (chdir (test_dir) < 0)
|
|
|
|
{
|
|
|
|
perror ("chdir");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
1998-04-11 17:51:01 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
test_dir = getenv ("TMPDIR");
|
|
|
|
if (test_dir == NULL || test_dir[0] == '\0')
|
|
|
|
test_dir = "/tmp";
|
|
|
|
}
|
1998-03-18 20:22:11 +08:00
|
|
|
|
1998-04-12 04:19:13 +08:00
|
|
|
/* Make sure we see all message, even those on stdout. */
|
|
|
|
setvbuf (stdout, NULL, _IONBF, 0);
|
|
|
|
|
1998-04-15 00:51:08 +08:00
|
|
|
/* make sure temporary files are deleted. */
|
|
|
|
atexit (delete_temp_files);
|
|
|
|
|
2000-10-26 16:11:19 +08:00
|
|
|
/* Correct for the possible parameters. */
|
|
|
|
argv += optind - 1;
|
|
|
|
argc -= optind - 1;
|
|
|
|
|
1998-04-15 00:51:08 +08:00
|
|
|
/* Call the initializing function, if one is available. */
|
|
|
|
#ifdef PREPARE
|
|
|
|
PREPARE (argc, argv);
|
|
|
|
#endif
|
|
|
|
|
1998-03-18 20:22:11 +08:00
|
|
|
/* If we are not expected to fork run the function immediately. */
|
|
|
|
if (direct)
|
|
|
|
return TEST_FUNCTION;
|
|
|
|
|
|
|
|
/* Set up the test environment:
|
|
|
|
- prevent core dumps
|
|
|
|
- set up the timer
|
|
|
|
- fork and execute the function. */
|
|
|
|
|
|
|
|
pid = fork ();
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
|
|
|
/* This is the child. */
|
|
|
|
#ifdef RLIMIT_CORE
|
|
|
|
/* Try to avoid dumping core. */
|
|
|
|
struct rlimit core_limit;
|
|
|
|
core_limit.rlim_cur = 0;
|
|
|
|
core_limit.rlim_max = 0;
|
|
|
|
setrlimit (RLIMIT_CORE, &core_limit);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Execute the test function and exit with the return value. */
|
|
|
|
exit (TEST_FUNCTION);
|
|
|
|
}
|
|
|
|
else if (pid < 0)
|
|
|
|
{
|
|
|
|
perror ("Cannot fork test program");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set timeout. */
|
|
|
|
#ifndef TIMEOUT
|
|
|
|
/* Default timeout is two seconds. */
|
|
|
|
# define TIMEOUT 2
|
|
|
|
#endif
|
|
|
|
alarm (TIMEOUT);
|
|
|
|
signal (SIGALRM, timeout_handler);
|
|
|
|
|
|
|
|
/* Wait for the regular termination. */
|
|
|
|
if (waitpid (pid, &status, 0) != pid)
|
|
|
|
{
|
|
|
|
perror ("Oops, wrong test program terminated");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef EXPECTED_SIGNAL
|
|
|
|
/* We don't expect any signal. */
|
|
|
|
# define EXPECTED_SIGNAL 0
|
|
|
|
#endif
|
1998-03-23 19:53:10 +08:00
|
|
|
if (WTERMSIG (status) != EXPECTED_SIGNAL)
|
1998-03-18 20:22:11 +08:00
|
|
|
{
|
1998-04-11 17:51:01 +08:00
|
|
|
if (EXPECTED_SIGNAL != 0)
|
|
|
|
fprintf (stderr, "Incorrect signal from child: got `%s', need `%s'\n",
|
|
|
|
strsignal (WTERMSIG (status)), strsignal (EXPECTED_SIGNAL));
|
|
|
|
else
|
1998-04-12 04:19:13 +08:00
|
|
|
fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
|
1998-04-11 17:51:01 +08:00
|
|
|
strsignal (WTERMSIG (status)));
|
1998-03-18 20:22:11 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Simply exit with the return value of the test. */
|
|
|
|
return WEXITSTATUS (status);
|
|
|
|
}
|