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