(_EXTERN_INLINE): Remove. Use static __inline instead of _EXTERN_INLINE.
[glibc.git] / test-skeleton.c
blobdb04b11e63c4e734e6a4c85503489b28dbd0ef14
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 name_list
60 struct qelem q;
61 const char *name;
62 } *name_list;
64 /* Add temporary files in list. */
65 void
66 add_temp_file (const char *name)
68 struct name_list *newp = (struct name_list *) calloc (sizeof (*newp), 1);
69 if (newp != NULL)
71 newp->name = name;
72 if (name_list == NULL)
73 name_list = (struct name_list *) &newp->q;
74 else
75 insque (newp, name_list);
79 /* Delete all temporary files. */
80 void
81 delete_temp_files (void)
83 while (name_list != NULL)
85 remove (name_list->name);
86 name_list = (struct name_list *) name_list->q.q_forw;
90 /* Timeout handler. We kill the child and exit with an error. */
91 void
92 timeout_handler (int sig __attribute__ ((unused)))
94 int killed;
96 /* Send signal. */
97 kill (pid, SIGKILL);
99 /* Wait for it to terminate. */
100 killed = waitpid (pid, NULL, WNOHANG);
101 if (killed != 0 && killed != pid)
103 perror ("Failed to killed test process");
104 exit (1);
107 #ifdef CLEANUP_HANDLER
108 CLEANUP_HANDLER;
109 #endif
111 fputs ("Timed out: killed the child process\n", stderr);
113 /* Exit with an error. */
114 exit (1);
117 /* We provide the entry point here. */
119 main (int argc, char *argv[])
121 int direct = 0; /* Directly call the test function? */
122 int status;
123 int opt;
125 #ifdef STDOUT_UNBUFFERED
126 setbuf (stdout, NULL);
127 #endif
129 while ((opt = getopt_long (argc, argv, "", options, NULL)) != -1)
130 switch (opt)
132 case '?':
133 exit (1);
134 case OPT_DIRECT:
135 direct = 1;
136 break;
137 case OPT_TESTDIR:
138 test_dir = optarg;
139 break;
140 #ifdef CMDLINE_PROCESS
141 CMDLINE_PROCESS
142 #endif
145 /* Set TMPDIR to specified test directory. */
146 if (test_dir != NULL)
148 setenv ("TMPDIR", test_dir, 1);
150 if (chdir (test_dir) < 0)
152 perror ("chdir");
153 exit (1);
156 else
158 test_dir = getenv ("TMPDIR");
159 if (test_dir == NULL || test_dir[0] == '\0')
160 test_dir = "/tmp";
163 /* Make sure we see all message, even those on stdout. */
164 setvbuf (stdout, NULL, _IONBF, 0);
166 /* make sure temporary files are deleted. */
167 atexit (delete_temp_files);
169 /* Call the initializing function, if one is available. */
170 #ifdef PREPARE
171 PREPARE (argc, argv);
172 #endif
174 /* If we are not expected to fork run the function immediately. */
175 if (direct)
176 return TEST_FUNCTION;
178 /* Set up the test environment:
179 - prevent core dumps
180 - set up the timer
181 - fork and execute the function. */
183 pid = fork ();
184 if (pid == 0)
186 /* This is the child. */
187 #ifdef RLIMIT_CORE
188 /* Try to avoid dumping core. */
189 struct rlimit core_limit;
190 core_limit.rlim_cur = 0;
191 core_limit.rlim_max = 0;
192 setrlimit (RLIMIT_CORE, &core_limit);
193 #endif
195 /* Execute the test function and exit with the return value. */
196 exit (TEST_FUNCTION);
198 else if (pid < 0)
200 perror ("Cannot fork test program");
201 exit (1);
204 /* Set timeout. */
205 #ifndef TIMEOUT
206 /* Default timeout is two seconds. */
207 # define TIMEOUT 2
208 #endif
209 alarm (TIMEOUT);
210 signal (SIGALRM, timeout_handler);
212 /* Wait for the regular termination. */
213 if (waitpid (pid, &status, 0) != pid)
215 perror ("Oops, wrong test program terminated");
216 exit (1);
219 #ifndef EXPECTED_SIGNAL
220 /* We don't expect any signal. */
221 # define EXPECTED_SIGNAL 0
222 #endif
223 if (WTERMSIG (status) != EXPECTED_SIGNAL)
225 if (EXPECTED_SIGNAL != 0)
226 fprintf (stderr, "Incorrect signal from child: got `%s', need `%s'\n",
227 strsignal (WTERMSIG (status)), strsignal (EXPECTED_SIGNAL));
228 else
229 fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
230 strsignal (WTERMSIG (status)));
231 exit (1);
234 /* Simply exit with the return value of the test. */
235 return WEXITSTATUS (status);