2002-09-04 Bruno Haible <bruno@clisp.org>
[glibc.git] / test-skeleton.c
blob3216b9451133f360628ab37ea7ee2d5615943a8e
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
19 02111-1307 USA. */
21 #include <getopt.h>
22 #include <search.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/resource.h>
29 #include <sys/wait.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. */
34 #ifndef TEST_FUNCTION
35 # define TEST_FUNCTION do_test (argc, argv)
36 #endif
39 #define OPT_DIRECT 1000
40 #define OPT_TESTDIR 1001
42 static struct option options[] =
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 }
52 /* PID of the test itself. */
53 static int pid;
55 /* Directory to place temporary files in. */
56 static const char *test_dir;
58 /* List of temporary files. */
59 struct temp_name_list
61 struct qelem q;
62 const char *name;
63 } *temp_name_list;
65 /* Add temporary files in list. */
66 static void
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);
72 if (newp != NULL)
74 newp->name = name;
75 if (temp_name_list == NULL)
76 temp_name_list = (struct temp_name_list *) &newp->q;
77 else
78 insque (newp, temp_name_list);
82 /* Delete all temporary files. */
83 static void
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. */
94 static int
95 __attribute__ ((unused))
96 create_temp_file (const char *base, char **filename)
98 char *fname;
99 int fd;
101 fname = (char *) malloc (strlen (test_dir) + 1 + strlen (base)
102 + sizeof ("XXXXXX"));
103 if (fname == NULL)
105 puts ("out of memory");
106 return -1;
108 strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX");
110 fd = mkstemp (fname);
111 if (fd == -1)
113 printf ("cannot open temporary file '%s': %m\n", fname);
114 free (fname);
115 return -1;
118 add_temp_file (fname);
119 if (filename != NULL)
120 *filename = fname;
122 return fd;
125 /* Timeout handler. We kill the child and exit with an error. */
126 static void
127 __attribute__ ((noreturn))
128 timeout_handler (int sig __attribute__ ((unused)))
130 int killed;
132 /* Send signal. */
133 kill (pid, SIGKILL);
135 /* Wait for it to terminate. */
136 killed = waitpid (pid, NULL, WNOHANG);
137 if (killed != 0 && killed != pid)
139 perror ("Failed to killed test process");
140 exit (1);
143 #ifdef CLEANUP_HANDLER
144 CLEANUP_HANDLER;
145 #endif
147 fputs ("Timed out: killed the child process\n", stderr);
149 /* Exit with an error. */
150 exit (1);
153 /* We provide the entry point here. */
155 main (int argc, char *argv[])
157 int direct = 0; /* Directly call the test function? */
158 int status;
159 int opt;
161 #ifdef STDOUT_UNBUFFERED
162 setbuf (stdout, NULL);
163 #endif
165 while ((opt = getopt_long (argc, argv, "", options, NULL)) != -1)
166 switch (opt)
168 case '?':
169 exit (1);
170 case OPT_DIRECT:
171 direct = 1;
172 break;
173 case OPT_TESTDIR:
174 test_dir = optarg;
175 break;
176 #ifdef CMDLINE_PROCESS
177 CMDLINE_PROCESS
178 #endif
181 /* Set TMPDIR to specified test directory. */
182 if (test_dir != NULL)
184 setenv ("TMPDIR", test_dir, 1);
186 if (chdir (test_dir) < 0)
188 perror ("chdir");
189 exit (1);
192 else
194 test_dir = getenv ("TMPDIR");
195 if (test_dir == NULL || test_dir[0] == '\0')
196 test_dir = "/tmp";
199 /* Make sure we see all message, even those on stdout. */
200 setvbuf (stdout, NULL, _IONBF, 0);
202 /* make sure temporary files are deleted. */
203 atexit (delete_temp_files);
205 /* Correct for the possible parameters. */
206 argv += optind - 1;
207 argc -= optind - 1;
209 /* Call the initializing function, if one is available. */
210 #ifdef PREPARE
211 PREPARE (argc, argv);
212 #endif
214 /* If we are not expected to fork run the function immediately. */
215 if (direct)
216 return TEST_FUNCTION;
218 /* Set up the test environment:
219 - prevent core dumps
220 - set up the timer
221 - fork and execute the function. */
223 pid = fork ();
224 if (pid == 0)
226 /* This is the child. */
227 #ifdef RLIMIT_CORE
228 /* Try to avoid dumping core. */
229 struct rlimit core_limit;
230 core_limit.rlim_cur = 0;
231 core_limit.rlim_max = 0;
232 setrlimit (RLIMIT_CORE, &core_limit);
233 #endif
235 /* Execute the test function and exit with the return value. */
236 exit (TEST_FUNCTION);
238 else if (pid < 0)
240 perror ("Cannot fork test program");
241 exit (1);
244 /* Set timeout. */
245 #ifndef TIMEOUT
246 /* Default timeout is two seconds. */
247 # define TIMEOUT 2
248 #endif
249 alarm (TIMEOUT);
250 signal (SIGALRM, timeout_handler);
252 /* Wait for the regular termination. */
253 if (waitpid (pid, &status, 0) != pid)
255 perror ("Oops, wrong test program terminated");
256 exit (1);
259 #ifndef EXPECTED_SIGNAL
260 /* We don't expect any signal. */
261 # define EXPECTED_SIGNAL 0
262 #endif
263 if (WTERMSIG (status) != EXPECTED_SIGNAL)
265 if (EXPECTED_SIGNAL != 0)
266 fprintf (stderr, "Incorrect signal from child: got `%s', need `%s'\n",
267 strsignal (WTERMSIG (status)), strsignal (EXPECTED_SIGNAL));
268 else
269 fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
270 strsignal (WTERMSIG (status)));
271 exit (1);
274 /* Simply exit with the return value of the test. */
275 return WEXITSTATUS (status);