]> git.ipfire.org Git - thirdparty/glibc.git/blame - test-skeleton.c
Update.
[thirdparty/glibc.git] / test-skeleton.c
CommitLineData
80a18298 1/* Skeleton for test programs.
5cd6f8f7 2 Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc.
41bdb6e2 3 This file is part of the GNU C Library.
80a18298
UD
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
80a18298
UD
10
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
41bdb6e2 14 Lesser General Public License for more details.
80a18298 15
41bdb6e2
AJ
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. */
80a18298
UD
20
21#include <getopt.h>
b9337b6a 22#include <search.h>
80a18298
UD
23#include <signal.h>
24#include <stdio.h>
25#include <stdlib.h>
4380ef5e 26#include <string.h>
80a18298
UD
27#include <unistd.h>
28#include <sys/resource.h>
29#include <sys/wait.h>
30
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
37
38
39#define OPT_DIRECT 1000
40#define OPT_TESTDIR 1001
41
42static struct option options[] =
43{
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 }
50};
51
52/* PID of the test itself. */
53static int pid;
54
55/* Directory to place temporary files in. */
56static const char *test_dir;
57
b9337b6a 58/* List of temporary files. */
51eecc4a 59struct temp_name_list
b9337b6a
UD
60{
61 struct qelem q;
62 const char *name;
51eecc4a 63} *temp_name_list;
b9337b6a
UD
64
65/* Add temporary files in list. */
9c0592ab 66static void
5cd6f8f7 67__attribute__ ((unused))
b9337b6a
UD
68add_temp_file (const char *name)
69{
51eecc4a
AJ
70 struct temp_name_list *newp
71 = (struct temp_name_list *) calloc (sizeof (*newp), 1);
b9337b6a
UD
72 if (newp != NULL)
73 {
74 newp->name = name;
51eecc4a
AJ
75 if (temp_name_list == NULL)
76 temp_name_list = (struct temp_name_list *) &newp->q;
b9337b6a 77 else
51eecc4a 78 insque (newp, temp_name_list);
b9337b6a
UD
79 }
80}
81
82/* Delete all temporary files. */
9c0592ab 83static void
b9337b6a
UD
84delete_temp_files (void)
85{
51eecc4a 86 while (temp_name_list != NULL)
b9337b6a 87 {
51eecc4a
AJ
88 remove (temp_name_list->name);
89 temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw;
b9337b6a
UD
90 }
91}
92
80a18298 93/* Timeout handler. We kill the child and exit with an error. */
9c0592ab 94static void
8c0b7170 95__attribute__ ((noreturn))
80a18298
UD
96timeout_handler (int sig __attribute__ ((unused)))
97{
98 int killed;
99
100 /* Send signal. */
101 kill (pid, SIGKILL);
102
103 /* Wait for it to terminate. */
104 killed = waitpid (pid, NULL, WNOHANG);
105 if (killed != 0 && killed != pid)
106 {
107 perror ("Failed to killed test process");
108 exit (1);
109 }
110
111#ifdef CLEANUP_HANDLER
112 CLEANUP_HANDLER;
113#endif
114
115 fputs ("Timed out: killed the child process\n", stderr);
116
117 /* Exit with an error. */
118 exit (1);
119}
120
121/* We provide the entry point here. */
122int
123main (int argc, char *argv[])
124{
125 int direct = 0; /* Directly call the test function? */
126 int status;
127 int opt;
128
e7c036b3
UD
129#ifdef STDOUT_UNBUFFERED
130 setbuf (stdout, NULL);
131#endif
132
80a18298
UD
133 while ((opt = getopt_long (argc, argv, "", options, NULL)) != -1)
134 switch (opt)
135 {
136 case '?':
137 exit (1);
138 case OPT_DIRECT:
139 direct = 1;
140 break;
141 case OPT_TESTDIR:
142 test_dir = optarg;
143 break;
144#ifdef CMDLINE_PROCESS
145 CMDLINE_PROCESS
146#endif
147 }
148
149 /* Set TMPDIR to specified test directory. */
150 if (test_dir != NULL)
151 {
152 setenv ("TMPDIR", test_dir, 1);
153
154 if (chdir (test_dir) < 0)
155 {
156 perror ("chdir");
157 exit (1);
158 }
159 }
b9337b6a
UD
160 else
161 {
162 test_dir = getenv ("TMPDIR");
163 if (test_dir == NULL || test_dir[0] == '\0')
164 test_dir = "/tmp";
165 }
80a18298 166
6b9c2e67
UD
167 /* Make sure we see all message, even those on stdout. */
168 setvbuf (stdout, NULL, _IONBF, 0);
169
310b3460
UD
170 /* make sure temporary files are deleted. */
171 atexit (delete_temp_files);
172
726b7b0f
UD
173 /* Correct for the possible parameters. */
174 argv += optind - 1;
175 argc -= optind - 1;
176
310b3460
UD
177 /* Call the initializing function, if one is available. */
178#ifdef PREPARE
179 PREPARE (argc, argv);
180#endif
181
80a18298
UD
182 /* If we are not expected to fork run the function immediately. */
183 if (direct)
184 return TEST_FUNCTION;
185
186 /* Set up the test environment:
187 - prevent core dumps
188 - set up the timer
189 - fork and execute the function. */
190
191 pid = fork ();
192 if (pid == 0)
193 {
194 /* This is the child. */
195#ifdef RLIMIT_CORE
196 /* Try to avoid dumping core. */
197 struct rlimit core_limit;
198 core_limit.rlim_cur = 0;
199 core_limit.rlim_max = 0;
200 setrlimit (RLIMIT_CORE, &core_limit);
201#endif
202
203 /* Execute the test function and exit with the return value. */
204 exit (TEST_FUNCTION);
205 }
206 else if (pid < 0)
207 {
208 perror ("Cannot fork test program");
209 exit (1);
210 }
211
212 /* Set timeout. */
213#ifndef TIMEOUT
214 /* Default timeout is two seconds. */
215# define TIMEOUT 2
216#endif
217 alarm (TIMEOUT);
218 signal (SIGALRM, timeout_handler);
219
220 /* Wait for the regular termination. */
221 if (waitpid (pid, &status, 0) != pid)
222 {
223 perror ("Oops, wrong test program terminated");
224 exit (1);
225 }
226
227#ifndef EXPECTED_SIGNAL
228 /* We don't expect any signal. */
229# define EXPECTED_SIGNAL 0
230#endif
789b13c4 231 if (WTERMSIG (status) != EXPECTED_SIGNAL)
80a18298 232 {
b9337b6a
UD
233 if (EXPECTED_SIGNAL != 0)
234 fprintf (stderr, "Incorrect signal from child: got `%s', need `%s'\n",
235 strsignal (WTERMSIG (status)), strsignal (EXPECTED_SIGNAL));
236 else
6b9c2e67 237 fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
b9337b6a 238 strsignal (WTERMSIG (status)));
80a18298
UD
239 exit (1);
240 }
241
242 /* Simply exit with the return value of the test. */
243 return WEXITSTATUS (status);
244}