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