Update.
[glibc.git] / test-skeleton.c
blob9126be0dbed4ddf27a813321e551a8426ded020a
1 /* Skeleton for test programs.
2 Copyright (C) 1998, 2000 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. */
20 #include <getopt.h>
21 #include <search.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/resource.h>
28 #include <sys/wait.h>
30 /* The test function is normally called `do_test' and it is called
31 with argc and argv as the arguments. We nevertheless provide the
32 possibility to overwrite this name. */
33 #ifndef TEST_FUNCTION
34 # define TEST_FUNCTION do_test (argc, argv)
35 #endif
38 #define OPT_DIRECT 1000
39 #define OPT_TESTDIR 1001
41 static struct option options[] =
43 #ifdef CMDLINE_OPTIONS
44 CMDLINE_OPTIONS
45 #endif
46 { "direct", no_argument, NULL, OPT_DIRECT },
47 { "test-dir", required_argument, NULL, OPT_TESTDIR },
48 { NULL, 0, NULL, 0 }
51 /* PID of the test itself. */
52 static int pid;
54 /* Directory to place temporary files in. */
55 static const char *test_dir;
57 /* List of temporary files. */
58 struct temp_name_list
60 struct qelem q;
61 const char *name;
62 } *temp_name_list;
64 /* Add temporary files in list. */
65 static void
66 add_temp_file (const char *name)
68 struct temp_name_list *newp
69 = (struct temp_name_list *) calloc (sizeof (*newp), 1);
70 if (newp != NULL)
72 newp->name = name;
73 if (temp_name_list == NULL)
74 temp_name_list = (struct temp_name_list *) &newp->q;
75 else
76 insque (newp, temp_name_list);
80 /* Delete all temporary files. */
81 static void
82 delete_temp_files (void)
84 while (temp_name_list != NULL)
86 remove (temp_name_list->name);
87 temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw;
91 /* Timeout handler. We kill the child and exit with an error. */
92 static void
93 __attribute__ ((noreturn))
94 timeout_handler (int sig __attribute__ ((unused)))
96 int killed;
98 /* Send signal. */
99 kill (pid, SIGKILL);
101 /* Wait for it to terminate. */
102 killed = waitpid (pid, NULL, WNOHANG);
103 if (killed != 0 && killed != pid)
105 perror ("Failed to killed test process");
106 exit (1);
109 #ifdef CLEANUP_HANDLER
110 CLEANUP_HANDLER;
111 #endif
113 fputs ("Timed out: killed the child process\n", stderr);
115 /* Exit with an error. */
116 exit (1);
119 /* We provide the entry point here. */
121 main (int argc, char *argv[])
123 int direct = 0; /* Directly call the test function? */
124 int status;
125 int opt;
127 #ifdef STDOUT_UNBUFFERED
128 setbuf (stdout, NULL);
129 #endif
131 while ((opt = getopt_long (argc, argv, "", options, NULL)) != -1)
132 switch (opt)
134 case '?':
135 exit (1);
136 case OPT_DIRECT:
137 direct = 1;
138 break;
139 case OPT_TESTDIR:
140 test_dir = optarg;
141 break;
142 #ifdef CMDLINE_PROCESS
143 CMDLINE_PROCESS
144 #endif
147 /* Set TMPDIR to specified test directory. */
148 if (test_dir != NULL)
150 setenv ("TMPDIR", test_dir, 1);
152 if (chdir (test_dir) < 0)
154 perror ("chdir");
155 exit (1);
158 else
160 test_dir = getenv ("TMPDIR");
161 if (test_dir == NULL || test_dir[0] == '\0')
162 test_dir = "/tmp";
165 /* Make sure we see all message, even those on stdout. */
166 setvbuf (stdout, NULL, _IONBF, 0);
168 /* make sure temporary files are deleted. */
169 atexit (delete_temp_files);
171 /* Correct for the possible parameters. */
172 argv += optind - 1;
173 argc -= optind - 1;
175 /* Call the initializing function, if one is available. */
176 #ifdef PREPARE
177 PREPARE (argc, argv);
178 #endif
180 /* If we are not expected to fork run the function immediately. */
181 if (direct)
182 return TEST_FUNCTION;
184 /* Set up the test environment:
185 - prevent core dumps
186 - set up the timer
187 - fork and execute the function. */
189 pid = fork ();
190 if (pid == 0)
192 /* This is the child. */
193 #ifdef RLIMIT_CORE
194 /* Try to avoid dumping core. */
195 struct rlimit core_limit;
196 core_limit.rlim_cur = 0;
197 core_limit.rlim_max = 0;
198 setrlimit (RLIMIT_CORE, &core_limit);
199 #endif
201 /* Execute the test function and exit with the return value. */
202 exit (TEST_FUNCTION);
204 else if (pid < 0)
206 perror ("Cannot fork test program");
207 exit (1);
210 /* Set timeout. */
211 #ifndef TIMEOUT
212 /* Default timeout is two seconds. */
213 # define TIMEOUT 2
214 #endif
215 alarm (TIMEOUT);
216 signal (SIGALRM, timeout_handler);
218 /* Wait for the regular termination. */
219 if (waitpid (pid, &status, 0) != pid)
221 perror ("Oops, wrong test program terminated");
222 exit (1);
225 #ifndef EXPECTED_SIGNAL
226 /* We don't expect any signal. */
227 # define EXPECTED_SIGNAL 0
228 #endif
229 if (WTERMSIG (status) != EXPECTED_SIGNAL)
231 if (EXPECTED_SIGNAL != 0)
232 fprintf (stderr, "Incorrect signal from child: got `%s', need `%s'\n",
233 strsignal (WTERMSIG (status)), strsignal (EXPECTED_SIGNAL));
234 else
235 fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
236 strsignal (WTERMSIG (status)));
237 exit (1);
240 /* Simply exit with the return value of the test. */
241 return WEXITSTATUS (status);