Update.
[glibc.git] / test-skeleton.c
blobbb02925dc96789abaebf8326adde685ef2e838d4
1 /* Skeleton for test programs.
2 Copyright (C) 1998, 2000 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 add_temp_file (const char *name)
69 struct temp_name_list *newp
70 = (struct temp_name_list *) calloc (sizeof (*newp), 1);
71 if (newp != NULL)
73 newp->name = name;
74 if (temp_name_list == NULL)
75 temp_name_list = (struct temp_name_list *) &newp->q;
76 else
77 insque (newp, temp_name_list);
81 /* Delete all temporary files. */
82 static void
83 delete_temp_files (void)
85 while (temp_name_list != NULL)
87 remove (temp_name_list->name);
88 temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw;
92 /* Timeout handler. We kill the child and exit with an error. */
93 static void
94 __attribute__ ((noreturn))
95 timeout_handler (int sig __attribute__ ((unused)))
97 int killed;
99 /* Send signal. */
100 kill (pid, SIGKILL);
102 /* Wait for it to terminate. */
103 killed = waitpid (pid, NULL, WNOHANG);
104 if (killed != 0 && killed != pid)
106 perror ("Failed to killed test process");
107 exit (1);
110 #ifdef CLEANUP_HANDLER
111 CLEANUP_HANDLER;
112 #endif
114 fputs ("Timed out: killed the child process\n", stderr);
116 /* Exit with an error. */
117 exit (1);
120 /* We provide the entry point here. */
122 main (int argc, char *argv[])
124 int direct = 0; /* Directly call the test function? */
125 int status;
126 int opt;
128 #ifdef STDOUT_UNBUFFERED
129 setbuf (stdout, NULL);
130 #endif
132 while ((opt = getopt_long (argc, argv, "", options, NULL)) != -1)
133 switch (opt)
135 case '?':
136 exit (1);
137 case OPT_DIRECT:
138 direct = 1;
139 break;
140 case OPT_TESTDIR:
141 test_dir = optarg;
142 break;
143 #ifdef CMDLINE_PROCESS
144 CMDLINE_PROCESS
145 #endif
148 /* Set TMPDIR to specified test directory. */
149 if (test_dir != NULL)
151 setenv ("TMPDIR", test_dir, 1);
153 if (chdir (test_dir) < 0)
155 perror ("chdir");
156 exit (1);
159 else
161 test_dir = getenv ("TMPDIR");
162 if (test_dir == NULL || test_dir[0] == '\0')
163 test_dir = "/tmp";
166 /* Make sure we see all message, even those on stdout. */
167 setvbuf (stdout, NULL, _IONBF, 0);
169 /* make sure temporary files are deleted. */
170 atexit (delete_temp_files);
172 /* Correct for the possible parameters. */
173 argv += optind - 1;
174 argc -= optind - 1;
176 /* Call the initializing function, if one is available. */
177 #ifdef PREPARE
178 PREPARE (argc, argv);
179 #endif
181 /* If we are not expected to fork run the function immediately. */
182 if (direct)
183 return TEST_FUNCTION;
185 /* Set up the test environment:
186 - prevent core dumps
187 - set up the timer
188 - fork and execute the function. */
190 pid = fork ();
191 if (pid == 0)
193 /* This is the child. */
194 #ifdef RLIMIT_CORE
195 /* Try to avoid dumping core. */
196 struct rlimit core_limit;
197 core_limit.rlim_cur = 0;
198 core_limit.rlim_max = 0;
199 setrlimit (RLIMIT_CORE, &core_limit);
200 #endif
202 /* Execute the test function and exit with the return value. */
203 exit (TEST_FUNCTION);
205 else if (pid < 0)
207 perror ("Cannot fork test program");
208 exit (1);
211 /* Set timeout. */
212 #ifndef TIMEOUT
213 /* Default timeout is two seconds. */
214 # define TIMEOUT 2
215 #endif
216 alarm (TIMEOUT);
217 signal (SIGALRM, timeout_handler);
219 /* Wait for the regular termination. */
220 if (waitpid (pid, &status, 0) != pid)
222 perror ("Oops, wrong test program terminated");
223 exit (1);
226 #ifndef EXPECTED_SIGNAL
227 /* We don't expect any signal. */
228 # define EXPECTED_SIGNAL 0
229 #endif
230 if (WTERMSIG (status) != EXPECTED_SIGNAL)
232 if (EXPECTED_SIGNAL != 0)
233 fprintf (stderr, "Incorrect signal from child: got `%s', need `%s'\n",
234 strsignal (WTERMSIG (status)), strsignal (EXPECTED_SIGNAL));
235 else
236 fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
237 strsignal (WTERMSIG (status)));
238 exit (1);
241 /* Simply exit with the return value of the test. */
242 return WEXITSTATUS (status);