xref: /titanic_44/usr/src/lib/libshell/common/tests/coprocess.sh (revision ee5416c9d7e449233197d5d20bc6b81e4ff091b2)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#           Copyright (c) 1982-2007 AT&T Knowledge Ventures            #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                      by AT&T Knowledge Ventures                      #
8#                                                                      #
9#                A copy of the License is available at                 #
10#            http://www.opensource.org/licenses/cpl1.0.txt             #
11#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                  David Korn <dgk@research.att.com>                   #
18#                                                                      #
19########################################################################
20# test the behavior of co-processes
21function err_exit
22{
23	print -u2 -n "\t"
24	print -u2 -r ${Command}[$1]: "${@:2}"
25	let Errors+=1
26}
27alias err_exit='err_exit $LINENO'
28
29Command=${0##*/}
30integer Errors=0
31
32if	[[ -d /cygdrive ]]
33then	err_exit cygwin detected - coprocess tests disabled - enable at the risk of wedging your system
34	exit $((Errors))
35fi
36
37function ping # id
38{
39	integer x=0
40	while ((x < 5))
41	do	read -r
42		print -r "$1 $REPLY"
43	done
44}
45
46cat |&
47print -p "hello"
48read -p line
49[[ $line == hello ]] || err_exit 'coprocessing fails'
50exec 5>&p 6<&p
51print -u5 'hello again' || err_exit 'write on u5 fails'
52read -u6 line
53[[ $line == 'hello again' ]] || err_exit 'coprocess after moving fds fails'
54exec 5<&- 6<&-
55
56ping three |&
57exec 3>&p
58ping four |&
59exec 4>&p
60ping pipe |&
61
62integer count
63for i in three four pipe four pipe four three pipe pipe three pipe
64do	case $i in
65	three)	to=-u3;;
66	four)	to=-u4;;
67	pipe)	to=-p;;
68	esac
69	count=count+1
70	print  $to $i $count
71done
72
73while	((count > 0))
74do	count=count-1
75	read -p
76#	print -r - "$REPLY"
77	set -- $REPLY
78	if	[[ $1 != $2 ]]
79	then	err_exit "$1 does not match 2"
80	fi
81	case $1 in
82	three);;
83	four) ;;
84	pipe) ;;
85	*)	err_exit "unknown message +|$REPLY|+"
86	esac
87done
88
89file=/tmp/regress$$
90trap "rm -f $file" EXIT
91cat > $file  <<\!
92/bin/cat |&
93!
94chmod +x $file
95$file 2> /dev/null  || err_exit "parent coprocess prevents script coprocess"
96exec 5<&p 6>&p
97exec 5<&- 6>&-
98${SHELL-ksh} |&
99print -p  $'print hello | cat\nprint Done'
100read -t 5 -p
101read -t 5 -p
102if	[[ $REPLY != Done ]]
103then	err_exit	"${SHELL-ksh} coprocess not working"
104fi
105exec 5<&p 6>&p
106exec 5<&- 6>&-
107count=0
108{
109echo line1 | grep 'line2'
110echo line2 | grep 'line1'
111} |&
112SECONDS=0
113while
114   read -p -t 10 line
115do
116   ((count = count + 1))
117   echo "Line $count: $line"
118done
119if	(( SECONDS > 8 ))
120then	err_exit 'read -p hanging'
121fi
122( sleep 3 |& sleep 1 && kill $!; sleep 1; sleep 3 |& sleep 1 && kill $! ) ||
123	err_exit "coprocess cleanup not working correctly"
124unset line
125(
126	integer n=0
127	while read  line
128	do	echo $line  |&
129		if	cat  <&p
130		then	((n++))
131			wait $!
132		fi
133	done > /dev/null 2>&1 <<-  !
134		line1
135		line2
136		line3
137		line4
138		line5
139		line6
140		line7
141	!
142	(( n==7 ))  && print ok
143)  | read -t 10 line
144if	[[ $line != ok ]]
145then	err_exit 'coprocess timing bug'
146fi
147(
148	/bin/cat |&
149	exec 6>&p
150	print -u6 ok
151	exec 6>&-
152	sleep 1
153	kill $! 2> /dev/null
154) && err_exit 'coprocess with subshell would hang'
155for sig in IOT ABRT
156do	if	( trap - $sig ) 2> /dev/null
157	then	if	[[ $(
158				cat |&
159				pid=$!
160				trap "print TRAP" $sig
161				(
162					sleep 2
163					kill -$sig $$
164					sleep 2
165					kill -$sig $$
166					kill $pid
167				) 2> /dev/null &
168				read -p
169			) != $'TRAP\nTRAP' ]]
170		then	err_exit 'traps when reading from coprocess not working'
171		fi
172		break
173	fi
174done
175
176trap 'sleep_pid=; kill $pid; err_exit "coprocess 1 hung"' TERM
177{ sleep 5; kill $$; } &
178sleep_pid=$!
179builtin cat
180cat |&
181pid=$!
182exec 5<&p 6>&p
183print -u6 hi; read -u5
184[[ $REPLY == hi ]] || err_exit 'REPLY is $REPLY not hi'
185exec 6>&-
186wait $pid
187trap - TERM
188[[ $sleep_pid ]] && kill $sleep_pid
189
190trap 'sleep_pid=; kill $pid; err_exit "coprocess 2 hung"' TERM
191{ sleep 5; kill $$; } &
192sleep_pid=$!
193cat |&
194pid=$!
195print foo >&p 2> /dev/null || err_exit 'first write of foo to coprocess failed'
196print foo >&p 2> /dev/null || err_exit 'second write of foo to coprocess failed'
197kill $pid
198wait $pid 2> /dev/null
199trap - TERM
200[[ $sleep_pid ]] && kill $sleep_pid
201
202trap 'sleep_pid=; kill $pid; err_exit "coprocess 3 hung"' TERM
203{ sleep 5; kill $$; } &
204sleep_pid=$!
205cat |&
206pid=$!
207print -p foo
208print -p bar
209read <&p || err_exit 'first read from coprocess failed'
210[[ $REPLY == foo ]] || err_exit "first REPLY is $REPLY not foo"
211read <&p || err_exit 'second read from coprocess failed'
212[[ $REPLY == bar ]] || err_exit "second REPLY is $REPLY not bar"
213kill $pid
214wait $pid 2> /dev/null
215trap - TERM
216[[ $sleep_pid ]] && kill $sleep_pid
217
218exit $((Errors))
219