Update.
[platform/upstream/glibc.git] / test-skeleton.c
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.
5
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.
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
14    Lesser General Public License for more details.
15
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.  */
20
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>
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
42 static 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.  */
53 static pid_t pid;
54
55 /* Directory to place temporary files in.  */
56 static const char *test_dir;
57
58 /* List of temporary files.  */
59 struct temp_name_list
60 {
61   struct qelem q;
62   const char *name;
63 } *temp_name_list;
64
65 /* Add temporary files in list.  */
66 static void
67 __attribute__ ((unused))
68 add_temp_file (const char *name)
69 {
70   struct temp_name_list *newp
71     = (struct temp_name_list *) calloc (sizeof (*newp), 1);
72   if (newp != NULL)
73     {
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);
79     }
80 }
81
82 /* Delete all temporary files.  */
83 static void
84 delete_temp_files (void)
85 {
86   while (temp_name_list != NULL)
87     {
88       remove (temp_name_list->name);
89       temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw;
90     }
91 }
92
93 /* Create a temporary file.  */
94 static int
95 __attribute__ ((unused))
96 create_temp_file (const char *base, char **filename)
97 {
98   char *fname;
99   int fd;
100
101   fname = (char *) malloc (strlen (test_dir) + 1 + strlen (base)
102                            + sizeof ("XXXXXX"));
103   if (fname == NULL)
104     {
105       puts ("out of memory");
106       return -1;
107     }
108   strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX");
109
110   fd = mkstemp (fname);
111   if (fd == -1)
112     {
113       printf ("cannot open temporary file '%s': %m\n", fname);
114       free (fname);
115       return -1;
116     }
117
118   add_temp_file (fname);
119   if (filename != NULL)
120     *filename = fname;
121
122   return fd;
123 }
124
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)))
129 {
130   int killed;
131   int status;
132
133   /* Send signal.  */
134   kill (pid, SIGKILL);
135
136   /* Wait for it to terminate.  */
137   killed = waitpid (pid, &status, WNOHANG|WUNTRACED);
138   if (killed != 0 && killed != pid)
139     {
140       perror ("Failed to killed test process");
141       exit (1);
142     }
143
144 #ifdef CLEANUP_HANDLER
145   CLEANUP_HANDLER;
146 #endif
147
148   /* If we expected this signal: good!  */
149 #ifdef EXPECTED_SIGNAL
150   if (EXPECTED_SIGNAL == SIGALRM)
151     exit (0);
152 #endif
153
154   if (WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL)
155     fputs ("Timed out: killed the child process\n", stderr);
156   else if (WIFSTOPPED (status))
157     fprintf (stderr, "Timed out: the child process was %s\n",
158              strsignal (WSTOPSIG (status)));
159   else if (WIFSIGNALED (status))
160     fprintf (stderr, "Timed out: the child process got signal %s\n",
161              strsignal (WTERMSIG (status)));
162   else
163     fprintf (stderr, "Timed out: killed the child process but it exited %d\n",
164              WEXITSTATUS (status));
165
166   /* Exit with an error.  */
167   exit (1);
168 }
169
170 /* We provide the entry point here.  */
171 int
172 main (int argc, char *argv[])
173 {
174   int direct = 0;       /* Directly call the test function?  */
175   int status;
176   int opt;
177   pid_t termpid;
178
179 #ifdef STDOUT_UNBUFFERED
180   setbuf (stdout, NULL);
181 #endif
182
183   while ((opt = getopt_long (argc, argv, "", options, NULL)) != -1)
184     switch (opt)
185       {
186       case '?':
187         exit (1);
188       case OPT_DIRECT:
189         direct = 1;
190         break;
191       case OPT_TESTDIR:
192         test_dir = optarg;
193         break;
194 #ifdef CMDLINE_PROCESS
195         CMDLINE_PROCESS
196 #endif
197       }
198
199   /* Set TMPDIR to specified test directory.  */
200   if (test_dir != NULL)
201     {
202       setenv ("TMPDIR", test_dir, 1);
203
204       if (chdir (test_dir) < 0)
205         {
206           perror ("chdir");
207           exit (1);
208         }
209     }
210   else
211     {
212       test_dir = getenv ("TMPDIR");
213       if (test_dir == NULL || test_dir[0] == '\0')
214         test_dir = "/tmp";
215     }
216
217   /* Make sure we see all message, even those on stdout.  */
218   setvbuf (stdout, NULL, _IONBF, 0);
219
220   /* make sure temporary files are deleted.  */
221   atexit (delete_temp_files);
222
223   /* Correct for the possible parameters.  */
224   argv += optind - 1;
225   argc -= optind - 1;
226
227   /* Call the initializing function, if one is available.  */
228 #ifdef PREPARE
229   PREPARE (argc, argv);
230 #endif
231
232   /* If we are not expected to fork run the function immediately.  */
233   if (direct)
234     return TEST_FUNCTION;
235
236   /* Set up the test environment:
237      - prevent core dumps
238      - set up the timer
239      - fork and execute the function.  */
240
241   pid = fork ();
242   if (pid == 0)
243     {
244       /* This is the child.  */
245 #ifdef RLIMIT_CORE
246       /* Try to avoid dumping core.  */
247       struct rlimit core_limit;
248       core_limit.rlim_cur = 0;
249       core_limit.rlim_max = 0;
250       setrlimit (RLIMIT_CORE, &core_limit);
251 #endif
252
253       /* We put the test process in its own pgrp so that if it bogusly
254          generates any job control signals, they won't hit the whole build.  */
255       setpgid (0, 0);
256
257       /* Execute the test function and exit with the return value.   */
258       exit (TEST_FUNCTION);
259     }
260   else if (pid < 0)
261     {
262       perror ("Cannot fork test program");
263       exit (1);
264     }
265
266   /* Set timeout.  */
267 #ifndef TIMEOUT
268   /* Default timeout is two seconds.  */
269 # define TIMEOUT 2
270 #endif
271   alarm (TIMEOUT);
272   signal (SIGALRM, timeout_handler);
273
274   /* Wait for the regular termination.  */
275   termpid = waitpid (pid, &status, 0);
276   if (termpid == -1)
277     {
278       printf ("Waiting for test program failed: %m\n");
279       exit (1);
280     }
281   if (termpid != pid)
282     {
283       printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
284               (long int) pid, (long int) termpid);
285       exit (1);
286     }
287
288 #ifndef EXPECTED_SIGNAL
289   /* We don't expect any signal.  */
290 # define EXPECTED_SIGNAL 0
291 #endif
292   if (WTERMSIG (status) != EXPECTED_SIGNAL)
293     {
294       if (EXPECTED_SIGNAL != 0)
295         {
296           if (WTERMSIG (status) == 0)
297             fprintf (stderr,
298                      "Expected signal '%s' from child, got none\n",
299                      strsignal (EXPECTED_SIGNAL));
300           else
301             fprintf (stderr,
302                      "Incorrect signal from child: got `%s', need `%s'\n",
303                      strsignal (WTERMSIG (status)),
304                      strsignal (EXPECTED_SIGNAL));
305         }
306       else
307         fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
308                  strsignal (WTERMSIG (status)));
309       exit (1);
310     }
311
312   /* Simply exit with the return value of the test.  */
313   return WEXITSTATUS (status);
314 }