xref: /onnv-gate/usr/src/lib/libshell/common/tests/coprocess.sh (revision 8462:6e341f5569ba)
14887Schin########################################################################
24887Schin#                                                                      #
34887Schin#               This software is part of the ast package               #
4*8462SApril.Chin@Sun.COM#          Copyright (c) 1982-2008 AT&T Intellectual Property          #
54887Schin#                      and is licensed under the                       #
64887Schin#                  Common Public License, Version 1.0                  #
7*8462SApril.Chin@Sun.COM#                    by AT&T Intellectual Property                     #
84887Schin#                                                                      #
94887Schin#                A copy of the License is available at                 #
104887Schin#            http://www.opensource.org/licenses/cpl1.0.txt             #
114887Schin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
124887Schin#                                                                      #
134887Schin#              Information and Software Systems Research               #
144887Schin#                            AT&T Research                             #
154887Schin#                           Florham Park NJ                            #
164887Schin#                                                                      #
174887Schin#                  David Korn <dgk@research.att.com>                   #
184887Schin#                                                                      #
194887Schin########################################################################
204887Schin# test the behavior of co-processes
214887Schinfunction err_exit
224887Schin{
234887Schin	print -u2 -n "\t"
244887Schin	print -u2 -r ${Command}[$1]: "${@:2}"
254887Schin	let Errors+=1
264887Schin}
274887Schinalias err_exit='err_exit $LINENO'
284887Schin
294887SchinCommand=${0##*/}
304887Schininteger Errors=0
314887Schin
324887Schinif	[[ -d /cygdrive ]]
334887Schinthen	err_exit cygwin detected - coprocess tests disabled - enable at the risk of wedging your system
344887Schin	exit $((Errors))
354887Schinfi
364887Schin
374887Schinfunction ping # id
384887Schin{
394887Schin	integer x=0
40*8462SApril.Chin@Sun.COM	while ((x++ < 5))
414887Schin	do	read -r
424887Schin		print -r "$1 $REPLY"
434887Schin	done
444887Schin}
454887Schin
464887Schincat |&
474887Schinprint -p "hello"
484887Schinread -p line
494887Schin[[ $line == hello ]] || err_exit 'coprocessing fails'
504887Schinexec 5>&p 6<&p
514887Schinprint -u5 'hello again' || err_exit 'write on u5 fails'
524887Schinread -u6 line
534887Schin[[ $line == 'hello again' ]] || err_exit 'coprocess after moving fds fails'
544887Schinexec 5<&- 6<&-
55*8462SApril.Chin@Sun.COMwait $!
564887Schin
574887Schinping three |&
584887Schinexec 3>&p
594887Schinping four |&
604887Schinexec 4>&p
614887Schinping pipe |&
624887Schin
634887Schininteger count
644887Schinfor i in three four pipe four pipe four three pipe pipe three pipe
654887Schindo	case $i in
664887Schin	three)	to=-u3;;
674887Schin	four)	to=-u4;;
684887Schin	pipe)	to=-p;;
694887Schin	esac
70*8462SApril.Chin@Sun.COM	(( count++ ))
71*8462SApril.Chin@Sun.COM	print $to $i $count
724887Schindone
734887Schin
744887Schinwhile	((count > 0))
75*8462SApril.Chin@Sun.COMdo	(( count-- ))
764887Schin	read -p
774887Schin	set -- $REPLY
784887Schin	if	[[ $1 != $2 ]]
79*8462SApril.Chin@Sun.COM	then	err_exit "$1 does not match $2"
804887Schin	fi
814887Schin	case $1 in
82*8462SApril.Chin@Sun.COM	three)	;;
83*8462SApril.Chin@Sun.COM	four)	;;
84*8462SApril.Chin@Sun.COM	pipe)	;;
85*8462SApril.Chin@Sun.COM	*)	err_exit "unknown message +|$REPLY|+" ;;
864887Schin	esac
874887Schindone
88*8462SApril.Chin@Sun.COMkill $(jobs -p) 2>/dev/null
894887Schin
904887Schinfile=/tmp/regress$$
914887Schintrap "rm -f $file" EXIT
924887Schincat > $file  <<\!
934887Schin/bin/cat |&
944887Schin!
954887Schinchmod +x $file
96*8462SApril.Chin@Sun.COMsleep 10 |&
97*8462SApril.Chin@Sun.COM$file 2> /dev/null || err_exit "parent coprocess prevents script coprocess"
984887Schinexec 5<&p 6>&p
994887Schinexec 5<&- 6>&-
100*8462SApril.Chin@Sun.COMkill $(jobs -p) 2>/dev/null
101*8462SApril.Chin@Sun.COM
1024887Schin${SHELL-ksh} |&
1034887Schinprint -p  $'print hello | cat\nprint Done'
1044887Schinread -t 5 -p
1054887Schinread -t 5 -p
1064887Schinif	[[ $REPLY != Done ]]
107*8462SApril.Chin@Sun.COMthen	err_exit "${SHELL-ksh} coprocess not working"
1084887Schinfi
1094887Schinexec 5<&p 6>&p
1104887Schinexec 5<&- 6>&-
111*8462SApril.Chin@Sun.COMwait $!
112*8462SApril.Chin@Sun.COM
1134887Schin{
1144887Schinecho line1 | grep 'line2'
1154887Schinecho line2 | grep 'line1'
1164887Schin} |&
117*8462SApril.Chin@Sun.COMSECONDS=0 count=0
118*8462SApril.Chin@Sun.COMwhile	read -p -t 10 line
119*8462SApril.Chin@Sun.COMdo	((count++))
1204887Schindone
1214887Schinif	(( SECONDS > 8 ))
122*8462SApril.Chin@Sun.COMthen	err_exit "read -p hanging (SECONDS=$SECONDS count=$count)"
1234887Schinfi
124*8462SApril.Chin@Sun.COMwait $!
125*8462SApril.Chin@Sun.COM
1264887Schin( sleep 3 |& sleep 1 && kill $!; sleep 1; sleep 3 |& sleep 1 && kill $! ) ||
1274887Schin	err_exit "coprocess cleanup not working correctly"
128*8462SApril.Chin@Sun.COM{ : |& } 2>/dev/null ||
129*8462SApril.Chin@Sun.COM	err_exit "subshell coprocess lingers in parent"
130*8462SApril.Chin@Sun.COMwait $!
131*8462SApril.Chin@Sun.COM
132*8462SApril.Chin@Sun.COMunset N r e
133*8462SApril.Chin@Sun.COMinteger N=5
134*8462SApril.Chin@Sun.COMe=12345
1354887Schin(
136*8462SApril.Chin@Sun.COM	integer i
137*8462SApril.Chin@Sun.COM	for ((i = 1; i <= N; i++))
138*8462SApril.Chin@Sun.COM	do	print $i |&
139*8462SApril.Chin@Sun.COM		read -p r
140*8462SApril.Chin@Sun.COM		print -n $r
141*8462SApril.Chin@Sun.COM		wait $!
142*8462SApril.Chin@Sun.COM	done
143*8462SApril.Chin@Sun.COM	print
144*8462SApril.Chin@Sun.COM) 2>/dev/null | read -t 10 r
145*8462SApril.Chin@Sun.COM[[ $r == $e ]] || err_exit "coprocess timing bug -- expected $e, got '$r'"
146*8462SApril.Chin@Sun.COMr=
147*8462SApril.Chin@Sun.COM(
148*8462SApril.Chin@Sun.COM	integer i
149*8462SApril.Chin@Sun.COM	for ((i = 1; i <= N; i++))
150*8462SApril.Chin@Sun.COM	do	print $i |&
151*8462SApril.Chin@Sun.COM		sleep 0.01
152*8462SApril.Chin@Sun.COM		r=$r$(cat <&p)
153*8462SApril.Chin@Sun.COM		wait $!
154*8462SApril.Chin@Sun.COM	done
155*8462SApril.Chin@Sun.COM	print $r
156*8462SApril.Chin@Sun.COM) 2>/dev/null | read -t 10 r
157*8462SApril.Chin@Sun.COM[[ $r == $e ]] || err_exit "coprocess command substitution bug -- expected $e, got '$r'"
158*8462SApril.Chin@Sun.COM
1594887Schin(
1604887Schin	/bin/cat |&
161*8462SApril.Chin@Sun.COM	sleep 0.01
1624887Schin	exec 6>&p
1634887Schin	print -u6 ok
1644887Schin	exec 6>&-
1654887Schin	sleep 1
1664887Schin	kill $! 2> /dev/null
1674887Schin) && err_exit 'coprocess with subshell would hang'
1684887Schinfor sig in IOT ABRT
1694887Schindo	if	( trap - $sig ) 2> /dev/null
170*8462SApril.Chin@Sun.COM	then	if	[[ $( { sig=$sig $SHELL  2> /dev/null <<- '++EOF++'
1714887Schin				cat |&
1724887Schin				pid=$!
1734887Schin				trap "print TRAP" $sig
1744887Schin				(
1754887Schin					sleep 2
1764887Schin					kill -$sig $$
1774887Schin					sleep 2
1784887Schin					kill -$sig $$
1794887Schin					kill $pid
180*8462SApril.Chin@Sun.COM					sleep 2
181*8462SApril.Chin@Sun.COM					kill  $$
182*8462SApril.Chin@Sun.COM				) &
1834887Schin				read -p
184*8462SApril.Chin@Sun.COM			++EOF++
185*8462SApril.Chin@Sun.COM			} ) != $'TRAP\nTRAP' ]] 2> /dev/null
1864887Schin		then	err_exit 'traps when reading from coprocess not working'
1874887Schin		fi
1884887Schin		break
1894887Schin	fi
1904887Schindone
1914887Schin
1924887Schintrap 'sleep_pid=; kill $pid; err_exit "coprocess 1 hung"' TERM
1934887Schin{ sleep 5; kill $$; } &
1944887Schinsleep_pid=$!
1954887Schinbuiltin cat
1964887Schincat |&
1974887Schinpid=$!
1984887Schinexec 5<&p 6>&p
1994887Schinprint -u6 hi; read -u5
2004887Schin[[ $REPLY == hi ]] || err_exit 'REPLY is $REPLY not hi'
2014887Schinexec 6>&-
2024887Schinwait $pid
2034887Schintrap - TERM
2044887Schin[[ $sleep_pid ]] && kill $sleep_pid
2054887Schin
2064887Schintrap 'sleep_pid=; kill $pid; err_exit "coprocess 2 hung"' TERM
2074887Schin{ sleep 5; kill $$; } &
2084887Schinsleep_pid=$!
2094887Schincat |&
2104887Schinpid=$!
2114887Schinprint foo >&p 2> /dev/null || err_exit 'first write of foo to coprocess failed'
2124887Schinprint foo >&p 2> /dev/null || err_exit 'second write of foo to coprocess failed'
2134887Schinkill $pid
2144887Schinwait $pid 2> /dev/null
2154887Schintrap - TERM
2164887Schin[[ $sleep_pid ]] && kill $sleep_pid
2174887Schin
2184887Schintrap 'sleep_pid=; kill $pid; err_exit "coprocess 3 hung"' TERM
2194887Schin{ sleep 5; kill $$; } &
2204887Schinsleep_pid=$!
2214887Schincat |&
2224887Schinpid=$!
2234887Schinprint -p foo
2244887Schinprint -p bar
2254887Schinread <&p || err_exit 'first read from coprocess failed'
2264887Schin[[ $REPLY == foo ]] || err_exit "first REPLY is $REPLY not foo"
2274887Schinread <&p || err_exit 'second read from coprocess failed'
2284887Schin[[ $REPLY == bar ]] || err_exit "second REPLY is $REPLY not bar"
2294887Schinkill $pid
2304887Schinwait $pid 2> /dev/null
2314887Schintrap - TERM
2324887Schin[[ $sleep_pid ]] && kill $sleep_pid
2334887Schin
2344887Schinexit $((Errors))
235