1 | # source this file; set up for tests
|
---|
2 |
|
---|
3 | # Copyright (C) 2009-2012 Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | # This program is free software: you can redistribute it and/or modify
|
---|
6 | # it under the terms of the GNU General Public License as published by
|
---|
7 | # the Free Software Foundation, either version 3 of the License, or
|
---|
8 | # (at your option) any later version.
|
---|
9 |
|
---|
10 | # This program 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
|
---|
13 | # GNU General Public License for more details.
|
---|
14 |
|
---|
15 | # You should have received a copy of the GNU General Public License
|
---|
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 |
|
---|
18 | # Using this file in a test
|
---|
19 | # =========================
|
---|
20 | #
|
---|
21 | # The typical skeleton of a test looks like this:
|
---|
22 | #
|
---|
23 | # #!/bin/sh
|
---|
24 | # . "${srcdir=.}/init.sh"; path_prepend_ .
|
---|
25 | # Execute some commands.
|
---|
26 | # Note that these commands are executed in a subdirectory, therefore you
|
---|
27 | # need to prepend "../" to relative filenames in the build directory.
|
---|
28 | # Note that the "path_prepend_ ." is useful only if the body of your
|
---|
29 | # test invokes programs residing in the initial directory.
|
---|
30 | # For example, if the programs you want to test are in src/, and this test
|
---|
31 | # script is named tests/test-1, then you would use "path_prepend_ ../src",
|
---|
32 | # or perhaps export PATH='$(abs_top_builddir)/src$(PATH_SEPARATOR)'"$$PATH"
|
---|
33 | # to all tests via automake's TESTS_ENVIRONMENT.
|
---|
34 | # Set the exit code 0 for success, 77 for skipped, or 1 or other for failure.
|
---|
35 | # Use the skip_ and fail_ functions to print a diagnostic and then exit
|
---|
36 | # with the corresponding exit code.
|
---|
37 | # Exit $?
|
---|
38 |
|
---|
39 | # Executing a test that uses this file
|
---|
40 | # ====================================
|
---|
41 | #
|
---|
42 | # Running a single test:
|
---|
43 | # $ make check TESTS=test-foo.sh
|
---|
44 | #
|
---|
45 | # Running a single test, with verbose output:
|
---|
46 | # $ make check TESTS=test-foo.sh VERBOSE=yes
|
---|
47 | #
|
---|
48 | # Running a single test, with single-stepping:
|
---|
49 | # 1. Go into a sub-shell:
|
---|
50 | # $ bash
|
---|
51 | # 2. Set relevant environment variables from TESTS_ENVIRONMENT in the
|
---|
52 | # Makefile:
|
---|
53 | # $ export srcdir=../../tests # this is an example
|
---|
54 | # 3. Execute the commands from the test, copy&pasting them one by one:
|
---|
55 | # $ . "$srcdir/init.sh"; path_prepend_ .
|
---|
56 | # ...
|
---|
57 | # 4. Finally
|
---|
58 | # $ exit
|
---|
59 |
|
---|
60 | ME_=`expr "./$0" : '.*/\(.*\)$'`
|
---|
61 |
|
---|
62 | # We use a trap below for cleanup. This requires us to go through
|
---|
63 | # hoops to get the right exit status transported through the handler.
|
---|
64 | # So use 'Exit STATUS' instead of 'exit STATUS' inside of the tests.
|
---|
65 | # Turn off errexit here so that we don't trip the bug with OSF1/Tru64
|
---|
66 | # sh inside this function.
|
---|
67 | Exit () { set +e; (exit $1); exit $1; }
|
---|
68 |
|
---|
69 | # Print warnings (e.g., about skipped and failed tests) to this file number.
|
---|
70 | # Override by defining to say, 9, in init.cfg, and putting say,
|
---|
71 | # export ...ENVVAR_SETTINGS...; $(SHELL) 9>&2
|
---|
72 | # in the definition of TESTS_ENVIRONMENT in your tests/Makefile.am file.
|
---|
73 | # This is useful when using automake's parallel tests mode, to print
|
---|
74 | # the reason for skip/failure to console, rather than to the .log files.
|
---|
75 | : ${stderr_fileno_=2}
|
---|
76 |
|
---|
77 | # Note that correct expansion of "$*" depends on IFS starting with ' '.
|
---|
78 | # Always write the full diagnostic to stderr.
|
---|
79 | # When stderr_fileno_ is not 2, also emit the first line of the
|
---|
80 | # diagnostic to that file descriptor.
|
---|
81 | warn_ ()
|
---|
82 | {
|
---|
83 | # If IFS does not start with ' ', set it and emit the warning in a subshell.
|
---|
84 | case $IFS in
|
---|
85 | ' '*) printf '%s\n' "$*" >&2
|
---|
86 | test $stderr_fileno_ = 2 \
|
---|
87 | || { printf '%s\n' "$*" | sed 1q >&$stderr_fileno_ ; } ;;
|
---|
88 | *) (IFS=' '; warn_ "$@");;
|
---|
89 | esac
|
---|
90 | }
|
---|
91 | fail_ () { warn_ "$ME_: failed test: $@"; Exit 1; }
|
---|
92 | skip_ () { warn_ "$ME_: skipped test: $@"; Exit 77; }
|
---|
93 | fatal_ () { warn_ "$ME_: hard error: $@"; Exit 99; }
|
---|
94 | framework_failure_ () { warn_ "$ME_: set-up failure: $@"; Exit 99; }
|
---|
95 |
|
---|
96 | # Sanitize this shell to POSIX mode, if possible.
|
---|
97 | DUALCASE=1; export DUALCASE
|
---|
98 | if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
|
---|
99 | emulate sh
|
---|
100 | NULLCMD=:
|
---|
101 | alias -g '${1+"$@"}'='"$@"'
|
---|
102 | setopt NO_GLOB_SUBST
|
---|
103 | else
|
---|
104 | case `(set -o) 2>/dev/null` in
|
---|
105 | *posix*) set -o posix ;;
|
---|
106 | esac
|
---|
107 | fi
|
---|
108 |
|
---|
109 | # We require $(...) support unconditionally.
|
---|
110 | # We require a few additional shell features only when $EXEEXT is nonempty,
|
---|
111 | # in order to support automatic $EXEEXT emulation:
|
---|
112 | # - hyphen-containing alias names
|
---|
113 | # - we prefer to use ${var#...} substitution, rather than having
|
---|
114 | # to work around lack of support for that feature.
|
---|
115 | # The following code attempts to find a shell with support for these features.
|
---|
116 | # If the current shell passes the test, we're done. Otherwise, test other
|
---|
117 | # shells until we find one that passes. If one is found, re-exec it.
|
---|
118 | # If no acceptable shell is found, skip the current test.
|
---|
119 | #
|
---|
120 | # The "...set -x; P=1 true 2>err..." test is to disqualify any shell that
|
---|
121 | # emits "P=1" into err, as /bin/sh from SunOS 5.11 and OpenBSD 4.7 do.
|
---|
122 | #
|
---|
123 | # Use "9" to indicate success (rather than 0), in case some shell acts
|
---|
124 | # like Solaris 10's /bin/sh but exits successfully instead of with status 2.
|
---|
125 |
|
---|
126 | # Eval this code in a subshell to determine a shell's suitability.
|
---|
127 | # 10 - passes all tests; ok to use
|
---|
128 | # 9 - ok, but enabling "set -x" corrupts app stderr; prefer higher score
|
---|
129 | # ? - not ok
|
---|
130 | gl_shell_test_script_='
|
---|
131 | test $(echo y) = y || exit 1
|
---|
132 | score_=10
|
---|
133 | if test "$VERBOSE" = yes; then
|
---|
134 | test -n "$( (exec 3>&1; set -x; P=1 true 2>&3) 2> /dev/null)" && score_=9
|
---|
135 | fi
|
---|
136 | test -z "$EXEEXT" && exit $score_
|
---|
137 | shopt -s expand_aliases
|
---|
138 | alias a-b="echo zoo"
|
---|
139 | v=abx
|
---|
140 | test ${v%x} = ab \
|
---|
141 | && test ${v#a} = bx \
|
---|
142 | && test $(a-b) = zoo \
|
---|
143 | && exit $score_
|
---|
144 | '
|
---|
145 |
|
---|
146 | if test "x$1" = "x--no-reexec"; then
|
---|
147 | shift
|
---|
148 | else
|
---|
149 | # Assume a working shell. Export to subshells (setup_ needs this).
|
---|
150 | gl_set_x_corrupts_stderr_=false
|
---|
151 | export gl_set_x_corrupts_stderr_
|
---|
152 |
|
---|
153 | # Record the first marginally acceptable shell.
|
---|
154 | marginal_=
|
---|
155 |
|
---|
156 | # Search for a shell that meets our requirements.
|
---|
157 | for re_shell_ in __current__ "${CONFIG_SHELL:-no_shell}" \
|
---|
158 | /bin/sh bash dash zsh pdksh fail
|
---|
159 | do
|
---|
160 | test "$re_shell_" = no_shell && continue
|
---|
161 |
|
---|
162 | # If we've made it all the way to the sentinel, "fail" without
|
---|
163 | # finding even a marginal shell, skip this test.
|
---|
164 | if test "$re_shell_" = fail; then
|
---|
165 | test -z "$marginal_" && skip_ failed to find an adequate shell
|
---|
166 | re_shell_=$marginal_
|
---|
167 | break
|
---|
168 | fi
|
---|
169 |
|
---|
170 | # When testing the current shell, simply "eval" the test code.
|
---|
171 | # Otherwise, run it via $re_shell_ -c ...
|
---|
172 | if test "$re_shell_" = __current__; then
|
---|
173 | # 'eval'ing this code makes Solaris 10's /bin/sh exit with
|
---|
174 | # $? set to 2. It does not evaluate any of the code after the
|
---|
175 | # "unexpected" first '('. Thus, we must run it in a subshell.
|
---|
176 | ( eval "$gl_shell_test_script_" ) > /dev/null 2>&1
|
---|
177 | else
|
---|
178 | "$re_shell_" -c "$gl_shell_test_script_" 2>/dev/null
|
---|
179 | fi
|
---|
180 |
|
---|
181 | st_=$?
|
---|
182 |
|
---|
183 | # $re_shell_ works just fine. Use it.
|
---|
184 | if test $st_ = 10; then
|
---|
185 | gl_set_x_corrupts_stderr_=false
|
---|
186 | break
|
---|
187 | fi
|
---|
188 |
|
---|
189 | # If this is our first marginally acceptable shell, remember it.
|
---|
190 | if test "$st_:$marginal_" = 9: ; then
|
---|
191 | marginal_="$re_shell_"
|
---|
192 | gl_set_x_corrupts_stderr_=true
|
---|
193 | fi
|
---|
194 | done
|
---|
195 |
|
---|
196 | if test "$re_shell_" != __current__; then
|
---|
197 | # Found a usable shell. Preserve -v and -x.
|
---|
198 | case $- in
|
---|
199 | *v*x* | *x*v*) opts_=-vx ;;
|
---|
200 | *v*) opts_=-v ;;
|
---|
201 | *x*) opts_=-x ;;
|
---|
202 | *) opts_= ;;
|
---|
203 | esac
|
---|
204 | exec "$re_shell_" $opts_ "$0" --no-reexec "$@"
|
---|
205 | echo "$ME_: exec failed" 1>&2
|
---|
206 | exit 127
|
---|
207 | fi
|
---|
208 | fi
|
---|
209 |
|
---|
210 | test -n "$EXEEXT" && shopt -s expand_aliases
|
---|
211 |
|
---|
212 | # Enable glibc's malloc-perturbing option.
|
---|
213 | # This is useful for exposing code that depends on the fact that
|
---|
214 | # malloc-related functions often return memory that is mostly zeroed.
|
---|
215 | # If you have the time and cycles, use valgrind to do an even better job.
|
---|
216 | : ${MALLOC_PERTURB_=87}
|
---|
217 | export MALLOC_PERTURB_
|
---|
218 |
|
---|
219 | # This is a stub function that is run upon trap (upon regular exit and
|
---|
220 | # interrupt). Override it with a per-test function, e.g., to unmount
|
---|
221 | # a partition, or to undo any other global state changes.
|
---|
222 | cleanup_ () { :; }
|
---|
223 |
|
---|
224 | # Emit a header similar to that from diff -u; Print the simulated "diff"
|
---|
225 | # command so that the order of arguments is clear. Don't bother with @@ lines.
|
---|
226 | emit_diff_u_header_ ()
|
---|
227 | {
|
---|
228 | printf '%s\n' "diff -u $*" \
|
---|
229 | "--- $1 1970-01-01" \
|
---|
230 | "+++ $2 1970-01-01"
|
---|
231 | }
|
---|
232 |
|
---|
233 | # Arrange not to let diff or cmp operate on /dev/null,
|
---|
234 | # since on some systems (at least OSF/1 5.1), that doesn't work.
|
---|
235 | # When there are not two arguments, or no argument is /dev/null, return 2.
|
---|
236 | # When one argument is /dev/null and the other is not empty,
|
---|
237 | # cat the nonempty file to stderr and return 1.
|
---|
238 | # Otherwise, return 0.
|
---|
239 | compare_dev_null_ ()
|
---|
240 | {
|
---|
241 | test $# = 2 || return 2
|
---|
242 |
|
---|
243 | if test "x$1" = x/dev/null; then
|
---|
244 | test -s "$2" || return 0
|
---|
245 | emit_diff_u_header_ "$@"; sed 's/^/+/' "$2"
|
---|
246 | return 1
|
---|
247 | fi
|
---|
248 |
|
---|
249 | if test "x$2" = x/dev/null; then
|
---|
250 | test -s "$1" || return 0
|
---|
251 | emit_diff_u_header_ "$@"; sed 's/^/-/' "$1"
|
---|
252 | return 1
|
---|
253 | fi
|
---|
254 |
|
---|
255 | return 2
|
---|
256 | }
|
---|
257 |
|
---|
258 | if diff_out_=`exec 2>/dev/null; diff -u "$0" "$0" < /dev/null` \
|
---|
259 | && diff -u Makefile "$0" 2>/dev/null | grep '^[+]#!' >/dev/null; then
|
---|
260 | # diff accepts the -u option and does not (like AIX 7 'diff') produce an
|
---|
261 | # extra space on column 1 of every content line.
|
---|
262 | if test -z "$diff_out_"; then
|
---|
263 | compare_ () { diff -u "$@"; }
|
---|
264 | else
|
---|
265 | compare_ ()
|
---|
266 | {
|
---|
267 | if diff -u "$@" > diff.out; then
|
---|
268 | # No differences were found, but Solaris 'diff' produces output
|
---|
269 | # "No differences encountered". Hide this output.
|
---|
270 | rm -f diff.out
|
---|
271 | true
|
---|
272 | else
|
---|
273 | cat diff.out
|
---|
274 | rm -f diff.out
|
---|
275 | false
|
---|
276 | fi
|
---|
277 | }
|
---|
278 | fi
|
---|
279 | elif diff_out_=`exec 2>/dev/null; diff -c "$0" "$0" < /dev/null`; then
|
---|
280 | if test -z "$diff_out_"; then
|
---|
281 | compare_ () { diff -c "$@"; }
|
---|
282 | else
|
---|
283 | compare_ ()
|
---|
284 | {
|
---|
285 | if diff -c "$@" > diff.out; then
|
---|
286 | # No differences were found, but AIX and HP-UX 'diff' produce output
|
---|
287 | # "No differences encountered" or "There are no differences between the
|
---|
288 | # files.". Hide this output.
|
---|
289 | rm -f diff.out
|
---|
290 | true
|
---|
291 | else
|
---|
292 | cat diff.out
|
---|
293 | rm -f diff.out
|
---|
294 | false
|
---|
295 | fi
|
---|
296 | }
|
---|
297 | fi
|
---|
298 | elif ( cmp --version < /dev/null 2>&1 | grep GNU ) > /dev/null 2>&1; then
|
---|
299 | compare_ () { cmp -s "$@"; }
|
---|
300 | else
|
---|
301 | compare_ () { cmp "$@"; }
|
---|
302 | fi
|
---|
303 |
|
---|
304 | # Usage: compare EXPECTED ACTUAL
|
---|
305 | #
|
---|
306 | # Given compare_dev_null_'s preprocessing, defer to compare_ if 2 or more.
|
---|
307 | # Otherwise, propagate $? to caller: any diffs have already been printed.
|
---|
308 | compare ()
|
---|
309 | {
|
---|
310 | # This looks like it can be factored to use a simple "case $?"
|
---|
311 | # after unchecked compare_dev_null_ invocation, but that would
|
---|
312 | # fail in a "set -e" environment.
|
---|
313 | if compare_dev_null_ "$@"; then
|
---|
314 | return 0
|
---|
315 | else
|
---|
316 | case $? in
|
---|
317 | 1) return 1;;
|
---|
318 | *) compare_ "$@";;
|
---|
319 | esac
|
---|
320 | fi
|
---|
321 | }
|
---|
322 |
|
---|
323 | # An arbitrary prefix to help distinguish test directories.
|
---|
324 | testdir_prefix_ () { printf gt; }
|
---|
325 |
|
---|
326 | # Run the user-overridable cleanup_ function, remove the temporary
|
---|
327 | # directory and exit with the incoming value of $?.
|
---|
328 | remove_tmp_ ()
|
---|
329 | {
|
---|
330 | __st=$?
|
---|
331 | cleanup_
|
---|
332 | # cd out of the directory we're about to remove
|
---|
333 | cd "$initial_cwd_" || cd / || cd /tmp
|
---|
334 | chmod -R u+rwx "$test_dir_"
|
---|
335 | # If removal fails and exit status was to be 0, then change it to 1.
|
---|
336 | rm -rf "$test_dir_" || { test $__st = 0 && __st=1; }
|
---|
337 | exit $__st
|
---|
338 | }
|
---|
339 |
|
---|
340 | # Given a directory name, DIR, if every entry in it that matches *.exe
|
---|
341 | # contains only the specified bytes (see the case stmt below), then print
|
---|
342 | # a space-separated list of those names and return 0. Otherwise, don't
|
---|
343 | # print anything and return 1. Naming constraints apply also to DIR.
|
---|
344 | find_exe_basenames_ ()
|
---|
345 | {
|
---|
346 | feb_dir_=$1
|
---|
347 | feb_fail_=0
|
---|
348 | feb_result_=
|
---|
349 | feb_sp_=
|
---|
350 | for feb_file_ in $feb_dir_/*.exe; do
|
---|
351 | # If there was no *.exe file, or there existed a file named "*.exe" that
|
---|
352 | # was deleted between the above glob expansion and the existence test
|
---|
353 | # below, just skip it.
|
---|
354 | test "x$feb_file_" = "x$feb_dir_/*.exe" && test ! -f "$feb_file_" \
|
---|
355 | && continue
|
---|
356 | # Exempt [.exe, since we can't create a function by that name, yet
|
---|
357 | # we can't invoke [ by PATH search anyways due to shell builtins.
|
---|
358 | test "x$feb_file_" = "x$feb_dir_/[.exe" && continue
|
---|
359 | case $feb_file_ in
|
---|
360 | *[!-a-zA-Z/0-9_.+]*) feb_fail_=1; break;;
|
---|
361 | *) # Remove leading file name components as well as the .exe suffix.
|
---|
362 | feb_file_=${feb_file_##*/}
|
---|
363 | feb_file_=${feb_file_%.exe}
|
---|
364 | feb_result_="$feb_result_$feb_sp_$feb_file_";;
|
---|
365 | esac
|
---|
366 | feb_sp_=' '
|
---|
367 | done
|
---|
368 | test $feb_fail_ = 0 && printf %s "$feb_result_"
|
---|
369 | return $feb_fail_
|
---|
370 | }
|
---|
371 |
|
---|
372 | # Consider the files in directory, $1.
|
---|
373 | # For each file name of the form PROG.exe, create an alias named
|
---|
374 | # PROG that simply invokes PROG.exe, then return 0. If any selected
|
---|
375 | # file name or the directory name, $1, contains an unexpected character,
|
---|
376 | # define no alias and return 1.
|
---|
377 | create_exe_shims_ ()
|
---|
378 | {
|
---|
379 | case $EXEEXT in
|
---|
380 | '') return 0 ;;
|
---|
381 | .exe) ;;
|
---|
382 | *) echo "$0: unexpected \$EXEEXT value: $EXEEXT" 1>&2; return 1 ;;
|
---|
383 | esac
|
---|
384 |
|
---|
385 | base_names_=`find_exe_basenames_ $1` \
|
---|
386 | || { echo "$0 (exe_shim): skipping directory: $1" 1>&2; return 0; }
|
---|
387 |
|
---|
388 | if test -n "$base_names_"; then
|
---|
389 | for base_ in $base_names_; do
|
---|
390 | alias "$base_"="$base_$EXEEXT"
|
---|
391 | done
|
---|
392 | fi
|
---|
393 |
|
---|
394 | return 0
|
---|
395 | }
|
---|
396 |
|
---|
397 | # Use this function to prepend to PATH an absolute name for each
|
---|
398 | # specified, possibly-$initial_cwd_-relative, directory.
|
---|
399 | path_prepend_ ()
|
---|
400 | {
|
---|
401 | while test $# != 0; do
|
---|
402 | path_dir_=$1
|
---|
403 | case $path_dir_ in
|
---|
404 | '') fail_ "invalid path dir: '$1'";;
|
---|
405 | /*) abs_path_dir_=$path_dir_;;
|
---|
406 | *) abs_path_dir_=`cd "$initial_cwd_/$path_dir_" && echo "$PWD"` \
|
---|
407 | || fail_ "invalid path dir: $path_dir_";;
|
---|
408 | esac
|
---|
409 | case $abs_path_dir_ in
|
---|
410 | *:*) fail_ "invalid path dir: '$abs_path_dir_'";;
|
---|
411 | esac
|
---|
412 | PATH="$abs_path_dir_:$PATH"
|
---|
413 |
|
---|
414 | # Create an alias, FOO, for each FOO.exe in this directory.
|
---|
415 | create_exe_shims_ "$abs_path_dir_" \
|
---|
416 | || fail_ "something failed (above): $abs_path_dir_"
|
---|
417 | shift
|
---|
418 | done
|
---|
419 | export PATH
|
---|
420 | }
|
---|
421 |
|
---|
422 | setup_ ()
|
---|
423 | {
|
---|
424 | if test "$VERBOSE" = yes; then
|
---|
425 | # Test whether set -x may cause the selected shell to corrupt an
|
---|
426 | # application's stderr. Many do, including zsh-4.3.10 and the /bin/sh
|
---|
427 | # from SunOS 5.11, OpenBSD 4.7 and Irix 5.x and 6.5.
|
---|
428 | # If enabling verbose output this way would cause trouble, simply
|
---|
429 | # issue a warning and refrain.
|
---|
430 | if $gl_set_x_corrupts_stderr_; then
|
---|
431 | warn_ "using SHELL=$SHELL with 'set -x' corrupts stderr"
|
---|
432 | else
|
---|
433 | set -x
|
---|
434 | fi
|
---|
435 | fi
|
---|
436 |
|
---|
437 | initial_cwd_=$PWD
|
---|
438 | fail=0
|
---|
439 |
|
---|
440 | pfx_=`testdir_prefix_`
|
---|
441 | test_dir_=`mktempd_ "$initial_cwd_" "$pfx_-$ME_.XXXX"` \
|
---|
442 | || fail_ "failed to create temporary directory in $initial_cwd_"
|
---|
443 | cd "$test_dir_"
|
---|
444 |
|
---|
445 | # As autoconf-generated configure scripts do, ensure that IFS
|
---|
446 | # is defined initially, so that saving and restoring $IFS works.
|
---|
447 | gl_init_sh_nl_='
|
---|
448 | '
|
---|
449 | IFS=" "" $gl_init_sh_nl_"
|
---|
450 |
|
---|
451 | # This trap statement, along with a trap on 0 below, ensure that the
|
---|
452 | # temporary directory, $test_dir_, is removed upon exit as well as
|
---|
453 | # upon receipt of any of the listed signals.
|
---|
454 | for sig_ in 1 2 3 13 15; do
|
---|
455 | eval "trap 'Exit $(expr $sig_ + 128)' $sig_"
|
---|
456 | done
|
---|
457 | }
|
---|
458 |
|
---|
459 | # Create a temporary directory, much like mktemp -d does.
|
---|
460 | # Written by Jim Meyering.
|
---|
461 | #
|
---|
462 | # Usage: mktempd_ /tmp phoey.XXXXXXXXXX
|
---|
463 | #
|
---|
464 | # First, try to use the mktemp program.
|
---|
465 | # Failing that, we'll roll our own mktemp-like function:
|
---|
466 | # - try to get random bytes from /dev/urandom
|
---|
467 | # - failing that, generate output from a combination of quickly-varying
|
---|
468 | # sources and gzip. Ignore non-varying gzip header, and extract
|
---|
469 | # "random" bits from there.
|
---|
470 | # - given those bits, map to file-name bytes using tr, and try to create
|
---|
471 | # the desired directory.
|
---|
472 | # - make only $MAX_TRIES_ attempts
|
---|
473 |
|
---|
474 | # Helper function. Print $N pseudo-random bytes from a-zA-Z0-9.
|
---|
475 | rand_bytes_ ()
|
---|
476 | {
|
---|
477 | n_=$1
|
---|
478 |
|
---|
479 | # Maybe try openssl rand -base64 $n_prime_|tr '+/=\012' abcd first?
|
---|
480 | # But if they have openssl, they probably have mktemp, too.
|
---|
481 |
|
---|
482 | chars_=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
|
---|
483 | dev_rand_=/dev/urandom
|
---|
484 | if test -r "$dev_rand_"; then
|
---|
485 | # Note: 256-length($chars_) == 194; 3 copies of $chars_ is 186 + 8 = 194.
|
---|
486 | dd ibs=$n_ count=1 if=$dev_rand_ 2>/dev/null \
|
---|
487 | | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
|
---|
488 | return
|
---|
489 | fi
|
---|
490 |
|
---|
491 | n_plus_50_=`expr $n_ + 50`
|
---|
492 | cmds_='date; date +%N; free; who -a; w; ps auxww; ps ef; netstat -n'
|
---|
493 | data_=` (eval "$cmds_") 2>&1 | gzip `
|
---|
494 |
|
---|
495 | # Ensure that $data_ has length at least 50+$n_
|
---|
496 | while :; do
|
---|
497 | len_=`echo "$data_"|wc -c`
|
---|
498 | test $n_plus_50_ -le $len_ && break;
|
---|
499 | data_=` (echo "$data_"; eval "$cmds_") 2>&1 | gzip `
|
---|
500 | done
|
---|
501 |
|
---|
502 | echo "$data_" \
|
---|
503 | | dd bs=1 skip=50 count=$n_ 2>/dev/null \
|
---|
504 | | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
|
---|
505 | }
|
---|
506 |
|
---|
507 | mktempd_ ()
|
---|
508 | {
|
---|
509 | case $# in
|
---|
510 | 2);;
|
---|
511 | *) fail_ "Usage: mktempd_ DIR TEMPLATE";;
|
---|
512 | esac
|
---|
513 |
|
---|
514 | destdir_=$1
|
---|
515 | template_=$2
|
---|
516 |
|
---|
517 | MAX_TRIES_=4
|
---|
518 |
|
---|
519 | # Disallow any trailing slash on specified destdir:
|
---|
520 | # it would subvert the post-mktemp "case"-based destdir test.
|
---|
521 | case $destdir_ in
|
---|
522 | /) ;;
|
---|
523 | */) fail_ "invalid destination dir: remove trailing slash(es)";;
|
---|
524 | esac
|
---|
525 |
|
---|
526 | case $template_ in
|
---|
527 | *XXXX) ;;
|
---|
528 | *) fail_ \
|
---|
529 | "invalid template: $template_ (must have a suffix of at least 4 X's)";;
|
---|
530 | esac
|
---|
531 |
|
---|
532 | # First, try to use mktemp.
|
---|
533 | d=`unset TMPDIR; { mktemp -d -t -p "$destdir_" "$template_"; } 2>/dev/null` \
|
---|
534 | || fail=1
|
---|
535 |
|
---|
536 | # The resulting name must be in the specified directory.
|
---|
537 | case $d in "$destdir_"*);; *) fail=1;; esac
|
---|
538 |
|
---|
539 | # It must have created the directory.
|
---|
540 | test -d "$d" || fail=1
|
---|
541 |
|
---|
542 | # It must have 0700 permissions. Handle sticky "S" bits.
|
---|
543 | perms=`ls -dgo "$d" 2>/dev/null|tr S -` || fail=1
|
---|
544 | case $perms in drwx------*) ;; *) fail=1;; esac
|
---|
545 |
|
---|
546 | test $fail = 0 && {
|
---|
547 | echo "$d"
|
---|
548 | return
|
---|
549 | }
|
---|
550 |
|
---|
551 | # If we reach this point, we'll have to create a directory manually.
|
---|
552 |
|
---|
553 | # Get a copy of the template without its suffix of X's.
|
---|
554 | base_template_=`echo "$template_"|sed 's/XX*$//'`
|
---|
555 |
|
---|
556 | # Calculate how many X's we've just removed.
|
---|
557 | template_length_=`echo "$template_" | wc -c`
|
---|
558 | nx_=`echo "$base_template_" | wc -c`
|
---|
559 | nx_=`expr $template_length_ - $nx_`
|
---|
560 |
|
---|
561 | err_=
|
---|
562 | i_=1
|
---|
563 | while :; do
|
---|
564 | X_=`rand_bytes_ $nx_`
|
---|
565 | candidate_dir_="$destdir_/$base_template_$X_"
|
---|
566 | err_=`mkdir -m 0700 "$candidate_dir_" 2>&1` \
|
---|
567 | && { echo "$candidate_dir_"; return; }
|
---|
568 | test $MAX_TRIES_ -le $i_ && break;
|
---|
569 | i_=`expr $i_ + 1`
|
---|
570 | done
|
---|
571 | fail_ "$err_"
|
---|
572 | }
|
---|
573 |
|
---|
574 | # If you want to override the testdir_prefix_ function,
|
---|
575 | # or to add more utility functions, use this file.
|
---|
576 | test -f "$srcdir/init.cfg" \
|
---|
577 | && . "$srcdir/init.cfg"
|
---|
578 |
|
---|
579 | setup_ "$@"
|
---|
580 | # This trap is here, rather than in the setup_ function, because some
|
---|
581 | # shells run the exit trap at shell function exit, rather than script exit.
|
---|
582 | trap remove_tmp_ 0
|
---|