1 /* Skeleton for test programs.
2 Copyright (C) 1998 Free Software Foundation, Inc.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
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. */
25 #include <sys/resource.h>
28 /* The test function is normally called `do_test' and it is called
29 with argc and argv as the arguments. We nevertheless provide the
30 possibility to overwrite this name. */
32 # define TEST_FUNCTION do_test (argc, argv)
36 #define OPT_DIRECT 1000
37 #define OPT_TESTDIR 1001
39 static struct option options
[] =
41 #ifdef CMDLINE_OPTIONS
44 { "direct", no_argument
, NULL
, OPT_DIRECT
},
45 { "test-dir", required_argument
, NULL
, OPT_TESTDIR
},
49 /* PID of the test itself. */
52 /* Directory to place temporary files in. */
53 static const char *test_dir
;
55 /* Timeout handler. We kill the child and exit with an error. */
57 timeout_handler (int sig
__attribute__ ((unused
)))
64 /* Wait for it to terminate. */
65 killed
= waitpid (pid
, NULL
, WNOHANG
);
66 if (killed
!= 0 && killed
!= pid
)
68 perror ("Failed to killed test process");
72 #ifdef CLEANUP_HANDLER
76 fputs ("Timed out: killed the child process\n", stderr
);
78 /* Exit with an error. */
82 /* We provide the entry point here. */
84 main (int argc
, char *argv
[])
86 int direct
= 0; /* Directly call the test function? */
90 while ((opt
= getopt_long (argc
, argv
, "", options
, NULL
)) != -1)
101 #ifdef CMDLINE_PROCESS
106 /* Set TMPDIR to specified test directory. */
107 if (test_dir
!= NULL
)
109 setenv ("TMPDIR", test_dir
, 1);
111 if (chdir (test_dir
) < 0)
118 /* If we are not expected to fork run the function immediately. */
120 return TEST_FUNCTION
;
122 /* Set up the test environment:
125 - fork and execute the function. */
130 /* This is the child. */
132 /* Try to avoid dumping core. */
133 struct rlimit core_limit
;
134 core_limit
.rlim_cur
= 0;
135 core_limit
.rlim_max
= 0;
136 setrlimit (RLIMIT_CORE
, &core_limit
);
139 /* Execute the test function and exit with the return value. */
140 exit (TEST_FUNCTION
);
144 perror ("Cannot fork test program");
150 /* Default timeout is two seconds. */
154 signal (SIGALRM
, timeout_handler
);
156 /* Wait for the regular termination. */
157 if (waitpid (pid
, &status
, 0) != pid
)
159 perror ("Oops, wrong test program terminated");
163 #ifndef EXPECTED_SIGNAL
164 /* We don't expect any signal. */
165 # define EXPECTED_SIGNAL 0
167 if (WTERMSIG (status
) != EXPECTED_SIGNAL
)
169 fprintf (stderr
, "Incorrect signal from child: got `%s', need `%s'\n",
170 strsignal (WTERMSIG (status
)), strsignal (EXPECTED_SIGNAL
));
174 /* Simply exit with the return value of the test. */
175 return WEXITSTATUS (status
);