1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2000,2008 Oracle.  All rights reserved.
4#
5# $Id: wrap.tcl,v 12.8 2008/01/08 20:58:53 bostic Exp $
6#
7# Sentinel file wrapper for multi-process tests.  This is designed to avoid a
8# set of nasty bugs, primarily on Windows, where pid reuse causes watch_procs
9# to sit around waiting for some random process that's not DB's and is not
10# exiting.
11
12source ./include.tcl
13source $test_path/testutils.tcl
14
15# Arguments:
16if { $argc < 3 } {
17	puts "FAIL: wrap.tcl: Usage: wrap.tcl script log scriptargs"
18	exit
19}
20
21set script [lindex $argv 0]
22set logfile [lindex $argv 1]
23set skip [lindex $argv 2]
24set args [lrange $argv 3 end]
25#
26# Account in args for SKIP command, or not.
27#
28if { $skip != "SKIP" } {
29	set args [lrange $argv 2 end]
30}
31
32# Create a sentinel file to mark our creation and signal that watch_procs
33# should look for us.
34set parentpid [pid]
35set parentsentinel $testdir/begin.$parentpid
36set f [open $parentsentinel w]
37close $f
38
39# Create a Tcl subprocess that will actually run the test.
40set t [open "|$tclsh_path >& $logfile" w]
41
42# Create a sentinel for the subprocess.
43set childpid [pid $t]
44puts "Script watcher process $parentpid launching $script process $childpid."
45set childsentinel $testdir/begin.$childpid
46set f [open $childsentinel w]
47close $f
48
49#
50# For the upgrade tests where a current release tclsh is starting up
51# a tclsh in an older release, we cannot tell it to source the current
52# test.tcl because new things may not exist in the old release.  So,
53# we need to skip that and the script we're running in the old
54# release will have to take care of itself.
55#
56if { $skip != "SKIP" } {
57	puts $t "source $test_path/test.tcl"
58}
59puts $t "set script $script"
60
61# Set up argv for the subprocess, since the args aren't passed in as true
62# arguments thanks to the pipe structure.
63puts $t "set argc [llength $args]"
64puts $t "set argv [list $args]"
65
66set scr $test_path/$script
67puts $t "set scr $scr"
68puts $t {set ret [catch { source $scr } result]}
69puts $t {if { [string length $result] > 0 } { puts $result }}
70puts $t {error_check_good "$scr run: $result: pid [pid]" $ret 0}
71
72# Close the pipe.  This will flush the above commands and actually run the
73# test, and will also return an error a la exec if anything bad happens
74# to the subprocess.  The magic here is that closing a pipe blocks
75# and waits for the exit of processes in the pipeline, at least according
76# to Ousterhout (p. 115).
77
78set ret [catch {close $t} res]
79
80# Write ending sentinel files--we're done.
81set f [open $testdir/end.$childpid w]
82close $f
83set f [open $testdir/end.$parentpid w]
84close $f
85
86error_check_good "Pipe close ($childpid: $script $argv: logfile $logfile)"\
87    $ret 0
88exit $ret
89