1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
########################################################################
# #
# This software is part of the ast package #
# Copyright (c) 1982-2012 AT&T Intellectual Property #
# and is licensed under the #
# Eclipse Public License, Version 1.0 #
# by AT&T Intellectual Property #
# #
# A copy of the License is available at #
# http://www.eclipse.org/org/documents/epl-v10.html #
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
# #
# Information and Software Systems Research #
# AT&T Research #
# Florham Park NJ #
# #
# David Korn <dgk@research.att.com> #
# #
########################################################################
# test the behavior of co-processes
function err_exit
{
print -u2 -n "\t"
print -u2 -r ${Command}[$1]: "${@:2}"
let Errors+=1
}
alias err_exit='err_exit $LINENO'
Command=${0##*/}
integer Errors=0
tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
trap "cd /; rm -rf $tmp" EXIT
if [[ -d /cygdrive ]]
then err_exit cygwin detected - coprocess tests disabled - enable at the risk of wedging your system
exit $((Errors))
fi
bintrue=$(whence -p true)
function ping # id
{
integer x=0
while ((x++ < 5))
do read -r
print -r "$1 $REPLY"
done
}
cat |&
print -p "hello"
read -p line
[[ $line == hello ]] || err_exit 'coprocessing fails'
exec 5>&p 6<&p
print -u5 'hello again' || err_exit 'write on u5 fails'
read -u6 line
[[ $line == 'hello again' ]] || err_exit 'coprocess after moving fds fails'
exec 5<&- 6<&-
wait $!
ping three |&
exec 3>&p
ping four |&
exec 4>&p
ping pipe |&
integer count
for i in three four pipe four pipe four three pipe pipe three pipe
do case $i in
three) to=-u3;;
four) to=-u4;;
pipe) to=-p;;
esac
(( count++ ))
print $to $i $count
done
while ((count > 0))
do (( count-- ))
read -p
set -- $REPLY
if [[ $1 != $2 ]]
then err_exit "$1 does not match $2"
fi
case $1 in
three) ;;
four) ;;
pipe) ;;
*) err_exit "unknown message +|$REPLY|+" ;;
esac
done
kill $(jobs -p) 2>/dev/null
file=$tmp/regress
cat > $file <<\!
/bin/cat |&
!
chmod +x $file
sleep 10 |&
$file 2> /dev/null || err_exit "parent coprocess prevents script coprocess"
exec 5<&p 6>&p
exec 5<&- 6>&-
kill $(jobs -p) 2>/dev/null
${SHELL-ksh} |&
cop=$!
exp=Done
print -p $'print hello | cat\nprint '$exp
read -t 5 -p
read -t 5 -p
got=$REPLY
if [[ $got != $exp ]]
then err_exit "${SHELL-ksh} coprocess io failed -- got '$got', expected '$exp'"
fi
exec 5<&p 6>&p
exec 5<&- 6>&-
{ sleep 4; kill $cop; } 2>/dev/null &
spy=$!
if wait $cop 2>/dev/null
then kill $spy 2>/dev/null
else err_exit "coprocess hung after 'exec 5<&p 6>&p; exec 5<&- 6>&-'"
fi
wait
{
echo line1 | grep 'line2'
echo line2 | grep 'line1'
} |&
SECONDS=0 count=0
while read -p -t 10 line
do ((count++))
done
if (( SECONDS > 8 ))
then err_exit "read -p hanging (SECONDS=$SECONDS count=$count)"
fi
wait $!
( sleep 3 |& sleep 1 && kill $!; sleep 1; sleep 3 |& sleep 1 && kill $! ) ||
err_exit "coprocess cleanup not working correctly"
{ : |& } 2>/dev/null ||
err_exit "subshell coprocess lingers in parent"
wait $!
unset N r e
integer N=5
e=12345
(
integer i
for ((i = 1; i <= N; i++))
do print $i |&
read -p r
print -n $r
wait $!
done
print
) 2>/dev/null | read -t 10 r
[[ $r == $e ]] || err_exit "coprocess timing bug -- expected $e, got '$r'"
r=
(
integer i
for ((i = 1; i <= N; i++))
do print $i |&
sleep 0.01
r=$r$(cat <&p)
wait $!
done
print $r
) 2>/dev/null | read -t 10 r
[[ $r == $e ]] || err_exit "coprocess command substitution bug -- expected $e, got '$r'"
(
/bin/cat |&
sleep 0.01
exec 6>&p
print -u6 ok
exec 6>&-
sleep 2
kill $! 2> /dev/null
) && err_exit 'coprocess with subshell would hang'
for sig in IOT ABRT
do if ( trap - $sig ) 2> /dev/null
then if [[ $( { sig=$sig $SHELL 2> /dev/null <<- '++EOF++'
cat |&
pid=$!
trap "print TRAP" $sig
(
sleep 2
kill -$sig $$
sleep 2
kill -$sig $$
kill $pid
sleep 2
kill $$
) &
while read -p || (($? > 256))
do :
done
++EOF++
} ) != $'TRAP\nTRAP' ]] 2> /dev/null
then err_exit 'traps when reading from coprocess not working'
fi
break
fi
done
trap 'sleep_pid=; kill $pid; err_exit "coprocess 1 hung"' TERM
{ sleep 5; kill $$; } &
sleep_pid=$!
builtin cat
cat |&
pid=$!
exec 5<&p 6>&p
exp=hi
print -u6 $exp; read -u5
[[ $REPLY == "$exp" ]] || err_exit "REPLY failed -- expected '$exp', got '$REPLY'"
exec 6>&-
wait $pid
trap - TERM
[[ $sleep_pid ]] && kill $sleep_pid
trap 'sleep_pid=; kill $pid; err_exit "coprocess 2 hung"' TERM
{ sleep 5; kill $$; } &
sleep_pid=$!
cat |&
pid=$!
print foo >&p 2> /dev/null || err_exit 'first write of foo to coprocess failed'
print foo >&p 2> /dev/null || err_exit 'second write of foo to coprocess failed'
kill $pid
wait $pid 2> /dev/null
trap - TERM
[[ $sleep_pid ]] && kill $sleep_pid
trap 'sleep_pid=; kill $pid; err_exit "coprocess 3 hung"' TERM
{ sleep 5; kill $$; } &
sleep_pid=$!
cat |&
pid=$!
print -p foo
print -p bar
read <&p || err_exit 'first read from coprocess failed'
[[ $REPLY == foo ]] || err_exit "first REPLY is $REPLY not foo"
read <&p || err_exit 'second read from coprocess failed'
[[ $REPLY == bar ]] || err_exit "second REPLY is $REPLY not bar"
kill $pid
wait $pid 2> /dev/null
trap - TERM
[[ $sleep_pid ]] && kill $sleep_pid
exp=ksh
got=$(print -r $'#00315
COATTRIBUTES=\'label=make \'
# @(#)$Id: libcoshell (AT&T Research) 2008-04-28 $
_COSHELL_msgfd=5
{ { (eval \'function fun { trap \":\" 0; return 1; }; trap \"exit 0\" 0; fun; exit 1\') && PATH= print -u$_COSHELL_msgfd ksh; } || { times && echo bsh >&$_COSHELL_msgfd; } || { echo osh >&$_COSHELL_msgfd; }; } >/dev/null 2>&1' | $SHELL 5>&1)
[[ $got == $exp ]] || err_exit "coshell(3) identification sequence failed -- expected '$exp', got '$got'"
function cop
{
read
print ok
}
exp=ok
cop |&
pid=$!
if print -p yo 2>/dev/null
then read -p got
else got='no coprocess'
fi
[[ $got == $exp ]] || err_exit "main coprocess main query failed -- expected $exp, got '$got'"
kill $pid 2>/dev/null
wait
cop |&
pid=$!
(
if print -p yo 2>/dev/null
then read -p got
else got='no coprocess'
fi
[[ $got == $exp ]] || err_exit "main coprocess subshell query failed -- expected $exp, got '$got'"
)
kill $pid 2>/dev/null
wait
exp='no coprocess'
(
cop |&
print $! > $tmp/pid
)
pid=$(<$tmp/pid)
if print -p yo 2>/dev/null
then read -p got
else got=$exp
fi
[[ $got == $exp ]] || err_exit "subshell coprocess main query failed -- expected $exp, got '$got'"
kill $pid 2>/dev/null
wait
(
cop |&
print $! > $tmp/pid
)
pid=$(<$tmp/pid)
(
if print -p yo 2>/dev/null
then read -p got
else got=$exp
fi
[[ $got == $exp ]] || err_exit "subshell coprocess subshell query failed -- expected $exp, got '$got'"
kill $pid 2>/dev/null
wait
)
function mypipe
{
read; read
print -r -- "$REPLY"
}
mypipe |&
print -p "hello"
z="$( $bintrue $($bintrue) )"
{ print -p "world";} 2> /dev/null
read -p
[[ $REPLY == world ]] || err_exit "expected 'world' got '$REPLY'"
kill $pid 2>/dev/null
wait
function cop
{
read
print ok
}
exp=ok
cop |&
pid=$!
(
if print -p yo 2>/dev/null
then read -p got
else got='no coprocess'
fi
[[ $got == $exp ]] || err_exit "main coprocess subshell query failed -- expected $exp, got '$got'"
)
kill $pid 2>/dev/null
wait
tee=$(whence -p tee)
ls -l |&
pid=$!
$tee -a /dev/null <&p > /dev/null
wait $pid
x=$?
[[ $x == 0 ]] || err_exit "coprocess exitval should be 0, not $x"
exit $((Errors<125?Errors:125))
|