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. */
26 #include <sys/resource.h>
29 /* The test function is normally called `do_test' and it is called
30 with argc and argv as the arguments. We nevertheless provide the
31 possibility to overwrite this name. */
33 # define TEST_FUNCTION do_test (argc, argv)
37 #define OPT_DIRECT 1000
38 #define OPT_TESTDIR 1001
40 static struct option options
[] =
42 #ifdef CMDLINE_OPTIONS
45 { "direct", no_argument
, NULL
, OPT_DIRECT
},
46 { "test-dir", required_argument
, NULL
, OPT_TESTDIR
},
50 /* PID of the test itself. */
53 /* Directory to place temporary files in. */
54 static const char *test_dir
;
56 /* List of temporary files. */
63 /* Add temporary files in list. */
65 add_temp_file (const char *name
)
67 struct name_list
*newp
= (struct name_list
*) calloc (sizeof (*newp
), 1);
71 if (name_list
== NULL
)
72 name_list
= (struct name_list
*) &newp
->q
;
74 insque (newp
, name_list
);
78 /* Delete all temporary files. */
80 delete_temp_files (void)
82 while (name_list
!= NULL
)
84 remove (name_list
->name
);
85 name_list
= (struct name_list
*) name_list
->q
.q_forw
;
89 /* Timeout handler. We kill the child and exit with an error. */
91 timeout_handler (int sig
__attribute__ ((unused
)))
98 /* Wait for it to terminate. */
99 killed
= waitpid (pid
, NULL
, WNOHANG
);
100 if (killed
!= 0 && killed
!= pid
)
102 perror ("Failed to killed test process");
106 #ifdef CLEANUP_HANDLER
110 fputs ("Timed out: killed the child process\n", stderr
);
112 /* Exit with an error. */
116 /* We provide the entry point here. */
118 main (int argc
, char *argv
[])
120 int direct
= 0; /* Directly call the test function? */
124 while ((opt
= getopt_long (argc
, argv
, "", options
, NULL
)) != -1)
135 #ifdef CMDLINE_PROCESS
140 /* Set TMPDIR to specified test directory. */
141 if (test_dir
!= NULL
)
143 setenv ("TMPDIR", test_dir
, 1);
145 if (chdir (test_dir
) < 0)
153 test_dir
= getenv ("TMPDIR");
154 if (test_dir
== NULL
|| test_dir
[0] == '\0')
158 /* Make sure we see all message, even those on stdout. */
159 setvbuf (stdout
, NULL
, _IONBF
, 0);
161 /* make sure temporary files are deleted. */
162 atexit (delete_temp_files
);
164 /* Call the initializing function, if one is available. */
166 PREPARE (argc
, argv
);
169 /* If we are not expected to fork run the function immediately. */
171 return TEST_FUNCTION
;
173 /* Set up the test environment:
176 - fork and execute the function. */
181 /* This is the child. */
183 /* Try to avoid dumping core. */
184 struct rlimit core_limit
;
185 core_limit
.rlim_cur
= 0;
186 core_limit
.rlim_max
= 0;
187 setrlimit (RLIMIT_CORE
, &core_limit
);
190 /* Execute the test function and exit with the return value. */
191 exit (TEST_FUNCTION
);
195 perror ("Cannot fork test program");
201 /* Default timeout is two seconds. */
205 signal (SIGALRM
, timeout_handler
);
207 /* Wait for the regular termination. */
208 if (waitpid (pid
, &status
, 0) != pid
)
210 perror ("Oops, wrong test program terminated");
214 #ifndef EXPECTED_SIGNAL
215 /* We don't expect any signal. */
216 # define EXPECTED_SIGNAL 0
218 if (WTERMSIG (status
) != EXPECTED_SIGNAL
)
220 if (EXPECTED_SIGNAL
!= 0)
221 fprintf (stderr
, "Incorrect signal from child: got `%s', need `%s'\n",
222 strsignal (WTERMSIG (status
)), strsignal (EXPECTED_SIGNAL
));
224 fprintf (stderr
, "Didn't expect signal from child: got `%s'\n",
225 strsignal (WTERMSIG (status
)));
229 /* Simply exit with the return value of the test. */
230 return WEXITSTATUS (status
);